forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParameterHintsManager.js
More file actions
1 lines (1 loc) · 6.26 KB
/
ParameterHintsManager.js
File metadata and controls
1 lines (1 loc) · 6.26 KB
1
define(function(require,exports,module){let _=require("thirdparty/lodash"),Commands=require("command/Commands"),AppInit=require("utils/AppInit"),CommandManager=require("command/CommandManager"),EditorManager=require("editor/EditorManager"),Menus=require("command/Menus"),Strings=require("strings"),WorkspaceManager=require("view/WorkspaceManager"),ProviderRegistrationHandler=require("features/PriorityBasedRegistration").RegistrationHandler,SHOW_PARAMETER_HINT_CMD_ID="showParameterHint",hintContainerHTML=require("text!htmlContent/parameter-hint-template.html"),KeyboardPrefs_showParameterHint=[{key:"Ctrl-Shift-Space"},{key:"Ctrl-Shift-Space",platform:"mac"}],$hintContainer,$hintContent,hintState={},lastChar=null,sessionEditor=null,keyDownEditor=null,POINTER_TOP_OFFSET=4,POSITION_BELOW_OFFSET=4,handleCursorActivity,popupShown=!1,_providerRegistrationHandler=new ProviderRegistrationHandler,registerHintProvider=_providerRegistrationHandler.registerProvider.bind(_providerRegistrationHandler),removeHintProvider=_providerRegistrationHandler.removeProvider.bind(_providerRegistrationHandler);function positionHint(xpos,ypos,ybot){let hintWidth=$hintContainer.width(),hintHeight,top=ypos-$hintContainer.height()-POINTER_TOP_OFFSET,left=xpos,$editorHolder=$("#editor-holder"),editorLeft;void 0!==$editorHolder.offset()&&(editorLeft=$editorHolder.offset().left,left=Math.max(left,editorLeft),left=Math.min(left,editorLeft+$editorHolder.width()-hintWidth),top<0?($hintContainer.removeClass("preview-bubble-above"),$hintContainer.addClass("preview-bubble-below"),top=ybot+POSITION_BELOW_OFFSET,$hintContainer.offset({left:left,top:top})):($hintContainer.removeClass("preview-bubble-below"),$hintContainer.addClass("preview-bubble-above"),$hintContainer.offset({left:left,top:top-POINTER_TOP_OFFSET})))}function _formatParameterHint(functionName,params,appendSeparators,appendParameter,typesOnly){let result="",pendingOptional=!1;return appendParameter(`${functionName}(`,"",-1),params.forEach(function(value,i){let param=value.label||value.type,documentation=value.documentation,separators="";value.isOptional&&(pendingOptional&&(separators+="]"),pendingOptional=!0),i>0&&(separators+=", "),value.isOptional&&(separators+="["),appendSeparators&&appendSeparators(separators),result+=separators,!typesOnly&&value.name&&(param+=" "+value.name),appendParameter&&appendParameter(param,documentation,i),result+=param}),pendingOptional&&(appendSeparators&&appendSeparators("]"),result+="]"),appendParameter(")","",-1),result}function _formatHint(editor,hints){function appendSeparators(separators){$hintContent.append(separators)}function appendParameter(param,documentation,index){hints.currentIndex===index?$hintContent.append($("<span>").append(_.escape(param)).addClass("current-parameter")):$hintContent.append($("<span>").append(_.escape(param)).addClass("parameter"))}if($hintContent.empty(),$hintContent.addClass("brackets-hints"),hints.parameters.length>0){let token;_formatParameterHint(editor.getToken(hints.functionCallPos).string,hints.parameters,appendSeparators,appendParameter)}else $hintContent.append(_.escape(Strings.NO_ARGUMENTS))}function dismissHint(editor){popupShown=!1,hintState.visible&&($hintContainer.hide(),$hintContent.empty(),hintState={},editor?(editor.off("cursorActivity.ParameterHinting",handleCursorActivity),sessionEditor=null):sessionEditor&&(sessionEditor.off("cursorActivity.ParameterHinting",handleCursorActivity),sessionEditor=null))}function popUpHint(editor,explicit,onCursorActivity){let request=null,$deferredPopUp=$.Deferred(),sessionProvider=null;dismissHint(editor),popupShown=!0;let language=editor.getLanguageForSelection(),enabledProviders;return _providerRegistrationHandler.getProvidersForLanguageId(language.getId()).some(function(item,index){if(item.provider.hasParameterHints(editor,lastChar))return sessionProvider=item.provider,!0}),sessionProvider&&(request=sessionProvider.getParameterHints(explicit,onCursorActivity)),request&&request.done(function(parameterHint){let cm=editor._codeMirror,pos=parameterHint.functionCallPos||editor.getCursorPos();pos=cm.charCoords(pos),_formatHint(editor,parameterHint),$hintContainer.show(),positionHint(pos.left,pos.top,pos.bottom),hintState.visible=!0,sessionEditor=editor,editor.on("cursorActivity.ParameterHinting",handleCursorActivity),$deferredPopUp.resolveWith(null)}).fail(function(){hintState={}}),$deferredPopUp}function installListeners(editor){editor.on("scroll.ParameterHinting",function(){dismissHint(editor)}).on("editorChange.ParameterHinting",_handleChange).on("keypress.ParameterHinting",_handleKeypressEvent)}function uninstallListeners(editor){editor.off(".ParameterHinting")}function _handleKeypressEvent(jqEvent,editor,event){keyDownEditor=editor,lastChar=String.fromCharCode(event.charCode)}function _handleChange(event,editor,changeList){!lastChar||"("!==lastChar&&","!==lastChar||editor!==keyDownEditor||(keyDownEditor=null,popUpHint(editor))}function activeEditorChangeHandler(event,current,previous){previous&&(previous.document.off("languageChanged.ParameterHinting"),uninstallListeners(previous)),current&&(current.document.on("languageChanged.ParameterHinting",function(){uninstallListeners(current),installListeners(current)}),installListeners(current))}function handleShowParameterHint(){let editor;popUpHint(EditorManager.getActiveEditor(),!0,!1)}function _handleEscapeKeyEvent(){return!!popupShown&&(dismissHint(),!0)}handleCursorActivity=function(event,editor){editor?popUpHint(editor,!1,!0):dismissHint()},AppInit.appReady(function(){CommandManager.register(Strings.CMD_SHOW_PARAMETER_HINT,"showParameterHint",handleShowParameterHint);let menu=Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);menu&&menu.addMenuItem("showParameterHint",KeyboardPrefs_showParameterHint,Menus.AFTER,Commands.SHOW_CODE_HINTS),$hintContainer=$(hintContainerHTML).appendTo($("body")),$hintContent=$hintContainer.find(".function-hint-content-new"),activeEditorChangeHandler(null,EditorManager.getActiveEditor(),null),EditorManager.on("activeEditorChange",activeEditorChangeHandler),CommandManager.on("beforeExecuteCommand",function(_event,commandId){"showParameterHint"!==commandId&&commandId!==Commands.SHOW_CODE_HINTS&&dismissHint()}),WorkspaceManager.addEscapeKeyEventHandler("parameterHints",_handleEscapeKeyEvent)}),exports.registerHintProvider=registerHintProvider,exports.removeHintProvider=removeHintProvider});