| 1 | ; Variable scopes, definitions and references for Nix. |
| 2 | ; |
| 3 | ; Nix `let` / `rec` bindings are recursive and lazily evaluated, so a reference |
| 4 | ; can resolve to a binding that appears later in source order. tree-sitter's |
| 5 | ; locals resolver is order-sensitive, so a forward reference simply keeps its |
| 6 | ; base `@variable` highlight instead of the definition's class — a cosmetic |
| 7 | ; limitation, never a miscolour. |
| 8 | |
| 9 | ; Scopes |
| 10 | |
| 11 | [ |
| 12 | (function_expression) |
| 13 | (let_expression) |
| 14 | (rec_attrset_expression) |
| 15 | ] @local.scope |
| 16 | |
| 17 | ; Definitions |
| 18 | |
| 19 | (formal |
| 20 | name: (identifier) @local.definition.variable.parameter) |
| 21 | |
| 22 | ; `@args:` binds the whole argument set. |
| 23 | (function_expression |
| 24 | universal: (identifier) @local.definition.variable.parameter) |
| 25 | |
| 26 | ; `let name = ...;` bindings. Dotted attrpaths aren't plain locals, so restrict |
| 27 | ; to a single-attr path. |
| 28 | (let_expression |
| 29 | (binding_set |
| 30 | (binding |
| 31 | attrpath: (attrpath |
| 32 | attr: (identifier) @local.definition.variable) .))) |
| 33 | |
| 34 | ; References |
| 35 | |
| 36 | ; Only `variable_expression` names are variable references; a bare `identifier` |
| 37 | ; elsewhere (attrpath members, binding names) is attribute access, not a local. |
| 38 | (variable_expression |
| 39 | name: (identifier) @local.reference) |
| 40 | |
| 41 | ; Discard: a variable in call position (`f x`) is highlighted `@function` by |
| 42 | ; highlights.scm. Cancel its reference resolution here so locals doesn't recolour |
| 43 | ; it as a plain variable. |
| 44 | (apply_expression |
| 45 | function: (variable_expression |
| 46 | name: (identifier) @_)) |