Skip to content

Commit

Permalink
Fix file path rename for js/ts
Browse files Browse the repository at this point in the history
Fixes #51175
  • Loading branch information
mjbvz committed Sep 27, 2018
1 parent f94cfe6 commit a2d3c87
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions extensions/typescript-language-features/src/features/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@

import * as path from 'path';
import * as vscode from 'vscode';
import * as nls from 'vscode-nls';
import * as Proto from '../protocol';
import { ITypeScriptServiceClient, ServerResponse } from '../typescriptService';
import API from '../utils/api';
import * as typeConverters from '../utils/typeConverters';

const localize = nls.loadMessageBundle();


class TypeScriptRenameProvider implements vscode.RenameProvider {
public constructor(
private readonly client: ITypeScriptServiceClient
Expand All @@ -34,10 +30,6 @@ class TypeScriptRenameProvider implements vscode.RenameProvider {
return Promise.reject<vscode.Range>(renameInfo.localizedErrorMessage);
}

if (renameInfo.fileToRename && this.client.apiVersion.gte(API.v310) && this.client.apiVersion.lt(API.v320)) {
return Promise.reject<vscode.Range>(localize('fileToRename.notSupported', "Renaming paths is not supported in this version of TypeScript"));
}

if (this.client.apiVersion.gte(API.v310)) {
const triggerSpan = renameInfo.triggerSpan;
if (triggerSpan) {
Expand All @@ -64,14 +56,13 @@ class TypeScriptRenameProvider implements vscode.RenameProvider {
return Promise.reject<vscode.WorkspaceEdit>(renameInfo.localizedErrorMessage);
}

const edit = new vscode.WorkspaceEdit();

if (this.client.apiVersion.gte(API.v310)) {
if (renameInfo.fileToRename) {
this.renameFile(edit, renameInfo.fileToRename, newName);
return this.renameFile(renameInfo.fileToRename, newName);
}
}
this.updateLocs(edit, response.body.locs, newName);
return edit;
return this.updateLocs(response.body.locs, newName);
}

public async execRename(
Expand All @@ -94,10 +85,10 @@ class TypeScriptRenameProvider implements vscode.RenameProvider {
}

private updateLocs(
edit: vscode.WorkspaceEdit,
locations: ReadonlyArray<Proto.SpanGroup>,
newName: string
) {
const edit = new vscode.WorkspaceEdit();
for (const spanGroup of locations) {
const resource = this.client.toResource(spanGroup.file);
if (resource) {
Expand All @@ -110,10 +101,11 @@ class TypeScriptRenameProvider implements vscode.RenameProvider {
}

private renameFile(
edit: vscode.WorkspaceEdit,
fileToRename: string,
newName: string,
): vscode.WorkspaceEdit {
const edit = new vscode.WorkspaceEdit();

// Make sure we preserve file exension if none provided
if (!path.extname(newName)) {
newName += path.extname(fileToRename);
Expand Down

0 comments on commit a2d3c87

Please sign in to comment.