-
Notifications
You must be signed in to change notification settings - Fork 0
Add menu command for "Open Primer documentation for variable" #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,9 @@ | ||||||||||||||||||||||||||||||||||||||||
| import {join} from 'node:path' | ||||||||||||||||||||||||||||||||||||||||
| import {workspace, ExtensionContext, window, ViewColumn, WebviewPanel} from 'vscode' | ||||||||||||||||||||||||||||||||||||||||
| import {workspace, ExtensionContext, window, ViewColumn, WebviewPanel, commands} from 'vscode' | ||||||||||||||||||||||||||||||||||||||||
| import {LanguageClient, LanguageClientOptions, ServerOptions, TransportKind} from 'vscode-languageclient/node' | ||||||||||||||||||||||||||||||||||||||||
| import {getCssVariable} from './utils/get-css-variable' | ||||||||||||||||||||||||||||||||||||||||
| import {getVariableInfo} from './utils/get-variable-info' | ||||||||||||||||||||||||||||||||||||||||
| import {getCurrentWord} from './utils/get-current-word' | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| let client: LanguageClient | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
|
@@ -57,19 +60,39 @@ export function activate(context: ExtensionContext) { | |||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| let panel: WebviewPanel | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| client.onRequest('open-docs', ({variable, openPanelIfClosed = true}) => { | ||||||||||||||||||||||||||||||||||||||||
| if (!panel && openPanelIfClosed) { | ||||||||||||||||||||||||||||||||||||||||
| commands.registerCommand('primer-primitives-autocomplete.openDocs', async () => { | ||||||||||||||||||||||||||||||||||||||||
| const editor = window.activeTextEditor | ||||||||||||||||||||||||||||||||||||||||
| if (!editor) return | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| const document = editor.document | ||||||||||||||||||||||||||||||||||||||||
| if (!document) return null | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| const position = editor.selection.active | ||||||||||||||||||||||||||||||||||||||||
| const offset = document.offsetAt(position) | ||||||||||||||||||||||||||||||||||||||||
| const variableName = getCssVariable(document, offset) | ||||||||||||||||||||||||||||||||||||||||
| if (!variableName) { | ||||||||||||||||||||||||||||||||||||||||
| const currentWord = getCurrentWord(document, offset) | ||||||||||||||||||||||||||||||||||||||||
| window.showInformationMessage(`Unrecognized variable: ${currentWord}. Cannot open Primer documentation.`) | ||||||||||||||||||||||||||||||||||||||||
| return null | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| const variableInfo = getVariableInfo(variableName) | ||||||||||||||||||||||||||||||||||||||||
| if (!variableInfo) { | ||||||||||||||||||||||||||||||||||||||||
| const currentWord = getCurrentWord(document, offset) | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+72
to
+81
|
||||||||||||||||||||||||||||||||||||||||
| const variableName = getCssVariable(document, offset) | |
| if (!variableName) { | |
| const currentWord = getCurrentWord(document, offset) | |
| window.showInformationMessage(`Unrecognized variable: ${currentWord}. Cannot open Primer documentation.`) | |
| return null | |
| } | |
| const variableInfo = getVariableInfo(variableName) | |
| if (!variableInfo) { | |
| const currentWord = getCurrentWord(document, offset) | |
| const currentWord = getCurrentWord(document, offset) | |
| const variableName = getCssVariable(document, offset) | |
| if (!variableName) { | |
| window.showInformationMessage(`Unrecognized variable: ${currentWord}. Cannot open Primer documentation.`) | |
| return null | |
| } | |
| const variableInfo = getVariableInfo(variableName) | |
| if (!variableInfo) { |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The HTML template has missing quotes around attribute values, which could cause XSS vulnerabilities or incorrect rendering if the URLs contain special characters.
Add quotes around the href and src attributes:
panel.webview.html = `
<style>
body {
padding: 0;
}
a {
padding: 16px;
text-decoration: none;
display: flex;
gap: 4px;
}
iframe {
width: calc(100% + 20px);
height: 100vh;
border: none;
}
</style>
<a href="${variableInfo.docsUrl}">
Open in Browser
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16" fill="currentColor">
<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>
</svg>
</a>
<iframe sandbox="allow-same-origin allow-scripts allow-popups allow-forms" src="${variableInfo.docsUrl}"></iframe>
`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The command registration is not being added to
context.subscriptions, which could lead to a resource leak. The command should be disposed when the extension is deactivated.Add the registration to subscriptions: