Skip to content

Commit

Permalink
fix: workaround for move to file refactor restriction (#2227)
Browse files Browse the repository at this point in the history
prevent Svelte files from showing up in the suggestions until TypeScript allows this
  • Loading branch information
jasonlyu123 authored Dec 12, 2023
1 parent a2f86e1 commit 3f7f27b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/typescript-plugin/src/language-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();

Expand Down Expand Up @@ -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;
}
Expand Down
43 changes: 43 additions & 0 deletions packages/typescript-plugin/src/language-service/move-to-file.ts
Original file line number Diff line number Diff line change
@@ -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;
}
};
}

0 comments on commit 3f7f27b

Please sign in to comment.