Skip to content

Commit

Permalink
When typing, only automatically show hints for the "." character and …
Browse files Browse the repository at this point in the history
…JS identifier characters.
  • Loading branch information
dloverin committed Apr 17, 2013
1 parent 725a1e8 commit f6a073c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 53 deletions.
18 changes: 14 additions & 4 deletions src/extensions/default/JavaScriptCodeHints/HintUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ define(function (require, exports, module) {
* @return {boolean} - could key be a valid identifier?
*/
function maybeIdentifier(key) {
return (/[0-9a-z_.\$]/i).test(key) ||
(key.indexOf(SINGLE_QUOTE) === 0) ||
(key.indexOf(DOUBLE_QUOTE) === 0);
return (/[0-9a-z_\$]/i).test(key);
}

/**
Expand All @@ -83,7 +81,18 @@ define(function (require, exports, module) {
return true;
}
}


/**
* Determine if hints should be displayed for the given key.
*
* @param {string} key - key entered by the user
* @return {boolean} true if the hints should be shown for the key,
* false otherwise.
*/
function hintableKey(key) {
return (key === null || key === "." || maybeIdentifier(key));
}

/**
* Divide a path into directory and filename parts
*
Expand Down Expand Up @@ -177,6 +186,7 @@ define(function (require, exports, module) {

exports.makeToken = makeToken;
exports.hintable = hintable;
exports.hintableKey = hintableKey;
exports.maybeIdentifier = maybeIdentifier;
exports.splitPath = splitPath;
exports.eventName = eventName;
Expand Down
4 changes: 3 additions & 1 deletion src/extensions/default/JavaScriptCodeHints/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ define(function (require, exports, module) {
query = "";

if (token) {
if (token.string !== "." && token.string !== "(" && token.string !== ',') {
// If the token string is not an identifier, then the query string
// is empty.
if (HintUtils.maybeIdentifier(token.string)) {
query = token.string.substring(0, token.string.length - (token.end - cursor.ch));
query = query.trim();
}
Expand Down
92 changes: 45 additions & 47 deletions src/extensions/default/JavaScriptCodeHints/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ define(function (require, exports, module) {
* @return {boolean} - can the provider provide hints for this session?
*/
JSHints.prototype.hasHints = function (editor, key) {
if (session && ((key === null) || HintUtils.maybeIdentifier(key))) {
if (session && HintUtils.hintableKey(key)) {
var cursor = session.getCursor(),
token = session.getToken(cursor);

Expand Down Expand Up @@ -209,56 +209,54 @@ define(function (require, exports, module) {
JSHints.prototype.getHints = function (key) {
var cursor = session.getCursor(),
token = session.getToken(cursor);
if ((key === null) || HintUtils.hintable(token)) {
if (token) {
var type = session.getType(),
query = session.getQuery();

// Compute fresh hints if none exist, or if the session
// type has changed since the last hint computation
if (!cachedHints ||
type.property !== cachedType.property ||
type.context !== cachedType.context ||
type.showFunctionType !== cachedType.showFunctionType || query.length === 0) {
var offset = session.getOffset(),
scopeResponse = ScopeManager.requestHints(session, session.editor.document, offset),
self = this;

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

$(self).triggerHandler("resolvedResponse", [cachedHints, cachedType]);

if ($deferredHints.state() === "pending") {
query = session.getQuery();
var hintResponse = getHintResponse(cachedHints, query, type);

$deferredHints.resolveWith(null, [hintResponse]);
$(self).triggerHandler("hintResponse", [query]);
}
}).fail(function () {
if ($deferredHints.state() === "pending") {
$deferredHints.reject();
}
});

$(this).triggerHandler("deferredResponse");
return $deferredHints;
} else {
if (token && HintUtils.hintableKey(key) && HintUtils.hintable(token)) {
var type = session.getType(),
query = session.getQuery();

// Compute fresh hints if none exist, or if the session
// type has changed since the last hint computation
if (!cachedHints ||
type.property !== cachedType.property ||
type.context !== cachedType.context ||
type.showFunctionType !== cachedType.showFunctionType || query.length === 0) {
var offset = session.getOffset(),
scopeResponse = ScopeManager.requestHints(session, session.editor.document, offset),
self = this;

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

if (cachedHints) {
cachedHints = session.getHints(session.getQuery(), matcher);
return getHintResponse(cachedHints, query, type);
$(self).triggerHandler("resolvedResponse", [cachedHints, cachedType]);

if ($deferredHints.state() === "pending") {
query = session.getQuery();
var hintResponse = getHintResponse(cachedHints, query, type);

$deferredHints.resolveWith(null, [hintResponse]);
$(self).triggerHandler("hintResponse", [query]);
}
}).fail(function () {
if ($deferredHints.state() === "pending") {
$deferredHints.reject();
}
});

$(this).triggerHandler("deferredResponse");
return $deferredHints;
} else {
cachedLine = cursor.line;
}
}

if (cachedHints) {
cachedHints = session.getHints(session.getQuery(), matcher);
return getHintResponse(cachedHints, query, type);
}
}

return null;
Expand Down
4 changes: 3 additions & 1 deletion src/extensions/default/JavaScriptCodeHints/unittests.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ define(function (require, exports, module) {
expectHints(JSCodeHints.jsHintProvider, ".");
});

/* Single quote and double quote keys cause hasHints() to return false.
It used to return true when string literals were supported.
it("should list implicit hints when typing string literals (single quote)", function () {
testEditor.setCursorPos({ line: 9, ch: 0 });
expectHints(JSCodeHints.jsHintProvider, "'");
Expand All @@ -469,7 +471,7 @@ define(function (require, exports, module) {
testEditor.setCursorPos({ line: 9, ch: 0 });
expectHints(JSCodeHints.jsHintProvider, "\"");
});
*/
it("should give priority to identifier names associated with the current context", function () {
testEditor.setCursorPos({ line: 16, ch: 0 });
var hintObj = expectHints(JSCodeHints.jsHintProvider);
Expand Down

0 comments on commit f6a073c

Please sign in to comment.