From da3263702e39574046b56586df8465b8eccdab7d Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 3 Jul 2026 09:01:38 -0300 Subject: separated all ts grammars vs a small set of them --- paint.zig | 172 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 86 insertions(+), 86 deletions(-) (limited to 'paint.zig') diff --git a/paint.zig b/paint.zig index 2f953a5a..b4ab932b 100644 --- a/paint.zig +++ b/paint.zig @@ -2,10 +2,13 @@ const std = @import("std"); const ui = @import("ui"); const app_config = @import("app_config"); const ghostty_vt = @import("ghostty-vt"); -const ts = if (app_config.syntax_highlighting) @import("tree-sitter") else struct { +const syntax_highlighting = app_config.syntax_highlighting; +const syntax_minimal_grammars = app_config.syntax_minimal_grammars; +const syntax_full_grammars = app_config.syntax_full_grammars; +const ts = if (syntax_highlighting) @import("tree-sitter") else struct { pub const Language = opaque {}; }; -const ts_queries = if (app_config.syntax_highlighting) @import("ts_queries") else struct {}; +const ts_queries = if (syntax_highlighting) @import("ts_queries") else struct {}; extern fn tree_sitter_ada() callconv(.c) *const ts.Language; extern fn tree_sitter_bash() callconv(.c) *const ts.Language; @@ -168,89 +171,86 @@ fn extIs(ext: []const u8, comptime choices: anytype) bool { } pub fn highlightFile(gpa: std.mem.Allocator, path: []const u8, content: []const u8) ![]u8 { - if (!app_config.syntax_highlighting) return &.{}; + if (!syntax_highlighting) return &.{}; const ext = std.fs.path.extension(path); - var lang: *const ts.Language = undefined; - var query_src: []const u8 = undefined; - if (extIs(ext, .{ ".adb", ".ads", ".ada" })) { - lang = tree_sitter_ada(); - query_src = ts_queries.ada_highlights; - } else if (extIs(ext, .{ ".sh", ".bash", ".zsh" })) { - lang = tree_sitter_bash(); - query_src = ts_queries.bash_highlights; - } else if (extIs(ext, .{ ".c", ".h" })) { - lang = tree_sitter_c(); - query_src = ts_queries.c_highlights; - } else if (extIs(ext, .{ ".cs", ".csx" })) { - lang = tree_sitter_c_sharp(); - query_src = ts_queries.c_sharp_highlights; - } else if (extIs(ext, .{ ".clj", ".cljs", ".cljc", ".edn" })) { - lang = tree_sitter_clojure(); - query_src = ts_queries.clojure_highlights; - } else if (extIs(ext, .{ ".cpp", ".cc", ".cxx", ".hpp", ".hh", ".hxx" })) { - lang = tree_sitter_cpp(); - query_src = ts_queries.cpp_highlights; - } else if (extIs(ext, .{".css"})) { - lang = tree_sitter_css(); - query_src = ts_queries.css_highlights; - } else if (extIs(ext, .{ ".ex", ".exs" })) { - lang = tree_sitter_elixir(); - query_src = ts_queries.elixir_highlights; - } else if (extIs(ext, .{ ".erl", ".hrl" })) { - lang = tree_sitter_erlang(); - query_src = ts_queries.erlang_highlights; - } else if (extIs(ext, .{ ".f", ".for", ".ftn", ".f90", ".f95", ".f03", ".f08" })) { - lang = tree_sitter_fortran(); - query_src = ts_queries.fortran_highlights; - } else if (extIs(ext, .{".go"})) { - lang = tree_sitter_go(); - query_src = ts_queries.go_highlights; - } else if (extIs(ext, .{ ".hs", ".lhs" })) { - lang = tree_sitter_haskell(); - query_src = ts_queries.haskell_highlights; - } else if (extIs(ext, .{ ".html", ".htm" })) { - lang = tree_sitter_html(); - query_src = ts_queries.html_highlights; - } else if (extIs(ext, .{".java"})) { - lang = tree_sitter_java(); - query_src = ts_queries.java_highlights; - } else if (extIs(ext, .{ ".js", ".jsx", ".mjs", ".cjs" })) { - lang = tree_sitter_javascript(); - query_src = ts_queries.javascript_highlights; - } else if (extIs(ext, .{".json"})) { - lang = tree_sitter_json(); - query_src = ts_queries.json_highlights; - } else if (extIs(ext, .{ ".kt", ".kts" })) { - lang = tree_sitter_kotlin(); - query_src = ts_queries.kotlin_highlights; - } else if (extIs(ext, .{ ".ml", ".mli" })) { - lang = tree_sitter_ocaml(); - query_src = ts_queries.ocaml_highlights; - } else if (extIs(ext, .{ ".pas", ".pp", ".p" })) { - lang = tree_sitter_pascal(); - query_src = ts_queries.pascal_highlights; - } else if (extIs(ext, .{ ".php", ".phtml", ".php3", ".php4", ".php5" })) { - lang = tree_sitter_php(); - query_src = ts_queries.php_highlights; - } else if (extIs(ext, .{ ".ps1", ".psm1", ".psd1" })) { - lang = tree_sitter_powershell(); - query_src = ts_queries.powershell_highlights; - } else if (extIs(ext, .{ ".py", ".pyw" })) { - lang = tree_sitter_python(); - query_src = ts_queries.python_highlights; - } else if (extIs(ext, .{ ".rb", ".rake" })) { - lang = tree_sitter_ruby(); - query_src = ts_queries.ruby_highlights; - } else if (extIs(ext, .{".rs"})) { - lang = tree_sitter_rust(); - query_src = ts_queries.rust_highlights; - } else if (extIs(ext, .{ ".scala", ".sc" })) { - lang = tree_sitter_scala(); - query_src = ts_queries.scala_highlights; - } else if (extIs(ext, .{ ".zig", ".zon" })) { - lang = tree_sitter_zig(); - query_src = ts_queries.zig_highlights; - } else return &.{}; + const selected = blk: { + const Selected = struct { + lang: *const ts.Language, + query_src: []const u8, + }; + + if (syntax_full_grammars) { + if (extIs(ext, .{ ".adb", ".ads", ".ada" })) { + break :blk Selected{ .lang = tree_sitter_ada(), .query_src = ts_queries.ada_highlights }; + } else if (extIs(ext, .{ ".sh", ".bash", ".zsh" })) { + break :blk Selected{ .lang = tree_sitter_bash(), .query_src = ts_queries.bash_highlights }; + } + } + if (syntax_minimal_grammars) { + if (extIs(ext, .{ ".c", ".h" })) { + break :blk Selected{ .lang = tree_sitter_c(), .query_src = ts_queries.c_highlights }; + } + } + if (syntax_full_grammars) { + if (extIs(ext, .{ ".cs", ".csx" })) { + break :blk Selected{ .lang = tree_sitter_c_sharp(), .query_src = ts_queries.c_sharp_highlights }; + } else if (extIs(ext, .{ ".clj", ".cljs", ".cljc", ".edn" })) { + break :blk Selected{ .lang = tree_sitter_clojure(), .query_src = ts_queries.clojure_highlights }; + } + } + if (syntax_minimal_grammars) { + if (extIs(ext, .{ ".cpp", ".cc", ".cxx", ".hpp", ".hh", ".hxx" })) { + break :blk Selected{ .lang = tree_sitter_cpp(), .query_src = ts_queries.cpp_highlights }; + } + } + if (syntax_full_grammars) { + if (extIs(ext, .{".css"})) { + break :blk Selected{ .lang = tree_sitter_css(), .query_src = ts_queries.css_highlights }; + } else if (extIs(ext, .{ ".ex", ".exs" })) { + break :blk Selected{ .lang = tree_sitter_elixir(), .query_src = ts_queries.elixir_highlights }; + } else if (extIs(ext, .{ ".erl", ".hrl" })) { + break :blk Selected{ .lang = tree_sitter_erlang(), .query_src = ts_queries.erlang_highlights }; + } else if (extIs(ext, .{ ".f", ".for", ".ftn", ".f90", ".f95", ".f03", ".f08" })) { + break :blk Selected{ .lang = tree_sitter_fortran(), .query_src = ts_queries.fortran_highlights }; + } else if (extIs(ext, .{".go"})) { + break :blk Selected{ .lang = tree_sitter_go(), .query_src = ts_queries.go_highlights }; + } else if (extIs(ext, .{ ".hs", ".lhs" })) { + break :blk Selected{ .lang = tree_sitter_haskell(), .query_src = ts_queries.haskell_highlights }; + } else if (extIs(ext, .{ ".html", ".htm" })) { + break :blk Selected{ .lang = tree_sitter_html(), .query_src = ts_queries.html_highlights }; + } else if (extIs(ext, .{".java"})) { + break :blk Selected{ .lang = tree_sitter_java(), .query_src = ts_queries.java_highlights }; + } else if (extIs(ext, .{ ".js", ".jsx", ".mjs", ".cjs" })) { + break :blk Selected{ .lang = tree_sitter_javascript(), .query_src = ts_queries.javascript_highlights }; + } else if (extIs(ext, .{".json"})) { + break :blk Selected{ .lang = tree_sitter_json(), .query_src = ts_queries.json_highlights }; + } else if (extIs(ext, .{ ".kt", ".kts" })) { + break :blk Selected{ .lang = tree_sitter_kotlin(), .query_src = ts_queries.kotlin_highlights }; + } else if (extIs(ext, .{ ".ml", ".mli" })) { + break :blk Selected{ .lang = tree_sitter_ocaml(), .query_src = ts_queries.ocaml_highlights }; + } else if (extIs(ext, .{ ".pas", ".pp", ".p" })) { + break :blk Selected{ .lang = tree_sitter_pascal(), .query_src = ts_queries.pascal_highlights }; + } else if (extIs(ext, .{ ".php", ".phtml", ".php3", ".php4", ".php5" })) { + break :blk Selected{ .lang = tree_sitter_php(), .query_src = ts_queries.php_highlights }; + } else if (extIs(ext, .{ ".ps1", ".psm1", ".psd1" })) { + break :blk Selected{ .lang = tree_sitter_powershell(), .query_src = ts_queries.powershell_highlights }; + } else if (extIs(ext, .{ ".py", ".pyw" })) { + break :blk Selected{ .lang = tree_sitter_python(), .query_src = ts_queries.python_highlights }; + } else if (extIs(ext, .{ ".rb", ".rake" })) { + break :blk Selected{ .lang = tree_sitter_ruby(), .query_src = ts_queries.ruby_highlights }; + } else if (extIs(ext, .{".rs"})) { + break :blk Selected{ .lang = tree_sitter_rust(), .query_src = ts_queries.rust_highlights }; + } else if (extIs(ext, .{ ".scala", ".sc" })) { + break :blk Selected{ .lang = tree_sitter_scala(), .query_src = ts_queries.scala_highlights }; + } + } + if (syntax_minimal_grammars) { + if (extIs(ext, .{ ".zig", ".zon" })) { + break :blk Selected{ .lang = tree_sitter_zig(), .query_src = ts_queries.zig_highlights }; + } + } + return &.{}; + }; const styles = try gpa.alloc(u8, content.len); errdefer gpa.free(styles); @@ -258,12 +258,12 @@ pub fn highlightFile(gpa: std.mem.Allocator, path: []const u8, content: []const const parser = ts.Parser.create(); defer parser.destroy(); - parser.setLanguage(lang) catch return styles; + parser.setLanguage(selected.lang) catch return styles; const tree = parser.parseString(content, null) orelse return styles; defer tree.destroy(); var error_offset: u32 = 0; - const query = ts.Query.create(lang, query_src, &error_offset) catch return styles; + const query = ts.Query.create(selected.lang, selected.query_src, &error_offset) catch return styles; defer query.destroy(); const cursor = ts.QueryCursor.create(); defer cursor.destroy(); -- cgit v1.3