Skip to content

Commit 7ae3c05

Browse files
authored
Merge pull request #735 from jupyter-lsp/prefilter-completion-items
Enable simple pre-filtering of completion matches
2 parents d2ef26c + 835638a commit 7ae3c05

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

atest/05_Features/Completion.robot

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ ${KERNEL_BUSY_INDICATOR} css:.jp-NotebookPanel-toolbar div[title="Kernel Busy
1212

1313
*** Test Cases ***
1414
Works When Kernel Is Idle
15-
Configure JupyterLab Plugin {"kernelResponseTimeout": -1, "waitForBusyKernel": false} plugin id=${COMPLETION PLUGIN ID}
16-
[Documentation] The suggestions from kernel and LSP should get integrated.
15+
Configure JupyterLab Plugin {"kernelResponseTimeout": -1, "waitForBusyKernel": false, "caseSensitive": false} plugin id=${COMPLETION PLUGIN ID}
16+
[Documentation] The suggestions from kernel and LSP should get integrated; operates in case insensitive mode
1717
Enter Cell Editor 1 line=2
1818
Capture Page Screenshot 01-entered-cell.png
1919
Trigger Completer
2020
Capture Page Screenshot 02-completions-shown.png
21-
# lowercase and uppercase suggestions:
2221
Completer Should Suggest TabError
2322
# this comes from LSP:
2423
Completer Should Suggest test
@@ -29,8 +28,18 @@ Works When Kernel Is Idle
2928
${content} = Get Cell Editor Content 1
3029
Should Contain ${content} TabError
3130

31+
Filters Completions In Case Sensitive Mode
32+
Configure JupyterLab Plugin {"caseSensitive": true} plugin id=${COMPLETION PLUGIN ID}
33+
[Documentation] Completions filtering is case-sensitive when caseSensitive is true
34+
Enter Cell Editor 1 line=2
35+
Trigger Completer
36+
Completer Should Suggest test
37+
Completer Should Not Suggest TabError
38+
39+
3240
Can Prioritize Kernel Completions
33-
Configure JupyterLab Plugin {"kernelCompletionsFirst": true, "kernelResponseTimeout": -1} plugin id=${COMPLETION PLUGIN ID}
41+
# note: disabling pre-filtering to get ranking without match scoring
42+
Configure JupyterLab Plugin {"kernelCompletionsFirst": true, "kernelResponseTimeout": -1, "preFilterMatches": false} plugin id=${COMPLETION PLUGIN ID}
3443
Enter Cell Editor 1 line=2
3544
Trigger Completer
3645
Completer Should Suggest %%timeit
@@ -39,7 +48,8 @@ Can Prioritize Kernel Completions
3948
Should Be True ${kernel_position} < ${lsp_position}
4049

4150
Can Prioritize LSP Completions
42-
Configure JupyterLab Plugin {"kernelCompletionsFirst": false, "kernelResponseTimeout": -1} plugin id=${COMPLETION PLUGIN ID}
51+
# note: disabling pre-filtering to get ranking without match scoring
52+
Configure JupyterLab Plugin {"kernelCompletionsFirst": false, "kernelResponseTimeout": -1, "preFilterMatches": false} plugin id=${COMPLETION PLUGIN ID}
4353
Enter Cell Editor 1 line=2
4454
Trigger Completer
4555
Completer Should Suggest %%timeit
@@ -160,6 +170,7 @@ Does Not Autocomplete If Multiple Options
160170
Completer Should Suggest copy
161171

162172
User Can Select Lowercase After Starting Uppercase
173+
Configure JupyterLab Plugin {"caseSensitive": false} plugin id=${COMPLETION PLUGIN ID}
163174
# `from time import Tim<tab>` → `from time import time`
164175
Enter Cell Editor 5 line=1
165176
Trigger Completer
@@ -212,7 +223,7 @@ Triggers Completer On Dot
212223
Completer Should Suggest append
213224

214225
Material Theme Works
215-
Configure JupyterLab Plugin {"theme": "material"} plugin id=${COMPLETION PLUGIN ID}
226+
Configure JupyterLab Plugin {"theme": "material", "caseSensitive": false} plugin id=${COMPLETION PLUGIN ID}
216227
Capture Page Screenshot 01-configured.png
217228
Enter Cell Editor 1 line=2
218229
Trigger Completer
@@ -223,7 +234,7 @@ Material Theme Works
223234
Completer Should Include Icon lsp:material-class-light
224235

225236
VSCode Theme Works
226-
Configure JupyterLab Plugin {"theme": "vscode"} plugin id=${COMPLETION PLUGIN ID}
237+
Configure JupyterLab Plugin {"theme": "vscode", "caseSensitive": false} plugin id=${COMPLETION PLUGIN ID}
227238
Capture Page Screenshot 01-configured.png
228239
Enter Cell Editor 1 line=2
229240
Trigger Completer
@@ -236,7 +247,7 @@ VSCode Dark Theme Works
236247
Lab Command Use Theme: JupyterLab Dark
237248
Wait For Splash
238249
Capture Page Screenshot 00-theme-changed.png
239-
Configure JupyterLab Plugin {"theme": "vscode"} plugin id=${COMPLETION PLUGIN ID}
250+
Configure JupyterLab Plugin {"theme": "vscode", "caseSensitive": false} plugin id=${COMPLETION PLUGIN ID}
240251
Capture Page Screenshot 01-configured.png
241252
Open ${file} in ${MENU NOTEBOOK}
242253
Wait Until Fully Initialized
@@ -249,7 +260,7 @@ VSCode Dark Theme Works
249260
Wait For Splash
250261

251262
Works Without A Theme
252-
Configure JupyterLab Plugin {"theme": null} plugin id=${COMPLETION PLUGIN ID}
263+
Configure JupyterLab Plugin {"theme": null, "caseSensitive": false} plugin id=${COMPLETION PLUGIN ID}
253264
Capture Page Screenshot 01-configured.png
254265
Enter Cell Editor 1 line=2
255266
Trigger Completer
@@ -258,7 +269,7 @@ Works Without A Theme
258269
Wait Until Page Contains Element ${COMPLETER_BOX} .jp-Completer-monogram
259270

260271
Works With Incorrect Theme
261-
Configure JupyterLab Plugin {"theme": "a-non-existing-theme"} plugin id=${COMPLETION PLUGIN ID}
272+
Configure JupyterLab Plugin {"theme": "a-non-existing-theme", "caseSensitive": false} plugin id=${COMPLETION PLUGIN ID}
262273
Capture Page Screenshot 01-configured.png
263274
Enter Cell Editor 1 line=2
264275
Trigger Completer

packages/jupyterlab-lsp/schema/completion.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@
7676
"type": "boolean",
7777
"description": "Should perfect matches be included in the completion suggestions list?"
7878
},
79+
"preFilterMatches": {
80+
"title": "Pre-filter matches",
81+
"default": true,
82+
"type": "boolean",
83+
"description": "Should matches be pre-filtered to ensure typed token is a prefix of the match?"
84+
},
7985
"labelExtra": {
8086
"title": "Text to display next to completion label",
8187
"default": "auto",

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ export class CompletionLabIntegration implements IFeatureLabIntegration {
130130
this.settings.composite.caseSensitive;
131131
this.model.settings.includePerfectMatches =
132132
this.settings.composite.includePerfectMatches;
133+
this.model.settings.preFilterMatches =
134+
this.settings.composite.preFilterMatches;
133135
}
134136
});
135137
}
@@ -285,7 +287,8 @@ export class CompletionLabIntegration implements IFeatureLabIntegration {
285287
completer.addClass('lsp-completer');
286288
completer.model = new LSPCompleterModel({
287289
caseSensitive: this.settings.composite.caseSensitive,
288-
includePerfectMatches: this.settings.composite.includePerfectMatches
290+
includePerfectMatches: this.settings.composite.includePerfectMatches,
291+
preFilterMatches: this.settings.composite.preFilterMatches
289292
});
290293
}
291294

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,21 @@ export class GenericCompleterModel<
4949

