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

@@ -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;