Browse Source

Update the completer

Steven Silvester 8 years ago
parent
commit
37ca693714

+ 6 - 16
packages/completer-extension/src/index.ts

@@ -87,10 +87,10 @@ const service: JupyterLabPlugin<ICompletionManager> = {
 
     return {
       register: (completable: ICompletionManager.ICompletable): ICompletionManager.ICompletableAttributes => {
-        const { editor, kernel, parent } = completable;
+        const { editor, session, parent } = completable;
         const model = new CompleterModel();
         const completer = new CompleterWidget({ editor, model });
-        const handler = new CompletionHandler({ completer, kernel });
+        const handler = new CompletionHandler({ completer, session });
         const id = parent.id;
 
         // Hide the widget when it first loads.
@@ -133,19 +133,14 @@ const consolePlugin: JupyterLabPlugin<void> = {
       const anchor = panel.console;
       const cell = anchor.prompt;
       const editor = cell && cell.editor;
-      const kernel = anchor.session.kernel;
+      const session = anchor.session;
       const parent = panel;
-      const handler = manager.register({ editor, kernel, parent });
+      const handler = manager.register({ editor, session, parent });
 
       // Listen for prompt creation.
       anchor.promptCreated.connect((sender, cell) => {
         handler.editor = cell && cell.editor;
       });
-
-      // Listen for kernel changes.
-      anchor.session.kernelChanged.connect((sender, kernel) => {
-        handler.kernel = kernel;
-      });
     });
 
     // Add console completer invoke command.
@@ -191,19 +186,14 @@ const notebookPlugin: JupyterLabPlugin<void> = {
     notebooks.widgetAdded.connect((sender, panel) => {
       const cell = panel.notebook.activeCell;
       const editor = cell && cell.editor;
-      const kernel = panel.kernel;
+      const session = panel.session;
       const parent = panel;
-      const handler = manager.register({ editor, kernel, parent });
+      const handler = manager.register({ editor, session, parent });
 
       // Listen for active cell changes.
       panel.notebook.activeCellChanged.connect((sender, cell) => {
         handler.editor = cell && cell.editor;
       });
-
-      // Listen for kernel changes.
-      panel.kernelChanged.connect((sender, kernel) => {
-        handler.kernel = kernel;
-      });
     });
 
     // Add notebook completer command.

+ 16 - 20
packages/completer/src/handler.ts

@@ -2,7 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  Kernel, KernelMessage
+  KernelMessage
 } from '@jupyterlab/services';
 
 import {
@@ -13,6 +13,10 @@ import {
   Message, MessageLoop
 } from '@phosphor/messaging';
 
+import {
+  IClientSession
+} from '@jupyterlab/apputils';
+
 import {
   CodeEditor
 } from '@jupyterlab/codeeditor';
@@ -45,7 +49,7 @@ class CompletionHandler implements IDisposable {
     this._completer = options.completer;
     this._completer.selected.connect(this.onCompletionSelected, this);
     this._completer.visibilityChanged.connect(this.onVisibilityChanged, this);
-    this._kernel = options.kernel || null;
+    this.session = options.session;
   }
 
   /**
@@ -94,6 +98,10 @@ class CompletionHandler implements IDisposable {
     }
   }
 
+  /**
+   * The session used by the completion handler.
+   */
+  readonly session: IClientSession;
 
   /**
    * Get whether the completion handler is disposed.
@@ -102,22 +110,11 @@ class CompletionHandler implements IDisposable {
     return this._completer === null;
   }
 
-  /**
-   * The kernel used by the completion handler.
-   */
-  get kernel(): Kernel.IKernel {
-    return this._kernel;
-  }
-  set kernel(value: Kernel.IKernel) {
-    this._kernel = value;
-  }
-
   /**
    * Dispose of the resources used by the handler.
    */
   dispose(): void {
     this._completer = null;
-    this._kernel = null;
 
     // Use public accessor to disconnect from editor signals.
     this.editor = null;
@@ -158,10 +155,10 @@ class CompletionHandler implements IDisposable {
   }
 
   /**
-   * Make a complete request using the kernel.
+   * Make a complete request using the session.
    */
   protected makeRequest(position: CodeEditor.IPosition): Promise<void> {
-    if (!this._kernel) {
+    if (!this.session.kernel) {
       return Promise.reject(new Error('no kernel for completion request'));
     }
 
@@ -178,7 +175,7 @@ class CompletionHandler implements IDisposable {
     let pending = ++this._pending;
     let request = this.getState(position);
 
-    return this._kernel.requestComplete(content).then(msg => {
+    return this.session.kernel.requestComplete(content).then(msg => {
       if (this.isDisposed) {
         return;
       }
@@ -219,7 +216,7 @@ class CompletionHandler implements IDisposable {
    */
   protected onInvokeRequest(msg: Message): void {
     // If there is neither a kernel nor a completer model, bail.
-    if (!this._kernel || !this._completer.model) {
+    if (!this.session.kernel || !this._completer.model) {
       return;
     }
 
@@ -369,7 +366,6 @@ class CompletionHandler implements IDisposable {
   private _editor: CodeEditor.IEditor | null = null;
   private _enabled = false;
   private _completer: CompleterWidget | null = null;
-  private _kernel: Kernel.IKernel | null = null;
   private _pending = 0;
 }
 
@@ -390,9 +386,9 @@ namespace CompletionHandler {
     completer: CompleterWidget;
 
     /**
-     * The kernel for the completion handler.
+     * The session for the completion handler.
      */
-    kernel?: Kernel.IKernel;
+    session: IClientSession;
   }
 
   /**

+ 8 - 9
packages/completer/src/index.ts

@@ -2,8 +2,12 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  Kernel
-} from '@jupyterlab/services';
+  IClientSession
+} from '@jupyterlab/apputils';
+
+import {
+  CodeEditor
+} from '@jupyterlab/codeeditor';
 
 import {
   Token
@@ -13,11 +17,6 @@ import {
   Widget
 } from '@phosphor/widgets';
 
-import {
-  CodeEditor
-} from '@jupyterlab/codeeditor';
-
-
 export * from './handler';
 export * from './model';
 export * from './widget';
@@ -63,9 +62,9 @@ namespace ICompletionManager {
     editor: CodeEditor.IEditor;
 
     /**
-     * The kernel used by the completer to make API requests.
+     * The session used by the completer to make API requests.
      */
-    kernel: Kernel.IKernel;
+    session: IClientSession;
   }
 
   /**