| 1 | ; Indent query for Nix. |
| 2 | ; |
| 3 | ; Nix is an expression language with no statement blocks. Indentation comes from |
| 4 | ; the bracketed scopes ({…} attrsets, […] lists, (…) groups, {…} parameter |
| 5 | ; sets), the `let … in` and `if … then … else` forms, multi-line function |
| 6 | ; application, and the continuation of a binding whose value spills onto a |
| 7 | ; following line. |
| 8 | |
| 9 | ; One level per bracketed scope; the matching close token dedents. |
| 10 | [ |
| 11 | (attrset_expression) |
| 12 | (rec_attrset_expression) |
| 13 | (let_attrset_expression) |
| 14 | (list_expression) |
| 15 | (parenthesized_expression) |
| 16 | (formals) |
| 17 | ] @indent |
| 18 | |
| 19 | [ |
| 20 | "}" |
| 21 | ")" |
| 22 | "]" |
| 23 | ] @outdent |
| 24 | |
| 25 | ; A binding value carried onto the line(s) after `=`. It shares the `=` line |
| 26 | ; with a same-line bracket, so `x = { … }` is not indented twice. |
| 27 | (binding) @indent |
| 28 | |
| 29 | ; `let … in`: indent everything inside the `let`, then pull `in` and the body |
| 30 | ; back so only the bindings stay indented. Indenting the whole `let_expression` |
| 31 | ; (rather than its `binding_set`) also gives the right indent when a newline is |
| 32 | ; typed straight after `let`, before any binding exists — the cursor is still |
| 33 | ; inside `let_expression`, whereas the `binding_set` does not yet exist. |
| 34 | (let_expression) @indent |
| 35 | (let_expression "in" @outdent) |
| 36 | (let_expression body: (_) @outdent) |
| 37 | |
| 38 | ; `if … then … else`: indent each branch. scope "all" covers a branch written on |
| 39 | ; its own line. An `else if` is skipped — the nested `if` indents its own |
| 40 | ; branches, so the chain stays flat instead of stair-stepping. |
| 41 | (if_expression |
| 42 | consequence: (_) @indent |
| 43 | (#set! "scope" "header")) |
| 44 | (if_expression |
| 45 | alternative: (_) @indent |
| 46 | (#not-kind-eq? @indent "if_expression") |
| 47 | (#set! "scope" "header")) |
| 48 | |
| 49 | ; Function application: arguments carried onto following lines. Nested |
| 50 | ; applications share a line, so they collapse to a single level. |
| 51 | (apply_expression) @indent |
| 52 | |
| 53 | ; Indented strings are literal text (often embedded scripts); preserve their |
| 54 | ; interior verbatim rather than reflowing it as code. |
| 55 | (indented_string_expression) @opaque |