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

Explore how to implement isIncomplete support #838

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 1 addition & 3 deletions packages/jupyterlab-lsp/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
// Introduced modifications are BSD licenced, copyright JupyterLab development team.
import { ISignal, Signal } from '@lumino/signaling';
import {
AnyCompletion,
AnyLocation,
IDocumentInfo,
ILspOptions,
IPosition,
Expand All @@ -19,7 +17,7 @@ import type * as rpc from 'vscode-jsonrpc';
import type * as lsp from 'vscode-languageserver-protocol';
import type { MessageConnection } from 'vscode-ws-jsonrpc';

import { ClientCapabilities } from './lsp';
import { AnyLocation, AnyCompletion, ClientCapabilities } from './lsp';
import { ILSPLogConsole } from './tokens';
import { until_ready } from './utils';

Expand Down
36 changes: 35 additions & 1 deletion packages/jupyterlab-lsp/src/features/completion/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,45 @@ export class CompletionLabIntegration implements IFeatureLabIntegration {
completer.model = new CompleterModel();
} else {
completer.addClass(LSP_COMPLETER_CLASS);
completer.model = new LSPCompleterModel({
const model = new LSPCompleterModel({
caseSensitive: this.settings.composite.caseSensitive,
includePerfectMatches: this.settings.composite.includePerfectMatches,
preFilterMatches: this.settings.composite.preFilterMatches
});
completer.model = model;
model.queryChanged.connect(this._handleQuery.bind(this));
// TODO: disconnect!
}
}

/**
* User typed a character while the completer is shown changing the query.
*/
private async _handleQuery(model: LSPCompleterModel, query: string) {
// it is important not to fail here: otherwise we break native completer too.
try {
if (!this.current_completion_connector.isIncomplete) {
// do nothing if the result was complete
return;
} else if(!this.current_adapter) {
return;
} else {
// TODO: can we only fetch LSP items, keep kernel items as-is?
await this.current_adapter.update_documents();
const reply = await this.current_completion_connector.fetch();
if (reply) {
model.query = ''
// ref `CompletionHandler._updateModel`
model.cursor = {
start: reply.start, // should be wrapped in `Text.charIndexToJsIndex(start, text)`,
end: reply.end
}
model.original = model.current;
model.setCompletionItems(reply.items as LazyCompletionItem[]);
}
}
} catch(e) {
this.console.error('handling query change failed', e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { LSPConnection } from '../../connection';
import { PositionConverter } from '../../converter';
import { FeatureSettings } from '../../feature';
import {
AnyCompletion,
AdditionalCompletionTriggerKinds,
CompletionItemKind,
CompletionTriggerKind,
Expand Down Expand Up @@ -60,6 +61,7 @@ export function transformLSPCompletions<T>(
createCompletionItem: (kind: string, match: lsProtocol.CompletionItem) => T,
console: ILSPLogConsole
) {
console.debug('Transforming');
let prefix = token.value.slice(0, position_in_token + 1);
let all_non_prefixed = true;
let items: T[] = [];
Expand Down Expand Up @@ -146,6 +148,22 @@ export function transformLSPCompletions<T>(
return response;
}

function toCompletionList(reply: AnyCompletion | null): lsProtocol.CompletionList {
if (!reply) {
return {
items: [],
isIncomplete: false
};
}
if (Array.isArray(reply)) {
return {
items: reply as lsProtocol.CompletionItem[],
isIncomplete: false
};
}
return reply as lsProtocol.CompletionList;
}

/**
* A LSP connector for completion handlers.
*/
Expand All @@ -158,6 +176,7 @@ export class LSPConnector
private _context_connector: ContextConnector;
private _kernel_connector: KernelConnector;
private _kernel_and_context_connector: CompletionConnector;
private _isIncomplete: boolean;
private console: ILSPLogConsole;

// signal that this is the new type connector (providing completion items)
Expand Down Expand Up @@ -272,13 +291,21 @@ export class LSPConnector
)!;
}

/**
* @deprecated; instead of using a global state like this,
* the latest reply should be cached by completer and this info extracted?
*/
get isIncomplete() {
return this._isIncomplete;
}

/**
* Fetch completion requests.
*
* @param request - The completion request text and details.
*/
async fetch(
request: CompletionHandler.IRequest
requestIn?: CompletionHandler.IRequest
): Promise<CompletionHandler.ICompletionItemsReply | undefined> {
let editor = this._editor;

Expand Down Expand Up @@ -311,6 +338,12 @@ export class LSPConnector
let position_in_token = cursor.column - start.column - 1;
const typed_character = token.value[cursor.column - start.column - 1];

const request: CompletionHandler.IRequest
= requestIn ? requestIn : {
offset: token.offset + position_in_token + 1,
text: editor.model.value.text
};

let start_in_root = this.transform_from_editor_to_root(start);
let end_in_root = this.transform_from_editor_to_root(end);
let cursor_in_root = this.transform_from_editor_to_root(cursor);
Expand Down Expand Up @@ -455,20 +488,23 @@ export class LSPConnector
? CompletionTriggerKind.Invoked
: this.trigger_kind;

let lspCompletionItems = ((await connection.getCompletion(
cursor,
{
start,
end,
text: token.value
let completionReply = toCompletionList(await connection.clientRequests['textDocument/completion'].request(
{
textDocument: {
uri: document.document_info.uri
},
document.document_info,
false,
typed_character,
trigger_kind
)) || []) as lsProtocol.CompletionItem[];
position: {
line: cursor.line,
character: cursor.ch
},
context: {
triggerKind: trigger_kind || CompletionTriggerKind.Invoked,
triggerCharacter: typed_character
}
}));
this._isIncomplete = completionReply.isIncomplete;

this.console.debug('Transforming');
let lspCompletionItems: lsProtocol.CompletionItem[] = completionReply.items || [];

return transformLSPCompletions(
token,
Expand Down
23 changes: 22 additions & 1 deletion packages/jupyterlab-lsp/src/features/completion/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { CompleterModel, CompletionHandler } from '@jupyterlab/completer';
import { StringExt } from '@lumino/algorithm';
import { Signal } from '@lumino/signaling';

import { LazyCompletionItem } from './item';

Expand Down Expand Up @@ -41,7 +42,6 @@ export class GenericCompleterModel<
this.query = '';
let unfilteredItems = super.completionItems!() as T[];
this.query = query;

// always want to sort
// TODO does this behave strangely with %%<tab> if always sorting?
return this._sortAndFilter(query, unfilteredItems);
Expand Down Expand Up @@ -208,6 +208,27 @@ export namespace GenericCompleterModel {
}

export class LSPCompleterModel extends GenericCompleterModel<LazyCompletionItem> {
queryChanged: Signal<LSPCompleterModel, string>;
private _lastQuery: string;

constructor(settings: GenericCompleterModel.IOptions = {}) {
super(settings);
this.queryChanged = new Signal(this);
this.stateChanged.connect(this.onStateChanged.bind(this))
}

onStateChanged() {
// TODO: this does not get called for the second time.
// It seems that there is a condition in completer usptream which is not met.

// we will bail when query gets set to empty strings as these
// are to invoke setter side-effects.
if (this.query && this.query != this._lastQuery) {
this.queryChanged.emit(this.query);
this._lastQuery = this.query;
}
}

protected getFilterText(item: LazyCompletionItem): string {
if (item.filterText) {
return item.filterText;
Expand Down
12 changes: 12 additions & 0 deletions packages/jupyterlab-lsp/src/lsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ export enum DiagnosticSeverity {
Hint = 4
}


export type AnyLocation =
| lsp.Location
| lsp.Location[]
| lsp.LocationLink[]
| undefined
| null;

export type AnyCompletion =
| lsp.CompletionList
| lsp.CompletionItem[];

export enum DiagnosticTag {
Unnecessary = 1,
Deprecated = 2
Expand Down
9 changes: 9 additions & 0 deletions packages/lsp-ws-connection/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,26 @@ export interface IDocumentInfo {
languageId: string;
}

/**
* @deprecated, moved to `@jupyter-lsp/jupyterlab-lsp/lsp.ts`
* (will become `@jupyterlab/lsp/lsp.ts` in near futurue)
*/
export type AnyLocation =
| lsProtocol.Location
| lsProtocol.Location[]
| lsProtocol.LocationLink[]
| undefined
| null;

/**
* @deprecated, moved to `@jupyter-lsp/jupyterlab-lsp/lsp.ts`
* (will become `@jupyterlab/lsp/lsp.ts` in near futurue)
*/
export type AnyCompletion =
| lsProtocol.CompletionList
| lsProtocol.CompletionItem[];


export enum CompletionTriggerKind {
Invoked = 1,
TriggerCharacter = 2,
Expand Down