fix unit tests and move function hint type parsing into the tern worker.

This commit is contained in:
dloverin committed 2013-07-26 13:08:59 -04:00
1 parent cc6d344694
commit 6ffa193689
7 files changed
+310 -44

No files matched your search

@@ -21,7 +21,7 @@
*
*/
/*jslint undef: true, vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true */
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true */
/*global define, brackets */
define(function (require, exports, module) {
@@ -54,9 +54,11 @@ define(function (require, exports, module) {
*
* @param {!Array.<name: {string}, type: {string},
* isOptional: {boolean}>} params - array of parameter descriptors
* @param {boolean=} typesOnly - only show parameter types. The
* default behavior is to include both parameter names and types.
* @returns {string} parameters formatted as a string
*/
function formatParams(params) {
function formatParams(params, typesOnly) {
var result = "";
params.forEach(function (value, i) {
@@ -75,7 +77,10 @@ define(function (require, exports, module) {
var type = value.type;
result += type;
result += " " + value.name;
if (!typesOnly) {
result += " " + value.name;
}
if (value.isOptional) {
result += "]";
@@ -156,8 +161,8 @@ define(function (require, exports, module) {
var result = "function(",
params = processInferFnTypeParameters(inferType);
result += formatParams(params);
result += "):";
result += formatParams(params, true);
result += "): ";
result += inferTypeToString(inferType.retval);
return result;
}
@@ -209,7 +214,7 @@ define(function (require, exports, module) {
return processInferFnTypeParameters(inferType);
} catch (e) {
console.log(e.message);
return [e.message];
return [{name: e.message, type: ""}];
}
});
@@ -21,7 +21,7 @@
*
*/
/*jslint undef: true, vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true */
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true */
/*global define, brackets, $ */
define(function (require, exports, module) {
@@ -100,8 +100,15 @@ define(function (require, exports, module) {
top = ypos - hintHeight - POINTER_TOP_OFFSET,
left = xpos,
$editorHolder = $("#editor-holder"),
editorLeft = $editorHolder.offset().left;
editorLeft;
if ($editorHolder.offset() === undefined) {
// this happens in jasmine tests that run
// without a windowed document.
return;
}
editorLeft = $editorHolder.offset().left;
left = Math.max(left, editorLeft);
left = Math.min(left, editorLeft + $editorHolder.width() - hintWidth);
@@ -276,12 +283,15 @@ define(function (require, exports, module) {
hintState.functionCallPos = functionInfo.functionCallPos;
var request = null;
var $deferredPopUp = $.Deferred();
if (!hint) {
request = ScopeManager.requestFunctionHint(session, functionInfo.functionCallPos);
} else {
session.setFnType(hint);
request = $.Deferred();
request.resolveWith(null, [hint]);
$deferredPopUp.resolveWith(null);
}
request.done(function (fnType) {
@@ -297,9 +307,10 @@ define(function (require, exports, module) {
hintState.fnType = fnType;
$(session.editor).on("cursorActivity", handleCursorActivity);
$deferredPopUp.resolveWith(null);
});
return request;
return $deferredPopUp;
}
/**
@@ -1290,6 +1290,7 @@ define(function (require, exports, module) {
$.when(fnTypePromise).done(
function (fnType) {
session.setFnType(fnType);
session.setFunctionCallPos(functionOffset);
$deferredHints.resolveWith(null, [fnType]);
}
).fail(function () {
@@ -630,28 +630,35 @@ define(function (require, exports, module) {
* @param {string} newFnType - the new tern function hint.
*/
Session.prototype.setFnType = function (newFnType) {
this.fnType = InferUtils.getParameters(newFnType);
this.fnType = newFnType; //InferUtils.getParameters(newFnType);
};
/**
* The position of the function call for the current fnType.
*
* @param functionCallPos
*/
Session.prototype.setFunctionCallPos = function (functionCallPos) {
this.functionCallPos = functionCallPos;
}
/**
* Get the function type hint. This will format the hint, showing the
* parameter at the cursor in bold.
*
* @param {{line: number, ch: number}} functionCallPos - the position of the current
* function call being hinted.
* @return {{parameters: Array<{name: string, type: string, isOptional: boolean}>,
* currentIndex: number}} An Object where the
* "parameters" property is an array of parameter objects;
* the "currentIndex" property index of the hint the cursor is on, may be
* -1 if the cursor is on the function identifier.
*/
Session.prototype.getParameterHint = function (functionCallPos) {
Session.prototype.getParameterHint = function () {
var fnHint = this.fnType,
cursor = this.getCursor(),
token = this.getToken(functionCallPos),
start = {line: functionCallPos.line, ch: token.start},
token = this.getToken(this.functionCallPos),
start = {line: this.functionCallPos.line, ch: token.start},
fragment = this.editor.document.getRange(start,
{line: functionCallPos.line + 10, ch: 0});
{line: this.functionCallPos.line + 10, ch: 0});
var ast;
try {
@@ -715,7 +722,7 @@ define(function (require, exports, module) {
}
// if there are no args, then figure out if we are on the function identifier
if (n === 0 && cursorOffset > this.getOffsetFromCursor(functionCallPos)) {
if (n === 0 && cursorOffset > this.getOffsetFromCursor(this.functionCallPos)) {
currentArg = 0;
}
}
@@ -30,12 +30,14 @@ importScripts("thirdparty/requirejs/require.js");
"use strict";
var MessageIds;
var Tern;
var Tern, Infer;
require(["./MessageIds"], function (messageIds) {
MessageIds = messageIds;
var ternRequire = require.config({baseUrl: "./thirdparty"});
ternRequire(["tern/lib/tern", "tern/plugin/requirejs", "tern/plugin/doc_comment"], function (tern, requirejs, docComment) {
ternRequire(["tern/lib/tern", "tern/lib/infer", "tern/plugin/requirejs", "tern/plugin/doc_comment"],
function (tern, infer, requirejs, docComment) {
Tern = tern;
Infer = infer;
var ternServer = null;
@@ -292,7 +294,200 @@ importScripts("thirdparty/requirejs/require.js");
}
});
}
/**
* Given a Tern formatted function type string, convert it to an array of Objects, where each object describes
* a parameter.
*
* @param {String} newFnType - Tern formatted function type string.
* @returns {Array<{name: string, type: string, isOptional: boolean}>} where each entry in the array is a parameter.
*/
function getParameters(newFnType) {
// work around define functions before use warning.
var inferTypeToString, processInferFnTypeParameters;
/**
*
* @param {!Array.<name: {string}, type: {string},
* isOptional: {boolean}>} params - array of parameter descriptors
* @param {boolean=} typesOnly - only show parameter types. The
* default behavior is to include both parameter names and types.
* @returns {string} parameters formatted as a string
*/
function formatParams(params, typesOnly) {
var result = "";
params.forEach(function (value, i) {
if (value.isOptional) {
if (i > 0) {
result += " ";
}
result += "[";
}
if (i > 0) {
result += ", ";
}
var type = value.type;
result += type;
if (!typesOnly) {
result += " " + value.name;
}
if (value.isOptional) {
result += "]";
}
});
return result;
}
/**
* Convert an infer array type to a string.
*
* Formatted using google closure style. For example:
*
* "Array.<string, number>"
*
* @param {Infer.Arr} inferArrType
*
* @return {String} - array formatted in google closure style.
*
*/
function inferArrTypeToString(inferArrType) {
var result = "Array.<";
inferArrType.props["<i>"].types.forEach(function (value, i) {
if (i > 0) {
result += ", ";
}
result += inferTypeToString(value);
});
// workaround case where types is zero length
if (inferArrType.props["<i>"].types.length === 0) {
result += "Object";
}
result += ">";
return result;
}
/**
* Convert an infer type to a string.
*
* @param {*} inferType - one of the Infer's types; infer.Prim, infer.Arr, infer.ANull. infer.Fn functions are
* not handled here.
*
* @return {String}
*
*/
inferTypeToString = function (inferType) {
var result;
if (inferType instanceof infer.Prim) {
result = inferType.toString();
if (result === "string") {
result = "String";
} else if (result === "number") {
result = "Number";
} else if (result === "boolean") {
result = "Boolean";
}
} else if (inferType instanceof infer.Arr) {
result = inferArrTypeToString(inferType);
} else if (inferType instanceof Infer.Fn) {
result = inferFnTypeToString(inferType);
} else if (inferType instanceof infer.Obj) {
result = inferType.name;
} else {
result = "Object";
}
return result;
};
/**
* Convert an infer function type to a Google closure type string.
*
* @param {Infer.Fn} inferType - type to convert.
* @return {string} - function type as a string.
*/
function inferFnTypeToString(inferType) {
var result = "function(",
params = processInferFnTypeParameters(inferType);
result += formatParams(params, true);
result += "):";
result += inferTypeToString(inferType.retval);
return result;
}
/**
* Convert an infer function type to string.
*
* @param {*} inferType - one of the Infer's types; infer.Fn, infer.Prim, infer.Arr, infer.ANull
* @returns {Array<{name: string, type: string, isOptional: boolean}>} where each entry in the array is a parameter.
*/
processInferFnTypeParameters = function (inferType) {
var params = [],
i;
for (i = 0; i < inferType.args.length; i++) {
var param = {},
name = inferType.argNames[i],
type = inferType.args[i];
if (name === undefined) {
name = 'arg' + (i + 1);
}
if (name[name.length - 1] === "?") {
name = name.substring(0, name.length - 1);
param.isOptional = true;
}
param.name = name;
param.type = inferTypeToString(type);
params.push(param);
}
return params;
};
if (newFnType && newFnType.indexOf("fn(") === 0) {
var params = Infer.withContext(ternServer.cx, function () {
try {
var typeParser = new Infer.def.TypeParser(newFnType, null, null, true),
inferType = typeParser.parseType("", true);
return processInferFnTypeParameters(inferType);
} catch (e) {
_log(e.message);
return [{name: e.message, type: ""}];
}
});
_log("newFnType = " + newFnType);
_log("convert to params:");
params.forEach(function (value) {
_log(value);
});
return params;
} else {
return [];
}
}
/**
* Get the function type for the given offset
*
@@ -316,7 +511,7 @@ importScripts("thirdparty/requirejs/require.js");
if (error) {
_log("Error returned from Tern 'type' request: " + error);
} else {
fnType = data.type;
fnType = getParameters(data.type);
}
// Post a message back to the main thread with the completions
@@ -160,6 +160,17 @@ require(["MyModule"], function (myModule) {
var x = myModule.c;
});
/**
* Test record type google annoations.
*
* @param {{line: number, type: string}} t
*/
function testRecordTypeAnnotation(t) {
'use strict';
}
// Function hint testing
function functionHintTesting() {
"use strict";
@@ -167,6 +178,9 @@ function functionHintTesting() {
// simple function with primative arguments
s.lastIndexOf(s);
// function with record type argument
testRecordTypeAnnotation();
// function with a function argument
arr.sort();
@@ -154,6 +154,7 @@ define(function (require, exports, module) {
runs(function () { callback(hintList); });
}
/*
* Test if hints should be closed or not closed at a given position.
*
@@ -339,34 +340,66 @@ define(function (require, exports, module) {
}
/**
* Verify there is no parameter hint at the current cursor.
*/
function expectNoParameterHint() {
expect(ParameterHintManager.popUpHint()).toBe(null);
}
/*
* Wait for a hint response object to resolve, then apply a callback
* to the result
*
* @param {Object + jQuery.Deferred} hintObj - a hint response object,
* possibly deferred
* @param {Function} callback - the callback to apply to the resolved
* hint response object
*/
function _waitForParameterHint(hintObj, callback) {
var complete = false,
hint = null;
hintObj.done(function () {
hint = JSCodeHints.getSession().getParameterHint();
complete = true;
});
waitsFor(function () {
return complete;
}, "Expected parameter hint did not resolve", 3000);
runs(function () { callback(hint); });
}
/**
* Show a function hint based on the code at the cursor. Verify the
* hint matches the passed in value.
*
* @param {?Array.<{name: string, type: string}> expectedHint - array of
* @param {?Array.<{name: string, type: string}> expectedParams - array of
* records, where each element of the array describes a function parameter.
* If null, then no hint is expected.
*/
function popUpAndVerifyParameterHint(expectedHint)
function expectParameterHint(expectedParams)
{
var request = ParameterHintManager.popUpHint();
if (expectedHint === null) {
if (expectedParams === null) {
expect(request).toBe(null);
return;
}
request.done(function () {
var actualHint = JSCodeHints.getSession().getParameterHint(),
n = actualHint.length,
_waitForParameterHint(request, function (hint) {
var params = hint.parameters,
n = params.length,
i;
// compare params to expected params
expect(actualHint.length.toBe(expectedHint.length));
expect(params.length).toBe(expectedParams.length);
for (i = 0; i < n; i++) {
expect(actualHint[i].name).toBe(expectedHint[i].name);
expect(actualHint[i].type).toBe(expectedHint[i].type);
expect(params[i].name).toBe(expectedParams[i].name);
expect(params[i].type).toBe(expectedParams[i].type);
}
});
@@ -772,7 +805,7 @@ define(function (require, exports, module) {
testDoc.replaceRange("funD(", start, start);
testEditor.setCursorPos(middle);
runs(function () {
popUpAndVerifyParameterHint([{name: "a", type: "String"}, {name: "b", type: "number"}]);
expectParameterHint([{name: "a", type: "String"}, {name: "b", type: "Number"}]);
});
});
@@ -855,7 +888,7 @@ define(function (require, exports, module) {
var start = { line: 59, ch: 10 };
testEditor.setCursorPos(start);
runs(function () {
popUpAndVerifyParameterHint([{name: "a4", type: "number"}, {name: "b4", type: "number"}]);
expectParameterHint([{name: "a4", type: "Number"}, {name: "b4", type: "Number"}]);
});
});
@@ -875,7 +908,7 @@ define(function (require, exports, module) {
testDoc.replaceRange("myCustomer.setAmountDue(", start);
testEditor.setCursorPos(testPos);
runs(function () {
popUpAndVerifyParameterHint([{name:"amountDue", type: "Object"}]);
expectParameterHint([{name:"amountDue", type: "Object"}]);
});
});
@@ -884,7 +917,7 @@ define(function (require, exports, module) {
testEditor.setCursorPos(testPos);
runs(function () {
popUpAndVerifyParameterHint([{name: "arg", type: "String"}]);
expectParameterHint([{name: "arg", type: "String"}]);
});
});
@@ -894,7 +927,7 @@ define(function (require, exports, module) {
testEditor.setCursorPos(testPos);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
popUpAndVerifyParameterHint([]);
expectParameterHint([]);
});
});
@@ -918,17 +951,17 @@ define(function (require, exports, module) {
testEditor.setCursorPos(testPos);
runs(function () {
popUpAndVerifyParameterHint([{name: "f", type: "function():number"}]);
expectParameterHint([{name: "f", type: "function(): number"}]);
});
});
// parameter type anotation tests
// parameter type annotation tests
it("should list parameter function type and best guess for function call/return types", function () {
var testPos = { line: 139, ch: 12 };
testEditor.setCursorPos(testPos);
runs(function () {
popUpAndVerifyParameterHint([{name: "f", type: "function(String s, number n):String)"}]);
expectParameterHint([{name: "f", type: "function(String, Number):String"}]);
});
});
@@ -949,7 +982,7 @@ define(function (require, exports, module) {
testDoc.replaceRange("funArr.index1(", start);
testEditor.setCursorPos(testPos);
runs(function () {
popUpAndVerifyParameterHint([]);
expectParameterHint([]);
});
});
@@ -1088,7 +1121,7 @@ define(function (require, exports, module) {
runs(function () {
testEditor.setCursorPos(func);
popUpAndVerifyParameterHint(null);
expectNoParameterHint();
testEditor.setCursorPos(param);
expectNoHints(JSCodeHints.jsHintProvider);
testEditor.setCursorPos(variable);
@@ -1141,7 +1174,7 @@ define(function (require, exports, module) {
testDoc.replaceRange("myCustomer.setAmountDue(10)", start);
testEditor.setCursorPos(testPos);
runs(function () {
popUpAndVerifyParameterHint([{name: "amountDue", type: "number"}]);
expectParameterHint([{name: "amountDue", type: "Number"}]);
});
});
@@ -1174,7 +1207,7 @@ define(function (require, exports, module) {
testEditor.setCursorPos(start);
runs(function () {
popUpAndVerifyParameterHint([{name: "a", type: "number"}]);
expectParameterHint([{name: "a", type: "Number"}]);
});
});
@@ -1183,7 +1216,7 @@ define(function (require, exports, module) {
testEditor.setCursorPos(start);
runs(function () {
popUpAndVerifyParameterHint([{name: "a", type: "String"}, {name: "b", type: "Number"}]);
expectParameterHint([{name: "a", type: "String"}, {name: "b", type: "Number"}]);
});
});
@@ -1192,7 +1225,7 @@ define(function (require, exports, module) {
testEditor.setCursorPos(start);
runs(function () {
popUpAndVerifyParameterHint([{name: "paramE1", type: "D1"}, {name: "paramE2", type: "Number"}]);
expectParameterHint([{name: "paramE1", type: "D1"}, {name: "paramE2", type: "Number"}]);
});
});