|
| 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 | +} |
0 commit comments