This commit is contained in:
dloverin committed 2013-08-01 21:05:07 -04:00
1 parent a9c916726f
commit 5205cba81c
7 files changed
+146 -144

No files matched your search

@@ -38,22 +38,22 @@ define(function (require, exports, module) {
/** @const {string} Show Function Hint command ID */
var SHOW_FUNCTION_HINT_CMD_ID = "showFunctionHint", // string must MATCH string in native code (brackets_extensions)
var SHOW_PARAMETER_HINT_CMD_ID = "showParameterHint", // string must MATCH string in native code (brackets_extensions)
PUSH_EXISTING_HINT = true,
OVERWRITE_EXISTING_HINT = false,
PRESERVE_FUNCTION_STACK = true,
functionHintContainerHTML = require("text!FunctionHintTemplate.html"),
hintContainerHTML = require("text!ParameterHintTemplate.html"),
KeyboardPrefs = JSON.parse(require("text!keyboard.json"));
var $functionHintContainer, // function hint container
$functionHintContent, // function hint content holder
var $hintContainer, // function hint container
$hintContent, // function hint content holder
/** @type {{inFunctionCall: boolean, functionCallPos: {line: number, ch: number},
* fnType: {string}}
*/
functionHintState = {},
functionHintStack = [], // stack for previous function hint to restore
preserveFunctionHintStack, // close a function hint without clearing stack
hintState = {},
hintStack = [], // stack for previous function hint to restore
preserveHintStack, // close a function hint without clearing stack
session; // current editor session, updated by main
// Constants
@@ -83,8 +83,8 @@ define(function (require, exports, module) {
* @returns {boolean} - true if a function hint is being displayed, false
* otherwise.
*/
function isFunctionHintDisplayed() {
return functionHintState.visible === true;
function isHintDisplayed() {
return hintState.visible === true;
}
/**
@@ -94,9 +94,9 @@ define(function (require, exports, module) {
* @param {number} ypos
* @param {number} ybot
*/
function positionFunctionHint(xpos, ypos, ybot) {
var hintWidth = $functionHintContainer.width(),
hintHeight = $functionHintContainer.height(),
function positionHint(xpos, ypos, ybot) {
var hintWidth = $hintContainer.width(),
hintHeight = $hintContainer.height(),
top = ypos - hintHeight - POINTER_TOP_OFFSET,
left = xpos,
$editorHolder = $("#editor-holder"),
@@ -106,17 +106,17 @@ define(function (require, exports, module) {
left = Math.min(left, editorLeft + $editorHolder.width() - hintWidth);
if (top < 0) {
$functionHintContainer.removeClass("preview-bubble-above");
$functionHintContainer.addClass("preview-bubble-below");
$hintContainer.removeClass("preview-bubble-above");
$hintContainer.addClass("preview-bubble-below");
top = ybot + POSITION_BELOW_OFFSET;
$functionHintContainer.offset({
$hintContainer.offset({
left: left,
top: top
});
} else {
$functionHintContainer.removeClass("preview-bubble-below");
$functionHintContainer.addClass("preview-bubble-above");
$functionHintContainer.offset({
$hintContainer.removeClass("preview-bubble-below");
$hintContainer.addClass("preview-bubble-above");
$hintContainer.offset({
left: left,
top: top - POINTER_TOP_OFFSET
});
@@ -130,11 +130,11 @@ define(function (require, exports, module) {
* functionInfo - tells if the caret is in a function call and the position
* of the function call.
*/
function formatFunctionHint(functionInfo) {
var hints = session.getFunctionHint(functionInfo.functionCallPos);
function formatHint(functionInfo) {
var hints = session.getParameterHint(functionInfo.functionCallPos);
$functionHintContent.empty();
$functionHintContent.addClass("brackets-js-hints");
$hintContent.empty();
$hintContent.addClass("brackets-js-hints");
hints.parameters.forEach(function (value, i) {
var param = value.type + " " + value.name,
separator = "";
@@ -152,24 +152,24 @@ define(function (require, exports, module) {
}
if (separator) {
$functionHintContent.append(separator);
$hintContent.append(separator);
}
if (hints.currentIndex === i) {
$functionHintContent.append($("<span>")
$hintContent.append($("<span>")
.append(StringUtils.htmlEscape(param))
.addClass("current-parameter"));
} else {
$functionHintContent.append(StringUtils.htmlEscape(param));
$hintContent.append(StringUtils.htmlEscape(param));
}
if (value.isOptional) {
$functionHintContent.append("]");
$hintContent.append("]");
}
});
if (hints.parameters.length === 0) {
$functionHintContent.append(StringUtils.htmlEscape(Strings.NO_ARGUMENTS));
$hintContent.append(StringUtils.htmlEscape(Strings.NO_ARGUMENTS));
}
}
@@ -178,8 +178,8 @@ define(function (require, exports, module) {
* for a parameter, when the parameter already part of an existing parameter
* hint.
*/
function pushFunctionHintOnStack() {
functionHintStack.push(functionHintState);
function pushHintOnStack() {
hintStack.push(hintState);
}
/**
@@ -187,10 +187,10 @@ define(function (require, exports, module) {
*
* @returns {boolean} - true the a parameter hint has been pushed, false otherwise.
*/
function popFunctionHintFromStack() {
if (functionHintStack.length > 0) {
functionHintState = functionHintStack.pop();
functionHintState.visible = false;
function popHintFromStack() {
if (hintStack.length > 0) {
hintState = hintStack.pop();
hintState.visible = false;
return true;
}
@@ -201,7 +201,7 @@ define(function (require, exports, module) {
* Reset the function hint stack.
*/
function clearFunctionHintStack() {
functionHintStack = [];
hintStack = [];
}
/**
@@ -212,7 +212,7 @@ define(function (require, exports, module) {
* @returns {boolean}
*/
function hasFunctionCallPosChanged(functionCallPos) {
var oldFunctionCallPos = functionHintState.functionCallPos;
var oldFunctionCallPos = hintState.functionCallPos;
return (oldFunctionCallPos === undefined ||
oldFunctionCallPos.line !== functionCallPos.line ||
oldFunctionCallPos.ch !== functionCallPos.ch);
@@ -222,15 +222,15 @@ define(function (require, exports, module) {
* Dismiss the function hint.
*
*/
function dismissFunctionHint() {
function dismissHint() {
if (functionHintState.visible) {
$functionHintContainer.hide();
$functionHintContent.empty();
functionHintState = {};
if (hintState.visible) {
$hintContainer.hide();
$hintContent.empty();
hintState = {};
$(session.editor).off("cursorActivity", handleCursorActivity);
if (!preserveFunctionHintStack) {
if (!preserveHintStack) {
clearFunctionHintStack();
}
}
@@ -251,29 +251,29 @@ define(function (require, exports, module) {
* displayed or the there is no function hint at the cursor.
*
*/
function popUpFunctionHint(pushExistingHint, hint, functionInfo) {
function popUpHint(pushExistingHint, hint, functionInfo) {
functionInfo = functionInfo || session.getFunctionInfo();
if (!functionInfo.inFunctionCall) {
dismissFunctionHint();
dismissHint();
return null;
}
if (hasFunctionCallPosChanged(functionInfo.functionCallPos)) {
var pushHint = pushExistingHint && isFunctionHintDisplayed();
var pushHint = pushExistingHint && isHintDisplayed();
if (pushHint) {
pushFunctionHintOnStack();
preserveFunctionHintStack = true;
pushHintOnStack();
preserveHintStack = true;
}
dismissFunctionHint();
preserveFunctionHintStack = false;
} else if (isFunctionHintDisplayed()) {
dismissHint();
preserveHintStack = false;
} else if (isHintDisplayed()) {
return null;
}
functionHintState.functionCallPos = functionInfo.functionCallPos;
hintState.functionCallPos = functionInfo.functionCallPos;
var request = null;
if (!hint) {
@@ -289,12 +289,12 @@ define(function (require, exports, module) {
var pos = cm.charCoords(functionInfo.functionCallPos);
formatFunctionHint(functionInfo);
formatHint(functionInfo);
$functionHintContainer.show();
positionFunctionHint(pos.left, pos.top, pos.bottom);
functionHintState.visible = true;
functionHintState.fnType = fnType;
$hintContainer.show();
positionHint(pos.left, pos.top, pos.bottom);
hintState.visible = true;
hintState.fnType = fnType;
$(session.editor).on("cursorActivity", handleCursorActivity);
});
@@ -313,28 +313,28 @@ define(function (require, exports, module) {
// If in a different function hint, then dismiss the old one a
// display the new one if there is one on the stack
if (hasFunctionCallPosChanged(functionInfo.functionCallPos)) {
if (popFunctionHintFromStack()) {
var poppedFunctionCallPos = functionHintState.functionCallPos,
if (popHintFromStack()) {
var poppedFunctionCallPos = hintState.functionCallPos,
currentFunctionCallPos = functionInfo.functionCallPos;
if (poppedFunctionCallPos.line === currentFunctionCallPos.line &&
poppedFunctionCallPos.ch === currentFunctionCallPos.ch) {
preserveFunctionHintStack = true;
popUpFunctionHint(OVERWRITE_EXISTING_HINT,
functionHintState.fnType, functionInfo);
preserveFunctionHintStack = false;
preserveHintStack = true;
popUpHint(OVERWRITE_EXISTING_HINT,
hintState.fnType, functionInfo);
preserveHintStack = false;
return;
}
} else {
dismissFunctionHint();
dismissHint();
}
}
formatFunctionHint(functionInfo);
formatHint(functionInfo);
return;
}
dismissFunctionHint();
dismissHint();
};
/**
@@ -358,14 +358,14 @@ define(function (require, exports, module) {
}
/**
* Show a function hint in its own pop-up.
* Show a parameter hint in its own pop-up.
*
* @param commandData
*/
function handleShowFunctionHint(commandData) {
function handleShowParameterHint(commandData) {
// Pop up function hint
popUpFunctionHint();
popUpHint();
}
/**
@@ -374,52 +374,52 @@ define(function (require, exports, module) {
* @param {Editor} editor - editor context on which to listen for
* changes
*/
function installFunctionHintListeners(editor) {
function installListeners(editor) {
$(editor).on("keyEvent", function (jqEvent, editor, event) {
if (event.type === "keydown" && event.keyCode === KeyEvent.DOM_VK_ESCAPE) {
dismissFunctionHint();
dismissHint();
}
}).on("scroll", function () {
dismissFunctionHint();
dismissHint();
});
}
/**
* Add the function hint command at start up.
*/
function addFunctionHintCommand() {
function addCommands() {
/* Register the command handler */
CommandManager.register(Strings.CMD_SHOW_PARAMETER_HINT, SHOW_FUNCTION_HINT_CMD_ID, handleShowFunctionHint);
CommandManager.register(Strings.CMD_SHOW_PARAMETER_HINT, SHOW_PARAMETER_HINT_CMD_ID, handleShowParameterHint);
// 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);
menu.addMenuItem(SHOW_PARAMETER_HINT_CMD_ID, KeyboardPrefs.showParameterHint, 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 &&
if (commandId !== SHOW_PARAMETER_HINT_CMD_ID &&
commandId !== Commands.SHOW_CODE_HINTS) {
dismissFunctionHint();
dismissHint();
}
});
}
// Create the function hint container
$functionHintContainer = $(functionHintContainerHTML).appendTo($("body"));
$functionHintContent = $functionHintContainer.find(".function-hint-content");
$hintContainer = $(hintContainerHTML).appendTo($("body"));
$hintContent = $hintContainer.find(".function-hint-content");
exports.PUSH_EXISTING_HINT = PUSH_EXISTING_HINT;
exports.addFunctionHintCommand = addFunctionHintCommand;
exports.dismissFunctionHint = dismissFunctionHint;
exports.installFunctionHintListeners = installFunctionHintListeners;
exports.isFunctionHintDisplayed = isFunctionHintDisplayed;
exports.popUpFunctionHint = popUpFunctionHint;
exports.setSession = setSession;
exports.startCursorTracking = startCursorTracking;
exports.stopCursorTracking = stopCursorTracking;
exports.PUSH_EXISTING_HINT = PUSH_EXISTING_HINT;
exports.addCommands = addCommands;
exports.dismissHint = dismissHint;
exports.installListeners = installListeners;
exports.isHintDisplayed = isHintDisplayed;
exports.popUpHint = popUpHint;
exports.setSession = setSession;
exports.startCursorTracking = startCursorTracking;
exports.stopCursorTracking = stopCursorTracking;
});
@@ -645,7 +645,7 @@ define(function (require, exports, module) {
* the "currentIndex" property index of the hint the cursor is on, may be
* -1 if the cursor is on the function identifier.
*/
Session.prototype.getFunctionHint = function (functionCallPos) {
Session.prototype.getParameterHint = function (functionCallPos) {
var fnHint = this.fnType,
cursor = this.getCursor(),
token = this.getToken(functionCallPos),
@@ -1,5 +1,5 @@
{
"showFunctionHint": [
"showParameterHint": [
{
"key": "Ctrl-Shift-Space"
},
@@ -27,24 +27,24 @@
define(function (require, exports, module) {
"use strict";
var CodeHintManager = brackets.getModule("editor/CodeHintManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
DocumentManager = brackets.getModule("document/DocumentManager"),
Commands = brackets.getModule("command/Commands"),
CommandManager = brackets.getModule("command/CommandManager"),
Menus = brackets.getModule("command/Menus"),
AppInit = brackets.getModule("utils/AppInit"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
PerfUtils = brackets.getModule("utils/PerfUtils"),
StringUtils = brackets.getModule("utils/StringUtils"),
StringMatch = brackets.getModule("utils/StringMatch"),
LanguageManager = brackets.getModule("language/LanguageManager"),
ProjectManager = brackets.getModule("project/ProjectManager"),
FunctionHintManager = require("FunctionHintManager"),
HintUtils = require("HintUtils"),
ScopeManager = require("ScopeManager"),
Session = require("Session"),
Acorn = require("thirdparty/acorn/acorn");
var CodeHintManager = brackets.getModule("editor/CodeHintManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
DocumentManager = brackets.getModule("document/DocumentManager"),
Commands = brackets.getModule("command/Commands"),
CommandManager = brackets.getModule("command/CommandManager"),
Menus = brackets.getModule("command/Menus"),
AppInit = brackets.getModule("utils/AppInit"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
PerfUtils = brackets.getModule("utils/PerfUtils"),
StringUtils = brackets.getModule("utils/StringUtils"),
StringMatch = brackets.getModule("utils/StringMatch"),
LanguageManager = brackets.getModule("language/LanguageManager"),
ProjectManager = brackets.getModule("project/ProjectManager"),
ParameterHintManager = require("ParameterHintManager"),
HintUtils = require("HintUtils"),
ScopeManager = require("ScopeManager"),
Session = require("Session"),
Acorn = require("thirdparty/acorn/acorn");
var session = null, // object that encapsulates the current session state
cachedCursor = null, // last cursor of the current hinting session
@@ -524,17 +524,17 @@ define(function (require, exports, module) {
// stop cursor tracking before setting the cursor to avoid bringing
// down the current hint.
if (FunctionHintManager.isFunctionHintDisplayed()) {
FunctionHintManager.stopCursorTracking(session);
if (ParameterHintManager.isHintDisplayed()) {
ParameterHintManager.stopCursorTracking(session);
}
session.editor._codeMirror.setCursor(pos);
if (FunctionHintManager.isFunctionHintDisplayed()) {
FunctionHintManager.startCursorTracking(session);
if (ParameterHintManager.isHintDisplayed()) {
ParameterHintManager.startCursorTracking(session);
}
FunctionHintManager.popUpFunctionHint(FunctionHintManager.PUSH_EXISTING_HINT, hint.type);
ParameterHintManager.popUpHint(ParameterHintManager.PUSH_EXISTING_HINT, hint.type);
}
// Return false to indicate that another hinting session is not needed
@@ -555,7 +555,7 @@ define(function (require, exports, module) {
session = new Session(editor);
ScopeManager.handleEditorChange(session, editor.document,
previousEditor ? previousEditor.document : null);
FunctionHintManager.setSession(session);
ParameterHintManager.setSession(session);
cachedHints = null;
}
@@ -580,7 +580,7 @@ define(function (require, exports, module) {
ignoreChange = false;
});
FunctionHintManager.installFunctionHintListeners(editor);
ParameterHintManager.installListeners(editor);
} else {
session = null;
}
@@ -788,7 +788,7 @@ define(function (require, exports, module) {
var jsHints = new JSHints();
CodeHintManager.registerHintProvider(jsHints, HintUtils.SUPPORTED_LANGUAGES, 0);
FunctionHintManager.addFunctionHintCommand();
ParameterHintManager.addCommands();
// for unit testing
exports.getSession = getSession;
@@ -162,6 +162,8 @@ require(["MyModule"], function (myModule) {
// Function hint testing
function functionHintTesting() {
"use strict";
// simple function with primative arguments
s.lastIndexOf(s);
@@ -27,20 +27,20 @@
define(function (require, exports, module) {
"use strict";
var Commands = brackets.getModule("command/Commands"),
CommandManager = brackets.getModule("command/CommandManager"),
DocumentManager = brackets.getModule("document/DocumentManager"),
Editor = brackets.getModule("editor/Editor").Editor,
EditorManager = brackets.getModule("editor/EditorManager"),
FileUtils = brackets.getModule("file/FileUtils"),
NativeFileSystem = brackets.getModule("file/NativeFileSystem").NativeFileSystem,
SpecRunnerUtils = brackets.getModule("spec/SpecRunnerUtils"),
UnitTestReporter = brackets.getModule("test/UnitTestReporter"),
JSCodeHints = require("main"),
Preferences = require("Preferences"),
ScopeManager = require("ScopeManager"),
HintUtils = require("HintUtils"),
FunctionHintManager = require("FunctionHintManager");
var Commands = brackets.getModule("command/Commands"),
CommandManager = brackets.getModule("command/CommandManager"),
DocumentManager = brackets.getModule("document/DocumentManager"),
Editor = brackets.getModule("editor/Editor").Editor,
EditorManager = brackets.getModule("editor/EditorManager"),
FileUtils = brackets.getModule("file/FileUtils"),
NativeFileSystem = brackets.getModule("file/NativeFileSystem").NativeFileSystem,
SpecRunnerUtils = brackets.getModule("spec/SpecRunnerUtils"),
UnitTestReporter = brackets.getModule("test/UnitTestReporter"),
JSCodeHints = require("main"),
Preferences = require("Preferences"),
ScopeManager = require("ScopeManager"),
HintUtils = require("HintUtils"),
ParameterHintManager = require("ParameterHintManager");
var extensionPath = FileUtils.getNativeModuleDirectoryPath(module),
testPath = extensionPath + "/unittest-files/basic-test-files/file1.js",
@@ -347,16 +347,16 @@ define(function (require, exports, module) {
* records, where each element of the array describes a function parameter.
* If null, then no hint is expected.
*/
function showAndVerifyFunctionHint(expectedHint)
function popUpAndVerifyParameterHint(expectedHint)
{
var request = FunctionHintManager.popUpFunctionHint();
var request = ParameterHintManager.popUpHint();
if (expectedHint === null) {
expect(request).toBe(null);
return;
}
request.done(function () {
var actualHint = JSCodeHints.getSession().getFunctionHint(),
var actualHint = JSCodeHints.getSession().getParameterHint(),
n = actualHint.length,
i;
@@ -772,7 +772,7 @@ define(function (require, exports, module) {
testDoc.replaceRange("funD(", start, start);
testEditor.setCursorPos(middle);
runs(function () {
showAndVerifyFunctionHint([{name: "a", type: "String"}, {name: "b", type: "number"}]);
popUpAndVerifyParameterHint([{name: "a", type: "String"}, {name: "b", type: "number"}]);
});
});
@@ -855,7 +855,7 @@ define(function (require, exports, module) {
var start = { line: 59, ch: 10 };
testEditor.setCursorPos(start);
runs(function () {
showAndVerifyFunctionHint([{name: "a4", type: "number"}, {name: "b4", type: "number"}]);
popUpAndVerifyParameterHint([{name: "a4", type: "number"}, {name: "b4", type: "number"}]);
});
});
@@ -875,7 +875,7 @@ define(function (require, exports, module) {
testDoc.replaceRange("myCustomer.setAmountDue(", start);
testEditor.setCursorPos(testPos);
runs(function () {
showAndVerifyFunctionHint([{name:"amountDue", type: "Object"}]);
popUpAndVerifyParameterHint([{name:"amountDue", type: "Object"}]);
});
});
@@ -884,7 +884,7 @@ define(function (require, exports, module) {
testEditor.setCursorPos(testPos);
runs(function () {
showAndVerifyFunctionHint([{name: "arg", type: "String"}]);
popUpAndVerifyParameterHint([{name: "arg", type: "String"}]);
});
});
@@ -894,7 +894,7 @@ define(function (require, exports, module) {
testEditor.setCursorPos(testPos);
var hintObj = expectHints(JSCodeHints.jsHintProvider);
runs(function () {
showAndVerifyFunctionHint([]);
popUpAndVerifyParameterHint([]);
});
});
@@ -918,7 +918,7 @@ define(function (require, exports, module) {
testEditor.setCursorPos(testPos);
runs(function () {
showAndVerifyFunctionHint([{name: "f", type: "function():number"}]);
popUpAndVerifyParameterHint([{name: "f", type: "function():number"}]);
});
});
@@ -928,7 +928,7 @@ define(function (require, exports, module) {
testEditor.setCursorPos(testPos);
runs(function () {
showAndVerifyFunctionHint([{name: "f", type: "function(String s, number n):String)"}]);
popUpAndVerifyParameterHint([{name: "f", type: "function(String s, number n):String)"}]);
});
});
@@ -949,7 +949,7 @@ define(function (require, exports, module) {
testDoc.replaceRange("funArr.index1(", start);
testEditor.setCursorPos(testPos);
runs(function () {
showAndVerifyFunctionHint([]);
popUpAndVerifyParameterHint([]);
});
});
@@ -1088,7 +1088,7 @@ define(function (require, exports, module) {
runs(function () {
testEditor.setCursorPos(func);
showAndVerifyFunctionHint(null);
popUpAndVerifyParameterHint(null);
testEditor.setCursorPos(param);
expectNoHints(JSCodeHints.jsHintProvider);
testEditor.setCursorPos(variable);
@@ -1141,7 +1141,7 @@ define(function (require, exports, module) {
testDoc.replaceRange("myCustomer.setAmountDue(10)", start);
testEditor.setCursorPos(testPos);
runs(function () {
showAndVerifyFunctionHint([{name: "amountDue", type: "number"}]);
popUpAndVerifyParameterHint([{name: "amountDue", type: "number"}]);
});
});
@@ -1174,7 +1174,7 @@ define(function (require, exports, module) {
testEditor.setCursorPos(start);
runs(function () {
showAndVerifyFunctionHint([{name: "a", type: "number"}]);
popUpAndVerifyParameterHint([{name: "a", type: "number"}]);
});
});
@@ -1183,7 +1183,7 @@ define(function (require, exports, module) {
testEditor.setCursorPos(start);
runs(function () {
showAndVerifyFunctionHint([{name: "a", type: "String"}, {name: "b", type: "Number"}]);
popUpAndVerifyParameterHint([{name: "a", type: "String"}, {name: "b", type: "Number"}]);
});
});
@@ -1192,7 +1192,7 @@ define(function (require, exports, module) {
testEditor.setCursorPos(start);
runs(function () {
showAndVerifyFunctionHint([{name: "paramE1", type: "D1"}, {name: "paramE2", type: "Number"}]);
popUpAndVerifyParameterHint([{name: "paramE1", type: "D1"}, {name: "paramE2", type: "Number"}]);
});
});