Skip to content

Commit 55becc4

Browse files
committed
perf(language-server): cache project info
1 parent 9e967bf commit 55becc4

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

packages/language-server/node.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ connection.onInitialize(params => {
2424

2525
const { typescript: ts } = loadTsdkByPath(options.typescript.tsdk, params.locale);
2626
const tsconfigProjects = createUriMap<LanguageService>();
27+
const file2ProjectInfo = new Map<string, Promise<ts.server.protocol.ProjectInfo | null>>();
2728

2829
server.fileWatcher.onDidChangeWatchedFiles(({ changes }) => {
2930
for (const change of changes) {
3031
const changeUri = URI.parse(change.uri);
3132
if (tsconfigProjects.has(changeUri)) {
3233
tsconfigProjects.get(changeUri)!.dispose();
3334
tsconfigProjects.delete(changeUri);
35+
file2ProjectInfo.clear();
3436
}
3537
}
3638
});
@@ -44,13 +46,18 @@ connection.onInitialize(params => {
4446
async getLanguageService(uri) {
4547
if (uri.scheme === 'file' && options.typescript.tsserverRequestCommand) {
4648
const fileName = uri.fsPath.replace(/\\/g, '/');
47-
const projectInfo = await sendTsRequest<ts.server.protocol.ProjectInfo>(
48-
ts.server.protocol.CommandTypes.ProjectInfo,
49-
{
50-
file: fileName,
51-
needFileNameList: false,
52-
} satisfies ts.server.protocol.ProjectInfoRequestArgs
53-
);
49+
let projectInfoPromise = file2ProjectInfo.get(fileName);
50+
if (!projectInfoPromise) {
51+
projectInfoPromise = sendTsRequest<ts.server.protocol.ProjectInfo>(
52+
ts.server.protocol.CommandTypes.ProjectInfo,
53+
{
54+
file: fileName,
55+
needFileNameList: false,
56+
}
57+
);
58+
file2ProjectInfo.set(fileName, projectInfoPromise);
59+
}
60+
const projectInfo = await projectInfoPromise;
5461
if (projectInfo) {
5562
const { configFileName } = projectInfo;
5663
let ls = tsconfigProjects.get(URI.file(configFileName));

0 commit comments

Comments
 (0)