| 1 | ; https://tree-sitter.github.io/tree-sitter/4-code-navigation.html |
| 2 | ; |
| 3 | ; The Nix data model is "everything is an attrset of expressions"; |
| 4 | ; functions and values share the same binding form. We capture |
| 5 | ; definitions broadly and mark function-valued bindings as |
| 6 | ; @definition.function specifically so code navigation can distinguish them. |
| 7 | |
| 8 | ; ----- Definitions ----- |
| 9 | |
| 10 | ; Generic binding - any top-level or nested attrset attribute. |
| 11 | ; Anchor @name to the leaf of the attrpath so `foo.bar.baz = ...` |
| 12 | ; tags `baz` rather than the whole dotted path. |
| 13 | (binding |
| 14 | attrpath: (attrpath |
| 15 | attr: (identifier) @name .) |
| 16 | expression: (_)) @definition.constant |
| 17 | |
| 18 | ; Function-valued bindings - `foo = x: ...`. |
| 19 | (binding |
| 20 | attrpath: (attrpath |
| 21 | attr: (identifier) @name .) |
| 22 | expression: (function_expression)) @definition.function |
| 23 | |
| 24 | ; inherit - definitions brought from another scope. |
| 25 | (inherit |
| 26 | attrs: (inherited_attrs attr: (identifier) @name)) @definition.constant |
| 27 | |
| 28 | (inherit_from |
| 29 | attrs: (inherited_attrs attr: (identifier) @name)) @definition.constant |
| 30 | |
| 31 | ; ----- References ----- |
| 32 | |
| 33 | ; Any bare identifier used as a value expression is a reference. |
| 34 | (variable_expression |
| 35 | name: (identifier) @name) @reference.constant |
| 36 | |
| 37 | ; Function application - the thing being called is a reference.call. |
| 38 | (apply_expression |
| 39 | function: (variable_expression |
| 40 | name: (identifier) @name)) @reference.call |
| 41 | |
| 42 | ; Method-style call: `foo.bar.baz arg` - tag the leaf attrname. |
| 43 | (apply_expression |
| 44 | function: (select_expression |
| 45 | attrpath: (attrpath |
| 46 | attr: (identifier) @name .))) @reference.call |
| 47 | |
| 48 | ; Attribute access (non-call) - `foo.bar.baz` lookup. |
| 49 | (select_expression |
| 50 | attrpath: (attrpath |
| 51 | attr: (identifier) @name .)) @reference.constant |