diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-06 12:40:11 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-06 14:56:36 -0300 |
| commit | 80f4398f1fed8d46d73cab8045005c02189b34fc (patch) | |
| tree | ad60d6e83a1ba08d9869012ac819ff58ca826248 /src/syntax.zig | |
| parent | a864ef66736d3cfbdffed8b98a3c46be7985db74 (diff) | |
| download | pardes-80f4398f1fed8d46d73cab8045005c02189b34fc.tar.gz pardes-80f4398f1fed8d46d73cab8045005c02189b34fc.zip | |
idioms #14, the rest: wire's has_cell_pixels (@hasField on a struct that always has the field) goes with its comptime branches, syntax's predicate ops are a stringToEnum switch, the public surfaces of sam_edit, locations, Names, Macro, selection_pipe, recent and builtins_json name their sets (OutOfMemory, Writer.Error), and std.fs.path and max_path_bytes are std.Io.Dir's
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/syntax.zig')
| -rw-r--r-- | src/syntax.zig | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/src/syntax.zig b/src/syntax.zig index a498357c..753b11b1 100644 --- a/src/syntax.zig +++ b/src/syntax.zig @@ -298,7 +298,7 @@ pub fn highlightFileRange(gpa: std.mem.Allocator, path: []const u8, content: []c const zone = tracy.zone(@src(), "highlightFileRange"); defer zone.end(); if (!enabled) return &.{}; - const selected = (try forExt(std.fs.path.extension(path))) orelse return &.{}; + const selected = (try forExt(std.Io.Dir.path.extension(path))) orelse return &.{}; const start_byte = @min(start_raw, content.len); const end_byte = @max(start_byte, @min(end_raw, content.len)); const source = content[start_byte..end_byte]; @@ -313,7 +313,7 @@ pub fn highlightFileRange(gpa: std.mem.Allocator, path: []const u8, content: []c /// ready here, on the loop, so the worker only reads its query. pub fn readyApart(path: []const u8) bool { if (!enabled) return false; - const selected = (forExt(std.fs.path.extension(path)) catch return false) orelse return false; + const selected = (forExt(std.Io.Dir.path.extension(path)) catch return false) orelse return false; return !std.mem.eql(u8, selected.name, "markdown") and !std.mem.eql(u8, selected.name, "typst"); } @@ -322,7 +322,7 @@ pub fn readyApart(path: []const u8) bool { /// is only read); `halt` ends the parse early. After `readyApart`. pub fn highlightApart(gpa: std.mem.Allocator, path: []const u8, source: []const u8, halt: *const std.atomic.Value(bool)) ![]u8 { if (comptime !enabled) return &.{}; - const shared = (try forExt(std.fs.path.extension(path))) orelse return &.{}; + const shared = (try forExt(std.Io.Dir.path.extension(path))) orelse return &.{}; const tree = try parseApart(shared.lang, source, halt); defer tree.destroy(); if (halt.load(.monotonic)) return error.Canceled; @@ -379,7 +379,7 @@ pub const ContextDeclaration = struct { pub fn supportsPath(path: []const u8) bool { if (!enabled) return false; - const ext = std.fs.path.extension(path); + const ext = std.Io.Dir.path.extension(path); for (specs) |spec| for (spec.exts) |choice| { if (std.ascii.eqlIgnoreCase(ext, choice)) return true; }; @@ -390,7 +390,7 @@ pub fn supportsPath(path: []const u8) bool { /// (helix's Alt-o and kin). Opaque outside this file; null without a grammar. pub fn parseTree(path: []const u8, content: []const u8) ?*anyopaque { if (!enabled) return null; - const selected = (forExt(std.fs.path.extension(path)) catch return null) orelse return null; + const selected = (forExt(std.Io.Dir.path.extension(path)) catch return null) orelse return null; const tree = selected.parser.parseString(content, null) orelse return null; if (held_trees) |h| h.born.append(h.meta, @ptrCast(tree)) catch {}; return @ptrCast(tree); @@ -510,7 +510,7 @@ fn objectName(o: Object) []const u8 { /// The grammar's textobject query for `path`, compiled the first time; null /// when the grammar has none. fn objectQuery(path: []const u8) ?*ts.Query { - const ext = std.fs.path.extension(path); + const ext = std.Io.Dir.path.extension(path); for (&specs) |*spec| for (spec.exts) |choice| { if (!std.ascii.eqlIgnoreCase(ext, choice)) continue; if (spec.objects) |q| return q; @@ -557,11 +557,12 @@ fn matchSpan(q: *const ts.Query, match: anytype, capture: u32, text: []const u8) const want = if (pred[2].type == .string) q.stringValueForId(pred[2].value_id) orelse continue else for (match.captures) |c| { if (c.index == pred[2].value_id) break text[@min(c.node.startByte(), text.len)..@min(c.node.endByte(), text.len)]; } else continue; - if (std.mem.eql(u8, op, "eq?")) { - if (!std.mem.eql(u8, subject, want)) return null; - } else if (std.mem.eql(u8, op, "match?")) { - var re = regexp.Regex.compile(want) catch continue; - if ((re.find(subject, 0, subject.len, subject.len) catch null) == null) return null; + switch (std.meta.stringToEnum(enum { @"eq?", @"match?" }, op) orelse continue) { + .@"eq?" => if (!std.mem.eql(u8, subject, want)) return null, + .@"match?" => { + var re = regexp.Regex.compile(want) catch continue; + if ((re.find(subject, 0, subject.len, subject.len) catch null) == null) return null; + }, } } return found; @@ -652,7 +653,7 @@ pub const SourceAnalysis = struct { pub fn analyzeSource(gpa: std.mem.Allocator, path: []const u8, content: []const u8, want_declarations: bool, want_colors: bool) !SourceAnalysis { if (!enabled or content.len == 0 or (!want_declarations and !want_colors)) return .{}; - const selected = (try forExt(std.fs.path.extension(path))) orelse return .{}; + const selected = (try forExt(std.Io.Dir.path.extension(path))) orelse return .{}; const tree = selected.parser.parseString(content, null) orelse return .{}; defer tree.destroy(); var result: SourceAnalysis = .{}; @@ -677,7 +678,7 @@ pub fn contextDeclarations(gpa: std.mem.Allocator, path: []const u8, content: [] pub fn declarationsApart(gpa: std.mem.Allocator, path: []const u8, content: []const u8, halt: *const std.atomic.Value(bool)) ![]ContextDeclaration { if (comptime !enabled) return &.{}; if (content.len == 0) return &.{}; - const ext = std.fs.path.extension(path); + const ext = std.Io.Dir.path.extension(path); const spec = specs: for (&specs) |*spec| { for (spec.exts) |choice| if (std.ascii.eqlIgnoreCase(ext, choice)) break :specs spec; } else return &.{}; @@ -915,7 +916,7 @@ pub fn highlightLocations(gpa: std.mem.Allocator, content: []const u8, start_byt while (lines.next()) |line| { defer offset += line.len + 1; const code = codeAfterLocation(line) orelse continue; - const ext = std.fs.path.extension(code.path); + const ext = std.Io.Dir.path.extension(code.path); if (!std.mem.eql(u8, ext, memo_ext)) { memo_ext = ext; memo = forExt(ext) catch null; @@ -972,7 +973,7 @@ pub fn highlightLocationRows(gpa: std.mem.Allocator, content: []const u8, rows: const previous = rows[sources[end - 1].index]; if (!std.mem.eql(u8, row.path, first_row.path) or row.at.line != previous.at.line +| 1) break; } - const language = (forExt(std.fs.path.extension(first_row.path)) catch null); + const language = (forExt(std.Io.Dir.path.extension(first_row.path)) catch null); if (language) |selected| { var code_len: usize = 0; for (sources[first..end]) |source| { @@ -1247,7 +1248,7 @@ const DiffWindow = struct { }; 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; + const selected: ?Selected = if (enabled and syntax_started) (forExt(std.Io.Dir.path.extension(path)) catch null) else null; if (selected) |sel| { const hash = std.hash.Wyhash.hash(std.hash.Wyhash.hash(0, sel.name), bytes); diff_clock += 1; @@ -2008,7 +2009,7 @@ test "syntax source analysis preserves declarations and injected colors" { // Compare the previous named-node walk to the cursor walk, including // source ordering, wrapper deduplication and inclusive declaration ends. - const selected = (try forExt(std.fs.path.extension(fixture.path))).?; + const selected = (try forExt(std.Io.Dir.path.extension(fixture.path))).?; const tree = selected.parser.parseString(fixture.source, null).?; defer tree.destroy(); var reference: std.ArrayList(ContextDeclaration) = .empty; |
