Close hints when the cursor moves to a new hinting context

This commit is contained in:
dloverin committed 2013-04-26 09:57:08 -04:00
1 parent 612188b9c3
commit 52d98980f0
4 files changed
+172 -20

No files matched your search

@@ -107,7 +107,24 @@ define(function (require, exports, module) {
return cm.getTokenAt(this.getCursor());
}
};
/**
* Get the token after the one at the given cursor position
*
* @param {{line: number, ch: number}} cursor - cursor position before
* which a token should be retrieved
* @return {Object} - the CodeMirror token after the one at the given
* cursor position
*/
Session.prototype.getNextTokenOnLine = function (cursor) {
cursor = this.getNextCursorOnLine(cursor);
if (cursor) {
return this.getToken(cursor);
}
return null;
};
/**
* Get the next cursor position on the line, or null if there isn't one.
*
@@ -115,9 +132,8 @@ define(function (require, exports, module) {
* immediately following the current cursor position, or null if
* none exists.
*/
Session.prototype.getNextCursorOnLine = function () {
var cursor = this.getCursor(),
doc = this.editor.document,
Session.prototype.getNextCursorOnLine = function (cursor) {
var doc = this.editor.document,
line = doc.getLine(cursor.line);
if (cursor.ch < line.length) {
@@ -46,11 +46,22 @@ define(function (require, exports, module) {
var JUMPTO_DEFINITION = "navigate.jumptoDefinition";
var session = null, // object that encapsulates the current session state
cachedHints = null, // sorted hints for the current hinting session
cachedType = null, // describes the lookup type and the object context
cachedLine = null, // the line number for the cached scope
matcher = null; // string matcher for hints
var session = null, // object that encapsulates the current session state
cachedCursor = null, // last cursor of the current hinting session
cachedHints = null, // sorted hints for the current hinting session
cachedType = null, // describes the lookup type and the object context
cachedToken = null, // the token used in the current hinting session
matcher = null; // string matcher for hints
/**
* Get the value of current session.
* Used for unit testing.
* @returns {Session} - the current session.
*/
function getSession() {
return session;
}
/**
* Creates a hint response object. Filters the hint list using the query
@@ -173,14 +184,56 @@ define(function (require, exports, module) {
JSHints.prototype.needNewHints = function (session) {
var cursor = session.getCursor(),
type = session.getType();
return !cachedHints ||
cachedLine !== cursor.line ||
cachedCursor.line !== cursor.line ||
type.property !== cachedType.property ||
type.context !== cachedType.context ||
type.showFunctionType !== cachedType.showFunctionType;
};
/**
* Have conditions have changed enough to justify closing the hints popup?
*
* @param {Session} session - the active hinting session
* @return {Boolean} - true if the hints popup should be closed.
*/
JSHints.prototype.shouldCloseHints = function (session) {
// close if the token className has changed then close the hints.
var cursor = session.getCursor(),
token = session.getToken(cursor),
lastToken = cachedToken;
// if the line has changed, then close the hints
if (!cachedCursor || cursor.line !== cachedCursor.line) {
return true;
}
if (token.className === null) {
token = session.getNextTokenOnLine(cursor);
}
if (lastToken && lastToken.className === null) {
lastToken = session.getNextTokenOnLine(cachedCursor);
}
// if (token)
// console.log("token: " + token.className + " " + token.string);
//
// if (lastToken)
// console.log("last : " + lastToken.className + " " + lastToken.string);
// Both of the tokens should never be null (happens when token is off
// the end of the line), so one is null then close the hints.
if (!lastToken || !token ||
token.className !== lastToken.className ||
token.string !== lastToken.string) {
return true;
}
return false;
};
/**
* Determine whether hints are available for a given editor context
*
@@ -200,9 +253,9 @@ define(function (require, exports, module) {
query = session.getQuery();
if (this.needNewHints(session)) {
//console.log("clear hints");
cachedLine = null;
cachedCursor = null;
cachedHints = null;
cachedToken = null;
matcher = null;
}
return true;
@@ -226,6 +279,15 @@ define(function (require, exports, module) {
var type = session.getType(),
query = session.getQuery();
// If the hint context is changed and the hints are open, then
// close the hints by returning null;
if (CodeHintManager.isOpen() && this.shouldCloseHints(session)) {
return null;
}
cachedCursor = cursor;
cachedToken = token;
// Compute fresh hints if none exist, or if the session
// type has changed since the last hint computation
if (this.needNewHints(session)) {
@@ -235,8 +297,8 @@ define(function (require, exports, module) {
if (scopeResponse.hasOwnProperty("promise")) {
var $deferredHints = $.Deferred();
scopeResponse.promise.done(function () {
cachedLine = cursor.line;
cachedType = session.getType();
matcher = new StringMatch.StringMatcher();
cachedHints = session.getHints(query, matcher);
@@ -253,8 +315,6 @@ define(function (require, exports, module) {
});
return $deferredHints;
} else {
cachedLine = cursor.line;
}
}
@@ -285,7 +345,7 @@ define(function (require, exports, module) {
delimiter;
if (token && token.string === ".") {
var nextCursor = session.getNextCursorOnLine(),
var nextCursor = session.getNextCursorOnLine(cursor),
nextToken = nextCursor ? session.getToken(nextCursor) : null;
if (nextToken && // don't replace delimiters, etc.
@@ -347,7 +407,7 @@ define(function (require, exports, module) {
*/
function installEditorListeners(editor) {
// always clean up cached scope and hint info
cachedLine = null;
cachedCursor = null;
cachedHints = null;
cachedType = null;
@@ -440,6 +500,7 @@ define(function (require, exports, module) {
CodeHintManager.registerHintProvider(jsHints, [HintUtils.LANGUAGE_ID], 0);
// for unit testing
exports.getSession = getSession;
exports.jsHintProvider = jsHints;
exports.initializeSession = initializeSession;
exports.handleJumpToDefinition = handleJumpToDefinition;
@@ -148,6 +148,22 @@ define(function (require, exports, module) {
runs(function () { callback(hintList); });
}
/*
* Test if hints are close or not closed at a given position.
*
* @param {Object} provider - a CodeHintProvider object
* @param {Object + jQuery.Deferred} hintObj - a hint response object,
* possibly deferred
* @param {line, ch} - new position to move to after hints are received.
* @param {Boolean} expectedValue - true if hints should close,
* false otherwise.
*/
function expectCloseHints(provider, hintObj, newPos, expectedValue) {
_waitForHints(hintObj, function (hintList) {
testEditor.setCursorPos(newPos);
expect(provider.shouldCloseHints(JSCodeHints.getSession())).toBe(expectedValue);
});
}
/*
* Expect a given list of hints to be absent from a given hint
@@ -443,7 +459,56 @@ define(function (require, exports, module) {
var hintObj = expectHints(JSCodeHints.jsHintProvider);
hintsAbsent(hintObj, ["case", "function", "var"]);
});
it("should close hints when move over '.' ", function () {
testEditor.setCursorPos({ line: 17, ch: 11 });
var hintObj = expectHints(JSCodeHints.jsHintProvider);
expectCloseHints(JSCodeHints.jsHintProvider, hintObj,
{ line: 17, ch: 10 }, true);
});
it("should close hints only when move off the end of a property ", function () {
testEditor.setCursorPos({ line: 17, ch: 11 });
var hintObj = expectHints(JSCodeHints.jsHintProvider);
expectCloseHints(JSCodeHints.jsHintProvider, hintObj,
{ line: 17, ch: 12 }, false);
expectCloseHints(JSCodeHints.jsHintProvider, hintObj,
{ line: 17, ch: 13 }, false);
expectCloseHints(JSCodeHints.jsHintProvider, hintObj,
{ line: 17, ch: 14 }, false);
expectCloseHints(JSCodeHints.jsHintProvider, hintObj,
{ line: 17, ch: 15 }, false);
expectCloseHints(JSCodeHints.jsHintProvider, hintObj,
{ line: 17, ch: 16 }, false);
expectCloseHints(JSCodeHints.jsHintProvider, hintObj,
{ line: 17, ch: 17 }, true);
});
it("should close hints only when move off the beginning of an identifier ", function () {
testEditor.setCursorPos({ line: 17, ch: 10 });
var hintObj = expectHints(JSCodeHints.jsHintProvider);
expectCloseHints(JSCodeHints.jsHintProvider, hintObj,
{ line: 17, ch: 9 }, false);
expectCloseHints(JSCodeHints.jsHintProvider, hintObj,
{ line: 17, ch: 8 }, false);
expectCloseHints(JSCodeHints.jsHintProvider, hintObj,
{ line: 17, ch: 7 }, true);
});
it("should close hints only when move off the beginning of a keyword ", function () {
testEditor.setCursorPos({ line: 24, ch: 7 });
var hintObj = expectHints(JSCodeHints.jsHintProvider);
hintsPresent(hintObj, ["var"]);
expectCloseHints(JSCodeHints.jsHintProvider, hintObj,
{ line: 24, ch: 6 }, false);
expectCloseHints(JSCodeHints.jsHintProvider, hintObj,
{ line: 24, ch: 5 }, false);
expectCloseHints(JSCodeHints.jsHintProvider, hintObj,
{ line: 24, ch: 4 }, false);
expectCloseHints(JSCodeHints.jsHintProvider, hintObj,
{ line: 24, ch: 3 }, true);
});
it("should NOT list implicit hints on left-brace", function () {
testEditor.setCursorPos({ line: 6, ch: 0 });
expectNoHints(JSCodeHints.jsHintProvider, "{");