forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCodeHintManager.js
More file actions
1 lines (1 loc) · 9.24 KB
/
CodeHintManager.js
File metadata and controls
1 lines (1 loc) · 9.24 KB
1
define(function(require,exports,module){const Commands=require("command/Commands"),CommandManager=require("command/CommandManager"),EditorManager=require("editor/EditorManager"),Strings=require("strings"),KeyEvent=require("utils/KeyEvent"),CodeHintListModule=require("editor/CodeHintList"),PreferencesManager=require("preferences/PreferencesManager"),CodeHintList=CodeHintListModule.CodeHintList;let hintProviders={all:[]},lastChar=null,sessionProvider=null,sessionEditor=null,hintList=null,deferredHints=null,keyDownEditor=null,codeHintsEnabled=!0,codeHintOpened=!1,hintsAtTopHandler=null;function _providerSort(a,b){return b.priority-a.priority}function registerHintProvider(providerInfo,languageIds,priority){var providerObj={provider:providerInfo,priority:priority||0},languageId;if(-1!==languageIds.indexOf("all"))for(languageId in hintProviders)hintProviders.hasOwnProperty(languageId)&&(hintProviders[languageId].push(providerObj),hintProviders[languageId].sort(_providerSort));else languageIds.forEach(function(languageId){hintProviders[languageId]||(hintProviders[languageId]=Array.prototype.concat(hintProviders.all)),hintProviders[languageId].push(providerObj),hintProviders[languageId].sort(_providerSort)})}function _removeHintProvider(provider,targetLanguageId){var index,providers,targetLanguageIdArr;(targetLanguageIdArr=Array.isArray(targetLanguageId)?targetLanguageId:targetLanguageId?[targetLanguageId]:Object.keys(hintProviders)).forEach(function(languageId){for(providers=hintProviders[languageId],index=0;index<providers.length;index++)if(providers[index].provider===provider){providers.splice(index,1);break}})}function _getProvidersForLanguageId(languageId){var providers;return(hintProviders[languageId]||hintProviders.all).filter(function(provider){var prefKey="codehint."+provider.provider.constructor.name;return!1!==PreferencesManager.get(prefKey)})}var _beginSession;function _endSession(){hintList&&(hintList.close(),hintList=null,codeHintOpened=!1,keyDownEditor=null,sessionProvider=null,sessionEditor=null,deferredHints&&(deferredHints.reject(),deferredHints=null))}function _inSession(editor){if(sessionEditor){if(sessionEditor===editor&&(hintList.isOpen()||deferredHints&&"pending"===deferredHints.state()))return!0;_endSession()}return!1}function _updateHintList(callMoveUpEvent){if(callMoveUpEvent=void 0!==callMoveUpEvent&&callMoveUpEvent,deferredHints&&(deferredHints.reject(),deferredHints=null),callMoveUpEvent)return hintList.callMoveUp(callMoveUpEvent);var response=null;sessionProvider&&(response=sessionProvider.getHints(lastChar)),lastChar=null;var usedTopHintsAsFallback=!1;if(!response&&hintsAtTopHandler&&hintsAtTopHandler.getHints&&(response=hintsAtTopHandler.getHints(sessionEditor,lastChar),usedTopHintsAsFallback=!0),response)if(!0===response){var previousEditor=sessionEditor;_endSession(),_beginSession(previousEditor)}else if(response.hasOwnProperty("hints")){if(!usedTopHintsAsFallback&&sessionProvider&&hintsAtTopHandler&&hintsAtTopHandler.getHints){var topHints=hintsAtTopHandler.getHints(sessionEditor,lastChar);if(topHints&&topHints.hints&&topHints.hints.length>0){var combinedHints=topHints.hints.concat(response.hints);response=$.extend({},response,{hints:combinedHints})}}hintList.isOpen()?hintList.update(response):hintList.open(response)}else deferredHints=response,response.done(function(hints){if(hintList){if(sessionProvider&&hintsAtTopHandler&&hintsAtTopHandler.getHints){var topHints=hintsAtTopHandler.getHints(sessionEditor,lastChar);if(topHints&&topHints.hints&&topHints.hints.length>0){var combinedHints=topHints.hints.concat(hints.hints);hints=$.extend({},hints,{hints:combinedHints})}}hintList.isOpen()?hintList.update(hints):hintList.open(hints)}});else _endSession()}function _handleKeydownEvent(jqEvent,editor,event){keyDownEditor=editor,event.ctrlKey||event.altKey||event.metaKey||event.keyCode!==KeyEvent.DOM_VK_ENTER&&event.keyCode!==KeyEvent.DOM_VK_RETURN&&event.keyCode!==KeyEvent.DOM_VK_TAB||(lastChar=String.fromCharCode(event.keyCode))}function _handleKeypressEvent(jqEvent,editor,event){keyDownEditor=editor,lastChar=String.fromCharCode(event.charCode),hintList&&hintList.addPendingText(lastChar)}function _handleKeyupEvent(jqEvent,editor,event){keyDownEditor=editor,_inSession(editor)&&(event.keyCode===KeyEvent.DOM_VK_HOME||event.keyCode===KeyEvent.DOM_VK_END?_endSession():event.keyCode===KeyEvent.DOM_VK_LEFT||event.keyCode===KeyEvent.DOM_VK_RIGHT||event.keyCode===KeyEvent.DOM_VK_BACK_SPACE?_updateHintList():event.ctrlKey&&event.keyCode===KeyEvent.DOM_VK_SPACE&&_updateHintList(event))}function _handleCursorActivity(event,editor){_inSession(editor)&&editor.getSelections().length>1&&_endSession()}function _handleChange(event,editor,changeList){if(lastChar&&editor===keyDownEditor){if(keyDownEditor=null,_inSession(editor)){var charToRetest=lastChar;_updateHintList(),_inSession(editor)||(lastChar=charToRetest,_beginSession(editor))}else _beginSession(editor);if(hintList&&changeList[0]&&changeList[0].text.length&&changeList[0].text[0].length){var expectedLength=editor.getCursorPos().ch-changeList[0].from.ch,newText=changeList[0].text[0];newText.length>expectedLength&&(newText=newText.substr(0,expectedLength)),hintList.removePendingText(newText)}}}function hasValidExclusion(exclusion,textAfterCursor){return exclusion&&exclusion===textAfterCursor}function isOpen(){return hintList&&hintList.isOpen()}function showHintsAtTop(handler){hintsAtTopHandler=handler}function clearHintsAtTop(){hintsAtTopHandler=null}function _startNewSession(editor){const SHOW_PARAMETER_HINT_CMD_ID="showParameterHint";CommandManager.execute("showParameterHint"),isOpen()||(editor||(editor=EditorManager.getFocusedEditor()),editor&&(lastChar=null,_inSession(editor)&&_endSession(),_beginSession(editor)))}function _getCodeHintList(){return hintList}function activeEditorChangeHandler(event,current,previous){current&&(current.off("editorChange",_handleChange),current.off("keydown",_handleKeydownEvent),current.off("keypress",_handleKeypressEvent),current.off("keyup",_handleKeyupEvent),current.off("cursorActivity",_handleCursorActivity),current.on("editorChange",_handleChange),current.on("keydown",_handleKeydownEvent),current.on("keypress",_handleKeypressEvent),current.on("keyup",_handleKeyupEvent),current.on("cursorActivity",_handleCursorActivity)),previous&&(previous.off("editorChange",_handleChange),previous.off("keydown",_handleKeydownEvent),previous.off("keypress",_handleKeypressEvent),previous.off("keyup",_handleKeyupEvent),previous.off("cursorActivity",_handleCursorActivity))}PreferencesManager.definePreference("showCodeHints","boolean",!0,{description:Strings.DESCRIPTION_SHOW_CODE_HINTS}),PreferencesManager.definePreference("insertHintOnTab","boolean",!0,{description:Strings.DESCRIPTION_INSERT_HINT_ON_TAB}),PreferencesManager.definePreference("maxCodeHints","number",50,{description:Strings.DESCRIPTION_MAX_CODE_HINTS}),PreferencesManager.on("change","showCodeHints",function(){codeHintsEnabled=PreferencesManager.get("showCodeHints")}),_beginSession=function(editor){if(codeHintsEnabled&&!(editor.getSelections().length>1)){var language,enabledProviders=_getProvidersForLanguageId(editor.getLanguageForSelection().getId()),hasTopHints=!1;if(hintsAtTopHandler&&hintsAtTopHandler.hasHints&&(hasTopHints=hintsAtTopHandler.hasHints(editor,lastChar)),hasTopHints||enabledProviders.some(function(item,index){if(item.provider.hasHints(editor,lastChar))return sessionProvider=item.provider,!0}),sessionProvider||hasTopHints){var insertHintOnTab=PreferencesManager.get("insertHintOnTab"),maxCodeHints=PreferencesManager.get("maxCodeHints");sessionProvider&&void 0!==sessionProvider.insertHintOnTab&&(insertHintOnTab=sessionProvider.insertHintOnTab),(hintList=new CodeHintList(sessionEditor=editor,insertHintOnTab,maxCodeHints)).onHighlight(function($hint,$hintDescContainer,reason){hintList.enableDescription&&$hintDescContainer&&$hintDescContainer.length?(sessionProvider&&sessionProvider.onHighlight&&sessionProvider.onHighlight($hint,$hintDescContainer,reason),sessionProvider&&sessionProvider.updateHintDescription&&sessionProvider.updateHintDescription($hint,$hintDescContainer)):sessionProvider&&sessionProvider.onHighlight&&sessionProvider.onHighlight($hint,void 0,reason)}),hintList.onSelect(function(hint){var handled=!1;if(hintsAtTopHandler&&hintsAtTopHandler.insertHint&&(handled=hintsAtTopHandler.insertHint(hint)),handled)_endSession();else if(sessionProvider){var restart=sessionProvider.insertHint(hint),previousEditor=sessionEditor;_endSession(),restart&&_beginSession(previousEditor)}else _endSession()}),hintList.onClose(()=>{sessionProvider&&sessionProvider.onClose&&sessionProvider.onClose(),_endSession()}),_updateHintList()}else lastChar=null}},activeEditorChangeHandler(null,EditorManager.getActiveEditor(),null),EditorManager.on("activeEditorChange",activeEditorChangeHandler),CommandManager.on("beforeExecuteCommand",function(event,commandId){commandId!==Commands.SHOW_CODE_HINTS&&_endSession()}),CommandManager.register(Strings.CMD_SHOW_CODE_HINTS,Commands.SHOW_CODE_HINTS,_startNewSession),exports._getCodeHintList=_getCodeHintList,exports._removeHintProvider=_removeHintProvider,exports.isOpen=isOpen,exports.registerHintProvider=registerHintProvider,exports.hasValidExclusion=hasValidExclusion,exports.showHintsAtTop=showHintsAtTop,exports.clearHintsAtTop=clearHintsAtTop,exports.SELECTION_REASON=CodeHintListModule._SELECTION_REASON});