Add support for hinting function types.

When in a function call, display the function type instead of the normal hints.  This only happens when nothing has been entered by the user - if the user starts typing, then we switch back to the normal hints based on what the user has typed.

This also requires a new version of CodeMirror that brackets has not updated to.  The old hints continue to work without the new version, but the function types will only work once codemirror is updated.
This commit is contained in:
Erik Tierney committed 2013-04-02 17:58:05 -04:00
1 parent 08f356c649
commit 63c89f4d62
5 files changed
+201 -40

No files matched your search

@@ -27,12 +27,13 @@
define(function (require, exports, module) {
"use strict";
var MODE_NAME = "javascript",
SINGLE_QUOTE = "\'",
DOUBLE_QUOTE = "\"",
TERN_INIT_MSG = "ternInit",
TERN_COMPLETIONS_MSG = "ternCompletions",
TERN_GET_FILE_MSG = "ternGetFile";
var MODE_NAME = "javascript",
SINGLE_QUOTE = "\'",
DOUBLE_QUOTE = "\"",
TERN_INIT_MSG = "Init",
TERN_COMPLETIONS_MSG = "Completions",
TERN_GET_FILE_MSG = "GetFile",
TERN_CALLED_FUNC_TYPE_MSG = "FunctionType";
/**
* Create a hint token with name value that occurs at the given list of
@@ -200,4 +201,5 @@ 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_CALLED_FUNC_TYPE_MSG = TERN_CALLED_FUNC_TYPE_MSG;
});
@@ -161,11 +161,24 @@ define(function (require, exports, module) {
file = split.file;
var $deferredHints = $.Deferred(),
ternPromise = getTernHints(dir, file, offset, document.getText());
hintPromise,
fnTypePromise;
$.when(ternPromise).done(
function(ternHints){
session.setTernHints(ternHints);
hintPromise = getTernHints(dir, file, offset, document.getText());
var sessionType = session.getType();
if( sessionType.showFunctionType ) {
// Show function sig
fnTypePromise = getTernFunctionType(dir, file, sessionType.functionCallPos, document.getText());
} else {
var $fnTypeDeferred = $.Deferred();
fnTypePromise = $fnTypeDeferred.promise();
$fnTypeDeferred.resolveWith(null);
}
$.when(hintPromise, fnTypePromise).done(
function(completions, fnType){
session.setTernHints(completions);
session.setFnType(fnType);
$deferredHints.resolveWith(null);
});
return {promise:$deferredHints.promise()};
@@ -186,9 +199,58 @@ define(function (require, exports, module) {
});
var $deferredHints = $.Deferred();
pendingTernRequests[file] = $deferredHints;
addPendingRequest(file, HintUtils.TERN_COMPLETIONS_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.
*/
function getTernFunctionType(dir, file, pos, text) {
ternWorker.postMessage({
type: HintUtils.TERN_CALLED_FUNC_TYPE_MSG,
dir:dir,
file:file,
pos:pos,
text:text
});
var $deferredHints = $.Deferred();
addPendingRequest(file, HintUtils.TERN_CALLED_FUNC_TYPE_MSG, $deferredHints);
return $deferredHints.promise();
}
/**
* Add a pending request waiting for the tern-worker to complete.
*
* @param {string} file - the name of the file
* @param {string} type - the type of request
* @param {jQuery.Deferred} deferredRequest - the $.Deferred object to save
*/
function addPendingRequest(file, type, $deferredRequest) {
var requests;
if( Object.prototype.hasOwnProperty.call(pendingTernRequests, file) ) {
requests = pendingTernRequests[file];
} else {
requests = {};
pendingTernRequests[file] = requests;
}
requests[type] = $deferredRequest;
}
/**
* Get any pending $.Deferred object waiting on the specified file and request type
* @param {string} file - the file
* @param {string} type - the type of request
* @param {jQuery.Deferred} - the $.Deferred for the request
*/
function getPendingRequest(file, type) {
if( Object.prototype.hasOwnProperty.call(pendingTernRequests, file) ) {
var requests = pendingTernRequests[file];
return requests[type];
}
}
/**
* Handle the response from the tern web worker when
@@ -202,12 +264,16 @@ define(function (require, exports, module) {
file = response.file,
offset = response.offset,
completions = response.completions,
$deferredHints = pendingTernRequests[file];
fnType = response.fnType,
type = response.type,
$deferredHints = getPendingRequest(file, type);
pendingTernRequests[file] = null;
if( $deferredHints ) {
$deferredHints.resolveWith(null, [completions]);
if( $deferredHints ) {
if( completions ) {
$deferredHints.resolveWith(null, [completions]);
} else if ( fnType ) {
$deferredHints.resolveWith(null, [fnType]);
}
}
}
@@ -319,13 +385,14 @@ define(function (require, exports, module) {
var response = e.data,
type = response.type;
if( type === HintUtils.TERN_COMPLETIONS_MSG) {
if( type === HintUtils.TERN_COMPLETIONS_MSG ||
type === HintUtils.TERN_CALLED_FUNC_TYPE_MSG) {
// handle any completions the worker calculated
handleTernCompletions(response);
} else if ( type === HintUtils.TERN_GET_FILE_MSG ) {
// handle a request for the contents of a file
handleTernGetFile(response);
} else {
} else{
console.log("Worker: " + (response.log || response));
}
});
@@ -41,6 +41,7 @@ define(function (require, exports, module) {
this.editor = editor;
this.path = editor.document.file.fullPath;
this.ternHints = [];
this.fnType = null;
}
/**
@@ -61,6 +62,17 @@ define(function (require, exports, module) {
Session.prototype.getCursor = function () {
return this.editor.getCursorPos();
};
/**
* Get the text of a line.
*
* @param {number} line - the line number
* @return {string} - the text of the line
*/
Session.prototype.getLine = function (line) {
var doc = this.editor.document;
return doc.getLine(line);
};
/**
* Get the offset of the current cursor position
@@ -202,20 +214,49 @@ define(function (require, exports, module) {
* Get the type of the current session, i.e., whether it is a property
* lookup and, if so, what the context of the lookup is.
*
* @return {{property: boolean, context: string}} - a pair consisting
* @return {{property: boolean,
showFunctionType:boolean,
context: string,
functionCallPos: {line:number, ch:number}}} - an Object consisting
* of a {boolean} "property" that indicates whether or not the type of
* the session is a property lookup, and a {string} "context" that
* indicates the object context (as described in getContext above) of
* the property lookup, or null if there is none. The context is
* always null for non-property lookups.
* a {boolean} "showFunctionType" indicating if the function type should
* be displayed instead of normal hints. If "showFunctionType" is true, then
* then "functionCallPos" will be an object with line & col information of the
* function being called
*/
Session.prototype.getType = function () {
var propertyLookup = false,
inFunctionCall = false,
showFunctionType= false,
context = null,
cursor = this.getCursor(),
functionCallPos,
token = this.getToken(cursor);
if (token) {
if( token.state.lexical.info === "call" ) {
inFunctionCall = true;
if( this.getQuery().length > 0 ) {
inFunctionCall = false;
showFunctionType = false;
} else {
showFunctionType = true;
var col = token.state.lexical.column;
for (var line = this.getCursor().line, e = Math.max(0, line - 9), found = false; line >= e; --line) {
if (this.getLine(line).charAt(col) == "(") {
found = true;
break;
}
}
if ( found ) {
functionCallPos = {line:line, ch:col};
}
}
}
if (token.string === ".") {
propertyLookup = true;
context = this.getContext(cursor);
@@ -230,10 +271,14 @@ define(function (require, exports, module) {
context = this.getContext(cursor);
}
}
}
if( propertyLookup ) { showFunctionType = false; }
}
return {
property: propertyLookup,
inFunctionCall: inFunctionCall,
showFunctionType: showFunctionType,
functionCallPos: functionCallPos,
context: context
};
};
@@ -337,6 +382,8 @@ define(function (require, exports, module) {
else {
hints = [];
}
} else if ( type.showFunctionType ) {
hints = this.fnType ? [{value:this.fnType, positions:[]}] : [];
} else {
hints = ternHints ? ternHints : [];
hints.sort(compareIdentifiers());
@@ -350,5 +397,8 @@ define(function (require, exports, module) {
Session.prototype.setTernHints = function(newHints) {
this.ternHints = newHints;
}
Session.prototype.setFnType = function(newFnType) {
this.fnType = newFnType;
}
module.exports = Session;
});
@@ -301,7 +301,8 @@ define(function (require, exports, module) {
if (!cachedHints ||
cachedLine !== cursor.line ||
type.property !== cachedType.property ||
type.context !== cachedType.context) {
type.context !== cachedType.context ||
type.showFunctionType !== cachedType.showFunctionType) {
//console.log("clear hints");
cachedLine = null;
cachedHints = null;
@@ -333,6 +334,7 @@ define(function (require, exports, module) {
if (!cachedHints ||
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
var offset = session.getOffset(),
@@ -375,7 +377,8 @@ define(function (require, exports, module) {
// Compute fresh hints if none exist, or if the session
// type has changed since the last hint computation
if (type.property !== cachedType.property ||
type.context !== cachedType.context) {
type.context !== cachedType.context ||
type.showFunctionType !== cachedType.showFunctionType) {
cachedType = type;
cachedHints = session.getHints();
}
@@ -431,6 +434,11 @@ define(function (require, exports, module) {
completion = delimeter + completion + delimeter;
}
if( session.getType().showFunctionType ) {
// function types show up as hints, so don't insert anything
// if we were displaying a function type
return false;
}
// Replace the current token with the completion
session.editor.document.replaceRange(completion, start, end);
@@ -85,6 +85,23 @@ importScripts("thirdparty/requirejs/require.js");
}
function buildRequest(dir, file, query, offset, text){
query = {type:query};
query.start = offset;
query.end = offset;
query.file = file;
query.filter = false;
query.sort = false;
query.depths = true;
query.types = true;
var request = {query:query, files:[], offset:offset};
request.files.push({type:"full", name:file, text:text});
return request;
}
/**
* Get the completions for the given offset
* @param {string} dir - the directory
@@ -94,23 +111,7 @@ importScripts("thirdparty/requirejs/require.js");
*/
function getTernHints(dir, file, offset, text) {
function buildRequest(dir, file, query, offset){
query = {type:query};
query.start = offset;
query.end = offset;
query.file = file;
query.filter = false;
query.sort = false;
query.depths = true;
query.types = true;
var request = {query:query, files:[], offset:offset};
request.files.push({type:"full", name:file, text:text});
return request;
}
var request = buildRequest(dir, file, "completions", offset);
var request = buildRequest(dir, file, "completions", offset, text);
//_log("request " + dir + " " + file + " " + offset /*+ " " + text */);
ternServer.request(request, function(error, data) {
//if (error) return displayError(error);
@@ -131,6 +132,33 @@ importScripts("thirdparty/requirejs/require.js");
});
});
}
/**
* Get the function type for the given offset
* @param {string} dir - the directory
* @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
*/
function handleFunctionType(dir, file, pos, text) {
var request = buildRequest(dir, file, "type", pos, text);
request.preferFunction = true;
//_log("request " + dir + " " + file + " " + offset /*+ " " + text */);
ternServer.request(request, function(error, data) {
//if (error) return displayError(error);
var fnType = data.type;
// Post a message back to the main thread with the completions
self.postMessage({type: HintUtils.TERN_CALLED_FUNC_TYPE_MSG,
dir: dir,
file: file,
fnType: fnType
});
});
}
/**
* Send a log message back from the worker to the main thread
@@ -163,6 +191,12 @@ importScripts("thirdparty/requirejs/require.js");
var file = request.file,
text = request.text;
handleGetFile(file, text);
} else if ( type === HintUtils.TERN_CALLED_FUNC_TYPE_MSG ) {
var file = request.file,
text = request.text,
dir = request.dir,
pos = request.pos;
handleFunctionType(dir, file, pos, text);
} else {
_log("Unknown message: " + JSON.stringify(request));
}