Skip to content

Commit 99f1f81

Browse files
committed
Introduce CodeView
1 parent b164990 commit 99f1f81

File tree

18 files changed

+718
-837
lines changed

18 files changed

+718
-837
lines changed

src/Document.js

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
11
import Source from "gi://GtkSource?version=5";
22
import Gio from "gi://Gio";
33
import GLib from "gi://GLib";
4-
import { replaceBufferText } from "./util.js";
54
import { promiseTask } from "../troll/src/util.js";
65

7-
export default function Document({
8-
data_dir,
9-
source_view,
10-
lang,
11-
placeholder,
12-
ext,
13-
}) {
14-
const { buffer } = source_view;
6+
export default function Document({ code_view, placeholder, file }) {
7+
const { buffer } = code_view;
158
let handler_id = null;
169

17-
buffer.set_language(language_manager.get_language(lang));
18-
19-
const file = Gio.File.new_for_path(
20-
GLib.build_filenamev([data_dir, `state.${ext}`]),
21-
);
22-
2310
const source_file = new Source.File({
2411
location: file,
2512
});
2613

2714
loadSourceBuffer({ file: source_file, buffer })
2815
.then((success) => {
29-
if (!success) replaceBufferText(buffer, placeholder, true);
16+
if (!success) code_view.replaceText(placeholder, true);
3017
})
3118
.catch(logError);
3219
start();
@@ -50,7 +37,7 @@ export default function Document({
5037
}
5138
}
5239

53-
return { start, stop, save, source_view, buffer, file };
40+
return { start, stop, save, code_view, file };
5441
}
5542

5643
async function saveSourceBuffer({ file, buffer }) {
@@ -97,8 +84,3 @@ async function loadSourceBuffer({ file, buffer }) {
9784
}
9885
return success;
9986
}
100-
101-
const language_manager = new Source.LanguageManager();
102-
language_manager.append_search_path(
103-
"resource:///re/sonny/Workbench/language-specs",
104-
);

src/Library/demos/Custom widget/main.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import Gtk from "gi://Gtk?version=4.0";
33

44
Gtk.init();
55

6-
// rome-ignore lint(correctness/noUnusedVariables): https://github.com/rome/tools/issues/3779
76
const AwesomeButton = GObject.registerClass(
87
{
98
GTypeName: "AwesomeButton",

src/PanelCode.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ import { settings } from "./util.js";
66
import { setup as setupVala } from "./langs/vala/vala.js";
77
import { setup as setupJavaScript } from "./langs/javascript/javascript.js";
88

9-
export default function PanelCode({ builder, previewer, data_dir }) {
9+
export default function PanelCode({
10+
builder,
11+
previewer,
12+
data_dir,
13+
document_vala,
14+
document_javascript,
15+
}) {
1016
const panel_code = builder.get_object("panel_code");
1117
const button_code = builder.get_object("button_code");
1218
const stack_code = builder.get_object("stack_code");
@@ -41,8 +47,8 @@ export default function PanelCode({ builder, previewer, data_dir }) {
4147
panel: panel_code,
4248
};
4349

44-
setupVala({ data_dir });
45-
setupJavaScript({ data_dir });
50+
setupVala({ data_dir, document: document_vala });
51+
setupJavaScript({ data_dir, document: document_javascript });
4652

4753
function switchLanguage() {
4854
panel.language = dropdown_code_lang.selected_item.string;

src/PanelStyle.js

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,9 @@
11
import Gio from "gi://Gio";
22
import GObject from "gi://GObject";
3-
import Gtk from "gi://Gtk";
4-
import Pango from "gi://Pango";
53

6-
import {
7-
connect_signals,
8-
getLanguage,
9-
settings,
10-
getItersAtRange,
11-
} from "./util.js";
12-
13-
import WorkbenchHoverProvider from "./WorkbenchHoverProvider.js";
4+
import { settings } from "./util.js";
145

156
export default function PanelStyle({ builder }) {
16-
const buffer = getLanguage("css").document.buffer;
17-
const provider = new WorkbenchHoverProvider();
18-
19-
prepareSourceView({
20-
source_view: getLanguage("css").document.source_view,
21-
provider,
22-
});
23-
247
const button_style = builder.get_object("button_style");
258
const panel_style = builder.get_object("panel_style");
269
settings.bind(
@@ -35,50 +18,4 @@ export default function PanelStyle({ builder }) {
3518
"visible",
3619
GObject.BindingFlags.SYNC_CREATE,
3720
);
38-
39-
const panel = {
40-
panel: panel_style,
41-
};
42-
43-
function onUpdate() {
44-
panel.reset();
45-
}
46-
47-
connect_signals(buffer, {
48-
"end-user-action": onUpdate,
49-
undo: onUpdate,
50-
redo: onUpdate,
51-
});
52-
53-
panel.handleDiagnostic = function handleCssDiagnostic(diagnostic) {
54-
// console.debug(`CssParser ${section.to_string()} ${error.message}`);
55-
56-
provider.diagnostics.push(diagnostic);
57-
const [start_iter, end_iter] = getItersAtRange(buffer, diagnostic.range);
58-
buffer.apply_tag_by_name("error", start_iter, end_iter);
59-
};
60-
61-
panel.reset = function reset() {
62-
buffer.remove_tag_by_name(
63-
"error",
64-
buffer.get_start_iter(),
65-
buffer.get_end_iter(),
66-
);
67-
provider.diagnostics = [];
68-
};
69-
70-
return panel;
71-
}
72-
73-
function prepareSourceView({ source_view, provider }) {
74-
const tag_table = source_view.buffer.get_tag_table();
75-
const tag = new Gtk.TextTag({
76-
name: "error",
77-
underline: Pango.Underline.ERROR,
78-
});
79-
tag_table.add(tag);
80-
81-
const hover = source_view.get_hover();
82-
// hover.hover_delay = 25;
83-
hover.add_provider(provider);
8421
}

src/PanelUI.js

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ import GObject from "gi://GObject";
33
import Gtk from "gi://Gtk";
44

55
import { LSPError } from "./lsp/LSP.js";
6-
import {
7-
getLanguage,
8-
settings,
9-
connect_signals,
10-
disconnect_signals,
11-
replaceBufferText,
12-
unstack,
13-
listenProperty,
14-
} from "./util.js";
6+
import { getLanguage, settings, unstack, listenProperty } from "./util.js";
157
import { once } from "../troll/src/util.js";
168

179
import {
@@ -26,16 +18,21 @@ export default function PanelUI({
2618
builder,
2719
data_dir,
2820
term_console,
21+
document_xml,
22+
document_blueprint,
2923
}) {
3024
let lang;
3125

26+
const code_view_xml = document_xml.code_view;
27+
const code_view_blueprint = document_blueprint.code_view;
28+
3229
const panel = {
3330
xml: "",
3431
};
3532
addSignalMethods(panel);
3633

37-
const buffer_blueprint = getLanguage("blueprint").document.buffer;
38-
const buffer_xml = getLanguage("xml").document.buffer;
34+
const buffer_blueprint = code_view_blueprint.buffer;
35+
const buffer_xml = code_view_xml.buffer;
3936

4037
const button_ui = builder.get_object("button_ui");
4138
const panel_ui = builder.get_object("panel_ui");
@@ -56,14 +53,15 @@ export default function PanelUI({
5653

5754
const blueprint = setupBlueprint({
5855
data_dir,
56+
document: document_blueprint,
5957
});
6058

6159
async function convertToXML() {
6260
term_console.clear();
6361
settings.set_boolean("show-console", true);
6462

6563
const xml = await blueprint.compile(buffer_blueprint.text);
66-
replaceBufferText(buffer_xml, xml);
64+
code_view_xml.replaceText(xml);
6765
}
6866

6967
async function convertToBlueprint() {
@@ -82,7 +80,7 @@ export default function PanelUI({
8280
throw err;
8381
}
8482

85-
replaceBufferText(buffer_blueprint, blp);
83+
code_view_blueprint.replaceText(blp);
8684
}
8785

8886
const button_ui_experimental_blueprint = builder.get_object(
@@ -104,9 +102,8 @@ export default function PanelUI({
104102
);
105103
});
106104

107-
let handler_ids_xml = null;
108-
let handler_ids_blueprint = null;
109-
let handler_id_lsp = null;
105+
let handler_id_xml = null;
106+
let handler_id_blueprint = null;
110107

111108
// FIXME we should wait for previewer update instead
112109
// when loading demo
@@ -134,35 +131,25 @@ export default function PanelUI({
134131
function start() {
135132
stop();
136133
lang = getLanguage(dropdown_ui_lang.selected_item.string);
137-
// cannot use "changed" signal as it triggers many time for pasting
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),
142-
});
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(
134+
handler_id_xml = code_view_xml.connect(
135+
"changed",
136+
() => code_view_xml.buffer.text,
137+
);
138+
handler_id_blueprint = code_view_blueprint.connect("changed", onBlueprint);
139+
blueprint.lspc.connect(
149140
"notification::textDocument/x-blueprintcompiler/publishCompiled",
150141
(_self, { xml }) => onXML(xml),
151142
);
152143
}
153144

154145
function stop() {
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;
146+
if (handler_id_xml !== null) {
147+
code_view_xml.disconnect(handler_id_xml);
148+
handler_id_xml = null;
162149
}
163-
if (handler_id_lsp !== null) {
164-
blueprint.lspc.disconnect(handler_id_lsp);
165-
handler_id_lsp = null;
150+
if (handler_id_blueprint !== null) {
151+
code_view_blueprint.disconnect(handler_id_blueprint);
152+
handler_id_blueprint = null;
166153
}
167154
}
168155

src/Previewer/External.js

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import Adw from "gi://Adw?version=1";
22
import Gio from "gi://Gio";
33
import DBusPreviewer from "./DBusPreviewer.js";
44

5-
export default function Previewer({
6-
output,
7-
builder,
8-
onWindowChange,
9-
panel_style,
10-
}) {
5+
export default function Previewer({ output, builder, onWindowChange }) {
116
const stack = builder.get_object("stack_preview");
127

138
(function start_process() {
@@ -29,28 +24,11 @@ export default function Previewer({
2924
onWindowChange(open);
3025
});
3126

32-
dbus_proxy.connectSignal(
33-
"CssParserError",
34-
(
35-
_proxy,
36-
_name_owner,
37-
[message, start_line, start_char, end_line, end_char],
38-
) => {
39-
panel_style.handleDiagnostic({
40-
message,
41-
range: {
42-
start: {
43-
line: start_line,
44-
character: start_char,
45-
},
46-
end: {
47-
line: end_line,
48-
character: end_char,
49-
},
50-
},
51-
});
52-
},
53-
);
27+
dbus_proxy.connectSignal("CssParserError", (_proxy, _name_owner, error) => {
28+
builder
29+
.get_object("code_view_css")
30+
.handleDiagnostics([getCssDiagnostic(error)]);
31+
});
5432

5533
function start() {
5634
builder.get_object("button_screenshot").visible = false;
@@ -134,3 +112,27 @@ export default function Previewer({
134112
screenshot() {},
135113
};
136114
}
115+
116+
// Converts a CssParserError to an LSP diagnostic object
117+
function getCssDiagnostic([
118+
message,
119+
start_line,
120+
start_char,
121+
end_line,
122+
end_char,
123+
]) {
124+
return {
125+
message,
126+
range: {
127+
start: {
128+
line: start_line,
129+
character: start_char,
130+
},
131+
end: {
132+
line: end_line,
133+
character: end_char,
134+
},
135+
},
136+
severity: 1,
137+
};
138+
}

0 commit comments

Comments
 (0)