Browse Source

Bugfix for state changed infinite loop. Bugfix for null patch. Implement IPython tab cycling. Implement arrow cycling.

A. Darian 8 years ago
parent
commit
0305dc0185
3 changed files with 15 additions and 8 deletions
  1. 5 1
      src/console/widget.ts
  2. 3 3
      src/notebook/completion/model.ts
  3. 7 4
      src/notebook/completion/widget.ts

+ 5 - 1
src/console/widget.ts

@@ -549,8 +549,12 @@ class ConsoleWidget extends Widget {
    * Handle a completion selected signal from the completion widget.
    */
   protected onCompletionSelect(widget: CompletionWidget, value: string): void {
-    let prompt = this.prompt;
     let patch = this._completion.model.createPatch(value);
+    if (!patch) {
+      return;
+    }
+
+    let prompt = this.prompt;
     prompt.model.source = patch.text;
     prompt.editor.setCursorPosition(patch.position);
   }

+ 3 - 3
src/notebook/completion/model.ts

@@ -233,7 +233,6 @@ class CompletionModel implements ICompletionModel {
     // If the text change means that the original start point has been preceded,
     // then the completion is no longer valid and should be reset.
     if (currentLine.length < originalLine.length) {
-      console.log('A');
       this.reset();
     } else {
       let {start, end} = this._cursor;
@@ -355,10 +354,11 @@ class CompletionModel implements ICompletionModel {
    * Reset the state of the model.
    */
   reset() {
-    this.original = null;
-    this.options = null;
+    this._original = null;
     this._query = '';
     this._cursor = null;
+    this._options = null;
+    this.stateChanged.emit(void 0);
   }
 
   /**

+ 7 - 4
src/notebook/completion/widget.ts

@@ -287,7 +287,6 @@ class CompletionWidget extends Widget {
       if (target === this._reference.node) {
         switch (event.keyCode) {
         case 13: // Enter key
-        case 9: // Tab key
           event.preventDefault();
           event.stopPropagation();
           event.stopImmediatePropagation();
@@ -301,17 +300,21 @@ class CompletionWidget extends Widget {
           event.stopImmediatePropagation();
           this._model.reset();
           return;
+        case 9: // Tab key
         case 38: // Up arrow key
         case 40: // Down arrow key
           event.preventDefault();
           event.stopPropagation();
           event.stopImmediatePropagation();
           let items = this.node.querySelectorAll(`.${ITEM_CLASS}`);
+          let index = this._activeIndex;
           active = node.querySelector(`.${ACTIVE_CLASS}`) as HTMLElement;
           active.classList.remove(ACTIVE_CLASS);
-          this._activeIndex = event.keyCode === 38 ?
-            Math.max(--this._activeIndex, 0)
-              : Math.min(++this._activeIndex, items.length - 1);
+          if (event.keyCode === 38) { // For up arrow, cycle up.
+            this._activeIndex = index === 0 ? items.length - 1 : index - 1;
+          } else { // For down arrow or tab, cycle down.
+            this._activeIndex = index < items.length - 1 ? index + 1 : 0;
+          }
           active = items[this._activeIndex] as HTMLElement;
           active.classList.add(ACTIVE_CLASS);
           Private.scrollIfNeeded(this.node, active);