5050
setCompletionItems(newValue: T[]) {
5151
super.setCompletionItems!(newValue);
52+
53+
if (this.settings.preFilterMatches && this.current && this.cursor) {
54+
// set initial query to pre-filter items; in future we should use:
55+
// https://github.com/jupyterlab/jupyterlab/issues/9763#issuecomment-1001603348
56+
const { start, end } = this.cursor;
57+
let query = this.current.text.substring(start, end).trim();
58+
// special case for "Completes Paths In Strings" test case
59+
if (query.startsWith('"') || query.startsWith("'")) {
60+
query = query.substring(1);
61+
}
62+
if (query.endsWith('"') || query.endsWith("'")) {
63+
query = query.substring(0, -1);
64+
}
65+
this.query = query;
66+
}
5267
}
5368

5469
private _markFragment(value: string): string {
@@ -180,10 +195,15 @@ export namespace GenericCompleterModel {
180195
* Whether perfect matches should be included (default = true)
181196
*/
182197
includePerfectMatches?: boolean;
198+
/**
199+
* Wheteher matches should be pre-filtered (default = true)
200+
*/
201+
preFilterMatches?: boolean;
183202
}
184203
export const defaultOptions: IOptions = {
185204
caseSensitive: true,
186-
includePerfectMatches: true
205+
includePerfectMatches: true,
206+
preFilterMatches: true
187207
};
188208
}
189209

0 commit comments

Comments
 (0)