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
|
// Selection
// SPDX-FileCopyrightText: 2025 Nguyễn Gia Phong
// SPDX-License-Identifier: GPL-3.0-or-later
const eql = std.meta.eql;
const order = std.math.order;
const std = @import("std");
const Node = tree_sitter.Node;
const Tree = tree_sitter.Tree;
const tree_sitter = @import("tree-sitter");
const Grapheme = vaxis.Graphemes.Grapheme;
const vaxis = @import("vaxis");
const graphemes = &@import("root").graphemes;
const Selection = @This();
head: Unit,
tail: Unit,
pub fn startByte(selection: Selection) u32 {
return @min(selection.head.startByte(), selection.tail.startByte());
}
pub fn endByte(selection: Selection) u32 {
return @max(selection.head.endByte(), selection.tail.endByte());
}
pub fn inHead(selection: Selection, grapheme: Grapheme) bool {
return selection.head.contains(grapheme);
}
pub fn inBody(selection: Selection, grapheme: Grapheme) bool {
return (!selection.inHead(grapheme)
and selection.startByte() <= grapheme.offset
and grapheme.offset + grapheme.len <= selection.endByte());
}
fn graphemeAt(text: []const u8, index: u32) Grapheme {
var iterator = graphemes.iterator(text[index..]);
return .{ .offset = index, .len = iterator.peek().?.len };
}
fn graphemeBefore(text: []const u8, index: u32) Grapheme {
var iterator = graphemes.reverseIterator(text[0..index]);
return iterator.peek().?;
}
// TODO: nesting
const Span = struct {
start: u32,
end: u32,
tree: *const Tree,
fn parent(unit: Span) Node {
const root_node = unit.tree.rootNode();
return root_node.descendantForByteRange(unit.start, unit.end).?;
}
};
const GraphemeUnit = struct {
slice: Grapheme,
tree: *const Tree,
fn startByte(unit: GraphemeUnit) u32 {
return unit.slice.offset;
}
fn endByte(unit: GraphemeUnit) u32 {
return unit.slice.offset + unit.slice.len;
}
fn parent(unit: GraphemeUnit) Span {
const start = unit.startByte();
const end = unit.endByte();
const root_node = unit.tree.rootNode();
const parent_node = root_node.descendantForByteRange(start, end).?;
if (parent_node.firstChildForByte(end)) |next_node| {
return if (next_node.prevSibling()) |prev_node| .{
.start = prev_node.endByte(),
.end = next_node.startByte(),
.tree = unit.tree,
} else .{
.start = parent_node.startByte(),
.end = next_node.startByte(),
.tree = unit.tree,
};
} else {
const siblings = parent_node.childCount();
if (siblings == 0)
return .{
.start = parent_node.startByte(),
.end = parent_node.endByte(),
.tree = unit.tree,
};
const prev_node = parent_node.child(siblings - 1).?;
return .{
.start = prev_node.endByte(),
.end = parent_node.endByte(),
.tree = unit.tree,
};
}
}
fn at(unit: GraphemeUnit, text: []const u8, index: u32) GraphemeUnit {
return .{
.slice = graphemeAt(text, index),
.tree = unit.tree,
};
}
fn before(unit: GraphemeUnit,
text: []const u8, index: u32) GraphemeUnit {
return .{
.slice = graphemeBefore(text, index),
.tree = unit.tree,
};
}
};
// TODO: undo
pub const Unit = union(enum) {
node: Node,
span: Span,
grapheme: GraphemeUnit,
fn startByte(unit: Unit) u32 {
return switch (unit) {
.node => |node| node.startByte(),
.span => |span| span.start,
.grapheme => |grapheme| grapheme.startByte(),
};
}
fn endByte(unit: Unit) u32 {
return switch (unit) {
.node => |node| node.endByte(),
.span => |span| span.end,
.grapheme => |grapheme| grapheme.endByte(),
};
}
pub fn up(unit: Unit, text: []const u8) Unit {
switch (unit) {
.node => |node| return if (node.parent()) |parent_node|
if (parent_node.eql(node))
.up(.{ .node = parent_node }, text)
else
.{ .node = parent_node }
else unit,
.span => |span| {
const parent_node = span.parent();
return if (parent_node.startByte() == span.start
and parent_node.endByte() == span.end)
.up(.{ .node = parent_node }, text)
else
.{ .node = parent_node };
},
.grapheme => |grapheme| {
const parent_span = grapheme.parent();
return if (parent_span.start == grapheme.startByte()
and parent_span.end == grapheme.endByte())
.up(.{ .span = parent_span }, text)
else
.{ .span = parent_span };
},
}
}
pub fn right(unit: Unit, text: []const u8) Unit {
switch (unit) {
.node => |node| if (node.nextSibling()) |sibling| {
const prev = node.endByte();
const next = sibling.startByte();
return switch (order(prev, next)) {
.lt => .{
.span = .{
.start = prev,
.end = next,
.tree = node.tree,
}
},
.eq => .{ .node = sibling },
.gt => unreachable,
};
},
.span => |span| {
const parent_node = unit.up(text).node;
if (parent_node.firstChildForByte(span.end)) |next_node|
return .{ .node = next_node };
},
.grapheme => |grapheme| {
const start = grapheme.endByte();
const next_grapheme = grapheme.at(text, start);
if (eql(next_grapheme.parent(), grapheme.parent()))
return .{ .grapheme = next_grapheme };
},
}
return .up(unit, text);
}
pub fn left(unit: Unit, text: []const u8) Unit {
switch (unit) {
.node => |node| if (node.prevSibling()) |sibling| {
const prev = sibling.endByte();
const next = node.startByte();
return switch (order(prev, next)) {
.lt => .{
.span = .{
.start = prev,
.end = next,
.tree = node.tree,
}
},
.eq => .{ .node = sibling },
.gt => unreachable,
};
},
.span => |span| {
const parent_node = unit.up(text).node;
if (parent_node.firstChildForByte(span.end)) |next_node| {
if (next_node.prevSibling()) |prev_node|
return .{ .node = prev_node };
} else {
const siblings = parent_node.childCount();
return .{ .node = parent_node.child(siblings - 1).? };
}
},
.grapheme => |grapheme| {
const end = grapheme.slice.offset;
const prev_grapheme = grapheme.before(text, end);
if (eql(prev_grapheme.parent(), grapheme.parent()))
return .{ .grapheme = prev_grapheme };
},
}
return .up(unit, text);
}
pub fn down(unit: Unit, text: []const u8) Unit {
switch (unit) {
.node => |node| {
const start = node.startByte();
const end = node.endByte();
// TODO: history
return if (node.child(0)) |child|
if (child.startByte() == start and child.endByte() == end)
.down(.{ .node = child }, text)
else
.{ .node = child }
else .down(.{
.span = .{ .start = start, .end = end, .tree = node.tree }
}, text);
},
.span => |span| return .{
.grapheme = .{
// TODO: history
.slice = graphemeAt(text, span.start),
.tree = span.tree,
}
},
.grapheme => return unit,
}
}
pub fn contains(unit: Unit, other: Grapheme) bool {
return switch (unit) {
.node => |node| (node.startByte() <= other.offset
and other.offset + other.len <= node.endByte()),
.span => |span| (span.start <= other.offset
and other.offset + other.len <= span.end),
.grapheme => |grapheme| eql(grapheme.slice, other),
};
}
};
|