| 1 | ## Adding Locals Queries |
| 2 | |
| 3 | `locals.scm` queries teach Helix about variable scopes and definitions so that |
| 4 | local variables can be highlighted distinctly from global ones. When a |
| 5 | `@local.reference` identifier is resolved to a `@local.definition`, it inherits |
| 6 | the highlight class of that definition rather than the class assigned by |
| 7 | `highlights.scm`. |
| 8 | |
| 9 | Query files should be placed in `runtime/queries/{language}/locals.scm` when |
| 10 | contributing to Helix. Like other query files, `locals.scm` may reuse another |
| 11 | language's with `; inherits: <lang>` on the first line. Locals are resolved in |
| 12 | their own pass with their own precedence, independently of `highlights.scm`. |
| 13 | |
| 14 | ## Captures |
| 15 | |
| 16 | ### `@local.scope` |
| 17 | |
| 18 | Marks a node as a scope boundary. Definitions are only visible to references |
| 19 | that appear inside the same scope or a nested one. Typical scope nodes are |
| 20 | function bodies and blocks. |
| 21 | |
| 22 | ```scm |
| 23 | [ |
| 24 | (function_definition) |
| 25 | (block) |
| 26 | ] @local.scope |
| 27 | ``` |
| 28 | |
| 29 | ### `@local.definition.*` |
| 30 | |
| 31 | Marks a name node as introducing a local symbol. The suffix after |
| 32 | `@local.definition.` becomes the highlight class applied to any reference that |
| 33 | resolves to this definition. For example, `@local.definition.variable.parameter` |
| 34 | causes matching references to be highlighted as `variable.parameter`. |
| 35 | |
| 36 | ```scm |
| 37 | (function_item |
| 38 | (parameters |
| 39 | (parameter |
| 40 | pattern: (identifier) @local.definition.variable.parameter))) |
| 41 | ``` |
| 42 | |
| 43 | Common suffixes mirror the highlight classes in `highlights.scm`: |
| 44 | `variable`, `variable.parameter`, `variable.builtin`, `variable.mutable`, |
| 45 | `function`, `namespace`, `type`, `constant`, etc. |
| 46 | |
| 47 | ### `@local.reference` |
| 48 | |
| 49 | Marks an identifier node as a potential reference to a local definition. |
| 50 | Helix searches enclosing scopes for a matching definition and, if found, |
| 51 | highlights the reference with that definition's class instead. |
| 52 | |
| 53 | ```scm |
| 54 | (identifier) @local.reference |
| 55 | ``` |
| 56 | |
| 57 | ## Discard captures |
| 58 | |
| 59 | Any capture in `locals.scm` that is not `@local.scope`, `@local.reference`, or |
| 60 | `@local.definition.*` acts as a discard. It prevents a `@local.reference` from |
| 61 | being resolved at that node without affecting the `highlights.scm` result. This |
| 62 | is useful for identifier nodes that look like references but should not be |
| 63 | treated as variable references, for example keyword argument names or struct |
| 64 | field names in struct literals. |
| 65 | |
| 66 | ```scm |
| 67 | ; Keyword argument names in a call are not variable references. |
| 68 | (keyword_argument |
| 69 | name: (identifier) @_) |
| 70 | ``` |
| 71 | |
| 72 | Later patterns in a query file have higher precedence than earlier ones. A |
| 73 | discard pattern must appear after the `@local.reference` pattern it is intended |
| 74 | to override. Placing it later ensures it takes precedence and cancels the |
| 75 | reference resolution for nodes it matches. |
| 76 | |
| 77 | The convention is to use a capture name beginning with an underscore (e.g. `@_`, |
| 78 | `@_keyword`) to make the discard intent clear, but any non-`@local.*` name works. |
| 79 | |
| 80 | ## How definitions and references are matched |
| 81 | |
| 82 | Helix matches references to definitions by comparing the text of the reference |
| 83 | node against the text of definitions visible from the current scope. Definitions |
| 84 | in inner scopes shadow those in outer scopes. If no definition is found the |
| 85 | reference is left with its `highlights.scm` highlight. |
| 86 | |
| 87 | ## Relationship with `highlights.scm` |
| 88 | |
| 89 | The locals system runs alongside `highlights.scm`, not instead of it. |
| 90 | `highlights.scm` always determines the baseline highlight for a node. |
| 91 | `locals.scm` can override that highlight only when a `@local.reference` |
| 92 | successfully resolves to a `@local.definition`, and only for that specific |
| 93 | resolution. Non-`@local.*` captures in `locals.scm` (i.e. discards) have no |
| 94 | effect on `highlights.scm` results. |