summaryrefslogtreecommitdiff
path: root/src/edit.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-09 00:52:52 -0300
committerGabriel Schneider <[email protected]>2026-11-09 01:28:28 -0300
commit5157579ff7f9ae0cfc3c7629e953da05bce340fc (patch)
tree02147789efdcea998981efcfe05d4bcb3e9bb7b0 /src/edit.zig
parent4c4b9bf54d77dbc0f9f4e2619746908e501c6001 (diff)
downloadpardes-5157579ff7f9ae0cfc3c7629e953da05bce340fc.tar.gz
pardes-5157579ff7f9ae0cfc3c7629e953da05bce340fc.zip
tests: at the end of their files: every top-level test block of the 46 files that had one mid-file (the locality analysis's 45 and test/hxdiff.zig; gui.zig, macos.zig, look, host_io, 9p_io, terminal, tui, mouse, fs, replica, layout among them, 11,429 test lines), with the functions only tests call, moved after the file's code in their order, a mechanical move (.scratch/suites/tests_to_end.py), zig fmt'd; the unit suites pass as before (tui 1486/1487, gui 1536/1537, core 956/956 on both), macos.zig only parse-checked here
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/edit.zig')
-rw-r--r--src/edit.zig184
1 files changed, 92 insertions, 92 deletions
diff --git a/src/edit.zig b/src/edit.zig
index 064f0f44..3de16c34 100644
--- a/src/edit.zig
+++ b/src/edit.zig
@@ -1900,6 +1900,98 @@ pub fn pointerTextSelection(p: *const Pardes, id: usize, col: u16, row: u16, bod
};
}
+/// acme's double-click (plan9port acme text.c:1407, textdoubleclick): from
+/// the point `q` (before the character clicked), the text up to the match
+/// of a bracket just before it or just after it (text.c:1386, `{[(<«` and
+/// their closers), the whole line at a line's start or end, the quoted text
+/// next to `' " \``, else the word, acme's isalnum (util.c:328) run.
+pub fn doubleClickRange(text: []const u8, q_in: usize) [2]usize {
+ const sets = [_][2][]const u21{
+ .{ &.{ '{', '[', '(', '<', 0xab }, &.{ '}', ']', ')', '>', 0xbb } },
+ .{ &.{'\n'}, &.{'\n'} },
+ .{ &.{ '\'', '"', '`' }, &.{ '\'', '"', '`' } },
+ };
+ const q = @min(q_in, text.len);
+ for (sets) |set| {
+ const left, const right = set;
+ // the character to the left, looking right
+ const before = if (q == 0) .{ @as(u21, '\n'), @as(usize, 0) } else runeBefore(text, q);
+ if (std.mem.indexOfScalar(u21, left, before[0])) |k| {
+ var at = q;
+ if (clickMatch(text, before[0], right[k], true, &at)) {
+ const closer = if (before[0] == '\n') 0 else runeLen(right[k]);
+ return .{ q, at - closer };
+ }
+ return .{ q, q };
+ }
+ // the character to the right, looking left
+ const after = if (q == text.len) @as(u21, '\n') else runeAt(text, q);
+ if (std.mem.indexOfScalar(u21, right, after)) |k| {
+ var at = q;
+ if (clickMatch(text, after, left[k], false, &at)) {
+ const q1 = q + @as(usize, @intFromBool(q < text.len and after == '\n'));
+ var q0 = at;
+ if (after != '\n' or at != 0 or (text.len > 0 and text[0] == '\n')) q0 += runeLen(left[k]);
+ return .{ @min(q0, q1), q1 };
+ }
+ return .{ q, q };
+ }
+ }
+ var lo = q;
+ var hi = q;
+ while (hi < text.len and isAlnum(runeAt(text, hi))) hi += std.unicode.utf8ByteSequenceLength(text[hi]) catch 1;
+ while (lo > 0 and isAlnum(runeBefore(text, lo)[0])) lo = runeBefore(text, lo)[1];
+ return .{ lo, hi };
+}
+
+/// acme's textclickmatch (text.c:1457): the nesting match of `cl` by `cr`
+/// from `q`, leaving `q` just past it (forward) or on it (back).
+fn clickMatch(text: []const u8, cl: u21, cr: u21, forward: bool, q: *usize) bool {
+ var nest: usize = 1;
+ while (true) {
+ var c: u21 = undefined;
+ if (forward) {
+ if (q.* >= text.len) break;
+ c = runeAt(text, q.*);
+ q.* += std.unicode.utf8ByteSequenceLength(text[q.*]) catch 1;
+ } else {
+ if (q.* == 0) break;
+ const b = runeBefore(text, q.*);
+ q.* = b[1];
+ c = b[0];
+ }
+ if (c == cr) {
+ nest -= 1;
+ if (nest == 0) return true;
+ } else if (c == cl) nest += 1;
+ }
+ return cl == '\n' and nest == 1;
+}
+
+fn runeLen(c: u21) usize {
+ return std.unicode.utf8CodepointSequenceLength(c) catch 1;
+}
+
+fn runeAt(text: []const u8, at: usize) u21 {
+ const n = std.unicode.utf8ByteSequenceLength(text[at]) catch return text[at];
+ if (at + n > text.len) return text[at];
+ return std.unicode.utf8Decode(text[at..][0..n]) catch text[at];
+}
+
+/// The rune ending at `at`, and where it starts.
+fn runeBefore(text: []const u8, at: usize) struct { u21, usize } {
+ var s = at - 1;
+ while (s > 0 and text[s] & 0xC0 == 0x80 and at - s < 4) s -= 1;
+ return .{ runeAt(text, s), s };
+}
+
+fn isAlnum(c: u21) bool {
+ if (c <= ' ') return false;
+ if (c >= 0x7F and c <= 0xA0) return false;
+ if (c < 0x80 and std.mem.indexOfScalar(u8, "!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", @intCast(c)) != null) return false;
+ return true;
+}
+
/// Drain the queue and say whether the shell was asked to do `want`.
fn drainedEffect(p: *Pardes, want: std.meta.Tag(Effect)) bool {
var seen = false;
@@ -2082,98 +2174,6 @@ test "only the SPC clipboard commands cross to the system clipboard" {
try std.testing.expect(p.view().clip_pending == null);
}
-/// acme's double-click (plan9port acme text.c:1407, textdoubleclick): from
-/// the point `q` (before the character clicked), the text up to the match
-/// of a bracket just before it or just after it (text.c:1386, `{[(<«` and
-/// their closers), the whole line at a line's start or end, the quoted text
-/// next to `' " \``, else the word, acme's isalnum (util.c:328) run.
-pub fn doubleClickRange(text: []const u8, q_in: usize) [2]usize {
- const sets = [_][2][]const u21{
- .{ &.{ '{', '[', '(', '<', 0xab }, &.{ '}', ']', ')', '>', 0xbb } },
- .{ &.{'\n'}, &.{'\n'} },
- .{ &.{ '\'', '"', '`' }, &.{ '\'', '"', '`' } },
- };
- const q = @min(q_in, text.len);
- for (sets) |set| {
- const left, const right = set;
- // the character to the left, looking right
- const before = if (q == 0) .{ @as(u21, '\n'), @as(usize, 0) } else runeBefore(text, q);
- if (std.mem.indexOfScalar(u21, left, before[0])) |k| {
- var at = q;
- if (clickMatch(text, before[0], right[k], true, &at)) {
- const closer = if (before[0] == '\n') 0 else runeLen(right[k]);
- return .{ q, at - closer };
- }
- return .{ q, q };
- }
- // the character to the right, looking left
- const after = if (q == text.len) @as(u21, '\n') else runeAt(text, q);
- if (std.mem.indexOfScalar(u21, right, after)) |k| {
- var at = q;
- if (clickMatch(text, after, left[k], false, &at)) {
- const q1 = q + @as(usize, @intFromBool(q < text.len and after == '\n'));
- var q0 = at;
- if (after != '\n' or at != 0 or (text.len > 0 and text[0] == '\n')) q0 += runeLen(left[k]);
- return .{ @min(q0, q1), q1 };
- }
- return .{ q, q };
- }
- }
- var lo = q;
- var hi = q;
- while (hi < text.len and isAlnum(runeAt(text, hi))) hi += std.unicode.utf8ByteSequenceLength(text[hi]) catch 1;
- while (lo > 0 and isAlnum(runeBefore(text, lo)[0])) lo = runeBefore(text, lo)[1];
- return .{ lo, hi };
-}
-
-/// acme's textclickmatch (text.c:1457): the nesting match of `cl` by `cr`
-/// from `q`, leaving `q` just past it (forward) or on it (back).
-fn clickMatch(text: []const u8, cl: u21, cr: u21, forward: bool, q: *usize) bool {
- var nest: usize = 1;
- while (true) {
- var c: u21 = undefined;
- if (forward) {
- if (q.* >= text.len) break;
- c = runeAt(text, q.*);
- q.* += std.unicode.utf8ByteSequenceLength(text[q.*]) catch 1;
- } else {
- if (q.* == 0) break;
- const b = runeBefore(text, q.*);
- q.* = b[1];
- c = b[0];
- }
- if (c == cr) {
- nest -= 1;
- if (nest == 0) return true;
- } else if (c == cl) nest += 1;
- }
- return cl == '\n' and nest == 1;
-}
-
-fn runeLen(c: u21) usize {
- return std.unicode.utf8CodepointSequenceLength(c) catch 1;
-}
-
-fn runeAt(text: []const u8, at: usize) u21 {
- const n = std.unicode.utf8ByteSequenceLength(text[at]) catch return text[at];
- if (at + n > text.len) return text[at];
- return std.unicode.utf8Decode(text[at..][0..n]) catch text[at];
-}
-
-/// The rune ending at `at`, and where it starts.
-fn runeBefore(text: []const u8, at: usize) struct { u21, usize } {
- var s = at - 1;
- while (s > 0 and text[s] & 0xC0 == 0x80 and at - s < 4) s -= 1;
- return .{ runeAt(text, s), s };
-}
-
-fn isAlnum(c: u21) bool {
- if (c <= ' ') return false;
- if (c >= 0x7F and c <= 0xA0) return false;
- if (c < 0x80 and std.mem.indexOfScalar(u8, "!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", @intCast(c)) != null) return false;
- return true;
-}
-
test "double-click selects as acme's does" {
const t = "fn foo_bar(a, b) {\n x\n}\n";
const Case = struct { q: usize, want: []const u8 };