summaryrefslogtreecommitdiff
path: root/core/look.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/look.zig')
-rw-r--r--core/look.zig134
1 files changed, 126 insertions, 8 deletions
diff --git a/core/look.zig b/core/look.zig
index b240b6e2..72bb42eb 100644
--- a/core/look.zig
+++ b/core/look.zig
@@ -67,13 +67,89 @@ fn num(tok: []const u8, i: usize) struct { v: usize, end: usize } {
return .{ .v = v, .end = j };
}
+/// A file's name in a row (a Grep's, Find's, a query pane's, the patch
+/// view's), written as git writes an unusual one (quote_c_style): one with
+/// a control byte, a `"`, a `\` or a `:` in double quotes, `\n \t \r \" \\`
+/// and `\ooo` for any other control byte, so that a row is one line and its
+/// name never splits at a `:`. Any other name as it is.
+pub fn quoted(name: []const u8) Quoted {
+ return .{ .name = name };
+}
+
+pub const Quoted = struct {
+ name: []const u8,
+
+ pub fn format(q: Quoted, w: *std.Io.Writer) std.Io.Writer.Error!void {
+ for (q.name) |c| {
+ if (c < 0x20 or c == 0x7f or c == '"' or c == '\\' or c == config.line_col_sep) break;
+ } else return w.writeAll(q.name);
+ try w.writeByte('"');
+ for (q.name) |c| switch (c) {
+ '\n' => try w.writeAll("\\n"),
+ '\t' => try w.writeAll("\\t"),
+ '\r' => try w.writeAll("\\r"),
+ '"', '\\' => try w.print("\\{c}", .{c}),
+ 0...0x08, 0x0b, 0x0c, 0x0e...0x1f, 0x7f => try w.print("\\{o:0>3}", .{c}),
+ else => try w.writeByte(c),
+ };
+ try w.writeByte('"');
+ }
+};
+
+/// A quoted name's inside (parsePathLine's path of a `"`-led word) read
+/// back into `buf` (escapes only shrink it: `buf` as long as `inside` is
+/// enough).
+pub fn unquoted(buf: []u8, inside: []const u8) []const u8 {
+ var n: usize = 0;
+ var i: usize = 0;
+ while (i < inside.len and n < buf.len) : (n += 1) {
+ const c = inside[i];
+ i += 1;
+ buf[n] = c;
+ if (c != '\\' or i == inside.len) continue;
+ const e = inside[i];
+ i += 1;
+ buf[n] = switch (e) {
+ 'n' => '\n',
+ 't' => '\t',
+ 'r' => '\r',
+ 'a' => 0x07,
+ 'b' => 0x08,
+ 'v' => 0x0b,
+ 'f' => 0x0c,
+ else => e,
+ };
+ // `\ooo`: three octal digits.
+ if (i + 2 <= inside.len) if (std.fmt.parseInt(u8, inside[i - 1 .. i + 2], 8)) |v| {
+ buf[n] = v;
+ i += 2;
+ } else |_| {};
+ }
+ return buf[0..n];
+}
+
+/// Where a `"`-led word's quoted name closes (`quoted`'s), past its
+/// escapes; null when it is no quoted name.
+fn quoteEnd(tok: []const u8) ?usize {
+ if (tok.len < 2 or tok[0] != '"') return null;
+ var i: usize = 1;
+ while (i < tok.len) : (i += 1) switch (tok[i]) {
+ '\\' => i += 1,
+ '"' => return i,
+ else => {},
+ };
+ return null;
+}
+
pub fn parsePathLine(tok: []const u8) struct { path: []const u8, at: Spot, end: usize } {
- var sep: usize = 0;
+ // A quoted name (a row's: `quoted`) is the path whole, its `:` its own.
+ const close = quoteEnd(tok);
+ var sep: usize = if (close) |c| c + 1 else 0;
while (sep < tok.len) : (sep += 1) {
if (tok[sep] != config.line_col_sep) continue;
const l = num(tok, sep + 1);
if (l.end == sep + 1) continue; // no digits after ':'
- const path = tok[0..sep];
+ const path = if (close) |c| tok[1..c] else tok[0..sep];
var i = l.end;
if (i < tok.len and tok[i] == config.range_sep) {
const e = num(tok, i + 1);
@@ -753,20 +829,29 @@ pub fn focusPaneLine(p: *Pardes, id: usize, at_in: Spot, landing: enum { center,
}
}
}
- pane.body.cur_row = @intCast(at.line - 1);
- pane.body.cur_col = if (at.col > 0) @intCast(at.col - 1) else 0;
+ pane.body.cur_row = std.math.cast(i32, at.line - 1) orelse return;
+ pane.body.cur_col = columnOn(pane, pane.body.cur_row, at.col);
pane.body.cur_pinned = true;
if (at.end_line != 0) return selectSpan(p, pane, at);
pane.ensureCursorVisible(p.settings.tab);
}
+/// A place's column (from 1) as a cursor's on `row`: past the line's end (a
+/// server's answer, a typed number: any) it is the end, as a search's
+/// landing and clampCursor have it. A terminal's (a jump's) as it was.
+fn columnOn(pane: *const Pane, row: i32, col: usize) i32 {
+ if (pane.file == null) return std.math.lossyCast(i32, col -| 1);
+ return @intCast(@min(col -| 1, panes.File.sourceLine(pane, row).len));
+}
+
fn selectSpan(p: *Pardes, pane: *Pane, at: Spot) void {
const text = edit.flatSurface(p.editor(), &pane.body) catch |err| return p.reportError(.at(p.view().active), "look", err);
// saturating, not `- 1`: `f.zig:0-5` is a legal thing to type and a
// 1-based zero is the same nothing an absent number is
- const acol: i32 = @intCast(at.col -| 1);
- const ecol: i32 = if (at.end_col > 0) @intCast(at.end_col - 1) else std.math.maxInt(i32);
- const r = panes.Text.cellRange(text, @intCast(at.line -| 1), acol, @intCast(at.end_line -| 1), ecol);
+ // Any number a place has: cellRange clamps each to the text.
+ const acol = std.math.lossyCast(i32, at.col -| 1);
+ const ecol: i32 = if (at.end_col > 0) std.math.lossyCast(i32, at.end_col - 1) else std.math.maxInt(i32);
+ const r = panes.Text.cellRange(text, std.math.lossyCast(i32, at.line -| 1), acol, std.math.lossyCast(i32, at.end_line -| 1), ecol);
pane.body.setRange(p.settings.tab, text, 0, r, true);
}
@@ -1605,7 +1690,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void {
const free = p.freeSlot() orelse return p.reportError(.at(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);
- if (target.at.col > 0) nt.body.cur_col = @intCast(target.at.col - 1);
+ nt.body.cur_col = columnOn(nt, nt.body.cur_row, target.at.col);
const failures = p.fs.failures;
layout.placeNew(p, id, id, free, .doc);
// Refused a place: that is the look's one failure, not a
@@ -2025,6 +2110,39 @@ test "Look copies its operand before releasing pointer selection storage" {
try std.testing.expectEqual(.none, p.view().drag);
}
+test "a Look whose column is past its line's end lands at the end, however big; a line past the file's is a miss" {
+ const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 });
+ defer p.deinit();
+ const pane = try p.setTestFile("alpha\n\tbeta\n");
+ // Up to 2026-11-23 a column past i32 panicked the session (`@intCast`
+ // in focusPaneLine): any text a B3 or Look takes, a server's row.
+ for ([_][]const u8{ "/test.txt:2:9", "/test.txt:2:3000000000", "/test.txt:2:18446744073709551615" }) |place| {
+ lookAt(p, 0, place);
+ try std.testing.expectEqual(@as(i32, 1), pane.body.cur_row);
+ try std.testing.expectEqual(@as(i32, 5), pane.body.cur_col);
+ }
+ // A span past the text: selected to its end.
+ lookAt(p, 0, "/test.txt:1:2-3000000000:3000000000");
+ try std.testing.expect(pane.body.vsel.active);
+ pane.body.cur_row = 0;
+ lookAt(p, 0, "/test.txt:3000000000:1");
+ try std.testing.expectEqual(@as(i32, 0), pane.body.cur_row);
+}
+
+test "a file's name written as a row writes it reads back whole: a newline, a `:5:`, a quote, a control byte; a plain name as it is" {
+ var row: [256]u8 = undefined;
+ var back: [256]u8 = undefined;
+ for ([_][]const u8{ "a\nb:1:1", "a:5:b.txt", "say \"hi\".txt", "bell\x07\x1b[31m", "back\\slash", "plain/name.zig" }) |name| {
+ const written = try std.fmt.bufPrint(&row, "{f}:12:3-5 code", .{quoted(name)});
+ try std.testing.expect(std.mem.indexOfScalar(u8, written, '\n') == null);
+ const got = parsePathLine(written);
+ try std.testing.expectEqual(@as(usize, 12), got.at.line);
+ try std.testing.expectEqual(@as(usize, 3), got.at.col);
+ try std.testing.expectEqualStrings(name, if (written[0] == '"') unquoted(&back, got.path) else got.path);
+ }
+ try std.testing.expectEqualStrings("plain/name.zig:1", try std.fmt.bufPrint(&row, "{f}:1", .{quoted("plain/name.zig")}));
+}
+
test "@p names a pane by its serial, as /index does" {
const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 });
defer p.deinit();