Browse Source

Merge pull request #233 from afshin/completion-tweak

Completion tweak
Jason Grout 9 years ago
parent
commit
fd92ef9b9a
2 changed files with 11 additions and 14 deletions
  1. 10 8
      src/notebook/cells/editor.ts
  2. 1 6
      src/notebook/console/widget.ts

+ 10 - 8
src/notebook/cells/editor.ts

@@ -128,13 +128,15 @@ class CellEditorWidget extends CodeMirrorWidget {
     this._model = model;
     let editor = this.editor;
     let doc = editor.getDoc();
+    if (model.source) {
+      doc.setValue(model.source);
+    }
     CodeMirror.on(doc, 'change', (instance, change) => {
       this.onDocChange(instance, change);
     });
     CodeMirror.on(editor, 'keydown', (instance, evt) => {
       this.onEditorKeydown(instance, evt);
     });
-    this.update();
     model.stateChanged.connect(this.onModelChanged, this);
   }
 
@@ -187,14 +189,11 @@ class CellEditorWidget extends CodeMirrorWidget {
   }
 
   /**
-   * Handle an `update-request` message.
+   * Set the current cursor position of the editor.
    */
-  protected onUpdateRequest(msg: Message): void {
+  setCursorPosition(position: number): void {
     let doc = this.editor.getDoc();
-    let value = doc.getValue();
-    if (value !== this._model.source) {
-      doc.setValue(this._model.source);
-    }
+    doc.setCursor(doc.posFromIndex(position));
   }
 
   /**
@@ -203,7 +202,10 @@ class CellEditorWidget extends CodeMirrorWidget {
   protected onModelChanged(model: ICellModel, args: IChangedArgs<any>): void {
     switch (args.name) {
     case 'source':
-      this.update();
+      let doc = this.editor.getDoc();
+      if (doc.getValue() !== args.newValue) {
+        doc.setValue(args.newValue);
+      }
       break;
     default:
       break;

+ 1 - 6
src/notebook/console/widget.ts

@@ -566,13 +566,8 @@ class ConsoleWidget extends Widget {
   protected onCompletionSelect(widget: CompletionWidget, value: string): void {
     let prompt = this.prompt;
     let patch = this._completion.model.createPatch(value);
-    let doc = prompt.editor.editor.getDoc();
-    // Update the prompt model.
     prompt.model.source = patch.text;
-    // Because the prompt model triggers a DOM update asynchronously,
-    // CodeMirror value and position need to be set manually (synchronously).
-    doc.setValue(patch.text);
-    doc.setCursor(doc.posFromIndex(patch.position));
+    prompt.editor.setCursorPosition(patch.position);
   }
 
   private _completion: CompletionWidget = null;