From 1db960c626330206b7b29ae4cff127d3945fae8d Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 3 May 2022 21:54:18 +0200 Subject: [PATCH] more robust change of files --- packages/svelte-vscode/src/extension.ts | 25 ++++++++++--------- .../src/language-service/update-imports.ts | 15 +++-------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/packages/svelte-vscode/src/extension.ts b/packages/svelte-vscode/src/extension.ts index 9e9225a0e..40fdb689b 100644 --- a/packages/svelte-vscode/src/extension.ts +++ b/packages/svelte-vscode/src/extension.ts @@ -357,19 +357,20 @@ function addRenameFileListener(getLS: () => LanguageClient) { return; } - // If a file move/rename of a TS/JS file results in TS/JS file updates only, - // skip because the TypeScript LS will take care of it. - if ( - (oldUri.endsWith('.ts') || oldUri.endsWith('.js')) && - !edits.some((change) => change.textDocument.uri.endsWith('.svelte')) - ) { - return; - } - const workspaceEdit = new WorkspaceEdit(); // Renaming a file should only result in edits of existing files - edits.forEach((change) => + edits.forEach((change) => { + const isTsOrJsFile = + change.textDocument.uri.endsWith('.ts') || + change.textDocument.uri.endsWith('.js'); + change.edits.forEach((edit) => { + // If the moved/renamed file is a TS/JS file, skip all TS/JS updates + // because the TypeScript LS will take care of it. + if (isTsOrJsFile && !edit.newText.endsWith('.svelte')) { + return; + } + workspaceEdit.replace( Uri.parse(change.textDocument.uri), new Range( @@ -378,8 +379,8 @@ function addRenameFileListener(getLS: () => LanguageClient) { ), edit.newText ); - }) - ); + }); + }); workspace.applyEdit(workspaceEdit); } ); diff --git a/packages/typescript-plugin/src/language-service/update-imports.ts b/packages/typescript-plugin/src/language-service/update-imports.ts index 18a7a1e1c..e34d84e04 100644 --- a/packages/typescript-plugin/src/language-service/update-imports.ts +++ b/packages/typescript-plugin/src/language-service/update-imports.ts @@ -1,7 +1,7 @@ import type ts from 'typescript/lib/tsserverlibrary'; import { Logger } from '../logger'; import { SvelteSnapshotManager } from '../svelte-snapshots'; -import { isNotNullOrUndefined, isSvelteFilePath } from '../utils'; +import { isSvelteFilePath } from '../utils'; export function decorateUpdateImports( ls: ts.LanguageService, @@ -19,16 +19,9 @@ export function decorateUpdateImports( // If a file move/rename of a TS/JS file results a Svelte file change, // the Svelte extension will notice that, too, and adjusts the same imports. // This results in duplicate adjustments which can break imports in some cases. - // Therefore don't do any updates in this case and let the Svelte extension handle that. - const containsSvelteFile = renameLocations?.some((renameLocation) => { - return isSvelteFilePath(renameLocation.fileName); + // Therefore don't do any updates of Svelte files and let the Svelte extension handle that. + return renameLocations?.filter((renameLocation) => { + return !isSvelteFilePath(renameLocation.fileName); }); - if (containsSvelteFile) { - logger.debug( - 'File rename also touched Svelte file -> let Svelte extension handle that' - ); - return []; - } - return renameLocations; }; }