summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Output.zig29
-rw-r--r--src/hunks.zig151
2 files changed, 128 insertions, 52 deletions
diff --git a/src/Output.zig b/src/Output.zig
index 0c6fdfc2..d4be640c 100644
--- a/src/Output.zig
+++ b/src/Output.zig
@@ -490,8 +490,33 @@ pub fn fillRows(p: *Pardes, id: usize, dir: []const u8, from: Origin, arg: []con
/// NoHunks when there is none in it.
pub fn openDiff(p: *Pardes, id: usize, path: []const u8, content: []const u8) !usize {
const dir = std.Io.Dir.path.dirname(path) orelse p.sessionDir();
- const root = hunks.sourceRoot(p, dir, content);
- const result = try hunks.format(p, root, content);
+ // Each file read is a site of the step's tape (fs.zig): a replica
+ // replays the authority's bytes and reads no disk of its own.
+ const Read = struct {
+ fn source(ctx: *const hunks.Ctx, from: []const u8, name: []const u8) ?hunks.Source {
+ const pp: *Pardes = @ptrCast(@alignCast(ctx.host));
+ const text = locations.sourceText(pp, ctx.arena, from, name) orelse return null;
+ return .{ .path = text.path, .bytes = text.bytes };
+ }
+ fn declarations(ctx: *const hunks.Ctx, name: []const u8, bytes: []const u8) error{OutOfMemory}![]const @import("syntax.zig").ContextDeclaration {
+ const pp: *Pardes = @ptrCast(@alignCast(ctx.host));
+ var hit = pp.locations_cache.get(pp.tree_sitter_gpa, name, bytes, true, false) catch
+ locations.Cache.Hit{ .analysis = @import("syntax.zig").analyzeSource(ctx.arena, name, bytes, true, false) catch .{} };
+ defer hit.deinit(pp.tree_sitter_gpa);
+ // The cache's may go at its next use: kept for the call.
+ return ctx.arena.dupe(@import("syntax.zig").ContextDeclaration, hit.analysis.declarations);
+ }
+ };
+ const ctx: hunks.Ctx = .{
+ .gpa = p.gpa,
+ .arena = p.scratch.allocator(),
+ .source = Read.source,
+ .declarations = Read.declarations,
+ .host = p,
+ .tslocations = p.locations_config.tslocations,
+ };
+ const root = hunks.sourceRoot(&ctx, dir, content);
+ const result = try hunks.format(&ctx, root, content);
errdefer {
p.gpa.free(result.content);
locations.freeRows(p.gpa, result.rows);
diff --git a/src/hunks.zig b/src/hunks.zig
index 19246392..28fffa38 100644
--- a/src/hunks.zig
+++ b/src/hunks.zig
@@ -6,21 +6,43 @@
//! a tinted ground as delta emphasises them (its edits.rs and align.rs, here as `inferEdits`
//! and `Alignment`).
const std = @import("std");
-const pardes = @import("pardes.zig");
const diff = @import("diff.zig");
const syntax = @import("syntax.zig");
const locations = @import("locations.zig");
-const Pardes = pardes.Pardes;
const Row = locations.Row;
+/// What `format` takes of the editor it stacks for: its allocators, the
+/// files a diff names read as the editor reads them (in pardes an open
+/// pane's text, else the disk's, each read a site of the step's tape:
+/// Output.openDiff), their declarations, and one setting.
+pub const Ctx = struct {
+ /// The text and rows made, the caller's.
+ gpa: std.mem.Allocator,
+ /// All else, the call's.
+ arena: std.mem.Allocator,
+ /// `path` as a look from `dir` finds it, its text the arena's (or the
+ /// editor's, good for the call); null when it is not there.
+ source: *const fn (ctx: *const Ctx, dir: []const u8, path: []const u8) ?Source,
+ /// The declarations of `bytes`, the file at `path`, outermost first
+ /// (syntax.analyzeSource's), the arena's.
+ declarations: *const fn (ctx: *const Ctx, path: []const u8, bytes: []const u8) error{OutOfMemory}![]const syntax.ContextDeclaration,
+ /// The two above's own.
+ host: *anyopaque,
+ /// A declaration above a hunk gets its `path:line` row too
+ /// (LocationsConfig's tslocations).
+ tslocations: bool,
+};
+
+/// A file's text, and its path as `dir` and the path named resolve.
+pub const Source = struct { path: []const u8, bytes: []const u8 };
+
/// The rows `content`, a diff, stacks to, for a pane in `dir`, with each
-/// row's metadata (Output.fillRows). Text and rows are `p.gpa`'s; all else
-/// is the step's scratch.
-pub fn format(p: *Pardes, dir: []const u8, content: []const u8) !locations.Result {
- const arena = p.scratch.allocator();
+/// row's metadata (Output.fillRows). Text and rows are `ctx.gpa`'s.
+pub fn format(ctx: *const Ctx, dir: []const u8, content: []const u8) !locations.Result {
+ const arena = ctx.arena;
const styles = try syntax.highlightDiff(arena, content, 0, content.len);
- var out: Out = .{ .p = p, .text = .init(p.gpa) };
+ var out: Out = .{ .gpa = ctx.gpa, .text = .init(ctx.gpa) };
errdefer out.deinit();
var walk: diff.Walk = .{};
var hunk: Hunk = .{};
@@ -46,7 +68,7 @@ pub fn format(p: *Pardes, dir: []const u8, content: []const u8) !locations.Resul
// A `\ No newline` belongs to the line above, as delta shows it.
.no_newline => {},
else => {
- try flush(&out, &hunk, &source, dir);
+ try flush(ctx, &out, &hunk, &source, dir);
switch (got.kind) {
.hunk => hunk = .{
.path = walk.path(),
@@ -69,9 +91,9 @@ pub fn format(p: *Pardes, dir: []const u8, content: []const u8) !locations.Resul
}
at = nl + 1;
}
- try flush(&out, &hunk, &source, dir);
- const rows = try out.rows.toOwnedSlice(p.gpa);
- errdefer locations.freeRows(p.gpa, rows);
+ try flush(ctx, &out, &hunk, &source, dir);
+ const rows = try out.rows.toOwnedSlice(ctx.gpa);
+ errdefer locations.freeRows(ctx.gpa, rows);
return .{ .content = try out.text.toOwnedSlice(), .rows = rows, .anchor = null };
}
@@ -105,13 +127,13 @@ fn gitContext(line: []const u8) []const u8 {
}
const Out = struct {
- p: *Pardes,
+ gpa: std.mem.Allocator,
text: std.Io.Writer.Allocating,
rows: std.ArrayList(Row) = .empty,
fn deinit(out: *Out) void {
out.text.deinit();
- locations.freeRowList(out.p.gpa, &out.rows);
+ locations.freeRowList(out.gpa, &out.rows);
}
const Meta = struct {
@@ -127,7 +149,7 @@ const Out = struct {
fn row(out: *Out, text: []const u8, path: []const u8, meta: Meta) !void {
try out.text.writer.writeAll(text);
try out.text.writer.writeByte('\n');
- try locations.appendRow(out.p.gpa, &out.rows, .{
+ try locations.appendRow(out.gpa, &out.rows, .{
.kind = meta.kind,
.declaration = meta.declaration,
.path = @constCast(path),
@@ -153,7 +175,7 @@ const SourceMemo = struct {
/// Where a +Diff of `content`, a diff in `dir`, stands: the directory its
/// first file is found from (`dir` or one above it, as `findSource` looks),
/// so its rows name their files as the diff does; `dir` when none is found.
-pub fn sourceRoot(p: *Pardes, dir: []const u8, content: []const u8) []const u8 {
+pub fn sourceRoot(ctx: *const Ctx, dir: []const u8, content: []const u8) []const u8 {
var walk: diff.Walk = .{};
var lines = std.mem.splitScalar(u8, content, '\n');
const path = while (lines.next()) |line| {
@@ -161,31 +183,27 @@ pub fn sourceRoot(p: *Pardes, dir: []const u8, content: []const u8) []const u8 {
} else return dir;
var base: []const u8 = dir;
for (0..8) |_| {
- if (locations.sourceText(p, p.scratch.allocator(), base, path) != null) return base;
+ if (ctx.source(ctx, base, path) != null) return base;
base = std.Io.Dir.path.dirname(base) orelse return dir;
}
return dir;
}
-fn findSource(p: *Pardes, memo: *SourceMemo, dir: []const u8, path: []const u8) !void {
+fn findSource(ctx: *const Ctx, memo: *SourceMemo, dir: []const u8, path: []const u8) !void {
if (std.mem.eql(u8, memo.path, path)) return;
- const arena = p.scratch.allocator();
+ const arena = ctx.arena;
memo.* = .{ .path = path };
// ponytail: eight directories up; a repository deeper than that under
// its patch is looked for from the patch's own directory only.
var base: []const u8 = dir;
for (0..8) |_| {
- if (locations.sourceText(p, arena, base, path)) |text| {
+ if (ctx.source(ctx, base, path)) |text| {
memo.shown = if (base.ptr == dir.ptr) path else try std.Io.Dir.path.relativePosix(arena, "/", dir, text.path);
var lines: std.ArrayList([]const u8) = .empty;
var split = std.mem.splitScalar(u8, text.bytes, '\n');
while (split.next()) |line| try lines.append(arena, std.mem.trimEnd(u8, line, "\r"));
memo.lines = lines.items;
- var hit = p.locations_cache.get(p.tree_sitter_gpa, text.path, text.bytes, true, false) catch
- locations.Cache.Hit{ .analysis = syntax.analyzeSource(arena, text.path, text.bytes, true, false) catch .{} };
- defer hit.deinit(p.tree_sitter_gpa);
- // The cache's may go at its next use: kept for the section.
- memo.declarations = try arena.dupe(syntax.ContextDeclaration, hit.analysis.declarations);
+ memo.declarations = try ctx.declarations(ctx, text.path, text.bytes);
return;
}
base = std.Io.Dir.path.dirname(base) orelse return;
@@ -251,12 +269,11 @@ fn heldAt(memo: *const SourceMemo, hunk: *const Hunk, comptime side: Side, start
return .{ .held = held, .of = @max(of, 1) };
}
-fn flush(out: *Out, hunk: *Hunk, memo: *SourceMemo, dir: []const u8) !void {
+fn flush(ctx: *const Ctx, out: *Out, hunk: *Hunk, memo: *SourceMemo, dir: []const u8) !void {
defer hunk.* = .{};
if (hunk.path.len == 0 or hunk.lines.items.len == 0) return;
- const p = out.p;
- const arena = p.scratch.allocator();
- try findSource(p, memo, dir, hunk.path);
+ const arena = ctx.arena;
+ try findSource(ctx, memo, dir, hunk.path);
const disk = sideOnDisk(memo, hunk);
const side = disk.side;
// A line number of the hunk's, where the file has it now.
@@ -287,7 +304,7 @@ fn flush(out: *Out, hunk: *Hunk, memo: *SourceMemo, dir: []const u8) !void {
var row = declaration.start_line;
while (row <= declaration.header_end_line and row < start_row) : (row += 1) {
const at: @FieldType(Row, "at") = .{ .line = row + 1 };
- if (row == declaration.start_line and p.locations_config.tslocations) {
+ if (row == declaration.start_line and ctx.tslocations) {
const label = try std.fmt.allocPrint(arena, "{s}:{d}", .{ shown, row + 1 });
try out.row(label, shown, .{ .kind = .context, .declaration = true, .at = at, .location_end = label.len, .code_start = label.len });
}
@@ -574,13 +591,40 @@ fn annotate(arena: std.mem.Allocator, a: *const Alignment, minus_line: []const u
}
fn trimmedWidth(section: []const u8) usize {
- return pardes.panes.File.displayWidth(std.mem.trim(u8, section, " \t"));
+ return @import("File.zig").displayWidth(std.mem.trim(u8, section, " \t"));
}
// ---- tests ----
const testing = std.testing;
+/// A Ctx over the disk, as the editor's is without panes or a cache.
+const TestCtx = struct {
+ arena_state: std.heap.ArenaAllocator,
+ ctx: Ctx,
+
+ fn init(t: *TestCtx) void {
+ t.arena_state = .init(testing.allocator);
+ t.ctx = .{ .gpa = testing.allocator, .arena = t.arena_state.allocator(), .source = source, .declarations = declarations, .host = t, .tslocations = true };
+ syntax.start(testing.allocator);
+ }
+
+ fn deinit(t: *TestCtx) void {
+ syntax.stop();
+ t.arena_state.deinit();
+ }
+
+ fn source(ctx: *const Ctx, dir: []const u8, path: []const u8) ?Source {
+ const full = std.Io.Dir.path.resolvePosix(ctx.arena, &.{ dir, path }) catch return null;
+ const bytes = std.Io.Dir.cwd().readFileAlloc(testing.io, full, ctx.arena, .limited(1 << 20)) catch return null;
+ return .{ .path = full, .bytes = bytes };
+ }
+
+ fn declarations(ctx: *const Ctx, path: []const u8, bytes: []const u8) error{OutOfMemory}![]const syntax.ContextDeclaration {
+ return (syntax.analyzeSource(ctx.arena, path, bytes, true, false) catch return &.{}).declarations;
+ }
+};
+
fn emphasised(line: HunkLine) ![]const u8 {
var out: std.ArrayList(u8) = .empty;
for (line.code, line.styles) |c, s| try out.append(testing.allocator, if (s & syntax.ground_emph != 0) c else '.');
@@ -631,8 +675,9 @@ test "delta's alignment: kitten to sitting, as align.rs has it" {
}
test "a diff stacks to a label a hunk and its lines, markers gone, tinted and stepped" {
- const p = try Pardes.init(testing.allocator, .{ .tty_only = true });
- defer p.deinit();
+ var t: TestCtx = undefined;
+ t.init();
+ defer t.deinit();
const patch =
\\diff --git a/nowhere/x.txt b/nowhere/x.txt
\\--- a/nowhere/x.txt
@@ -644,9 +689,9 @@ test "a diff stacks to a label a hunk and its lines, markers gone, tinted and st
\\ tail
\\
;
- const result = try format(p, "/pardes-surely-missing-dir", patch);
- defer p.gpa.free(result.content);
- defer locations.freeRows(p.gpa, result.rows);
+ const result = try format(&t.ctx, "/pardes-surely-missing-dir", patch);
+ defer testing.allocator.free(result.content);
+ defer locations.freeRows(testing.allocator, result.rows);
try testing.expectEqualStrings("nowhere/x.txt:2\n section one\n keep\n-old value\n+new value\n tail\n", result.content);
try testing.expectEqual(@as(usize, 6), result.rows.len);
try testing.expectEqual(.match, result.rows[0].kind);
@@ -664,8 +709,9 @@ test "a diff stacks to a label a hunk and its lines, markers gone, tinted and st
test "a hunk the file has moved since is found where it went, inside its declaration" {
if (!syntax.supportsPath("a.zig")) return error.SkipZigTest;
- const p = try Pardes.init(testing.allocator, .{ .tty_only = true });
- defer p.deinit();
+ var t: TestCtx = undefined;
+ t.init();
+ defer t.deinit();
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
// Two lines more above it than the patch knows of.
@@ -682,16 +728,17 @@ test "a hunk the file has moved since is found where it went, inside its declara
\\ b();
\\
;
- const result = try format(p, dir, patch);
- defer p.gpa.free(result.content);
- defer locations.freeRows(p.gpa, result.rows);
+ const result = try format(&t.ctx, dir, patch);
+ defer testing.allocator.free(result.content);
+ defer locations.freeRows(testing.allocator, result.rows);
try testing.expect(std.mem.indexOf(u8, result.content, "a.zig:3\n const S = struct {\na.zig:5 *\n") != null);
try testing.expect(std.mem.indexOf(u8, result.content, "git's guess") == null);
}
test "a mailed patch keeps its headers, message and diffstat above its hunks" {
- const p = try Pardes.init(testing.allocator, .{ .tty_only = true });
- defer p.deinit();
+ var t: TestCtx = undefined;
+ t.init();
+ defer t.deinit();
const patch =
\\From 0123 Mon Sep 17 00:00:00 2001
\\Subject: [PATCH] change x
@@ -709,12 +756,15 @@ test "a mailed patch keeps its headers, message and diffstat above its hunks" {
\\+b
\\
;
- const result = try format(p, "/pardes-surely-missing-dir", patch);
- defer p.gpa.free(result.content);
- defer locations.freeRows(p.gpa, result.rows);
+ const result = try format(&t.ctx, "/pardes-surely-missing-dir", patch);
+ defer testing.allocator.free(result.content);
+ defer locations.freeRows(testing.allocator, result.rows);
try testing.expectEqualStrings("From 0123 Mon Sep 17 00:00:00 2001\nSubject: [PATCH] change x\n\nWhy it changed.\n---\n x.txt | 2 +-\n\nx.txt:1\n-a\n+b\n", result.content);
}
+const pardes = @import("pardes.zig");
+const Pardes = pardes.Pardes;
+
test "a .patch looked at opens its +Diff, and Diff goes to the text and back" {
const p = try Pardes.init(testing.allocator, .{ .tty_only = true });
defer p.deinit();
@@ -755,8 +805,9 @@ test "a .patch looked at opens its +Diff, and Diff goes to the text and back" {
test "a hunk edited since, beside siblings with its signature's tail, is found in its own function" {
if (!syntax.supportsPath("a.rs")) return error.SkipZigTest;
- const p = try Pardes.init(testing.allocator, .{ .tty_only = true });
- defer p.deinit();
+ var t: TestCtx = undefined;
+ t.init();
+ defer t.deinit();
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
// Three functions ending in one signature's tail; the patch is for the
@@ -799,9 +850,9 @@ test "a hunk edited since, beside siblings with its signature's tail, is found i
\\ c
\\
;
- const result = try format(p, dir, patch);
- defer p.gpa.free(result.content);
- defer locations.freeRows(p.gpa, result.rows);
+ const result = try format(&t.ctx, dir, patch);
+ defer testing.allocator.free(result.content);
+ defer locations.freeRows(testing.allocator, result.rows);
try testing.expect(std.mem.indexOf(u8, result.content, "a.rs:12\n fn three(\na.rs:15 *\n") != null);
}