浏览代码

Switch console over to CompletionHandler.

A. Darian 8 年之前
父节点
当前提交
5a70d088a9
共有 2 个文件被更改,包括 17 次插入33 次删除
  1. 10 28
      src/console/widget.ts
  2. 7 5
      src/notebook/completion/handler.ts

+ 10 - 28
src/console/widget.ts

@@ -54,7 +54,7 @@ import {
 } from './history';
 
 import {
-  CompletionWidget, CompletionModel
+  CompletionWidget, CompletionModel, CellCompletionHandler
 } from '../notebook/completion';
 
 
@@ -248,7 +248,8 @@ class ConsoleWidget extends Widget {
     this._completion = constructor.createCompletion();
     this._completion.reference = this;
     this._completion.attach(document.body);
-    this._completion.selected.connect(this.onCompletionSelect, this);
+    this._completionHandler = new CellCompletionHandler(this._completion);
+    this._completionHandler.kernel = session.kernel;
 
     // Instantiate tooltip widget.
     this._tooltip = constructor.createTooltip();
@@ -273,6 +274,7 @@ class ConsoleWidget extends Widget {
       this.newPrompt();
       this.initialize();
       this._history = constructor.createHistory(kernel);
+      this._completionHandler.kernel = kernel;
     });
   }
 
@@ -307,6 +309,8 @@ class ConsoleWidget extends Widget {
     this._tooltip = null;
     this._history.dispose();
     this._history = null;
+    this._completionHandler.dispose();
+    this._completionHandler = null;
     this._completion.dispose();
     this._completion = null;
     this._session.dispose();
@@ -392,9 +396,11 @@ class ConsoleWidget extends Widget {
     // Hook up completion, tooltip, and history handling.
     let editor = prompt.editor;
     editor.textChanged.connect(this.onTextChange, this);
-    editor.completionRequested.connect(this.onCompletionRequest, this);
     editor.edgeRequested.connect(this.onEdgeRequest, this);
 
+    // Associate the new prompt with the completion handler.
+    this._completionHandler.activeCell = prompt;
+
     prompt.focus();
   }
 
@@ -511,17 +517,6 @@ class ConsoleWidget extends Widget {
     tooltip.node.style.maxWidth = `${maxWidth}px`;
   }
 
-  /**
-   * Handle a completion requested signal from an editor.
-   */
-  protected onCompletionRequest(editor: CellEditorWidget, change: ICompletionRequest): void {
-    let kernel = this._session.kernel;
-    if (!kernel) {
-      return;
-    }
-    this._completion.model.makeKernelRequest(change, kernel);
-  }
-
   /**
    * Handle an edge requested signal.
    */
@@ -545,21 +540,8 @@ class ConsoleWidget extends Widget {
     }
   }
 
-  /**
-   * Handle a completion selected signal from the completion widget.
-   */
-  protected onCompletionSelect(widget: CompletionWidget, value: string): void {
-    let patch = this._completion.model.createPatch(value);
-    if (!patch) {
-      return;
-    }
-
-    let prompt = this.prompt;
-    prompt.model.source = patch.text;
-    prompt.editor.setCursorPosition(patch.position);
-  }
-
   private _completion: CompletionWidget = null;
+  private _completionHandler: CellCompletionHandler = null;
   private _mimetype = 'text/x-ipython';
   private _rendermime: RenderMime<Widget> = null;
   private _tooltip: ConsoleTooltip = null;

+ 7 - 5
src/notebook/completion/handler.ts

@@ -99,7 +99,7 @@ class CellCompletionHandler implements IDisposable {
    * Handle a completion requested signal from an editor.
    */
   private _onCompletionRequested(editor: CellEditorWidget, change: ICompletionRequest): void {
-    if (!this.kernel) {
+    if (!this.kernel || !this._completion.model) {
       return;
     }
     this._completion.model.makeKernelRequest(change, this.kernel);
@@ -113,10 +113,12 @@ class CellCompletionHandler implements IDisposable {
       return;
     }
     let patch = this._completion.model.createPatch(value);
-    let editor = this._activeCell.editor.editor;
-    let doc = editor.getDoc();
-    doc.setValue(patch.text);
-    doc.setCursor(doc.posFromIndex(patch.position));
+    if (!patch) {
+      return;
+    }
+    let cell = this._activeCell;
+    cell.model.source = patch.text;
+    cell.editor.setCursorPosition(patch.position);
   }
 
   private _kernel: IKernel = null;