11import { join } from 'node:path'
2- import { workspace , ExtensionContext , window , ViewColumn , WebviewPanel } from 'vscode'
2+ import { workspace , ExtensionContext , window , ViewColumn , WebviewPanel , commands } from 'vscode'
33import { LanguageClient , LanguageClientOptions , ServerOptions , TransportKind } from 'vscode-languageclient/node'
4+ import { getCssVariable } from './utils/get-css-variable'
5+ import { getVariableInfo } from './utils/get-variable-info'
6+ import { getCurrentWord } from './utils/get-current-word'
47
58let client : LanguageClient
69
@@ -57,19 +60,39 @@ export function activate(context: ExtensionContext) {
5760
5861 let panel : WebviewPanel
5962
60- client . onRequest ( 'open-docs' , ( { variable, openPanelIfClosed = true } ) => {
61- if ( ! panel && openPanelIfClosed ) {
63+ commands . registerCommand ( 'primer-primitives-autocomplete.openDocs' , async ( ) => {
64+ const editor = window . activeTextEditor
65+ if ( ! editor ) return
66+
67+ const document = editor . document
68+ if ( ! document ) return null
69+
70+ const position = editor . selection . active
71+ const offset = document . offsetAt ( position )
72+ const variableName = getCssVariable ( document , offset )
73+ if ( ! variableName ) {
74+ const currentWord = getCurrentWord ( document , offset )
75+ window . showInformationMessage ( `Unrecognized variable: ${ currentWord } . Cannot open Primer documentation.` )
76+ return null
77+ }
78+
79+ const variableInfo = getVariableInfo ( variableName )
80+ if ( ! variableInfo ) {
81+ const currentWord = getCurrentWord ( document , offset )
82+ window . showInformationMessage ( `Unrecognized variable: ${ currentWord } . Cannot open Primer documentation.` )
83+ return null
84+ }
85+
86+ if ( ! panel ) {
6287 panel = window . createWebviewPanel ( 'custom view type' , 'Primer Primitives' , ViewColumn . Beside , {
6388 enableScripts : true ,
6489 enableFindWidget : true ,
6590 } )
6691 panel . onDidDispose ( ( ) => ( panel = null ) )
6792 }
6893
69- if ( panel ) {
70- panel . title = variable . name
71-
72- panel . webview . html = `
94+ panel . title = variableInfo . name
95+ panel . webview . html = `
7396 <style>
7497 body {
7598 padding: 0;
@@ -88,17 +111,16 @@ export function activate(context: ExtensionContext) {
88111
89112 </style>
90113
91- <a href=${ variable . docsUrl } >
114+ <a href=${ variableInfo . docsUrl } >
92115 Open in Browser
93116 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16" fill="currentColor">
94117 <path d="M3.75 2h3.5a.75.75 0 0 1 0 1.5h-3.5a.25.25 0 0 0-.25.25v8.5c0 .138.112.25.25.25h8.5a.25.25 0 0 0 .25-.25v-3.5a.75.75 0 0 1 1.5 0v3.5A1.75 1.75 0 0 1 12.25 14h-8.5A1.75 1.75 0 0 1 2 12.25v-8.5C2 2.784 2.784 2 3.75 2Zm6.854-1h4.146a.25.25 0 0 1 .25.25v4.146a.25.25 0 0 1-.427.177L13.03 4.03 9.28 7.78a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042l3.75-3.75-1.543-1.543A.25.25 0 0 1 10.604 1Z"></path>
95118 </svg>
96119 </a>
97120
98- <iframe sandbox="allow-same-origin allow-scripts allow-popups allow-forms" src="${ variable . docsUrl } "></iframe>
121+ <iframe sandbox="allow-same-origin allow-scripts allow-popups allow-forms" src="${ variableInfo . docsUrl } "></iframe>
99122
100123 `
101- }
102124 } )
103125}
104126
0 commit comments