diff options
Diffstat (limited to 'src/look.zig')
| -rw-r--r-- | src/look.zig | 49 |
1 files changed, 32 insertions, 17 deletions
diff --git a/src/look.zig b/src/look.zig index 082565ed..ab77773f 100644 --- a/src/look.zig +++ b/src/look.zig @@ -1390,18 +1390,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 }; -pub fn diffClick(p: *Pardes, pane: *Pane, row: usize, col: usize, buf: []u8) ?DiffClick { +/// 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 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 +1431,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 +1539,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; @@ -2331,7 +2346,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); } } |
