From 177b9ed985b7d6fbde7efb0454e17b1d215d38de Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Sun, 25 Aug 2024 01:50:59 +0800 Subject: [PATCH] Update semantic.ts --- packages/typescript/lib/plugins/semantic.ts | 81 ++++++++++----------- 1 file changed, 40 insertions(+), 41 deletions(-) diff --git a/packages/typescript/lib/plugins/semantic.ts b/packages/typescript/lib/plugins/semantic.ts index 9de5361..9facac5 100644 --- a/packages/typescript/lib/plugins/semantic.ts +++ b/packages/typescript/lib/plugins/semantic.ts @@ -244,58 +244,57 @@ export function create( let formattingOptions: FormattingOptions | undefined; - if (created) { - if (created.setPreferences && context.env.getConfiguration) { + if (created?.setPreferences && context.env.getConfiguration) { - updatePreferences(); - context.env.onDidChangeConfiguration?.(updatePreferences); + updatePreferences(); + context.env.onDidChangeConfiguration?.(updatePreferences); - async function updatePreferences() { - const preferences = await context.env.getConfiguration?.('typescript.preferences'); - if (preferences) { - created!.setPreferences?.(preferences); - } + 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(); + if (created?.projectUpdated) { - updateSourceScriptFileNames(); + const sourceScriptNames = new Set(); + const normalizeFileName = sys.useCaseSensitiveFileNames + ? (id: string) => id + : (id: string) => id.toLowerCase(); - 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()); - } + 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) { + } + }); + + 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)); + } } } }