Skip to content

Commit

Permalink
Aggressively cache the inlay hints
Browse files Browse the repository at this point in the history
  • Loading branch information
jiribenes committed Nov 7, 2024
1 parent 52e9cb0 commit f715b94
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,16 @@ let debugOutputChannel = vscode.window.createOutputChannel("Effekt DEBUG");
function log(message: string) {
debugOutputChannel.appendLine(message);
}

interface InlayHintCache {
[uri: string]: vscode.InlayHint[];
}

const inlayHintCache: InlayHintCache = {};

class EffektCapturesProvider implements vscode.InlayHintsProvider {
public async provideInlayHints(document: vscode.TextDocument, range: vscode.Range): Promise<vscode.InlayHint[]> {
log("Inlay hints requested for: " + document.uri.toString() + " & range: " + JSON.stringify(range));

const hints: vscode.InlayHint[] = [];

try {
const result = await client.sendRequest(ExecuteCommandRequest.type, {
Expand All @@ -94,9 +99,10 @@ class EffektCapturesProvider implements vscode.InlayHintsProvider {

if (!result) {
log("No results returned.");
return hints;
return inlayHintCache[document.uri.toString()];
}

inlayHintCache[document.uri.toString()] = [];
for (const response of result) {
log("processing a single response: " + JSON.stringify(response))
if (response.location.uri.toString() === document.uri.toString()) {
Expand All @@ -106,16 +112,17 @@ class EffektCapturesProvider implements vscode.InlayHintsProvider {
hint.tooltip = undefined; // NOTE: We could add a tooltip here if we wanted one.
hint.paddingRight = true;
hint.paddingLeft = false;
hints.push(hint);
inlayHintCache[document.uri.toString()].push(hint);
}
}

} catch (error) {
log("Error during inlay hints request: " + JSON.stringify(error));
vscode.window.showErrorMessage("An error occurred while fetching inlay hints.");
inlayHintCache[document.uri.toString()] = [];
}

return hints;
return inlayHintCache[document.uri.toString()];
}
}

Expand Down

0 comments on commit f715b94

Please sign in to comment.