diff options
24 files changed, 1039 insertions, 25 deletions
@@ -331,6 +331,9 @@ pub fn build(b: *std.Build) void { // grammar without one gets none const objects = configureRead(b, io, b.root, "vendor/queries/" ++ g.name ++ "/textobjects.scm", 0x100000) catch ""; ts_queries.addOption([]const u8, g.name ++ "_textobjects", objects); + // and their indent queries, beside them + const indents = configureRead(b, io, b.root, "vendor/queries/" ++ g.name ++ "/indents.scm", 0x100000) catch ""; + ts_queries.addOption([]const u8, g.name ++ "_indents", indents); const lib = b.addLibrary(.{ .name = "tree-sitter-" ++ g.name, 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); } diff --git a/vendor/queries/README.md b/vendor/queries/README.md index 082f4e6b..2977e1cc 100644 --- a/vendor/queries/README.md +++ b/vendor/queries/README.md @@ -1,4 +1,4 @@ -# Textobject queries +# Textobject and indent queries `<grammar>/textobjects.scm` is helix's own query for that grammar, vendored from helix (https://github.com/helix-editor/helix) at 278b24389, the @@ -16,3 +16,16 @@ them), and erlang, whose helix query does not compile against the grammar version pardes builds. Those grammars have no textobjects, and the keys do nothing there, as in helix without a query. + +`<grammar>/indents.scm` is helix's indent query for that grammar, vendored +the same way from the same commit, for src/indent.zig: the indent a new line +takes on Enter, `o` and `O`. Vendored for bash, c, c_sharp, clojure, cpp +(with c's inlined), css, elixir, fortran, go, html, java, javascript (ecma's +inlined), json, kotlin, ocaml, php, python, ruby, rust, scala and zig. +`zig/indents.scm` is MODIFIED for this zig grammar's node names, and +`rust/indents.scm` writes its three `#match?` regexes with `^` where helix's +open with `\A`, which mvzr does not know; their headers say so. Not vendored: +ada, haskell, markdown, pascal, powershell and typst (helix has no indent +query for them), and erlang, whose query does not compile against the +grammar version pardes builds. A file of those keeps acme's autoindent, the +line's own indent as it is. diff --git a/vendor/queries/bash/indents.scm b/vendor/queries/bash/indents.scm new file mode 100644 index 00000000..5ca59a57 --- /dev/null +++ b/vendor/queries/bash/indents.scm @@ -0,0 +1,23 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/bash/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (function_definition) + (if_statement) + (for_statement) + (while_statement) + (case_statement) + (case_item) + (pipeline) +] @indent + +[ + "}" + "fi" + "elif" + "else" + "done" + "esac" +] @outdent diff --git a/vendor/queries/c/indents.scm b/vendor/queries/c/indents.scm new file mode 100644 index 00000000..0ed0b66d --- /dev/null +++ b/vendor/queries/c/indents.scm @@ -0,0 +1,58 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/c/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (compound_statement) + (declaration_list) + (field_declaration_list) + (enumerator_list) + (parameter_list) + (init_declarator) + (expression_statement) +] @indent + +[ + "case" + "default" + "}" + "]" + ")" +] @outdent + +; Single statement after if/while/for/do without braces. Capture the body child +; naturally; on a typed newline the engine descends into it (no wrapper trick). +(if_statement + consequence: (_) @indent + (#not-kind-eq? @indent "compound_statement") + (#set! "scope" "header")) +(while_statement + body: (_) @indent + (#not-kind-eq? @indent "compound_statement") + (#set! "scope" "header")) +(do_statement + body: (_) @indent + (#not-kind-eq? @indent "compound_statement") + (#set! "scope" "header")) +(for_statement + body: (_) @indent + (#not-kind-eq? @indent "compound_statement") + (#set! "scope" "header")) + +(parameter_list + . (parameter_declaration) @anchor + ) @align +(argument_list + . (_) @anchor + ) @align +; These are a bit opinionated since some people just indent binary/ternary expressions spanning multiple lines. +; Since they are only triggered when a newline is inserted into an already complete binary/ternary expression, +; this should happen rarely, so it is not a big deal either way. +; Additionally, adding these queries has the advantage of preventing such continuation lines from being used +; as the baseline when the `hybrid` indent heuristic is used (which is desirable since their indentation is so inconsistent). +(binary_expression + ) @anchor @align +(conditional_expression + "?" @anchor + ) @align diff --git a/vendor/queries/c_sharp/indents.scm b/vendor/queries/c_sharp/indents.scm new file mode 100644 index 00000000..bf875d4b --- /dev/null +++ b/vendor/queries/c_sharp/indents.scm @@ -0,0 +1,20 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/c-sharp/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (declaration_list) + (block) + (enum_member_declaration_list) + (accessor_list) + (switch_body) + (switch_section) + (initializer_expression) +] @indent + +[ + "}" + ")" + "]" +] @outdent diff --git a/vendor/queries/clojure/indents.scm b/vendor/queries/clojure/indents.scm new file mode 100644 index 00000000..5c232fc9 --- /dev/null +++ b/vendor/queries/clojure/indents.scm @@ -0,0 +1,65 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/clojure/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +; If a list has 2 elements on the first line, align to the second element. +; Exclude literals and special keywords that have different indentation rules. +(list_lit . (_) @first . (_) @anchor + (#same-line? @first @anchor) + + (#not-kind-eq? @first "bool_lit") + (#not-kind-eq? @first "nil_lit") + (#not-kind-eq? @first "str_lit") + (#not-kind-eq? @first "num_lit") + (#not-kind-eq? @first "kwd_lit") + (#not-match? @first "^(def|defn|defn-|defmacro|defmethod|defmulti|defonce|defprotocol|deftype|defrecord|defstruct|definline|definterface|deftest|ns|let|letfn|binding|loop|for|doseq|dotimes|when-let|if-let|when-some|if-some|with-.*|fn)$")) @align + +; If the first element in a list is also a list and on a line by itself, +; the outer list is aligned to it +(list_lit . (list_lit) @anchor . + ) @align + +(list_lit . (list_lit) @anchor . (_) @second + (#not-same-line? @anchor @second) + ) @align + +; If the first element in a list is not a list and on a line by itself, +; indent the list body by one level +(list_lit . (_) @first . + (#not-kind-eq? @first "bool_lit") + (#not-kind-eq? @first "nil_lit") + (#not-kind-eq? @first "str_lit") + (#not-kind-eq? @first "num_lit") + (#not-kind-eq? @first "kwd_lit") + (#not-match? @first "^(def|defn|defn-|defmacro|defmethod|defmulti|defonce|defprotocol|deftype|defrecord|defstruct|definline|definterface|deftest|ns|let|letfn|binding|loop|for|doseq|dotimes|when-let|if-let|when-some|if-some|with-.*|fn)$")) @indent + +(list_lit . (_) @first . (_) @second + (#not-same-line? @first @second) + (#not-kind-eq? @first "bool_lit") + (#not-kind-eq? @first "nil_lit") + (#not-kind-eq? @first "str_lit") + (#not-kind-eq? @first "num_lit") + (#not-kind-eq? @first "kwd_lit") + (#not-match? @first "^(def|defn|defn-|defmacro|defmethod|defmulti|defonce|defprotocol|deftype|defrecord|defstruct|definline|definterface|deftest|ns|let|letfn|binding|loop|for|doseq|dotimes|when-let|if-let|when-some|if-some|with-.*|fn)$")) @indent + +; If the first element is a literal, align the list to it +(list_lit . [(bool_lit) (nil_lit) (str_lit) (num_lit) (kwd_lit)] @anchor + ) @align + +; Special indentation for def-like forms, let bindings, and other special forms +; These forms typically have the body indented by one level after the name/bindings +(list_lit . (sym_lit) @first + (#match? @first "^(def|defn|defn-|defmacro|defmethod|defmulti|defonce|defprotocol|deftype|defrecord|defstruct|definline|definterface|deftest|ns|let|letfn|binding|loop|for|doseq|dotimes|when-let|if-let|when-some|if-some|with-.*|fn)$")) @indent + +; Align vector/map elements when first two are on same line (e.g., let bindings) +(vec_lit . (_) @anchor . (_) @second + (#same-line? @anchor @second) + ) @align + +(map_lit . (_) @anchor . (_) @second + (#same-line? @anchor @second) + ) @align + +; Indent vectors, maps, and sets +[(vec_lit) (map_lit) (set_lit)] @indent diff --git a/vendor/queries/cpp/indents.scm b/vendor/queries/cpp/indents.scm new file mode 100644 index 00000000..ea91ba4b --- /dev/null +++ b/vendor/queries/cpp/indents.scm @@ -0,0 +1,63 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/cpp/indents.scm, with what it inherits from c inlined. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (compound_statement) + (declaration_list) + (field_declaration_list) + (enumerator_list) + (parameter_list) + (init_declarator) + (expression_statement) +] @indent + +[ + "case" + "default" + "}" + "]" + ")" +] @outdent + +; Single statement after if/while/for/do without braces. Capture the body child +; naturally; on a typed newline the engine descends into it (no wrapper trick). +(if_statement + consequence: (_) @indent + (#not-kind-eq? @indent "compound_statement") + (#set! "scope" "header")) +(while_statement + body: (_) @indent + (#not-kind-eq? @indent "compound_statement") + (#set! "scope" "header")) +(do_statement + body: (_) @indent + (#not-kind-eq? @indent "compound_statement") + (#set! "scope" "header")) +(for_statement + body: (_) @indent + (#not-kind-eq? @indent "compound_statement") + (#set! "scope" "header")) + +(parameter_list + . (parameter_declaration) @anchor + ) @align +(argument_list + . (_) @anchor + ) @align +; These are a bit opinionated since some people just indent binary/ternary expressions spanning multiple lines. +; Since they are only triggered when a newline is inserted into an already complete binary/ternary expression, +; this should happen rarely, so it is not a big deal either way. +; Additionally, adding these queries has the advantage of preventing such continuation lines from being used +; as the baseline when the `hybrid` indent heuristic is used (which is desirable since their indentation is so inconsistent). +(binary_expression + ) @anchor @align +(conditional_expression + "?" @anchor + ) @align + + +(access_specifier) @outdent + +(raw_string_literal) @opaque diff --git a/vendor/queries/css/indents.scm b/vendor/queries/css/indents.scm new file mode 100644 index 00000000..e42c3be0 --- /dev/null +++ b/vendor/queries/css/indents.scm @@ -0,0 +1,12 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/css/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (block) +] @indent + +[ + "}" +] @outdent diff --git a/vendor/queries/elixir/indents.scm b/vendor/queries/elixir/indents.scm new file mode 100644 index 00000000..cc535930 --- /dev/null +++ b/vendor/queries/elixir/indents.scm @@ -0,0 +1,27 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/elixir/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (anonymous_function) + (do_block) + (stab_clause) + (map) + (list) + (tuple) +] @indent + +; The else/rescue/catch/after clause blocks nest *inside* the do_block, which +; already provides the body's one level of indent; the keyword itself outdents +; back to the construct's level. +[ + "end" + "else" + "rescue" + "catch" + "after" + "]" + "}" + ")" +] @outdent diff --git a/vendor/queries/fortran/indents.scm b/vendor/queries/fortran/indents.scm new file mode 100644 index 00000000..073d4f32 --- /dev/null +++ b/vendor/queries/fortran/indents.scm @@ -0,0 +1,36 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/fortran/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (module) + (submodule) + (program) + (subroutine) + (module_procedure) + (function) + ; (interface) + (if_statement) + (do_loop) + (where_statement) + (derived_type_definition) + (enum) +] @indent + +[ + (end_module_statement) + (end_submodule_statement) + (end_program_statement) + (end_subroutine_statement) + (end_module_procedure_statement) + (end_function_statement) + ; (end_interface_statement) + (end_if_statement) + (end_do_loop_statement) + (else_clause) + (elseif_clause) + (end_type_statement) + (end_enum_statement) + (end_where_statement) +] @outdent diff --git a/vendor/queries/go/indents.scm b/vendor/queries/go/indents.scm new file mode 100644 index 00000000..3734f458 --- /dev/null +++ b/vendor/queries/go/indents.scm @@ -0,0 +1,55 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/go/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (import_declaration) + (const_declaration) + (type_declaration) + (type_spec) + (func_literal) + (literal_value) + (literal_element) + (keyed_element) + (expression_case) + (default_case) + (type_case) + (communication_case) + (argument_list) + (parameter_list) + (field_declaration_list) + (block) + (var_declaration) + (selector_expression) + (binary_expression) +] @indent + +[ + "]" + ")" +] @outdent + +; Switches and selects aren't indented, only their case bodies are. +; Outdent all closing braces except those closing switches or selects. +( + (_ "}" @outdent) @outer + (#not-kind-eq? @outer "select_statement") + (#not-kind-eq? @outer "type_switch_statement") + (#not-kind-eq? @outer "expression_switch_statement") +) + +; Handle ERROR nodes for when auto-pairs is disabled. +; Typing an opening delimiter without a closing one produces an ERROR node. +(ERROR "{") @indent @extend +(ERROR "(") @indent +(ERROR "[") @indent + +; Labels (`Loop:`, `done:`) are de-indented one level by gofmt. +; Capturing the label_name (a child, not an ancestor of the labeled body) +; outdents only the label line. +(labeled_statement + (label_name) @outdent) + +; Raw string literals (backticks) span lines and are literal content. +(raw_string_literal) @opaque diff --git a/vendor/queries/html/indents.scm b/vendor/queries/html/indents.scm new file mode 100644 index 00000000..ea83fddd --- /dev/null +++ b/vendor/queries/html/indents.scm @@ -0,0 +1,20 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/html/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +; Indent the contents of an element; pull the end tag — and the close of a +; multi-line start/self-closing tag — back to the element's column. Mirrors the +; jsx rules. Void elements (`<br>`, `<img …>`) are single-line `element`s, so the +; tail-scoped indent is a no-op for them. +[ + (element) + (script_element) + (style_element) +] @indent +(end_tag) @outdent +(start_tag ">" @outdent) +(self_closing_tag "/>" @outdent) + +; <script>/<style> bodies are embedded JS/CSS, handled by injection-aware indent +; (the engine indents them with the JS/CSS query). No @opaque needed. diff --git a/vendor/queries/java/indents.scm b/vendor/queries/java/indents.scm new file mode 100644 index 00000000..ad33accc --- /dev/null +++ b/vendor/queries/java/indents.scm @@ -0,0 +1,53 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/java/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (class_body) + (enum_body) + (interface_body) + (constructor_body) + (annotation_type_body) + (module_body) + (block) + (switch_block) + ; statements after a `case`/`default` label (the group also holds the label, + ; so the default tail scope indents only the body lines, not the label) + (switch_block_statement_group) + (array_initializer) + (argument_list) + (formal_parameters) + (annotation_argument_list) + (element_value_array_initializer) +] @indent + +[ + "}" + ")" + "]" +] @outdent + +; Single statement after if/while/for without braces. Capture the body child +; naturally; on a typed newline the engine descends into it. +(if_statement + consequence: (_) @indent + (#not-kind-eq? @indent "block") + (#set! "scope" "header")) +; Braceless `else` body (the body is the alternative field). Skip `else if` +; (alternative is an if_statement) and braced bodies. +(if_statement + alternative: (_) @indent + (#not-kind-eq? @indent "block") + (#not-kind-eq? @indent "if_statement") + (#set! "scope" "header")) +(while_statement + body: (_) @indent + (#not-kind-eq? @indent "block") + (#set! "scope" "header")) +(for_statement + body: (_) @indent + (#not-kind-eq? @indent "block") + (#set! "scope" "header")) + +(string_literal) @opaque diff --git a/vendor/queries/javascript/indents.scm b/vendor/queries/javascript/indents.scm new file mode 100644 index 00000000..ec0512ca --- /dev/null +++ b/vendor/queries/javascript/indents.scm @@ -0,0 +1,72 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/javascript/indents.scm, with what it inherits from ecma inlined (_javascript has none). +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (array) + (object) + (arguments) + (formal_parameters) + + (statement_block) + (switch_statement) + (object_pattern) + (class_body) + (named_imports) + + (binary_expression) + (return_statement) + (template_substitution) + (export_clause) + + (member_expression) + (call_expression) +] @indent + +[ + (switch_case) + (switch_default) +] @indent + +[ + "}" + "]" + ")" +] @outdent + +; Single-statement bodies without braces (if/else/while/for/do). Indent the +; statement node so a newline typed after the header indents — capturing the +; body child alone only reindents existing lines, since the typing-direction +; walk reaches the statement, not its body child. The else-bearing if keeps the +; consequence-child form (scope all) so the `else` line itself isn't indented; +; a braceless else/do body is handled via its own node for the same reason. +(if_statement + consequence: (_) @_body + (#not-kind-eq? @_body "statement_block") + !alternative) @indent +(if_statement + consequence: (_) @indent + (#not-kind-eq? @indent "statement_block") + alternative: (_) + (#set! "scope" "header")) +(else_clause + (_) @_body + (#not-kind-eq? @_body "statement_block") + (#not-kind-eq? @_body "if_statement")) @indent +(while_statement + body: (_) @_body + (#not-kind-eq? @_body "statement_block")) @indent +(for_statement + body: (_) @_body + (#not-kind-eq? @_body "statement_block")) @indent +(for_in_statement + body: (_) @_body + (#not-kind-eq? @_body "statement_block")) @indent +(do_statement + body: (_) @indent + (#not-kind-eq? @indent "statement_block") + (#set! "scope" "header")) + +; Template-literal bodies are literal content (between interpolations). +(template_string) @opaque diff --git a/vendor/queries/json/indents.scm b/vendor/queries/json/indents.scm new file mode 100644 index 00000000..3fb740c4 --- /dev/null +++ b/vendor/queries/json/indents.scm @@ -0,0 +1,14 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/json/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (object) + (array) +] @indent + +[ + "]" + "}" +] @outdent diff --git a/vendor/queries/kotlin/indents.scm b/vendor/queries/kotlin/indents.scm new file mode 100644 index 00000000..264258c2 --- /dev/null +++ b/vendor/queries/kotlin/indents.scm @@ -0,0 +1,51 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/kotlin/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (class_body) + (enum_class_body) + (lambda_literal) + + ; _block is hidden in the grammar, so list all public wrappers explicitly. + (function_body) + (anonymous_initializer) + (control_structure_body) + (secondary_constructor) + (try_expression) + (catch_block) + (finally_block) + + (property_declaration) + (assignment) + + (when_expression) + (call_expression) + (if_expression) + + ; Binary expressions + (multiplicative_expression) + (additive_expression) + (range_expression) + (infix_expression) + (elvis_expression) + (check_expression) + (comparison_expression) + (equality_expression) + (comparison_expression) + (equality_expression) + (conjunction_expression) + (disjunction_expression) + + (call_suffix) + (function_value_parameters) +] @indent + +[ + "}" + ")" + "]" +] @outdent + +(string_literal) @opaque diff --git a/vendor/queries/ocaml/indents.scm b/vendor/queries/ocaml/indents.scm new file mode 100644 index 00000000..6f4a13d9 --- /dev/null +++ b/vendor/queries/ocaml/indents.scm @@ -0,0 +1,27 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/ocaml/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (let_binding) + (type_binding) + (structure) + (signature) + (record_declaration) + (function_expression) + (fun_expression) + (match_case) + (then_clause) + (else_clause) +] @indent + +; `else if` chains nest else_clause -> if_expression -> else_clause, which would +; double-indent the trailing branch; `try`/`with` and explicit module `end` +; alignment are likewise left to the writer — see tests/indent/ocaml.ml. +[ + "}" + ")" + "]" + "end" +] @outdent diff --git a/vendor/queries/php/indents.scm b/vendor/queries/php/indents.scm new file mode 100644 index 00000000..48a28973 --- /dev/null +++ b/vendor/queries/php/indents.scm @@ -0,0 +1,34 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/php/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (array_creation_expression) + (arguments) + (formal_parameters) + (compound_statement) + (declaration_list) + (binary_expression) + (return_statement) + (expression_statement) + (switch_block) + ; statements after a `case`/`default` label (the node also holds the label, + ; so the default tail scope indents only the body lines, not the label) + (case_statement) + (default_statement) + (anonymous_function_use_clause) + (property_hook_list) +] @indent + +[ + "}" + ")" + "]" +] @outdent + +; Heredoc / nowdoc bodies are literal content. +[ + (heredoc) + (nowdoc) +] @opaque diff --git a/vendor/queries/python/indents.scm b/vendor/queries/python/indents.scm new file mode 100644 index 00000000..6e49c2fd --- /dev/null +++ b/vendor/queries/python/indents.scm @@ -0,0 +1,98 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/python/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (list) + (tuple) + (dictionary) + (set) + + (if_statement) + (for_statement) + (while_statement) + (with_statement) + (try_statement) + (match_statement) + (case_clause) + (import_from_statement) + + (parenthesized_expression) + (generator_expression) + (list_comprehension) + (set_comprehension) + (dictionary_comprehension) + + (tuple_pattern) + (list_pattern) + (argument_list) + (parameters) + (binary_operator) + + (function_definition) + (class_definition) +] @indent + +; Workaround for the tree-sitter grammar creating large errors when a +; try_statement is missing the except/finally clause +(ERROR + "try" + . + ":" @indent @extend) +(ERROR + . + "def") @indent @extend +(ERROR + (block) @indent @extend + ) + +[ + (if_statement) + (for_statement) + (while_statement) + (with_statement) + (try_statement) + (match_statement) + (case_clause) + + (function_definition) + (class_definition) + + (except_clause) + (finally_clause) +] @extend + +[ + (return_statement) + (break_statement) + (continue_statement) + (raise_statement) + (pass_statement) +] @extend.prevent-once + +[ + ")" + "]" + "}" +] @outdent +(elif_clause + "elif" @outdent) +(else_clause + "else" @outdent) +(except_clause + "except" @outdent) +(finally_clause + "finally" @outdent) + +(parameters + . + (identifier) @anchor + ) @align +(argument_list + . + (_) @anchor + ) @align + +; String bodies (triple-quoted strings span lines) are literal content. +(string) @opaque diff --git a/vendor/queries/ruby/indents.scm b/vendor/queries/ruby/indents.scm new file mode 100644 index 00000000..1100aef9 --- /dev/null +++ b/vendor/queries/ruby/indents.scm @@ -0,0 +1,32 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/ruby/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (argument_list) + (array) + (begin) + (block) + (call) + (class) + (case) + (if) + (hash) + (method) + (module) + (singleton_class) + (singleton_method) +] @indent + +[ + ")" + "}" + "]" + "end" + "when" + "else" + "elsif" + "rescue" + "ensure" +] @outdent diff --git a/vendor/queries/rust/indents.scm b/vendor/queries/rust/indents.scm new file mode 100644 index 00000000..0d63f197 --- /dev/null +++ b/vendor/queries/rust/indents.scm @@ -0,0 +1,166 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/rust/indents.scm, MODIFIED: its three #match? patterns +; open with \A, which mvzr (src/regexp.zig) reads as a letter A; written +; with ^, which a predicate here matches at the node's start alone. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (use_list) + (block) + (match_block) + (arguments) + (parameters) + (declaration_list) + (field_declaration_list) + (field_initializer_list) + (struct_pattern) + (tuple_pattern) + (unit_expression) + (enum_variant_list) + (call_expression) + (binary_expression) + (field_expression) + (await_expression) + (tuple_expression) + (array_expression) + (where_clause) + (type_cast_expression) + + (token_tree) + (macro_definition) + (token_tree_pattern) + (token_repetition) +] @indent + +[ + "}" + "]" + ")" +] @outdent + +; Indent the right side of assignments. +; The #not-same-line? predicate is required to prevent an extra indent for e.g. +; an else-clause where the previous if-clause starts on the same line as the assignment. +(assignment_expression + . + (_) @expr-start + right: (_) @indent + (#not-same-line? @indent @expr-start) + +) +(compound_assignment_expr + . + (_) @expr-start + right: (_) @indent + (#not-same-line? @indent @expr-start) + +) +(let_declaration + "let" @expr-start + value: (_) @indent + alternative: (_)? @indent + (#not-same-line? @indent @expr-start) + +) +(let_condition + . + (_) @expr-start + value: (_) @indent + (#not-same-line? @indent @expr-start) + +) +(if_expression + . + (_) @expr-start + condition: (_) @indent + (#not-same-line? @indent @expr-start) + +) +(static_item + . + (_) @expr-start + value: (_) @indent + (#not-same-line? @indent @expr-start) + +) +(field_pattern + . + (_) @expr-start + pattern: (_) @indent + (#not-same-line? @indent @expr-start) + +) +; Indent type aliases that span multiple lines, similar to +; regular assignment expressions +(type_item + . + (_) @expr-start + type: (_) @indent + (#not-same-line? @indent @expr-start) + +) + +; Some field expressions where the left part is a multiline expression are not +; indented by cargo fmt. +; Because this multiline expression might be nested in an arbitrary number of +; field expressions, this can only be matched using a Regex. +(field_expression + value: (_) @val + "." @outdent + ; Check whether the first line ends with `(`, `{` or `[` (up to whitespace). + (#match? @val "^[^\\n\\r]+(\\(|\\{|\\[)[\\t ]*(\\n|\\r)") +) +; Same as above, but with an additional `call_expression`. This is required since otherwise +; the arguments of the function call won't be outdented. +(call_expression + function: (field_expression + value: (_) @val + "." @outdent + (#match? @val "^[^\\n\\r]+(\\(|\\{|\\[)[\\t ]*(\\n|\\r)") + ) + arguments: (_) @outdent +) +; Same again for `.await` off a multiline receiver, so it lines up with the +; other links in the chain instead of indenting a level deeper. +(await_expression + (_) @val + "." @outdent + (#match? @val "^[^\\n\\r]+(\\(|\\{|\\[)[\\t ]*(\\n|\\r)") +) + +; Indent if guards in patterns. +; Since the tree-sitter grammar doesn't create a node for the if expression, +; it's not possible to do this correctly in all cases. Indenting the tail of the +; whole pattern whenever it contains an `if` only fails if the `if` appears after +; the second line of the pattern (which should only rarely be the case) +(match_pattern + . + (_) @expr-start + "if" @pattern-guard + (#not-same-line? @expr-start @pattern-guard) +) @indent + +; Align closure parameters if they span more than one line +(closure_parameters + "|" + . + (_) @anchor + (_) @expr-end + . + (#not-same-line? @anchor @expr-end) +) @align + +(for_expression + "in" @in + . + (_) @indent + (#not-same-line? @in @indent) + +) + +; Multi-line string / raw-string bodies are literal content: preserve them. +[ + (string_literal) + (raw_string_literal) +] @opaque diff --git a/vendor/queries/scala/indents.scm b/vendor/queries/scala/indents.scm new file mode 100644 index 00000000..0583c186 --- /dev/null +++ b/vendor/queries/scala/indents.scm @@ -0,0 +1,29 @@ +; Vendored from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/scala/indents.scm. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (block) + (arguments) + (parameter) + (class_definition) + (trait_definition) + (object_definition) + (function_definition) + (val_definition) + (import_declaration) + (while_expression) + (do_while_expression) + (for_expression) + (try_expression) + (match_expression) +] @indent + +(ERROR "for") @indent + +[ + "}" + "]" + ")" +] @outdent diff --git a/vendor/queries/zig/indents.scm b/vendor/queries/zig/indents.scm new file mode 100644 index 00000000..2173e029 --- /dev/null +++ b/vendor/queries/zig/indents.scm @@ -0,0 +1,22 @@ +; Adapted from helix (https://github.com/helix-editor/helix) at 278b24389: +; runtime/queries/zig/indents.scm, MODIFIED for the zig grammar pardes +; builds (maxxnino/tree-sitter-zig), whose node names differ from the +; tree-sitter-grammars one helix uses: block is Block, switch_expression +; SwitchExpr, initializer_list InitList, error_set_declaration ErrorSetDecl, +; and struct/enum/union_declaration are one ContainerDecl. +; Licensed under the Mozilla Public License 2.0 (vendor/queries/helix.LICENSE, +; https://mozilla.org/MPL/2.0/). + +[ + (Block) + (SwitchExpr) + (InitList) + (ContainerDecl) + (ErrorSetDecl) +] @indent + +[ + "}" + "]" + ")" +] @outdent |
