Skip to content

Commit af32c09

Browse files
committed
cli: Add Interrupt to simplify control flow
1 parent 584b9e4 commit af32c09

File tree

11 files changed

+79
-75
lines changed

11 files changed

+79
-75
lines changed

demos

src/cli/blueprint.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const languageId = "blueprint";
1212
export default async function blueprint({ file, lspc }) {
1313
print(` ${file.get_path()}`);
1414

15-
const text = await diagnose({
15+
await diagnose({
1616
file,
1717
lspc,
1818
languageId,
@@ -21,10 +21,12 @@ export default async function blueprint({ file, lspc }) {
2121
return ![
2222
"Gtk.ShortcutsShortcut is deprecated\nhint: This widget will be removed in GTK 5",
2323
"Gtk.ShortcutLabel is deprecated\nhint: This widget will be removed in GTK 5",
24+
"Gtk.ShortcutsWindow is deprecated\nhint: This widget will be removed in GTK 5",
25+
"Gtk.ShortcutsGroup is deprecated\nhint: This widget will be removed in GTK 5",
26+
"Gtk.ShortcutsSection is deprecated\nhint: This widget will be removed in GTK 5",
2427
].includes(diagnostic.message);
2528
},
2629
});
27-
if (text === false) return false;
2830

2931
const { xml } = await lspc._request("textDocument/x-blueprint-compile", {
3032
textDocument: {
@@ -53,13 +55,12 @@ export default async function blueprint({ file, lspc }) {
5355
}
5456
}
5557

56-
const checks = await checkFile({
58+
await checkFile({
5759
lspc,
5860
file,
5961
lang: getLanguage(languageId),
6062
uri: file.get_uri(),
6163
});
62-
if (!checks) return false;
6364

6465
await lspc._notify("textDocument/didClose", {
6566
textDocument: {

src/cli/css.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@ const languageId = "css";
88
export default async function css({ file, lspc }) {
99
print(` ${file.get_path()}`);
1010

11-
const text = await diagnose({ file, lspc, languageId });
12-
if (text === false) return false;
11+
await diagnose({ file, lspc, languageId });
1312

14-
const checks = await checkFile({
13+
await checkFile({
1514
lspc,
1615
file,
1716
lang: getLanguage(languageId),
1817
uri: file.get_uri(),
1918
});
20-
if (!checks) return false;
2119

2220
await lspc._notify("textDocument/didClose", {
2321
textDocument: {

src/cli/javascript.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-restricted-globals */
22

33
import { getLanguage } from "../common.js";
4-
import { checkFile, getCodeObjectIds, diagnose } from "./util.js";
4+
import { checkFile, getCodeObjectIds, diagnose, Interrupt } from "./util.js";
55

66
const languageId = "javascript";
77

@@ -18,21 +18,19 @@ export default async function javascript({
1818
print(` ${file.get_path()}`);
1919

2020
const text = await diagnose({ file, lspc, languageId });
21-
if (text === false) return false;
2221

23-
const checks = await checkFile({
22+
await checkFile({
2423
lspc: lspc,
2524
file: file,
2625
lang: getLanguage("javascript"),
2726
uri: file.get_uri(),
2827
});
29-
if (!checks) return false;
3028

3129
const js_object_ids = getCodeObjectIds(text);
3230
for (const object_id of js_object_ids) {
3331
if (!blueprint_object_ids.includes(object_id)) {
3432
print(` ❌ Reference to inexistant object id "${object_id}"`);
35-
return false;
33+
throw new Interrupt();
3634
}
3735
}
3836

src/cli/lint.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Gio from "gi://Gio";
44

55
import { formatting } from "./format.js";
66
import { diagnostic_severities } from "../lsp/LSP.js";
7+
import { waitForDiagnostics } from "./util.js";
78

89
export default async function lint({ filenames, lang, lspc, ci }) {
910
let success = true;
@@ -47,19 +48,6 @@ export default async function lint({ filenames, lang, lspc, ci }) {
4748
return success;
4849
}
4950

50-
export function waitForDiagnostics({ uri, lspc }) {
51-
return new Promise((resolve) => {
52-
const handler_id = lspc.connect(
53-
"notification::textDocument/publishDiagnostics",
54-
(_self, params) => {
55-
if (uri !== params.uri) return;
56-
lspc.disconnect(handler_id);
57-
resolve(params.diagnostics);
58-
},
59-
);
60-
});
61-
}
62-
6351
function serializeDiagnostics({ file, diagnostics }) {
6452
return (
6553
`\n${file.get_path()}\n` +

src/cli/main.js

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import typescript from "./typescript.js";
2121
import vala from "./vala.js";
2222
import python from "./python.js";
2323
import rust from "./rust.js";
24+
import { Interrupt } from "./util.js";
2425

2526
GObject.type_ensure(Shumate.SimpleMap);
2627
GObject.type_ensure(WebKit.WebView);
@@ -30,8 +31,16 @@ export async function main([action, ...args]) {
3031

3132
if (action === "ci") {
3233
const filenames = args;
33-
const success = await ci({ filenames });
34-
return success ? 0 : 1;
34+
try {
35+
await ci({ filenames });
36+
return 0;
37+
} catch (err) {
38+
if (err instanceof Interrupt) {
39+
return 1;
40+
} else {
41+
throw err;
42+
}
43+
}
3544
}
3645

3746
const [language_id, ...filenames] = args;
@@ -66,19 +75,27 @@ export async function main([action, ...args]) {
6675
});
6776
}
6877

69-
let success = false;
70-
71-
if (action === "lint") {
72-
success = await lint({ filenames, lang, lspc, ci: false });
73-
} else if (action === "check") {
74-
success = await lint({ filenames, lang, lspc, ci: true });
75-
} else if (action === "format") {
76-
success = await format({ filenames, lang, lspc });
77-
} else {
78-
printerr(`Unknown action "${action}"}`);
78+
try {
79+
if (action === "lint") {
80+
await lint({ filenames, lang, lspc, ci: false });
81+
return 0;
82+
} else if (action === "check") {
83+
await lint({ filenames, lang, lspc, ci: true });
84+
return 0;
85+
} else if (action === "format") {
86+
await format({ filenames, lang, lspc });
87+
return 0;
88+
} else {
89+
printerr(`Unknown action "${action}"}`);
90+
return 1;
91+
}
92+
} catch (err) {
93+
if (err instanceof Interrupt) {
94+
return 1;
95+
} else {
96+
throw err;
97+
}
7998
}
80-
81-
return success ? 0 : 1;
8299
}
83100

84101
const application = new Adw.Application();
@@ -133,12 +150,10 @@ async function ci({ filenames }) {
133150

134151
const file_blueprint = demo_dir.get_child("main.blp");
135152
if (file_blueprint.query_exists(null)) {
136-
const result = await blueprint({
153+
({ template, builder, blueprint_object_ids } = await blueprint({
137154
file: file_blueprint,
138155
lspc: lsp_clients.blueprint,
139-
});
140-
if (result === false) return;
141-
({ template, builder, blueprint_object_ids } = result);
156+
}));
142157
}
143158

144159
const file_css = demo_dir.get_child("main.css");
@@ -195,8 +210,6 @@ async function ci({ filenames }) {
195210
}),
196211
);
197212
}
198-
199-
return true;
200213
}
201214

202215
const key_file = new GLib.KeyFile();

src/cli/python.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@ export default async function python({ file, lspc }) {
1313
settings: PYTHON_LSP_CONFIG,
1414
});
1515

16-
const text = await diagnose({ file, lspc, languageId });
17-
if (text === false) return false;
16+
await diagnose({ file, lspc, languageId });
1817

19-
const checks = await checkFile({
18+
await checkFile({
2019
lspc,
2120
file,
2221
lang: getLanguage(languageId),
2322
uri: file.get_uri(),
2423
});
25-
if (!checks) return false;
2624

2725
await lspc._notify("textDocument/didClose", {
2826
textDocument: {

src/cli/rust.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,18 @@ export default async function rust({ file, lspc }) {
2727
// FIXME: rust analyzer doesn't publish diagnostics if there are none
2828
// probably we should switch to pulling diagnostics but unknown if supported
2929
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_pullDiagnostics
30-
31-
// const diagnostics = await waitForDiagnostics({
32-
// uri,
33-
// lspc: lsp_clients.rust,
30+
// await diagnose({
31+
// file,
32+
// lspc,
33+
// languageId,
3434
// });
35-
// if (diagnostics.length > 0) {
36-
// printerr(serializeDiagnostics({ diagnostics }));
37-
// return false;
38-
// }
39-
// print(` ✅ lints`);
4035

41-
const checks = await checkFile({
36+
await checkFile({
4237
lspc,
4338
file,
4439
lang: getLanguage(languageId),
4540
uri,
4641
});
47-
if (!checks) return false;
4842

4943
await lspc._notify("textDocument/didClose", {
5044
textDocument: {

src/cli/typescript.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-restricted-globals */
22

33
import { getLanguage } from "../common.js";
4-
import { checkFile, getCodeObjectIds, diagnose } from "./util.js";
4+
import { checkFile, getCodeObjectIds, diagnose, Interrupt } from "./util.js";
55

66
const languageId = "typescript";
77

@@ -18,21 +18,19 @@ export default async function typescript({
1818
print(` ${file.get_path()}`);
1919

2020
const text = await diagnose({ file, lspc, languageId });
21-
if (text === false) return false;
2221

23-
const checks = await checkFile({
22+
await checkFile({
2423
lspc,
2524
file,
2625
lang: getLanguage(languageId),
2726
uri: file.get_uri(),
2827
});
29-
if (!checks) return false;
3028

3129
const js_object_ids = getCodeObjectIds(text);
3230
for (const object_id of js_object_ids) {
3331
if (!blueprint_object_ids.includes(object_id)) {
3432
print(` ❌ Reference to inexistant object id "${object_id}"`);
35-
return false;
33+
throw new Interrupt();
3634
}
3735
}
3836

src/cli/util.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import Gtk from "gi://Gtk";
44

55
import { diagnostic_severities } from "../lsp/LSP.js";
66
import { formatting } from "./format.js";
7-
import { waitForDiagnostics } from "./lint.js";
7+
8+
export class Interrupt extends Error {
9+
constructor(...args) {
10+
super(...args);
11+
Error.captureStackTrace?.(this, Interrupt);
12+
}
13+
}
814

915
export async function diagnose({
1016
file,
@@ -36,7 +42,7 @@ export async function diagnose({
3642
diagnostics = diagnostics.filter(filter);
3743
if (diagnostics.length > 0) {
3844
printerr(serializeDiagnostics({ diagnostics }));
39-
return false;
45+
throw new Interrupt();
4046
}
4147

4248
print(` ✅ lints`);
@@ -73,14 +79,13 @@ export async function checkFile({ lspc, file, lang, uri }) {
7379

7480
if (buffer_tmp.text === buffer.text) {
7581
print(` ✅ checks`);
76-
return true;
7782
} else {
7883
printerr(
7984
` ❌ formatting differs - open and run ${file
8085
.get_parent()
8186
.get_basename()} with Workbench to fix`,
8287
);
83-
return false;
88+
throw new Interrupt();
8489
}
8590
}
8691

@@ -91,3 +96,16 @@ export function getCodeObjectIds(text) {
9196
}
9297
return object_ids;
9398
}
99+
100+
export function waitForDiagnostics({ uri, lspc }) {
101+
return new Promise((resolve) => {
102+
const handler_id = lspc.connect(
103+
"notification::textDocument/publishDiagnostics",
104+
(_self, params) => {
105+
if (uri !== params.uri) return;
106+
lspc.disconnect(handler_id);
107+
resolve(params.diagnostics);
108+
},
109+
);
110+
});
111+
}

src/cli/vala.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default async function vala({ file, lspc, demo_dir }) {
2020
null,
2121
);
2222

23-
const text = await diagnose({
23+
await diagnose({
2424
file,
2525
lspc,
2626
languageId,
@@ -46,15 +46,13 @@ export default async function vala({ file, lspc, demo_dir }) {
4646
return true;
4747
},
4848
});
49-
if (text === false) return false;
5049

51-
const checks = await checkFile({
50+
await checkFile({
5251
lspc,
5352
file,
5453
lang: getLanguage("vala"),
5554
uri: file.get_uri(),
5655
});
57-
if (!checks) return false;
5856

5957
await lspc._notify("textDocument/didClose", {
6058
textDocument: {

0 commit comments

Comments
 (0)