浏览代码

Move completer and inspection handler to panel

Steven Silvester 8 年之前
父节点
当前提交
ced293ff6c
共有 4 个文件被更改,包括 123 次插入94 次删除
  1. 112 0
      src/console/panel.ts
  2. 2 2
      src/console/plugin.ts
  3. 9 88
      src/console/widget.ts
  4. 0 4
      src/inspector/handler.ts

+ 112 - 0
src/console/panel.ts

@@ -17,10 +17,26 @@ import {
   Panel
 } from 'phosphor/lib/ui/panel';
 
+import {
+  Widget
+} from 'phosphor/lib/ui/widget';
+
 import {
   IEditorMimeTypeService
 } from '../codeeditor';
 
+import {
+  CellCompleterHandler, CompleterWidget
+} from '../completer';
+
+import {
+  InspectionHandler
+} from '../inspector';
+
+import {
+  CodeCellWidget
+} from '../notebook/cells';
+
 import {
   IRenderMime
 } from '../rendermime';
@@ -53,6 +69,33 @@ class ConsolePanel extends Panel {
     let consoleOpts = { rendermime, session, mimeTypeService, contentFactory };
     this.console = factory.createConsole(consoleOpts);
     this.addWidget(this.console);
+
+    // Set up the inspection handler.
+    this.inspectionHandler = factory.createInspectionHandler({
+      kernel: options.session.kernel,
+      rendermime: this.console.rendermime
+    });
+
+    // Instantiate the completer.
+    this._completer = factory.createCompleter({});
+
+    // Set the completer widget's anchor node to peg its position.
+    this._completer.anchor = this.node;
+
+    // Because a completer widget may be passed in, check if it is attached.
+    if (!this._completer.isAttached) {
+      Widget.attach(this._completer, document.body);
+    }
+
+    // Instantiate the completer handler.
+    this._completerHandler = factory.createCompleterHandler({
+      completer: this._completer,
+      kernel: options.session.kernel
+    });
+
+    this.console.promptCreated.connect(this._onPromptCreated, this);
+
+    options.session.kernelChanged.connect(this._onKernelChanged, this);
   }
 
   /**
@@ -60,6 +103,11 @@ class ConsolePanel extends Panel {
    */
   readonly console: Console;
 
+  /**
+   * The inspection handler used by the console.
+   */
+  readonly inspectionHandler: InspectionHandler;
+
   /**
    * Dispose of the resources held by the widget.
    */
@@ -72,6 +120,12 @@ class ConsolePanel extends Panel {
     this._content.dispose();
     this._content = null;
 
+    this._completerHandler.dispose();
+    this._completerHandler = null;
+    this._completer.dispose();
+    this._completer = null;
+    this.inspectionHandler.dispose();
+
     super.dispose();
   }
 
@@ -90,7 +144,30 @@ class ConsolePanel extends Panel {
     this.dispose();
   }
 
+  /**
+   * Handle the creation of a new prompt.
+   */
+  private _onPromptCreated(sender: Console, prompt: CodeCellWidget): void {
+    this._completer.reset();
+
+    // Associate the new prompt with the completer and inspection handlers.
+    this._completerHandler.activeCell = prompt;
+    this.inspectionHandler.activeCell = prompt;
+  }
+
+  /**
+   * Handle a change to the kernel.
+   */
+  private _onKernelChanged(): void {
+    let kernel = this.console.session.kernel;
+    this._completerHandler.kernel = kernel;
+    this.inspectionHandler.kernel = kernel;
+  }
+
   private _content: Console = null;
+  private _completer: CompleterWidget = null;
+  private _completerHandler: CellCompleterHandler = null;
+
 }
 
 
