Bug 971276 - Hitting enter should insert the currently selected item from an autocomplete list in the Style Editor, r=harth
authorGirish Sharma <scrapmachines@gmail.com>
Sun, 16 Feb 2014 13:56:26 +0530
changeset 169431 1e176b92c297d6087f22a2fef59b3f672559fb3e
parent 169430 314377c20d231831969f044e0877caa8f5a610e8
child 169432 a1cf2184b51dcddabceb26f0f8a0a8474357367f
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersharth
bugs971276
milestone30.0a1
Bug 971276 - Hitting enter should insert the currently selected item from an autocomplete list in the Style Editor, r=harth
browser/devtools/sourceeditor/autocomplete.js
browser/devtools/styleeditor/test/browser_styleeditor_autocomplete.js
--- a/browser/devtools/sourceeditor/autocomplete.js
+++ b/browser/devtools/sourceeditor/autocomplete.js
@@ -23,49 +23,44 @@ function setupAutoCompletion(ctx, walker
 
   let popup = new AutocompletePopup(win.parent.document, {
     position: "after_start",
     fixedWidth: true,
     theme: "auto",
     autoSelect: true
   });
 
+  let cycle = (reverse) => {
+    if (popup && popup.isOpen) {
+      cycleSuggestions(ed, reverse == true);
+      return;
+    }
+
+    return win.CodeMirror.Pass;
+  };
+
   let keyMap = {
-    "Tab": cm => {
+    "Tab": cycle,
+    "Down": cycle,
+    "Shift-Tab": cycle.bind(this, true),
+    "Up": cycle.bind(this, true),
+    "Enter": () => {
       if (popup && popup.isOpen) {
-        cycleSuggestions(ed);
-        return;
-      }
-
-      return win.CodeMirror.Pass;
-    },
-    "Shift-Tab": cm => {
-      if (popup && popup.isOpen) {
-        cycleSuggestions(ed, true);
+        if (!privates.get(ed).suggestionInsertedOnce) {
+          privates.get(ed).insertingSuggestion = true;
+          let {label, preLabel} = popup.getItemAtIndex(0);
+          let cur = ed.getCursor();
+          ed.replaceText(label.slice(preLabel.length), cur, cur);
+        }
+        popup.hidePopup();
         return;
       }
 
       return win.CodeMirror.Pass;
-    },
-    "Up": cm => {
-      if (popup && popup.isOpen) {
-        cycleSuggestions(ed, true);
-        return;
-      }
-
-      return win.CodeMirror.Pass;
-    },
-    "Down": cm => {
-      if (popup && popup.isOpen) {
-        cycleSuggestions(ed);
-        return;
-      }
-
-      return win.CodeMirror.Pass;
-    },
+    }
   };
   keyMap[Editor.accel("Space")] = cm => autoComplete(ctx);
   cm.addKeyMap(keyMap);
 
   cm.on("keydown", (cm, e) => onEditorKeypress(ed, e));
   ed.on("change", () => autoComplete(ctx));
   ed.on("destroy", () => {
     cm.off("keydown", (cm, e) => onEditorKeypress(ed, e));
@@ -167,17 +162,16 @@ function onEditorKeypress(ed, event) {
       if (private.popup.isOpen)
         event.preventDefault();
     case event.DOM_VK_LEFT:
     case event.DOM_VK_RIGHT:
     case event.DOM_VK_HOME:
     case event.DOM_VK_END:
     case event.DOM_VK_BACK_SPACE:
     case event.DOM_VK_DELETE:
-    case event.DOM_VK_RETURN:
       private.doNotAutocomplete = true;
       private.popup.hidePopup();
       break;
 
     default:
       private.doNotAutocomplete = false;
   }
 }
--- a/browser/devtools/styleeditor/test/browser_styleeditor_autocomplete.js
+++ b/browser/devtools/styleeditor/test/browser_styleeditor_autocomplete.js
@@ -44,17 +44,17 @@ let TEST_CASES = [
   ['VK_TAB', 4, 1, 1],
   ['VK_TAB', 4, 2, 1],
   ['VK_LEFT', -1],
   ['VK_RIGHT', -1],
   ['VK_DOWN', -1],
   ['VK_RETURN', -1],
   ['b', 2, 0],
   ['u', 1, 0],
-  ['VK_TAB', -1],
+  ['VK_RETURN', -1, 0, 1],
   ['{', -1],
   ['VK_HOME', -1],
   ['VK_DOWN', -1],
   ['VK_DOWN', -1],
   ['VK_RIGHT', -1],
   ['VK_RIGHT', -1],
   ['VK_RIGHT', -1],
   ['VK_RIGHT', -1],
@@ -142,16 +142,23 @@ function checkState() {
         let { line, ch } = gEditor.getCursor();
         let lineText = gEditor.getText(line);
         is(lineText.substring(ch - label.length, ch), label,
            "Current suggestion from the popup is inserted into the editor.");
       }
     }
     else {
       ok(!gPopup.isOpen, "Popup is closed for index " + index);
+      if (inserted) {
+        let { preLabel, label } = gPopup.getItemAtIndex(current);
+        let { line, ch } = gEditor.getCursor();
+        let lineText = gEditor.getText(line);
+        is(lineText.substring(ch - label.length, ch), label,
+           "Current suggestion from the popup is inserted into the editor.");
+      }
     }
     index++;
     testState();
   });
 }
 
 function testAutocompletionDisabled() {
   gBrowser.removeCurrentTab();