summaryrefslogtreecommitdiff
path: root/src/syntax.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/syntax.zig')
-rw-r--r--src/syntax.zig69
1 files changed, 45 insertions, 24 deletions
diff --git a/src/syntax.zig b/src/syntax.zig
index 96af9fa7..5fe557ae 100644
--- a/src/syntax.zig
+++ b/src/syntax.zig
@@ -12,7 +12,7 @@ const zig_grammar = config.syntax_zig_grammar;
const minimal_grammars = config.syntax_minimal_grammars;
const full_grammars = config.syntax_full_grammars;
-const ts = if (enabled) @import("tree-sitter") else struct {
+pub const ts = if (enabled) @import("tree-sitter") else struct {
pub const Language = opaque {};
pub const Query = opaque {};
pub const Parser = opaque {};
@@ -24,15 +24,18 @@ const LanguageFn = *const fn () callconv(.c) *const ts.Language;
pub const Syn = enum(u8) { none, keyword, string, number, comment };
+/// A query compiled when first asked for: "" for none, null when it fails.
+const Lazy = struct { src: []const u8 = "", query: ?*ts.Query = null, failed: bool = false };
+
const Spec = struct {
name: []const u8,
exts: []const []const u8,
language: *const fn () callconv(.c) *const ts.Language,
query_src: []const u8,
- /// helix's textobjects.scm for the grammar (vendor/queries), "" for none
- objects_src: []const u8 = "",
- objects: ?*ts.Query = null,
- objects_failed: bool = false,
+ /// helix's textobjects.scm and indents.scm for the grammar
+ /// (vendor/queries), compiled the first time they are asked for
+ objects: Lazy = .{},
+ indents: Lazy = .{},
selected: ?Selected = null,
capture_styles: [256]u8 = undefined,
};
@@ -137,7 +140,8 @@ fn initSpecs() [specCount()]Spec {
.exts = g.exts,
.language = @extern(LanguageFn, .{ .name = "tree_sitter_" ++ g.name }),
.query_src = querySrc(g),
- .objects_src = @field(ts_queries, g.name ++ "_textobjects"),
+ .objects = .{ .src = @field(ts_queries, g.name ++ "_textobjects") },
+ .indents = .{ .src = @field(ts_queries, g.name ++ "_indents") },
};
i += 1;
}
@@ -180,9 +184,10 @@ pub fn stop() void {
selected.query.destroy();
}
spec.selected = null;
- if (spec.objects) |q| q.destroy();
- spec.objects = null;
- spec.objects_failed = false;
+ for ([_]*Lazy{ &spec.objects, &spec.indents }) |lazy| {
+ if (lazy.query) |q| q.destroy();
+ lazy.* = .{ .src = lazy.src };
+ }
}
for (&diff_pieces) |*slot| {
if (slot.*) |piece| syntax_allocator.free(piece.styles);
@@ -499,21 +504,37 @@ 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 spec = specFor(path) orelse return null;
+ return lazyQuery(spec, &spec.objects);
+}
+
+/// The grammar's indent query for `path` (src/indent.zig) and the
+/// grammar's name; null when it has none.
+pub fn indentQuery(path: []const u8) ?struct { *ts.Query, []const u8 } {
+ if (!enabled) return null;
+ const spec = specFor(path) orelse return null;
+ return .{ lazyQuery(spec, &spec.indents) orelse return null, spec.name };
+}
+
+fn specFor(path: []const u8) ?*Spec {
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;
- if (spec.objects_src.len == 0 or spec.objects_failed) return null;
- var error_offset: u32 = 0;
- spec.objects = ts.Query.create(spec.language(), spec.objects_src, &error_offset) catch {
- spec.objects_failed = true;
- return null;
- };
- return spec.objects;
+ if (std.ascii.eqlIgnoreCase(ext, choice)) return spec;
};
return null;
}
+fn lazyQuery(spec: *Spec, lazy: *Lazy) ?*ts.Query {
+ if (lazy.query) |q| return q;
+ if (lazy.src.len == 0 or lazy.failed) return null;
+ var error_offset: u32 = 0;
+ lazy.query = ts.Query.create(spec.language(), lazy.src, &error_offset) catch {
+ lazy.failed = true;
+ return null;
+ };
+ return lazy.query;
+}
+
fn captureId(q: *const ts.Query, name: []const u8) ?u32 {
for (0..q.captureCount()) |i| if (std.mem.eql(u8, q.captureNameForId(@intCast(i)) orelse "", name)) return @intCast(i);
return null;
@@ -1260,19 +1281,19 @@ test {
_ = diff;
}
-test "every vendored textobject query compiles against its grammar" {
+test "every vendored textobject and indent query compiles against its grammar" {
if (!enabled) return;
var failed: usize = 0;
- for (&specs) |*spec| {
- if (spec.objects_src.len == 0) continue;
+ for (&specs) |*spec| for ([_]Lazy{ spec.objects, spec.indents }, [_][]const u8{ "textobjects", "indents" }) |lazy, what| {
+ if (lazy.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 });
+ const q = ts.Query.create(spec.language(), lazy.src, &error_offset) catch {
+ std.debug.print("{s} for {s} do not compile (offset {d})\n", .{ what, spec.name, error_offset });
failed += 1;
continue;
};
q.destroy();
- }
+ };
try std.testing.expectEqual(@as(usize, 0), failed);
}