Skip to content

Commit

Permalink
wip auto-import-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlyu123 committed Dec 21, 2023
1 parent bc82064 commit 6752119
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"svelte-preprocess": "~5.1.0",
"svelte2tsx": "workspace:~",
"typescript": "^5.3.2",
"typescript-auto-import-cache": "^0.3.0",
"vscode-css-languageservice": "~6.2.10",
"vscode-html-languageservice": "~5.1.1",
"vscode-languageserver": "8.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export class LSAndTSDocResolver {
this.getCanonicalFileName = createGetCanonicalFileName(
(options?.tsSystem ?? ts.sys).useCaseSensitiveFileNames
);

configManager.onChange(() => {
this.configChanged = true;
});
}

/**
Expand All @@ -96,6 +100,8 @@ export class LSAndTSDocResolver {
private extendedConfigCache = new Map<string, ts.ExtendedConfigCacheEntry>();
private getCanonicalFileName: GetCanonicalFileName;

private configChanged = true;

private get lsDocumentContext(): LanguageServiceDocumentContext {
return {
ambientTypesSource: this.options?.isSvelteCheck ? 'svelte-check' : 'svelte2tsx',
Expand All @@ -121,6 +127,11 @@ export class LSAndTSDocResolver {
}> {
const { tsDoc, lsContainer, userPreferences } = await this.getLSAndTSDocWorker(document);

if (this.configChanged) {
this.configChanged = false;
lsContainer.setUserPreferences(userPreferences);
}

return { tsDoc, lang: lsContainer.getService(), userPreferences };
}

Expand Down
53 changes: 50 additions & 3 deletions packages/language-server/src/plugins/typescript/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
hasTsExtensions,
isSvelteFilePath
} from './utils';
import { createLanguageService as createLanguageServiceWithCache } from 'typescript-auto-import-cache';

export interface LanguageServiceContainer {
readonly tsconfigPath: string;
Expand All @@ -47,6 +48,8 @@ export interface LanguageServiceContainer {
*/
fileBelongsToProject(filePath: string, isNew: boolean): boolean;

setUserPreferences(preferences: ts.UserPreferences): void;

dispose(): void;
}

Expand Down Expand Up @@ -302,14 +305,21 @@ async function createLanguageService(
tsSystem.useCaseSensitiveFileNames
);

const languageService = ts.createLanguageService(host, documentRegistry);
const transformationConfig: SvelteSnapshotOptions = {
parse: svelteCompiler?.parse,
version: svelteCompiler?.VERSION,
transformOnTemplateError: docContext.transformOnTemplateError,
typingsNamespace: raw?.svelteOptions?.namespace || 'svelteHTML'
};

const serviceWithCache = createLanguageServiceWithCache(
ts as any,
tsSystemWithPackageJsonCache,
host,
() => ts.createLanguageService(host, documentRegistry)
);
const { languageService } = serviceWithCache;

docContext.globalSnapshotsManager.onChange(scheduleUpdate);

reduceLanguageServiceCapabilityIfFileSizeTooBig();
Expand All @@ -329,6 +339,7 @@ async function createLanguageService(
fileBelongsToProject,
snapshotManager,
invalidateModuleCache,
setUserPreferences,
dispose
};

Expand Down Expand Up @@ -368,6 +379,8 @@ async function createLanguageService(

if (!prevSnapshot) {
svelteModuleLoader.deleteUnresolvedResolutionsFromCache(filePath);
// @ts-expect-error
host?.getCachedExportInfoMap()?.clear();
}

const newSnapshot = DocumentSnapshot.fromDocument(document, transformationConfig);
Expand Down Expand Up @@ -443,9 +456,20 @@ async function createLanguageService(
function updateProjectFiles(): void {
projectVersion++;
dirty = true;
const projectFileCountBefore = snapshotManager.getProjectFileNames().length;
const projectFileBefore = snapshotManager.getProjectFileNames();
const projectFileCountBefore = projectFileBefore.length;
snapshotManager.updateProjectFiles();
const projectFileCountAfter = snapshotManager.getProjectFileNames().length;
const projectFileAfter = snapshotManager.getProjectFileNames();
const projectFileCountAfter = projectFileAfter.length;

const hasAddedOrRemoved =
projectFileCountAfter !== projectFileCountBefore ||
checkProjectFileUpdate(projectFileBefore, projectFileAfter);

if (hasAddedOrRemoved) {
// @ts-expect-error
host?.getCachedExportInfoMap()?.clear();
}

if (projectFileCountAfter <= projectFileCountBefore) {
return;
Expand All @@ -454,6 +478,25 @@ async function createLanguageService(
reduceLanguageServiceCapabilityIfFileSizeTooBig();
}

function checkProjectFileUpdate(oldFiles: string[], newFiles: string[]) {
const oldSet = new Set(oldFiles);
const newSet = new Set(newFiles);

for (const file of oldSet) {
if (!newSet.has(file)) {
return true;
}
}

for (const file of newSet) {
if (!oldSet.has(file)) {
return true;
}
}

return false;
}

function getScriptFileNames() {
const projectFiles = languageServiceReducedMode
? []
Expand Down Expand Up @@ -645,6 +688,10 @@ async function createLanguageService(
}
}

function setUserPreferences(userPreferences: ts.UserPreferences) {
serviceWithCache.setPreferences?.(userPreferences);
}

function dispose() {
languageService.dispose();
snapshotManager.dispose();
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6752119

Please sign in to comment.