mirror of
https://github.com/RustyRaptor/brackets.git
synced 2026-12-04 00:15:22 -07:00
Work around issue https://github.com/marijnh/tern/issues/207 by calling Tern.findQueryExpr() and Infer.expressionType() as suggested by marijn. The problem with this fix is it requires a "full" text update with the parameter hint request. Suggested a possible solution in the issue.
The quality of the hints has been much improved and record type annotations can be hinted correctly.
This commit is contained in:
1 parent
d95544d504
commit
0c892a988f
4 files changed
+88
-49
No files matched your search
@@ -566,9 +566,11 @@ define(function (require, exports, module) {
|
||||
* changes are reset.
|
||||
*
|
||||
* @param {!Session} session - the current session
|
||||
* @param {boolean=} forceFullUpdate - if true, only a full update is allowed.
|
||||
* Optional, defaults to false.
|
||||
* @return {{type: string, name: string, offsetLines: number, text: string}}
|
||||
*/
|
||||
function getFileInfo(session) {
|
||||
function getFileInfo(session, forceFullUpdate) {
|
||||
var start = session.getCursor(),
|
||||
end = start,
|
||||
document = session.editor.document,
|
||||
@@ -580,11 +582,11 @@ define(function (require, exports, module) {
|
||||
result = {type: MessageIds.TERN_FILE_INFO_TYPE_FULL,
|
||||
name: path,
|
||||
text: session.getJavascriptText()};
|
||||
} else if (!documentChanges) {
|
||||
} else if (!forceFullUpdate && !documentChanges) {
|
||||
result = {type: MessageIds.TERN_FILE_INFO_TYPE_EMPTY,
|
||||
name: path,
|
||||
text: ""};
|
||||
} else if (session.editor.lineCount() > LARGE_LINE_COUNT &&
|
||||
} else if (!forceFullUpdate && session.editor.lineCount() > LARGE_LINE_COUNT &&
|
||||
(documentChanges.to - documentChanges.from < LARGE_LINE_CHANGE) &&
|
||||
documentChanges.from <= start.line &&
|
||||
documentChanges.to > end.line) {
|
||||
@@ -1286,7 +1288,7 @@ define(function (require, exports, module) {
|
||||
*/
|
||||
function requestParameterHint(session, functionOffset) {
|
||||
var $deferredHints = $.Deferred(),
|
||||
fileInfo = getFileInfo(session),
|
||||
fileInfo = getFileInfo(session, true),
|
||||
offset = getOffset(session, fileInfo, functionOffset),
|
||||
fnTypePromise = getTernFunctionType(fileInfo, offset);
|
||||
|
||||
|
||||
@@ -373,7 +373,6 @@ define(function (require, exports, module) {
|
||||
if (type === "variable-2" || type === "variable" || type === "property") {
|
||||
nextToken = self.getNextToken(localCursor, true);
|
||||
if (nextToken && nextToken.string === "(") {
|
||||
//nextToken = self.getNextToken(localCursor, true);
|
||||
localLexical = getLexicalState(nextToken);
|
||||
return localLexical;
|
||||
}
|
||||
@@ -426,7 +425,7 @@ define(function (require, exports, module) {
|
||||
lexical = lexical.prev;
|
||||
}
|
||||
|
||||
var col = lexical.info === "call" ? lexical.column : lexical.prev,
|
||||
var col = lexical.info === "call" ? lexical.column : lexical.prev.column,
|
||||
line,
|
||||
e,
|
||||
found;
|
||||
|
||||
@@ -296,16 +296,16 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a Tern formatted function type string, convert it to an array of Objects, where each object describes
|
||||
* Given a Tern type object, convert it to an array of Objects, where each object describes
|
||||
* a parameter.
|
||||
*
|
||||
* @param {string} newFnType - Tern formatted function type string.
|
||||
* @param {!Infer.Fn} inferFnType - type to convert.
|
||||
* @return {Array<{name: string, type: string, isOptional: boolean}>} where each entry in the array is a parameter.
|
||||
*/
|
||||
function getParameters(newFnType) {
|
||||
function getParameters(inferFnType) {
|
||||
|
||||
// work around define functions before use warning.
|
||||
var inferTypeToString, processInferFnTypeParameters, inferFnTypeToString;
|
||||
var recordTypeToString, inferTypeToString, processInferFnTypeParameters, inferFnTypeToString;
|
||||
|
||||
/**
|
||||
* Convert an infer array type to a string.
|
||||
@@ -338,6 +338,33 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert properties to a record type annotation.
|
||||
*
|
||||
* @param {Object} props
|
||||
* @return {string} - record type annotation
|
||||
*/
|
||||
recordTypeToString = function (props) {
|
||||
var result = "{",
|
||||
first = true,
|
||||
prop;
|
||||
|
||||
for (prop in props) {
|
||||
if (Object.prototype.hasOwnProperty.call(props, prop)) {
|
||||
if (!first) {
|
||||
result += ", ";
|
||||
}
|
||||
|
||||
first = false;
|
||||
result += prop + ": " + inferTypeToString(props[prop]);
|
||||
}
|
||||
}
|
||||
|
||||
result += "}";
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert an infer type to a string.
|
||||
*
|
||||
@@ -350,6 +377,10 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
inferTypeToString = function (inferType) {
|
||||
var result;
|
||||
|
||||
if (inferType instanceof Infer.AVal) {
|
||||
inferType = inferType.types[0];
|
||||
}
|
||||
|
||||
if (inferType instanceof Infer.Prim) {
|
||||
result = inferType.toString();
|
||||
if (result === "string") {
|
||||
@@ -364,7 +395,11 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
} else if (inferType instanceof Infer.Fn) {
|
||||
result = inferFnTypeToString(inferType);
|
||||
} else if (inferType instanceof Infer.Obj) {
|
||||
result = inferType.name;
|
||||
if (inferType.name === undefined) {
|
||||
result = recordTypeToString(inferType.props);
|
||||
} else {
|
||||
result = inferType.name;
|
||||
}
|
||||
} else {
|
||||
result = "Object";
|
||||
}
|
||||
@@ -423,18 +458,7 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
return params;
|
||||
};
|
||||
|
||||
if (newFnType && newFnType.indexOf("fn(") === 0) {
|
||||
var params = Infer.withContext(ternServer.cx, function () {
|
||||
var typeParser = new Infer.def.TypeParser(newFnType, null, null, true),
|
||||
inferType = typeParser.parseType("", true);
|
||||
return processInferFnTypeParameters(inferType);
|
||||
});
|
||||
|
||||
return params;
|
||||
} else {
|
||||
throw new Error("No parameter hint found in '" + newFnType + "'");
|
||||
}
|
||||
|
||||
return processInferFnTypeParameters(inferFnType);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -450,33 +474,48 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
*/
|
||||
function handleFunctionType(fileInfo, offset) {
|
||||
|
||||
var request = buildRequest(fileInfo, "type", offset);
|
||||
var request = buildRequest(fileInfo, "type", offset),
|
||||
error;
|
||||
|
||||
request.query.preferFunction = true;
|
||||
|
||||
//_log("request " + dir + " " + file + " " + offset /*+ " " + text */);
|
||||
ternServer.request(request, function (error, data) {
|
||||
var fnType = "";
|
||||
if (error) {
|
||||
_log("Error returned from Tern 'type' request: " + error);
|
||||
} else {
|
||||
try {
|
||||
fnType = getParameters(data.type);
|
||||
} catch (e) {
|
||||
error = e.message;
|
||||
_log("Error thrown in tern_worker, getParameters: " + error);
|
||||
var fnType = "";
|
||||
try {
|
||||
ternServer.request(request, function (error, data) {
|
||||
|
||||
var file = ternServer.findFile(fileInfo.name);
|
||||
|
||||
// convert query from partial to full offsets
|
||||
var newOffset = offset;
|
||||
if (fileInfo.type === MessageIds.TERN_FILE_INFO_TYPE_PART) {
|
||||
newOffset = {line: offset.line + fileInfo.offsetLines, ch: offset.ch};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Post a message back to the main thread with the completions
|
||||
self.postMessage({type: MessageIds.TERN_CALLED_FUNC_TYPE_MSG,
|
||||
file: fileInfo.name,
|
||||
offset: offset,
|
||||
fnType: fnType,
|
||||
error: error
|
||||
});
|
||||
});
|
||||
request = buildRequest(createEmptyUpdate(fileInfo.name), "type", newOffset);
|
||||
|
||||
var expr = Tern.findQueryExpr(file, request.query);
|
||||
Infer.resetGuessing();
|
||||
var type = Infer.expressionType(expr);
|
||||
type = type.getFunctionType() || type.getType();
|
||||
if (type) {
|
||||
fnType = getParameters(type);
|
||||
} else {
|
||||
error = "No parameter type found";
|
||||
_log(error);
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
error = e.message;
|
||||
_log("Error thrown in tern_worker:" + error);
|
||||
}
|
||||
|
||||
// Post a message back to the main thread with the completions
|
||||
self.postMessage({type: MessageIds.TERN_CALLED_FUNC_TYPE_MSG,
|
||||
file: fileInfo.name,
|
||||
offset: offset,
|
||||
fnType: fnType,
|
||||
error: error
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -819,8 +819,8 @@ define(function (require, exports, module) {
|
||||
});
|
||||
|
||||
it("should list function type", function () {
|
||||
var start = { line: 36, ch: 0 },
|
||||
middle = { line: 36, ch: 5 };
|
||||
var start = { line: 37, ch: 0 },
|
||||
middle = { line: 37, ch: 5 };
|
||||
|
||||
testDoc.replaceRange("funD(", start, start);
|
||||
testEditor.setCursorPos(middle);
|
||||
@@ -1199,8 +1199,7 @@ define(function (require, exports, module) {
|
||||
});
|
||||
});
|
||||
|
||||
// Tern issue #207. Tern can't parse its own function hint.
|
||||
xit("should list parameter hint for record type annotation", function () {
|
||||
it("should list parameter hint for record type annotation", function () {
|
||||
var testPos = { line: 178, ch: 25 };
|
||||
|
||||
testEditor.setCursorPos(testPos);
|
||||
|
||||
Reference in new issue
Block a user