Skip to content

Commit b37dca6

Browse files
authored
Merge pull request #507 from krassowski/completion-and-signature-invalidation
Completion and signature invalidation
2 parents 8b86b1f + 7ce5317 commit b37dca6

File tree

7 files changed

+76
-10
lines changed

7 files changed

+76
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
- bug fixes:
66

7+
- completion and signature suggestions get invalidated when editor changes ([#507])
8+
- signature suggestions now invalidate on cursor move to another line or backwards too ([#507])
79
- LaTeX is now rendered in documentation panel of completer ([#506])
810
- completion response returned as plain text use pre tag to retain whitespace formatting ([#506])
911
- pre-formatted code font size was reduced to match font-size of the text in completion panel ([#506])
1012

1113
[#506]: https://github.com/krassowski/jupyterlab-lsp/pull/506
14+
[#507]: https://github.com/krassowski/jupyterlab-lsp/pull/507
1215

1316
### `jupyter-lsp 1.1.3` (unreleased)
1417

atest/03_Notebook.robot

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Adding Text To Cells After Kernel Restart
9191
Setup Notebook Python ${file}
9292
${virtual_path} = Set Variable ${VIRTUALDOCS DIR}${/}${file}
9393
Wait Until Created ${virtual_path}
94+
Restart Kernel
9495
Enter Cell Editor 1
9596
Lab Command Insert Cell Below
9697
Enter Cell Editor 2 line=1

atest/05_Features/Completion.robot

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ Works When Kernel Is Idle
2929
${content} = Get Cell Editor Content 1
3030
Should Contain ${content} TabError
3131

32+
Invalidates On Cell Change
33+
Enter Cell Editor 1 line=2
34+
Press Keys None TAB
35+
Enter Cell Editor 2
36+
# just to increase chances of caching this on CI (which is slow)
37+
Sleep 5s
38+
Completer Should Not Suggest test
39+
40+
Invalidates On Focus Loss
41+
Enter Cell Editor 1 line=2
42+
Press Keys None TAB
43+
Enter Cell Editor 2
44+
# just to increase chances of caching this on CI (which is slow)
45+
Sleep 5s
46+
Completer Should Not Suggest test
47+
3248
Uses LSP Completions When Kernel Resoponse Times Out
3349
Configure JupyterLab Plugin {"kernelResponseTimeout": 1, "waitForBusyKernel": true} plugin id=${COMPLETION PLUGIN ID}
3450
Should Complete While Kernel Is Busy
@@ -87,7 +103,6 @@ Continious Hinting Works
87103
Autocompletes If Only One Option
88104
Enter Cell Editor 3 line=1
89105
Press Keys None cle
90-
Wait Until Fully Initialized
91106
# First tab brings up the completer
92107
Press Keys None TAB
93108
Completer Should Suggest clear
@@ -99,7 +114,6 @@ Autocompletes If Only One Option
99114
Does Not Autocomplete If Multiple Options
100115
Enter Cell Editor 3 line=1
101116
Press Keys None c
102-
Wait Until Fully Initialized
103117
# First tab brings up the completer
104118
Press Keys None TAB
105119
Completer Should Suggest copy
@@ -302,11 +316,6 @@ Completer Should Include Documentation
302316
Wait Until Keyword Succeeds 10 x 1 s Element Should Contain ${DOCUMENTATION_PANEL} ${text}
303317
Element Should Contain ${DOCUMENTATION_PANEL} ${text}
304318

305-
Restart Kernel
306-
Lab Command Restart Kernel…
307-
Wait For Dialog
308-
Accept Default Dialog Option
309-
310319
Count Completer Hints
311320
${count} = Get Element Count css:.jp-Completer-item
312321
[Return] ${count}

atest/05_Features/Signature.robot

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,12 @@ Triggers Signature Help After A Keystroke
1616
Wait Until Keyword Succeeds 20x 0.5s Page Should Contain Element ${SIGNATURE_BOX}
1717
Element Should Contain ${SIGNATURE_BOX} Important docstring of abc()
1818
[Teardown] Clean Up After Working With File Signature.ipynb
19+
20+
Invalidates On Cell Change
21+
Setup Notebook Python Signature.ipynb
22+
Enter Cell Editor 1 line=6
23+
Press Keys None (
24+
Enter Cell Editor 2
25+
# just to increase chances of caching this on CI (which is slow)
26+
Sleep 5s
27+
Page Should Not Contain Element ${SIGNATURE_BOX}

atest/Keywords.robot

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,8 @@ Open New Notebook
419419
Wait For Dialog
420420
# Kernel selection dialog shows up, accept Python as default kernel
421421
Accept Default Dialog Option
422+
423+
Restart Kernel
424+
Lab Command Restart Kernel…
425+
Wait For Dialog
426+
Accept Default Dialog Option

packages/jupyterlab-lsp/src/features/completion/completion_handler.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,17 @@ export class LSPConnector
618618
token: CodeEditor.IToken,
619619
cursor_at_request: CodeEditor.IPosition
620620
) {
621+
if (!this._editor.hasFocus()) {
622+
this.console.debug(
623+
'Ignoring completion response: the corresponding editor lost focus'
624+
);
625+
return {
626+
start: reply.start,
627+
end: reply.end,
628+
items: []
629+
};
630+
}
631+
621632
const cursor_now = this._editor.getCursorPosition();
622633

623634
// if the cursor advanced in the same line, the previously retrieved completions may still be useful
@@ -626,6 +637,9 @@ export class LSPConnector
626637
cursor_at_request.line != cursor_now.line ||
627638
cursor_now.column < cursor_at_request.column
628639
) {
640+
this.console.debug(
641+
'Ignoring completion response: cursor has receded or changed line'
642+
);
629643
return {
630644
start: reply.start,
631645
end: reply.end,

packages/jupyterlab-lsp/src/features/signature.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,41 @@ export class SignatureCM extends CodeMirrorIntegration {
9292
return markdown;
9393
}
9494

95-
private handleSignature(response: lsProtocol.SignatureHelp) {
95+
private handleSignature(
96+
response: lsProtocol.SignatureHelp,
97+
position_at_request: IRootPosition
98+
) {
9699
this.lab_integration.tooltip.remove();
97100

98101
this.console.log('Signature received', response);
102+
99103
if (!this.signature_character || !response || !response.signatures.length) {
104+
this.console.debug(
105+
'Ignoring signature response: cursor lost or response empty'
106+
);
100107
return;
101108
}
102109

103-
let root_position = this.signature_character;
110+
let root_position = position_at_request;
111+
112+
// if the cursor advanced in the same line, the previously retrieved signature may still be useful
113+
// if the line changed or cursor moved backwards then no reason to keep the suggestions
114+
if (
115+
position_at_request.line != root_position.line ||
116+
root_position.ch < position_at_request.ch
117+
) {
118+
this.console.debug(
119+
'Ignoring signature response: cursor has receded or changed line'
120+
);
121+
}
122+
104123
let cm_editor = this.get_cm_editor(root_position);
124+
if (!cm_editor.hasFocus()) {
125+
this.console.debug(
126+
'Ignoring signature response: the corresponding editor lost focus'
127+
);
128+
return;
129+
}
105130
let editor_position = this.virtual_editor.root_position_to_editor(
106131
root_position
107132
);
@@ -152,7 +177,7 @@ export class SignatureCM extends CodeMirrorIntegration {
152177
this.virtual_document.document_info,
153178
false
154179
)
155-
.then(help => this.handleSignature(help))
180+
.then(help => this.handleSignature(help, root_position))
156181
.catch(this.console.warn);
157182
}
158183
}

0 commit comments

Comments
 (0)