diff --git a/packages/typescript-plugin/src/language-service/file-references.ts b/packages/typescript-plugin/src/language-service/file-references.ts new file mode 100644 index 000000000..78563f985 --- /dev/null +++ b/packages/typescript-plugin/src/language-service/file-references.ts @@ -0,0 +1,32 @@ +import type ts from 'typescript/lib/tsserverlibrary'; +import { isNotNullOrUndefined, isSvelteFilePath } from '../utils'; +import { SvelteSnapshotManager } from '../svelte-snapshots'; + +export function decorateFileReferences( + ls: ts.LanguageService, + snapshotManager: SvelteSnapshotManager +): void { + const getFileReferences = ls.getFileReferences; + ls.getFileReferences = (fileName: string) => { + const references = getFileReferences(fileName); + + return references + .map((ref) => { + if (!isSvelteFilePath(ref.fileName)) { + return ref; + } + + let textSpan = snapshotManager.get(ref.fileName)?.getOriginalTextSpan(ref.textSpan); + + if (!textSpan) { + return; + } + + return { + ...ref, + textSpan + }; + }) + .filter(isNotNullOrUndefined); + }; +} diff --git a/packages/typescript-plugin/src/language-service/index.ts b/packages/typescript-plugin/src/language-service/index.ts index 54ee4b4e4..a552f7379 100644 --- a/packages/typescript-plugin/src/language-service/index.ts +++ b/packages/typescript-plugin/src/language-service/index.ts @@ -14,6 +14,8 @@ import { decorateInlayHints } from './inlay-hints'; import { decorateRename } from './rename'; import { decorateUpdateImports } from './update-imports'; import { decorateLanguageServiceHost } from './host'; +import { decorateNavigateToItems } from './navigate-to-items'; +import { decorateFileReferences } from './file-references'; const patchedProject = new Set(); @@ -60,6 +62,8 @@ function decorateLanguageServiceInner( decorateCallHierarchy(ls, snapshotManager, typescript); decorateHover(ls, info, typescript, logger); decorateInlayHints(ls, info, typescript, logger); + decorateNavigateToItems(ls, snapshotManager); + decorateFileReferences(ls, snapshotManager); decorateDispose(ls, info.project, onDispose); return ls; } diff --git a/packages/typescript-plugin/src/language-service/navigate-to-items.ts b/packages/typescript-plugin/src/language-service/navigate-to-items.ts new file mode 100644 index 000000000..815c6e8b1 --- /dev/null +++ b/packages/typescript-plugin/src/language-service/navigate-to-items.ts @@ -0,0 +1,55 @@ +import type ts from 'typescript/lib/tsserverlibrary'; +import { isGeneratedSvelteComponentName, isNotNullOrUndefined, isSvelteFilePath } from '../utils'; +import { SvelteSnapshotManager } from '../svelte-snapshots'; + +export function decorateNavigateToItems( + ls: ts.LanguageService, + snapshotManager: SvelteSnapshotManager +): void { + const getNavigateToItems = ls.getNavigateToItems; + ls.getNavigateToItems = ( + searchValue: string, + maxResultCount?: number, + fileName?: string, + excludeDtsFiles?: boolean + ) => { + const navigationToItems = getNavigateToItems( + searchValue, + maxResultCount, + fileName, + excludeDtsFiles + ); + + return navigationToItems + .map((item) => { + if (!isSvelteFilePath(item.fileName)) { + return item; + } + + if ( + item.name.startsWith('__sveltets_') || + (item.name === 'render' && !item.containerName) + ) { + return; + } + + let textSpan = snapshotManager + .get(item.fileName) + ?.getOriginalTextSpan(item.textSpan); + + if (!textSpan) { + if (isGeneratedSvelteComponentName(item.name)) { + textSpan = { start: 0, length: 1 }; + } else { + return; + } + } + + return { + ...item, + textSpan + }; + }) + .filter(isNotNullOrUndefined); + }; +}