From a7fd58cdc5b7416726e65300882e102b0b24759e Mon Sep 17 00:00:00 2001 From: Girish Sharma Date: Sun, 16 Feb 2014 13:56:26 +0530 Subject: [PATCH] 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 | 50 ++++++++----------- .../test/browser_styleeditor_autocomplete.js | 9 +++- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/browser/devtools/sourceeditor/autocomplete.js b/browser/devtools/sourceeditor/autocomplete.js index 42a6708d437f..71ea5da53f91 100644 --- a/browser/devtools/sourceeditor/autocomplete.js +++ b/browser/devtools/sourceeditor/autocomplete.js @@ -28,39 +28,34 @@ function setupAutoCompletion(ctx, walker) { 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); + 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; - }, - "Shift-Tab": cm => { - if (popup && popup.isOpen) { - cycleSuggestions(ed, true); - 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); @@ -172,7 +167,6 @@ function onEditorKeypress(ed, event) { 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; diff --git a/browser/devtools/styleeditor/test/browser_styleeditor_autocomplete.js b/browser/devtools/styleeditor/test/browser_styleeditor_autocomplete.js index 48780db02f72..af32e20958c4 100644 --- a/browser/devtools/styleeditor/test/browser_styleeditor_autocomplete.js +++ b/browser/devtools/styleeditor/test/browser_styleeditor_autocomplete.js @@ -49,7 +49,7 @@ let TEST_CASES = [ ['VK_RETURN', -1], ['b', 2, 0], ['u', 1, 0], - ['VK_TAB', -1], + ['VK_RETURN', -1, 0, 1], ['{', -1], ['VK_HOME', -1], ['VK_DOWN', -1], @@ -147,6 +147,13 @@ function checkState() { } 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();