Skip to content

Commit

Permalink
fix(typescript): do not reuse snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Mar 28, 2024
1 parent 044667b commit c579f98
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
4 changes: 2 additions & 2 deletions packages/typescript/lib/plugins/docCommentTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function create(ts: typeof import('typescript')): vscode.LanguageServiceP
return {
name: 'typescript-doc-comment-template',
triggerCharacters: ['*'],
create(context): vscode.LanguageServicePluginInstance {
create(): vscode.LanguageServicePluginInstance {

return {

Expand All @@ -27,7 +27,7 @@ export function create(ts: typeof import('typescript')): vscode.LanguageServiceP
return;
}

const { languageService, fileName } = getLanguageServiceByDocument(context, ts, document);
const { languageService, fileName } = getLanguageServiceByDocument(ts, document);
const offset = document.offsetAt(position);
const docCommentTemplate = languageService.getDocCommentTemplateAtPosition(fileName, offset);
if (!docCommentTemplate) {
Expand Down
37 changes: 16 additions & 21 deletions packages/typescript/lib/plugins/syntactic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function create(
&& lastChange.text.endsWith('>')
&& await isAutoClosingTagsEnabled(document, context)
) {
const { languageService, fileName } = getLanguageServiceByDocument(context, ts, document);
const { languageService, fileName } = getLanguageServiceByDocument(ts, document);
const close = languageService.getJsxClosingTagAtPosition(fileName, document.offsetAt(position));
if (close) {
return '$0' + close.newText;
Expand All @@ -56,7 +56,7 @@ export function create(
return;
}

const { languageService, fileName } = getLanguageServiceByDocument(context, ts, document);
const { languageService, fileName } = getLanguageServiceByDocument(ts, document);
const outliningSpans = safeCall(() => languageService.getOutliningSpans(fileName));
if (!outliningSpans) {
return [];
Expand All @@ -70,7 +70,7 @@ export function create(
return;
}

const { languageService, fileName } = getLanguageServiceByDocument(context, ts, document);
const { languageService, fileName } = getLanguageServiceByDocument(ts, document);
const barItems = safeCall(() => languageService.getNavigationTree(fileName));
if (!barItems) {
return [];
Expand All @@ -97,7 +97,7 @@ export function create(
if (codeOptions) {
tsOptions.baseIndentSize = codeOptions.initialIndentLevel * options.tabSize;
}
const { languageService, fileName } = getLanguageServiceByDocument(context, ts, document);
const { languageService, fileName } = getLanguageServiceByDocument(ts, document);
const scriptEdits = range
? safeCall(() => languageService.getFormattingEditsForRange(
fileName,
Expand Down Expand Up @@ -126,7 +126,7 @@ export function create(
if (codeOptions) {
tsOptions.baseIndentSize = codeOptions.initialIndentLevel * options.tabSize;
}
const { languageService, fileName } = getLanguageServiceByDocument(context, ts, document);
const { languageService, fileName } = getLanguageServiceByDocument(ts, document);
const scriptEdits = safeCall(() => languageService.getFormattingEditsAfterKeystroke(fileName, document.offsetAt(position), key, tsOptions));
if (!scriptEdits) {
return [];
Expand All @@ -138,21 +138,16 @@ export function create(
};
}

export function getLanguageServiceByDocument(context: ServiceContext, ts: typeof import('typescript'), document: TextDocument) {
const decoded = context.decodeEmbeddedDocumentUri(document.uri);
if (decoded) {
const embeddedCode = context.language.scripts
.get(decoded[0])?.generated?.embeddedCodes
.get(decoded[1]);
if (embeddedCode) {
return getLanguageService(ts, embeddedCode.snapshot, document.languageId, true);
}
}
else {
const sourceScript = context.language.scripts.get(document.uri);
if (sourceScript) {
return getLanguageService(ts, sourceScript.snapshot, document.languageId, true);
}
import type * as ts from 'typescript';

const snapshots = new WeakMap<TextDocument, [number, ts.IScriptSnapshot]>();

export function getLanguageServiceByDocument(ts: typeof import('typescript'), document: TextDocument) {
let cache = snapshots.get(document);
if (!cache || cache[0] !== document.version) {
const snapshot = ts.ScriptSnapshot.fromString(document.getText());
cache = [document.version, snapshot];
snapshots.set(document, cache);
}
throw new Error(`No language service for ${document.uri}`);
return getLanguageService(ts, cache[1], document.languageId, true);
}

0 comments on commit c579f98

Please sign in to comment.