-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathParameterHintsManager.js
More file actions
1 lines (1 loc) · 8.55 KB
/
Copy pathParameterHintsManager.js
File metadata and controls
1 lines (1 loc) · 8.55 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"),PreferencesManager=require("preferences/PreferencesManager"),WorkspaceManager=require("view/WorkspaceManager"),Metrics=require("utils/Metrics"),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,$hintScroll,$hintContent,hintState={},lastChar=null,sessionEditor=null,keyDownEditor=null,POINTER_TOP_OFFSET=4,POSITION_BELOW_OFFSET=4,handleCursorActivity,popupShown=!1,pendingRequestId=0;function _signatureKey(hint){return(hint.parameters||[]).map(function(p){return p.label||p.type||""}).join(",")}let _providerRegistrationHandler=new ProviderRegistrationHandler,registerHintProvider=_providerRegistrationHandler.registerProvider.bind(_providerRegistrationHandler),removeHintProvider=_providerRegistrationHandler.removeProvider.bind(_providerRegistrationHandler),paramHintsEnabled=!0;function _revealCurrentParameter(){let el=$hintScroll&&$hintScroll[0];if(!el)return;let maxScroll=el.scrollWidth-el.clientWidth,$cur=$hintContent.find(".current-parameter");if(maxScroll>0&&$cur.length){let elRect=el.getBoundingClientRect(),curRect=$cur[0].getBoundingClientRect(),curLeftInContent,target=curRect.left-elRect.left+el.scrollLeft-(el.clientWidth-curRect.width)/2;el.scrollLeft=Math.max(0,Math.min(target,maxScroll))}$hintScroll.toggleClass("fade-left",el.scrollLeft>1),$hintScroll.toggleClass("fade-right",el.scrollLeft<maxScroll-1)}function positionHint(xpos,ypos,ybot){let $editorHolder=$("#editor-holder"),editorLeft;if(void 0===$editorHolder.offset())return;editorLeft=$editorHolder.offset().left;let maxWidth=Math.min(700,$editorHolder.width()-24);$hintContainer.css("max-width",maxWidth+"px"),_revealCurrentParameter();let hintWidth=$hintContainer.outerWidth(),hintHeight,top=ypos-$hintContainer.outerHeight()-POINTER_TOP_OFFSET,left=xpos;left=Math.min(left,editorLeft+$editorHolder.width()-hintWidth),left=Math.max(left,editorLeft),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 functionName=hints.functionName;null==functionName&&(functionName=editor.getToken(hints.functionCallPos).string),_formatParameterHint(functionName,hints.parameters,appendSeparators,appendParameter)}else $hintContent.append(_.escape(Strings.NO_ARGUMENTS))}function dismissHint(editor){popupShown=!1,pendingRequestId++,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;if(!paramHintsEnabled)return dismissHint(editor),$deferredPopUp;popupShown=!0;let language=editor.getLanguageForSelection(),enabledProviders;if(_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)return dismissHint(editor),$deferredPopUp;let requestId=++pendingRequestId;return request.done(function(parameterHint){if(requestId!==pendingRequestId)return;let signature=_signatureKey(parameterHint),renderKey=parameterHint.currentIndex+"|"+signature;if(hintState.visible&&hintState.renderKey===renderKey)$deferredPopUp.resolveWith(null);else{if(hintState.visible||Metrics.countEventBatched(Metrics.EVENT_TYPE.CODE_HINTS,"sig",language.getId()),_formatHint(editor,parameterHint),$hintContainer.show(),hintState.visible&&hintState.signature===signature&&hintState.anchor)positionHint(hintState.anchor.left,hintState.anchor.top,hintState.anchor.bottom);else{let cm=editor._codeMirror,pos=parameterHint.functionCallPos||editor.getCursorPos();positionHint((pos=cm.charCoords(pos)).left,pos.top,pos.bottom),hintState.anchor=pos,hintState.signature=signature}hintState.visible=!0,hintState.renderKey=renderKey,sessionEditor!==editor&&(sessionEditor&&sessionEditor.off("cursorActivity.ParameterHinting",handleCursorActivity),sessionEditor=editor,editor.off("cursorActivity.ParameterHinting",handleCursorActivity),editor.on("cursorActivity.ParameterHinting",handleCursorActivity)),$deferredPopUp.resolveWith(null)}}).fail(function(){requestId===pendingRequestId&&dismissHint(editor)}),$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)}PreferencesManager.definePreference("showParameterHints","boolean",!0,{description:Strings.DESCRIPTION_SHOW_PARAMETER_HINTS}),PreferencesManager.on("change","showParameterHints",function(){(paramHintsEnabled=PreferencesManager.get("showParameterHints"))||(Metrics.countEvent(Metrics.EVENT_TYPE.CODE_HINTS,"pref","sigOff"),dismissHint())}),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")),$hintScroll=$hintContainer.find(".function-hint-scroll"),$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});