|
| 1 | +import * as vscode from "vscode"; |
| 2 | + |
| 3 | +import { Workspace } from "./workspace"; |
| 4 | + |
| 5 | +export async function collectRubyLspInfo(workspace: Workspace | undefined) { |
| 6 | + if (!workspace) { |
| 7 | + await vscode.window.showErrorMessage("No active Ruby workspace found."); |
| 8 | + return; |
| 9 | + } |
| 10 | + |
| 11 | + const lspInfo = await gatherLspInfo(workspace); |
| 12 | + const panel = vscode.window.createWebviewPanel( |
| 13 | + "rubyLspInfo", |
| 14 | + "Ruby LSP Information", |
| 15 | + vscode.ViewColumn.One, |
| 16 | + { enableScripts: true }, |
| 17 | + ); |
| 18 | + |
| 19 | + panel.webview.html = generateRubyLspInfoReport(lspInfo); |
| 20 | +} |
| 21 | + |
| 22 | +async function gatherLspInfo( |
| 23 | + workspace: Workspace, |
| 24 | +): Promise<Record<string, string | string[] | Record<string, unknown>>> { |
| 25 | + const vscodeVersion = vscode.version; |
| 26 | + const rubyLspExtension = vscode.extensions.getExtension("Shopify.ruby-lsp")!; |
| 27 | + const rubyLspExtensionVersion = rubyLspExtension.packageJSON.version; |
| 28 | + const rubyLspVersion = workspace.lspClient?.serverVersion ?? "Unknown"; |
| 29 | + const rubyLspAddons = |
| 30 | + workspace.lspClient?.addons?.map((addon) => addon.name) ?? []; |
| 31 | + const extensions = await getPublicExtensions(); |
| 32 | + |
| 33 | + // Fetch rubyLsp settings |
| 34 | + const workspaceSettings = vscode.workspace.getConfiguration( |
| 35 | + "rubyLsp", |
| 36 | + workspace.workspaceFolder, |
| 37 | + ); |
| 38 | + const userSettings = vscode.workspace.getConfiguration("rubyLsp"); |
| 39 | + |
| 40 | + // Get only the workspace-specific settings |
| 41 | + const workspaceSpecificSettings: Record<string, unknown> = {}; |
| 42 | + for (const key of Object.keys(workspaceSettings)) { |
| 43 | + if (workspaceSettings.inspect(key)?.workspaceValue !== undefined) { |
| 44 | + workspaceSpecificSettings[key] = workspaceSettings.get(key); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return { |
| 49 | + /* eslint-disable @typescript-eslint/naming-convention */ |
| 50 | + "VS Code Version": vscodeVersion, |
| 51 | + "Ruby LSP Extension Version": rubyLspExtensionVersion, |
| 52 | + "Ruby LSP Server Version": rubyLspVersion, |
| 53 | + "Ruby LSP Addons": rubyLspAddons, |
| 54 | + "Ruby Version": workspace.ruby.rubyVersion ?? "Unknown", |
| 55 | + "Ruby Version Manager": workspace.ruby.versionManager.identifier, |
| 56 | + "Installed Extensions": extensions, |
| 57 | + "Ruby LSP Settings": { |
| 58 | + Workspace: workspaceSpecificSettings, |
| 59 | + User: userSettings, |
| 60 | + }, |
| 61 | + /* eslint-enable @typescript-eslint/naming-convention */ |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +async function getPublicExtensions(): Promise<string[]> { |
| 66 | + return vscode.extensions.all |
| 67 | + .filter((ext) => { |
| 68 | + // Filter out built-in extensions |
| 69 | + if (ext.packageJSON.isBuiltin) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + // Assume if an extension doesn't have a license, it's private and should not be listed |
| 74 | + if ( |
| 75 | + ext.packageJSON.license === "UNLICENSED" || |
| 76 | + !ext.packageJSON.license |
| 77 | + ) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + return true; |
| 82 | + }) |
| 83 | + .map((ext) => `${ext.packageJSON.name} (${ext.packageJSON.version})`); |
| 84 | +} |
| 85 | + |
| 86 | +function generateRubyLspInfoReport( |
| 87 | + info: Record<string, string | string[] | Record<string, unknown>>, |
| 88 | +): string { |
| 89 | + let markdown = "\n### Ruby LSP Information\n\n"; |
| 90 | + |
| 91 | + for (const [key, value] of Object.entries(info)) { |
| 92 | + markdown += `#### ${key}\n\n`; |
| 93 | + if (Array.isArray(value)) { |
| 94 | + if (key === "Installed Extensions") { |
| 95 | + markdown += |
| 96 | + "<details>\n<summary>Click to expand</summary>\n\n"; |
| 97 | + markdown += `${value.map((val) => `- ${val}`).join("\n")}\n`; |
| 98 | + markdown += "</details>\n"; |
| 99 | + } else { |
| 100 | + markdown += `${value.map((val) => `- ${val}`).join("\n")}\n`; |
| 101 | + } |
| 102 | + } else if (typeof value === "object" && value !== null) { |
| 103 | + markdown += |
| 104 | + "<details>\n<summary>Click to expand</summary>\n\n"; |
| 105 | + for (const [subKey, subValue] of Object.entries(value)) { |
| 106 | + markdown += `##### ${subKey}\n\n`; |
| 107 | + markdown += `\`\`\`json\n${JSON.stringify(subValue, null, 2)}\n\`\`\`\n\n`; |
| 108 | + } |
| 109 | + markdown += "</details>\n"; |
| 110 | + } else { |
| 111 | + markdown += `${value}\n`; |
| 112 | + } |
| 113 | + markdown += "\n"; |
| 114 | + } |
| 115 | + |
| 116 | + const html = ` |
| 117 | + <!DOCTYPE html> |
| 118 | + <html lang="en"> |
| 119 | + <head> |
| 120 | + <meta charset="UTF-8"> |
| 121 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 122 | + <title>Ruby LSP Information</title> |
| 123 | + <style> |
| 124 | + body { font-family: var(--vscode-font-family); padding: 20px; } |
| 125 | + h1, h2, h3, p, li { color: var(--vscode-editor-foreground); } |
| 126 | + pre { |
| 127 | + background-color: var(--vscode-textBlockQuote-background); |
| 128 | + padding: 16px; |
| 129 | + overflow-x: auto; |
| 130 | + position: relative; |
| 131 | + } |
| 132 | + code { font-family: var(--vscode-editor-font-family); } |
| 133 | + #copyButton { |
| 134 | + position: absolute; |
| 135 | + top: 5px; |
| 136 | + right: 5px; |
| 137 | + background-color: var(--vscode-button-background); |
| 138 | + color: var(--vscode-button-foreground); |
| 139 | + border: none; |
| 140 | + padding: 5px 10px; |
| 141 | + cursor: pointer; |
| 142 | + } |
| 143 | + #copyButton:hover { background-color: var(--vscode-button-hoverBackground); } |
| 144 | + </style> |
| 145 | + </head> |
| 146 | + <body> |
| 147 | + <h1>Ruby LSP Information</h1> |
| 148 | + <p>Please copy the content below and paste it into the issue you're opening:</p> |
| 149 | + <pre><button id="copyButton">Copy</button><code id="diagnosticContent">${markdown}</code></pre> |
| 150 | + <script> |
| 151 | + const copyButton = document.getElementById('copyButton'); |
| 152 | + const diagnosticContent = document.getElementById('diagnosticContent'); |
| 153 | +
|
| 154 | + copyButton.addEventListener('click', () => { |
| 155 | + const range = document.createRange(); |
| 156 | + range.selectNode(diagnosticContent); |
| 157 | + window.getSelection().removeAllRanges(); |
| 158 | + window.getSelection().addRange(range); |
| 159 | + document.execCommand('copy'); |
| 160 | + window.getSelection().removeAllRanges(); |
| 161 | +
|
| 162 | + copyButton.textContent = 'Copied!'; |
| 163 | + setTimeout(() => { |
| 164 | + copyButton.textContent = 'Copy'; |
| 165 | + }, 2000); |
| 166 | + }); |
| 167 | + </script> |
| 168 | + </body> |
| 169 | + </html> |
| 170 | + `; |
| 171 | + |
| 172 | + return html; |
| 173 | +} |
0 commit comments