@@ -139,6 +216,21 @@ namespace ConsolePanel {
      * Create a new console panel.
      */
     createConsole(options: Console.IOptions): Console;
+
+    /**
+     * The inspection handler for a console widget.
+     */
+    createInspectionHandler(options: InspectionHandler.IOptions): InspectionHandler;
+
+    /**
+     * The completer widget for a console widget.
+     */
+    createCompleter(options: CompleterWidget.IOptions): CompleterWidget;
+
+    /**
+     * The completer handler for a console widget.
+     */
+    createCompleterHandler(options: CellCompleterHandler.IOptions): CellCompleterHandler;
   }
 
   /**
@@ -165,6 +257,26 @@ namespace ConsolePanel {
       return new Console(options);
     }
 
+    /**
+     * The inspection handler for a console widget.
+     */
+    createInspectionHandler(options: InspectionHandler.IOptions): InspectionHandler {
+      return new InspectionHandler(options);
+    }
+
+    /**
+     * The completer widget for a console widget.
+     */
+    createCompleter(options: CompleterWidget.IOptions): CompleterWidget {
+      return new CompleterWidget(options);
+    }
+
+    /**
+     * The completer handler for a console widget.
+     */
+   createCompleterHandler(options: CellCompleterHandler.IOptions): CellCompleterHandler {
+      return new CellCompleterHandler(options);
+   }
   }
 
   /**

+ 2 - 2
src/console/plugin.ts

@@ -165,7 +165,7 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
   // Set the source of the code inspector.
   tracker.currentChanged.connect((sender, widget) => {
     if (widget) {
-      inspector.source = widget.console.inspectionHandler;
+      inspector.source = widget.inspectionHandler;
     }
   });
 
@@ -388,7 +388,7 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
       panel.title.caption = Private.caption(captionOptions);
     });
     // Immediately set the inspector source to the current console.
-    inspector.source = panel.console.inspectionHandler;
+    inspector.source = panel.inspectionHandler;
     // Add the console panel to the tracker.
     tracker.add(panel);
     app.shell.addToMainArea(panel);

+ 9 - 88
src/console/widget.ts

@@ -25,14 +25,6 @@ import {
   Widget
 } from 'phosphor/lib/ui/widget';
 
-import {
-  CellCompleterHandler, CompleterWidget
-} from '../completer';
-
-import {
-  InspectionHandler
-} from '../inspector';
-
 import {
   IEditorMimeTypeService, CodeEditor
 } from '../codeeditor';
@@ -142,12 +134,6 @@ class Console extends Widget {
     // Set the banner text and the mimetype.
     this._initialize();
 
-    // Set up the inspection handler.
-    this.inspectionHandler = factory.createInspectionHandler({
-      kernel: this.session.kernel,
-      rendermime: this.rendermime
-    }, this);
-
     // Set up the foreign iopub handler.
     this._foreignHandler = factory.createForeignHandler({
       kernel: this.session.kernel,
@@ -158,30 +144,18 @@ class Console extends Widget {
     this._history = factory.createConsoleHistory({
       kernel: this.session.kernel
     }, this);
-
-    // Instantiate the completer.
-    this._completer = factory.createCompleter({}, this);
-
-    // Set the completer widget's anchor node to peg its position.
-    this._completer.anchor = this.node;
-
-    // Because a completer widget may be passed in, check if it is attached.
-    if (!this._completer.isAttached) {
-      Widget.attach(this._completer, document.body);
-    }
-
-    // Instantiate the completer handler.
-    this._completerHandler = factory.createCompleterHandler({
-      completer: this._completer,
-      kernel: this.session.kernel
-    }, this);
   }
 
   /**
-   * A signal emitted when the console executes its prompt.
+   * A signal emitted when the console finished executing its prompt.
    */
   readonly executed: ISignal<this, Date>;
 
+  /**
+   * A signal emitted when a new prompt is created.
+   */
+  readonly promptCreated: ISignal<this, CodeCellWidget>;
+
   /**
    * The content factory used by the console.
    */
@@ -192,11 +166,6 @@ class Console extends Widget {
    */
   readonly rendermime: IRenderMime;
 
-  /**
-   * The inspection handler used by the console.
-   */
-  readonly inspectionHandler: InspectionHandler;
-
   /**
    * The session used by the console.
    */
@@ -257,15 +226,10 @@ class Console extends Widget {
       return;
     }
     super.dispose();
-    this._completerHandler.dispose();
-    this._completerHandler = null;
-    this._completer.dispose();
-    this._completer = null;
     this._foreignHandler.dispose();
     this._foreignHandler = null;
     this._history.dispose();
     this._history = null;
-    this.inspectionHandler.dispose();
     this._cells.clear();
     this._cells = null;
   }
