summaryrefslogtreecommitdiff
path: root/src/locations.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/locations.zig')
-rw-r--r--src/locations.zig176
1 files changed, 176 insertions, 0 deletions
diff --git a/src/locations.zig b/src/locations.zig
index fc39f92d..ca6b7a7d 100644
--- a/src/locations.zig
+++ b/src/locations.zig
@@ -103,6 +103,135 @@ pub fn equalRows(a: []const Row, b: []const Row) bool {
return true;
}
+// ---- rows as text: a pane's `rows` file (ninep/pane.zig) ----
+
+/// Rows as text, a line a row, its fields one blank apart and its path
+/// last (a path may hold blanks): its kind (`m` a match n/N step to, `c`
+/// context, `p` a preview of source; then `d` when it is a declaration and
+/// `u` when its occurrence is not marked), its place (line, col, end_line,
+/// end_col; 0 for none), source_column, location_end and code_start (byte
+/// offsets in its line), its styles and its path. Styles are `-` for none,
+/// else how many bytes they cover from code_start, a colon and the runs
+/// that are not plain, `start+len=role`, comma-separated: a role is a
+/// syntax class (keyword string number comment) and grounds (add del emph
+/// line, syntax.ground_add's), joined by dots. `p 12 0 0 0 0 0 0
+/// 9:0+1=add.line,5+3=keyword.add src/a.zig`.
+pub fn formatRows(rows: []const Row, w: *std.Io.Writer) std.Io.Writer.Error!void {
+ for (rows) |r| {
+ try w.writeByte(switch (r.kind) {
+ .match => 'm',
+ .context => 'c',
+ .preview => 'p',
+ });
+ if (r.declaration) try w.writeByte('d');
+ if (!r.mark_occurrence) try w.writeByte('u');
+ try w.print(" {d} {d} {d} {d} {d} {d} {d} ", .{ r.at.line, r.at.col, r.at.end_line, r.at.end_col, r.source_column, r.location_end, r.code_start });
+ if (r.colors.len == 0) try w.writeByte('-') else try w.print("{d}:", .{r.colors.len});
+ var sep = false;
+ var at: usize = 0;
+ while (at < r.colors.len) {
+ const style = r.colors[at];
+ var end = at + 1;
+ while (end < r.colors.len and r.colors[end] == style) end += 1;
+ defer at = end;
+ if (style == 0) continue;
+ if (sep) try w.writeByte(',');
+ sep = true;
+ try w.print("{d}+{d}=", .{ at, end - at });
+ var dot = false;
+ const syn = style & syntax.syn_bits;
+ if (syn != 0) {
+ if (std.enums.fromInt(syntax.Syn, syn)) |known| try w.writeAll(@tagName(known)) else try w.print("{d}", .{syn});
+ dot = true;
+ }
+ for (grounds) |g| if (style & g[0] != 0) {
+ if (dot) try w.writeByte('.');
+ try w.writeAll(g[1]);
+ dot = true;
+ };
+ }
+ try w.print(" {s}\n", .{r.path});
+ }
+}
+
+const grounds = [_]struct { u8, []const u8 }{ .{ syntax.ground_add, "add" }, .{ syntax.ground_del, "del" }, .{ syntax.ground_emph, "emph" }, .{ syntax.ground_line, "line" } };
+
+/// formatRows' text read back, the rows and all they own `arena`'s.
+/// BadRows for a line that is not one; a last newline ends the last row.
+pub fn parseRows(arena: std.mem.Allocator, text: []const u8) error{ OutOfMemory, BadRows }![]Row {
+ const body = if (std.mem.endsWith(u8, text, "\n")) text[0 .. text.len - 1] else text;
+ if (body.len == 0) return &.{};
+ var rows: std.ArrayList(Row) = .empty;
+ var lines = std.mem.splitScalar(u8, body, '\n');
+ while (lines.next()) |line| {
+ var fields: [9][]const u8 = undefined;
+ var at: usize = 0;
+ for (&fields) |*field| {
+ const blank = std.mem.indexOfScalarPos(u8, line, at, ' ') orelse return error.BadRows;
+ field.* = line[at..blank];
+ at = blank + 1;
+ }
+ const kind = fields[0];
+ if (kind.len == 0) return error.BadRows;
+ var row: Row = .{
+ .kind = switch (kind[0]) {
+ 'm' => .match,
+ 'c' => .context,
+ 'p' => .preview,
+ else => return error.BadRows,
+ },
+ .path = try arena.dupe(u8, line[at..]),
+ .at = .{},
+ .location_end = 0,
+ .code_start = 0,
+ };
+ for (kind[1..]) |flag| switch (flag) {
+ 'd' => row.declaration = true,
+ 'u' => row.mark_occurrence = false,
+ else => return error.BadRows,
+ };
+ var numbers: [7]usize = undefined;
+ for (&numbers, fields[1..8]) |*n, field| n.* = std.fmt.parseInt(usize, field, 10) catch return error.BadRows;
+ row.at = .{ .line = numbers[0], .col = numbers[1], .end_line = numbers[2], .end_col = numbers[3] };
+ row.source_column = numbers[4];
+ row.location_end = numbers[5];
+ row.code_start = numbers[6];
+ const styles = fields[8];
+ if (!std.mem.eql(u8, styles, "-")) {
+ const colon = std.mem.indexOfScalar(u8, styles, ':') orelse return error.BadRows;
+ const len = std.fmt.parseInt(usize, styles[0..colon], 10) catch return error.BadRows;
+ // A line is never longer than a file: bounded before it is made.
+ if (len > @import("memory.zig").limits.max_file_bytes) return error.BadRows;
+ row.colors = try arena.alloc(u8, len);
+ @memset(row.colors, 0);
+ var runs = std.mem.splitScalar(u8, styles[colon + 1 ..], ',');
+ while (runs.next()) |run| {
+ if (run.len == 0) continue;
+ const plus = std.mem.indexOfScalar(u8, run, '+') orelse return error.BadRows;
+ const eq = std.mem.indexOfScalarPos(u8, run, plus, '=') orelse return error.BadRows;
+ const start = std.fmt.parseInt(usize, run[0..plus], 10) catch return error.BadRows;
+ const n = std.fmt.parseInt(usize, run[plus + 1 .. eq], 10) catch return error.BadRows;
+ if (start > len or n > len - start) return error.BadRows;
+ var style: u8 = 0;
+ var names = std.mem.splitScalar(u8, run[eq + 1 ..], '.');
+ names: while (names.next()) |name| {
+ for (grounds) |g| if (std.mem.eql(u8, name, g[1])) {
+ style |= g[0];
+ continue :names;
+ };
+ if (style & syntax.syn_bits != 0) return error.BadRows;
+ const syn: u8 = if (std.meta.stringToEnum(syntax.Syn, name)) |known| @backingInt(known) else std.fmt.parseInt(u4, name, 10) catch return error.BadRows;
+ if (syn == 0) return error.BadRows;
+ style |= syn;
+ }
+ @memset(row.colors[start..][0..n], style);
+ }
+ }
+ try rows.append(arena, row);
+ }
+ return rows.items;
+}
+
/// Compare native producer rows with an already formatted, context-free view.
/// Identical refreshes retain allocations, syntax and selection state.
pub fn sameInput(input: []const u8, rendered: []const u8, rows: []const Row, layout: Config.Layout) bool {
@@ -984,3 +1113,50 @@ test "locations cache transfers oversized completed analysis without reparsing"
try std.testing.expectEqual(@as(usize, 0), cache.len);
try std.testing.expectEqual(@as(usize, 0), cache.bytes);
}
+
+test "rows read back from their text are the rows: +Search's, +Callstack's and +Diff's" {
+ const testing = std.testing;
+ const p = try pardes.Pardes.init(testing.allocator, .{ .tty_only = true });
+ defer p.deinit();
+ var tmp = testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.zig", .data = "const S = struct {\n fn f() void {\n a(\"x y\");\n b();\n }\n};\n" });
+ var buf: [std.Io.Dir.max_path_bytes]u8 = undefined;
+ const dir = buf[0..try tmp.dir.realPath(testing.io, &buf)];
+ const patch = try std.Io.Dir.path.join(testing.allocator, &.{ dir, "c.patch" });
+ defer testing.allocator.free(patch);
+ const search = try format(p, dir, "a.zig:3:9 a(\"x y\");\na.zig:4:9 b();\n", null, true);
+ defer p.gpa.free(search.content);
+ defer freeRows(p.gpa, search.rows);
+ const callstack = try formatCallstack(p, dir, "a.zig:2:8 f\n└─ a.zig:3:9 a\n");
+ defer p.gpa.free(callstack.content);
+ defer freeRows(p.gpa, callstack.rows);
+ const hid = try @import("hunks.zig").openDiff(p, 0, patch, "--- a.zig\n+++ a.zig\n@@ -2,3 +2,4 @@\n fn f() void {\n- a(\"x\");\n+ a(\"x y\");\n+ b();\n }\n");
+ const stacked = p.panes[hid].?.file.?.location_rows;
+ // A +Diff's rows carry styles with grounds: the codec is tried on them.
+ var grounded = false;
+ for (stacked) |row| for (row.colors) |c| {
+ grounded = grounded or c & syntax.ground_add != 0;
+ };
+ try testing.expect(grounded and search.rows.len > 0 and callstack.rows.len > 0);
+ var arena_state: std.heap.ArenaAllocator = .init(testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ for ([_][]const Row{ search.rows, callstack.rows, stacked }) |rows| {
+ var text: std.Io.Writer.Allocating = .init(arena);
+ try formatRows(rows, &text.writer);
+ const back = try parseRows(arena, text.written());
+ try testing.expect(equalRows(rows, back));
+ var again: std.Io.Writer.Allocating = .init(arena);
+ try formatRows(back, &again.writer);
+ try testing.expectEqualStrings(text.written(), again.written());
+ }
+ try testing.expectError(error.BadRows, parseRows(arena, "x 1 0 0 0 0 0 0 - a\n"));
+ try testing.expectError(error.BadRows, parseRows(arena, "m 1 0 0 0 0 0 0 2:0+3=keyword a\n"));
+ try testing.expectError(error.BadRows, parseRows(arena, "m 1 0 0 0 0 0 0 2:0+1=keyword.string a\n"));
+ try testing.expectError(error.BadRows, parseRows(arena, "m 1 0 0\n"));
+ const one = try parseRows(arena, "pd 12 3 0 0 0 0 0 4:0+1=add.line,1+2=keyword.add src/a b.zig\n");
+ try testing.expectEqualStrings("src/a b.zig", one[0].path);
+ try testing.expect(one[0].declaration and one[0].kind == .preview and one[0].at.line == 12 and one[0].at.col == 3);
+ try testing.expectEqualSlices(u8, &.{ syntax.ground_add | syntax.ground_line, @backingInt(syntax.Syn.keyword) | syntax.ground_add, @backingInt(syntax.Syn.keyword) | syntax.ground_add, 0 }, one[0].colors);
+}