Skip to content

Commit 20e525f

Browse files
committed
Refactor document and format code logic
1 parent 77a1241 commit 20e525f

15 files changed

+319
-250
lines changed

src/Document.js

+30-21
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@ import Source from "gi://GtkSource";
22
import Gio from "gi://Gio";
33
import GLib from "gi://GLib";
44

5-
export default function Document({ session, code_view, lang }) {
6-
const { buffer } = code_view;
7-
let handler_id = null;
5+
export default class Document {
6+
handler_id = null;
87

9-
const file = session.file.get_child(lang.default_file);
10-
const source_file = new Source.File({
11-
location: file,
12-
});
8+
constructor({ session, code_view, lang }) {
9+
this.code_view = code_view;
10+
this.buffer = code_view.buffer;
11+
this.session = session;
12+
this.source_view = code_view.source_view;
13+
14+
const file = session.file.get_child(lang.default_file);
15+
this.file = file;
16+
this.source_file = new Source.File({
17+
location: file,
18+
});
1319

14-
start();
20+
this.start();
21+
}
1522

16-
function save() {
23+
save() {
24+
const { source_file, buffer, session } = this;
1725
saveSourceBuffer({ source_file, buffer })
1826
.catch(console.error)
1927
.finally(() => {
@@ -25,26 +33,27 @@ export default function Document({ session, code_view, lang }) {
2533
});
2634
}
2735

28-
function start() {
29-
stop();
30-
handler_id = buffer.connect("modified-changed", () => {
31-
if (!buffer.get_modified()) return;
32-
save();
36+
start() {
37+
this.stop();
38+
this.handler_id = this.buffer.connect("modified-changed", () => {
39+
if (!this.buffer.get_modified()) return;
40+
this.save();
3341
});
3442
}
3543

36-
function stop() {
37-
if (handler_id !== null) {
38-
buffer.disconnect(handler_id);
39-
handler_id = null;
44+
stop() {
45+
if (this.handler_id !== null) {
46+
this.buffer.disconnect(this.handler_id);
47+
this.handler_id = null;
4048
}
4149
}
4250

43-
function load() {
44-
return loadSourceBuffer({ source_file, buffer, lang });
51+
load() {
52+
const { source_file, buffer } = this;
53+
return loadSourceBuffer({ source_file, buffer });
4554
}
4655

47-
return { start, stop, save, code_view, file, load };
56+
format() {}
4857
}
4958

5059
async function saveSourceBuffer({ source_file, buffer }) {

src/PanelCode.js

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import Gio from "gi://Gio";
22
import GObject from "gi://GObject";
33

4-
import { setup as setupVala } from "./langs/vala/vala.js";
5-
import { setup as setupJavaScript } from "./langs/javascript/javascript.js";
64
import { settings as global_settings, makeDropdownFlat } from "./util.js";
75

8-
export default function PanelCode({
9-
builder,
10-
previewer,
11-
document_vala,
12-
document_javascript,
13-
settings,
14-
}) {
6+
export default function PanelCode({ builder, previewer, settings }) {
157
const panel_code = builder.get_object("panel_code");
168
const button_code = builder.get_object("button_code");
179
const stack_code = builder.get_object("stack_code");
@@ -51,10 +43,6 @@ export default function PanelCode({
5143
panel: panel_code,
5244
};
5345

54-
setupVala({ document: document_vala });
55-
const { format } = setupJavaScript({ document: document_javascript });
56-
document_javascript.format = format;
57-
5846
function switchLanguage() {
5947
panel.language = dropdown_code_lang.selected_item?.string;
6048
stack_code.visible_child_name = panel.language;

src/PanelUI.js

+5-14
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ import {
1010
makeDropdownFlat,
1111
} from "./util.js";
1212

13-
import {
14-
setup as setupBlueprint,
15-
logBlueprintError,
16-
} from "./langs/blueprint/blueprint.js";
13+
import { logBlueprintError } from "./langs/blueprint/blueprint.js";
1714

1815
// eslint-disable-next-line no-restricted-globals
1916
const { addSignalMethods } = imports.signals;
@@ -59,24 +56,19 @@ export default function PanelUI({
5956
makeDropdownFlat(dropdown_ui_lang);
6057
dropdown_ui_lang.set_selected(settings.get_enum("user-interface-language"));
6158

62-
const blueprint = setupBlueprint({
63-
document: document_blueprint,
64-
});
65-
6659
async function convertToXML() {
6760
term_console.clear();
6861
settings.set_boolean("show-console", true);
6962

70-
const xml = await blueprint.compile(buffer_blueprint.text);
63+
const xml = await document_blueprint.compile(buffer_blueprint.text);
7164
code_view_xml.replaceText(xml);
7265
}
7366

7467
async function convertToBlueprint() {
7568
term_console.clear();
7669
settings.set_boolean("show-console", true);
7770

78-
const blp = await blueprint.decompile(buffer_xml.text);
79-
71+
const blp = await document_blueprint.decompile(buffer_xml.text);
8072
code_view_blueprint.replaceText(blp);
8173
}
8274

@@ -106,7 +98,7 @@ export default function PanelUI({
10698
// when loading demo
10799
async function update() {
108100
if (lang === lang_blueprint) {
109-
onXML(await blueprint.compile());
101+
onXML(await document_blueprint.compile());
110102
} else if (lang === lang_xml) {
111103
onXML(buffer_xml.text);
112104
}
@@ -118,7 +110,7 @@ export default function PanelUI({
118110
}
119111

120112
const onBlueprint = unstack(function onBlueprint() {
121-
return blueprint.compile().then(onXML);
113+
return document_blueprint.compile().then(onXML);
122114
}, console.error);
123115

124116
function start() {
@@ -195,7 +187,6 @@ export default function PanelUI({
195187
panel.stop = stop;
196188
panel.update = update;
197189
panel.panel = panel_ui;
198-
panel.format = blueprint.format;
199190

200191
return panel;
201192
}
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import Document from "../../Document.js";
2+
import { applyTextEdits } from "../../lsp/sourceview.js";
3+
4+
import { setup as setupBlueprint } from "./blueprint.js";
5+
6+
export class BlueprintDocument extends Document {
7+
constructor(...args) {
8+
super(...args);
9+
10+
this.lspc = setupBlueprint({ document: this });
11+
}
12+
async update() {
13+
return this.lspc.didChange();
14+
}
15+
async compile() {
16+
await this.lspc.didChange();
17+
18+
let xml = null;
19+
20+
try {
21+
({ xml } = await this.lspc.request("textDocument/x-blueprint-compile", {
22+
textDocument: {
23+
uri: this.file.get_uri(),
24+
},
25+
}));
26+
} catch (err) {
27+
console.debug(err);
28+
}
29+
30+
return xml;
31+
}
32+
async decompile(text) {
33+
const { blp } = await this.lspc.request("x-blueprint/decompile", {
34+
text,
35+
});
36+
return blp;
37+
}
38+
async format() {
39+
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_formatting
40+
const text_edits = await this.lspc.request("textDocument/formatting", {
41+
textDocument: {
42+
uri: this.file.get_uri(),
43+
},
44+
options: {
45+
tabSize: 2,
46+
insertSpaces: true,
47+
trimTrailingWhitespace: true,
48+
insertFinalNewline: true,
49+
trimFinalNewlines: true,
50+
},
51+
});
52+
53+
applyTextEdits(text_edits, this.buffer);
54+
}
55+
}

src/langs/blueprint/blueprint.js

+1-47
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import GLib from "gi://GLib";
22

33
import LSPClient from "../../lsp/LSPClient.js";
4-
import { applyTextEdits } from "../../lsp/sourceview.js";
54

65
export function setup({ document }) {
76
const { file, code_view } = document;
@@ -13,52 +12,7 @@ export function setup({ document }) {
1312

1413
lspc.start().catch(console.error);
1514

16-
return {
17-
lspc,
18-
async update() {
19-
return lspc.didChange();
20-
},
21-
async compile() {
22-
await lspc.didChange();
23-
24-
let xml = null;
25-
26-
try {
27-
({ xml } = await lspc.request("textDocument/x-blueprint-compile", {
28-
textDocument: {
29-
uri: file.get_uri(),
30-
},
31-
}));
32-
} catch (err) {
33-
console.debug(err);
34-
}
35-
36-
return xml;
37-
},
38-
async decompile(text) {
39-
const { blp } = await lspc.request("x-blueprint/decompile", {
40-
text,
41-
});
42-
return blp;
43-
},
44-
async format() {
45-
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_formatting
46-
const text_edits = await lspc.request("textDocument/formatting", {
47-
textDocument: {
48-
uri: file.get_uri(),
49-
},
50-
options: {
51-
tabSize: 2,
52-
insertSpaces: true,
53-
trimTrailingWhitespace: true,
54-
insertFinalNewline: true,
55-
trimFinalNewlines: true,
56-
},
57-
});
58-
59-
applyTextEdits(text_edits, document.code_view.buffer);
60-
},
61-
};
15+
return lspc;
6216
}
6317

6418
const SYSLOG_IDENTIFIER = pkg.name;

src/langs/css/CssDocument.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { format as prettier } from "../../lib/prettier.js";
2+
import prettier_postcss from "../../lib/prettier-postcss.js";
3+
4+
import Document from "../../Document.js";
5+
6+
export class CssDocument extends Document {
7+
async format() {
8+
const code = await prettier(this.buffer.text, {
9+
parser: "css",
10+
plugins: [prettier_postcss],
11+
});
12+
this.code_view.replaceText(code, true);
13+
}
14+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { setup as setupJavaScript } from "./javascript.js";
2+
3+
import Document from "../../Document.js";
4+
import { applyTextEdits } from "../../lsp/sourceview.js";
5+
6+
export class JavaScriptDocument extends Document {
7+
constructor(...args) {
8+
super(...args);
9+
10+
this.lspc = setupJavaScript({ document: this });
11+
}
12+
13+
async format() {
14+
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_formatting
15+
const text_edits = await this.lspc.request("textDocument/formatting", {
16+
textDocument: {
17+
uri: this.file.get_uri(),
18+
},
19+
options: {
20+
tabSize: 2,
21+
insertSpaces: true,
22+
trimTrailingWhitespace: true,
23+
insertFinalNewline: true,
24+
trimFinalNewlines: true,
25+
},
26+
});
27+
28+
// Biome doesn't support diff - it just returns one edit
29+
// we don't want to loose the cursor position so we use this
30+
const state = this.code_view.saveState();
31+
applyTextEdits(text_edits, this.buffer);
32+
await this.code_view.restoreState(state);
33+
}
34+
}

src/langs/javascript/javascript.js

+1-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import GLib from "gi://GLib";
22

33
import LSPClient from "../../lsp/LSPClient.js";
4-
import { applyTextEdits } from "../../lsp/sourceview.js";
54

65
export function setup({ document }) {
76
const { file, code_view } = document;
@@ -18,26 +17,7 @@ export function setup({ document }) {
1817
lspc.didChange().catch(console.error);
1918
});
2019

21-
return {
22-
lspc,
23-
async format() {
24-
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_formatting
25-
const text_edits = await lspc.request("textDocument/formatting", {
26-
textDocument: {
27-
uri: file.get_uri(),
28-
},
29-
options: {
30-
tabSize: 2,
31-
insertSpaces: true,
32-
trimTrailingWhitespace: true,
33-
insertFinalNewline: true,
34-
trimFinalNewlines: true,
35-
},
36-
});
37-
38-
applyTextEdits(text_edits, document.code_view.buffer);
39-
},
40-
};
20+
return lspc;
4121
}
4222

4323
function createLSPClient({ file, code_view }) {

0 commit comments

Comments
 (0)