@@ -281,8 +245,6 @@ class Console extends Widget {
    * incomplete before attempting submission anyway. The default value is `250`.
    */
   execute(force = false, timeout = EXECUTION_TIMEOUT): Promise<void> {
-    this._completer.reset();
-
     if (this.session.status === 'dead') {
       return Promise.resolve(void 0);
     }
@@ -436,12 +398,10 @@ class Console extends Widget {
     editor.edgeRequested.connect(this.onEdgeRequest, this);
     editor.model.value.changed.connect(this.onTextChange, this);
 
-    // Associate the new prompt with the completer and inspection handlers.
-    this._completerHandler.activeCell = prompt;
-    this.inspectionHandler.activeCell = prompt;
-
     prompt.editor.focus();
     this.update();
+
+    this.promptCreated.emit(prompt);
   }
 
   /**
@@ -516,9 +476,7 @@ class Console extends Widget {
       this.newPrompt();
       this._initialize();
       this._history.kernel = kernel;
-      this._completerHandler.kernel = kernel;
       this._foreignHandler.kernel = kernel;
-      this.inspectionHandler.kernel = kernel;
     });
   }
 
@@ -640,8 +598,6 @@ class Console extends Widget {
 
   private _mimeTypeService: IEditorMimeTypeService;
   private _cells: IObservableVector<BaseCellWidget> = null;
-  private _completer: CompleterWidget = null;
-  private _completerHandler: CellCompleterHandler = null;
   private _content: Panel = null;
   private _foreignHandler: ForeignHandler =  null;
   private _history: IConsoleHistory = null;
@@ -654,6 +610,7 @@ class Console extends Widget {
 
 // Define the signals for the `Console` class.
 defineSignal(Console.prototype, 'executed');
+defineSignal(Console.prototype, 'promptCreated');
 
 
 /**
@@ -702,21 +659,6 @@ namespace Console {
      */
     readonly codeContentFactory: CodeCellWidget.IContentFactory;
 
-    /**
-     * The inspection handler for a console widget.
-     */
-    createInspectionHandler(options: InspectionHandler.IOptions, parent: Console): InspectionHandler;
-
-    /**
-     * The completer widget for a console widget.
-     */
-    createCompleter(options: CompleterWidget.IOptions, parent: Console): CompleterWidget;
-
-    /**
-     * The completer handler for a console widget.
-     */
-   createCompleterHandler(options: CellCompleterHandler.IOptions, parent: Console): CellCompleterHandler;
-
     /**
      * The history manager for a console widget.
      */
@@ -772,27 +714,6 @@ namespace Console {
      */
     readonly codeContentFactory: CodeCellWidget.IContentFactory;
 
-    /**
-     * The inspection handler for a console widget.
-     */
-    createInspectionHandler(options: InspectionHandler.IOptions, parent: Console): InspectionHandler {
-      return new InspectionHandler(options);
-    }
-
-    /**
-     * The completer widget for a console widget.
-     */
-    createCompleter(options: CompleterWidget.IOptions, parent: Console): CompleterWidget {
-      return new CompleterWidget(options);
-    }
-
-    /**
-     * The completer handler for a console widget.
-     */
-   createCompleterHandler(options: CellCompleterHandler.IOptions, parent: Console): CellCompleterHandler {
-      return new CellCompleterHandler(options);
-   }
-
     /**
      * The history manager for a console widget.
      */

+ 0 - 4
src/inspector/handler.ts

@@ -13,10 +13,6 @@ import {
   clearSignalData, defineSignal, ISignal
 } from 'phosphor/lib/core/signaling';
 
-import {
-  CodeEditor
-} from '../codeeditor';
-
 import {
   BaseCellWidget
 } from '../notebook/cells/widget';