Fix problem where hints are displayed before tern has finished computing the completions.

This means there is now a delay on the first request for hints.
This commit is contained in:
Erik Tierney committed 2013-03-21 16:18:32 -04:00
1 parent 9689288dec
commit d648572395
3 files changed
+50 -11

No files matched your search

@@ -55,6 +55,9 @@ define(function (require, exports, module) {
var MAX_TEXT_LENGTH = 1000000, // about 1MB
MAX_FILES_IN_DIR = 100;
/**
* Create a new tern server.
*/
function initTernServer(dir) {
var ternOptions = {
environment:ternEnvironment,
@@ -66,6 +69,10 @@ define(function (require, exports, module) {
};
ternServer = new Tern.Server(ternOptions);
}
/**
* Read in the json files that have type information for the builtins, dom,etc
*/
function initTernEnv() {
var path = module.uri.substring(0, module.uri.lastIndexOf("/") + 1) + "tern/";
var files = ["ecma5.json"];//, "browser.json", "plugin/requirejs/requirejs.json", "jquery.json"];
@@ -403,20 +410,45 @@ define(function (require, exports, module) {
* desired
* @param {number} offset - the offset into the document at which scope
* info is desired
* @return {Object + jQuery.Promise} - the inner scope info, or a promise
* for such info. (See refreshInnerScope above.)
* @return {jQuery.Promise} - A promise that will resolve to the inner scope info.
* The promise will not complete until the scope info is refreshed, and tern has
* computed completions.
*/
function getScopeInfo(document, offset) {
function getScopeInfo(session, document, offset) {
var path = document.file.fullPath,
split = HintUtils.splitPath(path),
dir = split.dir,
file = split.file;
return refreshInnerScope(dir, file, offset);
var $deferredHints = $.Deferred();
var scopeInfo = refreshInnerScope(dir, file, offset);
var scopePromise;
if( scopeInfo.hasOwnProperty("promise") ) {
scopePromise = scopeInfo.promise;
}
else {
// make a scopePromise that we can pass to when below
var $scopeInfo = $.Deferred();
scopePromise = $scopeInfo.promise();
$scopeInfo.resolveWith(null, [scopeInfo]);
}
var ternPromise = getTernHints("dir", "file", offset, document.getText());
$.when(scopePromise, ternPromise).done(
function(scopeInfo, ternHints){
session.setTernHints(ternHints);
$deferredHints.resolveWith(null, [scopeInfo]);
});
return {promise:$deferredHints.promise()};
}
/**
* Get completions from TernJS, from the file & offset passed in.
* Get a Promise for the completions from TernJS, for the file & offset passed in.
* @return {jQuery.Promise} - a promise that will resolve to an array of completions when
* it is done
*/
function getTernHints(dir, file, offset, text) {
function buildRequest(dir, file, query, offset){
@@ -434,20 +466,22 @@ define(function (require, exports, module) {
}
var request = buildRequest(dir, file, "completions", offset);
var ternHints = [];
var $deferredHints = $.Deferred();
ternServer.request(request, function(error, data) {
//if (error) return displayError(error);
var completions = [];
var ternHints = [];
for (var i = 0; i < data.completions.length; ++i) {
var completion = data.completions[i];//, className = typeToIcon(completion.type);
//if (data.guess) className += " Tern-completion-guess";
completions.push({value: completion/*, className: className*/});
}
//console.log("tern finished");
ternHints = completions;
$deferredHints.resolveWith(null, [ternHints]);
});
return ternHints;
return $deferredHints.promise();
}
/**
@@ -40,6 +40,7 @@ define(function (require, exports, module) {
function Session(editor) {
this.editor = editor;
this.path = editor.document.file.fullPath;
this.ternHints = [];
}
/**
@@ -538,7 +539,8 @@ define(function (require, exports, module) {
hints;
if (type.property) {
var ternHints = ScopeManager.getTernHints("dir", "file", offset, this.editor.document.getText());
var ternHints = this.ternHints;
//console.log("Got " + (ternHints ? ternHints.length : 0) + " hints from tern");
if( ternHints && ternHints.length > 0 ) {
hints = ternHints;
}
@@ -565,6 +567,9 @@ define(function (require, exports, module) {
return hints;
};
Session.prototype.setTernHints = function(newHints) {
this.ternHints = newHints;
}
module.exports = Session;
});
@@ -265,7 +265,7 @@ define(function (require, exports, module) {
if (token) {
if (!cachedScope) {
var offset = session.getOffset(),
scopeResponse = ScopeManager.getScopeInfo(session.editor.document, offset),
scopeResponse = ScopeManager.getScopeInfo(session, session.editor.document, offset),
self = this;
if (scopeResponse.hasOwnProperty("promise")) {