interplane.Get Omega
Read in your language

vscode-omega

aien-dev/vscode-omega

Brought by drakestapleton

branch main Branches Tags History

vscode-omega / extension.js

101 lines · 3677 bytes Raw Blame History
1'use strict';
2// Omega for VS Code: optional omc diagnostics on save and `om2 fmt` formatting. Both do nothing until
3// an omc is found (setting omega.omcPath, else `omc` on PATH). omc is a static AArch64 Linux program.
4const vscode = require('vscode');
5const omcLib = require('./omc');
6
7function settings() {
8 const c = vscode.workspace.getConfiguration('omega');
9 return {
10 omcPath: c.get('omcPath') || '',
11 onSave: c.get('diagnostics.onSave', true),
12 timeoutMs: c.get('omcTimeoutMs', 10000),
13 };
14}
15
16function toDiagnostic(doc, d) {
17 const lineNo = Math.min(Math.max(d.line - 1, 0), Math.max(doc.lineCount - 1, 0));
18 const line = doc.lineAt(lineNo);
19 let range = line.range;
20 if (d.col !== undefined && d.span) {
21 const start = Math.min(Math.max(d.col - 1, 0), line.text.length);
22 const end = Math.min(start + Math.max(d.span[1] - d.span[0], 1), line.text.length);
23 range = new vscode.Range(lineNo, start, lineNo, Math.max(end, start));
24 }
25 const extra = d.sev ? ` (${d.sev}${d.detail ? ', ' + d.detail : ''})` : '';
26 const diag = new vscode.Diagnostic(range, `omc error ${d.code}${extra}`, vscode.DiagnosticSeverity.Error);
27 diag.source = 'omc';
28 diag.code = d.code;
29 return diag;
30}
31
32function activate(context) {
33 const collection = vscode.languages.createDiagnosticCollection('omc');
34 const log = vscode.window.createOutputChannel('Omega');
35 context.subscriptions.push(collection, log);
36 let warned = false;
37
38 const omc = () => {
39 const s = settings();
40 const found = omcLib.findOmc(s.omcPath);
41 if (!found && !warned) {
42 warned = true;
43 log.appendLine(
44 s.omcPath
45 ? `omega.omcPath "${s.omcPath}" is not an executable file; diagnostics and formatting are off.`
46 : 'No omc on PATH; diagnostics and formatting are off. omc runs on AArch64 Linux; build it with ' +
47 '`sh seed/omega-from-seed.sh` in aien-dev/omega-mv and set omega.omcPath to seed/out/omc.',
48 );
49 }
50 return { path: found, ...s };
51 };
52
53 const check = async (doc) => {
54 if (doc.languageId !== 'omega') return;
55 const o = omc();
56 if (!o.path) return;
57 try {
58 const r = await omcLib.check(o.path, doc.getText(), o.timeoutMs);
59 if (r.killed) {
60 log.appendLine(`omc was stopped after ${o.timeoutMs} ms (${r.killed}) on ${doc.uri.fsPath}`);
61 return;
62 }
63 collection.set(doc.uri, r.diagnostics.map((d) => toDiagnostic(doc, d)));
64 } catch (e) {
65 log.appendLine(`could not run ${o.path}: ${e.message}`);
66 }
67 };
68
69 context.subscriptions.push(
70 vscode.workspace.onDidSaveTextDocument((doc) => {
71 if (settings().onSave) check(doc);
72 }),
73 vscode.workspace.onDidCloseTextDocument((doc) => collection.delete(doc.uri)),
74 vscode.commands.registerCommand('omega.check', () => {
75 const ed = vscode.window.activeTextEditor;
76 if (ed) check(ed.document);
77 }),
78 vscode.languages.registerDocumentFormattingEditProvider('omega', {
79 async provideDocumentFormattingEdits(doc) {
80 const o = omc();
81 if (!o.path) return [];
82 try {
83 const r = await omcLib.format(o.path, doc.getText(), o.timeoutMs);
84 if (!r.ok) {
85 collection.set(doc.uri, r.diagnostics.map((d) => toDiagnostic(doc, d)));
86 return [];
87 }
88 const all = new vscode.Range(doc.positionAt(0), doc.positionAt(doc.getText().length));
89 return [vscode.TextEdit.replace(all, r.text)];
90 } catch (e) {
91 log.appendLine(`could not run ${o.path}: ${e.message}`);
92 return [];
93 }
94 },
95 }),
96 );
97}
98
99function deactivate() {}
100
101module.exports = { activate, deactivate };