|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import { execFile } from 'child_process'; |
| 3 | + |
| 4 | +export function activate(context: vscode.ExtensionContext) { |
| 5 | + const diagCollection = vscode.languages.createDiagnosticCollection('ubon'); |
| 6 | + context.subscriptions.push(diagCollection); |
| 7 | + |
| 8 | + const runScan = () => { |
| 9 | + const cliPath = vscode.workspace.getConfiguration().get<string>('ubon.cliPath', 'ubon'); |
| 10 | + const args = vscode.workspace.getConfiguration().get<string[]>('ubon.args', ['check', '--json']); |
| 11 | + const cwd = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath || process.cwd(); |
| 12 | + |
| 13 | + execFile(cliPath, args, { cwd, maxBuffer: 10 * 1024 * 1024 }, (err, stdout, stderr) => { |
| 14 | + if (err && !stdout) { |
| 15 | + vscode.window.showErrorMessage(`Ubon failed: ${err.message}`); |
| 16 | + return; |
| 17 | + } |
| 18 | + try { |
| 19 | + const payload = JSON.parse(stdout || '{}'); |
| 20 | + const issues = Array.isArray(payload.issues) ? payload.issues : []; |
| 21 | + diagCollection.clear(); |
| 22 | + const byFile = new Map<string, vscode.Diagnostic[]>(); |
| 23 | + for (const i of issues) { |
| 24 | + if (!i.file) continue; |
| 25 | + const range = new vscode.Range( |
| 26 | + new vscode.Position(Math.max(0, (i.line || i.range?.startLine || 1) - 1), 0), |
| 27 | + new vscode.Position(Math.max(0, (i.line || i.range?.endLine || 1) - 1), 1000) |
| 28 | + ); |
| 29 | + const severity = i.type === 'error' ? vscode.DiagnosticSeverity.Error : i.type === 'warning' ? vscode.DiagnosticSeverity.Warning : vscode.DiagnosticSeverity.Information; |
| 30 | + const d = new vscode.Diagnostic(range, `${i.ruleId}: ${i.message}`, severity); |
| 31 | + d.code = i.ruleId; |
| 32 | + const list = byFile.get(i.file) || []; |
| 33 | + list.push(d); |
| 34 | + byFile.set(i.file, list); |
| 35 | + } |
| 36 | + for (const [file, diags] of byFile.entries()) { |
| 37 | + const uri = vscode.Uri.file(`${cwd}/${file}`); |
| 38 | + diagCollection.set(uri, diags); |
| 39 | + } |
| 40 | + } catch (e: any) { |
| 41 | + vscode.window.showErrorMessage(`Ubon parse error: ${e?.message || e}`); |
| 42 | + } |
| 43 | + }); |
| 44 | + }; |
| 45 | + |
| 46 | + const disposable = vscode.commands.registerCommand('ubon.scan', runScan); |
| 47 | + context.subscriptions.push(disposable); |
| 48 | + |
| 49 | + // Optionally run on startup |
| 50 | + setTimeout(runScan, 1500); |
| 51 | +} |
| 52 | + |
| 53 | +export function deactivate() {} |
| 54 | + |
| 55 | + |
0 commit comments