forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNpmHover.js
More file actions
1 lines (1 loc) · 2.59 KB
/
Copy pathNpmHover.js
File metadata and controls
1 lines (1 loc) · 2.59 KB
1
define(function(require,exports,module){const QuickViewManager=require("features/QuickViewManager"),JSONUtils=require("language/JSONUtils"),NativeApp=require("utils/NativeApp"),Strings=require("strings"),semver=require("thirdparty/semver.browser"),_=require("thirdparty/lodash"),NpmHints=require("./NpmHints"),NpmRegistry=require("./NpmRegistry");function _externalLink(label,url){return $("<a>").attr({href:"#",title:url}).text(label).on("click",function(e){e.preventDefault(),e.stopPropagation(),NativeApp.openURLInDefaultBrowser(url)})}function _npmPageUrl(name,declaredRange){const base="https://www.npmjs.com/package/"+name,anchor=(declaredRange||"").replace(/^[\s^~>=<]+/,"").trim();return semver.valid(anchor)?base+"/v/"+anchor:base}function _content(info,declaredRange){const $content=$("<div>").addClass("lsp-hover-quickview npm-hover-quickview"),$doc=$("<div>").addClass("lsp-hover-doc").appendTo($content),meta=[info.version&&"v"+info.version,info.license].filter(Boolean).join(" · "),$title=$("<p>").append($("<strong>").text(info.name));meta&&$title.append(document.createTextNode(" ")).append($("<span>").css("opacity",.65).text(meta)),$doc.append($title),info.description&&$doc.append($("<p>").text(info.description));const $links=$("<p>").addClass("npm-hover-links");return info.homepage&&/^https?:\/\//.test(info.homepage)&&$links.append(_externalLink(Strings.NPM_HOVER_HOMEPAGE,info.homepage)),$links.append(_externalLink(Strings.NPM_HOVER_VIEW_DOCS,_npmPageUrl(info.name,declaredRange)).addClass("npm-hover-docs-link")),$doc.append($links),$content}const provider={QUICK_VIEW_NAME:"npmPackageHover",getQuickView:function(editor,pos,token,line){return new Promise(function(resolve,reject){if(!NpmHints.isPackageJson(editor))return void reject();const ctxInfo=JSONUtils.getContextInfo(editor,pos,!0);if(!NpmHints.depContext(ctxInfo))return void reject();const onKey=ctxInfo.tokenType===JSONUtils.TOKEN_KEY,name=onKey?JSONUtils.stripQuotes(ctxInfo.token.string).trim():ctxInfo.keyName;if(!name)return void reject();let declaredRange;if(onKey){const lineMatch=(line||editor.document.getLine(pos.line)||"").match(/:\s*"((?:[^"\\]|\\.)*)"/);declaredRange=lineMatch&&lineMatch[1]||""}else declaredRange=JSONUtils.stripQuotes(ctxInfo.token.string||"").trim();NpmRegistry.getPackageInfo(name).then(function(info){resolve({start:{line:pos.line,ch:ctxInfo.token.start},end:{line:pos.line,ch:ctxInfo.token.end},content:_content(info,declaredRange)})}).catch(function(){reject()})})}};function init(){QuickViewManager.registerQuickViewProvider(provider,["json"])}exports.init=init,exports._content=_content,exports._npmPageUrl=_npmPageUrl});