diff --git a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts index 541898d77..b802f0063 100644 --- a/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts +++ b/packages/language-server/src/plugins/typescript/features/CompletionProvider.ts @@ -284,8 +284,14 @@ export class CompletionsProviderImpl implements CompletionsProvider .svelte-kit/types/foo/$types.d.ts const routesFolder = document.config?.kit?.files?.routes || 'src/routes'; - const relativeFilePath = filePath.split(routesFolder)[1]?.slice(1); - if (relativeFilePath) { + const relativeFileName = filePath.split(routesFolder)[1]?.slice(1); + if (relativeFileName) { + const relativePath = + dirname(relativeFileName) === '.' ? '' : `${dirname(relativeFileName)}/`; + const modifiedSource = + $typeImport.data.source.split('.svelte-kit/types')[0] + + // note the missing .d.ts at the end - TS wants it that way for some reason + `.svelte-kit/types/${routesFolder}/${relativePath}$types`; completionItems.push({ ...$typeImport, // Ensure it's sorted above the other imports @@ -295,12 +301,7 @@ export class CompletionsProviderImpl implements CompletionsProvider { - return ( - !isSvelteFilePath(renameLocation.fileName) && - !renameLocation.textChanges.some((change) => change.newText.endsWith('.svelte')) - ); - }); + return renameLocations + ?.filter((renameLocation) => { + // 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 or race conditions with conflicting text spans + // which can break imports in some cases. + // Therefore don't do any updates of Svelte files and and also no updates of mixed TS files + // and let the Svelte extension handle that. + return ( + !isSvelteFilePath(renameLocation.fileName) && + !renameLocation.textChanges.some((change) => change.newText.endsWith('.svelte')) + ); + }) + .map((renameLocation) => { + if (path.basename(renameLocation.fileName).startsWith('+')) { + // Filter out changes to './$type' imports for Kit route files, + // you'll likely want these to stay as-is + renameLocation.textChanges = renameLocation.textChanges.filter((change) => { + return !change.newText.includes('.svelte-kit/types/'); + }); + } + return renameLocation; + }); }; }