summaryrefslogtreecommitdiff
path: root/core/hunks.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/hunks.zig')
-rw-r--r--core/hunks.zig35
1 files changed, 19 insertions, 16 deletions
diff --git a/core/hunks.zig b/core/hunks.zig
index 186ba928..d3a8f5a7 100644
--- a/core/hunks.zig
+++ b/core/hunks.zig
@@ -37,6 +37,8 @@ pub const Ctx = struct {
/// A declaration above a hunk gets its `path:line` row too
/// (LocationsConfig's tslocations).
tslocations: bool,
+ /// A tab's width in cells (the core's `settings.tab`).
+ tab: u16,
};
/// A file's text, and its path as `dir` and the path named resolve.
@@ -346,7 +348,7 @@ fn flush(ctx: *const Ctx, out: *Out, hunk: *Hunk, memo: *SourceMemo, dir: []cons
while (i < lines.len and lines[i].kind == .removed) i += 1;
const plus_start = i;
while (i < lines.len and lines[i].kind == .added) i += 1;
- try inferEdits(arena, lines[minus_start..plus_start], lines[plus_start..i]);
+ try inferEdits(ctx.tab, arena, lines[minus_start..plus_start], lines[plus_start..i]);
}
// Each line behind its marker, `+`, `-` or a blank, as the diff
@@ -390,7 +392,7 @@ const pair_scratch_bytes = 8 << 20;
/// Pair each removed line with the first added line after the last one
/// paired that is near enough to it, in order, and emphasise in both the
/// tokens one has and the other has not.
-fn inferEdits(arena: std.mem.Allocator, minus: []HunkLine, plus: []HunkLine) !void {
+fn inferEdits(tab: u16, arena: std.mem.Allocator, minus: []HunkLine, plus: []HunkLine) !void {
if (minus.len == 0 or plus.len == 0 or minus.len > max_run or plus.len > max_run) return;
const plus_tokens = try arena.alloc([]const []const u8, plus.len);
for (plus, plus_tokens) |line, *tokens| tokens.* = try tokenize(arena, line.code);
@@ -408,7 +410,7 @@ fn inferEdits(arena: std.mem.Allocator, minus: []HunkLine, plus: []HunkLine) !vo
scratch.reset();
const pair = scratch.allocator();
var alignment = Alignment.init(pair, m_tokens, p_tokens) catch continue;
- const marks = annotate(pair, &alignment, m.code, plus[j].code) catch continue;
+ const marks = annotate(tab, pair, &alignment, m.code, plus[j].code) catch continue;
if (marks.distance > max_line_distance) continue;
for (marks.minus) |r| for (m.styles[r[0]..r[1]]) |*b| {
b.* |= syntax.ground_emph;
@@ -534,7 +536,7 @@ const Marks = struct {
/// what each deleted and inserted, and a run of blanks between two changes
/// with them; the distance is the changed width over the whole width (a
/// kept section counting on both lines).
-fn annotate(arena: std.mem.Allocator, a: *const Alignment, minus_line: []const u8, plus_line: []const u8) !Marks {
+fn annotate(tab: u16, arena: std.mem.Allocator, a: *const Alignment, minus_line: []const u8, plus_line: []const u8) !Marks {
var minus: std.ArrayList([2]usize) = .empty;
var plus: std.ArrayList([2]usize) = .empty;
var x_off: usize = 0;
@@ -560,7 +562,7 @@ fn annotate(arena: std.mem.Allocator, a: *const Alignment, minus_line: []const u
switch (op) {
.deletion => {
const r = Take.section(a.x, &x_off, &m_byte, n, minus_line);
- const w = trimmedWidth(minus_line[r[0]..r[1]]);
+ const w = trimmedWidth(tab, minus_line[r[0]..r[1]]);
numer += w;
denom += w;
try minus.append(arena, r);
@@ -569,7 +571,7 @@ fn annotate(arena: std.mem.Allocator, a: *const Alignment, minus_line: []const u
.noop => {
const r = Take.section(a.x, &x_off, &m_byte, n, minus_line);
const section = minus_line[r[0]..r[1]];
- denom += 2 * trimmedWidth(section);
+ denom += 2 * trimmedWidth(tab, section);
const is_space = std.mem.trim(u8, section, " \t").len == 0;
const coalesce = is_space and
((minus_prev == .deletion and plus_prev == .insertion and
@@ -583,7 +585,7 @@ fn annotate(arena: std.mem.Allocator, a: *const Alignment, minus_line: []const u
},
.insertion => {
const r = Take.section(a.y, &y_off, &p_byte, n, plus_line);
- const w = trimmedWidth(plus_line[r[0]..r[1]]);
+ const w = trimmedWidth(tab, plus_line[r[0]..r[1]]);
numer += w;
denom += w;
try plus.append(arena, r);
@@ -595,8 +597,8 @@ fn annotate(arena: std.mem.Allocator, a: *const Alignment, minus_line: []const u
return .{ .minus = minus.items, .plus = plus.items, .distance = distance };
}
-fn trimmedWidth(section: []const u8) usize {
- return @import("File.zig").displayWidth(std.mem.trim(u8, section, " \t"));
+fn trimmedWidth(tab: u16, section: []const u8) usize {
+ return @import("File.zig").displayWidth(tab, std.mem.trim(u8, section, " \t"));
}
// ---- the +Diff in the editor ----
@@ -734,6 +736,7 @@ pub fn openDiff(p: *Pardes, id: usize, path: []const u8, content: []const u8) !u
.declarations = Read.declarations,
.host = p,
.tslocations = p.locations_config.tslocations,
+ .tab = p.settings.tab,
};
const root = sourceRoot(&ctx, dir, content);
const result = try format(&ctx, root, content);
@@ -793,7 +796,7 @@ const TestCtx = struct {
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 };
+ t.ctx = .{ .gpa = testing.allocator, .arena = t.arena_state.allocator(), .source = source, .declarations = declarations, .host = t, .tslocations = true, .tab = @import("config.zig").tab_width };
syntax.start(testing.allocator);
}
@@ -819,7 +822,7 @@ fn emphasised(line: HunkLine) ![]const u8 {
return out.toOwnedSlice(testing.allocator);
}
-fn expectEmphasis(minus: []const u8, plus: []const u8, want_minus: []const u8, want_plus: []const u8) !void {
+fn expectEmphasis(tab: u16, minus: []const u8, plus: []const u8, want_minus: []const u8, want_plus: []const u8) !void {
var arena_state: std.heap.ArenaAllocator = .init(testing.allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
@@ -827,7 +830,7 @@ fn expectEmphasis(minus: []const u8, plus: []const u8, want_minus: []const u8, w
var p = [_]HunkLine{.{ .kind = .added, .code = plus, .styles = try arena.alloc(u8, plus.len), .old = 2, .new = 1 }};
@memset(m[0].styles, 0);
@memset(p[0].styles, 0);
- try inferEdits(arena, &m, &p);
+ try inferEdits(tab, arena, &m, &p);
const got_minus = try emphasised(m[0]);
defer testing.allocator.free(got_minus);
const got_plus = try emphasised(p[0]);
@@ -837,16 +840,16 @@ fn expectEmphasis(minus: []const u8, plus: []const u8, want_minus: []const u8, w
}
test "delta's emphasis: the word a line changed, in both lines" {
- try expectEmphasis(" let x = foo(1);", " let x = bar(1);", "............foo....", "............bar....");
- try expectEmphasis("a b c", "a x c", "..b..", "..x..");
+ try expectEmphasis(@import("config.zig").tab_width, " let x = foo(1);", " let x = bar(1);", "............foo....", "............bar....");
+ try expectEmphasis(@import("config.zig").tab_width, "a b c", "a x c", "..b..", "..x..");
}
test "delta's emphasis: lines too far apart are no change of one another" {
- try expectEmphasis("completely different text", "nothing alike here at all", ".........................", ".........................");
+ try expectEmphasis(@import("config.zig").tab_width, "completely different text", "nothing alike here at all", ".........................", ".........................");
}
test "delta's emphasis: a word added keeps the rest plain" {
- try expectEmphasis("fn run(a: u32)", "fn run(a: u32, b: u32)", "..............", "............., b: u32.");
+ try expectEmphasis(@import("config.zig").tab_width, "fn run(a: u32)", "fn run(a: u32, b: u32)", "..............", "............., b: u32.");
}
test "delta's alignment: kitten to sitting, as align.rs has it" {