summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-04 23:35:09 -0300
committerGabriel Schneider <[email protected]>2026-11-05 01:16:28 -0300
commitec25911885652c793f8694e6abd035bdc67d1aa6 (patch)
tree5a7c06b20930ba90c360965522abf1777bdf97d6 /src
parent7c299a78d72274a2c101f5da0be4114f7c4dc4f3 (diff)
downloadpardes-ec25911885652c793f8694e6abd035bdc67d1aa6.tar.gz
pardes-ec25911885652c793f8694e6abd035bdc67d1aa6.zip
syntax: highlightLocationRows takes []const locations.Row instead of anytype tested with @hasField, its tests build Rows, the diff window is a named DiffWindow paintDiffPiece takes by type, and the unused look import goes
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src')
-rw-r--r--src/syntax.zig149
1 files changed, 68 insertions, 81 deletions
diff --git a/src/syntax.zig b/src/syntax.zig
index 312678c4..1787335e 100644
--- a/src/syntax.zig
+++ b/src/syntax.zig
@@ -2,7 +2,7 @@ const std = @import("std");
const config = @import("pardes_config");
const tracy = @import("tracy.zig");
const grammar_manifest = @import("grammar_manifest.zig");
-const look = @import("look.zig");
+const locations = @import("locations.zig");
const regexp = @import("regexp.zig");
const c_heap = @import("c_heap");
const diff = @import("diff.zig");
@@ -934,7 +934,7 @@ pub fn highlightLocations(gpa: std.mem.Allocator, content: []const u8, start_byt
/// Formatted results carry the exact source boundary independently of their
/// padded location column. This also colors context whose location is hidden.
/// Consecutive source rows share one parse, retaining multiline syntax.
-pub fn highlightLocationRows(gpa: std.mem.Allocator, content: []const u8, rows: anytype) ![]u8 {
+pub fn highlightLocationRows(gpa: std.mem.Allocator, content: []const u8, rows: []const locations.Row) ![]u8 {
if (!enabled or content.len == 0 or rows.len == 0) return &.{};
const styles = try gpa.alloc(u8, content.len);
errdefer gpa.free(styles);
@@ -952,16 +952,10 @@ pub fn highlightLocationRows(gpa: std.mem.Allocator, content: []const u8, rows:
if (offset >= content.len) break;
const end = std.mem.indexOfScalarPos(u8, content, offset, '\n') orelse content.len;
const source_start = offset + @min(row.code_start, end - offset);
- var address_only = false;
- if (comptime @hasField(@TypeOf(row), "location_end")) {
- address_only = row.location_end > 0 and source_start == end;
- }
+ const address_only = row.location_end > 0 and source_start == end;
// A row its producer coloured whole is coloured below: no
// fragment of it to parse.
- var coloured = false;
- if (comptime @hasField(@TypeOf(row), "colors")) {
- coloured = row.colors.len > 0 and row.colors.len >= end - source_start;
- }
+ const coloured = row.colors.len > 0 and row.colors.len >= end - source_start;
if (!address_only and !coloured) {
sources[source_count] = .{ .index = index, .start = source_start, .len = end - source_start };
source_count += 1;
@@ -1005,34 +999,30 @@ pub fn highlightLocationRows(gpa: std.mem.Allocator, content: []const u8, rows:
// A shown context range can begin inside a comment/string whose opener was
// omitted. Formatter snapshots from the complete source take precedence,
// including zero styles that remove misleading fragment-parser captures.
- if (comptime @hasField(@TypeOf(rows[0]), "colors")) {
- offset = 0;
- for (rows) |row| {
- if (offset >= content.len) break;
- const end = std.mem.indexOfScalarPos(u8, content, offset, '\n') orelse content.len;
- const source_start = offset + @min(row.code_start, end - offset);
- const len = @min(row.colors.len, end - source_start);
- if (len > 0) {
- @memcpy(styles[source_start..][0..len], row.colors[0..len]);
- painted = true;
- }
- offset = @min(end + 1, content.len);
+ offset = 0;
+ for (rows) |row| {
+ if (offset >= content.len) break;
+ const end = std.mem.indexOfScalarPos(u8, content, offset, '\n') orelse content.len;
+ const source_start = offset + @min(row.code_start, end - offset);
+ const len = @min(row.colors.len, end - source_start);
+ if (len > 0) {
+ @memcpy(styles[source_start..][0..len], row.colors[0..len]);
+ painted = true;
}
+ offset = @min(end + 1, content.len);
}
// Declaration context is intentionally muted by the output painter. Keep
// its source in parsing groups, then remove every syntax/style flag after
// complete-source snapshots have been applied.
- if (comptime @hasField(@TypeOf(rows[0]), "declaration")) {
- offset = 0;
- for (rows) |row| {
- if (offset >= content.len) break;
- const end = std.mem.indexOfScalarPos(u8, content, offset, '\n') orelse content.len;
- if (row.declaration) {
- const source_start = offset + @min(row.code_start, end - offset);
- @memset(styles[source_start..end], 0);
- }
- offset = @min(end + 1, content.len);
+ offset = 0;
+ for (rows) |row| {
+ if (offset >= content.len) break;
+ const end = std.mem.indexOfScalarPos(u8, content, offset, '\n') orelse content.len;
+ if (row.declaration) {
+ const source_start = offset + @min(row.code_start, end - offset);
+ @memset(styles[source_start..end], 0);
}
+ offset = @min(end + 1, content.len);
}
if (!painted) {
gpa.free(styles);
@@ -1043,7 +1033,7 @@ pub fn highlightLocationRows(gpa: std.mem.Allocator, content: []const u8, rows:
fn codeAfterLocation(line: []const u8) ?struct { path: []const u8, at: usize, text: []const u8 } {
const trimmed = std.mem.trimStart(u8, line, " \t");
- const target = @import("locations.zig").parseRaw(trimmed);
+ const target = locations.parseRaw(trimmed);
if (target.at.line == 0) return null;
const at = line.len - trimmed.len + target.code_start;
if (at >= line.len) return null;
@@ -1055,15 +1045,15 @@ test "syntax formatted locations color hidden context and preserve multiline sou
const gpa = std.testing.allocator;
start(gpa);
defer stop();
- const Row = struct { path: []const u8, code_start: usize, at: look.Spot };
const first = "a path.c:1 \t/* open";
const second = " \t| comment body";
const third = "a path.c:3 \t*/ int value = 42;";
const content = first ++ "\n" ++ second ++ "\n" ++ third ++ "\n";
- const rows = [_]Row{
- .{ .path = "a path.c", .code_start = "a path.c:1 \t".len, .at = .{ .line = 1 } },
- .{ .path = "a path.c", .code_start = " \t| ".len, .at = .{ .line = 2 } },
- .{ .path = "a path.c", .code_start = "a path.c:3 \t".len, .at = .{ .line = 3 } },
+ const path = @constCast("a path.c");
+ const rows = [_]locations.Row{
+ .{ .kind = .match, .path = path, .code_start = "a path.c:1 \t".len, .location_end = 0, .at = .{ .line = 1 } },
+ .{ .kind = .context, .path = path, .code_start = " \t| ".len, .location_end = 0, .at = .{ .line = 2 } },
+ .{ .kind = .match, .path = path, .code_start = "a path.c:3 \t".len, .location_end = 0, .at = .{ .line = 3 } },
};
const styles = try highlightLocationRows(gpa, content, &rows);
defer gpa.free(styles);
@@ -1080,11 +1070,10 @@ test "syntax formatted locations separate alignment from Markdown indentation" {
const gpa = std.testing.allocator;
start(gpa);
defer stop();
- const Row = struct { path: []const u8, code_start: usize, at: look.Spot };
const content = "short.md:1 \t# Heading\n" ++ "longer path.md:5 \t # Code\n";
- const rows = [_]Row{
- .{ .path = "short.md", .code_start = "short.md:1 \t".len, .at = .{ .line = 1 } },
- .{ .path = "longer path.md", .code_start = "longer path.md:5 \t".len, .at = .{ .line = 5 } },
+ const rows = [_]locations.Row{
+ .{ .kind = .match, .path = @constCast("short.md"), .code_start = "short.md:1 \t".len, .location_end = 0, .at = .{ .line = 1 } },
+ .{ .kind = .match, .path = @constCast("longer path.md"), .code_start = "longer path.md:5 \t".len, .location_end = 0, .at = .{ .line = 5 } },
};
const styles = try highlightLocationRows(gpa, content, &rows);
defer gpa.free(styles);
@@ -1191,21 +1180,7 @@ pub fn highlightDiffRows(gpa: std.mem.Allocator, content: []const u8, start_raw:
const styles = try gpa.alloc(u8, end_byte - start_byte);
errdefer gpa.free(styles);
@memset(styles, 0);
- const Window = struct {
- styles: []u8,
- start: usize,
- fn put(w: @This(), at: usize, bytes: []const u8) void {
- const lo = @max(at, w.start);
- const hi = @min(at + bytes.len, w.start + w.styles.len);
- if (lo < hi) @memcpy(w.styles[lo - w.start .. hi - w.start], bytes[lo - at .. hi - at]);
- }
- fn fill(w: @This(), at: usize, len: usize, style: u8) void {
- const lo = @max(at, w.start);
- const hi = @min(at + len, w.start + w.styles.len);
- if (lo < hi) @memset(w.styles[lo - w.start .. hi - w.start], style);
- }
- };
- const window: Window = .{ .styles = styles, .start = start_byte };
+ const window: DiffWindow = .{ .styles = styles, .start = start_byte };
var walk: diff.Walk = .{ .continued_rows = continued_rows };
// The run of hunk lines not painted yet, with each line's kind.
@@ -1254,7 +1229,24 @@ pub fn highlightDiffRows(gpa: std.mem.Allocator, content: []const u8, start_raw:
return styles;
}
-fn paintDiffPiece(gpa: std.mem.Allocator, window: anytype, bytes: []const u8, at: usize, kinds: []const diff.Kind, path: []const u8) !void {
+/// The styles of `content[start..]` a diff is painted into: what falls
+/// outside them is dropped.
+const DiffWindow = struct {
+ styles: []u8,
+ start: usize,
+ fn put(w: DiffWindow, at: usize, bytes: []const u8) void {
+ const lo = @max(at, w.start);
+ const hi = @min(at + bytes.len, w.start + w.styles.len);
+ if (lo < hi) @memcpy(w.styles[lo - w.start .. hi - w.start], bytes[lo - at .. hi - at]);
+ }
+ fn fill(w: DiffWindow, at: usize, len: usize, style: u8) void {
+ const lo = @max(at, w.start);
+ const hi = @min(at + len, w.start + w.styles.len);
+ if (lo < hi) @memset(w.styles[lo - w.start .. hi - w.start], style);
+ }
+};
+
+fn paintDiffPiece(gpa: std.mem.Allocator, window: DiffWindow, bytes: []const u8, at: usize, kinds: []const diff.Kind, path: []const u8) !void {
const selected: ?Selected = if (enabled and syntax_started) (forExt(std.fs.path.extension(path)) catch null) else null;
if (selected) |sel| {
const hash = std.hash.Wyhash.hash(std.hash.Wyhash.hash(0, sel.name), bytes);
@@ -1931,11 +1923,12 @@ test "syntax location context snapshots retain omitted multiline comment scope"
defer gpa.free(source_colors);
const code = "const int value = 42;";
const source_start = std.mem.indexOf(u8, source, code).?;
- const Row = struct { path: []const u8, code_start: usize, at: look.Spot, colors: []const u8 };
const content = "| \t" ++ code ++ "\n";
- const rows = [_]Row{.{
- .path = "a.c",
+ const rows = [_]locations.Row{.{
+ .kind = .context,
+ .path = @constCast("a.c"),
.code_start = "| \t".len,
+ .location_end = 0,
.at = .{ .line = 2 },
.colors = source_colors[source_start..][0..code.len],
}};
@@ -1953,18 +1946,12 @@ test "syntax declaration locations clear snapshots without muting ordinary conte
defer stop();
const header = "pub fn run() void {";
const content = "| \t" ++ header ++ "\n| \t const value = 42;\nfile.zig:3\t}\n";
- const snapshots = @as([header.len]u8, @splat(0xff));
- const Row = struct {
- path: []const u8 = "file.zig",
- code_start: usize,
- at: look.Spot,
- colors: []const u8 = &.{},
- declaration: bool = false,
- };
- const rows = [_]Row{
- .{ .code_start = 3, .at = .{ .line = 1 }, .colors = &snapshots, .declaration = true },
- .{ .code_start = 3, .at = .{ .line = 2 } },
- .{ .code_start = "file.zig:3\t".len, .at = .{ .line = 3 } },
+ var snapshots = @as([header.len]u8, @splat(0xff));
+ const path = @constCast("file.zig");
+ const rows = [_]locations.Row{
+ .{ .kind = .context, .path = path, .code_start = 3, .location_end = 0, .at = .{ .line = 1 }, .colors = &snapshots, .declaration = true },
+ .{ .kind = .context, .path = path, .code_start = 3, .location_end = 0, .at = .{ .line = 2 } },
+ .{ .kind = .match, .path = path, .code_start = "file.zig:3\t".len, .location_end = 0, .at = .{ .line = 3 } },
};
const styles = try highlightLocationRows(gpa, content, &rows);
defer gpa.free(styles);
@@ -1977,15 +1964,15 @@ test "syntax stacked addresses do not split multiline source groups" {
const gpa = std.testing.allocator;
start(gpa);
defer stop();
- const Row = struct { path: []const u8 = "a.c", code_start: usize, location_end: usize, at: look.Spot };
const content = "a.c:1:1\n/* open\na.c:2:1\nint value = 42;\na.c:3:1\n*/ int other = 7;\n";
- const rows = [_]Row{
- .{ .code_start = 7, .location_end = 7, .at = .{ .line = 1 } },
- .{ .code_start = 0, .location_end = 0, .at = .{ .line = 1 } },
- .{ .code_start = 7, .location_end = 7, .at = .{ .line = 2 } },
- .{ .code_start = 0, .location_end = 0, .at = .{ .line = 2 } },
- .{ .code_start = 7, .location_end = 7, .at = .{ .line = 3 } },
- .{ .code_start = 0, .location_end = 0, .at = .{ .line = 3 } },
+ const path = @constCast("a.c");
+ const rows = [_]locations.Row{
+ .{ .kind = .match, .path = path, .code_start = 7, .location_end = 7, .at = .{ .line = 1 } },
+ .{ .kind = .preview, .path = path, .code_start = 0, .location_end = 0, .at = .{ .line = 1 } },
+ .{ .kind = .match, .path = path, .code_start = 7, .location_end = 7, .at = .{ .line = 2 } },
+ .{ .kind = .preview, .path = path, .code_start = 0, .location_end = 0, .at = .{ .line = 2 } },
+ .{ .kind = .match, .path = path, .code_start = 7, .location_end = 7, .at = .{ .line = 3 } },
+ .{ .kind = .preview, .path = path, .code_start = 0, .location_end = 0, .at = .{ .line = 3 } },
};
const styles = try highlightLocationRows(gpa, content, &rows);
defer gpa.free(styles);