| 1 | # Adding Rainbow Bracket Queries |
| 2 | |
| 3 | Helix uses `rainbows.scm` tree-sitter query files to provide rainbow bracket |
| 4 | functionality. |
| 5 | |
| 6 | Tree-sitter queries are documented in the tree-sitter online documentation. |
| 7 | If you're writing queries for the first time, be sure to check out the section |
| 8 | on [syntax highlighting queries] and on [query syntax]. |
| 9 | |
| 10 | Rainbow queries have two captures: `@rainbow.scope` and `@rainbow.bracket`. |
| 11 | `@rainbow.scope` should capture any node that increases the nesting level |
| 12 | while `@rainbow.bracket` should capture any bracket nodes. Put another way: |
| 13 | `@rainbow.scope` switches to the next rainbow color for all nodes in the tree |
| 14 | under it while `@rainbow.bracket` paints captured nodes with the current |
| 15 | rainbow color. |
| 16 | |
| 17 | For an example, let's add rainbow queries for the tree-sitter query (TSQ) |
| 18 | language itself. These queries will go into a |
| 19 | `runtime/queries/tsq/rainbows.scm` file in the repository root. |
| 20 | |
| 21 | First we'll add the `@rainbow.bracket` captures. TSQ only has parentheses and |
| 22 | square brackets: |
| 23 | |
| 24 | ```tsq |
| 25 | ["(" ")" "[" "]"] @rainbow.bracket |
| 26 | ``` |
| 27 | |
| 28 | The ordering of the nodes within the alternation (square brackets) is not |
| 29 | taken into consideration. |
| 30 | |
| 31 | Angle brackets need care: `<` and `>` are usually comparison operators too, so |
| 32 | capturing them globally would rainbow-colour every comparison. Capture them only |
| 33 | within the generic node instead, which also marks that node as a scope: |
| 34 | |
| 35 | ```tsq |
| 36 | (type_arguments ["<" ">"] @rainbow.bracket) @rainbow.scope |
| 37 | ``` |
| 38 | |
| 39 | > Note: Why are these nodes quoted? Most syntax highlights capture text |
| 40 | > surrounded by parentheses. These are _named nodes_ and correspond to the |
| 41 | > names of rules in the grammar. Brackets are usually written in tree-sitter |
| 42 | > grammars as literal strings, for example: |
| 43 | > |
| 44 | > ```js |
| 45 | > { |
| 46 | > // ... |
| 47 | > arguments: $ => seq("(", repeat($.argument), ")"), |
| 48 | > // ... |
| 49 | > } |
| 50 | > ``` |
| 51 | > |
| 52 | > Nodes written as literal strings in tree-sitter grammars may be captured |
| 53 | > in queries with those same literal strings. |
| 54 | |
| 55 | Then we'll add `@rainbow.scope` captures. The easiest way to do this is to |
| 56 | view the `grammar.js` file in the tree-sitter grammar's repository. For TSQ, |
| 57 | that file is [here][tsq grammar.js]. As we scroll down the `grammar.js`, we |
| 58 | see that the `(alternation)`, (L36) `(group)` (L57), `(named_node)` (L59), |
| 59 | `(predicate)` (L87) and `(wildcard_node)` (L97) nodes all contain literal |
| 60 | parentheses or square brackets in their definitions. These nodes are all |
| 61 | direct parents of brackets and happen to also be the nodes we want to change |
| 62 | to the next rainbow color, so we capture them as `@rainbow.scope`. |
| 63 | |
| 64 | ```tsq |
| 65 | [ |
| 66 | (group) |
| 67 | (named_node) |
| 68 | (wildcard_node) |
| 69 | (predicate) |
| 70 | (alternation) |
| 71 | ] @rainbow.scope |
| 72 | ``` |
| 73 | |
| 74 | This strategy works as a rule of thumb for most programming and configuration |
| 75 | languages. Markup languages can be trickier and may take additional |
| 76 | experimentation to find the correct nodes to use for scopes and brackets. |
| 77 | |
| 78 | The `:tree-sitter-subtree` command shows the syntax tree under the primary |
| 79 | selection in S-expression format and can be a useful tool for determining how |
| 80 | to write a query. |
| 81 | |
| 82 | ### Properties |
| 83 | |
| 84 | The `rainbow.include-children` property may be applied to `@rainbow.scope` |
| 85 | captures. By default, all `@rainbow.bracket` captures must be direct descendant |
| 86 | of a node captured with `@rainbow.scope` in a syntax tree in order to be |
| 87 | highlighted. The `rainbow.include-children` property disables that check and |
| 88 | allows `@rainbow.bracket` captures to be highlighted if they are direct or |
| 89 | indirect descendants of some node captured with `@rainbow.scope`. |
| 90 | |
| 91 | For example, this property is used in the HTML rainbow queries. |
| 92 | |
| 93 | For a document like `<a>link</a>`, the syntax tree is: |
| 94 | |
| 95 | ```tsq |
| 96 | (element ; <a>link</a> |
| 97 | (start_tag ; <a> |
| 98 | (tag_name)) ; a |
| 99 | (text) ; link |
| 100 | (end_tag ; </a> |
| 101 | (tag_name))) ; a |
| 102 | ``` |
| 103 | |
| 104 | If we want to highlight the `<`, `>` and `</` nodes with rainbow colors, we |
| 105 | capture them as `@rainbow.bracket`: |
| 106 | |
| 107 | ```tsq |
| 108 | ["<" ">" "</"] @rainbow.bracket |
| 109 | ``` |
| 110 | |
| 111 | And we capture `(element)` as `@rainbow.scope` because `(element)` nodes nest |
| 112 | within each other: they increment the nesting level and switch to the next |
| 113 | color in the rainbow. |
| 114 | |
| 115 | ```tsq |
| 116 | (element) @rainbow.scope |
| 117 | ``` |
| 118 | |
| 119 | But this combination of `@rainbow.scope` and `@rainbow.bracket` will not |
| 120 | highlight any nodes. `<`, `>` and `</` are children of the `(start_tag)` and |
| 121 | `(end_tag)` nodes. We can't capture `(start_tag)` and `(end_tag)` as |
| 122 | `@rainbow.scope` because they don't nest other elements. We can fix this case |
| 123 | by removing the requirement that `<`, `>` and `</` are direct descendants of |
| 124 | `(element)` using the `rainbow.include-children` property. |
| 125 | |
| 126 | ```tsq |
| 127 | ((element) @rainbow.scope |
| 128 | (#set! rainbow.include-children)) |
| 129 | ``` |
| 130 | |
| 131 | With this property set, `<`, `>`, and `</` will highlight with rainbow colors |
| 132 | even though they aren't direct descendents of the `(element)` node. |
| 133 | |
| 134 | `rainbow.include-children` is not necessary for the vast majority of programming |
| 135 | languages. It is only necessary when the node that increments the nesting level |
| 136 | (changes rainbow color) is not the direct parent of the bracket node. |
| 137 | |
| 138 | [syntax highlighting queries]: https://tree-sitter.github.io/tree-sitter/syntax-highlighting#highlights |
| 139 | [query syntax]: https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries |
| 140 | [tsq grammar.js]: https://github.com/the-mikedavis/tree-sitter-tsq/blob/48b5e9f82ae0a4727201626f33a17f69f8e0ff86/grammar.js |