Skip to content

Commit

Permalink
fix: virtual file version override source file version lead to TS plu…
Browse files Browse the repository at this point in the history
…gin always read file text

close #2228
  • Loading branch information
johnsoncodehk committed Dec 19, 2022
1 parent 7d4c5a3 commit 7f050a1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
29 changes: 15 additions & 14 deletions packages/language-core/src/languageContext.ts
Expand Up @@ -30,9 +30,10 @@ export function createEmbeddedLanguageServiceHost(

const documentRegistry = createDocumentRegistry();
const ts = host.getTypeScriptModule();
const tsFileVersions = new Map<string, string>();
const scriptSnapshots = new Map<string, [string, ts.IScriptSnapshot]>();
const fileVersions = new Map<string, string>();
const sourceTsFileVersions = new Map<string, string>();
const sourceVueFileVersions = new Map<string, string>();
const virtualFileVersions = new Map<string, string>();
const _tsHost: Partial<ts.LanguageServiceHost> = {
fileExists: host.fileExists
? fileName => {
Expand Down Expand Up @@ -135,8 +136,8 @@ export function createEmbeddedLanguageServiceHost(
for (const [sourceFile, languageModule] of documentRegistry.getAll()) {
remainFileNames.delete(sourceFile.fileName);
const newVersion = host.getScriptVersion(sourceFile.fileName);
if (fileVersions.get(sourceFile.fileName) !== newVersion) {
fileVersions.set(sourceFile.fileName, newVersion);
if (sourceVueFileVersions.get(sourceFile.fileName) !== newVersion) {
sourceVueFileVersions.set(sourceFile.fileName, newVersion);
const snapshot = host.getScriptSnapshot(sourceFile.fileName);
if (snapshot) {
// update
Expand All @@ -163,7 +164,7 @@ export function createEmbeddedLanguageServiceHost(
for (const languageModule of languageModules) {
const sourceFile = languageModule.createSourceFile(fileName, snapshot);
if (sourceFile) {
fileVersions.set(sourceFile.fileName, host.getScriptVersion(fileName));
sourceVueFileVersions.set(sourceFile.fileName, host.getScriptVersion(fileName));
documentRegistry.set(fileName, reactive(sourceFile), languageModule);
remainFileNames.delete(fileName);
break;
Expand All @@ -173,34 +174,34 @@ export function createEmbeddedLanguageServiceHost(
}

// .ts / .js / .d.ts / .json ...
for (const [oldTsFileName, oldTsFileVersion] of [...tsFileVersions]) {
for (const [oldTsFileName, oldTsFileVersion] of [...sourceTsFileVersions]) {
const newVersion = host.getScriptVersion(oldTsFileName);
if (oldTsFileVersion !== newVersion) {
if (!remainFileNames.has(oldTsFileName) && !host.getScriptSnapshot(oldTsFileName)) {
// delete
tsFileVersions.delete(oldTsFileName);
sourceTsFileVersions.delete(oldTsFileName);
}
else {
// update
tsFileVersions.set(oldTsFileName, newVersion);
sourceTsFileVersions.set(oldTsFileName, newVersion);
}
tsFileUpdated = true;
}
}

for (const nowFileName of remainFileNames) {
if (!tsFileVersions.has(nowFileName)) {
if (!sourceTsFileVersions.has(nowFileName)) {
// add
const newVersion = host.getScriptVersion(nowFileName);
tsFileVersions.set(nowFileName, newVersion);
sourceTsFileVersions.set(nowFileName, newVersion);
tsFileUpdated = true;
}
}

for (const [sourceFile, languageModule, snapshot] of sourceFilesToUpdate) {

forEachEmbeddeds(sourceFile, embedded => {
fileVersions.delete(embedded.fileName);
virtualFileVersions.delete(embedded.fileName);
});

const oldScripts: Record<string, string> = {};
Expand Down Expand Up @@ -262,16 +263,16 @@ export function createEmbeddedLanguageServiceHost(
function getScriptVersion(fileName: string) {
let mapped = documentRegistry.fromEmbeddedFileName(fileName);
if (mapped) {
if (fileVersions.has(mapped.embedded.fileName)) {
return fileVersions.get(mapped.embedded.fileName)!;
if (virtualFileVersions.has(mapped.embedded.fileName)) {
return virtualFileVersions.get(mapped.embedded.fileName)!;
}
else {
let version = ts.sys?.createHash?.(mapped.embedded.text) ?? mapped.embedded.text;
if (host.isTsc) {
// fix https://github.com/johnsoncodehk/volar/issues/1082
version = host.getScriptVersion(mapped.sourceFile.fileName) + ':' + version;
}
fileVersions.set(mapped.embedded.fileName, version);
virtualFileVersions.set(mapped.embedded.fileName, version);
return version;
}
}
Expand Down
49 changes: 23 additions & 26 deletions vue-language-tools/vue-language-core/src/sourceFile.ts
Expand Up @@ -349,35 +349,19 @@ export class VueSourceFile implements SourceFile {
return blocks;
});

get kind(): EmbeddedFileKind {
return EmbeddedFileKind.TextFile;
}
kind = EmbeddedFileKind.TextFile;

get capabilities(): DocumentCapabilities {
return {
diagnostic: true,
foldingRange: true,
documentFormatting: true,
documentSymbol: true,
codeAction: true,
inlayHint: true,
};
}
capabilities: DocumentCapabilities = {
diagnostic: true,
foldingRange: true,
documentFormatting: true,
documentSymbol: true,
codeAction: true,
inlayHint: true,
};

get mappings(): Mapping<PositionCapabilities>[] {
return [{
sourceRange: [0, this._snapshot.value.getLength()],
generatedRange: [0, this._snapshot.value.getLength()],
data: {
hover: true,
references: true,
definition: true,
rename: true,
completion: true,
diagnostic: true,
semanticTokens: true,
},
}];
return this._mappings.value;
}

get text() {
Expand All @@ -398,6 +382,19 @@ export class VueSourceFile implements SourceFile {

// refs
_snapshot: Ref<ts.IScriptSnapshot>;
_mappings = computed<Mapping<PositionCapabilities>[]>(() => [{
sourceRange: [0, this._snapshot.value.getLength()],
generatedRange: [0, this._snapshot.value.getLength()],
data: {
hover: true,
references: true,
definition: true,
rename: true,
completion: true,
diagnostic: true,
semanticTokens: true,
},
}]);
_allEmbeddeds = ref<{
file: VueEmbeddedFile;
text: string;
Expand Down

0 comments on commit 7f050a1

Please sign in to comment.