vscode-omega
aien-dev/vscode-omega
Brought by drakestapleton
vscode-omega / omc.js
| 1 | 'use strict'; |
| 2 | // Runs omc (aien-dev/omega-mv) on a buffer: a check for diagnostics, and `om2 fmt` for formatting. |
| 3 | // No VS Code dependency, so test/omc.test.js exercises it with a real omc in CI. |
| 4 | const { spawn } = require('child_process'); |
| 5 | const fs = require('fs'); |
| 6 | const path = require('path'); |
| 7 | |
| 8 | // `omc: line N: error E` is always printed; `om2 diag` units add |
| 9 | // `omc-diag 1 code=E sev=S line=N col=C span=A-B ...` (OMEGESE.md 12, "om2 diag"). |
| 10 | const HUMAN = /^omc: line (\d+): error (\d+)\s*$/; |
| 11 | const DIAG = /^omc-diag 1 code=(\d+) sev=(\S+) line=(\d+) col=(\d+) span=(\d+)-(\d+)(.*)$/; |
| 12 | |
| 13 | function parseDiagnostics(stderr) { |
| 14 | const out = []; |
| 15 | for (const raw of stderr.split('\n')) { |
| 16 | let m = DIAG.exec(raw); |
| 17 | if (m) { |
| 18 | const last = out[out.length - 1]; |
| 19 | const d = { line: +m[3], code: +m[1], sev: m[2], col: +m[4], span: [+m[5], +m[6]], detail: m[7].trim() }; |
| 20 | if (last && last.line === d.line && last.code === d.code && last.col === undefined) out[out.length - 1] = d; |
| 21 | else out.push(d); |
| 22 | continue; |
| 23 | } |
| 24 | m = HUMAN.exec(raw); |
| 25 | if (m) out.push({ line: +m[1], code: +m[2] }); |
| 26 | } |
| 27 | return out; |
| 28 | } |
| 29 | |
| 30 | // An explicit path wins; otherwise the first executable `omc` on PATH, or null. |
| 31 | function findOmc(configured, envPath = process.env.PATH || '') { |
| 32 | const can = (p) => { |
| 33 | try { |
| 34 | fs.accessSync(p, fs.constants.X_OK); |
| 35 | return fs.statSync(p).isFile(); |
| 36 | } catch { |
| 37 | return false; |
| 38 | } |
| 39 | }; |
| 40 | if (configured) return can(configured) ? configured : null; |
| 41 | for (const dir of envPath.split(path.delimiter)) { |
| 42 | if (!dir) continue; |
| 43 | const p = path.join(dir, 'omc'); |
| 44 | if (can(p)) return p; |
| 45 | } |
| 46 | return null; |
| 47 | } |
| 48 | |
| 49 | function run(omc, input, timeoutMs) { |
| 50 | return new Promise((resolve, reject) => { |
| 51 | const child = spawn(omc, [], { stdio: ['pipe', 'pipe', 'pipe'] }); |
| 52 | const out = []; |
| 53 | const err = []; |
| 54 | const timer = setTimeout(() => child.kill('SIGKILL'), timeoutMs); |
| 55 | child.stdout.on('data', (b) => out.push(b)); |
| 56 | child.stderr.on('data', (b) => err.push(b)); |
| 57 | child.on('error', (e) => { |
| 58 | clearTimeout(timer); |
| 59 | reject(e); |
| 60 | }); |
| 61 | child.on('close', (code, signal) => { |
| 62 | clearTimeout(timer); |
| 63 | resolve({ code, signal, stdout: Buffer.concat(out), stderr: Buffer.concat(err).toString('utf8') }); |
| 64 | }); |
| 65 | child.stdin.on('error', () => {}); |
| 66 | child.stdin.end(input); |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | // Compile the text and report what omc says. The binary omc writes is discarded. |
| 71 | async function check(omc, text, timeoutMs = 10000) { |
| 72 | const r = await run(omc, text, timeoutMs); |
| 73 | if (r.signal) return { ok: false, killed: r.signal, diagnostics: [] }; |
| 74 | return { ok: r.code === 0, exit: r.code, diagnostics: parseDiagnostics(r.stderr) }; |
| 75 | } |
| 76 | |
| 77 | // `om2 fmt` on the first line makes omc print the canonical text of the rest, or refuse it; its |
| 78 | // line numbers then count the header line, so they are shifted back by one here. |
| 79 | async function format(omc, text, timeoutMs = 10000) { |
| 80 | const r = await run(omc, 'om2 fmt\n' + text, timeoutMs); |
| 81 | if (r.signal) return { ok: false, killed: r.signal, diagnostics: [] }; |
| 82 | const diagnostics = parseDiagnostics(r.stderr).map((d) => ({ ...d, line: Math.max(1, d.line - 1) })); |
| 83 | if (r.code !== 0) return { ok: false, exit: r.code, diagnostics }; |
| 84 | return { ok: true, text: r.stdout.toString('utf8'), diagnostics }; |
| 85 | } |
| 86 | |
| 87 | module.exports = { parseDiagnostics, findOmc, check, format }; |