From 4423bc2f88fe2e88202056d6a11f759b4b143b74 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 23:02:01 -0300 Subject: modal, selection_pipe, syntax, sam_edit: helpers used once are inlined where they are used (consumeCount, clampLineCol, codepointAtCursor, isWordBoundary, wmIsPrev, tokenStopped, dropDiffPieces, Exec.text), and moveWord's doc comment goes back above moveWord Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/modal.zig | 52 ++++++++++++++------------------------------------ src/sam_edit.zig | 16 ++++++---------- src/selection_pipe.zig | 16 +++++++--------- src/syntax.zig | 14 +++++--------- 4 files changed, 32 insertions(+), 66 deletions(-) diff --git a/src/modal.zig b/src/modal.zig index 7e10a877..e123d468 100644 --- a/src/modal.zig +++ b/src/modal.zig @@ -412,12 +412,6 @@ pub const Normal = struct { return .{ .action = value }; } - fn consumeCount(state: *State) Counted { - const count = state.count; - state.count = 0; - return .{ .count = @max(1, count), .explicit = count != 0 }; - } - fn armPrefix(state: *State, prefix: Prefix, saved_count: u32) Result { state.prefix = prefix; state.count = saved_count; @@ -448,7 +442,8 @@ pub const Normal = struct { return .pending; } - const counted = consumeCount(state); + const counted: Counted = .{ .count = @max(1, state.count), .explicit = state.count != 0 }; + state.count = 0; const count = counted.count; switch (state.prefix) { @@ -846,22 +841,11 @@ pub fn charRight(lines: []const []const u8, c: Cursor) Cursor { return .{ .row = c.row, .col = if (c.col < last) @min(nextGrapheme(lines[c.row], c.col), last) else last }; } -fn clampLineCol(line: []const u8, col: usize) usize { - if (line.len == 0) return 0; - const last = prevGrapheme(line, line.len); - return graphemeStart(line, @min(col, last)); -} - -// the character the cursor sits on; line terminators / EOF read as '\n'. -fn codepointAtCursor(lines: []const []const u8, c: Cursor) u21 { - if (c.row >= lines.len) return '\n'; - const line = lines[c.row]; - if (c.col >= line.len) return '\n'; - return codepointAt(line, graphemeStart(line, c.col)); -} - +// 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 { - const cp = codepointAtCursor(lines, c); + 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; } @@ -949,7 +933,7 @@ pub fn clampToChar(lines: []const []const u8, c: Cursor) Cursor { } const llen = lineLenOf(lines, c.row); if (llen == 0) return .{ .row = c.row, .col = 0 }; - return .{ .row = c.row, .col = clampLineCol(lines[c.row], c.col) }; + return .{ .row = c.row, .col = graphemeStart(lines[c.row], @min(c.col, prevGrapheme(lines[c.row], llen))) }; } pub fn lineCount(content: []const u8) usize { @@ -1494,10 +1478,6 @@ fn isWordWhitespace(c: WordClass) bool { // Rust char::is_whitespace (includes l return c == .ws or c == .eol; } -fn isWordBoundary(a: WordClass, b: WordClass) bool { - return a != b; -} - fn isLongWordBoundary(a: WordClass, b: WordClass) bool { if ((a == .word and b == .punct) or (a == .punct and b == .word)) return false; return a != b; @@ -1505,21 +1485,13 @@ fn isLongWordBoundary(a: WordClass, b: WordClass) bool { fn reachedWordTarget(target: WordTarget, prev: WordClass, next: WordClass) bool { return switch (target) { - .next_word_start, .prev_word_end => isWordBoundary(prev, next) and (next == .eol or !isWordWhitespace(next)), - .next_word_end, .prev_word_start => isWordBoundary(prev, next) and (!isWordWhitespace(prev) or next == .eol), + .next_word_start, .prev_word_end => 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, .prev_long_word_end => 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), }; } -fn wmIsPrev(t: WordTarget) bool { - return switch (t) { - .prev_word_start, .prev_word_end, .prev_long_word_start, .prev_long_word_end => true, - else => false, - }; -} - -/// w/b/e/W/B/E: helix word_move — each step selects the traversed span. /// 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 @@ -1567,8 +1539,12 @@ pub fn jumpWords(text: []const u8, cursor: usize, start: usize, end: usize, out: 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 = wmIsPrev(target); + const is_prev = switch (target) { + .prev_word_start, .prev_word_end, .prev_long_word_start, .prev_long_word_end => true, + else => false, + }; 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) diff --git a/src/sam_edit.zig b/src/sam_edit.zig index 73cd6ecd..b06379f9 100644 --- a/src/sam_edit.zig +++ b/src/sam_edit.zig @@ -582,15 +582,11 @@ const Exec = struct { return std.math.cast(u32, n) orelse std.math.maxInt(u32); } - fn text(ex: *const Exec, fi: usize) []const u8 { - return ex.files.items[fi].text; - } - /// An address in file `fi` from `dot`, or `dot` when there is none /// (aDot). fn address(ex: *Exec, fi: usize, expr: ?[]const u8, dot: Range) Failure!Range { const e = expr orelse return dot; - var a: addr_lang.Addr = .{ .text = ex.text(fi), .lim = null, .expr = e }; + var a: addr_lang.Addr = .{ .text = ex.files.items[fi].text, .lim = null, .expr = e }; // A miss names the pattern that missed, not the whole Edit line. const r = a.address(dot) orelse return if (std.mem.eql(u8, a.err, addr_lang.e_no_match)) fail(ex.why, "no match for regexp {s}", .{e}) @@ -726,7 +722,7 @@ const Exec = struct { } // w's default address is the whole file (aAll). const r = if (c.c == 'w' and c.addr == null) - Range{ .q0 = 0, .q1 = clip(ex.text(fi).len) } + Range{ .q0 = 0, .q1 = clip(ex.files.items[fi].text.len) } else try ex.address(fi, c.addr, dot); const d = try ex.execIn(c, fi, r); @@ -738,7 +734,7 @@ const Exec = struct { /// The command itself, at `r` in file `fi`. fn execIn(ex: *Exec, c: *Cmd, fi: usize, r: Range) Failure!Range { - const txt = ex.text(fi); + const txt = ex.files.items[fi].text; switch (c.c) { '\n' => return r, 'a' => { @@ -948,7 +944,7 @@ const Exec = struct { /// ecmd.c:443, s_cmd. fn substitute(ex: *Exec, c: *Cmd, fi: usize, r: Range) Failure!Range { - const txt = ex.text(fi); + const txt = ex.files.items[fi].text; var rx = try ex.compile(c.re.?, 's'); var n = c.num; var op: ?usize = null; @@ -992,7 +988,7 @@ const Exec = struct { /// ecmd.c:796, looper: the matches (x) or what lies between them (y). fn looper(ex: *Exec, pat: []const u8, fi: usize, r: Range, xy: bool) Failure![]Range { - const txt = ex.text(fi); + const txt = ex.files.items[fi].text; var rx = try ex.compile(pat, if (xy) 'x' else 'y'); var out: std.ArrayList(Range) = .empty; var op: ?usize = if (xy) null else r.q0; @@ -1026,7 +1022,7 @@ const Exec = struct { /// ecmd.c:840, linelooper: each line in the range, the first from where /// the range starts, the last cut where it ends. fn lines(ex: *Exec, fi: usize, r: Range) Failure![]Range { - const txt = ex.text(fi); + const txt = ex.files.items[fi].text; var out: std.ArrayList(Range) = .empty; var p: usize = r.q0; while (p < r.q1) { diff --git a/src/selection_pipe.zig b/src/selection_pipe.zig index 207d45c2..7bb919e1 100644 --- a/src/selection_pipe.zig +++ b/src/selection_pipe.zig @@ -505,12 +505,6 @@ pub fn runIn( return .{ .ok = stdout }; } -fn tokenStopped(token: u32) bool { - Running.take(); - defer Running.lock.unlock(); - return Running.isStopped(token); -} - /// Invoke the command independently for every selection. The response is all /// or nothing: one failure frees every earlier stdout and returns a failed, /// empty answer for the core to ignore. @@ -522,9 +516,13 @@ pub fn runJob(gpa: std.mem.Allocator, io: std.Io, job: *const Job) Response { for (job.inputs, 0..) |input, i| { const command = if (job.commands.len > 0) job.commands[i] else job.command; const cwd = if (job.cwds.len > 0) job.cwds[i] else job.cwd; - if (job.token != 0 and tokenStopped(job.token)) { - response.failure = .{ .kind = .signal, .index = @intCast(i) }; - break; + if (job.token != 0) { + Running.take(); + defer Running.lock.unlock(); + if (Running.isStopped(job.token)) { + response.failure = .{ .kind = .signal, .index = @intCast(i) }; + break; + } } const output = switch (runIn(gpa, io, job.shell, command, cwd, input, job.token, job.env)) { .ok => |bytes| bytes, diff --git a/src/syntax.zig b/src/syntax.zig index 858ef6a2..acbe543a 100644 --- a/src/syntax.zig +++ b/src/syntax.zig @@ -188,7 +188,11 @@ pub fn stop() void { spec.objects = null; spec.objects_failed = false; } - dropDiffPieces(); + for (&diff_pieces) |*slot| { + if (slot.*) |piece| syntax_allocator.free(piece.styles); + slot.* = null; + } + diff_clock = 0; ts_set_allocator(null, null, null, null); syntax_started = false; syntax_allocator = undefined; @@ -1181,14 +1185,6 @@ const DiffPiece = struct { hash: u64, styles: []u8, used: u64 }; var diff_pieces: [96]?DiffPiece = @splat(null); var diff_clock: u64 = 0; -fn dropDiffPieces() void { - for (&diff_pieces) |*slot| { - if (slot.*) |piece| syntax_allocator.free(piece.styles); - slot.* = null; - } - diff_clock = 0; -} - /// The styles of `content[start..end)` read as a unified diff: headers /// muted, `@@` lines as keywords, and each hunk's code in the language of /// the file its section names. A hunk's old side (context and removed -- cgit v1.3