mirror of
https://github.com/RustyRaptor/brackets.git
synced 2026-12-04 00:15:22 -07:00
Function Hints
This commit is contained in:
1 parent
c9ed878471
commit
d86ae75c30
12 files changed
+995
-138
No files matched your search
@@ -0,0 +1,406 @@
|
||||
/*
|
||||
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
/*jslint undef: true, vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true */
|
||||
/*global define, brackets, $ */
|
||||
|
||||
define(function (require, exports, module) {
|
||||
"use strict";
|
||||
|
||||
var Commands = brackets.getModule("command/Commands"),
|
||||
CommandManager = brackets.getModule("command/CommandManager"),
|
||||
KeyEvent = brackets.getModule("utils/KeyEvent"),
|
||||
Menus = brackets.getModule("command/Menus"),
|
||||
Strings = brackets.getModule("strings"),
|
||||
StringUtils = brackets.getModule("utils/StringUtils"),
|
||||
ScopeManager = require("ScopeManager"),
|
||||
Session = require("Session");
|
||||
|
||||
|
||||
/** @const {string} Show Function Hint command ID */
|
||||
var SHOW_FUNCTION_HINT_CMD_ID = "showFunctionHint", // 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"),
|
||||
KeyboardPrefs = JSON.parse(require("text!keyboard.json"));
|
||||
|
||||
var $functionHintContainer, // function hint container
|
||||
$functionHintContent, // 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
|
||||
session; // current editor session, updated by main
|
||||
|
||||
// Constants
|
||||
var
|
||||
POSITION_OFFSET = 38, // Distance between the bottom of the line and the bottom of the preview container
|
||||
POINTER_LEFT_OFFSET = 17, // Half of the pointer width, used to find the center of the pointer
|
||||
POINTER_TOP_OFFSET = 4, // Size of margin + border of hint.
|
||||
POSITION_BELOW_OFFSET = 16, // Amount to adjust to top position when the preview bubble is below the text
|
||||
POPOVER_HORZ_MARGIN = 5; // Horizontal margin
|
||||
|
||||
|
||||
/**
|
||||
* Update the current session for use by the Function Hint Manager.
|
||||
*
|
||||
* @param {Session} value - current session.
|
||||
*/
|
||||
function setSession(value) {
|
||||
session = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a function hint is being displayed.
|
||||
*
|
||||
* @returns {boolean} - true if a function hint is being displayed, false
|
||||
* otherwise.
|
||||
*/
|
||||
function isFunctionHintDisplayed() {
|
||||
return functionHintState.visible === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Position a function hint.
|
||||
*
|
||||
* @param {number} xpos
|
||||
* @param {number} ypos
|
||||
* @param {number} ybot
|
||||
*/
|
||||
function positionFunctionHint(xpos, ypos, ybot) {
|
||||
var hintWidth = $functionHintContainer.width(),
|
||||
hintHeight = $functionHintContainer.height(),
|
||||
top = ypos - hintHeight - POINTER_TOP_OFFSET,
|
||||
left = xpos,
|
||||
$editorHolder = $("#editor-holder"),
|
||||
editorLeft = $editorHolder.offset().left;
|
||||
|
||||
left = Math.max(left, editorLeft);
|
||||
left = Math.min(left, editorLeft + $editorHolder.width() - hintWidth);
|
||||
|
||||
if (top < 0) {
|
||||
$functionHintContainer.removeClass("preview-bubble-above");
|
||||
$functionHintContainer.addClass("preview-bubble-below");
|
||||
top = ybot + POSITION_BELOW_OFFSET;
|
||||
$functionHintContainer.offset({
|
||||
left: left,
|
||||
top: top
|
||||
});
|
||||
} else {
|
||||
$functionHintContainer.removeClass("preview-bubble-below");
|
||||
$functionHintContainer.addClass("preview-bubble-above");
|
||||
$functionHintContainer.offset({
|
||||
left: left,
|
||||
top: top - POINTER_TOP_OFFSET
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bold the parameter at the caret.
|
||||
*
|
||||
* @param {{inFunctionCall: boolean, functionCallPos: {{line: number, ch: number}}}
|
||||
* 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);
|
||||
|
||||
$functionHintContent.empty();
|
||||
$functionHintContent.addClass("brackets-js-hints");
|
||||
hints.parameters.forEach(function (value, i) {
|
||||
var param = value.type + " " + value.name,
|
||||
separator = "";
|
||||
|
||||
if (value.isOptional) {
|
||||
if (i > 0) {
|
||||
separator += " ";
|
||||
}
|
||||
|
||||
separator += "[";
|
||||
}
|
||||
|
||||
if (i > 0) {
|
||||
separator += ", ";
|
||||
}
|
||||
|
||||
if (separator) {
|
||||
$functionHintContent.append(separator);
|
||||
}
|
||||
|
||||
if (hints.currentIndex === i) {
|
||||
$functionHintContent.append($("<span>")
|
||||
.append(StringUtils.htmlEscape(param))
|
||||
.addClass("current-parameter"));
|
||||
} else {
|
||||
$functionHintContent.append(StringUtils.htmlEscape(param));
|
||||
}
|
||||
|
||||
if (value.isOptional) {
|
||||
$functionHintContent.append("]");
|
||||
}
|
||||
});
|
||||
|
||||
if (hints.parameters.length === 0) {
|
||||
$functionHintContent.append(StringUtils.htmlEscape(Strings.NO_ARGUMENTS));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the state of the current hint. Called when popping up a parameter hint
|
||||
* for a parameter, when the parameter already part of an existing parameter
|
||||
* hint.
|
||||
*/
|
||||
function pushFunctionHintOnStack() {
|
||||
functionHintStack.push(functionHintState);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore the state of the previous function hint.
|
||||
*
|
||||
* @returns {boolean} - true the a parameter hint has been pushed, false otherwise.
|
||||
*/
|
||||
function popFunctionHintFromStack() {
|
||||
if (functionHintStack.length > 0) {
|
||||
functionHintState = functionHintStack.pop();
|
||||
functionHintState.visible = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the function hint stack.
|
||||
*/
|
||||
function clearFunctionHintStack() {
|
||||
functionHintStack = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the function call at the cursor is different from the currently displayed
|
||||
* function hint.
|
||||
*
|
||||
* @param functionCallPos
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function hasFunctionCallPosChanged(functionCallPos) {
|
||||
var oldFunctionCallPos = functionHintState.functionCallPos;
|
||||
return (oldFunctionCallPos === undefined ||
|
||||
oldFunctionCallPos.line !== functionCallPos.line ||
|
||||
oldFunctionCallPos.ch !== functionCallPos.ch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dismiss the function hint.
|
||||
*
|
||||
*/
|
||||
function dismissFunctionHint() {
|
||||
|
||||
if (functionHintState.visible) {
|
||||
$functionHintContainer.hide();
|
||||
$functionHintContent.empty();
|
||||
functionHintState = {};
|
||||
$(session.editor).off("cursorActivity", handleFunctionHintCursorActivity);
|
||||
|
||||
if (!preserveFunctionHintStack) {
|
||||
clearFunctionHintStack();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pop up a function hint on the line above the caret position.
|
||||
*
|
||||
* @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 -
|
||||
*
|
||||
*/
|
||||
function popUpFunctionHint(pushExistingHint, hint, functionInfo) {
|
||||
|
||||
functionInfo = functionInfo || session.getFunctionInfo();
|
||||
if (!functionInfo.inFunctionCall) {
|
||||
dismissFunctionHint();
|
||||
return;
|
||||
}
|
||||
|
||||
if (hasFunctionCallPosChanged(functionInfo.functionCallPos)) {
|
||||
|
||||
var pushHint = pushExistingHint && isFunctionHintDisplayed();
|
||||
if (pushHint) {
|
||||
pushFunctionHintOnStack();
|
||||
preserveFunctionHintStack = true;
|
||||
}
|
||||
|
||||
dismissFunctionHint();
|
||||
preserveFunctionHintStack = false;
|
||||
} else if (isFunctionHintDisplayed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
functionHintState.functionCallPos = functionInfo.functionCallPos;
|
||||
|
||||
var request = null;
|
||||
if (!hint) {
|
||||
request = ScopeManager.requestFunctionHint(session, functionInfo.functionCallPos);
|
||||
} else {
|
||||
session.setFnType(hint);
|
||||
request = $.Deferred();
|
||||
request.resolveWith(null, [hint]);
|
||||
}
|
||||
|
||||
request.done(function (fnType) {
|
||||
var cm = session.editor._codeMirror;
|
||||
|
||||
var pos = cm.charCoords(functionInfo.functionCallPos);
|
||||
|
||||
formatFunctionHint(functionInfo);
|
||||
|
||||
$functionHintContainer.show();
|
||||
positionFunctionHint(pos.left, pos.top, pos.bottom);
|
||||
functionHintState.visible = true;
|
||||
functionHintState.fnType = fnType;
|
||||
|
||||
$(session.editor).on("cursorActivity", handleFunctionHintCursorActivity);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
var functionInfo = session.getFunctionInfo();
|
||||
|
||||
if (functionInfo.inFunctionCall) {
|
||||
// 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,
|
||||
currentFunctionCallPos = functionInfo.functionCallPos;
|
||||
|
||||
if (poppedFunctionCallPos.line === currentFunctionCallPos.line &&
|
||||
poppedFunctionCallPos.ch === currentFunctionCallPos.ch) {
|
||||
preserveFunctionHintStack = true;
|
||||
popUpFunctionHint(OVERWRITE_EXISTING_HINT,
|
||||
functionHintState.fnType, functionInfo);
|
||||
preserveFunctionHintStack = false;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
dismissFunctionHint();
|
||||
}
|
||||
}
|
||||
|
||||
formatFunctionHint(functionInfo);
|
||||
return;
|
||||
}
|
||||
|
||||
dismissFunctionHint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable cursor tracking in the current session.
|
||||
*
|
||||
* @param {Session} session - session to stop cursor tracking on.
|
||||
*/
|
||||
function startCursorTracking(session) {
|
||||
$(session.editor).on("cursorActivity", handleFunctionHintCursorActivity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop cursor tracking in the current session.
|
||||
*
|
||||
* Use this to move the cursor without changing the function hint state.
|
||||
*
|
||||
* @param {Session} session - session to stop cursor tracking on.
|
||||
*/
|
||||
function stopCursorTracking(session) {
|
||||
$(session.editor).off("cursorActivity", handleFunctionHintCursorActivity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a function hint in its own pop-up.
|
||||
*
|
||||
* @param commandData
|
||||
*/
|
||||
function handleShowFunctionHint(commandData) {
|
||||
|
||||
// Pop up function hint
|
||||
popUpFunctionHint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Install function hint listeners.
|
||||
*
|
||||
* @param {Editor} editor - editor context on which to listen for
|
||||
* changes
|
||||
*/
|
||||
function installFunctionHintListeners(editor) {
|
||||
|
||||
$(editor).on("keyEvent", function (jqEvent, editor, event) {
|
||||
if (event.type === "keydown" && event.keyCode === KeyEvent.DOM_VK_ESCAPE) {
|
||||
dismissFunctionHint();
|
||||
}
|
||||
}).on("scroll", function () {
|
||||
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.dismissFunctionHint = dismissFunctionHint;
|
||||
exports.installFunctionHintListeners = installFunctionHintListeners;
|
||||
exports.isFunctionHintDisplayed = isFunctionHintDisplayed;
|
||||
exports.popUpFunctionHint = popUpFunctionHint;
|
||||
exports.setSession = setSession;
|
||||
exports.startCursorTracking = startCursorTracking;
|
||||
exports.stopCursorTracking = stopCursorTracking;
|
||||
|
||||
});
|
||||
@@ -0,0 +1,4 @@
|
||||
<div id="function-hint-container">
|
||||
<div class="function-hint-content">
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,209 @@
|
||||
/*
|
||||
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
/*jslint undef: true, vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true */
|
||||
/*global define, brackets */
|
||||
|
||||
define(function (require, exports, module) {
|
||||
"use strict";
|
||||
|
||||
var ExtensionUtils = brackets.getModule("utils/ExtensionUtils");
|
||||
|
||||
var Infer,
|
||||
cx,
|
||||
thirdPartyPath = ExtensionUtils.getModulePath(module, "thirdparty");
|
||||
|
||||
brackets.libRequire.config({baseUrl: thirdPartyPath})(["tern/lib/infer"], function (infer) {
|
||||
Infer = infer;
|
||||
|
||||
cx = new infer.Context([], null);
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {!Array.<name: {string}, type: {string},
|
||||
* isOptional: {boolean}>} params - array of parameter descriptors
|
||||
* @returns {string} parameters formatted as a string
|
||||
*/
|
||||
function formatParams(params) {
|
||||
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;
|
||||
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}
|
||||
*
|
||||
*/
|
||||
function inferTypeToString(inferType) {
|
||||
var result;
|
||||
|
||||
if (inferType instanceof infer.Prim) {
|
||||
result = inferType.toString();
|
||||
} else if (inferType instanceof infer.Arr) {
|
||||
result = inferArrTypeToString(inferType);
|
||||
} 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);
|
||||
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.
|
||||
*/
|
||||
function processInferFnTypeParameters(inferType) {
|
||||
var params = [],
|
||||
i;
|
||||
|
||||
for (i = 0; i < inferType.args.length; i++) {
|
||||
var param = {},
|
||||
name = inferType.argNames[i],
|
||||
type = inferType.args[i];
|
||||
|
||||
if (name[name.length - 1] === "?") {
|
||||
name = name.substring(0, name.length - 1);
|
||||
param.isOptional = true;
|
||||
}
|
||||
|
||||
param.name = name;
|
||||
|
||||
if (type instanceof Infer.Fn) {
|
||||
param.type = inferFnTypeToString(type);
|
||||
} else {
|
||||
param.type = inferTypeToString(type);
|
||||
}
|
||||
|
||||
params.push(param);
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
return params;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
exports.getParameters = getParameters;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
@@ -613,7 +613,13 @@ define(function (require, exports, module) {
|
||||
* @returns {{line: number, ch: number}}
|
||||
*/
|
||||
function getOffset(session, fileInfo, offset) {
|
||||
var newOffset = offset || session.getCursor();
|
||||
var newOffset;
|
||||
|
||||
if (offset) {
|
||||
newOffset = {line: offset.line, ch: offset.ch};
|
||||
} else {
|
||||
newOffset = session.getCursor();
|
||||
}
|
||||
|
||||
if (fileInfo.type === MessageIds.TERN_FILE_INFO_TYPE_PART) {
|
||||
newOffset.line = Math.max(0, newOffset.line - fileInfo.offsetLines);
|
||||
@@ -1267,6 +1273,32 @@ define(function (require, exports, module) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Request a function type hint from Tern.
|
||||
*
|
||||
* @param {Session} session - the active hinting session
|
||||
* @param {{line: number, ch: number}} functionOffset - the offset the function call.
|
||||
* @return {jQuery.Promise} - The promise will not complete until the
|
||||
* hint has completed.
|
||||
*/
|
||||
function requestFunctionHint(session, functionOffset) {
|
||||
var $deferredHints = $.Deferred(),
|
||||
fileInfo = getFileInfo(session),
|
||||
offset = getOffset(session, fileInfo, functionOffset),
|
||||
fnTypePromise = getTernFunctionType(fileInfo, offset);
|
||||
|
||||
$.when(fnTypePromise).done(
|
||||
function (fnType) {
|
||||
session.setFnType(fnType);
|
||||
$deferredHints.resolveWith(null, [fnType]);
|
||||
}
|
||||
).fail(function () {
|
||||
$deferredHints.reject();
|
||||
});
|
||||
|
||||
return $deferredHints.promise();
|
||||
}
|
||||
|
||||
/**
|
||||
* Request hints from Tern.
|
||||
*
|
||||
@@ -1284,26 +1316,15 @@ define(function (require, exports, module) {
|
||||
function requestHints(session, document) {
|
||||
var $deferredHints = $.Deferred(),
|
||||
hintPromise,
|
||||
fnTypePromise,
|
||||
sessionType = session.getType(),
|
||||
fileInfo = getFileInfo(session),
|
||||
offset = getOffset(session, fileInfo,
|
||||
sessionType.showFunctionType ? sessionType.functionCallPos : null);
|
||||
offset = getOffset(session, fileInfo, null);
|
||||
|
||||
maybeReset(session, document);
|
||||
|
||||
hintPromise = getTernHints(fileInfo, offset, sessionType.property);
|
||||
|
||||
if (sessionType.showFunctionType) {
|
||||
// Show function sig
|
||||
fnTypePromise = getTernFunctionType(fileInfo, offset);
|
||||
} else {
|
||||
var $fnTypeDeferred = $.Deferred();
|
||||
fnTypePromise = $fnTypeDeferred.promise();
|
||||
$fnTypeDeferred.resolveWith(null);
|
||||
}
|
||||
|
||||
$.when(hintPromise, fnTypePromise).done(
|
||||
$.when(hintPromise).done(
|
||||
function (completions, fnType) {
|
||||
if (completions.completions) {
|
||||
session.setTernHints(completions.completions);
|
||||
@@ -1313,7 +1334,6 @@ define(function (require, exports, module) {
|
||||
session.setGuesses(completions.properties);
|
||||
}
|
||||
|
||||
session.setFnType(fnType);
|
||||
$deferredHints.resolveWith(null);
|
||||
}
|
||||
).fail(function () {
|
||||
@@ -1408,6 +1428,7 @@ define(function (require, exports, module) {
|
||||
exports.handleFileChange = handleFileChange;
|
||||
exports.requestHints = requestHints;
|
||||
exports.requestJumptoDef = requestJumptoDef;
|
||||
exports.requestFunctionHint = requestFunctionHint;
|
||||
exports.handleProjectClose = handleProjectClose;
|
||||
exports.handleProjectOpen = handleProjectOpen;
|
||||
|
||||
|
||||
@@ -32,7 +32,10 @@ define(function (require, exports, module) {
|
||||
HTMLUtils = brackets.getModule("language/HTMLUtils"),
|
||||
TokenUtils = brackets.getModule("utils/TokenUtils"),
|
||||
HintUtils = require("HintUtils"),
|
||||
ScopeManager = require("ScopeManager");
|
||||
ScopeManager = require("ScopeManager"),
|
||||
InferUtils = require("InferUtils"),
|
||||
Acorn = require("thirdparty/acorn/acorn"),
|
||||
Acorn_Loose = require("thirdparty/acorn/acorn_loose");
|
||||
|
||||
/**
|
||||
* Session objects encapsulate state associated with a hinting session
|
||||
@@ -271,7 +274,7 @@ define(function (require, exports, module) {
|
||||
*
|
||||
* @param {{line: number, ch: number}} cursor - the cursor position
|
||||
* at which context information is to be retrieved
|
||||
* @param {number} depth - the current depth of the parenthesis stack, or
|
||||
* @param {number=} depth - the current depth of the parenthesis stack, or
|
||||
* undefined if the depth is 0.
|
||||
* @return {string} - the context for the property that was looked up
|
||||
*/
|
||||
@@ -319,42 +322,151 @@ define(function (require, exports, module) {
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {{line: number, ch: number}} cursor - the cursor position
|
||||
* at which to retrieve a token
|
||||
* @returns {*} - the lexical state of the token
|
||||
*/
|
||||
function getLexicalState(token) {
|
||||
if (token.state.lexical) {
|
||||
// in a javascript file this is just in the state field
|
||||
return token.state.lexical;
|
||||
} else if (token.state.localState && token.state.localState.lexical) {
|
||||
// inline javascript in an html file will have this in
|
||||
// the localState field
|
||||
return token.state.localState.lexical;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the caret is either within a function call or on the function call itself.
|
||||
*
|
||||
* @returns {{inFunctionCall: boolean, functionCallPos: {{line: number, ch: number}}}
|
||||
* inFunctionCall - true if the caret if either within a function call or on the
|
||||
* function call itself.
|
||||
* functionCallPos - the offset of the '(' character of the function call.
|
||||
*/
|
||||
Session.prototype.getFunctionInfo = function () {
|
||||
var inFunctionCall = false,
|
||||
cursor = this.getCursor(),
|
||||
functionCallPos,
|
||||
token = this.getToken(cursor),
|
||||
lexical,
|
||||
self = this,
|
||||
foundCall = false;
|
||||
|
||||
/**
|
||||
* Test if the cursor is on a function identifier
|
||||
*
|
||||
* @return {Object} - lexical state if on a function identifier, null otherwise.
|
||||
*/
|
||||
function isOnFunctionIdentifier() {
|
||||
|
||||
// Check if we might be on function identifier of the function call.
|
||||
var type = token.type,
|
||||
nextToken,
|
||||
localLexical,
|
||||
localCursor = {line: cursor.line, ch: token.end};
|
||||
|
||||
if (type === "variable-2" || type === "variable" || type === "property") {
|
||||
nextToken = self.getNextToken(localCursor, true);
|
||||
if (nextToken.string === "(") {
|
||||
//nextToken = self.getNextToken(localCursor, true);
|
||||
localLexical = getLexicalState(nextToken);
|
||||
return localLexical;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is a lexical state is in a function call.
|
||||
*
|
||||
* @param {Object} lex - lexical state.
|
||||
* @return {Object | boolean}
|
||||
*
|
||||
*/
|
||||
function isInFunctionalCall(lex) {
|
||||
// in a call, or inside array or object brackets that are inside a function.
|
||||
return (lex && (lex.info === "call" ||
|
||||
(lex.info === undefined && (lex.type === "]" || lex.type === "}") &&
|
||||
lex.prev.info === "call")));
|
||||
}
|
||||
|
||||
if (token) {
|
||||
// if this token is part of a function call, then the tokens lexical info
|
||||
// will be annotated with "call".
|
||||
// If the cursor is inside an array, "[]", or object, "{}", the lexical state
|
||||
// will be undefined, not "call". lexical.prev will be the function state.
|
||||
// Handle this case and then set "lexical" to lexical.prev.
|
||||
// Also test if the cursor is on a function identifier of a function call.
|
||||
lexical = getLexicalState(token);
|
||||
foundCall = isInFunctionalCall(lexical);
|
||||
|
||||
if (!foundCall) {
|
||||
lexical = isOnFunctionIdentifier();
|
||||
foundCall = isInFunctionalCall(lexical);
|
||||
}
|
||||
|
||||
if (foundCall) {
|
||||
inFunctionCall = true;
|
||||
// we need to find the location of the called function so that we can request the functions type.
|
||||
// the token's lexical info will contain the column where the open "(" for the
|
||||
// function call occurs, but for whatever reason it does not have the line, so
|
||||
// we have to walk back and try to find the correct location. We do this by walking
|
||||
// up the lines starting with the line the token is on, and seeing if any of the lines
|
||||
// have "(" at the column indicated by the tokens lexical state.
|
||||
// We walk back 9 lines, as that should be far enough to find most function calls,
|
||||
// and it will prevent us from walking back thousands of lines if something went wrong.
|
||||
// there is nothing magical about 9 lines, and it can be adjusted if it doesn't seem to be
|
||||
// working well
|
||||
if (lexical.info === undefined) {
|
||||
lexical = lexical.prev;
|
||||
}
|
||||
|
||||
var col = lexical.info === "call" ? lexical.column : lexical.prev,
|
||||
line,
|
||||
e,
|
||||
found;
|
||||
for (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};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
inFunctionCall: inFunctionCall,
|
||||
functionCallPos: functionCallPos
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* 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,
|
||||
showFunctionType:boolean,
|
||||
context: string,
|
||||
functionCallPos: {line:number, ch:number}}} - an Object consisting
|
||||
context: string} - 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 () {
|
||||
function getLexicalState(token) {
|
||||
if (token.state.lexical) {
|
||||
// in a javascript file this is just in the state field
|
||||
return token.state.lexical;
|
||||
} else if (token.state.localState && token.state.localState.lexical) {
|
||||
// inline javascript in an html file will have this in
|
||||
// the localState field
|
||||
return token.state.localState.lexical;
|
||||
}
|
||||
}
|
||||
var propertyLookup = false,
|
||||
inFunctionCall = false,
|
||||
showFunctionType = false,
|
||||
context = null,
|
||||
cursor = this.getCursor(),
|
||||
functionCallPos,
|
||||
token = this.getToken(cursor),
|
||||
lexical;
|
||||
|
||||
@@ -362,38 +474,6 @@ define(function (require, exports, module) {
|
||||
// if this token is part of a function call, then the tokens lexical info
|
||||
// will be annotated with "call"
|
||||
lexical = getLexicalState(token);
|
||||
if (lexical.info === "call") {
|
||||
inFunctionCall = true;
|
||||
if (this.getQuery().length > 0) {
|
||||
inFunctionCall = false;
|
||||
showFunctionType = false;
|
||||
} else {
|
||||
showFunctionType = true;
|
||||
// we need to find the location of the called function so that we can request the functions type.
|
||||
// the token's lexical info will contain the column where the open "(" for the
|
||||
// function call occurrs, but for whatever reason it does not have the line, so
|
||||
// we have to walk back and try to find the correct location. We do this by walking
|
||||
// up the lines starting with the line the token is on, and seeing if any of the lines
|
||||
// have "(" at the column indicated by the tokens lexical state.
|
||||
// We walk back 9 lines, as that should be far enough to find most function calls,
|
||||
// and it will prevent us from walking back thousands of lines if something went wrong.
|
||||
// there is nothing magical about 9 lines, and it can be adjusted if it doesn't seem to be
|
||||
// working well
|
||||
var col = lexical.column,
|
||||
line,
|
||||
e,
|
||||
found;
|
||||
for (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.type === "property") {
|
||||
propertyLookup = true;
|
||||
}
|
||||
@@ -403,14 +483,10 @@ define(function (require, exports, module) {
|
||||
propertyLookup = true;
|
||||
context = this.getContext(cursor);
|
||||
}
|
||||
if (propertyLookup) { showFunctionType = false; }
|
||||
}
|
||||
|
||||
return {
|
||||
property: propertyLookup,
|
||||
inFunctionCall: inFunctionCall,
|
||||
showFunctionType: showFunctionType,
|
||||
functionCallPos: functionCallPos,
|
||||
context: context
|
||||
};
|
||||
};
|
||||
@@ -483,6 +559,7 @@ define(function (require, exports, module) {
|
||||
if (searchResult) {
|
||||
searchResult.value = hint.value;
|
||||
searchResult.guess = hint.guess;
|
||||
searchResult.type = hint.type;
|
||||
|
||||
if (hint.keyword !== undefined) {
|
||||
searchResult.keyword = hint.keyword;
|
||||
@@ -524,8 +601,6 @@ define(function (require, exports, module) {
|
||||
}
|
||||
|
||||
StringMatch.multiFieldSort(hints, [ "matchGoodness", penalizeUnderscoreValueCompare ]);
|
||||
} else if (type.showFunctionType) {
|
||||
hints = this.getFunctionTypeHint();
|
||||
} else { // identifiers, literals, and keywords
|
||||
hints = this.ternHints || [];
|
||||
hints = hints.concat(HintUtils.LITERALS);
|
||||
@@ -549,39 +624,104 @@ define(function (require, exports, module) {
|
||||
this.ternGuesses = newGuesses;
|
||||
};
|
||||
|
||||
Session.prototype.setFnType = function (newFnType) {
|
||||
this.fnType = newFnType;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the function type hint. This will format the hint so
|
||||
* that it has the called variable name instead of just "fn()".
|
||||
* Set a new function type hint.
|
||||
*
|
||||
* @param {string} newFnType - the new tern function hint.
|
||||
*/
|
||||
Session.prototype.getFunctionTypeHint = function () {
|
||||
Session.prototype.setFnType = function (newFnType) {
|
||||
this.fnType = InferUtils.getParameters(newFnType);
|
||||
};
|
||||
|
||||
/**
|
||||
* 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.getFunctionHint = function (functionCallPos) {
|
||||
var fnHint = this.fnType,
|
||||
hints = [];
|
||||
|
||||
if (fnHint && (fnHint.substring(0, 3) === "fn(")) {
|
||||
var sessionType = this.getType(),
|
||||
cursor = sessionType.functionCallPos,
|
||||
token = cursor ? this.getToken(cursor) : undefined,
|
||||
varName;
|
||||
if (token &&
|
||||
// only change the 'fn' when the token looks like a function
|
||||
// name, and isn't some other kind of expression
|
||||
(token.type === "variable" ||
|
||||
token.type === "variable-2" ||
|
||||
token.type === "property")) {
|
||||
varName = token.string;
|
||||
if (varName) {
|
||||
fnHint = varName + fnHint.substr(2);
|
||||
cursor = this.getCursor(),
|
||||
token = this.getToken(functionCallPos),
|
||||
start = {line: functionCallPos.line, ch: token.start},
|
||||
fragment = this.editor.document.getRange(start,
|
||||
{line: functionCallPos.line + 10, ch: 0});
|
||||
|
||||
var ast;
|
||||
try {
|
||||
ast = Acorn.parse(fragment);
|
||||
} catch (e) { ast = Acorn_Loose.parse_dammit(fragment); }
|
||||
|
||||
// find argument as cursor location and bold it.
|
||||
var startOffset = this.getOffsetFromCursor(start),
|
||||
cursorOffset = this.getOffsetFromCursor(cursor),
|
||||
offset = cursorOffset - startOffset,
|
||||
node = ast.body[0],
|
||||
currentArg = -1;
|
||||
|
||||
if (node.type === "ExpressionStatement") {
|
||||
node = node.expression;
|
||||
if (node.type === "SequenceExpression") {
|
||||
node = node.expressions[0];
|
||||
}
|
||||
if (node.type === "CallExpression") {
|
||||
var args = node["arguments"],
|
||||
i,
|
||||
n = args.length,
|
||||
lastEnd = offset,
|
||||
text;
|
||||
for (i = 0; i < n; i++) {
|
||||
node = args[i];
|
||||
if (offset >= node.start && offset <= node.end) {
|
||||
currentArg = i;
|
||||
break;
|
||||
} else if (offset < node.start) {
|
||||
// The range of nodes can be disjoint so see i f we
|
||||
// passed the node. If we passed the node look at the
|
||||
// text between the nodes to figure out which
|
||||
// arg we are on.
|
||||
text = fragment.substring(lastEnd, node.start);
|
||||
|
||||
// test if comma is before or after the offset
|
||||
if (text.indexOf(",") >= (offset - lastEnd)) {
|
||||
// comma is after the offset so the current arg is the
|
||||
// previous arg node.
|
||||
i--;
|
||||
} else if (i === 0 && text.indexOf("(") !== -1) {
|
||||
// the cursor is on the function identifier
|
||||
currentArg = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
currentArg = Math.max(0, i);
|
||||
break;
|
||||
} else if (i + 1 === n) {
|
||||
// look for a comma after the node.end. This will tell us we
|
||||
// are on the next argument, even there is no text, and therefore no node,
|
||||
// for the next argument.
|
||||
text = fragment.substring(node.end, offset);
|
||||
if (text.indexOf(",") !== -1) {
|
||||
currentArg = i + 1; // we know we are after the current arg, but keep looking
|
||||
}
|
||||
}
|
||||
|
||||
lastEnd = node.end;
|
||||
}
|
||||
|
||||
// if there are no args, then figure out if we are on the function identifier
|
||||
if (n === 0 && cursorOffset > this.getOffsetFromCursor(functionCallPos)) {
|
||||
currentArg = 0;
|
||||
}
|
||||
}
|
||||
hints[0] = {value: fnHint, positions: []};
|
||||
}
|
||||
|
||||
hints.handleWideResults = true;
|
||||
return hints;
|
||||
|
||||
return {parameters: fnHint, currentIndex: currentArg};
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -630,12 +770,13 @@ define(function (require, exports, module) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Deterimine if the cursor is located in the name of a function declaration.
|
||||
* This is so we can suppress hints when in a funtion name, as we do for variable and
|
||||
* Determine if the cursor is located in the name of a function declaration.
|
||||
* This is so we can suppress hints when in a function name, as we do for variable and
|
||||
* parameter declarations, but we can tell those from the token itself rather than having
|
||||
* to look at previous tokens.
|
||||
*
|
||||
* @return {boolean} - true if the current cursor position is in the name of a function decl.
|
||||
* @return {boolean} - true if the current cursor position is in the name of a function
|
||||
* declaration.
|
||||
*/
|
||||
Session.prototype.isFunctionName = function () {
|
||||
var cursor = this.getCursor(),
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"showFunctionHint": [
|
||||
{
|
||||
"key": "Ctrl-Shift-Space"
|
||||
},
|
||||
{
|
||||
"key": "Ctrl-Shift-Space",
|
||||
"platform": "mac"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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"),
|
||||
Strings = brackets.getModule("strings"),
|
||||
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"),
|
||||
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"),
|
||||
FunctionHintManager = require("FunctionHintManager"),
|
||||
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
|
||||
@@ -467,14 +467,9 @@ define(function (require, exports, module) {
|
||||
query = session.getQuery(),
|
||||
start = {line: cursor.line, ch: cursor.ch - query.length},
|
||||
end = {line: cursor.line, ch: cursor.ch},
|
||||
delimiter;
|
||||
invalidPropertyName = false,
|
||||
displayFunctionHint = false;
|
||||
|
||||
if (session.getType().showFunctionType) {
|
||||
// function types show up as hints, so don't insert anything
|
||||
// if we were displaying a function type
|
||||
return false;
|
||||
}
|
||||
|
||||
if (session.getType().property) {
|
||||
// if we're inserting a property name, we need to make sure the
|
||||
// hint is a valid property name.
|
||||
@@ -482,9 +477,8 @@ define(function (require, exports, module) {
|
||||
// it should result in one token, and that token should either be
|
||||
// a 'name' or a 'keyword', as javascript allows keywords as property names
|
||||
var tokenizer = Acorn.tokenize(completion);
|
||||
var currentToken = tokenizer(),
|
||||
invalidPropertyName = false;
|
||||
|
||||
var currentToken = tokenizer();
|
||||
|
||||
// the name is invalid if the hint is not a 'name' or 'keyword' token
|
||||
if (currentToken.type !== Acorn.tokTypes.name && !currentToken.type.keyword) {
|
||||
invalidPropertyName = true;
|
||||
@@ -508,6 +502,14 @@ define(function (require, exports, module) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If the completion is for a valid function, then append
|
||||
// "()" to the function name.
|
||||
if (!invalidPropertyName && /^fn\(/.test(hint.type)) {
|
||||
completion = completion.concat("()");
|
||||
displayFunctionHint = true;
|
||||
}
|
||||
|
||||
// Replace the current token with the completion
|
||||
// HACK (tracking adobe/brackets#1688): We talk to the private CodeMirror instance
|
||||
// directly to replace the range instead of using the Document, as we should. The
|
||||
@@ -515,11 +517,31 @@ define(function (require, exports, module) {
|
||||
// inline editors are open.
|
||||
session.editor._codeMirror.replaceRange(completion, start, end);
|
||||
|
||||
// If displaying a function hint, move the cursor inside the "()".
|
||||
// Then pop-up a function hint.
|
||||
if (displayFunctionHint) {
|
||||
var pos = {line: start.line, ch: start.ch + completion.length - 1};
|
||||
|
||||
// stop cursor tracking before setting the cursor to avoid bringing
|
||||
// down the current hint.
|
||||
if (FunctionHintManager.isFunctionHintDisplayed()) {
|
||||
FunctionHintManager.stopCursorTracking(session);
|
||||
}
|
||||
|
||||
session.editor._codeMirror.setCursor(pos);
|
||||
|
||||
if (FunctionHintManager.isFunctionHintDisplayed()) {
|
||||
FunctionHintManager.startCursorTracking(session);
|
||||
}
|
||||
|
||||
FunctionHintManager.popUpFunctionHint(FunctionHintManager.PUSH_EXISTING_HINT, hint.type);
|
||||
}
|
||||
|
||||
// Return false to indicate that another hinting session is not needed
|
||||
return false;
|
||||
};
|
||||
|
||||
// load the extension
|
||||
// load the extension
|
||||
AppInit.appReady(function () {
|
||||
|
||||
/*
|
||||
@@ -533,6 +555,7 @@ define(function (require, exports, module) {
|
||||
session = new Session(editor);
|
||||
ScopeManager.handleEditorChange(session, editor.document,
|
||||
previousEditor ? previousEditor.document : null);
|
||||
FunctionHintManager.setSession(session);
|
||||
cachedHints = null;
|
||||
}
|
||||
|
||||
@@ -556,6 +579,8 @@ define(function (require, exports, module) {
|
||||
}
|
||||
ignoreChange = false;
|
||||
});
|
||||
|
||||
FunctionHintManager.installFunctionHintListeners(editor);
|
||||
} else {
|
||||
session = null;
|
||||
}
|
||||
|
||||
@@ -65,4 +65,26 @@
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
#function-hint-container {
|
||||
display: none;
|
||||
|
||||
background: #e0e1e3;
|
||||
position: absolute;
|
||||
z-index: 15;
|
||||
left: 200px;
|
||||
top: 40px;
|
||||
pointer-events: none;
|
||||
|
||||
padding: 1px;
|
||||
text-align: center;
|
||||
|
||||
border-radius: 2px;
|
||||
border: 1px solid #a6a8a8;
|
||||
}
|
||||
|
||||
#function-hint-container .function-hint-content {
|
||||
}
|
||||
|
||||
.brackets-js-hints .current-parameter {
|
||||
font-weight: 500;
|
||||
}
|
||||
@@ -146,6 +146,7 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
query.depths = true;
|
||||
query.guess = true;
|
||||
query.origins = true;
|
||||
query.types = true;
|
||||
query.expandWordForward = false;
|
||||
query.lineCharPositions = true;
|
||||
|
||||
@@ -233,7 +234,7 @@ importScripts("thirdparty/requirejs/require.js");
|
||||
//_log("tern properties: completions = " + data.completions.length);
|
||||
for (i = 0; i < data.completions.length; ++i) {
|
||||
var property = data.completions[i];
|
||||
properties.push({value: property, guess: true});
|
||||
properties.push({value: property, type: property.type, guess: true});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -160,6 +160,21 @@ require(["MyModule"], function (myModule) {
|
||||
var x = myModule.c;
|
||||
});
|
||||
|
||||
// Function hint testing
|
||||
function functionHintTesting() {
|
||||
// simple function with primative arguments
|
||||
s.lastIndexOf(s);
|
||||
|
||||
// function with a function argument
|
||||
arr.sort();
|
||||
|
||||
// function with array argument
|
||||
arr.concat();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* Add large comment to make this test over 250 lines which will trigger
|
||||
* partial updates to be used.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user