Skip to content

Commit

Permalink
fix: patch file reference and navigate to items in ts plugin (#2174)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlyu123 authored Oct 6, 2023
1 parent 1d53bdc commit 62a1c94
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/typescript-plugin/src/language-service/file-references.ts
Original file line number Diff line number Diff line change
@@ -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);
};
}
4 changes: 4 additions & 0 deletions packages/typescript-plugin/src/language-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
};
}

0 comments on commit 62a1c94

Please sign in to comment.