Use tern to sort ids by depth, remove scope info

This commit is contained in:
dloverin committed 2013-03-28 15:06:15 -04:00
1 parent 33579f69d2
commit 7bfaedfa0a
5 files changed
+88 -434

No files matched your search

@@ -39,7 +39,6 @@ define(function (require, exports, module) {
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
cachedScope = null, // the inner-most scope returned by the query worker
cachedLine = null; // the line number for the cached scope
var MAX_DISPLAYED_HINTS = 100,
@@ -52,9 +51,10 @@ define(function (require, exports, module) {
*
* @param {Array.<Object>} hints - hints to be included in the response
* @param {string} query - querystring with which to filter the hint list
* @param {Object} type - the type of query, property vs. identifier
* @return {Object} - hint response as defined by the CodeHintManager API
*/
function getHintResponse(hints, query) {
function getHintResponse(hints, query, type) {
var trimmedQuery,
filteredHints,
@@ -136,7 +136,7 @@ define(function (require, exports, module) {
* @param {Array.<Object>} hints - the list of hints to format
* @param {string} query - querystring used for highlighting matched
* poritions of each hint
* @param {Array.<jQuery.Object>} - array of hints formatted as jQuery
* @return {Array.<jQuery.Object>} - array of hints formatted as jQuery
* objects
*/
function formatHints(hints, query) {
@@ -162,18 +162,26 @@ define(function (require, exports, module) {
delimiter = "";
// level indicates either variable scope or property confidence
switch (token.level) {
case 0:
$hintObj.addClass("priority-high");
break;
case 1:
$hintObj.addClass("priority-medium");
break;
case 2:
$hintObj.addClass("priority-low");
break;
if (!type.property && token.depth !== undefined) {
switch (token.depth) {
case 0:
$hintObj.addClass("priority-high");
break;
case 1:
$hintObj.addClass("priority-medium");
break;
case 2:
$hintObj.addClass("priority-low");
break;
default:
$hintObj.addClass("priority-lowest");
break;
}
}
if (token.guess)
$hintObj.addClass("guess-hint");
// is the token a global variable?
if (token.global) {
$hintObj.addClass("global-hint");
@@ -224,8 +232,13 @@ define(function (require, exports, module) {
trimmedQuery = query;
}
filteredHints = filterWithQuery(hints, MAX_DISPLAYED_HINTS);
formattedHints = formatHints(filteredHints, trimmedQuery);
if (hints) {
filteredHints = filterWithQuery(hints, MAX_DISPLAYED_HINTS);
formattedHints = formatHints(filteredHints, trimmedQuery);
}
else {
formattedHints = [];
}
return {
hints: formattedHints,
@@ -254,17 +267,19 @@ define(function (require, exports, module) {
// don't autocomplete within strings or comments, etc.
if (token && HintUtils.hintable(token)) {
var offset = session.getOffset();
var offset = session.getOffset(),
type = session.getType(),
query = session.getQuery();
// Invalidate cached information if: 1) no scope exists; 2) the
// cursor has moved a line; 3) the scope is dirty; or 4) if the
// cursor has moved into a different scope. Cached information
// is also reset on editor change.
if (!cachedScope ||
if (!cachedHints ||
cachedLine !== cursor.line ||
ScopeManager.isScopeDirty(session.editor.document) ||
!cachedScope.containsPositionImmediate(offset)) {
cachedScope = null;
type.property !== cachedType.property ||
type.context !== cachedType.context) {
//console.log("clear hints");
cachedLine = null;
cachedHints = null;
}
@@ -292,18 +307,18 @@ define(function (require, exports, module) {
// Compute fresh hints if none exist, or if the session
// type has changed since the last hint computation
if (!cachedScope ||
if (!cachedHints ||
type.property !== cachedType.property ||
type.context !== cachedType.context) {
type.context !== cachedType.context ||
query.length == 0 ||
(cachedHints.length = 0 && query.length == 2)) { // FIXME: length of 2 is special because that is when tern starts guessing
var offset = session.getOffset(),
scopeResponse = ScopeManager.getScopeInfo(session, session.editor.document, offset),
self = this;
if (scopeResponse.hasOwnProperty("promise")) {
var $deferredHints = $.Deferred();
scopeResponse.promise.done(function (scopeInfo) {
session.setScopeInfo(scopeInfo);
cachedScope = scopeInfo.scope;
scopeResponse.promise.done(function () {
cachedLine = cursor.line;
cachedType = session.getType();
cachedHints = session.getHints();
@@ -312,7 +327,7 @@ define(function (require, exports, module) {
if ($deferredHints.state() === "pending") {
var query = session.getQuery(),
hintResponse = getHintResponse(cachedHints, query);
hintResponse = getHintResponse(cachedHints, query, type);
$deferredHints.resolveWith(null, [hintResponse]);
$(self).triggerHandler("hintResponse", [query]);
@@ -326,13 +341,11 @@ define(function (require, exports, module) {
$(this).triggerHandler("deferredResponse");
return $deferredHints;
} else {
session.setScopeInfo(scopeResponse);
cachedScope = scopeResponse.scope;
cachedLine = cursor.line;
}
}
if (cachedScope) {
if (cachedHints) {
var type = session.getType(),
query = session.getQuery();
@@ -344,7 +357,7 @@ define(function (require, exports, module) {
cachedType = type;
cachedHints = session.getHints();
}
return getHintResponse(cachedHints, query);
return getHintResponse(cachedHints, query, type);
}
}
}
@@ -426,7 +439,6 @@ define(function (require, exports, module) {
*/
function installEditorListeners(editor) {
// always clean up cached scope and hint info
cachedScope = null;
cachedLine = null;
cachedHints = null;
cachedType = null;