From 33be3bc48e99b2f878272ea76533923e53932a40 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Sun, 25 Aug 2024 02:03:39 +0800 Subject: [PATCH] feat(typescript): add option to disable auto import cache (#105) --- packages/typescript/lib/plugins/semantic.ts | 32 ++++++++++++++------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/typescript/lib/plugins/semantic.ts b/packages/typescript/lib/plugins/semantic.ts index 0e5e543..9facac5 100644 --- a/packages/typescript/lib/plugins/semantic.ts +++ b/packages/typescript/lib/plugins/semantic.ts @@ -84,6 +84,7 @@ function getDocumentRegistry(ts: typeof import('typescript'), useCaseSensitiveFi export function create( ts: typeof import('typescript'), { + disableAutoImportCache = false, isValidationEnabled = async (document, context) => { return await context.env.getConfiguration?.(getConfigTitle(document) + '.validate.enable') ?? true; }, @@ -91,6 +92,7 @@ export function create( return await context.env.getConfiguration?.(getConfigTitle(document) + '.suggest.enabled') ?? true; }, }: { + disableAutoImportCache?: boolean; isValidationEnabled?(document: TextDocument, context: LanguageServiceContext): ProviderResult; isSuggestionsEnabled?(document: TextDocument, context: LanguageServiceContext): ProviderResult; } = {} @@ -172,13 +174,23 @@ export function create( return {}; } const { sys, languageServiceHost, uriConverter, getExtraServiceScript } = context.project.typescript; - const created = tsWithImportCache.createLanguageService( - ts, - sys, - languageServiceHost, - proxiedHost => ts.createLanguageService(proxiedHost, getDocumentRegistry(ts, sys.useCaseSensitiveFileNames, languageServiceHost.getCurrentDirectory())) - ); - const { languageService } = created; + let languageService: ts.LanguageService; + let created: ReturnType | undefined; + if (disableAutoImportCache) { + languageService = ts.createLanguageService( + languageServiceHost, + getDocumentRegistry(ts, sys.useCaseSensitiveFileNames, languageServiceHost.getCurrentDirectory()) + ); + } + else { + created = tsWithImportCache.createLanguageService( + ts, + sys, + languageServiceHost, + proxiedHost => ts.createLanguageService(proxiedHost, getDocumentRegistry(ts, sys.useCaseSensitiveFileNames, languageServiceHost.getCurrentDirectory())) + ); + languageService = created.languageService; + } const ctx: SharedContext = { ...context, languageServiceHost, @@ -232,7 +244,7 @@ export function create( let formattingOptions: FormattingOptions | undefined; - if (created.setPreferences && context.env.getConfiguration) { + if (created?.setPreferences && context.env.getConfiguration) { updatePreferences(); context.env.onDidChangeConfiguration?.(updatePreferences); @@ -240,12 +252,12 @@ export function create( async function updatePreferences() { const preferences = await context.env.getConfiguration?.('typescript.preferences'); if (preferences) { - created.setPreferences?.(preferences); + created!.setPreferences?.(preferences); } } } - if (created.projectUpdated) { + if (created?.projectUpdated) { const sourceScriptNames = new Set(); const normalizeFileName = sys.useCaseSensitiveFileNames