浏览代码

Merge pull request #768 from afshin/autocomplete-bug

Bugfix: inspector not receiving events on autocompletion selections.
Steven Silvester 8 年之前
父节点
当前提交
0141b319c3

+ 2 - 8
src/notebook/codemirror/cells/editor.ts

@@ -215,18 +215,10 @@ class CodeMirrorCellEditorWidget extends CodeMirrorWidget implements ICellEditor
    * Handle change events from the document.
    */
   protected onDocChange(doc: CodeMirror.Doc, change: CodeMirror.EditorChange): void {
-    if (change.origin === 'setValue') {
-      return;
-    }
     let model = this.model;
     let editor = this.editor;
     let oldValue = model.source;
     let newValue = doc.getValue();
-    if (oldValue === newValue) {
-      return;
-    }
-    model.source = newValue;
-
     let cursor = doc.getCursor();
     let line = cursor.line;
     let ch = cursor.ch;
@@ -234,6 +226,8 @@ class CodeMirrorCellEditorWidget extends CodeMirrorWidget implements ICellEditor
     let chWidth = editor.defaultCharWidth();
     let coords = editor.charCoords({ line, ch }, 'page') as ICoords;
     let position = editor.getDoc().indexFromPos({ line, ch });
+
+    model.source = newValue;
     this.textChanged.emit({
       line, ch, chHeight, chWidth, coords, position, oldValue, newValue
     });

+ 4 - 20
test/src/notebook/cells/editor.spec.ts

@@ -133,10 +133,10 @@ describe('notebook/cells/editor', () => {
         };
         widget.textChanged.connect(listener);
 
-        // CodeMirrorCellEditorWidget suppresses signals when the code mirror instance's
-        // content is changed programmatically via the `setValue` method, so
-        // for this test, the `replaceRange` method is being used to generate
-        // the text change.
+        // CodeMirrorCellEditorWidget suppresses signals when the code mirror
+        // instance's content is changed programmatically via the `setValue`
+        // method, so for this test, the `replaceRange` method is being used to
+        // generate the text change.
         expect(change).to.not.be.ok();
         doc.replaceRange(want.newValue, fromPos, toPos);
         expect(change).to.be.ok();
@@ -144,22 +144,6 @@ describe('notebook/cells/editor', () => {
         expect(change.newValue).to.equal(want.newValue);
       });
 
-      it('should not emit a signal if editor text already matches model', () => {
-        let widget = new CodeMirrorCellEditorWidget();
-        widget.model = new CellModel();
-        let doc = widget.editor.getDoc();
-        let fromPos = { line: 0, ch: 0 };
-        let toPos = { line: 0, ch: 3 };
-        let called = false;
-        let listener = (sender: any, args: ITextChange) => { called = true; };
-        widget.model.source = 'foo';
-        widget.textChanged.connect(listener);
-
-        expect(called).to.be(false);
-        doc.replaceRange('foo', fromPos, toPos);
-        expect(called).to.be(false);
-      });
-
     });
 
     describe('#completionRequested', () => {

+ 6 - 4
test/src/notebook/completion/handler.spec.ts

@@ -413,8 +413,9 @@ describe('notebook/completion/handler', () => {
         });
         let handler = new TestCompletionHandler(completion);
         let model = completion.model as TestCompletionModel;
+        let renderer = CodeMirrorCodeCellWidgetRenderer.defaultRenderer;
 
-        handler.activeCell = new BaseCellWidget({renderer:CodeMirrorCodeCellWidgetRenderer.defaultRenderer});
+        handler.activeCell = new BaseCellWidget({ renderer });
         expect(model.methods).to.not.contain('createPatch');
         completion.selected.emit('foo');
         expect(model.methods).to.contain('createPatch');
@@ -425,7 +426,8 @@ describe('notebook/completion/handler', () => {
         let patch = 'foobar';
         let completion = new CompletionWidget({ model });
         let handler = new TestCompletionHandler(completion);
-        let cell = new BaseCellWidget({renderer:CodeMirrorCodeCellWidgetRenderer.defaultRenderer});
+        let renderer = CodeMirrorCodeCellWidgetRenderer.defaultRenderer;
+        let cell = new BaseCellWidget({ renderer });
         let request: ICompletionRequest = {
           ch: 0,
           chHeight: 0,
@@ -437,10 +439,10 @@ describe('notebook/completion/handler', () => {
         };
 
         cell.model = new CellModel();
-        model.original = request;
-        model.cursor = { start: 0, end: 3 };
         handler.activeCell = cell;
         handler.activeCell.model.source = request.currentValue;
+        model.original = request;
+        model.cursor = { start: 0, end: 3 };
         completion.selected.emit(patch);
         expect(handler.activeCell.model.source).to.equal(patch);
       });