From f5059d4c9504efe2f66178ad9e6da41b55c0c6f2 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Sun, 25 Aug 2024 01:50:21 +0800 Subject: [PATCH] Update semantic.ts --- packages/typescript/lib/plugins/semantic.ts | 112 ++++++++++---------- 1 file changed, 58 insertions(+), 54 deletions(-) diff --git a/packages/typescript/lib/plugins/semantic.ts b/packages/typescript/lib/plugins/semantic.ts index 452362d..9de5361 100644 --- a/packages/typescript/lib/plugins/semantic.ts +++ b/packages/typescript/lib/plugins/semantic.ts @@ -175,6 +175,7 @@ export function create( } const { sys, languageServiceHost, uriConverter, getExtraServiceScript } = context.project.typescript; let languageService: ts.LanguageService; + let created: ReturnType | undefined; if (disableAutoImportCache) { languageService = ts.createLanguageService( languageServiceHost, @@ -182,65 +183,12 @@ export function create( ); } else { - const created = tsWithImportCache.createLanguageService( + created = tsWithImportCache.createLanguageService( ts, sys, languageServiceHost, proxiedHost => ts.createLanguageService(proxiedHost, getDocumentRegistry(ts, sys.useCaseSensitiveFileNames, languageServiceHost.getCurrentDirectory())) ); - if (created.setPreferences && context.env.getConfiguration) { - - updatePreferences(); - context.env.onDidChangeConfiguration?.(updatePreferences); - - async function updatePreferences() { - const preferences = await context.env.getConfiguration?.('typescript.preferences'); - if (preferences) { - created.setPreferences?.(preferences); - } - } - } - if (created.projectUpdated) { - - const sourceScriptNames = new Set(); - const normalizeFileName = sys.useCaseSensitiveFileNames - ? (id: string) => id - : (id: string) => id.toLowerCase(); - - updateSourceScriptFileNames(); - - context.env.onDidChangeWatchedFiles?.(params => { - const someFileCreateOrDeiete = params.changes.some(change => change.type !== 2 satisfies typeof FileChangeType.Changed); - if (someFileCreateOrDeiete) { - updateSourceScriptFileNames(); - } - for (const change of params.changes) { - const fileName = uriConverter.asFileName(URI.parse(change.uri)); - if (sourceScriptNames.has(normalizeFileName(fileName))) { - created.projectUpdated?.(languageServiceHost.getCurrentDirectory()); - } - } - }); - - function updateSourceScriptFileNames() { - sourceScriptNames.clear(); - for (const fileName of languageServiceHost.getScriptFileNames()) { - const maybeEmbeddedUri = ctx.fileNameToUri(fileName); - const decoded = context.decodeEmbeddedDocumentUri(maybeEmbeddedUri); - const uri = decoded ? decoded[0] : maybeEmbeddedUri; - const sourceScript = context.language.scripts.get(uri); - if (sourceScript?.generated) { - const tsCode = sourceScript.generated.languagePlugin.typescript?.getServiceScript(sourceScript.generated.root); - if (tsCode) { - sourceScriptNames.add(normalizeFileName(fileName)); - } - } - else if (sourceScript) { - sourceScriptNames.add(normalizeFileName(fileName)); - } - } - } - } languageService = created.languageService; } const ctx: SharedContext = { @@ -296,6 +244,62 @@ export function create( let formattingOptions: FormattingOptions | undefined; + if (created) { + if (created.setPreferences && context.env.getConfiguration) { + + updatePreferences(); + context.env.onDidChangeConfiguration?.(updatePreferences); + + async function updatePreferences() { + const preferences = await context.env.getConfiguration?.('typescript.preferences'); + if (preferences) { + created!.setPreferences?.(preferences); + } + } + } + if (created.projectUpdated) { + + const sourceScriptNames = new Set(); + const normalizeFileName = sys.useCaseSensitiveFileNames + ? (id: string) => id + : (id: string) => id.toLowerCase(); + + updateSourceScriptFileNames(); + + context.env.onDidChangeWatchedFiles?.(params => { + const someFileCreateOrDeiete = params.changes.some(change => change.type !== 2 satisfies typeof FileChangeType.Changed); + if (someFileCreateOrDeiete) { + updateSourceScriptFileNames(); + } + for (const change of params.changes) { + const fileName = uriConverter.asFileName(URI.parse(change.uri)); + if (sourceScriptNames.has(normalizeFileName(fileName))) { + created.projectUpdated?.(languageServiceHost.getCurrentDirectory()); + } + } + }); + + function updateSourceScriptFileNames() { + sourceScriptNames.clear(); + for (const fileName of languageServiceHost.getScriptFileNames()) { + const maybeEmbeddedUri = ctx.fileNameToUri(fileName); + const decoded = context.decodeEmbeddedDocumentUri(maybeEmbeddedUri); + const uri = decoded ? decoded[0] : maybeEmbeddedUri; + const sourceScript = context.language.scripts.get(uri); + if (sourceScript?.generated) { + const tsCode = sourceScript.generated.languagePlugin.typescript?.getServiceScript(sourceScript.generated.root); + if (tsCode) { + sourceScriptNames.add(normalizeFileName(fileName)); + } + } + else if (sourceScript) { + sourceScriptNames.add(normalizeFileName(fileName)); + } + } + } + } + } + return { provide: {