Skip to content

Commit

Permalink
refactor(typescript): simplify snapshot retrieval in language service…
Browse files Browse the repository at this point in the history
… plugins
  • Loading branch information
johnsoncodehk committed Dec 13, 2024
1 parent 15f8750 commit 404c5dc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function createAsyncLanguageServicePlugin(
if (extensions.some(ext => fileName.endsWith(ext))) {
return emptySnapshot;
}
if (info.project.getScriptInfo(fileName)?.isScriptOpen()) {
if (getScriptInfo(fileName)?.isScriptOpen()) {
return emptySnapshot;
}
}
Expand All @@ -51,7 +51,7 @@ export function createAsyncLanguageServicePlugin(
if (extensions.some(ext => fileName.endsWith(ext))) {
return 'initializing...';
}
if (info.project.getScriptInfo(fileName)?.isScriptOpen()) {
if (getScriptInfo(fileName)?.isScriptOpen()) {
return getScriptVersion(fileName) + ',initializing...';
}
}
Expand Down Expand Up @@ -92,17 +92,12 @@ export function createAsyncLanguageServicePlugin(
],
new FileMap(ts.sys.useCaseSensitiveFileNames),
fileName => {
let snapshot: ts.IScriptSnapshot | undefined;

try { // getSnapshot could be crashed if the file is too large
snapshot = info.project.getScriptInfo(fileName)?.getSnapshot();
if (!snapshot) {
// trigger projectService.getOrCreateScriptInfoNotOpenedByClient
info.project.getScriptVersion(fileName);
snapshot = info.project.getScriptInfo(fileName)?.getSnapshot();
}
} catch { }

let snapshot = getScriptInfo(fileName)?.getSnapshot();
if (!snapshot) {
// trigger projectService.getOrCreateScriptInfoNotOpenedByClient
info.project.getScriptVersion(fileName);
snapshot = getScriptInfo(fileName)?.getSnapshot();
}
if (snapshot) {
language.scripts.set(fileName, snapshot);
}
Expand All @@ -124,6 +119,13 @@ export function createAsyncLanguageServicePlugin(
}

return info.languageService;

function getScriptInfo(fileName: string) {
// getSnapshot could be crashed if the file is too large
try {
return info.project.getScriptInfo(fileName);
} catch { }
}
},
getExternalFiles(project, updateLevel = 0) {
if (
Expand Down
24 changes: 13 additions & 11 deletions packages/typescript/lib/quickstart/createLanguageServicePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,12 @@ export function createLanguageServicePlugin(
],
new FileMap(ts.sys.useCaseSensitiveFileNames),
fileName => {
let snapshot: ts.IScriptSnapshot | undefined;

try { // getSnapshot could be crashed if the file is too large
snapshot = info.project.getScriptInfo(fileName)?.getSnapshot();
if (!snapshot) {
// trigger projectService.getOrCreateScriptInfoNotOpenedByClient
info.project.getScriptVersion(fileName);
snapshot = info.project.getScriptInfo(fileName)?.getSnapshot();
}
} catch { }

let snapshot = getScriptInfo(fileName)?.getSnapshot();
if (!snapshot) {
// trigger projectService.getOrCreateScriptInfoNotOpenedByClient
info.project.getScriptVersion(fileName);
snapshot = getScriptInfo(fileName)?.getSnapshot();
}
if (snapshot) {
language.scripts.set(fileName, snapshot);
}
Expand All @@ -69,6 +64,13 @@ export function createLanguageServicePlugin(
}

return info.languageService;

function getScriptInfo(fileName: string) {
// getSnapshot could be crashed if the file is too large
try {
return info.project.getScriptInfo(fileName);
} catch { }
}
},
getExternalFiles(project, updateLevel = 0) {
if (
Expand Down

0 comments on commit 404c5dc

Please sign in to comment.