Skip to content

Commit 2a65be9

Browse files
committed
cli: Split by lang
1 parent 630dbb4 commit 2a65be9

File tree

9 files changed

+596
-477
lines changed

9 files changed

+596
-477
lines changed

src/cli/blueprint.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* eslint-disable no-restricted-globals */
2+
3+
import Gtk from "gi://Gtk";
4+
5+
import { getLanguage } from "../common.js";
6+
import { parse } from "../langs/xml/xml.js";
7+
import { LSPError } from "../lsp/LSP.js";
8+
import { waitForDiagnostics } from "./lint.js";
9+
import { serializeDiagnostics, checkFile } from "./util.js";
10+
11+
export default async function blueprint({ file_blueprint, lsp_clients }) {
12+
print(` ${file_blueprint.get_path()}`);
13+
const uri = file_blueprint.get_uri();
14+
const languageId = "blueprint";
15+
let version = 0;
16+
17+
const [contents] = await file_blueprint.load_contents_async(null);
18+
const text = new TextDecoder().decode(contents);
19+
20+
await lsp_clients.blueprint._notify("textDocument/didOpen", {
21+
textDocument: {
22+
uri,
23+
languageId,
24+
version: version++,
25+
text,
26+
},
27+
});
28+
29+
const diagnostics = await waitForDiagnostics({
30+
uri,
31+
lspc: lsp_clients.blueprint,
32+
});
33+
if (diagnostics.length > 0) {
34+
printerr(serializeDiagnostics({ diagnostics }));
35+
return false;
36+
}
37+
38+
print(` ✅ lints`);
39+
40+
const { xml } = await lsp_clients.blueprint._request(
41+
"textDocument/x-blueprint-compile",
42+
{
43+
textDocument: {
44+
uri,
45+
},
46+
},
47+
);
48+
49+
print(` ✅ compiles`);
50+
51+
try {
52+
await lsp_clients.blueprint._request("x-blueprint/decompile", {
53+
text: xml,
54+
});
55+
print(" ✅ decompiles");
56+
} catch (err) {
57+
if (!(err instanceof LSPError)) throw err;
58+
if (
59+
![
60+
// https://gitlab.gnome.org/jwestman/blueprint-compiler/-/issues/128
61+
"unsupported XML tag: <condition>",
62+
// https://gitlab.gnome.org/jwestman/blueprint-compiler/-/issues/139
63+
"unsupported XML tag: <items>",
64+
].includes(err.message)
65+
) {
66+
throw err;
67+
}
68+
}
69+
70+
const checks = await checkFile({
71+
lspc: lsp_clients.blueprint,
72+
file: file_blueprint,
73+
lang: getLanguage("blueprint"),
74+
uri,
75+
});
76+
if (!checks) return false;
77+
78+
await lsp_clients.blueprint._notify("textDocument/didClose", {
79+
textDocument: {
80+
uri,
81+
},
82+
});
83+
84+
const tree = parse(xml);
85+
const template_el = tree.getChild("template");
86+
87+
let template;
88+
const builder = new Gtk.Builder();
89+
const blueprint_object_ids = [];
90+
91+
if (template_el) {
92+
template = tree.toString();
93+
} else {
94+
builder.add_from_string(xml, -1);
95+
print(` ✅ instantiates`);
96+
getXMLObjectIds(tree, blueprint_object_ids);
97+
}
98+
99+
return { template, builder, blueprint_object_ids };
100+
}
101+
102+
function getXMLObjectIds(tree, object_ids) {
103+
for (const object of tree.getChildren("object")) {
104+
if (object.attrs.id) object_ids.push(object.attrs.id);
105+
// <child> or <property name="child">
106+
for (const child of object.getChildElements()) {
107+
getXMLObjectIds(child, object_ids);
108+
}
109+
}
110+
}

src/cli/css.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* eslint-disable no-restricted-globals */
2+
3+
import { getLanguage } from "../common.js";
4+
import { waitForDiagnostics } from "./lint.js";
5+
import { serializeDiagnostics, checkFile } from "./util.js";
6+
7+
export default async function css({ file_css, lsp_clients }) {
8+
print(` ${file_css.get_path()}`);
9+
10+
const uri = file_css.get_uri();
11+
const languageId = "css";
12+
let version = 0;
13+
14+
const [contents] = await file_css.load_contents_async(null);
15+
const text = new TextDecoder().decode(contents);
16+
17+
await lsp_clients.css._notify("textDocument/didOpen", {
18+
textDocument: {
19+
uri,
20+
languageId,
21+
version: version++,
22+
text,
23+
},
24+
});
25+
26+
const diagnostics = await waitForDiagnostics({
27+
uri,
28+
lspc: lsp_clients.css,
29+
});
30+
if (diagnostics.length > 0) {
31+
printerr(serializeDiagnostics({ diagnostics }));
32+
return false;
33+
}
34+
print(` ✅ lints`);
35+
36+
const checks = await checkFile({
37+
lspc: lsp_clients.css,
38+
file: file_css,
39+
lang: getLanguage("css"),
40+
uri,
41+
});
42+
if (!checks) return false;
43+
44+
await lsp_clients.css._notify("textDocument/didClose", {
45+
textDocument: {
46+
uri,
47+
},
48+
});
49+
}

src/cli/javascript.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* eslint-disable no-restricted-globals */
2+
3+
import { getLanguage } from "../common.js";
4+
import { waitForDiagnostics } from "./lint.js";
5+
import { serializeDiagnostics, checkFile, getCodeObjectIds } from "./util.js";
6+
7+
export default async function typescript({
8+
file_javascript,
9+
lsp_clients,
10+
blueprint_object_ids,
11+
demo_dir,
12+
application,
13+
builder,
14+
template,
15+
window,
16+
}) {
17+
print(` ${file_javascript.get_path()}`);
18+
19+
const uri = file_javascript.get_uri();
20+
const languageId = "javascript";
21+
let version = 0;
22+
23+
const [contents] = await file_javascript.load_contents_async(null);
24+
const text = new TextDecoder().decode(contents);
25+
26+
await lsp_clients.javascript._notify("textDocument/didOpen", {
27+
textDocument: {
28+
uri,
29+
languageId,
30+
version: version++,
31+
text,
32+
},
33+
});
34+
35+
const diagnostics = await waitForDiagnostics({
36+
uri,
37+
lspc: lsp_clients.javascript,
38+
});
39+
if (diagnostics.length > 0) {
40+
printerr(serializeDiagnostics({ diagnostics }));
41+
return false;
42+
}
43+
print(` ✅ lints`);
44+
45+
const checks = await checkFile({
46+
lspc: lsp_clients.javascript,
47+
file: file_javascript,
48+
lang: getLanguage("javascript"),
49+
uri,
50+
});
51+
if (!checks) return false;
52+
53+
const js_object_ids = getCodeObjectIds(text);
54+
for (const object_id of js_object_ids) {
55+
if (!blueprint_object_ids.includes(object_id)) {
56+
print(` ❌ Reference to inexistant object id "${object_id}"`);
57+
return false;
58+
}
59+
}
60+
61+
globalThis.workbench = {
62+
window,
63+
application,
64+
builder,
65+
template,
66+
resolve(path) {
67+
return demo_dir.resolve_relative_path(path).get_uri();
68+
},
69+
preview() {},
70+
};
71+
72+
await import(`file://${file_javascript.get_path()}`);
73+
print(" ✅ runs");
74+
75+
await lsp_clients.javascript._notify("textDocument/didClose", {
76+
textDocument: {
77+
uri,
78+
},
79+
});
80+
}

0 commit comments

Comments
 (0)