From 4a33b8d2bd409d4a06205f28e0eb1b4cdaf49889 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:51:40 -0300 Subject: hunks: the +Diff's editor side in one place: openDiff, showDiffText and diffSource move from Output.zig, the `Diff` word's body and PaneKind.diff's test from builtins.zig, lookCommit (as lookedAtCommit) and its test from look.zig; builtins.zig's Diff is its one registration (its doc, traits and `run = hunks.word`); the three open hooks a plumber's rules would replace are a call each, look.zig's `hunks.lookedAt` (a bare look at a .diff or .patch) and `hunks.lookedAtCommit` (a commit hash) and ninep/ctl.zig's `hunks.paged` (a +Pager filled with a diff), their conditions inside them; the diff language (diff.zig, syntax.zig's highlightDiff, look's diff-line address) stays where the languages are; code locality's far share 0.575 before and after (codeloc.py --compare); goldens unchanged Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/look.zig | 57 +++------------------------------------------------------ 1 file changed, 3 insertions(+), 54 deletions(-) (limited to 'src/look.zig') diff --git a/src/look.zig b/src/look.zig index ab77773f..115e3683 100644 --- a/src/look.zig +++ b/src/look.zig @@ -4,6 +4,7 @@ const builtin = @import("builtin"); const libc = std.c; const pardes = @import("pardes.zig"); const exec = @import("exec.zig"); +const hunks = @import("hunks.zig"); const config = @import("config.zig"); const diff = @import("diff.zig"); const platform_has_fs = filesystem.platform_has_fs; @@ -1089,7 +1090,7 @@ fn lookUnresolved(p: *Pardes, id: usize, pane: *Pane, trimmed: []const u8, text: // 9P is no keyboard input, and searches whatever the key mode. if (pane.addon != null or (pane.body.mode == .tty and !p.fs.serving)) return; if (text.len == 0) return; - if (lookCommit(p, id, pane, trimmed)) return; + if (hunks.lookedAtCommit(p, id, pane, trimmed)) return; if (pane.file != null) return lookWord(p, id, pane, text); p.word_look = true; defer p.word_look = false; @@ -1110,38 +1111,6 @@ fn lookUnresolved(p: *Pardes, id: usize, pane: *Pane, trimmed: []const u8, text: } } -/// A commit hash (7 to 40 hex digits, a letter and a digit among them) -/// looked at inside a git repository: `git show` it, paged into a +Pager, -/// which stacks its hunks in a +Diff (ctl.fillPager); the command pane -/// goes when it succeeds (Pane.commandDone). -// ponytail: a hex word that names no commit leaves git's error in a command pane, not a search; check the object first if that bites. -fn lookCommit(p: *Pardes, id: usize, pane: *Pane, word: []const u8) bool { - if (!isCommitHash(word)) return false; - var dir = filesystem.localPath(Pardes.paneDir(pane)) orelse return false; - while (true) { - var buf: [4096]u8 = undefined; - const dot_git = std.fmt.bufPrint(&buf, "{s}/.git", .{dir}) catch return false; - if (exec.kindOf(dot_git) != null) break; - dir = std.Io.Dir.path.dirname(dir) orelse return false; - } - var line: [64]u8 = undefined; - const dst = exec.execute(p, id, std.fmt.bufPrint(&line, "git show {s}", .{word}) catch return false) orelse return true; - if (p.panes[dst]) |c| c.command_pages = true; - return true; -} - -fn isCommitHash(word: []const u8) bool { - if (word.len < 7 or word.len > 40) return false; - var digit = false; - var letter = false; - for (word) |c| switch (c) { - '0'...'9' => digit = true, - 'a'...'f' => letter = true, - else => return false, - }; - return digit and letter; -} - /// Opens (or finds) `path` as Look would, `id`'s own pane when it is empty, /// and selects what `expr` addresses there from its dot. An address that /// does not evaluate moves nothing and says why (acme: no jump). @@ -1634,18 +1603,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void { const before = v.active; var opened: ?usize = null; if (!focusPaneByPath(p, target.path, target.at)) { - // A .diff or .patch looked at bare: its hunks stacked in a - // +Diff, which takes the keyboard as the file would; one - // with none in it opens as text. - if (!p.look_text and target.at.line == 0 and - (std.mem.endsWith(u8, target.path, ".diff") or std.mem.endsWith(u8, target.path, ".patch"))) - if (filesystem.read(p, target.path)) |bytes| { - defer p.gpa.free(bytes); - if (panes.Output.openDiff(p, id, target.path, bytes)) |hid| { - v.active = hid; - return; - } else |_| {} - } else |_| {}; + if (hunks.lookedAt(p, id, target.path, target.at)) return; const free = p.freeSlot() orelse return p.reportError(id, "look", error.NoPaneSlots); const nt = panes.File.open(p, free, target.path, target.at.line) catch |err| return lookOpenFailed(p, id, target.path, err); @@ -1915,15 +1873,6 @@ test "a zero column after a path is found, in every spelling a look takes" { for ([_][]const u8{ "", ":3", ":3:1", ":3:2-5", ":3:2-5:7", ":30:10" }) |rest| try std.testing.expect(!zeroColumn(rest)); } -test "a commit hash is 7 to 40 lowercase hex digits with a letter and a digit" { - try std.testing.expect(isCommitHash("9bffa7041c")); - try std.testing.expect(isCommitHash("9bffa7041cd2e8a0b6b1f7c3e4d5a6b7c8d9e0f1")); - try std.testing.expect(!isCommitHash("9bffa7")); - try std.testing.expect(!isCommitHash("defaced")); - try std.testing.expect(!isCommitHash("1234567")); - try std.testing.expect(!isCommitHash("0x9bffa7041c")); - try std.testing.expect(!isCommitHash("9BFFA7041C")); -} test "Look hover waits without mutating the pane and input cancels it" { const delay = config.look_preview_delay_frames; -- cgit v1.3