Skip to content

Commit 7d58506

Browse files
committed
Use dropdown to convert between XML and Blueprint
1 parent 6c06dfe commit 7d58506

File tree

9 files changed

+98
-77
lines changed

9 files changed

+98
-77
lines changed

blueprint-compiler

Submodule blueprint-compiler updated from c587c13 to f4afa0c

src/PanelUI.js

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
disconnect_signals,
1111
replaceBufferText,
1212
unstack,
13+
listenProperty,
1314
} from "./util.js";
1415

1516
import {
@@ -60,12 +61,7 @@ export default function PanelUI({
6061

6162
const xml = await compile();
6263
replaceBufferText(buffer_xml, xml);
63-
settings.set_int("ui-language", 0);
6464
}
65-
const button_ui_export_xml = builder.get_object("button_ui_export_xml");
66-
button_ui_export_xml.connect("clicked", () => {
67-
convertToXML().catch(logError);
68-
});
6965

7066
async function convertToBlueprint() {
7167
term_console.clear();
@@ -84,21 +80,7 @@ export default function PanelUI({
8480
}
8581

8682
replaceBufferText(buffer_blueprint, blp);
87-
settings.set_int("ui-language", 1);
8883
}
89-
const button_ui_export_blueprint = builder.get_object(
90-
"button_ui_export_blueprint",
91-
);
92-
button_ui_export_blueprint.connect("clicked", () => {
93-
convertToBlueprint().catch(logError);
94-
});
95-
96-
settings.bind(
97-
"ui-language",
98-
dropdown_ui_lang,
99-
"selected",
100-
Gio.SettingsBindFlags.DEFAULT,
101-
);
10284

10385
const button_ui_experimental_blueprint = builder.get_object(
10486
"button_ui_experimental_blueprint",
@@ -119,21 +101,13 @@ export default function PanelUI({
119101
);
120102
});
121103

122-
dropdown_ui_lang.connect("notify::selected-item", switchLanguage);
123-
function switchLanguage() {
124-
const language = getLanguage(dropdown_ui_lang.selected_item.string);
125-
stack_ui.set_visible_child_name(language.id);
126-
button_ui_experimental_blueprint.visible = language.id === "blueprint";
127-
}
128-
switchLanguage();
129-
130104
let handler_ids = null;
131105

132106
const scheduleUpdate = unstack(update);
133107
async function update() {
134108
let xml;
135109
if (lang.id === "xml") {
136-
xml = lang.document.buffer.text;
110+
xml = buffer_xml.text;
137111
} else {
138112
xml = await compile();
139113
}
@@ -159,12 +133,44 @@ export default function PanelUI({
159133
}
160134
}
161135

162-
settings.connect_after("changed::ui-language", () => {
163-
start();
164-
scheduleUpdate();
136+
async function onChangeLang(value) {
137+
if (value === 0) {
138+
try {
139+
await convertToXML();
140+
} catch (err) {
141+
logError(err);
142+
dropdown_ui_lang.block();
143+
dropdown_ui_lang.set_selected(1);
144+
dropdown_ui_lang.unblock();
145+
return;
146+
}
147+
} else if (value === 1) {
148+
try {
149+
await convertToBlueprint();
150+
} catch (err) {
151+
logError(err);
152+
dropdown_ui_lang.block();
153+
dropdown_ui_lang.set_selected(0);
154+
dropdown_ui_lang.unblock();
155+
return;
156+
}
157+
}
158+
159+
settings.set_int("ui-language", dropdown_ui_lang.selected);
160+
setupLanguage();
161+
}
162+
163+
dropdown_ui_lang.set_selected(settings.get_int("ui-language"));
164+
listenProperty(dropdown_ui_lang, "selected", (value) => {
165+
onChangeLang(value).catch(logError);
165166
});
166167

167-
start();
168+
function setupLanguage() {
169+
start();
170+
stack_ui.set_visible_child_name(lang.id);
171+
button_ui_experimental_blueprint.visible = lang.id === "blueprint";
172+
}
173+
setupLanguage();
168174

169175
panel.start = start;
170176
panel.stop = stop;

src/bin.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ GLib.set_application_name("Workbench");
1818
const resource = Gio.resource_load("@pkgdatadir@/@app_id@.gresource");
1919
Gio.resources_register(resource);
2020

21+
globalThis.__DEV__ = pkg.name.endsWith(".Devel");
22+
if (__DEV__) {
23+
pkg.sourcedir = "@sourcedir@";
24+
}
25+
2126
const loop = new GLib.MainLoop(null, false);
2227
import("resource:///re/sonny/Workbench/src/main.js")
2328
.then((main) => {

src/langs/blueprint/blueprint.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
getLanguage,
88
prepareSourceView,
99
handleDiagnostics,
10+
connect_signals,
11+
disconnect_signals,
1012
} from "../../util.js";
1113
import WorkbenchHoverProvider from "../../WorkbenchHoverProvider.js";
1214
import { getPid, once } from "../../../troll/src/util.js";
@@ -30,11 +32,14 @@ export function setup({ data_dir }) {
3032
provider,
3133
});
3234

33-
let modifed_changed_signal;
35+
let handler_ids = null;
3436
async function setupLSP() {
3537
if (lspc.proc) return;
3638

37-
modifed_changed_signal && lspc.disconnect(modifed_changed_signal);
39+
if (handler_ids !== null) {
40+
disconnect_signals(buffer, handler_ids);
41+
handler_ids = null;
42+
}
3843
lspc.start();
3944

4045
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#initialize
@@ -64,18 +69,22 @@ export function setup({ data_dir }) {
6469
},
6570
});
6671

67-
modifed_changed_signal = buffer.connect("modified-changed", () => {
68-
if (!buffer.get_modified()) return;
69-
72+
function onUpdate() {
7073
lspc
7174
.notify("textDocument/didChange", {
7275
textDocument: {
7376
uri,
7477
version: ++document_version,
7578
},
76-
contentChanges: [{text: buffer.text}],
79+
contentChanges: [{ text: buffer.text }],
7780
})
7881
.catch(logError);
82+
}
83+
84+
handler_ids = connect_signals(buffer, {
85+
"end-user-action": onUpdate,
86+
undo: onUpdate,
87+
redo: onUpdate,
7988
});
8089
}
8190
setupLSP().catch(logError);
@@ -92,8 +101,12 @@ export function setup({ data_dir }) {
92101
null,
93102
);
94103
const lspc = new LSPClient([
95-
// "/home/sonny/Projects/Workbench/blueprint-compiler/blueprint-compiler.py",
96-
"/app/bin/blueprint-compiler",
104+
__DEV__
105+
? GLib.build_filenamev([
106+
pkg.sourcedir,
107+
"blueprint-compiler/blueprint-compiler.py",
108+
])
109+
: "/app/bin/blueprint-compiler",
97110
"lsp",
98111
]);
99112
lspc.connect("exit", () => {
@@ -134,7 +147,7 @@ export function setup({ data_dir }) {
134147
uri,
135148
version: ++document_version,
136149
},
137-
contentChanges: [{text: buffer.text}],
150+
contentChanges: [{ text: buffer.text }],
138151
});
139152

140153
const [{ xml }] = await once(

src/lsp/LSPClient.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ export default class LSPClient {
2525
}
2626

2727
_start_process() {
28-
const flags =
29-
Gio.SubprocessFlags.STDIN_PIPE |
30-
Gio.SubprocessFlags.STDOUT_PIPE |
31-
// vala-language-server emit lots of criticals https://github.com/vala-lang/vala-language-server/issues/274
32-
// It's harmless but it looks scary.
33-
// If you need to debug a language server, remove this flag
34-
Gio.SubprocessFlags.STDERR_SILENCE;
28+
let flags =
29+
Gio.SubprocessFlags.STDIN_PIPE | Gio.SubprocessFlags.STDOUT_PIPE;
30+
// vala-language-server emit lots of criticals so we disable this on release
31+
// https://github.com/vala-lang/vala-language-server/issues/274
32+
if (!__DEV__) {
33+
flags = flags | Gio.SubprocessFlags.STDERR_SILENCE;
34+
}
3535

3636
this.proc = Gio.Subprocess.new(this.argv, flags);
3737
this.proc.wait_async(null, (_self, res) => {

src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ bin_conf.set('prefix', prefix)
66
bin_conf.set('libdir', join_paths(get_option('prefix'), get_option('libdir')))
77
bin_conf.set('datadir', datadir)
88
bin_conf.set('pkgdatadir', pkgdatadir)
9+
bin_conf.set('sourcedir', meson.source_root())
910

1011
blueprint_compiler = find_program(
1112
# Flatpak

src/util.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,29 @@ export function getLanguageForFile(file) {
117117
});
118118
}
119119

120+
export function listenProperty(
121+
object,
122+
property,
123+
fn,
124+
{ initial = false, equal = true } = {},
125+
) {
126+
let value = object[property];
127+
if (initial) {
128+
fn(value);
129+
}
130+
const signal_spec = object.connect(`notify::${property}`, () => {
131+
const new_value = object[property];
132+
if (equal && new_value === value) return;
133+
value = new_value;
134+
fn(value);
135+
});
136+
return {
137+
disconnect() {
138+
return signal_spec.disconnect();
139+
},
140+
};
141+
}
142+
120143
export function connect_signals(target, signals) {
121144
return Object.entries(signals).map(([signal, handler]) => {
122145
return target.connect_after(signal, handler);

src/window.blp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -277,33 +277,6 @@ Gtk.ApplicationWindow window {
277277
tooltip-text: _("Informaton about Blueprint");
278278
}
279279
}
280-
281-
[end]
282-
Stack stack_ui_export {
283-
visible-child-name: bind stack_ui.visible-child-name;
284-
285-
StackPage {
286-
name: 'blueprint';
287-
child: Button button_ui_export_xml {
288-
icon-name: "horizontal-arrows-right-symbolic";
289-
tooltip-text: _("Convert to XML");
290-
accessibility {
291-
label: _("Convert to XML");
292-
}
293-
};
294-
}
295-
296-
StackPage {
297-
name: 'xml';
298-
child: Button button_ui_export_blueprint {
299-
icon-name: "horizontal-arrows-left-symbolic";
300-
tooltip-text: _("Convert to Blueprint");
301-
accessibility {
302-
label: _("Convert to Blueprint");
303-
}
304-
};
305-
}
306-
}
307280
}
308281

309282
Separator {

src/window.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default function Window({ application }) {
5757
const builder = Gtk.Builder.new_from_resource(resource);
5858

5959
const window = builder.get_object("window");
60-
if (pkg.name.endsWith("Devel")) {
60+
if (__DEV__) {
6161
window.add_css_class("devel");
6262
}
6363
window.set_application(application);

0 commit comments

Comments
 (0)