11import Gio from "gi://Gio" ;
22import GObject from "gi://GObject" ;
3- import GLib from "gi://GLib" ;
43import Gtk from "gi://Gtk" ;
54
6- import LSPClient from "./lsp/LSPClient.js" ;
75import { LSPError } from "./lsp/LSP.js" ;
86import {
97 getLanguage ,
@@ -12,17 +10,15 @@ import {
1210 disconnect_signals ,
1311 replaceBufferText ,
1412 unstack ,
15- handleDiagnostics ,
16- prepareSourceView ,
1713} from "./util.js" ;
1814
19- import { getPid , once } from "../troll/src/util.js" ;
20- import WorkbenchHoverProvider from "./WorkbenchHoverProvider.js" ;
15+ import {
16+ setup as setupBlueprint ,
17+ logBlueprintError ,
18+ } from "./langs/blueprint/blueprint.js" ;
2119
2220const { addSignalMethods } = imports . signals ;
2321
24- const SYSLOG_IDENTIFIER = pkg . name ;
25-
2622export default function PanelUI ( {
2723 application,
2824 builder,
@@ -38,19 +34,6 @@ export default function PanelUI({
3834
3935 const buffer_blueprint = getLanguage ( "blueprint" ) . document . buffer ;
4036 const buffer_xml = getLanguage ( "xml" ) . document . buffer ;
41- const provider = new WorkbenchHoverProvider ( ) ;
42-
43- const blueprint = createBlueprintClient ( {
44- data_dir,
45- buffer : buffer_blueprint ,
46- provider,
47- } ) ;
48-
49- let document_version = 0 ;
50- prepareSourceView ( {
51- source_view : getLanguage ( "blueprint" ) . document . source_view ,
52- provider,
53- } ) ;
5437
5538 const button_ui = builder . get_object ( "button_ui" ) ;
5639 const panel_ui = builder . get_object ( "panel_ui" ) ;
@@ -69,11 +52,13 @@ export default function PanelUI({
6952 // flat does nothing on GtkDropdown or GtkComboBox or GtkComboBoxText
7053 dropdown_ui_lang . get_first_child ( ) . get_style_context ( ) . add_class ( "flat" ) ;
7154
55+ const { compile, decompile } = setupBlueprint ( { data_dir } ) ;
56+
7257 async function convertToXML ( ) {
7358 term_console . clear ( ) ;
7459 settings . set_boolean ( "show-console" , true ) ;
7560
76- const xml = await compileBlueprint ( buffer_blueprint . text ) ;
61+ const xml = await compile ( ) ;
7762 replaceBufferText ( buffer_xml , xml ) ;
7863 settings . set_int ( "ui-language" , 0 ) ;
7964 }
@@ -89,7 +74,7 @@ export default function PanelUI({
8974 let blp ;
9075
9176 try {
92- blp = await decompileXML ( buffer_xml . text ) ;
77+ blp = await decompile ( buffer_xml . text ) ;
9378 } catch ( err ) {
9479 if ( err instanceof LSPError ) {
9580 logBlueprintError ( err ) ;
@@ -150,7 +135,7 @@ export default function PanelUI({
150135 if ( lang . id === "xml" ) {
151136 xml = lang . document . buffer . text ;
152137 } else {
153- xml = await compileBlueprint ( lang . document . buffer . text ) ;
138+ xml = await compile ( ) ;
154139 }
155140 panel . xml = xml || "" ;
156141 panel . emit ( "updated" ) ;
@@ -181,129 +166,10 @@ export default function PanelUI({
181166
182167 start ( ) ;
183168
184- const uri = "workbench://state.blp" ;
185-
186- async function setupLSP ( ) {
187- if ( blueprint . proc ) return ;
188- blueprint . start ( ) ;
189-
190- // https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#initialize
191- await blueprint . request ( "initialize" , {
192- processId : getPid ( ) ,
193- clientInfo : {
194- name : pkg . name ,
195- version : pkg . version ,
196- } ,
197- // https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#clientCapabilities
198- capabilities : {
199- textDocument : {
200- publishDiagnostics : { } ,
201- "x-blueprintcompiler/publishCompiled" : { } ,
202- } ,
203- } ,
204- locale : "en" ,
205- } ) ;
206-
207- await blueprint . notify ( "textDocument/didOpen" , {
208- textDocument : {
209- uri,
210- languageId : "blueprint" ,
211- version : ++ document_version ,
212- text : buffer_blueprint . text ,
213- } ,
214- } ) ;
215- }
216-
217- async function compileBlueprint ( ) {
218- await setupLSP ( ) ;
219-
220- await blueprint . notify ( "textDocument/didChange" , {
221- textDocument : {
222- uri,
223- version : ++ document_version ,
224- } ,
225- contentChanges : [ buffer_blueprint . text ] ,
226- } ) ;
227-
228- const [ { xml } ] = await once (
229- blueprint ,
230- "notification::textDocument/x-blueprintcompiler/publishCompiled" ,
231- ) ;
232-
233- return xml ;
234- }
235-
236- async function decompileXML ( text ) {
237- await setupLSP ( ) ;
238-
239- const { blp } = await blueprint . request ( "x-blueprintcompiler/decompile" , {
240- text,
241- } ) ;
242- return blp ;
243- }
244-
245169 panel . start = start ;
246170 panel . stop = stop ;
247171 panel . update = update ;
248172 panel . panel = panel_ui ;
249173
250174 return panel ;
251175}
252-
253- function logBlueprintError ( err ) {
254- GLib . log_structured ( "Blueprint" , GLib . LogLevelFlags . LEVEL_CRITICAL , {
255- MESSAGE : `${ err . message } ` ,
256- SYSLOG_IDENTIFIER ,
257- } ) ;
258- }
259-
260- // function logBlueprintInfo(info) {
261- // GLib.log_structured("Blueprint", GLib.LogLevelFlags.LEVEL_WARNING, {
262- // MESSAGE: `${info.line + 1}:${info.col} ${info.message}`,
263- // SYSLOG_IDENTIFIER,
264- // });
265- // }
266-
267- function createBlueprintClient ( { data_dir, buffer, provider } ) {
268- const file_blueprint_logs = Gio . File . new_for_path (
269- GLib . build_filenamev ( [ data_dir , "blueprint-logs" ] ) ,
270- ) ;
271- file_blueprint_logs . replace_contents (
272- " " ,
273- null ,
274- false ,
275- Gio . FileCreateFlags . REPLACE_DESTINATION ,
276- null ,
277- ) ;
278- const blueprint = new LSPClient ( [
279- // "/home/sonny/Projects/Workbench/blueprint-compiler/blueprint-compiler.py",
280- // "/app/bin/blueprint-compiler",
281- "blueprint-compiler" ,
282- "lsp" ,
283- "--logfile" ,
284- file_blueprint_logs . get_path ( ) ,
285- ] ) ;
286- blueprint . connect ( "exit" , ( ) => {
287- console . debug ( "blueprint exit" ) ;
288- } ) ;
289- blueprint . connect ( "output" , ( _self , message ) => {
290- console . debug ( `blueprint OUT:\n${ JSON . stringify ( message ) } ` ) ;
291- } ) ;
292- blueprint . connect ( "input" , ( _self , message ) => {
293- console . debug ( `blueprint IN:\n${ JSON . stringify ( message ) } ` ) ;
294- } ) ;
295-
296- blueprint . connect (
297- "notification::textDocument/publishDiagnostics" ,
298- ( _self , { diagnostics } ) => {
299- handleDiagnostics ( {
300- language : "Blueprint" ,
301- diagnostics,
302- buffer,
303- provider,
304- } ) ;
305- } ,
306- ) ;
307-
308- return blueprint ;
309- }
0 commit comments