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

Implement a workaround for #334 #335

Merged
merged 1 commit into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
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
13 changes: 8 additions & 5 deletions packages/jupyterlab-lsp/src/features/completion/completion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { CompletionTriggerKind } from '../../lsp';
import {
AdditionalCompletionTriggerKinds,
CompletionTriggerKind,
ExtendedCompletionTriggerKind
} from '../../lsp';
import * as CodeMirror from 'codemirror';
import { CodeMirrorIntegration } from '../../editor_integration/codemirror';
import { JupyterFrontEnd } from '@jupyterlab/application';
Expand All @@ -18,7 +22,6 @@ import { ILSPCompletionThemeManager } from '@krassowski/completion-theme/lib/typ

export class CompletionCM extends CodeMirrorIntegration {
private _completionCharacters: string[];
// TODO chek if this works if yest then remove settings from options
settings: FeatureSettings<LSPCompletionSettings>;

get completionCharacters() {
Expand All @@ -44,7 +47,7 @@ export class CompletionCM extends CodeMirrorIntegration {
this.settings.composite.continuousHinting
) {
(this.feature.labIntegration as CompletionLabIntegration)
.invoke_completer(CompletionTriggerKind.Invoked)
.invoke_completer(AdditionalCompletionTriggerKinds.AutoInvoked)
.catch(console.warn);
return;
}
Expand Down Expand Up @@ -126,7 +129,7 @@ export class CompletionLabIntegration implements IFeatureLabIntegration {
});
}

invoke_completer(kind: CompletionTriggerKind) {
invoke_completer(kind: ExtendedCompletionTriggerKind) {
let command: string;
this.current_completion_connector.trigger_kind = kind;

Expand All @@ -135,7 +138,7 @@ export class CompletionLabIntegration implements IFeatureLabIntegration {
} else {
command = 'completer:invoke-file';
}
return this.app.commands.execute(command).then(() => {
return this.app.commands.execute(command).catch(() => {
this.current_completion_connector.trigger_kind =
CompletionTriggerKind.Invoked;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import {
CompletionConnector,
CompletionHandler,
ContextConnector,
KernelConnector,
CompletionConnector
KernelConnector
} from '@jupyterlab/completer';
import { CodeEditor } from '@jupyterlab/codeeditor';
import { JSONArray, JSONObject } from '@lumino/coreutils';
import { CompletionItemKind, CompletionTriggerKind } from '../../lsp';
import {
AdditionalCompletionTriggerKinds,
CompletionItemKind,
CompletionTriggerKind,
ExtendedCompletionTriggerKind
} from '../../lsp';
import * as lsProtocol from 'vscode-languageserver-types';
import { VirtualDocument } from '../../virtual/document';
import { IVirtualEditor } from '../../virtual/editor';
Expand All @@ -17,7 +22,6 @@ import {
} from '../../positioning';
import { LSPConnection } from '../../connection';
import { Session } from '@jupyterlab/services';
import ICompletionItemsResponseType = CompletionHandler.ICompletionItemsResponseType;

import { CodeCompletion as LSPCompletionSettings } from '../../_completion';
import { FeatureSettings } from '../../feature';
Expand All @@ -27,6 +31,7 @@ import {
KernelKind
} from '@krassowski/completion-theme/lib/types';
import { LabIcon } from '@jupyterlab/ui-components';
import ICompletionItemsResponseType = CompletionHandler.ICompletionItemsResponseType;

/**
* A LSP connector for completion handlers.
Expand All @@ -44,7 +49,7 @@ export class LSPConnector
responseType = ICompletionItemsResponseType;

virtual_editor: IVirtualEditor<CodeEditor.IEditor>;
trigger_kind: CompletionTriggerKind;
trigger_kind: ExtendedCompletionTriggerKind;

protected get suppress_auto_invoke_in(): string[] {
return this.options.settings.composite.suppressInvokeIn;
Expand Down Expand Up @@ -156,6 +161,17 @@ export class LSPConnector
cursor_in_root
);

const lsp_promise = this.fetch_lsp(
token,
typed_character,
virtual_start,
virtual_end,
virtual_cursor,
document,
position_in_token
);
let promise: Promise<CompletionHandler.ICompletionItemsReply> = null;

try {
if (this._kernel_connector && this._has_kernel) {
// TODO: this would be awesome if we could connect to rpy2 for R suggestions in Python,
Expand All @@ -165,41 +181,34 @@ export class LSPConnector
const kernelLanguage = await this._kernel_language();

if (document.language === kernelLanguage) {
return Promise.all([
promise = Promise.all([
this._kernel_connector.fetch(request),
this.fetch_lsp(
token,
typed_character,
virtual_start,
virtual_end,
virtual_cursor,
document,
position_in_token
)
lsp_promise
]).then(([kernel, lsp]) =>
this.merge_replies(this.transform_reply(kernel), lsp, this._editor)
);
}
}

return this.fetch_lsp(
token,
typed_character,
virtual_start,
virtual_end,
virtual_cursor,
document,
position_in_token
).catch(e => {
console.warn('LSP: hint failed', e);
return this.fallback_connector
.fetch(request)
.then(this.transform_reply);
});
if (!promise) {
promise = lsp_promise.catch(e => {
console.warn('LSP: hint failed', e);
return this.fallback_connector
.fetch(request)
.then(this.transform_reply);
});
}
} catch (e) {
console.warn('LSP: kernel completions failed', e);
return this.fallback_connector.fetch(request).then(this.transform_reply);
promise = this.fallback_connector
.fetch(request)
.then(this.transform_reply);
}

return promise.then(reply => {
reply = this.suppress_if_needed(reply);
this.trigger_kind = CompletionTriggerKind.Invoked;
return reply;
});
}

async fetch_lsp(
Expand All @@ -216,6 +225,11 @@ export class LSPConnector
console.log('[LSP][Completer] Fetching and Transforming');
console.log('[LSP][Completer] Token:', token, start, end);

const trigger_kind =
this.trigger_kind == AdditionalCompletionTriggerKinds.AutoInvoked
? CompletionTriggerKind.Invoked
: this.trigger_kind;

let lspCompletionItems = ((await connection.getCompletion(
cursor,
{
Expand All @@ -226,7 +240,7 @@ export class LSPConnector
document.document_info,
false,
typed_character,
this.trigger_kind
trigger_kind
)) || []) as lsProtocol.CompletionItem[];

let prefix = token.value.slice(0, position_in_token + 1);
Expand Down Expand Up @@ -398,6 +412,19 @@ export class LSPConnector
save(id: CompletionHandler.IRequest, value: void): Promise<any> {
return Promise.resolve(undefined);
}

private suppress_if_needed(reply: CompletionHandler.ICompletionItemsReply) {
if (this.trigger_kind == AdditionalCompletionTriggerKinds.AutoInvoked) {
if (reply.start == reply.end) {
return {
start: reply.start,
end: reply.end,
items: []
};
}
}
return reply;
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions packages/jupyterlab-lsp/src/lsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ export enum CompletionTriggerKind {
TriggerForIncompleteCompletions = 3
}

export enum AdditionalCompletionTriggerKinds {
AutoInvoked = 9999
}

export type ExtendedCompletionTriggerKind =
| CompletionTriggerKind
| AdditionalCompletionTriggerKinds;

export type CompletionItemKindStrings = keyof typeof CompletionItemKind;

/**
Expand Down