diff --git a/src/contextualHelp/contextualHelp.ts b/src/contextualHelp/contextualHelp.ts index 79de325..285cb45 100644 --- a/src/contextualHelp/contextualHelp.ts +++ b/src/contextualHelp/contextualHelp.ts @@ -53,8 +53,11 @@ export class ContextualHelp extends WebviewViewHost 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 @@ -64,10 +67,13 @@ export class ContextualHelp extends WebviewViewHost 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) { @@ -83,28 +89,41 @@ export class ContextualHelp extends WebviewViewHost 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) {