Fix a bug where the hinter was not failing over to gueses. Added code to Session.getHints to detect when guesses are needed so the caller can get the guesses from tern and call getHints again.

This commit is contained in:
dloverin committed 2013-05-15 12:37:43 -04:00
1 parent 3c1614b5e6
commit 8142ccdeb6
7 files changed
+181 -27

No files matched your search

@@ -38,7 +38,8 @@ define(function (require, exports, module) {
TERN_COMPLETIONS_MSG = "Completions",
TERN_GET_FILE_MSG = "GetFile",
TERN_CALLED_FUNC_TYPE_MSG = "FunctionType",
TERN_PRIME_PUMP_MSG = "PrimePump";
TERN_PRIME_PUMP_MSG = "PrimePump",
TERN_GET_GUESSES_MSG = "GetGuesses";
/**
* Create a hint token with name value that occurs at the given list of
@@ -212,5 +213,6 @@ define(function (require, exports, module) {
exports.TERN_GET_FILE_MSG = TERN_GET_FILE_MSG;
exports.TERN_CALLED_FUNC_TYPE_MSG = TERN_CALLED_FUNC_TYPE_MSG;
exports.TERN_PRIME_PUMP_MSG = TERN_PRIME_PUMP_MSG;
exports.TERN_GET_GUESSES_MSG = TERN_GET_GUESSES_MSG;
exports.SUPPORTED_LANGUAGES = SUPPORTED_LANGUAGES;
});
@@ -516,8 +516,6 @@ define(function (require, exports, module) {
* @param {Session} session - the active hinting session
* @param {Document} document - the document for which scope info is
* desired
* @param {number} offset - the offset into the document at which scope
* info is desired
* @return {jQuery.Promise} - The promise will not complete until the tern
* hints have completed.
*/
@@ -547,7 +545,14 @@ define(function (require, exports, module) {
$.when(hintPromise, fnTypePromise).done(
function (completions, fnType) {
session.setTernHints(completions);
if (completions.completions) {
session.setTernHints(completions.completions);
session.setGuesses(null);
} else {
session.setTernHints([]);
session.setGuesses(completions.properties);
}
session.setFnType(fnType);
$deferredHints.resolveWith(null);
}
@@ -555,11 +560,44 @@ define(function (require, exports, module) {
return {promise: $deferredHints.promise()};
}
/**
* Get a Promise for all of the known properties from TernJS, for the directory and file.
* The properties will be used as guesses in tern.
* @param {Session} session - the active hinting session
* @param {Document} document - the document for which scope info is
* desired
* @return {jQuery.Promise} - The promise will not complete until the tern
* request has completed.
*/
function requestGuesses(session, document) {
var path = document.file.fullPath,
text = session.getJavascriptText(),
offset = session.getOffset(),
$deferred = $.Deferred();
postMessage({
type: HintUtils.TERN_GET_GUESSES_MSG,
dir: "",
file: path,
offset: offset,
text: text
});
var promise = addPendingRequest(path, offset, HintUtils.TERN_GET_GUESSES_MSG);
promise.done(function (guesses) {
session.setGuesses(guesses);
$deferred.resolve();
});
return $deferred.promise();
}
/**
* Handle the response from the tern web worker when
* it responds with the list of completions
*
* @param {{dir:string, file:string, offset:number, completions:Array.<string>}} response - the response from the worker
* @param {{dir:string, file:string, offset:number, completions:Array.<string>,
* properties:Array.<string>}} response - the response from the worker
*/
function handleTernCompletions(response) {
@@ -573,9 +611,9 @@ define(function (require, exports, module) {
if ($deferredHints) {
if (completions) {
$deferredHints.resolveWith(null, [completions]);
$deferredHints.resolveWith(null, [{completions: completions}]);
} else if (properties) {
$deferredHints.resolveWith(null, [properties]);
$deferredHints.resolveWith(null, [{properties: properties}]);
} else if (fnType) {
$deferredHints.resolveWith(null, [fnType]);
}
@@ -705,6 +743,25 @@ define(function (require, exports, module) {
}
}
/**
* Handle the response from the tern web worker when
* it responds to the get guesses message.
*
* @param {{file:string, type: string, offset: number, properties: Array.<string>}} response -
* the response from the worker contains the guesses for a
* property lookup.
*/
function handleGetGuesses(response) {
var path = response.file,
type = response.type,
offset = response.offset,
$deferredHints = getPendingRequest(path, offset, type);
if ($deferredHints) {
$deferredHints.resolveWith(null, [response.properties]);
}
}
/**
* We can skip tern initialization if we are opening a file that has
* already been added to tern.
@@ -821,6 +878,8 @@ define(function (require, exports, module) {
handleJumptoDef(response);
} else if (type === HintUtils.TERN_PRIME_PUMP_MSG) {
handlePrimePumpCompletion(response);
} else if (type === HintUtils.TERN_GET_GUESSES_MSG) {
handleGetGuesses(response);
} else {
console.log("Worker: " + (response.log || response));
}
@@ -830,6 +889,7 @@ define(function (require, exports, module) {
exports.getResolvedPath = getResolvedPath;
exports.getTernHints = getTernHints;
exports.handleEditorChange = handleEditorChange;
exports.requestGuesses = requestGuesses;
exports.requestHints = requestHints;
exports.requestJumptoDef = requestJumptoDef;
});
@@ -45,6 +45,7 @@ define(function (require, exports, module) {
this.editor = editor;
this.path = editor.document.file.fullPath;
this.ternHints = [];
this.ternGuesses = null;
this.fnType = null;
this.builtins = null;
}
@@ -392,8 +393,9 @@ define(function (require, exports, module) {
*
* @param {string} query - the query prefix
* @param {StringMatcher} matcher - the class to find query matches and sort the results
* @return {Array.<Object>} - the sorted list of hints for the current
* session.
* @returns {hints: Array.<string>, needGuesses: boolean} - array of
* matching hints. If needGuesses is true, then the caller needs to
* request guesses and call getHints again.
*/
Session.prototype.getHints = function (query, matcher) {
@@ -402,8 +404,9 @@ define(function (require, exports, module) {
}
var MAX_DISPLAYED_HINTS = 500,
type = this.getType(),
builtins = this._getBuiltins(),
type = this.getType(),
builtins = this._getBuiltins(),
needGuesses = false,
hints;
/**
@@ -461,6 +464,16 @@ define(function (require, exports, module) {
if (type.property) {
hints = this.ternHints || [];
hints = filterWithQueryAndMatcher(hints, matcher);
// If there are no hints then switch over to guesses.
if (hints.length === 0) {
if (this.ternGuesses) {
hints = filterWithQueryAndMatcher(this.ternGuesses, matcher);
} else {
needGuesses = true;
}
}
StringMatch.multiFieldSort(hints, [ "matchGoodness", penalizeUnderscoreValueCompare ]);
} else if (type.showFunctionType) {
hints = this.getFunctionTypeHint();
@@ -476,13 +489,17 @@ define(function (require, exports, module) {
hints = hints.slice(0, MAX_DISPLAYED_HINTS);
}
return hints;
return {hints: hints, needGuesses: needGuesses};
};
Session.prototype.setTernHints = function (newHints) {
this.ternHints = newHints;
};
Session.prototype.setGuesses = function (newGuesses) {
this.ternGuesses = newGuesses;
};
Session.prototype.setFnType = function (newFnType) {
this.fnType = newFnType;
};
@@ -248,6 +248,52 @@ define(function (require, exports, module) {
return editor.getModeForSelection() === "javascript";
}
/**
* Common code to get the session hints. Will get guesses if there were
* no completions for the query.
*
* @param {string} query - user text to search hints with
* @param {Object} type - the type of query, property vs. identifier
* @param {jQuery.Deferred=} $deferredHints - existing Deferred we need to
* resolve (optional). If not supplied a new Deferred will be created if
* needed.
* @return {Object + jQuery.Deferred} - hint response (immediate or
* deferred) as defined by the CodeHintManager API
*/
function getSessionHints(query, type, $deferredHints) {
var hintResults = session.getHints(query, matcher);
if (hintResults.needGuesses) {
var guessesResponse = ScopeManager.requestGuesses(session,
session.editor.document);
if (!$deferredHints) {
$deferredHints = $.Deferred();
}
guessesResponse.done(function () {
query = session.getQuery();
hintResults = session.getHints(query, matcher);
cachedHints = hintResults.hints;
var hintResponse = getHintResponse(cachedHints, query, type);
$deferredHints.resolveWith(null, [hintResponse]);
});
return $deferredHints;
} else if ($deferredHints && $deferredHints.state() === "pending") {
cachedHints = hintResults.hints;
var hintResponse = getHintResponse(cachedHints, query, type);
$deferredHints.resolveWith(null, [hintResponse]);
return null;
} else {
cachedHints = hintResults.hints;
return getHintResponse(cachedHints, query, type);
}
}
/**
* Determine whether hints are available for a given editor context
*
@@ -287,7 +333,7 @@ define(function (require, exports, module) {
return false;
};
/**
/**
* Return a list of hints, possibly deferred, for the current editor
* context
*
@@ -298,6 +344,7 @@ define(function (require, exports, module) {
JSHints.prototype.getHints = function (key) {
var cursor = session.getCursor(),
token = session.getToken(cursor);
if (token && HintUtils.hintableKey(key) && HintUtils.hintable(token)) {
var type = session.getType(),
query = session.getQuery();
@@ -323,14 +370,8 @@ define(function (require, exports, module) {
matcher = new StringMatch.StringMatcher({
preferPrefixMatches: true
});
cachedHints = session.getHints(query, matcher);
if ($deferredHints.state() === "pending") {
query = session.getQuery();
var hintResponse = getHintResponse(cachedHints, query, type);
$deferredHints.resolveWith(null, [hintResponse]);
}
getSessionHints(session.getQuery(), type, $deferredHints);
}).fail(function () {
if ($deferredHints.state() === "pending") {
$deferredHints.reject();
@@ -342,8 +383,7 @@ define(function (require, exports, module) {
}
if (cachedHints) {
cachedHints = session.getHints(session.getQuery(), matcher);
return getHintResponse(cachedHints, query, type);
return getSessionHints(query, type);
}
}
@@ -168,18 +168,19 @@ importScripts("thirdparty/requirejs/require.js");
* @param {string} file - the file name
* @param {number} offset - the offset into the file where we want completions for
* @param {string} text - the text of the file
* @param {string} type - the type of the message to reply with.
*/
function getTernProperties(dir, file, offset, text) {
function getTernProperties(dir, file, offset, text, type) {
var request = buildRequest(dir, file, "properties", undefined, text),
i;
//_log("request " + request.type + dir + " " + file);
//_log("tern properties: request " + request.type + dir + " " + file);
ternServer.request(request, function (error, data) {
var properties = [];
if (error) {
_log("Error returned from Tern 'properties' request: " + error);
} else {
//_log("completions = " + data.completions.length);
//_log("tern properties: completions = " + data.completions.length);
for (i = 0; i < data.completions.length; ++i) {
var property = data.completions[i];
properties.push({value: property, guess: true});
@@ -187,7 +188,7 @@ importScripts("thirdparty/requirejs/require.js");
}
// Post a message back to the main thread with the completions
self.postMessage({type: HintUtils.TERN_COMPLETIONS_MSG,
self.postMessage({type: type,
dir: dir,
file: file,
offset: offset,
@@ -234,7 +235,7 @@ importScripts("thirdparty/requirejs/require.js");
});
} else {
// if there are no completions, then get all the properties
getTernProperties(dir, file, offset, text);
getTernProperties(dir, file, offset, text, HintUtils.TERN_COMPLETIONS_MSG);
}
});
}
@@ -338,6 +339,12 @@ importScripts("thirdparty/requirejs/require.js");
handleAddFiles(request.files);
} else if (type === HintUtils.TERN_PRIME_PUMP_MSG) {
handlePrimePump(request.path, request.text);
} else if (type === HintUtils.TERN_GET_GUESSES_MSG) {
dir = request.dir;
file = request.file;
text = request.text;
offset = request.offset;
getTernProperties(dir, file, offset, text, HintUtils.TERN_GET_GUESSES_MSG);
} else {
_log("Unknown message: " + JSON.stringify(request));
}
@@ -144,3 +144,8 @@ funArr.index1 = testRef1;
funArr.index2 = testRef2;
var _A1 = A1;
var s = "", // string type
help; // unknown type
@@ -1061,6 +1061,29 @@ define(function (require, exports, module) {
var hintObj = expectHints(JSCodeHints.jsHintProvider);
hintsPresentOrdered(hintObj, ["A1", "A2", "A3", "funB", "_A1"]);
});
it("should list all properties for unknown type", function () {
var start = { line: 149, ch: 0 },
end = { line: 149, ch: 5 };
testDoc.replaceRange("help.", start, start);
testEditor.setCursorPos(end);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
// check we have a properties from "Function", "Array", and "Date"
hintsPresentOrdered(hintObj, ["apply", "concat", "getSeconds"]);
});
it("should switch to guesses after typing a query that does not match any hints", function () {
var start = { line: 150, ch: 0 },
end = { line: 150, ch: 5 };
testDoc.replaceRange("s.shift", start, start);
testEditor.setCursorPos(end);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
// check we have a properties that start with "shift"
hintsPresentOrdered(hintObj, ["shift", "shiftKey"]);
});
});
describe("JavaScript Code Hinting in a HTML file", function () {