Skip to content

Commit 58b40d9

Browse files
authored
cli: Check formatting in ci (#864)
1 parent e34f01d commit 58b40d9

File tree

3 files changed

+68
-23
lines changed

3 files changed

+68
-23
lines changed

src/cli/main.js

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import Gio from "gi://Gio";
88
import Gtk from "gi://Gtk";
99
import Adw from "gi://Adw";
1010

11-
import { createLSPClient, languages } from "../common.js";
11+
import { createLSPClient, languages, getLanguage } from "../common.js";
1212
import lint, { waitForDiagnostics } from "./lint.js";
13-
import format from "./format.js";
13+
import format, { formatting } from "./format.js";
1414

1515
Gtk.init();
1616

@@ -90,6 +90,27 @@ function createLSPClients({ root_uri }) {
9090
);
9191
}
9292

93+
async function checkFile({ lspc, file, lang, uri }) {
94+
const [contents] = await file.load_contents_async(null);
95+
const text = new TextDecoder().decode(contents);
96+
const buffer = new Gtk.TextBuffer({ text });
97+
98+
const buffer_tmp = new Gtk.TextBuffer({ text: buffer.text });
99+
await formatting({ buffer: buffer_tmp, uri, lang, lspc });
100+
101+
if (buffer_tmp.text === buffer.text) {
102+
print(` ✅ checks`);
103+
return true;
104+
} else {
105+
printerr(
106+
` ❌ formatting differs - open and run ${file
107+
.get_parent()
108+
.get_basename()} with Workbench to fix`,
109+
);
110+
return false;
111+
}
112+
}
113+
93114
async function ci({ filenames, current_dir }) {
94115
for (const filename of filenames) {
95116
const demo_dir = Gio.File.new_for_path(filename);
@@ -158,7 +179,6 @@ async function ci({ filenames, current_dir }) {
158179
print(` ✅ compiles`);
159180

160181
try {
161-
// const { blp } =
162182
await lsp_clients.blueprint._request("x-blueprint/decompile", {
163183
text: xml,
164184
});
@@ -175,14 +195,22 @@ async function ci({ filenames, current_dir }) {
175195
) {
176196
throw err;
177197
}
178-
179-
await lsp_clients.blueprint._notify("textDocument/didClose", {
180-
textDocument: {
181-
uri,
182-
},
183-
});
184198
}
185199

200+
const checks = await checkFile({
201+
lspc: lsp_clients.blueprint,
202+
file: file_blueprint,
203+
lang: getLanguage("blueprint"),
204+
uri,
205+
});
206+
if (!checks) return false;
207+
208+
await lsp_clients.blueprint._notify("textDocument/didClose", {
209+
textDocument: {
210+
uri,
211+
},
212+
});
213+
186214
const tree = parse(xml);
187215
const template_el = tree.getChild("template");
188216

@@ -223,9 +251,16 @@ async function ci({ filenames, current_dir }) {
223251
printerr(serializeDiagnostics({ diagnostics }));
224252
return false;
225253
}
226-
227254
print(` ✅ lints`);
228255

256+
const checks = await checkFile({
257+
lspc: lsp_clients.css,
258+
file: file_css,
259+
lang: getLanguage("css"),
260+
uri,
261+
});
262+
if (!checks) return false;
263+
229264
await lsp_clients.css._notify("textDocument/didClose", {
230265
textDocument: {
231266
uri,
@@ -261,9 +296,16 @@ async function ci({ filenames, current_dir }) {
261296
printerr(serializeDiagnostics({ diagnostics }));
262297
return false;
263298
}
264-
265299
print(` ✅ lints`);
266300

301+
const checks = await checkFile({
302+
lspc: lsp_clients.javascript,
303+
file: file_javascript,
304+
lang: getLanguage("javascript"),
305+
uri,
306+
});
307+
if (!checks) return false;
308+
267309
const js_object_ids = getCodeObjectIds(text);
268310
for (const object_id of js_object_ids) {
269311
if (!blueprint_object_ids.includes(object_id)) {
@@ -341,9 +383,16 @@ async function ci({ filenames, current_dir }) {
341383
printerr(serializeDiagnostics({ diagnostics }));
342384
return false;
343385
}
344-
345386
print(` ✅ lints`);
346387

388+
const checks = await checkFile({
389+
lspc: lsp_clients.vala,
390+
file: file_vala,
391+
lang: getLanguage("vala"),
392+
uri,
393+
});
394+
if (!checks) return false;
395+
347396
await lsp_clients.vala._notify("textDocument/didClose", {
348397
textDocument: {
349398
uri,

src/common.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ export const languages = [
106106
index: 3,
107107
},
108108
];
109-
if (!GLib.getenv("FLATPAK_ID")) {
110-
languages.forEach((lang) => {
111-
if (!lang.language_server) return;
112-
lang.language_server = ["./build-aux/fun", ...lang.language_server];
113-
});
109+
110+
export function getLanguage(id) {
111+
return languages.find(
112+
(language) => language.id.toLowerCase() === id.toLowerCase(),
113+
);
114114
}
115115

116116
export function createLSPClient({ lang, root_uri }) {

src/util.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import GLib from "gi://GLib";
22
import Gio from "gi://Gio";
33
import Xdp from "gi://Xdp";
44
import GObject from "gi://GObject";
5-
import { languages } from "./common.js";
5+
import { getLanguage } from "./common.js";
66

77
export const portal = new Xdp.Portal();
88

@@ -38,11 +38,7 @@ export function getFlatpakInfo() {
3838
return keyFile;
3939
}
4040

41-
export function getLanguage(id) {
42-
return languages.find(
43-
(language) => language.id.toLowerCase() === id.toLowerCase(),
44-
);
45-
}
41+
export { getLanguage };
4642

4743
export function listenProperty(object, property, fn, { initial = false } = {}) {
4844
if (initial) {

0 commit comments

Comments
 (0)