From ae7d3eb88853f31b90e6b2c263e222a27a5f8fe2 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Thu, 14 Jun 2018 15:56:46 -0700 Subject: [PATCH] Add workaround for https://github.com/Microsoft/TypeScript/issues/24968 --- .../src/features/updatePathsOnRename.ts | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/extensions/typescript-language-features/src/features/updatePathsOnRename.ts b/extensions/typescript-language-features/src/features/updatePathsOnRename.ts index b19695f0afc..26c54230508 100644 --- a/extensions/typescript-language-features/src/features/updatePathsOnRename.ts +++ b/extensions/typescript-language-features/src/features/updatePathsOnRename.ts @@ -209,6 +209,7 @@ export class UpdateImportsOnFileRenameHandler { oldFile: string, newFile: string, ) { + const isDirectoryRename = fs.lstatSync(newFile).isDirectory(); await this.fileConfigurationManager.ensureConfigurationForDocument(document, undefined); const args: Proto.GetEditsForFileRenameRequestArgs = { @@ -221,7 +222,37 @@ export class UpdateImportsOnFileRenameHandler { return; } - return typeConverters.WorkspaceEdit.fromFromFileCodeEdits(this.client, response.body); + return typeConverters.WorkspaceEdit.fromFromFileCodeEdits(this.client, response.body.map((edit: Proto.FileCodeEdits) => this.fixEdit(edit, isDirectoryRename))); + } + + private fixEdit( + edit: Proto.FileCodeEdits, + isDirectoryRename: boolean + ): Proto.FileCodeEdits { + if (!isDirectoryRename || this.client.apiVersion.gte(API.v300)) { + return edit; + } + + // Workaround for https://github.com/Microsoft/TypeScript/issues/24968 + const textChanges = edit.textChanges.map((change): Proto.CodeEdit => { + const match = change.newText.match(/\/[^\/]+$/g); + if (!match) { + return change; + } + return { + newText: change.newText.slice(0, -match[0].length), + start: change.start, + end: { + line: change.end.line, + offset: change.end.offset - match[0].length + } + }; + }); + + return { + fileName: edit.fileName, + textChanges + }; } } -- GitLab