parameter hint fixes

This commit is contained in:
dloverin committed 2013-07-25 13:32:37 -04:00
1 parent d86ae75c30
commit a176d7ee1e
5 files changed
+135 -64

No files matched your search

@@ -64,6 +64,9 @@ define(function (require, exports, module) {
POSITION_BELOW_OFFSET = 16, // Amount to adjust to top position when the preview bubble is below the text
POPOVER_HORZ_MARGIN = 5; // Horizontal margin
// keep jslint from complaining about handleCursorActivity being used before
// it was defined.
var handleCursorActivity;
/**
* Update the current session for use by the Function Hint Manager.
@@ -225,7 +228,7 @@ define(function (require, exports, module) {
$functionHintContainer.hide();
$functionHintContent.empty();
functionHintState = {};
$(session.editor).off("cursorActivity", handleFunctionHintCursorActivity);
$(session.editor).off("cursorActivity", handleCursorActivity);
if (!preserveFunctionHintStack) {
clearFunctionHintStack();
@@ -239,7 +242,13 @@ define(function (require, exports, module) {
* @param {boolean=} pushExistingHint - if true, push the existing hint on the stack. Default is false, not
* to push the hint.
* @param {string=} hint - function hint string from tern.
* @param {{inFunctionCall: boolean, functionCallPos: {{line: number, ch: number}}=} functionInfo -
* @param {{inFunctionCall: boolean, functionCallPos:
* {{line: number, ch: number}}=} functionInfo -
* if the functionInfo is already known, it can be passed in to avoid
* figuring it out again.
* @return {jQuery.Promise} - The promise will not complete until the
* hint has completed. Returns null, if the function hint is already
* displayed or the there is no function hint at the cursor.
*
*/
function popUpFunctionHint(pushExistingHint, hint, functionInfo) {
@@ -247,7 +256,7 @@ define(function (require, exports, module) {
functionInfo = functionInfo || session.getFunctionInfo();
if (!functionInfo.inFunctionCall) {
dismissFunctionHint();
return;
return null;
}
if (hasFunctionCallPosChanged(functionInfo.functionCallPos)) {
@@ -261,7 +270,7 @@ define(function (require, exports, module) {
dismissFunctionHint();
preserveFunctionHintStack = false;
} else if (isFunctionHintDisplayed()) {
return;
return null;
}
functionHintState.functionCallPos = functionInfo.functionCallPos;
@@ -287,15 +296,17 @@ define(function (require, exports, module) {
functionHintState.visible = true;
functionHintState.fnType = fnType;
$(session.editor).on("cursorActivity", handleFunctionHintCursorActivity);
$(session.editor).on("cursorActivity", handleCursorActivity);
});
return request;
}
/**
* Show the parameter the cursor is on in bold when the cursor moves.
* Dismiss the pop up when the cursor moves off the function.
*/
function handleFunctionHintCursorActivity() {
handleCursorActivity = function () {
var functionInfo = session.getFunctionInfo();
if (functionInfo.inFunctionCall) {
@@ -324,7 +335,7 @@ define(function (require, exports, module) {
}
dismissFunctionHint();
}
};
/**
* Enable cursor tracking in the current session.
@@ -332,7 +343,7 @@ define(function (require, exports, module) {
* @param {Session} session - session to stop cursor tracking on.
*/
function startCursorTracking(session) {
$(session.editor).on("cursorActivity", handleFunctionHintCursorActivity);
$(session.editor).on("cursorActivity", handleCursorActivity);
}
/**
@@ -343,7 +354,7 @@ define(function (require, exports, module) {
* @param {Session} session - session to stop cursor tracking on.
*/
function stopCursorTracking(session) {
$(session.editor).off("cursorActivity", handleFunctionHintCursorActivity);
$(session.editor).off("cursorActivity", handleCursorActivity);
}
/**
@@ -374,27 +385,35 @@ define(function (require, exports, module) {
});
}
/**
* Add the function hint command at start up.
*/
function addFunctionHintCommand() {
/* Register the command handler */
CommandManager.register(Strings.CMD_SHOW_PARAMETER_HINT, SHOW_FUNCTION_HINT_CMD_ID, handleShowFunctionHint);
// Add the menu items
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
if (menu) {
menu.addMenuItem(SHOW_FUNCTION_HINT_CMD_ID, KeyboardPrefs.showFunctionHint, Menus.AFTER, Commands.SHOW_CODE_HINTS);
}
// Close the function hint when commands are executed, except for the commands
// to show function hints for code hints.
$(CommandManager).on("beforeExecuteCommand", function (jqEvent, commandId) {
if (commandId !== SHOW_FUNCTION_HINT_CMD_ID &&
commandId !== Commands.SHOW_CODE_HINTS) {
dismissFunctionHint();
}
});
}
// Create the function hint container
$functionHintContainer = $(functionHintContainerHTML).appendTo($("body"));
$functionHintContent = $functionHintContainer.find(".function-hint-content");
/* Register all the command handlers */
CommandManager.register(Strings.CMD_SHOW_FUNCTION_HINT, SHOW_FUNCTION_HINT_CMD_ID, handleShowFunctionHint);
// Close the function hint when commands are executed, except for the commands
// to show function hints for code hints.
$(CommandManager).on("beforeExecuteCommand", function (jqEvent, commandId) {
if (commandId !== SHOW_FUNCTION_HINT_CMD_ID &&
commandId !== Commands.SHOW_CODE_HINTS) {
dismissFunctionHint();
}
});
// Add the menu items
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
menu.addMenuItem(SHOW_FUNCTION_HINT_CMD_ID, KeyboardPrefs.showFunctionHint, Menus.AFTER, Commands.SHOW_CODE_HINTS);
exports.PUSH_EXISTING_HINT = PUSH_EXISTING_HINT;
exports.addFunctionHintCommand = addFunctionHintCommand;
exports.dismissFunctionHint = dismissFunctionHint;
exports.installFunctionHintListeners = installFunctionHintListeners;
exports.isFunctionHintDisplayed = isFunctionHintDisplayed;
@@ -47,6 +47,9 @@ define(function (require, exports, module) {
*/
function getParameters(newFnType) {
// work around define functions before use warning.
var inferTypeToString, processInferFnTypeParameters;
/**
*
* @param {!Array.<name: {string}, type: {string},
@@ -122,11 +125,18 @@ define(function (require, exports, module) {
* @return {String}
*
*/
function inferTypeToString(inferType) {
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 {
@@ -134,7 +144,7 @@ define(function (require, exports, module) {
}
return result;
}
};
/**
* Convert an infer function type to a Google closure type string.
@@ -158,7 +168,7 @@ define(function (require, exports, module) {
* @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.
*/
function processInferFnTypeParameters(inferType) {
processInferFnTypeParameters = function (inferType) {
var params = [],
i;
@@ -167,6 +177,10 @@ define(function (require, exports, module) {
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;
@@ -184,14 +198,27 @@ define(function (require, exports, module) {
}
return params;
}
};
if (newFnType && newFnType.indexOf("fn(") === 0) {
var params = Infer.withContext(cx, function () {
var typeParser = new Infer.def.TypeParser(newFnType, null, null, true),
inferType = typeParser.parseType("", true);
return processInferFnTypeParameters(inferType);
try {
var typeParser = new Infer.def.TypeParser(newFnType, null, null, true),
inferType = typeParser.parseType("", true);
return processInferFnTypeParameters(inferType);
} catch (e) {
console.log(e.message);
return [e.message];
}
});
console.log("newFnType = " + newFnType);
console.log("convert to params:");
params.forEach(function (value) {
console.log(value);
});
return params;
@@ -187,12 +187,12 @@ define(function (require, exports, module) {
type = session.getType();
return !cachedHints || !cachedCursor || !cachedType ||
cachedCursor.line !== cursor.line ||
type.property !== cachedType.property ||
type.context !== cachedType.context ||
type.showFunctionType !== cachedType.showFunctionType ||
(type.functionCallPos && cachedType.functionCallPos &&
type.functionCallPos.ch !== cachedType.functionCallPos.ch);
cachedCursor.line !== cursor.line ||
type.property !== cachedType.property ||
type.context !== cachedType.context ||
type.showFunctionType !== cachedType.showFunctionType ||
(type.functionCallPos && cachedType.functionCallPos &&
type.functionCallPos.ch !== cachedType.functionCallPos.ch);
};
/**
@@ -788,6 +788,8 @@ define(function (require, exports, module) {
var jsHints = new JSHints();
CodeHintManager.registerHintProvider(jsHints, HintUtils.SUPPORTED_LANGUAGES, 0);
FunctionHintManager.addFunctionHintCommand();
// for unit testing
exports.getSession = getSession;
exports.jsHintProvider = jsHints;
@@ -39,7 +39,8 @@ define(function (require, exports, module) {
JSCodeHints = require("main"),
Preferences = require("Preferences"),
ScopeManager = require("ScopeManager"),
HintUtils = require("HintUtils");
HintUtils = require("HintUtils"),
FunctionHintManager = require("FunctionHintManager");
var extensionPath = FileUtils.getNativeModuleDirectoryPath(module),
testPath = extensionPath + "/unittest-files/basic-test-files/file1.js",
@@ -338,6 +339,39 @@ define(function (require, exports, module) {
}
/**
* 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
* records, where each element of the array describes a function parameter.
* If null, then no hint is expected.
*/
function showAndVerifyFunctionHint(expectedHint)
{
var request = FunctionHintManager.popUpFunctionHint();
if (expectedHint === null) {
expect(request).toBe(null);
return;
}
request.done(function () {
var actualHint = JSCodeHints.getSession().getFunctionHint(),
n = actualHint.length,
i;
// compare params to expected params
expect(actualHint.length.toBe(expectedHint.length));
for (i = 0; i < n; i++) {
expect(actualHint[i].name).toBe(expectedHint[i].name);
expect(actualHint[i].type).toBe(expectedHint[i].type);
}
});
}
function setupTest(path, primePump) {
DocumentManager.getDocumentForPath(path).done(function (doc) {
testDoc = doc;
@@ -737,9 +771,8 @@ define(function (require, exports, module) {
testDoc.replaceRange("funD(", start, start);
testEditor.setCursorPos(middle);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
hintsPresentExact(hintObj, ["funD(a: string, b: number) -> {x, y}"]);
showAndVerifyFunctionHint([{name: "a", type: "String"}, {name: "b", type: "number"}]);
});
});
@@ -821,9 +854,8 @@ define(function (require, exports, module) {
it("should list function type defined from .prototype", function () {
var start = { line: 59, ch: 10 };
testEditor.setCursorPos(start);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
hintsPresentExact(hintObj, ["calc(a4: number, b4: number) -> number"]);
showAndVerifyFunctionHint([{name: "a4", type: "number"}, {name: "b4", type: "number"}]);
});
});
@@ -842,9 +874,8 @@ define(function (require, exports, module) {
testDoc.replaceRange("myCustomer.setAmountDue(", start);
testEditor.setCursorPos(testPos);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
hintsPresentOrdered(hintObj, ["setAmountDue(amountDue: ?)"]);
showAndVerifyFunctionHint([{name:"amountDue", type: "Object"}]);
});
});
@@ -852,9 +883,8 @@ define(function (require, exports, module) {
var testPos = { line: 96, ch: 23 };
testEditor.setCursorPos(testPos);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
hintsPresentOrdered(hintObj, ["innerFunc(arg: string) -> {t}"]);
showAndVerifyFunctionHint([{name: "arg", type: "String"}]);
});
});
@@ -864,7 +894,7 @@ define(function (require, exports, module) {
testEditor.setCursorPos(testPos);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
hintsPresentOrdered(hintObj, ["t() -> string"]);
showAndVerifyFunctionHint([]);
});
});
@@ -887,9 +917,8 @@ define(function (require, exports, module) {
var testPos = { line: 123, ch: 11 };
testEditor.setCursorPos(testPos);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
hintsPresentExact(hintObj, ["funFuncArg(f: fn() -> number) -> number"]);
showAndVerifyFunctionHint([{name: "f", type: "function():number"}]);
});
});
@@ -898,9 +927,8 @@ define(function (require, exports, module) {
var testPos = { line: 139, ch: 12 };
testEditor.setCursorPos(testPos);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
hintsPresentExact(hintObj, ["funFunc2Arg(f: fn(s: string, n: number) -> string)"]);
showAndVerifyFunctionHint([{name: "f", type: "function(String s, number n):String)"}]);
});
});
@@ -920,9 +948,8 @@ define(function (require, exports, module) {
testDoc.replaceRange("funArr.index1(", start);
testEditor.setCursorPos(testPos);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
hintsPresentExact(hintObj, ["index1() -> number"]);
showAndVerifyFunctionHint([]);
});
});
@@ -1061,7 +1088,7 @@ define(function (require, exports, module) {
runs(function () {
testEditor.setCursorPos(func);
expectNoHints(JSCodeHints.jsHintProvider);
showAndVerifyFunctionHint(null);
testEditor.setCursorPos(param);
expectNoHints(JSCodeHints.jsHintProvider);
testEditor.setCursorPos(variable);
@@ -1113,9 +1140,8 @@ define(function (require, exports, module) {
testPos = { line: 80, ch: 24 };
testDoc.replaceRange("myCustomer.setAmountDue(10)", start);
testEditor.setCursorPos(testPos);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
hintsPresentOrdered(hintObj, ["setAmountDue(amountDue: number)"]);
showAndVerifyFunctionHint([{name: "amountDue", type: "number"}]);
});
});
@@ -1147,9 +1173,8 @@ define(function (require, exports, module) {
var start = { line: 36, ch: 12 };
testEditor.setCursorPos(start);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
hintsPresentExact(hintObj, ["foo(a: number) -> string"]);
showAndVerifyFunctionHint([{name: "a", type: "number"}]);
});
});
@@ -1157,9 +1182,8 @@ define(function (require, exports, module) {
var start = { line: 22, ch: 17 };
testEditor.setCursorPos(start);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
hintsPresentExact(hintObj, ["funD(a: string, b: number) -> {x, y}"]);
showAndVerifyFunctionHint([{name: "a", type: "String"}, {name: "b", type: "Number"}]);
});
});
@@ -1167,9 +1191,8 @@ define(function (require, exports, module) {
var start = { line: 23, ch: 17 };
testEditor.setCursorPos(start);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
hintsPresentExact(hintObj, ["funE(paramE1: D1, paramE2: number)"]);
showAndVerifyFunctionHint([{name: "paramE1", type: "D1"}, {name: "paramE2", type: "Number"}]);
});
});