Skip to content

Commit

Permalink
more robust change of files
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon committed May 3, 2022
1 parent 840b213 commit 1db960c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
25 changes: 13 additions & 12 deletions packages/svelte-vscode/src/extension.ts
Expand Up @@ -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(
Expand All @@ -378,8 +379,8 @@ function addRenameFileListener(getLS: () => LanguageClient) {
),
edit.newText
);
})
);
});
});
workspace.applyEdit(workspaceEdit);
}
);
Expand Down
15 changes: 4 additions & 11 deletions 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,
Expand All @@ -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;
};
}

0 comments on commit 1db960c

Please sign in to comment.