summaryrefslogtreecommitdiff
path: root/src/syntax.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/syntax.zig')
-rw-r--r--src/syntax.zig41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/syntax.zig b/src/syntax.zig
index 8209dc2c..bd6a64a0 100644
--- a/src/syntax.zig
+++ b/src/syntax.zig
@@ -690,6 +690,21 @@ pub fn highlightLocationRows(gpa: std.mem.Allocator, content: []const u8, rows:
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);
+ }
+ }
if (!painted) {
gpa.free(styles);
return &.{};
@@ -1203,3 +1218,29 @@ test "syntax location context snapshots retain omitted multiline comment scope"
try std.testing.expectEqual(@intFromEnum(Syn.comment), style);
try std.testing.expectEqual(@intFromEnum(Syn.none), styles[0]);
}
+
+test "syntax declaration locations clear snapshots without muting ordinary context" {
+ if (!enabled) return;
+ const gpa = std.testing.allocator;
+ start(gpa);
+ defer stop();
+ const header = "pub fn run() void {";
+ const content = "| \t" ++ header ++ "\n| \t const value = 42;\nfile.zig:3\t}\n";
+ const snapshots = [_]u8{0xff} ** header.len;
+ 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 } },
+ };
+ const styles = try highlightLocationRows(gpa, content, &rows);
+ defer gpa.free(styles);
+ for (styles[3..][0..header.len]) |style| try std.testing.expectEqual(@as(u8, 0), style);
+ try std.testing.expectEqual(@intFromEnum(Syn.number), styles[std.mem.indexOf(u8, content, "42").?]);
+}