diff --git a/packages/typescript-plugin/src/language-service/index.ts b/packages/typescript-plugin/src/language-service/index.ts index a552f7379..98b3af210 100644 --- a/packages/typescript-plugin/src/language-service/index.ts +++ b/packages/typescript-plugin/src/language-service/index.ts @@ -16,6 +16,7 @@ import { decorateUpdateImports } from './update-imports'; import { decorateLanguageServiceHost } from './host'; import { decorateNavigateToItems } from './navigate-to-items'; import { decorateFileReferences } from './file-references'; +import { decorateMoveToRefactoringFileSuggestions } from './move-to-file'; const patchedProject = new Set(); @@ -64,6 +65,7 @@ function decorateLanguageServiceInner( decorateInlayHints(ls, info, typescript, logger); decorateNavigateToItems(ls, snapshotManager); decorateFileReferences(ls, snapshotManager); + decorateMoveToRefactoringFileSuggestions(ls); decorateDispose(ls, info.project, onDispose); return ls; } diff --git a/packages/typescript-plugin/src/language-service/move-to-file.ts b/packages/typescript-plugin/src/language-service/move-to-file.ts new file mode 100644 index 000000000..858ecd66a --- /dev/null +++ b/packages/typescript-plugin/src/language-service/move-to-file.ts @@ -0,0 +1,43 @@ +import type ts from 'typescript/lib/tsserverlibrary'; + +export function decorateMoveToRefactoringFileSuggestions(ls: ts.LanguageService): void { + const getMoveToRefactoringFileSuggestions = ls.getMoveToRefactoringFileSuggestions; + + ls.getMoveToRefactoringFileSuggestions = ( + fileName, + positionOrRange, + preferences, + triggerReason, + kind + ) => { + const program = ls.getProgram(); + + if (!program) { + return getMoveToRefactoringFileSuggestions( + fileName, + positionOrRange, + preferences, + triggerReason, + kind + ); + } + + const getSourceFiles = program.getSourceFiles; + try { + // typescript currently only allows js/ts files to be moved to. + // Once there isn't a restriction anymore, we can remove this. + program.getSourceFiles = () => + getSourceFiles().filter((file) => !file.fileName.endsWith('.svelte')); + + return getMoveToRefactoringFileSuggestions( + fileName, + positionOrRange, + preferences, + triggerReason, + kind + ); + } finally { + program.getSourceFiles = getSourceFiles; + } + }; +}