diff options
Diffstat (limited to 'src/syntax.zig')
| -rw-r--r-- | src/syntax.zig | 295 |
1 files changed, 148 insertions, 147 deletions
diff --git a/src/syntax.zig b/src/syntax.zig index 753b11b1..08b26096 100644 --- a/src/syntax.zig +++ b/src/syntax.zig @@ -7,10 +7,6 @@ const regexp = @import("regexp.zig"); const c_heap = @import("c_heap"); const diff = @import("diff.zig"); -test { - _ = diff; -} - pub const enabled = config.syntax_highlighting; const zig_grammar = config.syntax_zig_grammar; const minimal_grammars = config.syntax_minimal_grammars; @@ -622,22 +618,6 @@ pub fn objectNext(path: []const u8, tree_ptr: *anyopaque, text: []const u8, obje return s; } -test "every vendored textobject query compiles against its grammar" { - if (!enabled) return; - var failed: usize = 0; - for (&specs) |*spec| { - if (spec.objects_src.len == 0) continue; - var error_offset: u32 = 0; - const q = ts.Query.create(spec.language(), spec.objects_src, &error_offset) catch { - std.debug.print("textobjects for {s} do not compile (offset {d})\n", .{ spec.name, error_offset }); - failed += 1; - continue; - }; - q.destroy(); - } - try std.testing.expectEqual(@as(usize, 0), failed); -} - /// Owned source analysis. Both slices use the allocator passed to analyzeSource. /// The parsed tree is released before returning; callers can cache this result. pub const SourceAnalysis = struct { @@ -814,89 +794,6 @@ fn paintTree(styles: []u8, source: []const u8, selected: Selected, tree: *ts.Tre } } -test "syntax context nested Zig containers and multiline function signatures" { - if (!enabled) return; - const gpa = std.testing.allocator; - start(gpa); - defer stop(); - const source = - "pub const Outer = struct {\n" ++ - " const Inner = struct {\n" ++ - " pub fn run(\n" ++ - " value: u32,\n" ++ - " ) void {\n" ++ - " const ignored = .{\n" ++ - " value,\n" ++ - " };\n" ++ - " }\n" ++ - " };\n" ++ - "};\n"; - const declarations = try contextDeclarations(gpa, "nested.zig", source); - defer gpa.free(declarations); - try std.testing.expectEqual(@as(usize, 3), declarations.len); - try std.testing.expectEqual(@as(usize, 0), declarations[0].start_line); - try std.testing.expectEqual(@as(usize, 10), declarations[0].end_line); - try std.testing.expectEqual(@as(usize, 1), declarations[1].start_line); - try std.testing.expectEqual(@as(usize, 2), declarations[2].start_line); - try std.testing.expectEqual(@as(usize, 4), declarations[2].header_end_line); - try std.testing.expectEqual(@as(usize, 8), declarations[2].end_line); - try std.testing.expectEqualStrings(" pub fn run(\n value: u32,\n ) void {", source[declarations[2].start_byte..declarations[2].header_end_byte]); - const unsupported = try contextDeclarations(gpa, "notes.unknown", source); - defer gpa.free(unsupported); - try std.testing.expectEqual(@as(usize, 0), unsupported.len); -} - -test "syntax context Python class and function body excludes first statement" { - if (!enabled or !full_grammars) return; - const gpa = std.testing.allocator; - start(gpa); - defer stop(); - const source = "class Outer:\n def run(\n self, value,\n ):\n return value\n"; - const declarations = try contextDeclarations(gpa, "nested.py", source); - defer gpa.free(declarations); - try std.testing.expectEqual(@as(usize, 2), declarations.len); - try std.testing.expectEqual(@as(usize, 0), declarations[0].header_end_line); - try std.testing.expectEqual(@as(usize, 3), declarations[1].header_end_line); -} - -test "syntax context multiline bindings and C++ namespaces preserve declaration starts" { - if (!enabled) return; - const gpa = std.testing.allocator; - start(gpa); - defer stop(); - const zig_source = "const Outer =\n struct {\n field: u8,\n };\n"; - const zig_declarations = try contextDeclarations(gpa, "nested.zig", zig_source); - defer gpa.free(zig_declarations); - try std.testing.expectEqual(@as(usize, 1), zig_declarations.len); - try std.testing.expectEqual(@as(usize, 0), zig_declarations[0].start_line); - try std.testing.expectEqual(@as(usize, 1), zig_declarations[0].header_end_line); - if (!minimal_grammars and !full_grammars) return; - const cpp_source = "namespace example {\nstruct Outer {\n int run() {\n return 1;\n }\n};\n}\n"; - const cpp_declarations = try contextDeclarations(gpa, "nested.cpp", cpp_source); - defer gpa.free(cpp_declarations); - try std.testing.expectEqual(@as(usize, 3), cpp_declarations.len); - for (cpp_declarations, 0..) |declaration, row| { - try std.testing.expectEqual(row, declaration.start_line); - try std.testing.expectEqual(row, declaration.header_end_line); - } -} - -test "syntax context Rust modules impls and methods remain nested" { - if (!enabled or !full_grammars) return; - const gpa = std.testing.allocator; - start(gpa); - defer stop(); - const source = "mod outer {\n impl Example {\n fn run(&self) {\n work();\n }\n }\n}\n"; - const declarations = try contextDeclarations(gpa, "nested.rs", source); - defer gpa.free(declarations); - try std.testing.expectEqual(@as(usize, 3), declarations.len); - for (declarations, 0..) |declaration, row| { - try std.testing.expectEqual(row, declaration.start_line); - try std.testing.expectEqual(row, declaration.header_end_line); - try std.testing.expectEqual(@as(usize, 6) - row, declaration.end_line); - } -} - pub fn highlightLocations(gpa: std.mem.Allocator, content: []const u8, start_byte_raw: usize, end_byte_raw: usize) ![]u8 { const tz = tracy.zone(@src(), "highlightLocations"); defer tz.end(); @@ -1041,46 +938,6 @@ fn codeAfterLocation(line: []const u8) ?struct { path: []const u8, at: usize, te return .{ .path = target.path, .at = at, .text = line[at..] }; } -test "syntax formatted locations color hidden context and preserve multiline source" { - if (!enabled or (!minimal_grammars and !full_grammars)) return; - const gpa = std.testing.allocator; - start(gpa); - defer stop(); - 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 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); - const body = std.mem.indexOf(u8, content, "comment body").?; - try std.testing.expectEqual(@backingInt(Syn.comment), styles[body]); - try std.testing.expectEqual(@backingInt(Syn.none), styles[body - 1]); - const number = std.mem.indexOf(u8, content, "42").?; - try std.testing.expectEqual(@backingInt(Syn.number), styles[number]); - try std.testing.expectEqual(@backingInt(Syn.none), styles[0]); -} - -test "syntax formatted locations separate alignment from Markdown indentation" { - if (!enabled or !full_grammars) return; - const gpa = std.testing.allocator; - start(gpa); - defer stop(); - const content = "short.md:1 \t# Heading\n" ++ "longer path.md:5 \t # Code\n"; - 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); - try std.testing.expectEqual(@backingInt(Syn.keyword), styles[std.mem.indexOf(u8, content, "Heading").?]); - try std.testing.expectEqual(@backingInt(Syn.string), styles[std.mem.indexOf(u8, content, "Code").?]); -} fn inject(styles: []u8, source: []const u8, node: ts.Node, markdown: bool) void { const kind = node.kind(); if (markdown and std.mem.eql(u8, kind, "inline")) { @@ -1394,6 +1251,154 @@ fn codeStart(line: []const u8, kind: diff.Kind) usize { }; } +fn diffSynAt(styles: []const u8, text: []const u8, needle: []const u8, offset: usize) Syn { + return @fromBackingInt(@intCast(styles[std.mem.indexOf(u8, text, needle).? + offset] & syn_bits)); +} + +test { + _ = diff; +} + +test "every vendored textobject query compiles against its grammar" { + if (!enabled) return; + var failed: usize = 0; + for (&specs) |*spec| { + if (spec.objects_src.len == 0) continue; + var error_offset: u32 = 0; + const q = ts.Query.create(spec.language(), spec.objects_src, &error_offset) catch { + std.debug.print("textobjects for {s} do not compile (offset {d})\n", .{ spec.name, error_offset }); + failed += 1; + continue; + }; + q.destroy(); + } + try std.testing.expectEqual(@as(usize, 0), failed); +} + +test "syntax context nested Zig containers and multiline function signatures" { + if (!enabled) return; + const gpa = std.testing.allocator; + start(gpa); + defer stop(); + const source = + "pub const Outer = struct {\n" ++ + " const Inner = struct {\n" ++ + " pub fn run(\n" ++ + " value: u32,\n" ++ + " ) void {\n" ++ + " const ignored = .{\n" ++ + " value,\n" ++ + " };\n" ++ + " }\n" ++ + " };\n" ++ + "};\n"; + const declarations = try contextDeclarations(gpa, "nested.zig", source); + defer gpa.free(declarations); + try std.testing.expectEqual(@as(usize, 3), declarations.len); + try std.testing.expectEqual(@as(usize, 0), declarations[0].start_line); + try std.testing.expectEqual(@as(usize, 10), declarations[0].end_line); + try std.testing.expectEqual(@as(usize, 1), declarations[1].start_line); + try std.testing.expectEqual(@as(usize, 2), declarations[2].start_line); + try std.testing.expectEqual(@as(usize, 4), declarations[2].header_end_line); + try std.testing.expectEqual(@as(usize, 8), declarations[2].end_line); + try std.testing.expectEqualStrings(" pub fn run(\n value: u32,\n ) void {", source[declarations[2].start_byte..declarations[2].header_end_byte]); + const unsupported = try contextDeclarations(gpa, "notes.unknown", source); + defer gpa.free(unsupported); + try std.testing.expectEqual(@as(usize, 0), unsupported.len); +} + +test "syntax context Python class and function body excludes first statement" { + if (!enabled or !full_grammars) return; + const gpa = std.testing.allocator; + start(gpa); + defer stop(); + const source = "class Outer:\n def run(\n self, value,\n ):\n return value\n"; + const declarations = try contextDeclarations(gpa, "nested.py", source); + defer gpa.free(declarations); + try std.testing.expectEqual(@as(usize, 2), declarations.len); + try std.testing.expectEqual(@as(usize, 0), declarations[0].header_end_line); + try std.testing.expectEqual(@as(usize, 3), declarations[1].header_end_line); +} + +test "syntax context multiline bindings and C++ namespaces preserve declaration starts" { + if (!enabled) return; + const gpa = std.testing.allocator; + start(gpa); + defer stop(); + const zig_source = "const Outer =\n struct {\n field: u8,\n };\n"; + const zig_declarations = try contextDeclarations(gpa, "nested.zig", zig_source); + defer gpa.free(zig_declarations); + try std.testing.expectEqual(@as(usize, 1), zig_declarations.len); + try std.testing.expectEqual(@as(usize, 0), zig_declarations[0].start_line); + try std.testing.expectEqual(@as(usize, 1), zig_declarations[0].header_end_line); + if (!minimal_grammars and !full_grammars) return; + const cpp_source = "namespace example {\nstruct Outer {\n int run() {\n return 1;\n }\n};\n}\n"; + const cpp_declarations = try contextDeclarations(gpa, "nested.cpp", cpp_source); + defer gpa.free(cpp_declarations); + try std.testing.expectEqual(@as(usize, 3), cpp_declarations.len); + for (cpp_declarations, 0..) |declaration, row| { + try std.testing.expectEqual(row, declaration.start_line); + try std.testing.expectEqual(row, declaration.header_end_line); + } +} + +test "syntax context Rust modules impls and methods remain nested" { + if (!enabled or !full_grammars) return; + const gpa = std.testing.allocator; + start(gpa); + defer stop(); + const source = "mod outer {\n impl Example {\n fn run(&self) {\n work();\n }\n }\n}\n"; + const declarations = try contextDeclarations(gpa, "nested.rs", source); + defer gpa.free(declarations); + try std.testing.expectEqual(@as(usize, 3), declarations.len); + for (declarations, 0..) |declaration, row| { + try std.testing.expectEqual(row, declaration.start_line); + try std.testing.expectEqual(row, declaration.header_end_line); + try std.testing.expectEqual(@as(usize, 6) - row, declaration.end_line); + } +} + +test "syntax formatted locations color hidden context and preserve multiline source" { + if (!enabled or (!minimal_grammars and !full_grammars)) return; + const gpa = std.testing.allocator; + start(gpa); + defer stop(); + 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 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); + const body = std.mem.indexOf(u8, content, "comment body").?; + try std.testing.expectEqual(@backingInt(Syn.comment), styles[body]); + try std.testing.expectEqual(@backingInt(Syn.none), styles[body - 1]); + const number = std.mem.indexOf(u8, content, "42").?; + try std.testing.expectEqual(@backingInt(Syn.number), styles[number]); + try std.testing.expectEqual(@backingInt(Syn.none), styles[0]); +} + +test "syntax formatted locations separate alignment from Markdown indentation" { + if (!enabled or !full_grammars) return; + const gpa = std.testing.allocator; + start(gpa); + defer stop(); + const content = "short.md:1 \t# Heading\n" ++ "longer path.md:5 \t # Code\n"; + 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); + try std.testing.expectEqual(@backingInt(Syn.keyword), styles[std.mem.indexOf(u8, content, "Heading").?]); + try std.testing.expectEqual(@backingInt(Syn.string), styles[std.mem.indexOf(u8, content, "Code").?]); +} + test "syntax a results row is coloured by the file its location names" { if (!enabled) return; start(std.testing.allocator); @@ -1630,10 +1635,6 @@ test "syntax highlightDiff colors unified diff lines by prefix" { try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, " context").?] & (diff_added | diff_removed) == 0); } -fn diffSynAt(styles: []const u8, text: []const u8, needle: []const u8, offset: usize) Syn { - return @fromBackingInt(@intCast(styles[std.mem.indexOf(u8, text, needle).? + offset] & syn_bits)); -} - test "syntax a Zig hunk is coloured as Zig, each side parsed whole" { if (!enabled) return; start(std.testing.allocator); |
