From 102787b40ed0c2ae45038f656747cbc0cb048086 Mon Sep 17 00:00:00 2001 From: Sonny Piers Date: Sat, 13 Jan 2024 21:06:40 +0100 Subject: [PATCH] cli: Check formatting in ci --- src/cli/main.js | 73 +++++++++++++++++++++++++++++++++++++++++-------- src/common.js | 10 +++---- src/util.js | 8 ++---- 3 files changed, 68 insertions(+), 23 deletions(-) diff --git a/src/cli/main.js b/src/cli/main.js index 8de74c1dd..b08dee780 100644 --- a/src/cli/main.js +++ b/src/cli/main.js @@ -8,9 +8,9 @@ import Gio from "gi://Gio"; import Gtk from "gi://Gtk"; import Adw from "gi://Adw"; -import { createLSPClient, languages } from "../common.js"; +import { createLSPClient, languages, getLanguage } from "../common.js"; import lint, { waitForDiagnostics } from "./lint.js"; -import format from "./format.js"; +import format, { formatting } from "./format.js"; Gtk.init(); @@ -90,6 +90,27 @@ function createLSPClients({ root_uri }) { ); } +async function checkFile({ lspc, file, lang, uri }) { + const [contents] = await file.load_contents_async(null); + const text = new TextDecoder().decode(contents); + const buffer = new Gtk.TextBuffer({ text }); + + const buffer_tmp = new Gtk.TextBuffer({ text: buffer.text }); + await formatting({ buffer: buffer_tmp, uri, lang, lspc }); + + if (buffer_tmp.text === buffer.text) { + print(` ✅ checks`); + return true; + } else { + printerr( + ` ❌ formatting differs - open and run ${file + .get_parent() + .get_basename()} with Workbench to fix`, + ); + return false; + } +} + async function ci({ filenames, current_dir }) { for (const filename of filenames) { const demo_dir = Gio.File.new_for_path(filename); @@ -158,7 +179,6 @@ async function ci({ filenames, current_dir }) { print(` ✅ compiles`); try { - // const { blp } = await lsp_clients.blueprint._request("x-blueprint/decompile", { text: xml, }); @@ -175,14 +195,22 @@ async function ci({ filenames, current_dir }) { ) { throw err; } - - await lsp_clients.blueprint._notify("textDocument/didClose", { - textDocument: { - uri, - }, - }); } + const checks = await checkFile({ + lspc: lsp_clients.blueprint, + file: file_blueprint, + lang: getLanguage("blueprint"), + uri, + }); + if (!checks) return false; + + await lsp_clients.blueprint._notify("textDocument/didClose", { + textDocument: { + uri, + }, + }); + const tree = parse(xml); const template_el = tree.getChild("template"); @@ -223,9 +251,16 @@ async function ci({ filenames, current_dir }) { printerr(serializeDiagnostics({ diagnostics })); return false; } - print(` ✅ lints`); + const checks = await checkFile({ + lspc: lsp_clients.css, + file: file_css, + lang: getLanguage("css"), + uri, + }); + if (!checks) return false; + await lsp_clients.css._notify("textDocument/didClose", { textDocument: { uri, @@ -261,9 +296,16 @@ async function ci({ filenames, current_dir }) { printerr(serializeDiagnostics({ diagnostics })); return false; } - print(` ✅ lints`); + const checks = await checkFile({ + lspc: lsp_clients.javascript, + file: file_javascript, + lang: getLanguage("javascript"), + uri, + }); + if (!checks) return false; + const js_object_ids = getCodeObjectIds(text); for (const object_id of js_object_ids) { if (!blueprint_object_ids.includes(object_id)) { @@ -341,9 +383,16 @@ async function ci({ filenames, current_dir }) { printerr(serializeDiagnostics({ diagnostics })); return false; } - print(` ✅ lints`); + const checks = await checkFile({ + lspc: lsp_clients.vala, + file: file_vala, + lang: getLanguage("vala"), + uri, + }); + if (!checks) return false; + await lsp_clients.vala._notify("textDocument/didClose", { textDocument: { uri, diff --git a/src/common.js b/src/common.js index c77b74421..8d2d84877 100644 --- a/src/common.js +++ b/src/common.js @@ -106,11 +106,11 @@ export const languages = [ index: 3, }, ]; -if (!GLib.getenv("FLATPAK_ID")) { - languages.forEach((lang) => { - if (!lang.language_server) return; - lang.language_server = ["./build-aux/fun", ...lang.language_server]; - }); + +export function getLanguage(id) { + return languages.find( + (language) => language.id.toLowerCase() === id.toLowerCase(), + ); } export function createLSPClient({ lang, root_uri }) { diff --git a/src/util.js b/src/util.js index ad3611882..ca7705cc0 100644 --- a/src/util.js +++ b/src/util.js @@ -2,7 +2,7 @@ import GLib from "gi://GLib"; import Gio from "gi://Gio"; import Xdp from "gi://Xdp"; import GObject from "gi://GObject"; -import { languages } from "./common.js"; +import { getLanguage } from "./common.js"; export const portal = new Xdp.Portal(); @@ -38,11 +38,7 @@ export function getFlatpakInfo() { return keyFile; } -export function getLanguage(id) { - return languages.find( - (language) => language.id.toLowerCase() === id.toLowerCase(), - ); -} +export { getLanguage }; export function listenProperty(object, property, fn, { initial = false } = {}) { if (initial) {