/* * 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 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 _ = brackets.getModule("thirdparty/lodash"); 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"), HintsUtils2 = require("HintUtils2"), ScopeManager = require("ScopeManager"), Session = require("Session"); /** @const {string} Show Function Hint command ID */ 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, hintContainerHTML = require("text!ParameterHintTemplate.html"), KeyboardPrefs = JSON.parse(require("text!keyboard.json")); var $hintContainer, // function hint container $hintContent, // function hint content holder /** @type {{inFunctionCall: boolean, functionCallPos: {line: number, ch: number}, * fnType: Array.") .append(_.escape(param)) .addClass("current-parameter")); } else { $hintContent.append(_.escape(param)); } } if (hints.parameters.length > 0) { HintsUtils2.formatParameterHint(hints.parameters, appendSeparators, appendParameter); } else { $hintContent.append(_.escape(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 pushHintOnStack() { hintStack.push(hintState); } /** * Restore the state of the previous function hint. * * @return {boolean} - true the a parameter hint has been popped, false otherwise. */ function popHintFromStack() { if (hintStack.length > 0) { hintState = hintStack.pop(); hintState.visible = false; return true; } return false; } /** * Reset the function hint stack. */ function clearFunctionHintStack() { hintStack = []; } /** * Test if the function call at the cursor is different from the currently displayed * function hint. * * @param {{line:number, ch:number}} functionCallPos - the offset of the function call. * @return {boolean} */ function hasFunctionCallPosChanged(functionCallPos) { var oldFunctionCallPos = hintState.functionCallPos; return (oldFunctionCallPos === undefined || oldFunctionCallPos.line !== functionCallPos.line || oldFunctionCallPos.ch !== functionCallPos.ch); } /** * Dismiss the function hint. * */ function dismissHint() { if (hintState.visible) { $hintContainer.hide(); $hintContent.empty(); hintState = {}; $(session.editor).off("cursorActivity", handleCursorActivity); if (!preserveHintStack) { 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 - * 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 there is no function hint at the cursor. * */ function popUpHint(pushExistingHint, hint, functionInfo) { functionInfo = functionInfo || session.getFunctionInfo(); if (!functionInfo.inFunctionCall) { dismissHint(); return null; } if (hasFunctionCallPosChanged(functionInfo.functionCallPos)) { var pushHint = pushExistingHint && isHintDisplayed(); if (pushHint) { pushHintOnStack(); preserveHintStack = true; } dismissHint(); preserveHintStack = false; } else if (isHintDisplayed()) { return null; } hintState.functionCallPos = functionInfo.functionCallPos; var request = null; var $deferredPopUp = $.Deferred(); if (!hint) { request = ScopeManager.requestParameterHint(session, functionInfo.functionCallPos); } else { session.setFnType(hint); request = $.Deferred(); request.resolveWith(null, [hint]); $deferredPopUp.resolveWith(null); } request.done(function (fnType) { var cm = session.editor._codeMirror, pos = cm.charCoords(functionInfo.functionCallPos); formatHint(functionInfo); $hintContainer.show(); positionHint(pos.left, pos.top, pos.bottom); hintState.visible = true; hintState.fnType = fnType; $(session.editor).on("cursorActivity", handleCursorActivity); $deferredPopUp.resolveWith(null); }).fail(function () { hintState = {}; }); return $deferredPopUp; } /** * Pop up a function hint on the line above the caret position if the character before * the current cursor is an open parenthesis * * @return {jQuery.Promise} - The promise will not complete until the * hint has completed. Returns null, if the function hint is already * displayed or there is no function hint at the cursor. */ function popUpHintAtOpenParen() { var functionInfo = session.getFunctionInfo(); if (functionInfo.inFunctionCall) { var token = session.getToken(); if (token && token.string === "(") { return popUpHint(); } } else { dismissHint(); } return null; } /** * Show the parameter the cursor is on in bold when the cursor moves. * Dismiss the pop up when the cursor moves off the function. */ handleCursorActivity = function () { var functionInfo = session.getFunctionInfo(); if (functionInfo.inFunctionCall) { // If in a different function hint, then dismiss the old one and // display the new one if there is one on the stack if (hasFunctionCallPosChanged(functionInfo.functionCallPos)) { if (popHintFromStack()) { var poppedFunctionCallPos = hintState.functionCallPos, currentFunctionCallPos = functionInfo.functionCallPos; if (poppedFunctionCallPos.line === currentFunctionCallPos.line && poppedFunctionCallPos.ch === currentFunctionCallPos.ch) { preserveHintStack = true; popUpHint(OVERWRITE_EXISTING_HINT, hintState.fnType, functionInfo); preserveHintStack = false; return; } } else { dismissHint(); } } formatHint(functionInfo); return; } dismissHint(); }; /** * Enable cursor tracking in the current session. * * @param {Session} session - session to start cursor tracking on. */ function startCursorTracking(session) { $(session.editor).on("cursorActivity", handleCursorActivity); } /** * 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", handleCursorActivity); } /** * Show a parameter hint in its own pop-up. * */ function handleShowParameterHint() { // Pop up function hint popUpHint(); } /** * Install function hint listeners. * * @param {Editor} editor - editor context on which to listen for * changes */ function installListeners(editor) { $(editor).on("keydown", function (jqEvent, editor, event) { if (event.keyCode === KeyEvent.DOM_VK_ESCAPE) { dismissHint(); } }).on("scroll", function () { dismissHint(); }); } /** * Add the function hint command at start up. */ function addCommands() { /* Register the command handler */ 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_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_PARAMETER_HINT_CMD_ID && commandId !== Commands.SHOW_CODE_HINTS) { dismissHint(); } }); } // Create the function hint container $hintContainer = $(hintContainerHTML).appendTo($("body")); $hintContent = $hintContainer.find(".function-hint-content"); exports.PUSH_EXISTING_HINT = PUSH_EXISTING_HINT; exports.addCommands = addCommands; exports.dismissHint = dismissHint; exports.installListeners = installListeners; exports.isHintDisplayed = isHintDisplayed; exports.popUpHint = popUpHint; exports.popUpHintAtOpenParen = popUpHintAtOpenParen; exports.setSession = setSession; exports.startCursorTracking = startCursorTracking; exports.stopCursorTracking = stopCursorTracking; });