|
@@ -1,6 +1,10 @@
|
|
|
// Copyright (c) Jupyter Development Team.
|
|
|
// Distributed under the terms of the Modified BSD License.
|
|
|
|
|
|
+import {
|
|
|
+ IKernel
|
|
|
+} from 'jupyter-js-services';
|
|
|
+
|
|
|
import {
|
|
|
IDisposable
|
|
|
} from 'phosphor-disposable';
|
|
@@ -123,6 +127,11 @@ interface ICompletionModel extends IDisposable {
|
|
|
*/
|
|
|
original: ICompletionRequest;
|
|
|
|
|
|
+ /**
|
|
|
+ * Handle a completion request using a kernel.
|
|
|
+ */
|
|
|
+ makeKernelRequest(request: ICompletionRequest, kernel: IKernel): void;
|
|
|
+
|
|
|
/**
|
|
|
* Create a resolved patch between the original state and a patch string.
|
|
|
*/
|
|
@@ -233,6 +242,37 @@ class CompletionModel implements ICompletionModel {
|
|
|
this.stateChanged.emit(void 0);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Make a request using a kernel.
|
|
|
+ */
|
|
|
+ makeKernelRequest(request: ICompletionRequest, kernel: IKernel): void {
|
|
|
+ let contents = {
|
|
|
+ // Only send the current line of code for completion.
|
|
|
+ code: request.currentValue.split('\n')[request.line],
|
|
|
+ cursor_pos: request.ch
|
|
|
+ };
|
|
|
+ let pendingComplete = ++this._pendingComplete;
|
|
|
+ kernel.complete(contents).then(value => {
|
|
|
+ // If we have been disposed, bail.
|
|
|
+ if (this.isDisposed) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // If a newer completion request has created a pending request, bail.
|
|
|
+ if (pendingComplete !== this._pendingComplete) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // Completion request failures or negative results fail silently.
|
|
|
+ if (value.status !== 'ok') {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // Update the state.
|
|
|
+ this.options = value.matches;
|
|
|
+ this.cursor = { start: value.cursor_start, end: value.cursor_end };
|
|
|
+ }).then(() => {
|
|
|
+ this.original = request;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Create a resolved patch between the original state and a patch string.
|
|
|
*
|
|
@@ -325,6 +365,7 @@ class CompletionModel implements ICompletionModel {
|
|
|
private _current: ITextChange = null;
|
|
|
private _query = '';
|
|
|
private _cursor: { start: number, end: number } = null;
|
|
|
+ private _pendingComplete = 0;
|
|
|
}
|
|
|
|
|
|
|