From 99f5b10eccd95aa0066ea6fe6bb75f367ab56a47 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 18 Nov 2026 22:03:14 -0300 Subject: core: src/ becomes core/, pardes.zig its root.zig (the reorganisation's tree, step 2d), and every mention of a src/ path follows: the build's paths, the core's imports of its root, the tests that read the repository's files by path (lspbench's, look.zig's spans over core/look.zig), the comments that cite files, the documents and the manual (the typst corpus reads core/tutor.txt and core/plumbing); left as they were: the /src tree's served names (src/pardes.zig and the rest are the 9P tree's, not the repository's), the CHANGELOG's history, fixtures that are only strings; fs-unit-test runs from the core's root (a module of the core rooted at fs.zig had the runtime's and the devices' `core` be fs.zig); nothing else changes: the ledger (pardes.test.* now root.test.*), frames byte-identical, replicas and process-test the base's Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/modal.zig | 1880 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1880 insertions(+) create mode 100644 core/modal.zig (limited to 'core/modal.zig') diff --git a/core/modal.zig b/core/modal.zig new file mode 100644 index 00000000..f540b1a7 --- /dev/null +++ b/core/modal.zig @@ -0,0 +1,1880 @@ +const std = @import("std"); +const uucode = @import("uucode"); + +pub const Normal = struct { + /// Every normal-mode command, by the name config.zig binds it under. + /// Grouped by the context a key is read in (after `g`, `z`, `m`, `]` or + /// `[`, or none), and within each group in the order a key is tried: + /// the first role a key has is the command. + pub const Role = enum { + escape, + + goto_prefix, + view_prefix, + match_prefix, + find_char_fwd, + find_char_back, + till_char_fwd, + till_char_back, + replace_prefix, + next_prefix, + prev_prefix, + register_prefix, + view_prefix_sticky, + + goto_file_start, + goto_last_line, + goto_line_start, + goto_line_end, + goto_first_nonws, + goto_line_down, + goto_line_up, + goto_column, + goto_view_top, + goto_view_bottom, + goto_last_modification, + goto_word, + goto_definition, + goto_declaration, + goto_type_definition, + goto_implementation, + goto_references, + goto_callstack, + + view_top, + view_center, + view_bottom, + view_middle, + view_scroll_down, + view_scroll_up, + + match_inside, + match_around, + surround_add, + surround_replace, + surround_delete, + + goto_paragraph, + add_newline, + goto_diagnostic, + goto_diagnostic_end, + goto_function, + goto_class, + goto_parameter, + goto_comment, + goto_test, + goto_entry, + goto_xml_element, + + move_left, + move_right, + move_down, + move_up, + next_word_start, + prev_word_start, + next_word_end, + next_long_word_start, + prev_long_word_start, + next_long_word_end, + repeat_find, + line_start, + line_end, + line_first_nonws, + goto_line, + half_page_down, + half_page_up, + page_down, + page_up, + + insert, + append, + insert_line_start, + insert_line_end, + open_below, + open_above, + + select_mode, + select_line, + select_line_bounds, + shrink_to_line_bounds, + collapse_selection, + flip_selection, + select_all, + copy_sel_below, + copy_sel_above, + keep_primary_sel, + remove_primary_sel, + rotate_sel_fwd, + rotate_sel_back, + split_sel_newline, + merge_sels, + merge_consecutive_sels, + trim_sels, + select_regex, + split_regex, + keep_regex, + remove_regex, + ensure_forward, + expand_node, + shrink_node, + next_sibling, + prev_sibling, + all_siblings, + all_children, + parent_node_end, + parent_node_start, + + delete, + delete_noyank, + change, + yank, + replace_with_yank, + paste_after, + paste_before, + switch_case, + to_lowercase, + to_uppercase, + join_lines, + join_select_space, + rotate_contents_fwd, + rotate_contents_back, + align_selections, + indent, + unindent, + format, + increment, + decrement, + comment_toggle, + undo, + redo, + + leader, + command_line, + pipe_selection, + pipe_selection_to, + insert_output, + append_output, + keep_pipe, + macro_record, + macro_replay, + repeat_insert, + search, + search_next, + search_prev, + search_selection, + search_selection_word, + }; + + /// The roles from `from` to `to`, in the order a context tries them. + fn roles(comptime from: Role, comptime to: Role) [@backingInt(to) - @backingInt(from) + 1]Role { + var out: [@backingInt(to) - @backingInt(from) + 1]Role = undefined; + for (&out, @backingInt(from)..) |*role, i| role.* = @fromBackingInt(@intCast(i)); + return out; + } + const goto_roles: []const Role = &roles(.goto_file_start, .goto_callstack); + const view_roles: []const Role = &(roles(.view_top, .view_scroll_up) ++ roles(.half_page_down, .page_up)); + const match_roles: []const Role = &roles(.match_inside, .surround_delete); + const bracket_roles: []const Role = &roles(.goto_paragraph, .goto_xml_element); + const command_roles: []const Role = &roles(.move_left, .search_selection_word); + + pub const Input = struct { + roles: std.EnumSet(Role) = .empty, + cp: u21, + ctrl: bool = false, + alt: bool = false, + + pub fn has(value: Input, role: Role) bool { + return value.roles.contains(role); + } + + fn literal(value: Input) ?u21 { + if (value.ctrl or value.alt or value.cp >= 0xF0000) return null; + return value.cp; + } + }; + + pub const State = struct { + count: u32 = 0, + /// The prefix key read (`g`, `z`, `m`, `f`, `]`, `"`...), its + /// continuation awaited. + prefix: ?Role = null, + /// After `m`: which of `mi`, `ma`, `ms`, `mr`, `md` waits for its + /// character. + match_sub: ?Role = null, + /// `mr`'s pair to replace, while its new one is awaited. + held_char: u21 = 0, + /// `"`: the register the next command names, 0 for none + register: u21 = 0, + /// `Z`: the view keys stay armed until Esc (helix's sticky view) + sticky: bool = false, + + /// Drop what is pending; sticky view mode stays armed through it. + pub fn clear(state: *State) void { + state.* = if (state.sticky) .{ .sticky = true, .prefix = .view_prefix } else .{}; + } + }; + + pub const PipeBehavior = enum { + /// `|` — stdin is the selection, and the output REPLACES it. + replace, + /// `A-|` — stdin is the selection, and the output is discarded. The text + /// is not touched at all; the point is the command's side effect. + ignore, + /// `!` — no stdin, and the output is inserted BEFORE each selection. + insert, + /// `A-!` — no stdin, and the output is appended AFTER each selection. + append, + /// `$` — stdin is the selection; the output is discarded, and only the + /// selections the command exits 0 on are kept. + keep, + + /// Do the selections become stdin? helix's `pipe` flag. + pub fn pipes(b: PipeBehavior) bool { + return b == .replace or b == .ignore or b == .keep; + } + }; + + /// helix's tree-sitter textobjects, by what their queries call them + pub const Object = enum { function, class, parameter, comment, @"test", entry, xml_element }; + /// helix's tree-sitter selection keys: where a range goes in the tree + pub const NodeWalk = enum { expand_node, shrink_node, next_sibling, prev_sibling, all_siblings, all_children, parent_node_end, parent_node_start }; + /// what the s/S/K/Alt-K prompt does with its pattern's matches + pub const RegexMode = enum { select, split, keep, remove }; + /// f/F/t/T: to the character (or till it), forward or back + pub const Find = enum { forward, backward, till_forward, till_backward }; + + /// What a key sequence asks for: the command and what it was given. The + /// text's executor (normal.zig) takes it. + pub const Action = struct { + role: Role, + count: u32 = 1, + /// The count was typed (`5G`, `3gg`), not the 1 a bare key means. + explicit: bool = false, + /// The literal the command takes: f/t's target, r's replacement, the + /// pair of `mi`, `ma`, `ms` and `md`, and `mr`'s new one. + char: u21 = 0, + /// `mr`'s pair to replace. + from: u21 = 0, + /// After `]` (true) or `[` (false). + forward: bool = true, + + /// Run once for the whole selection, not at each range. + pub fn once(a: Action) bool { + return switch (a.role) { + .escape, + .copy_sel_below, + .copy_sel_above, + .keep_primary_sel, + .remove_primary_sel, + .rotate_sel_fwd, + .rotate_sel_back, + .split_sel_newline, + .merge_sels, + .merge_consecutive_sels, + .trim_sels, + .ensure_forward, + .select_regex, + .split_regex, + .keep_regex, + .remove_regex, + .expand_node, + .shrink_node, + .next_sibling, + .prev_sibling, + .all_siblings, + .all_children, + .parent_node_end, + .parent_node_start, + .join_lines, + .join_select_space, + .rotate_contents_fwd, + .rotate_contents_back, + .align_selections, + .comment_toggle, + .undo, + .redo, + .leader, + .command_line, + .pipe_selection, + .pipe_selection_to, + .insert_output, + .append_output, + .keep_pipe, + .macro_record, + .macro_replay, + .repeat_insert, + .search, + .search_next, + .search_prev, + .search_selection, + .search_selection_word, + .goto_word, + => true, + else => false, + }; + } + }; + + pub const Result = union(enum) { + pending, + ignored, + unbound, + action: Action, + }; + + /// The first of `among` the key has, with the count it was given; `none` + /// when it has none of them. + fn first(key: Input, comptime among: []const Role, count: u32, explicit: bool, forward: bool, none: Result) Result { + inline for (among) |role| if (key.has(role)) + return .{ .action = .{ .role = role, .count = count, .explicit = explicit, .forward = forward } }; + return none; + } + + pub fn parse(state: *State, key: Input) Result { + if (key.has(.escape)) { + // Esc out of sticky view mode leaves that mode and nothing else + const sticky = state.sticky; + state.* = .{}; + if (sticky) return .ignored; + return .{ .action = .{ .role = .escape } }; + } + + // A digit is a count only before a command/prefix. A leading zero keeps + // its configured line-start role; after another digit it extends count. + if (state.prefix == null and !key.ctrl and !key.alt and + key.cp >= '0' and key.cp <= '9' and + !(key.cp == '0' and state.count == 0)) + { + if (state.count < 0xffff) + state.count = state.count * 10 + key.cp - '0'; + return .pending; + } + + const explicit = state.count != 0; + const count = @max(1, state.count); + state.count = 0; + + if (state.prefix) |prefix| switch (prefix) { + .goto_prefix => { + state.prefix = null; + return first(key, goto_roles, count, explicit, true, .ignored); + }, + .view_prefix => { + if (!state.sticky) state.prefix = null; + return first(key, view_roles, count, explicit, true, .ignored); + }, + .find_char_fwd, .find_char_back, .till_char_fwd, .till_char_back, .replace_prefix => { + state.prefix = null; + const char = key.literal() orelse return .ignored; + return .{ .action = .{ .role = prefix, .count = count, .explicit = explicit, .char = char } }; + }, + .match_prefix => { + const sub = state.match_sub orelse { + // `mm`: the bracket matching the one under the cursor + if (key.has(.match_prefix)) { + state.prefix = null; + return .{ .action = .{ .role = .match_prefix } }; + } + inline for (match_roles) |role| if (key.has(role)) { + state.match_sub = role; + return .pending; + }; + state.prefix = null; + return .ignored; + }; + const char = key.literal() orelse { + state.clear(); + return .ignored; + }; + if (sub == .surround_replace and state.held_char == 0) { + state.held_char = char; + return .pending; + } + const from = state.held_char; + state.clear(); + return .{ .action = .{ .role = sub, .char = char, .from = from } }; + }, + .next_prefix, .prev_prefix => { + state.prefix = null; + return first(key, bracket_roles, count, explicit, prefix == .next_prefix, .ignored); + }, + .register_prefix => { + state.prefix = null; + const char = key.literal() orelse return .ignored; + state.register = char; + // the count typed before `"` is still the command's + state.count = if (explicit) count else 0; + return .pending; + }, + else => unreachable, + }; + + // Prefix setters retain the count for their continuation. + inline for (.{ .goto_prefix, .view_prefix, .find_char_fwd, .find_char_back, .till_char_fwd, .till_char_back, .replace_prefix, .next_prefix, .prev_prefix, .register_prefix }) |prefix| + if (key.has(prefix)) { + state.prefix = prefix; + state.count = if (explicit) count else 0; + return .pending; + }; + if (key.has(.view_prefix_sticky)) { + state.sticky = true; + state.prefix = .view_prefix; + return .pending; + } + if (key.has(.match_prefix)) { + state.prefix = .match_prefix; + state.match_sub = null; + state.held_char = 0; + return .pending; + } + return first(key, command_roles, count, explicit, true, .unbound); + } + + fn input(cp: u21, among: []const Role) Input { + return .{ .cp = cp, .roles = .initMany(among) }; + } + + test "counts survive prefixes and identical parser actions can feed both adapters" { + var text: State = .{}; + var pdf: State = .{}; + const sequence = [_]Input{ + input('1', &.{}), + input('2', &.{}), + input('g', &.{ .goto_prefix, .goto_file_start }), + input('j', &.{ .move_down, .goto_line_down, .view_scroll_down }), + }; + for (sequence[0 .. sequence.len - 1]) |key| { + try std.testing.expectEqualDeep(parse(&text, key), parse(&pdf, key)); + } + const ta = parse(&text, sequence[sequence.len - 1]); + const pa = parse(&pdf, sequence[sequence.len - 1]); + try std.testing.expectEqualDeep(ta, pa); + try std.testing.expectEqualDeep(Result{ .action = .{ .role = .goto_line_down, .count = 12, .explicit = true } }, ta); + try std.testing.expectEqual(State{}, text); + try std.testing.expectEqual(State{}, pdf); + } + + test "invalid continuations are ignored and clear prefix plus count" { + var state: State = .{}; + try std.testing.expectEqual(Result.pending, parse(&state, input('4', &.{}))); + try std.testing.expectEqual(Result.pending, parse(&state, input('g', &.{.goto_prefix}))); + try std.testing.expectEqual(Result.ignored, parse(&state, input('?', &.{}))); + try std.testing.expectEqual(State{}, state); + try std.testing.expectEqualDeep(Result{ .action = .{ .role = .move_down } }, parse(&state, input('j', &.{.move_down}))); + } + + test "literal arguments retain conflicting command characters" { + var state: State = .{}; + try std.testing.expectEqual(Result.pending, parse(&state, input('f', &.{.find_char_fwd}))); + try std.testing.expectEqualDeep(Result{ .action = .{ .role = .find_char_fwd, .char = 'p' } }, parse(&state, input('p', &.{.paste_after}))); + + try std.testing.expectEqual(Result.pending, parse(&state, input('m', &.{.match_prefix}))); + try std.testing.expectEqual(Result.pending, parse(&state, input('r', &.{.surround_replace}))); + try std.testing.expectEqual(Result.pending, parse(&state, input('[', &.{.prev_prefix}))); + try std.testing.expectEqualDeep(Result{ .action = .{ .role = .surround_replace, .from = '[', .char = ']' } }, parse(&state, input(']', &.{.next_prefix}))); + try std.testing.expectEqual(State{}, state); + } + + test "modified and special keys cannot satisfy literal continuations" { + var state: State = .{}; + _ = parse(&state, input('r', &.{.replace_prefix})); + try std.testing.expectEqual(Result.ignored, parse(&state, .{ .cp = 'x', .ctrl = true })); + try std.testing.expectEqual(State{}, state); + _ = parse(&state, input('f', &.{.find_char_fwd})); + try std.testing.expectEqual(Result.ignored, parse(&state, .{ .cp = 0xF0001 })); + try std.testing.expectEqual(State{}, state); + } + + test "replace accepts a Unicode literal" { + var state: State = .{}; + try std.testing.expectEqual(Result.pending, parse(&state, input('r', &.{.replace_prefix}))); + try std.testing.expectEqualDeep(Result{ .action = .{ .role = .replace_prefix, .char = '界' } }, parse(&state, input('界', &.{}))); + try std.testing.expectEqual(State{}, state); + } + + test "once versus per-selection is semantic action metadata" { + try std.testing.expect((Action{ .role = .search }).once()); + try std.testing.expect((Action{ .role = .undo }).once()); + try std.testing.expect(!(Action{ .role = .delete }).once()); + try std.testing.expect(!(Action{ .role = .move_down, .count = 3 }).once()); + } +}; + +// Cursor columns are UTF-8 byte offsets at grapheme boundaries; line.len is the terminator. + +pub const Cursor = struct { + row: usize = 0, + col: usize = 0, + + pub fn eql(a: Cursor, b: Cursor) bool { + return a.row == b.row and a.col == b.col; + } +}; + +fn codepointAt(text: []const u8, off: usize) u21 { + if (off >= text.len) return 0xFFFD; + const n = std.unicode.utf8ByteSequenceLength(text[off]) catch return 0xFFFD; + if (off + n > text.len) return 0xFFFD; + return std.unicode.utf8Decode(text[off .. off + n]) catch 0xFFFD; +} + +pub fn isUnicodeWhitespace(cp: u21) bool { + if (cp == ' ' or (cp >= '\t' and cp <= '\r') or cp == 0x85) return true; + return switch (uucode.get(.general_category, cp)) { + .separator_space, .separator_line, .separator_paragraph => true, + else => false, + }; +} + +fn lineLenOf(lines: []const []const u8, row: usize) usize { + if (row >= lines.len) return 0; + return lines[row].len; +} + +// advance one position across line boundaries (line terminators are positions +// too: col == line.len is the newline). Returns false at EOF. +fn stepFwd(lines: []const []const u8, c: *Cursor) bool { + if (c.row >= lines.len) return false; + const llen = lineLenOf(lines, c.row); + if (c.col < llen) { + c.col = nextGrapheme(lines[c.row], c.col); + return true; + } + // at the newline: move to next line start + if (c.row + 1 < lines.len) { + c.row += 1; + c.col = 0; + return true; + } + return false; // EOF +} + +fn stepBwd(lines: []const []const u8, c: *Cursor) bool { + if (c.col > 0) { + c.col = prevGrapheme(lines[c.row], c.col); + return true; + } + if (c.row == 0) return false; + c.row -= 1; + c.col = lineLenOf(lines, c.row); // the previous line's newline + return true; +} + +pub fn firstNonWs(line: []const u8) usize { + var i: usize = 0; + while (i < line.len and isUnicodeWhitespace(codepointAt(line, i))) i = nextGrapheme(line, i); + return i; +} + +pub fn charLeft(lines: []const []const u8, c: Cursor) Cursor { + if (c.row >= lines.len) return .{ .row = c.row, .col = 0 }; + return .{ .row = c.row, .col = prevGrapheme(lines[c.row], @min(c.col, lines[c.row].len)) }; +} + +pub fn charRight(lines: []const []const u8, c: Cursor) Cursor { + const llen = lineLenOf(lines, c.row); + const last = if (llen == 0) 0 else prevGrapheme(lines[c.row], llen); + return .{ .row = c.row, .col = if (c.col < last) @min(nextGrapheme(lines[c.row], c.col), last) else last }; +} + +// the ASCII character the cursor sits on, 0 for any other; line terminators +// and EOF read as '\n'. +fn charAt(lines: []const []const u8, c: Cursor) u8 { + if (c.row >= lines.len or c.col >= lines[c.row].len) return '\n'; + const cp = codepointAt(lines[c.row], graphemeStart(lines[c.row], c.col)); + return if (cp <= 0x7f) @intCast(cp) else 0; +} + +// `mm`: the bracket matching the one under the cursor (dumb text scan with +// nesting; no tree-sitter). Null when the cursor is not on a bracket. +pub fn matchBracket(lines: []const []const u8, c: Cursor) ?Cursor { + const opens = "([{<"; + const closes = ")]}>"; + const start = clampToChar(lines, c); + const ch = charAt(lines, start); + if (std.mem.indexOfScalar(u8, opens, ch)) |i| { + var depth: usize = 0; + var p = start; + while (true) { + const cc = charAt(lines, p); + if (cc == opens[i]) depth += 1; + if (cc == closes[i]) { + depth -= 1; + if (depth == 0) return p; + } + if (!stepFwd(lines, &p)) return null; + } + } + if (std.mem.indexOfScalar(u8, closes, ch)) |i| { + var depth: usize = 0; + var p = start; + while (true) { + const cc = charAt(lines, p); + if (cc == closes[i]) depth += 1; + if (cc == opens[i]) { + depth -= 1; + if (depth == 0) return p; + } + if (!stepBwd(lines, &p)) return null; + } + } + return null; +} + +// an inclusive char range [a, b] in document order +pub const Range = struct { a: Cursor, b: Cursor }; + +// the nearest pair of brackets enclosing the cursor (nesting-aware; the +// cursor sitting ON a bracket belongs to that pair). Positions of the +// bracket chars themselves. +pub fn enclosingPair(lines: []const []const u8, c: Cursor, open: u8, close: u8) ?Range { + var a = clampToChar(lines, c); + if (charAt(lines, a) != open) { + var depth: usize = 0; + while (true) { + if (!stepBwd(lines, &a)) return null; + const ch = charAt(lines, a); + if (ch == close) depth += 1; + if (ch == open) { + if (depth == 0) break; + depth -= 1; + } + } + } + const b = matchBracket(lines, a) orelse return null; + return .{ .a = a, .b = b }; +} + +// the quote pair around the cursor, scanned on the cursor's line only +// (plain-text strings don't span lines). Positions of the quote chars. +pub fn enclosingQuote(lines: []const []const u8, c0: Cursor, q: u8) ?Range { + const c = clampToChar(lines, c0); + if (c.row >= lines.len) return null; + const line = lines[c.row]; + var i: usize = 0; + while (i < line.len) { + const o = std.mem.indexOfScalarPos(u8, line, i, q) orelse return null; + const e = std.mem.indexOfScalarPos(u8, line, o + 1, q) orelse return null; + if (c.col < o) return null; // the cursor sits before any pair + if (c.col <= e) return .{ .a = .{ .row = c.row, .col = o }, .b = .{ .row = c.row, .col = e } }; + i = e + 1; + } + return null; +} + +// clamp a (possibly terminator/EOF) position onto a real character. +pub fn clampToChar(lines: []const []const u8, c: Cursor) Cursor { + if (c.row >= lines.len) { + return .{ .row = if (lines.len == 0) 0 else lines.len - 1, .col = 0 }; + } + const llen = lineLenOf(lines, c.row); + if (llen == 0) return .{ .row = c.row, .col = 0 }; + return .{ .row = c.row, .col = graphemeStart(lines[c.row], @min(c.col, prevGrapheme(lines[c.row], llen))) }; +} + +pub fn lineCount(content: []const u8) usize { + if (content.len == 0) return 0; + return std.mem.count(u8, content, "\n") + 1; +} + +// byte offset of the start of line `row` (0-based). row may == lineCount() +// (== content.len, the end). +pub fn lineStartOffset(content: []const u8, row: usize) usize { + // Newlines counted 32 bytes at a time, the last block byte by byte: one + // indexOfScalarPos per line was a call per line, 7000 of them to find + // line 7000 of a source, for every cursor move, selection and language + // request there. + const block = 32; + const Block = @Vector(block, u8); + var off: usize = 0; + var left = row; + if (left == 0) return 0; + while (off + block <= content.len) : (off += block) { + const chunk: Block = content[off..][0..block].*; + const found: usize = @popCount(@as(u32, @bitCast(chunk == @as(Block, @splat('\n'))))); + if (found >= left) break; + left -= found; + } + while (off < content.len) : (off += 1) if (content[off] == '\n') { + left -= 1; + if (left == 0) return off + 1; + }; + return content.len; +} + +// the text of line `row` (no terminator), a slice into `content`. +pub fn lineSlice(content: []const u8, row: usize) []const u8 { + const start = lineStartOffset(content, row); + if (start >= content.len) return ""; + // indexOfScalarPos, not indexOfPos with a one-byte needle: the latter runs the generic + // substring search where a memchr will do, and this is called once per visible row per frame. + const nl = std.mem.indexOfScalarPos(u8, content, start, '\n') orelse content.len; + return content[start..nl]; +} + +/// A bounded scan of one line; null if the row does not exist. +pub const LineSpan = struct { start: usize, end: usize }; + +/// Just past the `row`-th newline; null if the text has fewer. +pub fn lineStartExact(content: []const u8, row: usize) ?usize { + const at = lineStartOffset(content, row); + // At the end: either the row-th newline was the last byte, or there were fewer. + if (at == content.len and std.mem.count(u8, content, "\n") < row) return null; + return at; +} + +pub fn lineSpan(content: []const u8, row: usize) ?LineSpan { + // Match lineCount: empty content has no lines; a trailing newline adds one. + if (content.len == 0) return null; + const start = lineStartExact(content, row) orelse return null; + if (start == content.len and !(row == 0 or content.len == 0 or content[content.len - 1] == '\n')) return null; + const end = std.mem.indexOfScalarPos(u8, content, start, '\n') orelse content.len; + return .{ .start = start, .end = end }; +} + +pub fn spliceAlloc(alloc: std.mem.Allocator, content: []const u8, start: usize, end: usize, replacement: []const u8) ![]u8 { + // Raw: every byte is written below, where `alloc` fills a safe build's + // new memory first, a whole file's worth per keystroke. + const len = content.len - (end - start) + replacement.len; + if (len == 0) return alloc.alloc(u8, 0); + const out = (alloc.rawAlloc(len, .of(u8), @returnAddress()) orelse return error.OutOfMemory)[0..len]; + @memcpy(out[0..start], content[0..start]); + @memcpy(out[start..][0..replacement.len], replacement); + @memcpy(out[start + replacement.len ..], content[end..]); + return out; +} + +/// insert `text` at (row, col). col is clamped to the line length. +pub fn insertAt(alloc: std.mem.Allocator, content: []const u8, c: Cursor, text: []const u8) ![]u8 { + const off = insertOffset(content, c); + return spliceAlloc(alloc, content, off, off, text); +} + +/// Where insertAt puts its text: col clamped to the line, no further. +pub fn insertOffset(content: []const u8, c: Cursor) usize { + // Only an out-of-range row requires counting the whole document. + const span = lineSpan(content, c.row) orelse blk: { + const last = lineCount(content) -| 1; + break :blk lineSpan(content, last) orelse LineSpan{ .start = content.len, .end = content.len }; + }; + return span.start + @min(c.col, span.end - span.start); +} + +// delete whole lines [r0, r1] inclusive (the line content + their terminators). +pub fn deleteLines(alloc: std.mem.Allocator, content: []const u8, r0: usize, r1: usize) ![]u8 { + const n = lineCount(content); + const lo = @min(r0, r1); + const hi = @min(@max(r0, r1), if (n == 0) 0 else n - 1); + if (n == 0 or hi < lo) return alloc.dupe(u8, content); + const start = lineStartOffset(content, lo); + // end = start of line (hi+1), or content.len if hi is the last line. + const end = if (hi + 1 < n) lineStartOffset(content, hi + 1) else content.len; + // the last line takes the '\n' before it too: no trailing blank line is left + return spliceAlloc(alloc, content, if (hi + 1 == n and start > 0) start - 1 else start, end, ""); +} + +// paste `text` as a new line BELOW `row`. Multiline `text` becomes several lines. +pub fn pasteLineBelow(alloc: std.mem.Allocator, content: []const u8, row: usize, text: []const u8) ![]u8 { + // The next line's start, or the end: no whole-text line count to know which. + const off = lineStartExact(content, row + 1) orelse content.len; + const suffix_newline = off < content.len or (content.len > 0 and content[content.len - 1] == '\n'); + const prefix_newline = !suffix_newline and off > 0; + const extra: usize = @intFromBool(suffix_newline or prefix_newline); + const out = try @import("memory.zig").unfilled(alloc, content.len + text.len + extra); + var write: usize = 0; + @memcpy(out[write..][0..off], content[0..off]); + write += off; + if (prefix_newline) { + out[write] = '\n'; + write += 1; + } + @memcpy(out[write..][0..text.len], text); + write += text.len; + if (suffix_newline) { + out[write] = '\n'; + write += 1; + } + @memcpy(out[write..], content[off..]); + return out; +} + +// the cursor position AFTER `text` inserted at `c` (one past its last char). +pub fn advanceBy(c: Cursor, text: []const u8) Cursor { + const nl = std.mem.lastIndexOfScalar(u8, text, '\n') orelse return .{ .row = c.row, .col = c.col + text.len }; + return .{ .row = c.row + std.mem.count(u8, text, "\n"), .col = text.len - nl - 1 }; +} + +// `~` / `` ` `` / ``Alt-` ``: case-map the bytes [s, e). +pub const CaseOp = enum { toggle, lower, upper }; + +pub fn changeCase(alloc: std.mem.Allocator, content: []const u8, s: usize, e: usize, op: CaseOp) ![]u8 { + const out = try alloc.dupe(u8, content); + for (out[s..e]) |*p| { + p.* = switch (op) { + .toggle => if (std.ascii.isUpper(p.*)) std.ascii.toLower(p.*) else std.ascii.toUpper(p.*), + .lower => std.ascii.toLower(p.*), + .upper => std.ascii.toUpper(p.*), + }; + } + return out; +} + +// `>` / `<`: indent/unindent lines [r0, r1]. Fixed width — pardes has no +// per-language indent config; 4 spaces, one tab counts as one level out. +pub const INDENT_W = 4; + +// delete the EXCLUSIVE span [a, b) — insert-mode kills. col may equal the +// line length (the newline); a kill crossing it passes b = (row+1, 0). +pub fn deleteSpan(alloc: std.mem.Allocator, content: []const u8, a: Cursor, b: Cursor) ![]u8 { + const s = lineStartOffset(content, a.row) + @min(a.col, lineSlice(content, a.row).len); + const e = lineStartOffset(content, b.row) + @min(b.col, lineSlice(content, b.row).len); + if (e <= s) return alloc.dupe(u8, content); + return spliceAlloc(alloc, content, s, e, ""); +} + +// Ported from Helix movement.rs and selection.rs at 278b24389. +// Half-open selections use UTF-8 byte gaps at grapheme boundaries. +// A forward selection's cursor is the grapheme before head; a backward one's is at head. + +pub const Selection = struct { anchor: usize, head: usize }; + +/// The first byte of the grapheme containing off. Adjacent ASCII bytes are +/// boundaries except CR-LF (UAX #29 GB3); other cases need full segmentation. +pub fn graphemeStart(text: []const u8, off: usize) usize { + const bounded = @min(off, text.len); + if (bounded == text.len) return text.len; + if (text[bounded] < 0x80) { + if (bounded == 0) return 0; + const prev = text[bounded - 1]; + if (prev < 0x80 and !(prev == '\r' and text[bounded] == '\n')) return bounded; + } + var it = uucode.grapheme.utf8Iterator(text); + while (it.nextGrapheme()) |g| { + if (bounded < g.end) return g.start; + } + return text.len; +} + +/// one extended grapheme forward, clamped at text.len +pub fn nextGrapheme(text: []const u8, off: usize) usize { + if (off >= text.len) return text.len; + // Inputs are grapheme boundaries; repair continuation bytes. CR-LF stays one cluster. + if (text[off] < 0x80 and (off + 1 == text.len or text[off + 1] < 0x80) and + !(text[off] == '\r' and off + 1 < text.len and text[off + 1] == '\n')) return off + 1; + var start = off; + while (start > 0 and (text[start] & 0xC0) == 0x80) start -= 1; + if (start != off) start = graphemeStart(text, off); + var it = uucode.grapheme.utf8Iterator(text[start..]); + const g = it.nextGrapheme() orelse return @min(start + 1, text.len); + return start + g.end; +} + +pub fn prevGrapheme(text: []const u8, off: usize) usize { + var bounded = @min(off, text.len); + if (bounded < text.len) { + const repaired = graphemeStart(text, bounded); + if (repaired < bounded) return repaired; + bounded = repaired; + } + if (bounded == 0) return 0; + // GB3 also excludes stepping backward into CR-LF. + if (text[bounded - 1] < 0x80 and (bounded == 1 or text[bounded - 2] < 0x80) and + !(bounded >= 2 and text[bounded - 2] == '\r' and text[bounded - 1] == '\n')) return bounded - 1; + // No grapheme crosses a line break; reverse segmentation only needs this line. + const line_start = if (std.mem.lastIndexOfScalar(u8, text[0 .. bounded - 1], '\n')) |nl| nl + 1 else 0; + if (line_start == bounded) return bounded - 1; // the newline is its own editor cell + var it = uucode.grapheme.utf8Iterator(text[line_start..bounded]); + var last = line_start; + while (it.nextGrapheme()) |g| last = line_start + g.start; + return last; +} + +/// Byte offset of the zero-based grapheme column, clamped to the line end. +pub fn graphemeAtColumn(text: []const u8, column: usize) usize { + var at: usize = 0; + var col: usize = 0; + while (at < text.len and col < column) : (col += 1) at = nextGrapheme(text, at); + return at; +} + +/// the block cursor cell of a range (helix Range::cursor) +pub fn selectionCursor(text: []const u8, r: Selection) usize { + return if (r.head > r.anchor) prevGrapheme(text, r.head) else r.head; +} + +/// Crossing the anchor moves its byte gap one grapheme, preserving the anchor cell. +pub fn moveSelectionCursor(text: []const u8, r: Selection, idx: usize, extend: bool) Selection { + if (!extend) return .{ .anchor = idx, .head = idx }; + var anchor = r.anchor; + if (r.head >= r.anchor and idx < r.anchor) { + anchor = nextGrapheme(text, r.anchor); + } else if (r.head < r.anchor and idx >= r.anchor) { + anchor = prevGrapheme(text, r.anchor); + } + if (anchor <= idx) return .{ .anchor = anchor, .head = nextGrapheme(text, idx) }; + return .{ .anchor = anchor, .head = idx }; +} + +// ropey-style line math: len_lines = count('\n') + 1 — the slot after a +// trailing '\n' is a real, empty last line and the cursor can sit there. +pub fn cursorLineCount(text: []const u8) usize { + return std.mem.count(u8, text, "\n") + 1; +} + +pub fn lineAtOffset(text: []const u8, off: usize) usize { + return std.mem.count(u8, text[0..@min(off, text.len)], "\n"); +} + +/// offset of line's terminator ('\n'), or text.len on the last line +pub fn lineEndOffset(text: []const u8, line: usize) usize { + const s = lineStartOffset(text, line); + return if (std.mem.indexOfScalarPos(u8, text, s, '\n')) |nl| nl else text.len; +} + +/// gap offset -> (row, col) cell +/// The start of the UTF-8 rune `off` falls in. Addresses (the 9P `addr`, +/// `dot`, `#n`, `line:col`, a search's match) count runes, as sam's and +/// acme's do, not grapheme clusters: a lone combining mark or a `\r` is a +/// place of its own, whatever a terminal draws in one cell. +pub fn runeStart(text: []const u8, off: usize) usize { + const o = @min(off, text.len); + if (o == text.len or text[o] & 0xC0 != 0x80) return o; + var s = o; + var steps: u8 = 0; + while (s > 0 and text[s] & 0xC0 == 0x80 and steps < 3) : (steps += 1) s -= 1; + // Only a lead byte whose sequence reaches `o`, its bytes all + // continuations, makes `o` the inside of a rune; a stray continuation + // byte (invalid UTF-8) is a place of its own. + const n = std.unicode.utf8ByteSequenceLength(text[s]) catch return o; + if (s + n <= o or s + n > text.len) return o; + for (text[s + 1 .. s + n]) |c| if (c & 0xC0 != 0x80) return o; + return s; +} + +/// `off`, or the end of the rune it falls inside. +pub fn runeEnd(text: []const u8, off: usize) usize { + const o = @min(off, text.len); + const s = runeStart(text, o); + if (s == o) return o; + return s + (std.unicode.utf8ByteSequenceLength(text[s]) catch 1); +} + +/// The rune boundary after the one at `off`. +pub fn nextRune(text: []const u8, off: usize) usize { + return if (off >= text.len) text.len else runeEnd(text, off + 1); +} + +/// The rune boundary before `off`. +pub fn prevRune(text: []const u8, off: usize) usize { + return if (off == 0) 0 else runeStart(text, @min(off, text.len) - 1); +} + +/// `positionAt` snapped to a rune rather than a grapheme cluster. +pub fn runePositionAt(text: []const u8, off: usize) Cursor { + const bounded = runeStart(text, off); + const s = if (std.mem.lastIndexOfScalar(u8, text[0..bounded], '\n')) |nl| nl + 1 else 0; + return .{ .row = lineAtOffset(text, bounded), .col = bounded - s }; +} + +/// `offsetAt` snapped to a rune rather than a grapheme cluster. +pub fn runeOffsetAt(text: []const u8, c: Cursor) usize { + const row = @min(c.row, cursorLineCount(text) - 1); + const s = lineStartOffset(text, row); + const e = std.mem.indexOfScalarPos(u8, text, s, '\n') orelse text.len; + return s + runeStart(text[s..e], @min(c.col, e - s)); +} + +pub fn positionAt(text: []const u8, off: usize) Cursor { + const bounded = @min(off, text.len); + // Find the line start backward without a second scan of the document prefix. + const s = if (std.mem.lastIndexOfScalar(u8, text[0..bounded], '\n')) |nl| nl + 1 else 0; + const e = std.mem.indexOfScalarPos(u8, text, s, '\n') orelse text.len; + const col = graphemeStart(text[s..e], @min(bounded - s, e - s)); + return .{ .row = lineAtOffset(text, s + col), .col = col }; +} + +/// (row, col) -> clamped gap offset; col == line length lands ON the '\n' +pub fn offsetAt(text: []const u8, c: Cursor) usize { + // A row past the last is the last: found back from the end, where + // counting every line of the text to clamp it cost a pass over all of it. + var s = lineStartOffset(text, c.row); + if (s == text.len) s = if (std.mem.lastIndexOfScalar(u8, text, '\n')) |nl| nl + 1 else 0; + // lineEndOffset(text, row) inlined: it starts by walking to `row` again, + // and we are already standing there + const e = std.mem.indexOfScalarPos(u8, text, s, '\n') orelse text.len; + const raw = @min(c.col, e - s); + return s + graphemeStart(text[s..e], raw); +} + +pub const WordTarget = enum { + next_word_start, + next_word_end, + prev_word_start, + next_long_word_start, + next_long_word_end, + prev_long_word_start, +}; + +// helix categorize_char: Eol is its OWN category, distinct from Whitespace — +// that distinction is load-bearing in reached_target. +const WordClass = enum { word, punct, ws, eol }; + +/// A codepoint's word class: Unicode word characters, punctuation, or +/// whitespace (line ends included; wordClassAt tells them apart). +fn kindOfCodepoint(cp: u21) WordClass { + if (isUnicodeWhitespace(cp)) return .ws; + if (cp == '_') return .word; + return switch (uucode.get(.general_category, cp)) { + .letter_uppercase, + .letter_lowercase, + .letter_titlecase, + .letter_modifier, + .letter_other, + .mark_nonspacing, + .mark_spacing_combining, + .mark_enclosing, + .number_decimal_digit, + .number_letter, + .number_other, + .punctuation_connector, + => .word, + else => .punct, + }; +} + +fn wordClassAt(text: []const u8, off: usize) WordClass { + if (off >= text.len) return .eol; + const cp = codepointAt(text, off); + if (cp == '\n' or cp == '\r') return .eol; + return kindOfCodepoint(cp); +} + +pub fn searchWordAt(text: []const u8, off: usize) bool { + return off < text.len and wordClassAt(text, off) == .word; +} + +pub fn searchWordBefore(text: []const u8, off: usize) bool { + if (off == 0) return false; + var previous = off - 1; + while (previous > 0 and text[previous] & 0xc0 == 0x80) previous -= 1; + return searchWordAt(text, previous); +} + +fn isWordWhitespace(c: WordClass) bool { // Rust char::is_whitespace (includes line endings) + return c == .ws or c == .eol; +} + +fn isLongWordBoundary(a: WordClass, b: WordClass) bool { + if ((a == .word and b == .punct) or (a == .punct and b == .word)) return false; + return a != b; +} + +fn reachedWordTarget(target: WordTarget, prev: WordClass, next: WordClass) bool { + return switch (target) { + .next_word_start => prev != next and (next == .eol or !isWordWhitespace(next)), + .next_word_end, .prev_word_start => prev != next and (!isWordWhitespace(prev) or next == .eol), + .next_long_word_start => isLongWordBoundary(prev, next) and (next == .eol or !isWordWhitespace(next)), + .next_long_word_end, .prev_long_word_start => isLongWordBoundary(prev, next) and (!isWordWhitespace(prev) or next == .eol), + }; +} + +/// helix jump_to_word's candidates: the words of two or more word +/// characters between `start` and `end`, nearest the cursor first, taking one +/// forward and one back in turn, each over its word pointing the way it was +/// found, as many as `out` holds. +pub fn jumpWords(text: []const u8, cursor: usize, start: usize, end: usize, out: []Selection) usize { + var fwd: Selection = .{ .anchor = cursor, .head = cursor }; + var rev = fwd; + if (cursor < text.len and !isWordWhitespace(wordClassAt(text, cursor))) { + const word_end = moveWord(text, fwd, 1, .next_word_end); + if (word_end.anchor == cursor) fwd = word_end; // a word of one grapheme + const word_start = moveWord(text, rev, 1, .prev_word_start); + if (word_start.anchor == nextGrapheme(text, cursor)) rev = word_start; + } + var n: usize = 0; + while (n < out.len) { + var changed = false; + while (fwd.head < end) { + fwd = moveWord(text, fwd, 1, .next_word_end); + // the two graphemes before the head are word characters + const one = prevGrapheme(text, fwd.head); + if (fwd.head == 0 or one == 0 or wordClassAt(text, one) != .word or wordClassAt(text, prevGrapheme(text, one)) != .word) continue; + changed = true; + var from = fwd.anchor; + while (from < fwd.head and wordClassAt(text, from) != .word) from = nextGrapheme(text, from); + fwd.anchor = from; + out[n] = fwd; + n += 1; + break; + } + if (n == out.len) break; + while (rev.head > start) { + rev = moveWord(text, rev, 1, .prev_word_start); + // the two graphemes from the head are word characters + if (wordClassAt(text, rev.head) != .word or wordClassAt(text, nextGrapheme(text, rev.head)) != .word) continue; + changed = true; + var to = rev.anchor; + while (to > rev.head and wordClassAt(text, prevGrapheme(text, to)) != .word) to = prevGrapheme(text, to); + rev.anchor = to; + out[n] = rev; + n += 1; + break; + } + if (!changed) break; + } + return n; +} + +/// w/b/e/W/B/E: helix word_move — each step selects the traversed span. +pub fn moveWord(text: []const u8, r0: Selection, count: usize, target: WordTarget) Selection { + const is_prev = target == .prev_word_start or target == .prev_long_word_start; + if ((is_prev and r0.head == 0) or (!is_prev and r0.head == text.len)) return r0; + // block-cursor prep: collapse to the 1-wide cell at the head, pointing + // in the motion direction (the anchor of the input is irrelevant) + var r: Selection = if (is_prev) + (if (r0.anchor < r0.head) + .{ .anchor = r0.head, .head = prevGrapheme(text, r0.head) } + else + .{ .anchor = nextGrapheme(text, r0.head), .head = r0.head }) + else + (if (r0.anchor < r0.head) + .{ .anchor = prevGrapheme(text, r0.head), .head = r0.head } + else + .{ .anchor = r0.head, .head = nextGrapheme(text, r0.head) }); + for (0..@max(1, count)) |_| { + const next = wordRangeToTarget(text, target, r, is_prev); + if (next.anchor == r.anchor and next.head == r.head) break; + r = next; + } + return r; +} + +// port of CharHelpers::range_to_target — a char iterator walking away from +// origin.head; when reversed, "next" reads the byte just behind the position. +fn wordRangeToTarget(text: []const u8, target: WordTarget, origin: Selection, is_prev: bool) Selection { + var anchor = origin.anchor; + var head = origin.head; + var it = origin.head; + var prev_cat: ?WordClass = if (is_prev) + (if (it < text.len) wordClassAt(text, it) else null) + else + (if (it > 0) wordClassAt(text, prevGrapheme(text, it)) else null); + + // skip any initial newline characters + while (true) { + if ((is_prev and it == 0) or (!is_prev and it >= text.len)) break; + const cell = if (is_prev) prevGrapheme(text, it) else it; + const cat = wordClassAt(text, cell); + if (cat != .eol) break; + it = if (is_prev) cell else nextGrapheme(text, cell); + prev_cat = cat; + head = it; + } + if (prev_cat == .eol) anchor = head; + + // find the target position + const head_start = head; + while (true) { + if ((is_prev and it == 0) or (!is_prev and it >= text.len)) break; + const cell = if (is_prev) prevGrapheme(text, it) else it; + const next_cat = wordClassAt(text, cell); + if (prev_cat == null or reachedWordTarget(target, prev_cat.?, next_cat)) { + if (head == head_start) anchor = head else break; + } + prev_cat = next_cat; + it = if (is_prev) cell else nextGrapheme(text, cell); + head = it; + } + return .{ .anchor = anchor, .head = head }; +} + +/// a ropey "line is a line ending" — the line has no content of its own +fn lineIsEmpty(text: []const u8, line: usize) bool { + return lineStartOffset(text, line) == lineEndOffset(text, line); +} + +/// ]p / [p: helix move_next_paragraph / move_prev_paragraph +pub fn moveParagraph(text: []const u8, r: Selection, count: usize, fwd: bool, extend: bool) Selection { + const nlines = cursorLineCount(text); + const cursor = selectionCursor(text, r); + var line = lineAtOffset(text, cursor); + if (fwd) { + const nxt_start = if (line + 1 >= nlines) text.len else lineStartOffset(text, line + 1); + const last_char = prevGrapheme(text, nxt_start) == cursor; + const curr_empty = lineIsEmpty(text, line); + const next_empty = lineIsEmpty(text, @min(nlines - 1, line + 1)); + const curr_empty_to_line = curr_empty and !next_empty; + // skip the character after the paragraph boundary + if (curr_empty_to_line and last_char) line += 1; + var l = line; + var last_line = l; + for (0..@max(1, count)) |_| { + while (l < nlines and !lineIsEmpty(text, l)) l += 1; + while (l < nlines and lineIsEmpty(text, l)) l += 1; + if (l == last_line) break; + last_line = l; + } + const head = if (l >= nlines) text.len else lineStartOffset(text, l); + const anchor = if (extend) + moveSelectionCursor(text, r, head, true).anchor + else if (curr_empty_to_line and last_char) + r.head + else + cursor; + return .{ .anchor = anchor, .head = head }; + } + const first_char = lineStartOffset(text, line) == cursor; + const prev_empty = lineIsEmpty(text, line -| 1); + const curr_empty = lineIsEmpty(text, line); + const prev_empty_to_line = prev_empty and !curr_empty; + // skip the character before the paragraph boundary + if (prev_empty_to_line and !first_char) line += 1; + var l = line; + var last_line = l; + for (0..@max(1, count)) |_| { + while (l > 0 and lineIsEmpty(text, l - 1)) l -= 1; + while (l > 0 and !lineIsEmpty(text, l - 1)) l -= 1; + if (l == last_line) break; + last_line = l; + } + const head = lineStartOffset(text, l); + const anchor = if (extend) + moveSelectionCursor(text, r, head, true).anchor + else if (prev_empty_to_line and first_char) + cursor + else + r.head; + return .{ .anchor = anchor, .head = head }; +} + +/// f/F/t/T target cell. helix find_char: the exclusive (till) search starts +/// one further out so repeats make progress; not-found = null (no move). +pub fn findTarget(text: []const u8, cursor: usize, ch: u21, kind: Normal.Find, count: usize) ?usize { + const till = kind == .till_forward or kind == .till_backward; + var left = @max(1, count); + if (kind == .forward or kind == .till_forward) { + const head = nextGrapheme(text, cursor); + var i = if (till) nextGrapheme(text, head) else head; + if (i > text.len) return null; + while (i < text.len) : (i = nextGrapheme(text, i)) { + if (codepointAt(text, i) == ch) { + left -= 1; + if (left == 0) return if (till) prevGrapheme(text, i) else i; + } + } + return null; + } + var i = if (till) prevGrapheme(text, cursor) else cursor; + while (i > 0) { + i = prevGrapheme(text, i); + if (codepointAt(text, i) == ch) { + left -= 1; + if (left == 0) return if (till) nextGrapheme(text, i) else i; + } + } + return null; +} + +// helix textobject.rs find_word_boundary +fn findWordBoundary(text: []const u8, pos0: usize, fwd: bool, long: bool) usize { + var prev: WordClass = if (fwd) + (if (pos0 == 0) .ws else wordClassAt(text, prevGrapheme(text, pos0))) + else + (if (pos0 >= text.len) .ws else wordClassAt(text, pos0)); + var pos = pos0; + var it = pos0; + while (true) { + if ((fwd and it >= text.len) or (!fwd and it == 0)) break; + const cell = if (fwd) it else prevGrapheme(text, it); + const cat = wordClassAt(text, cell); + if (cat == .eol or cat == .ws) return pos; + if (!long and cat != prev and pos != 0 and pos != text.len) return pos; + it = if (fwd) nextGrapheme(text, cell) else cell; + pos = it; + prev = cat; + } + return pos; +} + +/// miw/maw (and W): helix textobject_word — on whitespace it selects the +/// whitespace run's boundary (a 1-wide cursor there) +pub fn selectWord(text: []const u8, r: Selection, around: bool, long: bool) Selection { + const pos = selectionCursor(text, r); + const word_start = findWordBoundary(text, pos, false, long); + const cat: WordClass = if (pos < text.len) wordClassAt(text, pos) else .ws; + const word_end = if (cat == .ws or cat == .eol) pos else findWordBoundary(text, nextGrapheme(text, pos), true, long); + if (word_start == word_end or !around) return .{ .anchor = word_start, .head = word_end }; + var end = word_end; + while (end < text.len and isWordWhitespace(wordClassAt(text, end)) and wordClassAt(text, end) != .eol) + end = nextGrapheme(text, end); + if (end > word_end) return .{ .anchor = word_start, .head = end }; + var start = word_start; + while (start > 0) { + const before = prevGrapheme(text, start); + const before_cat = wordClassAt(text, before); + if (!isWordWhitespace(before_cat) or before_cat == .eol) break; + start = before; + } + return .{ .anchor = start, .head = word_end }; +} + +/// mip/map: helix textobject_paragraph +pub fn selectParagraph(text: []const u8, r: Selection, around: bool, count: usize) Selection { + const nlines = cursorLineCount(text); + const cursor = selectionCursor(text, r); + var line = lineAtOffset(text, cursor); + const prev_empty = lineIsEmpty(text, line -| 1); + const curr_empty = lineIsEmpty(text, line); + const next_empty = line + 1 >= nlines or lineIsEmpty(text, line + 1); + const nxt_start = if (line + 1 >= nlines) text.len else lineStartOffset(text, line + 1); + const last_char = prevGrapheme(text, nxt_start) == cursor; + const prev_empty_to_line = prev_empty and !curr_empty; + const curr_empty_to_line = curr_empty and !next_empty; + + var line_back = line; + if (prev_empty_to_line or curr_empty_to_line) line_back += 1; + // do not include the current paragraph on a paragraph end (include next) + if (!(curr_empty_to_line and last_char)) { + while (line_back > 0 and lineIsEmpty(text, line_back - 1)) line_back -= 1; + while (line_back > 0 and !lineIsEmpty(text, line_back - 1)) line_back -= 1; + } + if (curr_empty_to_line and last_char) line += 1; + const n = @max(1, count); + var count_done: usize = 0; + for (0..n) |_| { + var done = false; + while (line < nlines and !lineIsEmpty(text, line)) { + line += 1; + done = true; + } + while (line < nlines and lineIsEmpty(text, line)) line += 1; + if (done) count_done += 1; + } + // search one paragraph backwards when we ran off the end + if (count_done != n and line >= nlines) { + while (line_back > 0 and lineIsEmpty(text, line_back - 1)) line_back -= 1; + while (line_back > 0 and !lineIsEmpty(text, line_back - 1)) line_back -= 1; + } + if (!around) { + // inside: drop the trailing whitespace paragraph + while (line > 0 and lineIsEmpty(text, line - 1)) line -= 1; + } + return .{ + .anchor = lineStartOffset(text, line_back), + .head = if (line >= nlines) text.len else lineStartOffset(text, line), + }; +} + +/// A line's indent as it is, tabs and spaces both, as acme's autoindent +/// copies it: what o/O/insert-newline give the new line. +pub fn indentText(line: []const u8) []const u8 { + return line[0 .. std.mem.indexOfNone(u8, line, " \t") orelse line.len]; +} + +/// Copy full indent levels and add one after (, [, {, or ), without parsing. +pub fn newlineIndentWidth(line: []const u8, col: usize) usize { + const prefix = std.mem.trimEnd(u8, line[0..@min(col, line.len)], " \t"); + const extra = if (prefix.len == 0) false else switch (prefix[prefix.len - 1]) { + '(', '[', '{', ')' => true, + else => false, + }; + return indentText(line).len + @as(usize, if (extra) INDENT_W else 0); +} + +/// Increment a selected decimal integer, preserving zero-padding across sign changes. +pub fn incrementDecimal(alloc: std.mem.Allocator, frag: []const u8, amount: i64) !?[]u8 { + if (frag.len == 0) return null; + const neg = frag[0] == '-'; + const digits = if (neg) frag[1..] else frag; + if (digits.len == 0) return null; + for (digits) |ch| if (!std.ascii.isDigit(ch)) return null; + const val = std.fmt.parseInt(i128, frag, 10) catch return null; + const nv = val +| @as(i128, amount); + const pad = digits[0] == '0'; + const neg_after = nv < 0; + // format_length includes the sign, adjusted when the sign flips + var flen: usize = frag.len; + if (neg and !neg_after) flen -= 1; + if (!neg and neg_after) flen += 1; + var buf: [48]u8 = undefined; + // "{d}" prints '+' for positive signed ints — format the magnitude unsigned + const mag = std.fmt.bufPrint(&buf, "{d}", .{@abs(nv)}) catch unreachable; // a u128's 39 digits fit 48 + const sign_len: usize = @intFromBool(neg_after); + const want = if (pad) flen - sign_len else mag.len; + const digits_len = @max(want, mag.len); + const out = try alloc.alloc(u8, sign_len + digits_len); + var write: usize = 0; + if (neg_after) { + out[0] = '-'; + write = 1; + } + @memset(out[write..][0 .. digits_len - mag.len], '0'); + write += digits_len - mag.len; + @memcpy(out[write..], mag); + return out; +} + +test { + _ = Normal; +} + +test "lineStartOffset by blocks and offsetAt agree with a line at a time" { + var prng: std.Random.DefaultPrng = .init(0x9e3779b9); + const random = prng.random(); + var buf: [300]u8 = undefined; + for (0..2000) |_| { + const len = random.uintLessThan(usize, buf.len); + for (buf[0..len]) |*b| b.* = if (random.uintLessThan(u8, 4) == 0) '\n' else 'a'; + const text = buf[0..len]; + for (0..len + 3) |row| { + var off: usize = 0; + var r: usize = 0; + const want = while (r < row) : (r += 1) { + off = (std.mem.indexOfScalarPos(u8, text, off, '\n') orelse break text.len) + 1; + } else off; + try std.testing.expectEqual(want, lineStartOffset(text, row)); + const cursor: Cursor = .{ .row = row, .col = random.uintLessThan(usize, 6) }; + const clamped = lineStartOffset(text, @min(row, cursorLineCount(text) - 1)); + const end = std.mem.indexOfScalarPos(u8, text, clamped, '\n') orelse text.len; + try std.testing.expectEqual(clamped + graphemeStart(text[clamped..end], @min(cursor.col, end - clamped)), offsetAt(text, cursor)); + // lineSpan, as it was: a line at a time, null past the last. + const span: ?LineSpan = span: { + if (text.len == 0) break :span null; + var start: usize = 0; + for (0..row) |_| start = (std.mem.indexOfScalarPos(u8, text, start, '\n') orelse break :span null) + 1; + if (start == text.len and !(row == 0 or text[text.len - 1] == '\n')) break :span null; + break :span .{ .start = start, .end = std.mem.indexOfScalarPos(u8, text, start, '\n') orelse text.len }; + }; + try std.testing.expectEqual(span, lineSpan(text, row)); + } + } +} + +test "selection textobjects distinguish words and paragraph boundaries" { + const t = "alpha beta gamma\n"; + // miw mid-word + var r = selectWord(t, .{ .anchor = 8, .head = 9 }, false, false); + try std.testing.expectEqual(@as(usize, 6), r.anchor); + try std.testing.expectEqual(@as(usize, 10), r.head); + // maw on the space after "beta": collapses to the boundary + r = selectWord(t, .{ .anchor = 10, .head = 11 }, true, false); + try std.testing.expectEqual(@as(usize, 10), r.anchor); + try std.testing.expectEqual(@as(usize, 10), r.head); + const t2 = "aa\n\ncc\n"; + // mip from the blank line selects the NEXT paragraph + r = selectParagraph(t2, .{ .anchor = 3, .head = 4 }, false, 1); + try std.testing.expectEqual(@as(usize, 4), r.anchor); + try std.testing.expectEqual(@as(usize, 7), r.head); +} + +test "newline indent keeps levels and adds one after delimiters" { + try std.testing.expectEqual(@as(usize, 4), newlineIndentWidth(" value", 9)); + try std.testing.expectEqual(@as(usize, 8), newlineIndentWidth(" call()", 10)); + try std.testing.expectEqual(@as(usize, 8), newlineIndentWidth(" callback({}) ", 16)); + try std.testing.expectEqual(@as(usize, 4), newlineIndentWidth("work(", 5)); + try std.testing.expectEqual(@as(usize, 4), newlineIndentWidth("list[tail", 5)); +} + +test "word selections match Helix motions" { + const t = "alpha beta\n"; + // w from a fresh 1-wide cursor selects "alpha " (cursor on the space) + var r = moveWord(t, .{ .anchor = 0, .head = 1 }, 1, .next_word_start); + try std.testing.expectEqual(@as(usize, 0), r.anchor); + try std.testing.expectEqual(@as(usize, 6), r.head); + // e from the same start ends on 'a' of alpha + r = moveWord(t, .{ .anchor = 0, .head = 1 }, 1, .next_word_end); + try std.testing.expectEqual(@as(usize, 5), r.head); + try std.testing.expectEqual(@as(usize, 0), r.anchor); + // b from the w result selects "alpha" backward + r = moveWord(t, .{ .anchor = 6, .head = 10 }, 1, .prev_word_start); + try std.testing.expectEqual(@as(usize, 10), r.anchor); + try std.testing.expectEqual(@as(usize, 6), r.head); + // 2w on "one two three": anchor comes from the last hop only + const t2 = "one two three\n"; + r = moveWord(t2, .{ .anchor = 0, .head = 1 }, 2, .next_word_start); + try std.testing.expectEqual(@as(usize, 4), r.anchor); + try std.testing.expectEqual(@as(usize, 8), r.head); + // w at EOF collapses to a zero-width range at len + const t3 = "alpha\n"; + r = moveWord(t3, .{ .anchor = 0, .head = 5 }, 1, .next_word_start); + try std.testing.expectEqual(@as(usize, 6), r.head); + try std.testing.expectEqual(@as(usize, 6), r.anchor); + // W treats punct runs as word chars + const t4 = "foo.bar baz\n"; + r = moveWord(t4, .{ .anchor = 0, .head = 1 }, 1, .next_long_word_start); + try std.testing.expectEqual(@as(usize, 0), r.anchor); + try std.testing.expectEqual(@as(usize, 8), r.head); +} + +test "selection cursor keeps the anchor cell across crossings" { + const t = "abcdef\n"; + // forward range [2,3) extended left of the anchor: anchor cell stays 2 + var r = moveSelectionCursor(t, .{ .anchor = 2, .head = 3 }, 0, true); + try std.testing.expectEqual(@as(usize, 3), r.anchor); + try std.testing.expectEqual(@as(usize, 0), r.head); + try std.testing.expectEqual(@as(usize, 0), selectionCursor(t, r)); + // and back: cursor to 4 -> forward again, anchor gap back to 2 + r = moveSelectionCursor(t, r, 4, true); + try std.testing.expectEqual(@as(usize, 2), r.anchor); + try std.testing.expectEqual(@as(usize, 5), r.head); +} + +test "paragraph selections cross blank lines" { + const t = "aa\nbb\n\ncc\ndd\n\nee\n"; + // ]p from the top selects through the blank line to the next block + var r = moveParagraph(t, .{ .anchor = 0, .head = 1 }, 1, true, false); + try std.testing.expectEqual(@as(usize, 0), r.anchor); + try std.testing.expectEqual(@as(usize, 7), r.head); + // [p from "ee" (line 6, offset 14) goes back to "cc" block start + r = moveParagraph(t, .{ .anchor = 14, .head = 15 }, 1, false, false); + try std.testing.expectEqual(@as(usize, 14), r.anchor); + try std.testing.expectEqual(@as(usize, 7), r.head); +} + +test "find targets count matches and skip adjacent till targets" { + const t = "abcabc\n"; + try std.testing.expectEqual(@as(usize, 2), findTarget(t, 0, 'c', .forward, 1).?); + try std.testing.expectEqual(@as(usize, 5), findTarget(t, 0, 'c', .forward, 2).?); + try std.testing.expectEqual(@as(usize, 1), findTarget(t, 0, 'c', .till_forward, 1).?); + // till repeat skips the adjacent target: from cell 1, next tc reaches 4 + try std.testing.expectEqual(@as(usize, 4), findTarget(t, 1, 'c', .till_forward, 1).?); + try std.testing.expectEqual(@as(usize, 3), findTarget(t, 5, 'a', .backward, 1).?); + try std.testing.expectEqual(@as(usize, 4), findTarget(t, 5, 'a', .till_backward, 1).?); + try std.testing.expectEqual(@as(?usize, null), findTarget(t, 0, 'z', .forward, 1)); +} + +test "extended grapheme boundaries cover combining emoji flag and CJK text" { + const text = "a" ++ "e\u{301}" ++ "👩🏽\u{200d}🚀" ++ "🇧🇷" ++ "界"; + const boundaries = [_]usize{ 0, 1, 4, 19, 27, 30 }; + for (boundaries[0 .. boundaries.len - 1], boundaries[1..]) |start, end| { + try std.testing.expectEqual(end, nextGrapheme(text, start)); + try std.testing.expectEqual(start, prevGrapheme(text, end)); + } + // Stale byte offsets are repaired to a cluster boundary instead of being + // allowed to leak continuation bytes into cursor state. + try std.testing.expectEqual(@as(usize, 1), graphemeStart(text, 2)); + try std.testing.expectEqual(@as(usize, 1), prevGrapheme(text, 3)); + try std.testing.expectEqual(@as(usize, 19), graphemeAtColumn(text, 3)); +} + +test "the ASCII arms of graphemeStart and nextGrapheme agree with the UAX #29 walk" { + // Compare every offset against segmentation with the ASCII fast paths removed. + const H = struct { + // `graphemeStart` with the ASCII arm deleted — nothing else changed. + fn start(text: []const u8, off: usize) usize { + const bounded = @min(off, text.len); + if (bounded == text.len) return text.len; + var it = uucode.grapheme.utf8Iterator(text); + while (it.nextGrapheme()) |g| { + if (bounded < g.end) return g.start; + } + return text.len; + } + // `nextGrapheme` with the ASCII arm deleted. + fn next(text: []const u8, off: usize) usize { + if (off >= text.len) return text.len; + var s = off; + while (s > 0 and (text[s] & 0xC0) == 0x80) s -= 1; + if (s != off) s = start(text, off); + var it = uucode.grapheme.utf8Iterator(text[s..]); + const g = it.nextGrapheme() orelse return @min(s + 1, text.len); + return s + g.end; + } + fn check(text: []const u8) !void { + var off: usize = 0; + while (off <= text.len + 2) : (off += 1) { + std.testing.expectEqual(start(text, off), graphemeStart(text, off)) catch |e| { + std.debug.print("graphemeStart({any}, {d})\n", .{ text, off }); + return e; + }; + std.testing.expectEqual(next(text, off), nextGrapheme(text, off)) catch |e| { + std.debug.print("nextGrapheme({any}, {d})\n", .{ text, off }); + return e; + }; + } + } + }; + + // Combining marks, ZWJ, spacing marks, selectors, wide glyphs, flags, and invalid UTF-8. + const neighbours = [_][]const u8{ + "", "a", "\u{301}", "\u{200d}\u{1f680}", + "\u{903}", "\u{fe0f}", "\u{20e3}", "\u{4e16}\u{754c}", + "\u{1f642}", "\u{1f1e6}\u{1f1e7}", "\xff", "\xe4\xb8", + "\xe4\x28\xb8", + }; + // Every ASCII byte paired with each Unicode or invalid neighbor, in both orders. + var buf: [16]u8 = undefined; + var b: u8 = 0; + while (b < 0x80) : (b += 1) { + buf[0] = b; + for (neighbours) |tail| { + @memcpy(buf[1..][0..tail.len], tail); + try H.check(buf[0 .. 1 + tail.len]); + // ...and the same byte as a follower, so a boundary is probed from both sides. + @memcpy(buf[0..tail.len], tail); + buf[tail.len] = b; + try H.check(buf[0 .. tail.len + 1]); + } + } + + for ([_][]const u8{ "a\r", "\ra", "\n\r", "a\rb\nc" }) |text| try H.check(text); + + // Mixed text long enough that a fast-path run starts, ends and restarts inside one string. + try H.check("plain ascii then \u{4e16}\u{754c} then e\u{301} then more ascii"); +} + +// CR-LF must have identical boundaries in forward, backward, and containing-cluster queries. +test "GB3 keeps CR-LF one cluster for every grapheme step" { + const text = "a\r\nb"; + // The reference: the same segmentation the slow arms of these functions run. + var it = uucode.grapheme.utf8Iterator(text); + var starts: [8]usize = undefined; + var ends: [8]usize = undefined; + var n: usize = 0; + while (it.nextGrapheme()) |g| : (n += 1) { + starts[n] = g.start; + ends[n] = g.end; + } + for (starts[0..n], ends[0..n]) |start, end| { + try std.testing.expectEqual(start, graphemeStart(text, start)); + try std.testing.expectEqual(end, nextGrapheme(text, start)); + try std.testing.expectEqual(start, prevGrapheme(text, end)); + } +} + +test "Unicode find and word motion stay on grapheme boundaries" { + try std.testing.expectEqual(@as(usize, 3), findTarget("\u{e9}x\u{e9}", 0, 'é', .forward, 1).?); + + const text = "café 世界 ok\n"; + const first = moveWord(text, .{ .anchor = 0, .head = 1 }, 1, .next_word_start); + try std.testing.expectEqual(@as(usize, 0), first.anchor); + try std.testing.expectEqual(@as(usize, 6), first.head); + const second = moveWord(text, .{ .anchor = 0, .head = 1 }, 2, .next_word_start); + try std.testing.expectEqual(@as(usize, 6), second.anchor); + try std.testing.expectEqual(@as(usize, 13), second.head); + + // Long-word motions split on Unicode whitespace, not only ASCII spaces. + const nbsp = "alpha\u{a0}beta\n"; + const long = moveWord(nbsp, .{ .anchor = 0, .head = 1 }, 1, .next_long_word_start); + try std.testing.expectEqual(@as(usize, 7), long.head); +} + +test "decimal increment preserves padding and handles sign changes" { + const a = std.testing.allocator; + { + const r = (try incrementDecimal(a, "15", 1)).?; + defer a.free(r); + try std.testing.expectEqualStrings("16", r); + } + { + const r = (try incrementDecimal(a, "007", 1)).?; + defer a.free(r); + try std.testing.expectEqualStrings("008", r); + } + { + const r = (try incrementDecimal(a, "-3", 1)).?; + defer a.free(r); + try std.testing.expectEqualStrings("-2", r); + } + { + const r = (try incrementDecimal(a, "9", -10)).?; + defer a.free(r); + try std.testing.expectEqualStrings("-1", r); + } + try std.testing.expectEqual(@as(?[]u8, null), try incrementDecimal(a, "a 1", 1)); + try std.testing.expectEqual(@as(?[]u8, null), try incrementDecimal(a, "", 1)); +} + +test "codepoint classes distinguish words punctuation and whitespace" { + try std.testing.expectEqual(WordClass.word, kindOfCodepoint('a')); + try std.testing.expectEqual(WordClass.word, kindOfCodepoint('_')); + try std.testing.expectEqual(WordClass.word, kindOfCodepoint('9')); + try std.testing.expectEqual(WordClass.punct, kindOfCodepoint('.')); + try std.testing.expectEqual(WordClass.punct, kindOfCodepoint('(')); + try std.testing.expectEqual(WordClass.ws, kindOfCodepoint(' ')); + try std.testing.expectEqual(WordClass.ws, kindOfCodepoint('\n')); +} + +test "insert cursor steps characters" { + const lines = [_][]const u8{ "alpha beta", " two words", "x" }; + const c = Cursor{ .row = 0, .col = 5 }; + try std.testing.expectEqual(Cursor{ .row = 0, .col = 4 }, charLeft(&.{"hello"}, c)); + try std.testing.expectEqual(Cursor{ .row = 0, .col = 6 }, charRight(&lines, c)); +} + +test "lineSpan agrees with the whole-document scans it replaces" { + for ([_][]const u8{ "", "a", "a\n", "a\nbb\n", "a\nbb\nccc", "\n", "\n\n" }) |content| { + const n = lineCount(content); + var row: usize = 0; + while (row < n) : (row += 1) { + const span = lineSpan(content, row) orelse { + std.debug.print("row {d} of {s} missing\n", .{ row, content }); + return error.MissingRow; + }; + try std.testing.expectEqual(lineStartOffset(content, row), span.start); + try std.testing.expectEqualStrings(lineSlice(content, row), content[span.start..span.end]); + } + // One past the last line must be absent, which is what lets insertAt clamp. + try std.testing.expectEqual(@as(?LineSpan, null), lineSpan(content, n)); + } +} + +test "insertAt still clamps a row past the end onto the last line" { + const gpa = std.testing.allocator; + const content = "a\nbb\nccc"; + const out = try insertAt(gpa, content, .{ .row = 99, .col = 99 }, "X"); + defer gpa.free(out); + try std.testing.expectEqualStrings("a\nbb\ncccX", out); + + const mid = try insertAt(gpa, content, .{ .row = 1, .col = 1 }, "X"); + defer gpa.free(mid); + try std.testing.expectEqualStrings("a\nbXb\nccc", mid); +} + +test "lineStartOffset + lineSlice" { + const content = "alpha\nbeta\n\ngamma"; + try std.testing.expectEqual(@as(usize, 0), lineStartOffset(content, 0)); + try std.testing.expectEqual(@as(usize, 6), lineStartOffset(content, 1)); + try std.testing.expectEqual(@as(usize, 11), lineStartOffset(content, 2)); + try std.testing.expectEqual(@as(usize, 12), lineStartOffset(content, 3)); + try std.testing.expectEqual(@as(usize, 17), lineStartOffset(content, 4)); // past end + try std.testing.expectEqualStrings("alpha", lineSlice(content, 0)); + try std.testing.expectEqualStrings("beta", lineSlice(content, 1)); + try std.testing.expectEqualStrings("", lineSlice(content, 2)); + try std.testing.expectEqualStrings("gamma", lineSlice(content, 3)); + try std.testing.expectEqual(@as(usize, 4), lineCount(content)); +} + +test "insertAt mid-line and at end" { + const content = "hello world"; + const a = std.testing.allocator; + const r1 = try insertAt(a, content, .{ .row = 0, .col = 5 }, "!"); + defer a.free(r1); + try std.testing.expectEqualStrings("hello! world", r1); + const r2 = try insertAt(a, content, .{ .row = 0, .col = 99 }, "!"); + defer a.free(r2); + try std.testing.expectEqualStrings("hello world!", r2); +} + +test "insertAt multiline creates lines" { + const content = "a\nb"; + const a = std.testing.allocator; + const r = try insertAt(a, content, .{ .row = 0, .col = 1 }, "X\nY"); + defer a.free(r); + try std.testing.expectEqualStrings("aX\nY\nb", r); + try std.testing.expectEqual(@as(usize, 3), lineCount(r)); +} + +test "deleteLines middle" { + const content = "one\ntwo\nthree\nfour"; + const a = std.testing.allocator; + const d = try deleteLines(a, content, 1, 2); + defer a.free(d); + try std.testing.expectEqualStrings("one\nfour", d); +} + +test "deleteLines last line drops preceding newline" { + const content = "one\ntwo\nthree"; + const a = std.testing.allocator; + const d = try deleteLines(a, content, 2, 2); + defer a.free(d); + try std.testing.expectEqualStrings("one\ntwo", d); +} + +test "deleteLines only line" { + const content = "only"; + const a = std.testing.allocator; + const d = try deleteLines(a, content, 0, 0); + defer a.free(d); + try std.testing.expectEqualStrings("", d); +} + +test "matchBracket nesting both directions" { + const lines = [_][]const u8{"a (b (c) d) e"}; + const w = &lines; + try std.testing.expectEqual(Cursor{ .row = 0, .col = 10 }, matchBracket(w, .{ .row = 0, .col = 2 }).?); + try std.testing.expectEqual(Cursor{ .row = 0, .col = 2 }, matchBracket(w, .{ .row = 0, .col = 10 }).?); + try std.testing.expectEqual(Cursor{ .row = 0, .col = 7 }, matchBracket(w, .{ .row = 0, .col = 5 }).?); + try std.testing.expectEqual(@as(?Cursor, null), matchBracket(w, .{ .row = 0, .col = 0 })); +} + +test "matchBracket across lines" { + const lines = [_][]const u8{ "if (x) {", " y", "}" }; + const w = &lines; + try std.testing.expectEqual(Cursor{ .row = 2, .col = 0 }, matchBracket(w, .{ .row = 0, .col = 7 }).?); + try std.testing.expectEqual(Cursor{ .row = 0, .col = 7 }, matchBracket(w, .{ .row = 2, .col = 0 }).?); +} + +test "enclosing pair includes delimiters and chooses the nearest nested pair" { + const lines = [_][]const u8{"f(a, (b))"}; + const w = &lines; + const around = enclosingPair(w, .{ .row = 0, .col = 3 }, '(', ')').?; + try std.testing.expectEqual(Cursor{ .row = 0, .col = 1 }, around.a); + try std.testing.expectEqual(Cursor{ .row = 0, .col = 8 }, around.b); + const nested = enclosingPair(w, .{ .row = 0, .col = 5 }, '(', ')').?; + try std.testing.expectEqual(Cursor{ .row = 0, .col = 5 }, nested.a); + try std.testing.expectEqual(Cursor{ .row = 0, .col = 7 }, nested.b); + const empty = [_][]const u8{"()"}; + const pair = enclosingPair(&empty, .{ .row = 0, .col = 0 }, '(', ')').?; + try std.testing.expectEqual(Cursor{ .row = 0, .col = 0 }, pair.a); + try std.testing.expectEqual(Cursor{ .row = 0, .col = 1 }, pair.b); + try std.testing.expectEqual(@as(?Range, null), enclosingPair(&empty, .{ .row = 0, .col = 1 }, '[', ']')); +} + +test "enclosing quote stays on its line" { + const lines = [_][]const u8{"say 'hi there' end"}; + const w = &lines; + const r = enclosingQuote(w, .{ .row = 0, .col = 7 }, '\'').?; + try std.testing.expectEqual(Cursor{ .row = 0, .col = 4 }, r.a); + try std.testing.expectEqual(Cursor{ .row = 0, .col = 13 }, r.b); + try std.testing.expectEqual(@as(?Range, null), enclosingQuote(w, .{ .row = 0, .col = 16 }, '\'')); + try std.testing.expectEqual(@as(?Range, null), enclosingQuote(&.{ "'open", "close'" }, .{}, '\'')); +} + +test "advanceBy" { + try std.testing.expectEqual(Cursor{ .row = 0, .col = 5 }, advanceBy(.{ .row = 0, .col = 2 }, "abc")); + try std.testing.expectEqual(Cursor{ .row = 2, .col = 1 }, advanceBy(.{ .row = 0, .col = 2 }, "a\nbc\nd")); +} + +test "changeCase" { + const a = std.testing.allocator; + const t = try changeCase(a, "aB cD", 0, 5, .toggle); + defer a.free(t); + try std.testing.expectEqualStrings("Ab Cd", t); + const lo = try changeCase(a, "AB CD", 0, 2, .lower); + defer a.free(lo); + try std.testing.expectEqualStrings("ab CD", lo); + const up = try changeCase(a, "ab cd", 3, 5, .upper); + defer a.free(up); + try std.testing.expectEqualStrings("ab CD", up); +} + +test "deleteSpan including the newline" { + const a = std.testing.allocator; + const r = try deleteSpan(a, "hello world", .{ .row = 0, .col = 2 }, .{ .row = 0, .col = 5 }); + defer a.free(r); + try std.testing.expectEqualStrings("he world", r); + const j = try deleteSpan(a, "ab\ncd", .{ .row = 0, .col = 2 }, .{ .row = 1, .col = 0 }); + defer a.free(j); + try std.testing.expectEqualStrings("abcd", j); + // empty span: copy + const e = try deleteSpan(a, "ab", .{ .row = 0, .col = 1 }, .{ .row = 0, .col = 1 }); + defer a.free(e); + try std.testing.expectEqualStrings("ab", e); +} + +test "pasteLineBelow" { + const content = "one\ntwo"; + const a = std.testing.allocator; + const r = try pasteLineBelow(a, content, 0, "INSERTED"); + defer a.free(r); + try std.testing.expectEqualStrings("one\nINSERTED\ntwo", r); + // paste below last line + const r2 = try pasteLineBelow(a, content, 1, "END"); + defer a.free(r2); + try std.testing.expectEqualStrings("one\ntwo\nEND", r2); + // multiline yanked text + const r3 = try pasteLineBelow(a, content, 0, "a\nb"); + defer a.free(r3); + try std.testing.expectEqualStrings("one\na\nb\ntwo", r3); +} -- cgit v1.3