From e9336d7edae5734873eb9d304deec9f62c20b292 Mon Sep 17 00:00:00 2001 From: Pete Nykanen Date: Fri, 1 Apr 2016 20:14:38 +0300 Subject: [PATCH] All tests pass. --- src/editor/CodeHintList.js | 8 ++-- src/editor/CodeHintManager.js | 50 ++++++--------------- test/spec/CodeHint-test.js | 82 +++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 39 deletions(-) diff --git a/src/editor/CodeHintList.js b/src/editor/CodeHintList.js index 24b8e9203b9..824c091a180 100644 --- a/src/editor/CodeHintList.js +++ b/src/editor/CodeHintList.js @@ -320,6 +320,7 @@ define(function (require, exports, module) { keyCode === KeyEvent.DOM_VK_PAGE_UP || keyCode === KeyEvent.DOM_VK_PAGE_DOWN || keyCode === KeyEvent.DOM_VK_RETURN || keyCode === KeyEvent.DOM_VK_CONTROL || + keyCode === KeyEvent.DOM_VK_ESCAPE || (ctrlKey && keyCode === KeyEvent.DOM_VK_SPACE) || (keyCode === KeyEvent.DOM_VK_TAB && this.insertHintOnTab)); }; @@ -394,11 +395,12 @@ define(function (require, exports, module) { if (event.type === "keydown" && this.isHandlingKeyCode(event)) { keyCode = event.keyCode; - if (event.shiftKey && + if (event.keyCode === KeyEvent.DOM_VK_ESCAPE || + (event.shiftKey && (event.keyCode === KeyEvent.DOM_VK_UP || event.keyCode === KeyEvent.DOM_VK_DOWN || event.keyCode === KeyEvent.DOM_VK_PAGE_UP || - event.keyCode === KeyEvent.DOM_VK_PAGE_DOWN)) { + event.keyCode === KeyEvent.DOM_VK_PAGE_DOWN))) { this.handleClose(); // Let the event bubble. @@ -406,7 +408,7 @@ define(function (require, exports, module) { } else if (keyCode === KeyEvent.DOM_VK_UP) { _rotateSelection.call(this, -1); } else if (keyCode === KeyEvent.DOM_VK_DOWN || - (event.ctrlKey && keyCode === KeyEvent.DOM_VK_SPACE)) { + (event.ctrlKey && keyCode === KeyEvent.DOM_VK_SPACE)) { _rotateSelection.call(this, 1); } else if (keyCode === KeyEvent.DOM_VK_PAGE_UP) { _rotateSelection.call(this, -_itemsPerPage()); diff --git a/src/editor/CodeHintManager.js b/src/editor/CodeHintManager.js index c443b150ed1..e303a1068a8 100644 --- a/src/editor/CodeHintManager.js +++ b/src/editor/CodeHintManager.js @@ -447,11 +447,7 @@ define(function (require, exports, module) { } else if (response.hasOwnProperty("hints")) { // a synchronous response if (hintList.isOpen()) { // the session is open - if (callMoveUpEvent) { - hintList.callMoveUp(callMoveUpEvent); - } else { - hintList.update(response); - } + hintList.update(response); } else { hintList.open(response); } @@ -468,11 +464,7 @@ define(function (require, exports, module) { if (hintList.isOpen()) { // the session is open - if (callMoveUpEvent) { - hintList.callMoveUp(callMoveUpEvent); - } else { - hintList.update(response); - } + hintList.update(hints); } else { hintList.open(hints); } @@ -535,31 +527,6 @@ define(function (require, exports, module) { } }; - /** - * Explicitly start a new session. If we have an existing session, - * then close the current one and restart a new one. - * @param {Editor} editor - */ - function _startNewSession(editor) { - - if (isOpen()) { - return; - } - - if (!editor) { - editor = EditorManager.getFocusedEditor(); - } - if (editor) { - lastChar = null; - if (_inSession(editor)) { - _endSession(); - } - - // Begin a new explicit session - _beginSession(editor); - } - } - /** * Handles keys related to displaying, searching, and navigating the hint list. * This gets called before handleChange. @@ -596,7 +563,8 @@ define(function (require, exports, module) { function _handleKeyupEvent(jqEvent, editor, event) { keyDownEditor = editor; if (_inSession(editor)) { - if (event.keyCode === KeyEvent.DOM_VK_HOME || event.keyCode === KeyEvent.DOM_VK_END) { + if (event.keyCode === KeyEvent.DOM_VK_HOME || + event.keyCode === KeyEvent.DOM_VK_END) { _endSession(); } else if (event.keyCode === KeyEvent.DOM_VK_LEFT || event.keyCode === KeyEvent.DOM_VK_RIGHT || @@ -744,6 +712,16 @@ define(function (require, exports, module) { activeEditorChangeHandler(null, EditorManager.getActiveEditor(), null); EditorManager.on("activeEditorChange", activeEditorChangeHandler); + + // Dismiss code hints before executing any command other than showing code hints since the command + // may make the current hinting session irrevalent after execution. + // For example, when the user hits Ctrl+K to open Quick Doc, it is + // pointless to keep the hint list since the user wants to view the Quick Doc + CommandManager.on("beforeExecuteCommand", function (event, commandId) { + if (commandId !== Commands.SHOW_CODE_HINTS) { + _endSession(); + } + }); CommandManager.register(Strings.CMD_SHOW_CODE_HINTS, Commands.SHOW_CODE_HINTS, _startNewSession); diff --git a/test/spec/CodeHint-test.js b/test/spec/CodeHint-test.js index 0d1c4f24ae1..b78297a36d3 100644 --- a/test/spec/CodeHint-test.js +++ b/test/spec/CodeHint-test.js @@ -279,6 +279,88 @@ define(function (require, exports, module) { }); }); + it("should go to next hint with ctrl+space", function () { + var editor, + pos = {line: 3, ch: 1}, + hintBefore, + hintAfter; + + // minimal markup with an open '<' before IP + // Note: line for pos is 0-based and editor lines numbers are 1-based + initCodeHintTest("test1.html", pos); + + // simulate ctrl+space key to make sure it goes to next hint + runs(function () { + var e = $.Event("keydown"); + e.keyCode = KeyEvent.DOM_VK_SPACE; + e.ctrlKey = true; + + editor = EditorManager.getCurrentFullEditor(); + expect(editor).toBeTruthy(); + + invokeCodeHints(); + var codeHintList = expectSomeHints(); + hintBefore = codeHintList.selectedIndex; + + // make sure hint list starts at 0 + expect(hintBefore).toEqual(0); + + // simulate ctrl+space keyhook + CodeHintManager._getCodeHintList()._keydownHook(e); + hintAfter = codeHintList.selectedIndex; + + // selectedIndex should be one more after doing ctrl+space key event. + expect(hintBefore).toEqual(hintAfter-1); + + editor = null; + }); + }); + + it("should loop to first hint when ctrl+space at last hint", function () { + var editor, + pos = {line: 3, ch: 1}, + hintBefore, + hintAfter; + + // minimal markup with an open '<' before IP + // Note: line for pos is 0-based and editor lines numbers are 1-based + initCodeHintTest("test1.html", pos); + + // simulate ctrl+space key to make sure it goes to next hint + runs(function () { + var e = $.Event("keydown"); + e.keyCode = KeyEvent.DOM_VK_UP; + + editor = EditorManager.getCurrentFullEditor(); + expect(editor).toBeTruthy(); + + invokeCodeHints(); + + // simulate up keyhook to send it to last hint + CodeHintManager._getCodeHintList()._keydownHook(e); + + var codeHintList = expectSomeHints(); + hintBefore = codeHintList.selectedIndex; + var numberOfHints = codeHintList.$hintMenu.find("li").length-1; + + // should be at last hint + expect(hintBefore).toEqual(numberOfHints); + + // call ctrl+space to loop it to first hint + e.keyCode = KeyEvent.DOM_VK_SPACE; + e.ctrlKey = true; + + // simulate ctrl+space keyhook to send it to first hint + CodeHintManager._getCodeHintList()._keydownHook(e); + hintAfter = codeHintList.selectedIndex; + + // should now be at hint 0 + expect(hintAfter).toEqual(0); + + editor = null; + }); + }); + it("should not show code hints if there is a multiple selection", function () { // minimal markup with an open '<' before IP // Note: line for pos is 0-based and editor lines numbers are 1-based