Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce unnecessary inspects for same word #87

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading