Bug 971276 - Hitting enter should insert the currently selected item from an autocomplete list in the Style Editor. r=harth, a=sledru
authorGirish Sharma <scrapmachines@gmail.com>
Fri, 14 Feb 2014 01:13:28 +0530
changeset 182896 c6c359ce5f0287737c0031239640c707fa736742
parent 182895 1e578a03b49173071b79661207e29ca29a6f2fa8
child 182897 56b7db749e442def329d6772c0ea3228259ff469
push id3343
push userffxbld
push dateMon, 17 Mar 2014 21:55:32 +0000
treeherdermozilla-beta@2f7d3415f79f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersharth, sledru
bugs971276
milestone29.0a2
Bug 971276 - Hitting enter should insert the currently selected item from an autocomplete list in the Style Editor. r=harth, a=sledru
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,18 +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_ENTER:
-    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();