Skip to content

Commit 69ecc56

Browse files
committed
More efficient Blueprint support
Use the publishCompiled event instead of requesting a new compilation with every (or so) update
1 parent 7d58506 commit 69ecc56

File tree

2 files changed

+62
-44
lines changed

2 files changed

+62
-44
lines changed

src/PanelUI.js

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
unstack,
1313
listenProperty,
1414
} from "./util.js";
15+
import { once } from "../troll/src/util.js";
1516

1617
import {
1718
setup as setupBlueprint,
@@ -53,13 +54,15 @@ export default function PanelUI({
5354
// flat does nothing on GtkDropdown or GtkComboBox or GtkComboBoxText
5455
dropdown_ui_lang.get_first_child().get_style_context().add_class("flat");
5556

56-
const { compile, decompile } = setupBlueprint({ data_dir });
57+
const blueprint = setupBlueprint({
58+
data_dir,
59+
});
5760

5861
async function convertToXML() {
5962
term_console.clear();
6063
settings.set_boolean("show-console", true);
6164

62-
const xml = await compile();
65+
const xml = await blueprint.compile(buffer_blueprint.text);
6366
replaceBufferText(buffer_xml, xml);
6467
}
6568

@@ -70,7 +73,7 @@ export default function PanelUI({
7073
let blp;
7174

7275
try {
73-
blp = await decompile(buffer_xml.text);
76+
blp = await blueprint.decompile(buffer_xml.text);
7477
} catch (err) {
7578
if (err instanceof LSPError) {
7679
logBlueprintError(err);
@@ -101,35 +104,65 @@ export default function PanelUI({
101104
);
102105
});
103106

104-
let handler_ids = null;
107+
let handler_ids_xml = null;
108+
let handler_ids_blueprint = null;
109+
let handler_id_lsp = null;
105110

106-
const scheduleUpdate = unstack(update);
111+
// FIXME we should wait for previewer update instead
112+
// when loading demo
107113
async function update() {
108-
let xml;
109-
if (lang.id === "xml") {
110-
xml = buffer_xml.text;
111-
} else {
112-
xml = await compile();
114+
if (lang.id === "blueprint") {
115+
await blueprint.update(buffer_blueprint.text);
116+
await once(
117+
blueprint.lspc,
118+
"notification::textDocument/x-blueprintcompiler/publishCompiled",
119+
);
120+
} else if (lang.id === "xml") {
121+
onXML(buffer_xml.text);
113122
}
123+
}
124+
125+
function onXML(xml) {
114126
panel.xml = xml || "";
115127
panel.emit("updated");
116128
}
117129

130+
const onBlueprint = unstack(function onBlueprint() {
131+
return blueprint.update(buffer_blueprint.text);
132+
});
133+
118134
function start() {
119135
stop();
120136
lang = getLanguage(dropdown_ui_lang.selected_item.string);
121137
// cannot use "changed" signal as it triggers many time for pasting
122-
handler_ids = connect_signals(lang.document.buffer, {
123-
"end-user-action": scheduleUpdate,
124-
undo: scheduleUpdate,
125-
redo: scheduleUpdate,
138+
handler_ids_xml = connect_signals(buffer_xml, {
139+
"end-user-action": () => onXML(buffer_xml.text),
140+
undo: () => onXML(buffer_xml.text),
141+
redo: () => onXML(buffer_xml.text),
126142
});
143+
handler_ids_blueprint = connect_signals(buffer_blueprint, {
144+
"end-user-action": onBlueprint,
145+
undo: onBlueprint,
146+
redo: onBlueprint,
147+
});
148+
handler_id_lsp = blueprint.lspc.connect(
149+
"notification::textDocument/x-blueprintcompiler/publishCompiled",
150+
(_self, { xml }) => onXML(xml),
151+
);
127152
}
128153

129154
function stop() {
130-
if (handler_ids !== null) {
131-
disconnect_signals(lang.document.buffer, handler_ids);
132-
handler_ids = null;
155+
if (handler_ids_xml !== null) {
156+
disconnect_signals(buffer_xml, handler_ids_xml);
157+
handler_ids_xml = null;
158+
}
159+
if (handler_ids_blueprint !== null) {
160+
disconnect_signals(buffer_blueprint, handler_ids_blueprint);
161+
handler_ids_blueprint = null;
162+
}
163+
if (handler_id_lsp !== null) {
164+
blueprint.lspc.disconnect(handler_id_lsp);
165+
handler_id_lsp = null;
133166
}
134167
}
135168

src/langs/blueprint/blueprint.js

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import {
77
getLanguage,
88
prepareSourceView,
99
handleDiagnostics,
10-
connect_signals,
11-
disconnect_signals,
1210
} from "../../util.js";
1311
import WorkbenchHoverProvider from "../../WorkbenchHoverProvider.js";
1412
import { getPid, once } from "../../../troll/src/util.js";
@@ -32,14 +30,9 @@ export function setup({ data_dir }) {
3230
provider,
3331
});
3432

35-
let handler_ids = null;
3633
async function setupLSP() {
3734
if (lspc.proc) return;
3835

39-
if (handler_ids !== null) {
40-
disconnect_signals(buffer, handler_ids);
41-
handler_ids = null;
42-
}
4336
lspc.start();
4437

4538
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#initialize
@@ -68,24 +61,6 @@ export function setup({ data_dir }) {
6861
text: buffer.text,
6962
},
7063
});
71-
72-
function onUpdate() {
73-
lspc
74-
.notify("textDocument/didChange", {
75-
textDocument: {
76-
uri,
77-
version: ++document_version,
78-
},
79-
contentChanges: [{ text: buffer.text }],
80-
})
81-
.catch(logError);
82-
}
83-
84-
handler_ids = connect_signals(buffer, {
85-
"end-user-action": onUpdate,
86-
undo: onUpdate,
87-
redo: onUpdate,
88-
});
8964
}
9065
setupLSP().catch(logError);
9166

@@ -139,15 +114,25 @@ export function setup({ data_dir }) {
139114
}
140115

141116
return {
142-
async compile() {
117+
lspc,
118+
async update(text) {
119+
return lspc.notify("textDocument/didChange", {
120+
textDocument: {
121+
uri,
122+
version: ++document_version,
123+
},
124+
contentChanges: [{ text }],
125+
});
126+
},
127+
async compile(text) {
143128
await setupLSP();
144129

145130
await lspc.notify("textDocument/didChange", {
146131
textDocument: {
147132
uri,
148133
version: ++document_version,
149134
},
150-
contentChanges: [{ text: buffer.text }],
135+
contentChanges: [{ text }],
151136
});
152137

153138
const [{ xml }] = await once(

0 commit comments

Comments
 (0)