forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHoverProvider.js
More file actions
1 lines (1 loc) · 3.8 KB
/
Copy pathHoverProvider.js
File metadata and controls
1 lines (1 loc) · 3.8 KB
1
define(function(require,exports,module){const marked=require("thirdparty/marked.min"),CommandManager=require("command/CommandManager"),Commands=require("command/Commands"),KeyBindingManager=require("command/KeyBindingManager"),QuickViewManager=require("features/QuickViewManager"),Strings=require("strings"),Metrics=require("utils/Metrics");function _contentsToMarkdown(contents){return contents?"string"==typeof contents?contents:Array.isArray(contents)?contents.map(_contentsToMarkdown).filter(Boolean).join("\n\n---\n\n"):contents.kind?contents.value||"":contents.language?"```"+contents.language+"\n"+(contents.value||"")+"\n```":contents.value||"":""}function _highlightCode(html){const hljs="undefined"!=typeof Phoenix&&Phoenix.libs&&Phoenix.libs.hljs;if(!hljs)return html;const $wrap=$("<div>").html(html);return $wrap.find("pre > code").each(function(){const $code=$(this),match=($code.attr("class")||"").match(/language-([\w-]+)/),lang=match&&match[1];if(lang&&hljs.getLanguage(lang))try{$code.html(hljs.highlight($code.text(),{language:lang}).value).addClass("hljs")}catch(e){}}),$wrap.html()}function _renderContents(contents){const markdown=_contentsToMarkdown(contents);if(!markdown||!markdown.trim())return null;try{return _highlightCode(marked.parse(markdown))}catch(e){return null}}function _action(iconClass,label,commandId,editor,pos,alignRight){const $action=$("<div>").addClass("lsp-hover-action").attr("tabindex","0");alignRight&&$action.addClass("lsp-hover-action--end"),$("<i>").addClass(iconClass+" lsp-hover-action-icon").appendTo($action),$("<span>").addClass("lsp-hover-action-label").text(label).appendTo($action);const shortcut=KeyBindingManager.getKeyBindingsDisplay(commandId);function run(e){e&&(e.preventDefault(),e.stopPropagation()),QuickViewManager.hideQuickView(),editor.setCursorPos(pos.line,pos.ch),editor.focus(),CommandManager.execute(commandId)}return $action.attr("title",shortcut?label+" ("+shortcut+")":label),$action.on("click",run),$action.on("keydown",function(e){13!==e.keyCode&&32!==e.keyCode||run(e)}),$action}function _buildActions(client,editor,pos){const caps=client.getServerCapabilities()||{},$actions=$("<div>").addClass("lsp-hover-actions");let count=0;return caps.definitionProvider&&($actions.append(_action("fa-solid fa-arrow-right",Strings.CMD_JUMPTO_DEFINITION,Commands.NAVIGATE_JUMPTO_DEFINITION,editor,pos)),count++),caps.referencesProvider&&($actions.append(_action("fa-solid fa-magnifying-glass",Strings.FIND_ALL_REFERENCES,Commands.CMD_FIND_ALL_REFERENCES,editor,pos,!0)),count++),count?$actions:null}function HoverProvider(client){this.client=client,this.QUICK_VIEW_NAME="lsp.hover."+client.serverId}HoverProvider.prototype.getQuickView=function(editor,pos,token,line){const self=this;return new Promise(function(resolve,reject){if(!(self.client&&self.client.servesDocument(editor)&&self.client.getServerCapabilities()&&self.client.getServerCapabilities().hoverProvider))return void reject();const filePath=editor.document.file._path;self.client.requestHover({filePath:filePath,cursorPos:pos}).done(function(hover){const html=hover&&_renderContents(hover.contents);if(!html)return void reject();let start={line:pos.line,ch:token.start},end={line:pos.line,ch:token.end};hover.range&&(start={line:hover.range.start.line,ch:hover.range.start.character},end={line:hover.range.end.line,ch:hover.range.end.character});const $content=$("<div>").addClass("lsp-hover-quickview");$("<div>").addClass("lsp-hover-doc").html(html).appendTo($content);const $actions=_buildActions(self.client,editor,pos);$actions&&$content.append($actions),Metrics.countEventBatched(Metrics.EVENT_TYPE.LSP,"hover","Ok."+self.client._metricLabel),resolve({start:start,end:end,content:$content})}).fail(function(err){err&&Metrics.countEvent(Metrics.EVENT_TYPE.LSP,"hover","Fail."+self.client._metricLabel),reject()})})},exports.HoverProvider=HoverProvider});