Skip to content

Commit

Permalink
Reduce unnecessary inspects for same word (#87)
Browse files Browse the repository at this point in the history
* Reduce unnecessary inspects for same word

* Throttling requests
  • Loading branch information
DonJayamanne authored Apr 4, 2024
1 parent d075ebd commit 739a5f1
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/contextualHelp/contextualHelp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ export class ContextualHelp extends WebviewViewHost<MessageMapping> implements v
token: vscode.CancellationTokenSource;
code: string;
cursor_pos: number;
lineNumber: number;
word: string;
wordAtPos: string;
document: vscode.TextDocument;
timer?: NodeJS.Timeout;
};
public showHelp(editor: vscode.TextEditor) {
// Code should be the entire cell
Expand All @@ -64,10 +67,13 @@ export class ContextualHelp extends WebviewViewHost<MessageMapping> implements v
const cursor_pos = editor.document.offsetAt(editor.selection.active);

// Word under cursor can be computed from the line
const lineNumber = editor.selection.active.line;
const line = editor.document.lineAt(editor.selection.active.line).text;
// Find first quote, parenthesis or space to the left
let start = editor.selection.active.character;
let end = editor.selection.active.character + 1;
const wordPos = editor.document.getWordRangeAtPosition(editor.selection.active);
const wordAtPos = wordPos ? editor.document.getText(wordPos) : '';
let startFound = false;
let endFound = false;
while (!startFound || !endFound) {
Expand All @@ -83,28 +89,41 @@ export class ContextualHelp extends WebviewViewHost<MessageMapping> implements v
}
}
const word = line.slice(start, end);
if (this.lastHelpRequest?.token) {
this.lastHelpRequest.token.cancel();
}
if (
this.lastHelpRequest?.code === code &&
this.lastHelpRequest?.cursor_pos === cursor_pos &&
this.lastHelpRequest?.word === word
(this.lastHelpRequest?.cursor_pos === cursor_pos || this.lastHelpRequest?.lineNumber === lineNumber) &&
this.lastHelpRequest?.word === word &&
this.lastHelpRequest?.wordAtPos === wordAtPos
) {
return;
}
if (this.lastHelpRequest?.token) {
this.lastHelpRequest.token.cancel();
}
if (this.lastHelpRequest?.timer) {
clearTimeout(this.lastHelpRequest.timer);
}
const token = new vscode.CancellationTokenSource();
// lets wait a while before showing the help
// We need to wait for the user to stop typing before we show the help
// Or possible user is tabbing through cells/files, etc
const timer = setTimeout(() => {
// Make our inspect request
this.inspect(code, cursor_pos, word, editor.document, token.token).finally(() => {
token.dispose();
});
}, 300);

this.lastHelpRequest = {
code,
cursor_pos,
word,
lineNumber,
wordAtPos,
document: editor.document,
token
token,
timer
};
// Make our inspect request
this.inspect(code, cursor_pos, word, editor.document, token.token).finally(() => {
token.dispose();
});
}

public async load(codeWebview: vscode.WebviewView) {
Expand Down

0 comments on commit 739a5f1

Please sign in to comment.