<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>book/src/guides/rainbow_bracket_queries.md - fork/helix @ master</title>
<meta name="description" content="book/src/guides/rainbow_bracket_queries.md in fork/helix at master.">
<meta property="og:title" content="book/src/guides/rainbow_bracket_queries.md - fork/helix @ master">
<meta property="og:description" content="book/src/guides/rainbow_bracket_queries.md in fork/helix at master.">
<meta property="og:type" content="website">
<meta property="og:site_name" content="git.julia.blue">
<link rel="stylesheet" href="/css/style.css?v=62">
<script type="module" src="/js/main.js?v=62"></script>
</head>
<body>
<main data-ref="master" data-tip="10f07d7eb089430930e6d3547a98b440b927d025">
<noscript>
  <div class="callout">
    <p>
      hey! this is a static git repo viewer. commit diffs, file history,
      older commits and other revisions won't load without javascript.
    </p>
    <p>
      you are still able to view source files on branch/tag tips,
      but the experience is degraded.
    </p>
  </div>
</noscript>
<header class="repo"><div>
<h1>fork/helix</h1>
</div>
<pre class="clone">git clone https://git.julia.blue/fork/helix</pre>
</header>
<p class="snapshot-commit"><span class="commit-author">may</span><span class="commit-message" data-commit="e678743115e26ecf2c22b544b5549cd100a0b71c">fix tree-sitter grammar example in rainbow bracket guide (#15033)</span><span class="snapshot-sha sha" data-commit="e678743115e26ecf2c22b544b5549cd100a0b71c">e6787431</span><time datetime="2025-12-30T14:51:42+01:00" title="2025-12-30 14:51:42 +0100">2025-12-30</time></p>
<header class="topbar"><details class="switcher"><summary>master</summary><div class="menu"><strong>branches</strong><a class="current" href="/fork/helix/ref/master/">master</a><strong>tags</strong><a href="/fork/helix/ref/23.05/">23.05</a><a href="/fork/helix/ref/23.10/">23.10</a><a href="/fork/helix/ref/24.03/">24.03</a><a href="/fork/helix/ref/24.07/">24.07</a><a href="/fork/helix/ref/25.01/">25.01</a><a href="/fork/helix/ref/25.01.1/">25.01.1</a><a href="/fork/helix/ref/25.07/">25.07</a><a href="/fork/helix/ref/25.07.1/">25.07.1</a><a class="all" href="/fork/helix/refs">all refs →</a></div></details><nav class="crumbs"><a href="/fork/helix/">helix</a> / <a href="/fork/helix/ref/master/tree/book/">book</a> / <a href="/fork/helix/ref/master/tree/book/src/">src</a> / <a href="/fork/helix/ref/master/tree/book/src/guides/">guides</a> / <span class="cur">rainbow_bracket_queries.md</span></nav>
<span class="view-stats"><span>5.0 KiB</span><span>132 lines</span></span><span class="actions"><a class="raw" href="/fork/helix/raw/0e27740fec33cf919de2aba6370e0987ec9bd108/book/src/guides/rainbow_bracket_queries.md">raw</a></span></header>
<div class="markdown-view"><input type="radio" name="markdown-view" id="markdown-rendered" checked><label for="markdown-rendered">rendered</label><input type="radio" name="markdown-view" id="markdown-code"><label for="markdown-code">code</label><section class="readme rendered"><h1>Adding Rainbow Bracket Queries</h1>
<p>Helix uses <code>rainbows.scm</code> tree-sitter query files to provide rainbow bracket
functionality.</p>
<p>Tree-sitter queries are documented in the tree-sitter online documentation.
If you're writing queries for the first time, be sure to check out the section
on <a href="https://tree-sitter.github.io/tree-sitter/syntax-highlighting#highlights">syntax highlighting queries</a> and on <a href="https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries">query syntax</a>.</p>
<p>Rainbow queries have two captures: <code>@rainbow.scope</code> and <code>@rainbow.bracket</code>.
<code>@rainbow.scope</code> should capture any node that increases the nesting level
while <code>@rainbow.bracket</code> should capture any bracket nodes. Put another way:
<code>@rainbow.scope</code> switches to the next rainbow color for all nodes in the tree
under it while <code>@rainbow.bracket</code> paints captured nodes with the current
rainbow color.</p>
<p>For an example, let's add rainbow queries for the tree-sitter query (TSQ)
language itself. These queries will go into a
<code>runtime/queries/tsq/rainbows.scm</code> file in the repository root.</p>
<p>First we'll add the <code>@rainbow.bracket</code> captures. TSQ only has parentheses and
square brackets:</p>
<pre>[&quot;(&quot; &quot;)&quot; &quot;[&quot; &quot;]&quot;] @rainbow.bracket
</pre>
<p>The ordering of the nodes within the alternation (square brackets) is not
taken into consideration.</p>
<blockquote>
<p>Note: Why are these nodes quoted? Most syntax highlights capture text
surrounded by parentheses. These are <em>named nodes</em> and correspond to the
names of rules in the grammar. Brackets are usually written in tree-sitter
grammars as literal strings, for example:</p>
<pre class="language-javascript"><a-p>{</a-p>
  <a-c>// ...</a-c>
  <a-f>arguments</a-f>: <a-v>$</a-v> <a-o>=&gt;</a-o> <a-f>seq</a-f><a-p>(</a-p><a-s>&quot;(&quot;</a-s><a-p>,</a-p> <a-f>repeat</a-f><a-p>(</a-p><a-v>$</a-v><a-p>.</a-p><a-pr>argument</a-pr><a-p>),</a-p> <a-s>&quot;)&quot;</a-s><a-p>),</a-p>
  <a-c>// ...</a-c>
<a-p>}</a-p></pre>
<p>Nodes written as literal strings in tree-sitter grammars may be captured
in queries with those same literal strings.</p>
</blockquote>
<p>Then we'll add <code>@rainbow.scope</code> captures. The easiest way to do this is to
view the <code>grammar.js</code> file in the tree-sitter grammar's repository. For TSQ,
that file is <a href="https://github.com/the-mikedavis/tree-sitter-tsq/blob/48b5e9f82ae0a4727201626f33a17f69f8e0ff86/grammar.js">here</a>. As we scroll down the <code>grammar.js</code>, we
see that the <code>(alternation)</code>, (L36) <code>(group)</code> (L57), <code>(named_node)</code> (L59),
<code>(predicate)</code> (L87) and  <code>(wildcard_node)</code> (L97) nodes all contain literal
parentheses or square brackets in their definitions. These nodes are all
direct parents of brackets and happen to also be the nodes we want to change
to the next rainbow color, so we capture them as <code>@rainbow.scope</code>.</p>
<pre>[
  (group)
  (named_node)
  (wildcard_node)
  (predicate)
  (alternation)
] @rainbow.scope
</pre>
<p>This strategy works as a rule of thumb for most programming and configuration
languages. Markup languages can be trickier and may take additional
experimentation to find the correct nodes to use for scopes and brackets.</p>
<p>The <code>:tree-sitter-subtree</code> command shows the syntax tree under the primary
selection in S-expression format and can be a useful tool for determining how
to write a query.</p>
<h3>Properties</h3>
<p>The <code>rainbow.include-children</code> property may be applied to <code>@rainbow.scope</code>
captures. By default, all <code>@rainbow.bracket</code> captures must be direct descendant
of a node captured with <code>@rainbow.scope</code> in a syntax tree in order to be
highlighted. The <code>rainbow.include-children</code> property disables that check and
allows <code>@rainbow.bracket</code> captures to be highlighted if they are direct or
indirect descendants of some node captured with <code>@rainbow.scope</code>.</p>
<p>For example, this property is used in the HTML rainbow queries.</p>
<p>For a document like <code>&lt;a&gt;link&lt;/a&gt;</code>, the syntax tree is:</p>
<pre>(element                   ; &lt;a&gt;link&lt;/a&gt;
  (start_tag               ; &lt;a&gt;
    (tag_name))            ; a
  (text)                   ; link
  (end_tag                 ; &lt;/a&gt;
    (tag_name)))           ; a
</pre>
<p>If we want to highlight the <code>&lt;</code>, <code>&gt;</code> and <code>&lt;/</code> nodes with rainbow colors, we
capture them as <code>@rainbow.bracket</code>:</p>
<pre>[&quot;&lt;&quot; &quot;&gt;&quot; &quot;&lt;/&quot;] @rainbow.bracket
</pre>
<p>And we capture <code>(element)</code> as <code>@rainbow.scope</code> because <code>(element)</code> nodes nest
within each other: they increment the nesting level and switch to the next
color in the rainbow.</p>
<pre>(element) @rainbow.scope
</pre>
<p>But this combination of <code>@rainbow.scope</code> and <code>@rainbow.bracket</code> will not
highlight any nodes. <code>&lt;</code>, <code>&gt;</code> and <code>&lt;/</code> are children of the <code>(start_tag)</code> and
<code>(end_tag)</code> nodes. We can't capture <code>(start_tag)</code> and <code>(end_tag)</code> as
<code>@rainbow.scope</code> because they don't nest other elements. We can fix this case
by removing the requirement that <code>&lt;</code>, <code>&gt;</code> and <code>&lt;/</code> are direct descendants of
<code>(element)</code> using the <code>rainbow.include-children</code> property.</p>
<pre>((element) @rainbow.scope
 (#set! rainbow.include-children))
</pre>
<p>With this property set, <code>&lt;</code>, <code>&gt;</code>, and <code>&lt;/</code> will highlight with rainbow colors
even though they aren't direct descendents of the <code>(element)</code> node.</p>
<p><code>rainbow.include-children</code> is not necessary for the vast majority of programming
languages. It is only necessary when the node that increments the nesting level
(changes rainbow color) is not the direct parent of the bracket node.</p>
</section><div class="code-panel"><pre class="code src language-markdown"><span class="code-line"><a class="ln" id="L1" href="#L1">1</a><span class="code-text"><a-p>#</a-p> <a-tt>Adding Rainbow Bracket Queries</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L2" href="#L2">2</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L3" href="#L3">3</a><span class="code-text">Helix uses `rainbows.scm` tree-sitter query files to provide rainbow bracket</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L4" href="#L4">4</a><span class="code-text">functionality.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L5" href="#L5">5</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L6" href="#L6">6</a><span class="code-text">Tree-sitter queries are documented in the tree-sitter online documentation.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L7" href="#L7">7</a><span class="code-text">If you&#39;re writing queries for the first time, be sure to check out the section</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L8" href="#L8">8</a><span class="code-text">on [syntax highlighting queries] and on [query syntax].</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L9" href="#L9">9</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L10" href="#L10">10</a><span class="code-text">Rainbow queries have two captures: `@rainbow.scope` and `@rainbow.bracket`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L11" href="#L11">11</a><span class="code-text">`@rainbow.scope` should capture any node that increases the nesting level</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L12" href="#L12">12</a><span class="code-text">while `@rainbow.bracket` should capture any bracket nodes. Put another way:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L13" href="#L13">13</a><span class="code-text">`@rainbow.scope` switches to the next rainbow color for all nodes in the tree</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L14" href="#L14">14</a><span class="code-text">under it while `@rainbow.bracket` paints captured nodes with the current</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L15" href="#L15">15</a><span class="code-text">rainbow color.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L16" href="#L16">16</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L17" href="#L17">17</a><span class="code-text">For an example, let&#39;s add rainbow queries for the tree-sitter query (TSQ)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L18" href="#L18">18</a><span class="code-text">language itself. These queries will go into a</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L19" href="#L19">19</a><span class="code-text">`runtime/queries/tsq/rainbows.scm` file in the repository root.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L20" href="#L20">20</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L21" href="#L21">21</a><span class="code-text">First we&#39;ll add the `@rainbow.bracket` captures. TSQ only has parentheses and</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L22" href="#L22">22</a><span class="code-text">square brackets:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L23" href="#L23">23</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L24" href="#L24">24</a><span class="code-text"><a-p>```</a-p><a-tl>tsq</a-tl></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L25" href="#L25">25</a><span class="code-text"><a-p>[&quot;(&quot; &quot;)&quot; &quot;[&quot; &quot;]&quot;] @rainbow.bracket</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L26" href="#L26">26</a><span class="code-text"><a-p>```</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L27" href="#L27">27</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L28" href="#L28">28</a><span class="code-text"><a-p>The ordering of the nodes within the alternation (square brackets) is not</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L29" href="#L29">29</a><span class="code-text"><a-p>taken into consideration.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L30" href="#L30">30</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L31" href="#L31">31</a><span class="code-text"><a-p>&gt; Note: Why are these nodes quoted? Most syntax highlights capture text</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L32" href="#L32">32</a><span class="code-text"><a-p>&gt; surrounded by parentheses. These are _named nodes_ and correspond to the</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L33" href="#L33">33</a><span class="code-text"><a-p>&gt; names of rules in the grammar. Brackets are usually written in tree-sitter</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L34" href="#L34">34</a><span class="code-text"><a-p>&gt; grammars as literal strings, for example:</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L35" href="#L35">35</a><span class="code-text"><a-p>&gt;</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L36" href="#L36">36</a><span class="code-text"><a-p>&gt; </a-p><a-tl>```js</a-tl></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L37" href="#L37">37</a><span class="code-text"><a-p>&gt; </a-p><a-tl>{</a-tl></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L38" href="#L38">38</a><span class="code-text"><a-p>&gt; </a-p><a-tl>  // ...</a-tl></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L39" href="#L39">39</a><span class="code-text"><a-p>&gt; </a-p><a-tl>  arguments: $ =&gt; seq(&quot;(&quot;, repeat($.argument), &quot;)&quot;),</a-tl></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L40" href="#L40">40</a><span class="code-text"><a-p>&gt; </a-p><a-tl>  // ...</a-tl></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L41" href="#L41">41</a><span class="code-text"><a-p>&gt; </a-p><a-tl>}</a-tl></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L42" href="#L42">42</a><span class="code-text"><a-p>&gt; ```</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L43" href="#L43">43</a><span class="code-text"><a-p>&gt;</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L44" href="#L44">44</a><span class="code-text"><a-p>&gt; Nodes written as literal strings in tree-sitter grammars may be captured</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L45" href="#L45">45</a><span class="code-text"><a-p>&gt; in queries with those same literal strings.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L46" href="#L46">46</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L47" href="#L47">47</a><span class="code-text"><a-p>Then we&#39;ll add `@rainbow.scope` captures. The easiest way to do this is to</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L48" href="#L48">48</a><span class="code-text"><a-p>view the `grammar.js` file in the tree-sitter grammar&#39;s repository. For TSQ,</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L49" href="#L49">49</a><span class="code-text"><a-p>that file is [here][tsq grammar.js]. As we scroll down the `grammar.js`, we</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L50" href="#L50">50</a><span class="code-text"><a-p>see that the `(alternation)`, (L36) `(group)` (L57), `(named_node)` (L59),</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L51" href="#L51">51</a><span class="code-text"><a-p>`(predicate)` (L87) and  `(wildcard_node)` (L97) nodes all contain literal</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L52" href="#L52">52</a><span class="code-text"><a-p>parentheses or square brackets in their definitions. These nodes are all</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L53" href="#L53">53</a><span class="code-text"><a-p>direct parents of brackets and happen to also be the nodes we want to change</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L54" href="#L54">54</a><span class="code-text"><a-p>to the next rainbow color, so we capture them as `@rainbow.scope`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L55" href="#L55">55</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L56" href="#L56">56</a><span class="code-text"><a-p>```</a-p><a-tl>tsq</a-tl></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L57" href="#L57">57</a><span class="code-text"><a-p>[</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L58" href="#L58">58</a><span class="code-text"><a-p>  (group)</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L59" href="#L59">59</a><span class="code-text"><a-p>  (named_node)</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L60" href="#L60">60</a><span class="code-text"><a-p>  (wildcard_node)</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L61" href="#L61">61</a><span class="code-text"><a-p>  (predicate)</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L62" href="#L62">62</a><span class="code-text"><a-p>  (alternation)</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L63" href="#L63">63</a><span class="code-text"><a-p>] @rainbow.scope</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L64" href="#L64">64</a><span class="code-text"><a-p>```</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L65" href="#L65">65</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L66" href="#L66">66</a><span class="code-text"><a-p>This strategy works as a rule of thumb for most programming and configuration</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L67" href="#L67">67</a><span class="code-text"><a-p>languages. Markup languages can be trickier and may take additional</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L68" href="#L68">68</a><span class="code-text"><a-p>experimentation to find the correct nodes to use for scopes and brackets.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L69" href="#L69">69</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L70" href="#L70">70</a><span class="code-text"><a-p>The `:tree-sitter-subtree` command shows the syntax tree under the primary</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L71" href="#L71">71</a><span class="code-text"><a-p>selection in S-expression format and can be a useful tool for determining how</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L72" href="#L72">72</a><span class="code-text"><a-p>to write a query.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L73" href="#L73">73</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L74" href="#L74">74</a><span class="code-text"><a-p>### </a-p><a-tt>Properties</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L75" href="#L75">75</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L76" href="#L76">76</a><span class="code-text"><a-p>The `rainbow.include-children` property may be applied to `@rainbow.scope`</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L77" href="#L77">77</a><span class="code-text"><a-p>captures. By default, all `@rainbow.bracket` captures must be direct descendant</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L78" href="#L78">78</a><span class="code-text"><a-p>of a node captured with `@rainbow.scope` in a syntax tree in order to be</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L79" href="#L79">79</a><span class="code-text"><a-p>highlighted. The `rainbow.include-children` property disables that check and</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L80" href="#L80">80</a><span class="code-text"><a-p>allows `@rainbow.bracket` captures to be highlighted if they are direct or</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L81" href="#L81">81</a><span class="code-text"><a-p>indirect descendants of some node captured with `@rainbow.scope`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L82" href="#L82">82</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L83" href="#L83">83</a><span class="code-text"><a-p>For example, this property is used in the HTML rainbow queries.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L84" href="#L84">84</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L85" href="#L85">85</a><span class="code-text"><a-p>For a document like `&lt;a&gt;link&lt;/a&gt;`, the syntax tree is:</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L86" href="#L86">86</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L87" href="#L87">87</a><span class="code-text"><a-p>```</a-p><a-tl>tsq</a-tl></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L88" href="#L88">88</a><span class="code-text"><a-p>(element                   ; &lt;a&gt;link&lt;/a&gt;</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L89" href="#L89">89</a><span class="code-text"><a-p>  (start_tag               ; &lt;a&gt;</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L90" href="#L90">90</a><span class="code-text"><a-p>    (tag_name))            ; a</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L91" href="#L91">91</a><span class="code-text"><a-p>  (text)                   ; link</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L92" href="#L92">92</a><span class="code-text"><a-p>  (end_tag                 ; &lt;/a&gt;</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L93" href="#L93">93</a><span class="code-text"><a-p>    (tag_name)))           ; a</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L94" href="#L94">94</a><span class="code-text"><a-p>```</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L95" href="#L95">95</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L96" href="#L96">96</a><span class="code-text"><a-p>If we want to highlight the `&lt;`, `&gt;` and `&lt;/` nodes with rainbow colors, we</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L97" href="#L97">97</a><span class="code-text"><a-p>capture them as `@rainbow.bracket`:</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L98" href="#L98">98</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L99" href="#L99">99</a><span class="code-text"><a-p>```</a-p><a-tl>tsq</a-tl></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L100" href="#L100">100</a><span class="code-text"><a-p>[&quot;&lt;&quot; &quot;&gt;&quot; &quot;&lt;/&quot;] @rainbow.bracket</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L101" href="#L101">101</a><span class="code-text"><a-p>```</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L102" href="#L102">102</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L103" href="#L103">103</a><span class="code-text"><a-p>And we capture `(element)` as `@rainbow.scope` because `(element)` nodes nest</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L104" href="#L104">104</a><span class="code-text"><a-p>within each other: they increment the nesting level and switch to the next</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L105" href="#L105">105</a><span class="code-text"><a-p>color in the rainbow.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L106" href="#L106">106</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L107" href="#L107">107</a><span class="code-text"><a-p>```</a-p><a-tl>tsq</a-tl></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L108" href="#L108">108</a><span class="code-text"><a-p>(element) @rainbow.scope</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L109" href="#L109">109</a><span class="code-text"><a-p>```</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L110" href="#L110">110</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L111" href="#L111">111</a><span class="code-text"><a-p>But this combination of `@rainbow.scope` and `@rainbow.bracket` will not</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L112" href="#L112">112</a><span class="code-text"><a-p>highlight any nodes. `&lt;`, `&gt;` and `&lt;/` are children of the `(start_tag)` and</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L113" href="#L113">113</a><span class="code-text"><a-p>`(end_tag)` nodes. We can&#39;t capture `(start_tag)` and `(end_tag)` as</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L114" href="#L114">114</a><span class="code-text"><a-p>`@rainbow.scope` because they don&#39;t nest other elements. We can fix this case</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L115" href="#L115">115</a><span class="code-text"><a-p>by removing the requirement that `&lt;`, `&gt;` and `&lt;/` are direct descendants of</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L116" href="#L116">116</a><span class="code-text"><a-p>`(element)` using the `rainbow.include-children` property.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L117" href="#L117">117</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L118" href="#L118">118</a><span class="code-text"><a-p>```</a-p><a-tl>tsq</a-tl></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L119" href="#L119">119</a><span class="code-text"><a-p>((element) @rainbow.scope</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L120" href="#L120">120</a><span class="code-text"><a-p> (#set! rainbow.include-children))</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L121" href="#L121">121</a><span class="code-text"><a-p>```</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L122" href="#L122">122</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L123" href="#L123">123</a><span class="code-text"><a-p>With this property set, `&lt;`, `&gt;`, and `&lt;/` will highlight with rainbow colors</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L124" href="#L124">124</a><span class="code-text"><a-p>even though they aren&#39;t direct descendents of the `(element)` node.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L125" href="#L125">125</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L126" href="#L126">126</a><span class="code-text"><a-p>`rainbow.include-children` is not necessary for the vast majority of programming</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L127" href="#L127">127</a><span class="code-text"><a-p>languages. It is only necessary when the node that increments the nesting level</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L128" href="#L128">128</a><span class="code-text"><a-p>(changes rainbow color) is not the direct parent of the bracket node.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L129" href="#L129">129</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L130" href="#L130">130</a><span class="code-text"><a-tu>[syntax highlighting queries]</a-tu><a-p>: </a-p><a-tu>https://tree-sitter.github.io/tree-sitter/syntax-highlighting#highlights</a-tu></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L131" href="#L131">131</a><span class="code-text"><a-tu>[query syntax]</a-tu><a-p>: </a-p><a-tu>https://tree-sitter.github.io/tree-sitter/using-parsers#pattern-matching-with-queries</a-tu></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L132" href="#L132">132</a><span class="code-text"><a-tu>[tsq grammar.js]</a-tu><a-p>: </a-p><a-tu>https://github.com/the-mikedavis/tree-sitter-tsq/blob/48b5e9f82ae0a4727201626f33a17f69f8e0ff86/grammar.js</a-tu></span></span></pre></div></div>
</main>
</body>
</html>
