Repository navigation
Expand file tree
/
Copy pathtext_editor.py
More file actions
378 lines (327 loc) · 14.4 KB
/
Copy pathtext_editor.py
File metadata and controls
378 lines (327 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# SPDX-License-Identifier: BSD-3-Clause
"""Colorized ALSA Topology2 (.conf) Text Editor with live syntax highlighting and Diagram sync."""
from __future__ import annotations
import re
from typing import Callable, Optional, Tuple
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import Gdk, GLib, Gtk, Pango
# Pre-compiled regex patterns for ALSA Topology2 (.conf) syntax colorization
RE_BRACE = re.compile(r"[\{\}\[\]]")
RE_NUMBER = re.compile(
r"\b(?:0x[0-9A-Fa-f]+|[0-9a-fA-F]{2}(?::[0-9a-fA-F]{2}){7,15}|-?\d+(?:\.\d+)?)\b"
)
RE_KEYWORD = re.compile(
r"\b(?:Class|Object|DefineAttribute|Define|IncludeByKey|SubTreeCopy|CombineArrays|"
r"attributes|constraints|tree|source|sink|index|direction|instance|type|uuid|value|"
r"token_ref|name|rate|channels|formats|rates)\b"
)
RE_CATEGORY = re.compile(
r"\b(?:Widget|Pipeline|Control|Dai|PCM|Base|VendorToken|route|mixer|bytes|enum|"
r"input_audio_format|output_audio_format|hw_config|fe_dai|pcm_caps|tlv|scale|ops|"
r"data|manifest|virtual)\b"
)
RE_DIRECTIVE = re.compile(
r"(?:!(?:constructor|mandatory|immutable|deprecated|valid_values|tuple_values|route)\b|\bunique\b)"
)
RE_VARIABLE = re.compile(r"\$(?:\{[A-Za-z0-9_]+\}|[A-Za-z0-9_]+)")
RE_INCLUDE = re.compile(r"<[^>\n]+>")
RE_STRING = re.compile(r'"[^"\n]*"|\'[^\'\n]*\'')
RE_COMMENT = re.compile(r"/\*[\s\S]*?\*/|#[^\n]*")
RE_DOXY = re.compile(r"##[^\n]*")
def check_alsaconf_syntax_balance(text: str) -> Tuple[bool, Optional[int], str]:
"""Quick structural syntax check for balanced braces/brackets and quotes.
Returns:
(is_valid, error_line_1based, message)
"""
stack: list[Tuple[str, int]] = []
pairs = {"{": "}", "[": "]"}
closing = {"}": "{", "]": "["}
in_squote = False
in_dquote = False
in_comment = False
line_no = 1
i = 0
n = len(text)
while i < n:
ch = text[i]
if ch == "\n":
line_no += 1
in_comment = False
in_squote = False
in_dquote = False
i += 1
continue
if in_comment:
i += 1
continue
if not in_squote and not in_dquote:
if ch == "#":
in_comment = True
i += 1
continue
if ch == "/" and i + 1 < n and text[i + 1] == "*":
end_c = text.find("*/", i + 2)
if end_c == -1:
return (False, line_no, "Unclosed block comment '/*'")
line_no += text[i : end_c + 2].count("\n")
i = end_c + 2
continue
if ch == "'" and not in_dquote:
in_squote = not in_squote
i += 1
continue
if ch == '"' and not in_squote:
in_dquote = not in_dquote
i += 1
continue
if not in_squote and not in_dquote:
if ch in pairs:
stack.append((ch, line_no))
elif ch in closing:
if not stack:
return (False, line_no, f"Unexpected closing '{ch}' on line {line_no}")
top_ch, top_ln = stack.pop()
if top_ch != closing[ch]:
return (
False,
line_no,
f"Mismatched '{ch}' on line {line_no} (opened '{top_ch}' on line {top_ln})",
)
i += 1
if stack:
top_ch, top_ln = stack[-1]
return (False, top_ln, f"Unclosed '{top_ch}' opened on line {top_ln}")
return (True, None, "Valid Topology2 (.conf) syntax")
class TopologyTextEditor(Gtk.Box):
"""Colorized ALSA Topology2 (.conf) source editor widget with live Diagram synchronization."""
def __init__(
self,
on_text_committed: Optional[Callable[[str], Tuple[bool, str]]] = None,
):
super().__init__(orientation=Gtk.Orientation.VERTICAL, spacing=0)
self.set_vexpand(True)
self.set_hexpand(True)
self.on_text_committed = on_text_committed
self._suppress_change_signal: bool = False
self._highlight_timer_id: Optional[int] = None
self._sync_timer_id: Optional[int] = None
self.is_modified_in_text: bool = False
self._last_synced_text: str = ""
# 1. Top Editor Toolbar
toolbar = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=8)
toolbar.set_margin_start(8)
toolbar.set_margin_end(8)
toolbar.set_margin_top(4)
toolbar.set_margin_bottom(4)
self.file_lbl = Gtk.Label(label="Topology2 (.conf) Source", xalign=0.0)
self.file_lbl.add_css_class("heading")
self.file_lbl.set_ellipsize(Pango.EllipsizeMode.MIDDLE)
self.file_lbl.set_hexpand(True)
toolbar.append(self.file_lbl)
self.status_lbl = Gtk.Label(label="✔ Synced with Diagram", xalign=1.0)
self.status_lbl.add_css_class("monospace")
toolbar.append(self.status_lbl)
self.cursor_lbl = Gtk.Label(label="Ln 1, Col 1", xalign=1.0)
self.cursor_lbl.add_css_class("dim-label")
self.cursor_lbl.add_css_class("monospace")
toolbar.append(self.cursor_lbl)
self.apply_btn = Gtk.Button(label="⟳ Apply to Diagram")
self.apply_btn.set_tooltip_text(
"Parse edited .conf text and update the visual graph diagram immediately (Shortcut: Ctrl+Enter)"
)
self.apply_btn.connect("clicked", lambda _b: self.commit_to_diagram())
toolbar.append(self.apply_btn)
self.append(toolbar)
self.append(Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL))
# 2. Main Scrolled Editor Area (Line-number gutter + Monospace TextView)
editor_hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
editor_hbox.set_vexpand(True)
editor_hbox.set_hexpand(True)
self.scroll = Gtk.ScrolledWindow()
self.scroll.set_vexpand(True)
self.scroll.set_hexpand(True)
self.scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
self.text_view = Gtk.TextView()
self.text_view.set_monospace(True)
self.text_view.set_editable(True)
self.text_view.set_wrap_mode(Gtk.WrapMode.NONE)
self.text_view.set_left_margin(12)
self.text_view.set_right_margin(12)
self.text_view.set_top_margin(8)
self.text_view.set_bottom_margin(12)
self.buffer = self.text_view.get_buffer()
self._init_syntax_tags()
self.buffer.connect("changed", self._on_buffer_changed)
self.buffer.connect("notify::cursor-position", self._on_cursor_moved)
key_ctrl = Gtk.EventControllerKey.new()
key_ctrl.connect("key-pressed", self._on_key_pressed)
self.text_view.add_controller(key_ctrl)
self.scroll.set_child(self.text_view)
editor_hbox.append(self.scroll)
self.append(editor_hbox)
def _init_syntax_tags(self) -> None:
table = self.buffer.get_tag_table()
if table.lookup("syn_keyword") is not None:
return
self.buffer.create_tag("syn_brace", foreground="#ffd166", weight=Pango.Weight.BOLD)
self.buffer.create_tag("syn_number", foreground="#b5cea8")
self.buffer.create_tag("syn_keyword", foreground="#569cd6", weight=Pango.Weight.BOLD)
self.buffer.create_tag("syn_category", foreground="#c586c0", weight=Pango.Weight.BOLD)
self.buffer.create_tag("syn_directive", foreground="#ff79c6", weight=Pango.Weight.BOLD)
self.buffer.create_tag("syn_variable", foreground="#7ec8ff", weight=Pango.Weight.BOLD)
self.buffer.create_tag("syn_include", foreground="#ffb86c")
self.buffer.create_tag("syn_string", foreground="#e6db74")
self.buffer.create_tag("syn_comment", foreground="#6a9955", style=Pango.Style.ITALIC)
self.buffer.create_tag("syn_doxy", foreground="#4ec9b0", style=Pango.Style.ITALIC)
self.buffer.create_tag(
"syn_error_line",
background="#421c22",
underline=Pango.Underline.ERROR,
)
def set_text(self, text: str, header_title: str = "") -> None:
"""Populate the text editor without triggering a feedback loop to the diagram."""
self._cancel_timers()
self._suppress_change_signal = True
try:
self.buffer.set_text(text)
self._last_synced_text = text
self.is_modified_in_text = False
if header_title:
self.file_lbl.set_text(header_title)
self.apply_syntax_highlighting()
self.status_lbl.set_text("✔ Synced with Diagram")
finally:
self._suppress_change_signal = False
def get_text(self) -> str:
start_it = self.buffer.get_start_iter()
end_it = self.buffer.get_end_iter()
return self.buffer.get_text(start_it, end_it, True)
def scroll_to_hint(self, hint: Optional[str]) -> bool:
"""Move the cursor to the first occurrence of hint in the buffer and scroll it into view."""
if not hint:
return False
text = self.get_text()
if not text:
return False
idx = text.find(hint)
if idx < 0:
# Try unquoted fallback if hint had quotes
unquoted = hint.replace('"', "").replace("'", "")
if unquoted:
idx = text.find(unquoted)
if idx < 0:
return False
it = self.buffer.get_iter_at_offset(idx)
self.buffer.place_cursor(it)
self.text_view.scroll_to_iter(it, 0.15, True, 0.0, 0.25)
return True
def _cancel_timers(self) -> None:
if self._highlight_timer_id is not None:
GLib.source_remove(self._highlight_timer_id)
self._highlight_timer_id = None
if self._sync_timer_id is not None:
GLib.source_remove(self._sync_timer_id)
self._sync_timer_id = None
def _on_key_pressed(
self,
_ctrl: Gtk.EventControllerKey,
keyval: int,
_keycode: int,
state: Gdk.ModifierType,
) -> bool:
ctrl_held = bool(state & Gdk.ModifierType.CONTROL_MASK)
if ctrl_held and keyval in (Gdk.KEY_Return, Gdk.KEY_KP_Enter):
self.commit_to_diagram()
return True
return False
def _on_cursor_moved(self, *_args: object) -> None:
mark = self.buffer.get_insert()
it = self.buffer.get_iter_at_mark(mark)
ln = it.get_line() + 1
col = it.get_line_offset() + 1
self.cursor_lbl.set_text(f"Ln {ln}, Col {col}")
def _on_buffer_changed(self, _buf: Gtk.TextBuffer) -> None:
if self._suppress_change_signal:
return
self.is_modified_in_text = True
self.status_lbl.set_text("✎ Editing text…")
if self._highlight_timer_id is not None:
GLib.source_remove(self._highlight_timer_id)
self._highlight_timer_id = GLib.timeout_add(90, self._on_debounced_highlight)
if self._sync_timer_id is not None:
GLib.source_remove(self._sync_timer_id)
self._sync_timer_id = GLib.timeout_add(480, self._on_debounced_sync)
def _on_debounced_highlight(self) -> bool:
self._highlight_timer_id = None
self.apply_syntax_highlighting()
return False
def _on_debounced_sync(self) -> bool:
self._sync_timer_id = None
self.commit_to_diagram()
return False
def commit_to_diagram(self) -> bool:
"""Validate syntax and invoke on_text_committed to update the visual graph diagram."""
if self._sync_timer_id is not None:
GLib.source_remove(self._sync_timer_id)
self._sync_timer_id = None
text = self.get_text()
self.apply_syntax_highlighting()
ok_syn, err_line, syn_msg = check_alsaconf_syntax_balance(text)
if not ok_syn:
if err_line is not None:
self._highlight_error_line(err_line)
self.status_lbl.set_text(f"⚠ {syn_msg}")
return False
if text == self._last_synced_text:
self.is_modified_in_text = False
self.status_lbl.set_text("✔ Synced with Diagram")
return True
if self.on_text_committed is not None:
ok, msg = self.on_text_committed(text)
if ok:
self._last_synced_text = text
self.is_modified_in_text = False
self.status_lbl.set_text(f"✔ {msg}")
return True
else:
self.status_lbl.set_text(f"⚠ {msg}")
return False
return True
def _highlight_error_line(self, line_1based: int) -> None:
ok, s_it = self.buffer.get_iter_at_line(max(0, line_1based - 1))
if not ok:
return
e_it = s_it.copy()
if not e_it.ends_line():
e_it.forward_to_line_end()
self.buffer.apply_tag_by_name("syn_error_line", s_it, e_it)
def apply_syntax_highlighting(self) -> None:
"""Apply ALSA Topology2 (.conf) syntax colorization tags across the buffer."""
start_it = self.buffer.get_start_iter()
end_it = self.buffer.get_end_iter()
self.buffer.remove_all_tags(start_it, end_it)
text = self.buffer.get_text(start_it, end_it, True)
if not text:
return
# Convert byte/char offsets using Gtk.TextBuffer iter_at_offset (char-based in Python)
def _apply_pattern(regex: re.Pattern, tag_name: str, clear_under: bool = False) -> None:
for m in regex.finditer(text):
s_idx, e_idx = m.start(), m.end()
s_it = self.buffer.get_iter_at_offset(s_idx)
e_it = self.buffer.get_iter_at_offset(e_idx)
if clear_under:
self.buffer.remove_all_tags(s_it, e_it)
self.buffer.apply_tag_by_name(tag_name, s_it, e_it)
_apply_pattern(RE_BRACE, "syn_brace")
_apply_pattern(RE_NUMBER, "syn_number")
_apply_pattern(RE_KEYWORD, "syn_keyword")
_apply_pattern(RE_CATEGORY, "syn_category")
_apply_pattern(RE_DIRECTIVE, "syn_directive")
_apply_pattern(RE_VARIABLE, "syn_variable")
_apply_pattern(RE_INCLUDE, "syn_include", clear_under=True)
_apply_pattern(RE_STRING, "syn_string", clear_under=True)
# Re-apply variable tags inside strings like '$index' or "$PLATFORM"
_apply_pattern(RE_VARIABLE, "syn_variable", clear_under=False)
_apply_pattern(RE_COMMENT, "syn_comment", clear_under=True)
_apply_pattern(RE_DOXY, "syn_doxy", clear_under=True)