new guessing

This commit is contained in:
dloverin authored and Erik Tierney committed 2013-04-05 12:51:39 -04:00
1 parent 3107c979c7
commit c887e8760f
5 files changed
+90 -4

No files matched your search

@@ -34,6 +34,7 @@ define(function (require, exports, module) {
TERN_INIT_MSG = "Init",
TERN_COMPLETIONS_MSG = "Completions",
TERN_GET_FILE_MSG = "GetFile",
TERN_GET_PROPERTIES_MSG = "Properties",
TERN_CALLED_FUNC_TYPE_MSG = "FunctionType";
/**
@@ -202,5 +203,6 @@ define(function (require, exports, module) {
exports.TERN_COMPLETIONS_MSG = TERN_COMPLETIONS_MSG;
exports.TERN_INIT_MSG = TERN_INIT_MSG;
exports.TERN_GET_FILE_MSG = TERN_GET_FILE_MSG;
exports.TERN_GET_PROPERTIES_MSG = TERN_GET_PROPERTIES_MSG;
exports.TERN_CALLED_FUNC_TYPE_MSG = TERN_CALLED_FUNC_TYPE_MSG;
});
@@ -203,6 +203,52 @@ define(function (require, exports, module) {
return $deferredHints.promise();
}
/**
* Request properties from Tern.
*
* @param {Document} document - the document for which scope info is
* desired
* @return {jQuery.Promise} - The promise will not complete until the tern
* hints have completed.
*/
function requestProperties(session, document) {
var path = document.file.fullPath,
split = HintUtils.splitPath(path),
dir = split.dir,
file = split.file;
var $deferredHints = $.Deferred(),
propertiesPromise,
fnTypePromise;
propertiesPromise = getTernProperties(dir, file, document.getText());
$.when(hintPromise).done(
function(properties){
session.setTernProperties(properties);
$deferredHints.resolveWith(null);
});
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.
*
* @return {jQuery.Promise} - a promise that will resolve to an array of properties when
* it is done
*/
function getTernProperties(dir, file, text) {
ternWorker.postMessage({
type: HintUtils.TERN_GET_PROPERTIES_MSG,
dir:dir,
file:file
});
var $deferredHints = $.Deferred();
addPendingRequest(file, HintUtils.TERN_PROPERTIES_MSG, $deferredHints);
return $deferredHints.promise();
}
/**
* Get a Promise for the function type from TernJS.
* @return {jQuery.Promise} - a promise that will resolve to the function type of the function being called.
@@ -428,6 +474,7 @@ define(function (require, exports, module) {
exports.handleEditorChange = handleEditorChange;
exports.handleFileChange = handleFileChange;
exports.requestHints = requestHints;
exports.requestProperties = requestProperties;
exports.isScopeDirty = isScopeDirty;
exports.getTernHints = getTernHints;
});
@@ -41,6 +41,7 @@ define(function (require, exports, module) {
this.editor = editor;
this.path = editor.document.file.fullPath;
this.ternHints = [];
this.ternProperies = [];
this.fnType = null;
}
@@ -397,6 +398,9 @@ define(function (require, exports, module) {
Session.prototype.setTernHints = function(newHints) {
this.ternHints = newHints;
};
Session.prototype.setTernProperties = function(newProperties) {
this.ternProperies = newProperties;
}
Session.prototype.setFnType = function(newFnType) {
this.fnType = newFnType;
};
@@ -336,8 +336,7 @@ define(function (require, exports, module) {
type.property !== cachedType.property ||
type.context !== cachedType.context ||
type.showFunctionType !== cachedType.showFunctionType ||
query.length === 0 ||
(cachedHints.length === 0 && query.length === 2)) { // FIXME: length of 2 is special because that is when tern starts guessing
query.length == 0) {
var offset = session.getOffset(),
scopeResponse = ScopeManager.requestHints(session, session.editor.document, offset),
self = this;
@@ -94,6 +94,7 @@ importScripts("thirdparty/requirejs/require.js");
query.sort = false;
query.depths = true;
query.types = true;
query.guess = false;
var request = {query:query, files:[], offset:offset};
request.files.push({type:"full", name:file, text:text});
@@ -117,13 +118,12 @@ importScripts("thirdparty/requirejs/require.js");
//if (error) return displayError(error);
//_log("completions = " + data.completions.length);
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.name, type: completion.type, depth: completion.depth, guess: completion.guess /*, className: className*/});
}
// Post a message back to the main thread with the completions
self.postMessage({type: HintUtils.TERN_COMPLETIONS_MSG,
dir: dir,
@@ -133,6 +133,36 @@ importScripts("thirdparty/requirejs/require.js");
});
}
/**
* Get all the known properties for guessing.
*
* @param {string} dir - the directory
* @param {string} file - the file name
* @param {string} text - the text of the file
*/
function handleGetProperties(dir, file, text) {
var request = buildRequest(dir, file, "properties", undefined, text);
_log("request " + request.type + dir + " " + file);
ternServer.request(request, function(error, data) {
//if (error) return displayError(error);
//_log("completions = " + data.completions.length);
var properties = [];
for (var i = 0; i < data.completions.length; ++i) {
var property = data.completions[i];//, className = typeToIcon(completion.type);
//if (data.guess) className += " Tern-completion-guess";
properties.push({value: completion.name, type: completion.type, guess: true /*, className: className*/});
}
// Post a message back to the main thread with the completions
self.postMessage({type: HintUtils.TERN_GET_PROPERTIES_MSG,
dir: dir,
file: file,
completions: properties
});
});
}
/**
* Get the function type for the given offset
* @param {string} dir - the directory
@@ -187,6 +217,10 @@ importScripts("thirdparty/requirejs/require.js");
text = request.text;
var offset = request.offset;
getTernHints(dir, file, offset, text);
} else if ( type === HintUtils.TERN_GET_PROPERTIES_MSG) {
var file = request.file,
dir = request.dir;
handleGetProperties(file, dir);
} else if ( type === HintUtils.TERN_GET_FILE_MSG ) {
file = request.file,
text = request.text;