-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.zig
More file actions
2238 lines (1990 loc) · 82.1 KB
/
Copy pathprotocol.zig
File metadata and controls
2238 lines (1990 loc) · 82.1 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const std = @import("std");
const vaxis = @import("vaxis");
const safe = @import("safe.zig");
const hover_doc = @import("../services/hover_doc.zig");
pub const Mode = enum {
select,
insert,
visual,
visual_search,
view,
terminal,
file_picker,
/// Tree-shaped, modal sidebar of the current working
/// directory. Open via `Space e`. Navigate with j/k or arrows;
/// h/l (or Left/Right) collapse/expand directories; Enter on
/// a file opens it and dismisses the explorer. Esc dismisses.
file_explorer,
buffer_picker,
save_as_mode,
command_palette,
go_to_line,
symbol_picker,
workspace_symbol_picker,
log_view,
global_search,
/// LSP textDocument/references results, rendered as a modal
/// list. j/k navigates entries; Enter opens the file at the
/// match (recording a jump); Esc dismisses and returns the
/// cursor to the trigger position. Replaces the earlier
/// "open a flat text buffer" UX so users no longer have to
/// hunt back through buffer history after closing it.
references_picker,
/// Same shape for LSP diagnostics — j/k navigates the buffer's
/// errors / warnings; Enter jumps to the diagnostic; Esc
/// returns to the trigger position.
diagnostics_picker,
};
pub const DirEntry = struct {
name: []const u8,
is_dir: bool,
};
/// One row in the file-explorer's flattened visible tree. The
/// builder walks the cwd once per render and emits an
/// `ExplorerEntry` per visible row (directories collapse their
/// children when not expanded), so the renderer is a straight
/// list iteration — no tree traversal at draw time.
pub const ExplorerEntry = struct {
/// Display name (basename only, not the full path).
name: []const u8,
/// Full path from the explorer root, so opening / expanding
/// doesn't have to reconstruct it.
path: []const u8,
/// 0 = direct child of the root.
depth: u16,
is_dir: bool,
/// Only meaningful when `is_dir` is true.
is_expanded: bool,
};
/// One entry in the references picker. Snapshot-only (the per-frame
/// arena owns the strings); the underlying buffered list in Core
/// keeps the durable copies.
pub const ReferenceEntry = struct {
/// Absolute path used to open the file when the user picks it.
full_path: []const u8,
/// Basename for display in the picker.
display_path: []const u8,
/// 0-based line / col of the reference inside the target file.
line: u32,
col: u32,
/// Trimmed source-line preview (or "(unable to read)").
snippet: []const u8,
};
pub const DiagnosticPickerEntry = struct {
line: u32,
col: u32,
severity: DiagnosticSeverity,
message: []const u8,
};
pub const RenderParams = struct {
rows: usize,
cols: usize,
};
pub const PaneBound = struct {
pane: struct {
id: u32,
buffer_index: usize,
},
x: usize,
y: usize,
width: usize,
height: usize,
};
pub const SyntaxToken = struct {
line: u32,
start_col: u32,
length: u32,
token_type: TokenType,
pub const TokenType = enum {
keyword,
function,
variable,
parameter,
property,
type_name,
string,
number,
comment,
operator,
builtin,
namespace,
other,
bracket_1,
bracket_2,
bracket_3,
bracket_4,
bracket_5,
bracket_6,
scope_bracket,
};
};
pub const CommandEntry = struct {
id: []const u8,
title: []const u8,
description: []const u8,
};
pub const SymbolEntry = struct {
name: []const u8,
kind: []const u8,
line: usize,
};
pub const WorkspaceSymbolEntry = struct {
name: []const u8,
kind: []const u8,
/// Absolute path of the file the symbol lives in. Renderer
/// truncates this to the basename for display.
file_path: []const u8,
line: usize,
col: usize,
};
pub const PluginInfo = struct {
id: []const u8,
name: []const u8,
description: []const u8,
uptime_s: u64,
widget_count: u32,
is_running: bool,
};
pub const BufferPresentation = enum(u8) {
/// Normal editable text buffer: draw line numbers, cursor, syntax, etc.
text = 0,
/// Read-only report/dashboard content authored as markdown.
markdown_view = 1,
};
pub const BufferInfo = struct {
id: u32,
name: []const u8,
modified: bool,
is_active: bool,
/// True when the buffer crossed the configured large-file
/// thresholds at load time. Tabs render a `[L]` badge so the user
/// knows tree-sitter / LSP / brackets are intentionally off.
is_large: bool = false,
presentation: BufferPresentation = .text,
};
pub const LogEntry = struct {
timestamp: i64,
level: u8,
scope: []const u8,
message: []const u8,
};
pub const DiagnosticSeverity = enum(u8) { err, warning, info, hint };
/// Single inlay hint as the renderer sees it. Position is buffer-
/// local (0-based line/col); label is the flattened text, kind is
/// 1 = type, 2 = parameter, null = unspecified.
pub const InlayHintSnapshot = struct {
line: u32,
col: u32,
label: []const u8,
kind: ?u8 = null,
padding_left: bool = false,
padding_right: bool = false,
};
/// Snapshot of an LSP diagnostic for the currently visible buffer. Positions
/// are buffer-local (0-based line/col, same convention as cursor_row/col).
pub const DiagnosticSnapshot = struct {
start_line: u32,
start_col: u32,
end_line: u32,
end_col: u32,
severity: DiagnosticSeverity,
message: []const u8,
};
pub const GlobalSearchMatch = struct {
line_num: usize,
line_content: []const u8,
match_start: usize,
match_end: usize,
};
pub const GlobalSearchFileGroup = struct {
file_path: []const u8,
matches: []const GlobalSearchMatch,
collapsed: bool,
};
pub const GlobalSearchOptions = struct {
case_sensitive: bool = false,
whole_word: bool = false,
use_regex: bool = false,
};
pub const WidgetID = u64;
pub const StatusAlignment = enum(u8) {
left = 0,
center = 1,
right = 2,
};
pub const StatusItem = struct {
id: []const u8,
plugin_id: []const u8,
text: []const u8,
alignment: StatusAlignment,
priority: i8,
widget_id: WidgetID,
};
pub const PanelPosition = enum(u8) {
left = 0,
right = 1,
bottom = 2,
};
pub const PanelInfo = struct {
id: []const u8,
plugin_id: []const u8,
title: []const u8,
content: []const []const u8,
position: PanelPosition,
width_percent: u8,
widget_id: WidgetID,
scroll_offset: u32 = 0,
};
pub const EditorStateView = struct {
file_path: ?[]const u8,
file_modified: bool,
cursor_row: usize,
cursor_col: usize,
mode: Mode,
buffer_id: u32,
buffer_name: []const u8,
total_lines: usize,
selection_start_row: ?usize,
selection_start_col: ?usize,
selection_end_row: ?usize,
selection_end_col: ?usize,
};
pub const PluginEvent = enum(u8) {
buffer_changed = 0,
cursor_moved = 1,
mode_changed = 2,
file_opened = 3,
file_saved = 4,
buffer_switched = 5,
custom_event = 6,
};
pub const NotificationLevel = enum(u8) {
info = 0,
warning = 1,
err = 2,
};
/// Visual treatment for transient `status_message` toasts. Drives the
/// icon and colour chosen by `status_bar.zig` so a save success and a
/// save failure don't both render as a green ✓.
pub const StatusLevel = enum(u8) {
info = 0,
success = 1,
warning = 2,
err = 3,
};
pub const CompletionEntry = struct {
label: []const u8,
kind: []const u8,
detail: []const u8,
/// Category of the completion item, used by the view to pick a color
/// for the kind glyph. Maps from LSP CompletionItemKind.
kind_category: KindCategory = .other,
pub const KindCategory = enum {
function, // function, method, constructor
variable, // variable, parameter
field, // field, property
type_, // class, interface, struct, enum, type_parameter
module, // module, namespace, folder
keyword, // keyword
value, // value, enumMember, constant
snippet,
text,
other,
};
};
pub const SplitDirection = enum {
horizontal,
vertical,
};
pub const EditorConfigSnapshot = struct {
tab_size: u32 = 4,
insert_spaces: bool = true,
line_numbers: LineNumbersMode = .relative,
wrap: bool = false,
show_status_bar: bool = true,
cursor_line: bool = true,
/// Mirrors `EditorConfig.inline_diagnostics`. When true the view
/// renders every line's worst diagnostic at end-of-line, not just
/// the cursor's line.
inline_diagnostics: bool = true,
/// Mirrors `EditorConfig.inlay_hints`. When true the view paints
/// LSP inlay hints as dim virtual text inline.
inlay_hints: bool = false,
pub const LineNumbersMode = enum { absolute, relative, none };
};
pub const PaneSnapshot = struct {
id: u32,
buffer_index: usize,
presentation: BufferPresentation = .text,
is_focused: bool,
x: f32,
y: f32,
width: f32,
height: f32,
cursor_row: usize,
cursor_col: usize,
scroll_offset: usize,
selection_anchor_row: ?usize,
selection_anchor_col: ?usize,
visible_lines: []const []const u8,
syntax_tokens: ?[]const SyntaxToken,
total_lines: usize,
diff_highlights: ?[]const DiffLineHighlight = null,
};
pub const DiffLineHighlight = struct {
line: usize,
kind: DiffKind,
pub const DiffKind = enum { added, deleted, changed };
};
pub const RenderSnapshot = struct {
visible_lines: []const []const u8,
first_visible_line: usize,
total_lines: usize,
cursor_row: usize,
cursor_col: usize,
nav_repeat_count: usize = 0,
selection_anchor_row: ?usize = null,
selection_anchor_col: ?usize = null,
scroll_offset: usize,
version: u64,
mode: Mode,
terminal_output: ?[]const u8,
terminal_input: ?[]const u8,
terminal_cwd: ?[]const u8 = null,
terminal_scroll_offset: usize = 0,
terminal_running: bool = false,
file_path: ?[]const u8,
file_modified: bool,
buffers: []const BufferInfo,
active_buffer_index: usize,
buffer_picker_selected: usize,
file_picker_cwd: ?[]const u8,
file_picker_entries: ?[]const DirEntry,
file_picker_selected: usize,
/// File-explorer state. Each entry already carries its depth
/// and is_dir / is_expanded flags so the view doesn't have to
/// walk the tree. The list is the visible flattening of the
/// expanded tree, top-to-bottom; `file_explorer_selected` is
/// an index into it.
file_explorer_cwd: ?[]const u8 = null,
file_explorer_entries: ?[]const ExplorerEntry = null,
file_explorer_selected: usize = 0,
file_explorer_scroll_offset: usize = 0,
buffer_picker_scroll_offset: usize,
buffer_picker_number_input: ?[]const u8 = null,
save_as_input: ?[]const u8,
search_input: ?[]const u8,
/// `/` (forward) or `?` (backward), shown in the search prompt
/// title so the user knows which direction n/N will step.
search_direction_forward: bool = true,
/// Total matches in the current buffer for the live search query
/// and the 1-based index of the active one (cursor sits on it
/// during incremental search). 0 / 0 = unknown or no matches.
search_match_count: usize = 0,
search_match_index: usize = 0,
syntax_tokens: ?[]const SyntaxToken = null,
hover_content: ?[]const u8 = null,
/// Parsed-and-cloned hover document. Lives in the same arena as
/// the rest of the snapshot, freed when the snapshot is. Null
/// when there's no hover (or the parse failed and we fell back
/// to the raw `hover_content`).
hover_document: ?hover_doc.HoverDocument = null,
/// Anchor cell — token start, not the cursor — used by the
/// renderer to position the popup. Buffer-space coordinates;
/// view converts to screen.
hover_anchor_row: usize = 0,
hover_anchor_col: usize = 0,
/// Per-section scroll offset for the popup. The signature panel
/// is never scrolled; only the body sections move.
hover_scroll_offset: usize = 0,
/// True for hover triggered by `Space h` (sticky — Esc dismisses,
/// scroll keys scroll). False for idle-timer auto-hover
/// (dismissed by any keypress / cursor move).
hover_sticky: bool = false,
/// LSP has been asked, no response yet, and enough time has
/// elapsed that we want to show a "Loading…" affordance. The
/// 150 ms grace period keeps fast hovers from flickering.
hover_loading: bool = false,
/// Which-key panel: surfaces every available leader binding once
/// the Space leader has been held idle for a short delay. `null`
/// when the popup isn't currently visible.
which_key_visible: bool = false,
/// When the user has entered a chord prefix (`l` / `g` / `w` /
/// `t`), this is the prefix byte so the popup can show that
/// chord's sub-bindings instead of the top-level catalogue.
/// null when no chord is pending.
leader_chord: ?u8 = null,
/// True while the leader-chord gate is open (post-Space,
/// pre-action). Distinct from `which_key_visible`, which
/// reflects the *popup* state. The status bar uses this to
/// render a persistent `SPC▸` badge so the user has
/// unambiguous, frame-independent feedback that the chord is
/// armed — separate from the transient toast that lives on
/// the next render frame.
leader_pending: bool = false,
command_palette_query: ?[]const u8 = null,
command_palette_results: ?[]const CommandEntry = null,
command_palette_selected: usize = 0,
go_to_line_input: ?[]const u8 = null,
symbol_picker_query: ?[]const u8 = null,
symbol_picker_results: ?[]const SymbolEntry = null,
symbol_picker_selected: usize = 0,
workspace_symbol_query: ?[]const u8 = null,
workspace_symbol_results: ?[]const WorkspaceSymbolEntry = null,
workspace_symbol_selected: usize = 0,
workspace_symbol_pending: bool = false,
completion_active: bool = false,
completion_items: ?[]const CompletionEntry = null,
completion_selected: usize = 0,
/// LSP signature-help popup contents. Non-null only when the
/// signature_help slot in Core holds a parsed response (i.e. the
/// user just typed `(` or `,` and the server replied). The
/// renderer drops a one-line overlay above the cursor.
signature_help_label: ?[]const u8 = null,
signature_help_active_parameter: u32 = 0,
/// Slices into `signature_help_label` are *not* duped — the
/// renderer only needs the param-label list to compute where to
/// bold/underline. Param labels are owned by the snapshot arena.
signature_help_parameters: ?[]const []const u8 = null,
/// Inlay hints for the current viewport — point, label, kind.
/// Owned by the snapshot arena (cloned in `clone`).
inlay_hints: ?[]const InlayHintSnapshot = null,
split_enabled: bool = false,
panes: []const PaneSnapshot = &.{},
focused_pane_id: u32 = 0,
plugin_count: usize = 0,
plugin_status_items: []const StatusItem = &.{},
plugin_panels: []const PanelInfo = &.{},
lsp_status: ?[]const u8 = null,
logs: ?[]const LogEntry = null,
editor_config: EditorConfigSnapshot = .{},
global_search_query: ?[]const u8 = null,
global_search_replace: ?[]const u8 = null,
global_search_results: ?[]const GlobalSearchFileGroup = null,
global_search_total_matches: usize = 0,
global_search_total_files: usize = 0,
global_search_selected_file: usize = 0,
global_search_selected_match: usize = 0,
global_search_focus_replace: bool = false,
global_search_options: GlobalSearchOptions = .{},
global_search_in_progress: bool = false,
/// True once a search has actually run since the panel was opened. Used
/// to distinguish the empty initial state from "0 matches".
global_search_ran: bool = false,
/// References picker payload — present when mode ==
/// .references_picker. Each entry is a flattened LSP location
/// with its file basename, line/col, and a short source-line
/// preview. `selected` is an index into the slice.
references_entries: ?[]const ReferenceEntry = null,
references_selected: usize = 0,
references_scroll_offset: usize = 0,
references_symbol: ?[]const u8 = null,
/// Diagnostics picker payload — same shape for mode ==
/// .diagnostics_picker. Filtered to the active buffer.
diagnostics_entries: ?[]const DiagnosticPickerEntry = null,
diagnostics_picker_selected: usize = 0,
diagnostics_picker_scroll_offset: usize = 0,
diff_highlight_lines: ?[]const DiffLineHighlight = null,
/// Diagnostics for the currently visible buffer, sorted by line.
diagnostics: ?[]const DiagnosticSnapshot = null,
diagnostic_error_count: u32 = 0,
diagnostic_warning_count: u32 = 0,
/// Current git branch name, if the working dir is in a git repo.
git_branch: ?[]const u8 = null,
/// Number of jobs currently running in the JobManager.
active_job_count: u32 = 0,
status_message: ?[]const u8 = null,
status_message_level: StatusLevel = .success,
// NOTE: a deep-clone method previously lived here. It was never used in
// production (snapshots are passed by pointer alongside their arena;
// see main.zig render_update handling) and ~180 LOC of dupe-everything
// code is a maintenance liability. If you genuinely need to outlive
// the per-frame arena, take a pointer + arena pair the same way the
// bus does, or pick the specific fields you need rather than reviving
// a clone-everything path.
};
pub const TerminalResult = struct {
output: []const u8,
exit_code: i32,
success: bool,
};
pub const RenderUpdateMessage = struct {
snapshot_ptr: usize,
arena_ptr: usize,
/// Pointer to the `ArenaPool` that produced `arena_ptr`. The receiver
/// returns the arena via `pool.release(arena)` instead of deiniting it.
pool_ptr: usize,
};
pub const Message = union(enum) {
input: vaxis.Key,
mouse: vaxis.Mouse,
command: Command,
render_update: RenderUpdateMessage,
resize: vaxis.Winsize,
mode_change: Mode,
terminal_execute: []const u8,
terminal_output_chunk: []const u8,
terminal_result: TerminalResult,
quit,
tick,
plugin_message: PluginMessage,
focus: bool,
const TAG_INPUT: u8 = 0;
const TAG_MOUSE: u8 = 1;
const TAG_COMMAND: u8 = 2;
const TAG_RENDER_UPDATE: u8 = 3;
const TAG_RESIZE: u8 = 4;
const TAG_MODE_CHANGE: u8 = 5;
const TAG_TERMINAL_EXECUTE: u8 = 6;
const TAG_TERMINAL_OUTPUT: u8 = 7;
const TAG_TERMINAL_RESULT: u8 = 8;
pub const TAG_QUIT: u8 = 9;
const TAG_TICK: u8 = 10;
pub const TAG_PLUGIN_MSG: u8 = 11;
const TAG_FOCUS: u8 = 12;
pub fn encode(self: Message, allocator: std.mem.Allocator) ![]u8 {
switch (self) {
.plugin_message => |pm| {
return pm.encode(allocator);
},
.focus => |f| {
const buf = try allocator.alloc(u8, 2);
buf[0] = TAG_FOCUS;
buf[1] = if (f) 1 else 0;
return buf;
},
.input => |key| {
const text_len: u8 = if (key.text) |t| @intCast(@min(t.len, 255)) else 0;
const buf = try allocator.alloc(u8, 7 + text_len);
buf[0] = TAG_INPUT;
const cp: u32 = @intCast(key.codepoint);
buf[1] = @truncate(cp >> 24);
buf[2] = @truncate(cp >> 16);
buf[3] = @truncate(cp >> 8);
buf[4] = @truncate(cp);
var mods: u8 = 0;
if (key.mods.shift) mods |= 0x01;
if (key.mods.alt) mods |= 0x02;
if (key.mods.ctrl) mods |= 0x04;
if (key.mods.super) mods |= 0x08;
buf[5] = mods;
buf[6] = text_len;
if (key.text) |t| {
@memcpy(buf[7..][0..text_len], t[0..text_len]);
}
return buf;
},
.mouse => |m| {
const buf = try allocator.alloc(u8, 8);
buf[0] = TAG_MOUSE;
const col_u: u16 = @bitCast(m.col);
const row_u: u16 = @bitCast(m.row);
buf[1] = @truncate(col_u >> 8);
buf[2] = @truncate(col_u);
buf[3] = @truncate(row_u >> 8);
buf[4] = @truncate(row_u);
buf[5] = @intFromEnum(m.button);
buf[6] = @intFromEnum(m.type);
var mods: u8 = 0;
if (m.mods.shift) mods |= 0x01;
if (m.mods.alt) mods |= 0x02;
if (m.mods.ctrl) mods |= 0x04;
buf[7] = mods;
return buf;
},
.command => |cmd| {
const buf = try allocator.alloc(u8, 2);
buf[0] = TAG_COMMAND;
buf[1] = @intFromEnum(cmd);
return buf;
},
.render_update => |ru| {
const buf = try allocator.alloc(u8, 25);
buf[0] = TAG_RENDER_UPDATE;
const sp: u64 = @intCast(ru.snapshot_ptr);
const ap: u64 = @intCast(ru.arena_ptr);
const pp: u64 = @intCast(ru.pool_ptr);
inline for (0..8) |i| {
buf[1 + i] = @truncate(sp >> @intCast((7 - i) * 8));
buf[9 + i] = @truncate(ap >> @intCast((7 - i) * 8));
buf[17 + i] = @truncate(pp >> @intCast((7 - i) * 8));
}
return buf;
},
.resize => |ws| {
const buf = try allocator.alloc(u8, 5);
buf[0] = TAG_RESIZE;
buf[1] = @truncate(ws.rows >> 8);
buf[2] = @truncate(ws.rows);
buf[3] = @truncate(ws.cols >> 8);
buf[4] = @truncate(ws.cols);
return buf;
},
.mode_change => |mode| {
const buf = try allocator.alloc(u8, 2);
buf[0] = TAG_MODE_CHANGE;
buf[1] = @intFromEnum(mode);
return buf;
},
.terminal_execute => |text| {
const buf = try allocator.alloc(u8, 5 + text.len);
buf[0] = TAG_TERMINAL_EXECUTE;
const len: u32 = @intCast(text.len);
buf[1] = @truncate(len >> 24);
buf[2] = @truncate(len >> 16);
buf[3] = @truncate(len >> 8);
buf[4] = @truncate(len);
@memcpy(buf[5..], text);
return buf;
},
.terminal_output_chunk => |text| {
const buf = try allocator.alloc(u8, 5 + text.len);
buf[0] = TAG_TERMINAL_OUTPUT;
const len: u32 = @intCast(text.len);
buf[1] = @truncate(len >> 24);
buf[2] = @truncate(len >> 16);
buf[3] = @truncate(len >> 8);
buf[4] = @truncate(len);
@memcpy(buf[5..], text);
return buf;
},
.terminal_result => |tr| {
const buf = try allocator.alloc(u8, 10 + tr.output.len);
buf[0] = TAG_TERMINAL_RESULT;
const ec: u32 = @bitCast(tr.exit_code);
buf[1] = @truncate(ec >> 24);
buf[2] = @truncate(ec >> 16);
buf[3] = @truncate(ec >> 8);
buf[4] = @truncate(ec);
buf[5] = if (tr.success) 1 else 0;
const len: u32 = @intCast(tr.output.len);
buf[6] = @truncate(len >> 24);
buf[7] = @truncate(len >> 16);
buf[8] = @truncate(len >> 8);
buf[9] = @truncate(len);
@memcpy(buf[10..], tr.output);
return buf;
},
.quit => {
const buf = try allocator.alloc(u8, 1);
buf[0] = TAG_QUIT;
return buf;
},
.tick => {
const buf = try allocator.alloc(u8, 1);
buf[0] = TAG_TICK;
return buf;
},
}
}
pub fn decode(bytes: []const u8) !Message {
if (bytes.len == 0) return error.EmptyMessage;
const tag = bytes[0];
switch (tag) {
TAG_INPUT => {
if (bytes.len < 7) return error.InvalidMessage;
const cp: u32 = @as(u32, bytes[1]) << 24 | @as(u32, bytes[2]) << 16 | @as(u32, bytes[3]) << 8 | bytes[4];
const mods_byte = bytes[5];
const text_len = bytes[6];
const text: ?[]const u8 = if (text_len > 0 and bytes.len >= 7 + text_len)
bytes[7..][0..text_len]
else
null;
const codepoint = safe.intToCodepoint(cp) orelse return error.InvalidMessage;
return .{ .input = .{
.codepoint = codepoint,
.mods = .{
.shift = mods_byte & 0x01 != 0,
.alt = mods_byte & 0x02 != 0,
.ctrl = mods_byte & 0x04 != 0,
.super = mods_byte & 0x08 != 0,
},
.text = text,
} };
},
TAG_MOUSE => {
if (bytes.len < 8) return error.InvalidMessage;
const col_u: u16 = @as(u16, bytes[1]) << 8 | bytes[2];
const row_u: u16 = @as(u16, bytes[3]) << 8 | bytes[4];
const mods_byte = bytes[7];
const button = safe.intToEnum(vaxis.Mouse.Button, bytes[5]) orelse return error.InvalidMessage;
const mouse_type = safe.intToEnum(vaxis.Mouse.Type, bytes[6]) orelse return error.InvalidMessage;
return .{ .mouse = .{
.col = @bitCast(col_u),
.row = @bitCast(row_u),
.button = button,
.type = mouse_type,
.mods = .{
.shift = mods_byte & 0x01 != 0,
.alt = mods_byte & 0x02 != 0,
.ctrl = mods_byte & 0x04 != 0,
},
} };
},
TAG_COMMAND => {
if (bytes.len < 2) return error.InvalidMessage;
const cmd = safe.intToEnum(Command, bytes[1]) orelse return error.InvalidMessage;
return .{ .command = cmd };
},
TAG_RENDER_UPDATE => {
if (bytes.len < 25) return error.InvalidMessage;
var sp: u64 = 0;
var ap: u64 = 0;
var pp: u64 = 0;
inline for (0..8) |i| {
sp |= @as(u64, bytes[1 + i]) << @intCast((7 - i) * 8);
ap |= @as(u64, bytes[9 + i]) << @intCast((7 - i) * 8);
pp |= @as(u64, bytes[17 + i]) << @intCast((7 - i) * 8);
}
return .{ .render_update = .{
.snapshot_ptr = @intCast(sp),
.arena_ptr = @intCast(ap),
.pool_ptr = @intCast(pp),
} };
},
TAG_RESIZE => {
if (bytes.len < 5) return error.InvalidMessage;
return .{ .resize = .{
.rows = @as(u16, bytes[1]) << 8 | bytes[2],
.cols = @as(u16, bytes[3]) << 8 | bytes[4],
.x_pixel = 0,
.y_pixel = 0,
} };
},
TAG_MODE_CHANGE => {
if (bytes.len < 2) return error.InvalidMessage;
const m = safe.intToEnum(Mode, bytes[1]) orelse return error.InvalidMessage;
return .{ .mode_change = m };
},
TAG_TERMINAL_EXECUTE => {
if (bytes.len < 5) return error.InvalidMessage;
const len: u32 = @as(u32, bytes[1]) << 24 | @as(u32, bytes[2]) << 16 | @as(u32, bytes[3]) << 8 | bytes[4];
if (bytes.len < 5 + len) return error.InvalidMessage;
return .{ .terminal_execute = bytes[5 .. 5 + len] };
},
TAG_TERMINAL_OUTPUT => {
if (bytes.len < 5) return error.InvalidMessage;
const len: u32 = @as(u32, bytes[1]) << 24 | @as(u32, bytes[2]) << 16 | @as(u32, bytes[3]) << 8 | bytes[4];
if (bytes.len < 5 + len) return error.InvalidMessage;
return .{ .terminal_output_chunk = bytes[5 .. 5 + len] };
},
TAG_TERMINAL_RESULT => {
if (bytes.len < 10) return error.InvalidMessage;
const ec: u32 = @as(u32, bytes[1]) << 24 | @as(u32, bytes[2]) << 16 | @as(u32, bytes[3]) << 8 | bytes[4];
const len: u32 = @as(u32, bytes[6]) << 24 | @as(u32, bytes[7]) << 16 | @as(u32, bytes[8]) << 8 | bytes[9];
if (bytes.len < 10 + len) return error.InvalidMessage;
return .{ .terminal_result = .{
.output = bytes[10 .. 10 + len],
.exit_code = @bitCast(ec),
.success = bytes[5] != 0,
} };
},
TAG_QUIT => return .quit,
TAG_TICK => return .tick,
TAG_PLUGIN_MSG => {
if (bytes.len < 6) return error.InvalidMessage;
const id_len = std.mem.readInt(u32, bytes[1..5], .big);
if (bytes.len < 5 + id_len + 1) return error.InvalidMessage;
return .{ .plugin_message = try PluginMessage.decode(bytes[1..]) };
},
TAG_FOCUS => {
if (bytes.len < 2) return error.InvalidMessage;
return .{ .focus = bytes[1] != 0 };
},
else => return error.UnknownTag,
}
}
};
pub const PluginMessage = struct {
plugin_id: []const u8,
message_type: PluginMessageType,
payload: PluginPayload,
/// Correlation ID for request/response matching. 0 = uncorrelated
/// (events, fire-and-forget commands). Set by the SDK on outgoing
/// requests and echoed back by core on the matching response, so a
/// plugin can have multiple in-flight requests without their replies
/// being mis-routed.
correlation_id: u64 = 0,
pub const PluginMessageType = enum(u8) {
register_command = 0,
unregister_command = 1,
lsp_request = 2,
lsp_response = 3,
syntax_highlight = 4,
ui_render = 5,
file_operation = 6,
custom_message = 7,
execute_command = 8,
get_state = 9,
state_response = 10,
subscribe_event = 11,
unsubscribe_event = 12,
event_notification = 13,
get_config = 14,
set_config = 15,
config_response = 16,
show_notification = 17,
open_buffer = 18,
get_buffer_content = 19,
switch_buffer = 20,
buffer_content_response = 21,
create_status_item = 22,
update_status_item = 23,
destroy_status_item = 24,
create_panel = 25,
update_panel_content = 26,
destroy_panel = 27,
update_panel_scroll = 31,
create_widget_id = 28,
destroy_widget_id = 29,
widget_id_response = 30,
execute_core_command = 32,
get_plugin_list = 33,
get_plugin_list_response = 34,
load_plugin = 36,
unload_plugin = 37,
emit_event = 35,
plugin_log = 38,
};
pub const PluginPayload = union(enum) {
command_register: CommandEntry,
command_unregister: []const u8,
lsp: []const u8,
custom: []const u8,
command_execute: []const u8,
state_request: void,
state: EditorStateView,
event_subscribe: PluginEvent,
event_unsubscribe: PluginEvent,
event_notification: struct {
event: PluginEvent,
data: []const u8,
},
config_get: []const u8,
config_set: struct {
key: []const u8,
value: []const u8,
},
config_value: struct {
key: []const u8,
value: ?[]const u8,
},
notification: struct {
level: NotificationLevel,
message: []const u8,
},
buffer_open: struct {
name: []const u8,
content: []const u8,
},
buffer_content_request: void,
buffer_content_response: struct {
id: u32,
content: []const u8,
},
buffer_switch: u32,
status_item_create: struct {
id: []const u8,
text: []const u8,
alignment: StatusAlignment,
priority: i8,
},
status_item_update: struct {
id: []const u8,
text: []const u8,
},
status_item_destroy: []const u8,
panel_create: struct {
id: []const u8,