Skip to content

Commit

Permalink
Merge pull request #686 from jupyter-lsp/completion-fixes
Browse files Browse the repository at this point in the history
Improve: kernel completions in R, completions in strings and paths
  • Loading branch information
krassowski authored Oct 10, 2021
2 parents 8cfd4f4 + fc92b7b commit 8c9b01b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@
lines will be collapsed into an expandable details section.
- the signature box is now displayed above the current line
- the signature box takes up less space
- bug fixes:
- fix missing translation strings ([#675])
- fix kernel completions not showing up for R ([#686])
- fix tab completions not showing up in strings due to incomplete trigger kind invalidation ([#686])
- fix path completions reconciliation for `pyls`/`pylsp` with `IPython` ([#686])

[#671]: https://github.com/krassowski/jupyterlab-lsp/pull/671
[#675]: https://github.com/krassowski/jupyterlab-lsp/pull/675
[#686]: https://github.com/krassowski/jupyterlab-lsp/pull/686

### `@krassowski/jupyterlab-lsp 3.8.1` (2021-08-02)

Expand Down
7 changes: 7 additions & 0 deletions atest/05_Features/Completion.robot
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,13 @@ Completes In R Magics
Trigger Completer
Completer Should Suggest library

Completes Paths In Strings
Enter Cell Editor 26
Press Keys None LEFT
Trigger Completer
Press Keys None ENTER
Wait Until Keyword Succeeds 40x 0.5s Cell Editor Should Equal 26 '../Completion.ipynb'

*** Keywords ***
Setup Completion Test
Setup Notebook Python Completion.ipynb
Expand Down
2 changes: 1 addition & 1 deletion atest/Keywords.robot
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ Open Context Menu for File
Click Element ${JLAB CSS REFRESH FILES}
${selector} = Set Variable xpath://span[@class='jp-DirListing-itemText']/span\[text() = '${file}']
Wait Until Page Contains Element ${selector} timeout=10s
Open Context Menu ${selector}
Wait Until Keyword Succeeds 10 x 0.1 s Open Context Menu ${selector}

Rename Jupyter File
[Arguments] ${old} ${new}
Expand Down
25 changes: 24 additions & 1 deletion atest/examples/Completion.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,27 @@
"source": [
"%R li"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Path completion should work"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"'../Com'"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -219,6 +235,13 @@
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ export class CompletionLabIntegration implements IFeatureLabIntegration {
this.current_index
];

if (!item || active.insertText != item.insertText) {
if (!item || !active || active.insertText != item.insertText) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,13 @@ export class LSPConnector
if (this.trigger_kind == AdditionalCompletionTriggerKinds.AutoInvoked) {
if (this.suppress_continuous_hinting_in.indexOf(token.type) !== -1) {
this.console.debug('Suppressing completer auto-invoke in', token.type);
this.trigger_kind = CompletionTriggerKind.Invoked;
return;
}
} else if (this.trigger_kind == CompletionTriggerKind.TriggerCharacter) {
if (this.suppress_trigger_character_in.indexOf(token.type) !== -1) {
this.console.debug('Suppressing completer auto-invoke in', token.type);
this.trigger_kind = CompletionTriggerKind.Invoked;
return;
}
}
Expand Down Expand Up @@ -260,7 +262,9 @@ export class LSPConnector
// TODO: should it be cashed?
const kernelLanguage = await this._kernel_language();

if (document.language === kernelLanguage) {
if (
document.language.toLocaleLowerCase() === kernelLanguage.toLowerCase()
) {
let default_kernel_promise = this._kernel_connector.fetch(request);
let kernel_promise: Promise<CompletionHandler.IReply>;

Expand Down Expand Up @@ -368,16 +372,11 @@ export class LSPConnector
let items: IExtendedCompletionItem[] = [];
lspCompletionItems.forEach(match => {
let kind = match.kind ? CompletionItemKind[match.kind] : '';
let completionItem = new LazyCompletionItem(
kind,
this.icon_for(kind),
match,
this,
document.uri
);

// Update prefix values
let text = match.insertText ? match.insertText : match.label;

// declare prefix presence if needed and update it
if (text.toLowerCase().startsWith(prefix.toLowerCase())) {
all_non_prefixed = false;
if (prefix !== token.value) {
Expand All @@ -391,6 +390,34 @@ export class LSPConnector
}
}
}
// add prefix if needed
else if (token.type === 'string' && prefix.includes('/')) {
// special case for path completion in strings, ensuring that:
// '/Com<tab> → '/Completion.ipynb
// when the returned insert text is `Completion.ipynb` (the token here is `'/Com`)
// developed against pyls and pylsp server, may not work well in other cases
const parts = prefix.split('/');
if (
text.toLowerCase().startsWith(parts[parts.length - 1].toLowerCase())
) {
let pathPrefix = parts.slice(0, -1).join('/') + '/';
match.insertText = pathPrefix + match.insertText;
// for label removing the prefix quote if present
if (pathPrefix.startsWith("'") || pathPrefix.startsWith('"')) {
pathPrefix = pathPrefix.substr(1);
}
match.label = pathPrefix + match.label;
all_non_prefixed = false;
}
}

let completionItem = new LazyCompletionItem(
kind,
this.icon_for(kind),
match,
this,
document.uri
);

items.push(completionItem);
});
Expand Down

0 comments on commit 8c9b01b

Please sign in to comment.