Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
All tests pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
petetnt authored and bmax committed Jul 2, 2016
1 parent 47ed6b5 commit 50619f2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 39 deletions.
8 changes: 5 additions & 3 deletions src/editor/CodeHintList.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};
Expand Down Expand Up @@ -394,19 +395,20 @@ 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.
return false;
} 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());
Expand Down
50 changes: 14 additions & 36 deletions src/editor/CodeHintManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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);

Expand Down
82 changes: 82 additions & 0 deletions test/spec/CodeHint-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 50619f2

Please sign in to comment.