diff --git a/packages/typescript/lib/node/decorateProgram.ts b/packages/typescript/lib/node/decorateProgram.ts index fdd5354f..3893b199 100644 --- a/packages/typescript/lib/node/decorateProgram.ts +++ b/packages/typescript/lib/node/decorateProgram.ts @@ -1,6 +1,6 @@ import type { Language } from '@volar/language-core'; import type * as ts from 'typescript'; -import { notEmpty } from './utils'; +import { getServiceScript, notEmpty } from './utils'; import { transformDiagnostic } from './transform'; export function decorateProgram(language: Language, program: ts.Program) { @@ -11,6 +11,7 @@ export function decorateProgram(language: Language, program: ts.Program) { const getSyntacticDiagnostics = program.getSyntacticDiagnostics; const getSemanticDiagnostics = program.getSemanticDiagnostics; const getGlobalDiagnostics = program.getGlobalDiagnostics; + const getSourceFileByPath = program.getSourceFileByPath; // for tsc --noEmit --watch // @ts-ignore @@ -46,4 +47,17 @@ export function decorateProgram(language: Language, program: ts.Program) { .map(d => transformDiagnostic(language, d)) .filter(notEmpty); }; + + // fix https://github.com/vuejs/language-tools/issues/4099 + program.getSourceFileByPath = path => { + const sourceFile = getSourceFileByPath(path); + if (sourceFile) { + const [serviceScript, sourceScript] = getServiceScript(language, sourceFile.fileName); + if (serviceScript) { + sourceFile.text = sourceScript.snapshot.getText(0, sourceScript.snapshot.getLength()) + + sourceFile.text.substring(sourceScript.snapshot.getLength()); + } + } + return sourceFile; + }; } diff --git a/packages/typescript/lib/node/proxyCreateProgram.ts b/packages/typescript/lib/node/proxyCreateProgram.ts index 190232c3..b3f72f11 100644 --- a/packages/typescript/lib/node/proxyCreateProgram.ts +++ b/packages/typescript/lib/node/proxyCreateProgram.ts @@ -1,7 +1,7 @@ -import type * as ts from 'typescript'; -import { decorateProgram } from './decorateProgram'; import { LanguagePlugin, createLanguage } from '@volar/language-core'; +import type * as ts from 'typescript'; import { createResolveModuleName } from '../resolveModuleName'; +import { decorateProgram } from './decorateProgram'; export function proxyCreateProgram( ts: typeof import('typescript'), diff --git a/packages/typescript/lib/node/transform.ts b/packages/typescript/lib/node/transform.ts index bbed9f1f..626a451d 100644 --- a/packages/typescript/lib/node/transform.ts +++ b/packages/typescript/lib/node/transform.ts @@ -3,7 +3,6 @@ import type * as ts from 'typescript'; import { getServiceScript, notEmpty } from './utils'; const transformedDiagnostics = new WeakMap(); -const transformedSourceFiles = new WeakMap(); export function transformCallHierarchyItem(language: Language, item: ts.CallHierarchyItem, filter: (data: CodeInformation) => boolean): ts.CallHierarchyItem { const span = transformSpan(language, item.file, item.span, filter); @@ -39,7 +38,6 @@ export function transformDiagnostic(language: Language, ...diagnostic, start: sourceSpan.start, length: sourceSpan.length, - file: transformSourceFile(diagnostic.file, sourceScript.snapshot.getText(0, sourceScript.snapshot.getLength())), }); } } @@ -54,16 +52,6 @@ export function transformDiagnostic(language: Language, return transformedDiagnostics.get(diagnostic) as T | undefined; } -export function transformSourceFile(sourceFile: ts.SourceFile, sourceText: string): ts.SourceFile { - if (!transformedSourceFiles.has(sourceFile)) { - transformedSourceFiles.set(sourceFile, { - ...sourceFile, - text: sourceText, - }); - } - return transformedSourceFiles.get(sourceFile)!; -} - export function transformFileTextChanges(language: Language, changes: ts.FileTextChanges, filter: (data: CodeInformation) => boolean): ts.FileTextChanges | undefined { const [_, source] = getServiceScript(language, changes.fileName); if (source) {