Skip to content

Commit 1c7ffd1

Browse files
authored
fix: cache completion item to avoid jitter (#6946)
Fixes #6913 Cache the completion item (LRU of 10) to avoid any jitter in completions
1 parent f2a7780 commit 1c7ffd1

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

frontend/src/core/codemirror/lsp/notebook-lsp.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
102102

103103
private static readonly SEEN_CELL_DOCUMENT_URIS = new Set<CellDocumentUri>();
104104

105+
/**
106+
* Cache of completion items to avoid jitter while typing in the same completion item
107+
*/
108+
private readonly completionItemCache = new LRUCache<
109+
string,
110+
Promise<LSP.CompletionItem>
111+
>(10);
112+
105113
constructor(
106114
client: ILanguageServerClient,
107115
initialSettings: Record<string, unknown>,
@@ -428,10 +436,19 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
428436
};
429437
}
430438

431-
completionItemResolve(
439+
async completionItemResolve(
432440
params: LSP.CompletionItem,
433441
): Promise<LSP.CompletionItem> {
434-
return this.client.completionItemResolve(params);
442+
// Used cached result to avoid jitter while typing in the same completion item
443+
const key = JSON.stringify(params);
444+
const cached = this.completionItemCache.get(key);
445+
if (cached) {
446+
return cached;
447+
}
448+
449+
const resolved = this.client.completionItemResolve(params);
450+
this.completionItemCache.set(key, resolved);
451+
return resolved;
435452
}
436453

437454
async textDocumentPrepareRename(

0 commit comments

Comments
 (0)