Skip to content

Commit

Permalink
perf(language-server): cache project info
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Mar 7, 2025
1 parent 9e967bf commit 55becc4
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/language-server/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ connection.onInitialize(params => {

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

server.fileWatcher.onDidChangeWatchedFiles(({ changes }) => {
for (const change of changes) {
const changeUri = URI.parse(change.uri);
if (tsconfigProjects.has(changeUri)) {
tsconfigProjects.get(changeUri)!.dispose();
tsconfigProjects.delete(changeUri);
file2ProjectInfo.clear();
}
}
});
Expand All @@ -44,13 +46,18 @@ connection.onInitialize(params => {
async getLanguageService(uri) {
if (uri.scheme === 'file' && options.typescript.tsserverRequestCommand) {
const fileName = uri.fsPath.replace(/\\/g, '/');
const projectInfo = await sendTsRequest<ts.server.protocol.ProjectInfo>(
ts.server.protocol.CommandTypes.ProjectInfo,
{
file: fileName,
needFileNameList: false,
} satisfies ts.server.protocol.ProjectInfoRequestArgs
);
let projectInfoPromise = file2ProjectInfo.get(fileName);
if (!projectInfoPromise) {
projectInfoPromise = sendTsRequest<ts.server.protocol.ProjectInfo>(
ts.server.protocol.CommandTypes.ProjectInfo,
{
file: fileName,
needFileNameList: false,
}
);
file2ProjectInfo.set(fileName, projectInfoPromise);
}
const projectInfo = await projectInfoPromise;
if (projectInfo) {
const { configFileName } = projectInfo;
let ls = tsconfigProjects.get(URI.file(configFileName));
Expand Down

0 comments on commit 55becc4

Please sign in to comment.