summaryrefslogtreecommitdiff
path: root/src/look.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 02:31:20 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commitf749eb712d990a9b03f1db372d5311e77bd32edd (patch)
treeee4d9966f1eb293054b7ed865c9d4d6eb49e432b /src/look.zig
parent8f6621f9f70db86d56266a59b56dfb92b324b0d9 (diff)
parentb3d32656413734952bc918c2d4cfd12fede6c214 (diff)
downloadpardes-f749eb712d990a9b03f1db372d5311e77bd32edd.tar.gz
pardes-f749eb712d990a9b03f1db372d5311e77bd32edd.zip
integration: the terminal behind src/Term.zig, with the diff viewer and the plumber's first phase
A merge of term-phase0 and pardes-next. File.zig's recolorSyntax keeps the theme's grounds and names Term.Filter and Term.Span; the changelog keeps both sides' entries, the palette sentence in the place of the entry the terminal work cut down. core-test 985/985 on tui; the diff, pager and tree process cases pass, goldens unchanged. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/look.zig')
-rw-r--r--src/look.zig106
1 files changed, 35 insertions, 71 deletions
diff --git a/src/look.zig b/src/look.zig
index 082565ed..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).
@@ -1390,18 +1359,32 @@ pub fn diffAddress(pane: *const Pane, lines: []const []const u8, row: usize, buf
return target.address(buf);
}
-/// What a click at byte `col` of line `row` of a diff pane takes, the one
-/// answer for every way a click arrives (a mouse click, a 9P look of the
-/// whole line, an empty look or exec written back to `event`): a file or
-/// hunk header anywhere, or a hunk line on its `+`/`-`/` ` prefix, is the
-/// address the line names (diffAddress); a hunk line's code is words, from
-/// `word_from` on, the prefix never one of them. Null when the pane shows
-/// no diff or the line is no part of one.
-pub const DiffClick = struct { address: ?[]const u8 = null, word_from: usize = 0 };
+/// What a click at byte `col` of line `row` of a diff pane takes: a file
+/// or hunk header anywhere, or a hunk line on its `+`/`-`/` ` prefix, is
+/// the address the line names (diffAddress); a hunk line's code is words,
+/// from `word_from` on, the prefix never one of them. Null when the line is
+/// no part of a diff.
+const DiffClick = struct { address: ?[]const u8 = null, word_from: usize = 0 };
+
+/// What a click at byte `col` of row `row` takes, the one answer for every
+/// way a click arrives (the mouse, exec.operandAt; an empty look written
+/// back to `event`, events.expandAt; a whole line written to `look`): the
+/// address a row's producer gave it (a query pane's row on its address,
+/// Query.rowAddress), else the one the pane's language reads in the line (a
+/// diff's: diffClickIn); or where its words start, the address no part of
+/// them. `line` is the row's text it was read from, and the address's text
+/// in it ends at `end`. Null for a row that names no address.
+pub const RowAddress = struct { address: ?[]const u8 = null, word_from: usize = 0, line: []const u8, end: usize };
-pub fn diffClick(p: *Pardes, pane: *Pane, row: usize, col: usize, buf: []u8) ?DiffClick {
+pub fn rowAddressAt(p: *Pardes, pane: *Pane, row: usize, col: usize, buf: []u8) ?RowAddress {
+ if (panes.Query.rowAddress(pane, row, col, buf)) |address| {
+ const f = &pane.file.?;
+ return .{ .address = address, .line = modal.lineSlice(f.content, row), .end = f.location_rows[row].location_end };
+ }
const lines = diffLines(p, pane) orelse return null;
- return diffClickIn(pane, lines, row, col, buf);
+ if (row >= lines.len) return null;
+ const click = diffClickIn(pane, lines, row, col, buf) orelse return null;
+ return .{ .address = click.address, .word_from = click.word_from, .line = lines[row], .end = lines[row].len };
}
fn diffClickIn(pane: *const Pane, lines: []const []const u8, row: usize, col: usize, buf: []u8) ?DiffClick {
@@ -1417,18 +1400,19 @@ fn diffClickIn(pane: *const Pane, lines: []const []const u8, row: usize, col: us
return .{ .address = address };
}
-/// A look written over 9P of a whole line of a diff pane (the cursor's line
-/// first, then the first after it): what a click on its first column takes.
+/// A look written over 9P of a whole line of a pane whose rows name
+/// addresses (a diff's, a query pane's: rowAddressAt), the cursor's line
+/// first, then the first after it: what a click on its first column takes.
/// A blank context line (` `) is a line too.
-fn diffLineAddress(p: *Pardes, pane: *Pane, operand: []const u8, buf: []u8) ?[]const u8 {
+fn lineAddress(p: *Pardes, pane: *Pane, operand: []const u8, buf: []u8) ?[]const u8 {
const text = std.mem.trimEnd(u8, operand, "\r\n");
if (text.len == 0) return null;
- const lines = diffLines(p, pane) orelse return null;
+ const lines = (if (pane.query != null) panes.File.cursorLines(p.scratch.allocator(), pane, &pane.file.?) catch null else diffLines(p, pane)) orelse return null;
const cur: usize = @intCast(@max(0, pane.body.cur_row));
for (0..lines.len) |k| {
const i = (cur + k) % lines.len;
if (std.mem.eql(u8, std.mem.trimEnd(u8, lines[i], "\r"), text))
- return (diffClickIn(pane, lines, i, 0, buf) orelse return null).address;
+ return (rowAddressAt(p, pane, i, 0, buf) orelse return null).address;
}
return null;
}
@@ -1524,7 +1508,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void {
// A whole line of a diff written over 9P (a context line ` Del`
// included) is a look at the address it names, never the word run.
var diff_buf: [4096 + 32]u8 = undefined;
- const looked: []const u8 = diffLineAddress(p, pane, operand_raw, &diff_buf) orelse looked: {
+ const looked: []const u8 = lineAddress(p, pane, operand_raw, &diff_buf) orelse looked: {
if (config.commandWord(command)) |cmd| {
if (exec.execute(p, id, cmd)) |dst| v.active = dst;
return;
@@ -1619,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);
@@ -1900,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;
@@ -2331,7 +2295,7 @@ test "a click, a 9P whole-line look and an empty event write-back take one expan
if (at.whole_line) {
const lines = diffLines(p, pane).?;
var buf: [4096 + 32]u8 = undefined;
- const whole = diffLineAddress(p, pane, lines[at.row], &buf) orelse return error.NoWholeLineAddress;
+ const whole = lineAddress(p, pane, lines[at.row], &buf) orelse return error.NoWholeLineAddress;
try std.testing.expectEqualStrings(at.want, whole);
}
}