diff options
| author | Yuan Fu | 2022-10-05 14:11:33 -0700 |
|---|---|---|
| committer | Yuan Fu | 2022-10-05 14:11:33 -0700 |
| commit | cb183f6467401fb5ed2b7fc98ca75be9d943cbe3 (patch) | |
| tree | ef42ea6ae71e0829d900ffb46d8306fbba962a8e /admin/notes/tree-sitter/html-manual/Retrieving-Node.html | |
| parent | 1ea503ed4b3a14b3dc0a597cfbfe57d73b871422 (diff) | |
| download | emacs-cb183f6467401fb5ed2b7fc98ca75be9d943cbe3.tar.gz emacs-cb183f6467401fb5ed2b7fc98ca75be9d943cbe3.zip | |
Add tree-sitter admin notes
starter-guide: Guide on writing major mode features.
build-module: Script for building official language definitions.
html-manual: HTML version of the manual for easy access.
* admin/notes/tree-sitter/build-module/README: New file.
* admin/notes/tree-sitter/build-module/batch.sh: New file.
* admin/notes/tree-sitter/build-module/build.sh: New file.
* admin/notes/tree-sitter/starter-guide: New file.
* admin/notes/tree-sitter/html-manual/Accessing-Node.html: New file.
* admin/notes/tree-sitter/html-manual/Language-Definitions.html: New file.
* admin/notes/tree-sitter/html-manual/Multiple-Languages.html: New file.
* admin/notes/tree-sitter/html-manual/Parser_002dbased-Font-Lock.html:
New file.
* admin/notes/tree-sitter/html-manual/Parser_002dbased-Indentation.html:
New file.
* admin/notes/tree-sitter/html-manual/Parsing-Program-Source.html: New
file.
* admin/notes/tree-sitter/html-manual/Pattern-Matching.html: New file.
* admin/notes/tree-sitter/html-manual/Retrieving-Node.html: New file.
* admin/notes/tree-sitter/html-manual/Tree_002dsitter-C-API.html: New
file.
* admin/notes/tree-sitter/html-manual/Using-Parser.html: New file.
* admin/notes/tree-sitter/html-manual/build-manual.sh: New file.
* admin/notes/tree-sitter/html-manual/manual.css: New file.
Diffstat (limited to 'admin/notes/tree-sitter/html-manual/Retrieving-Node.html')
| -rw-r--r-- | admin/notes/tree-sitter/html-manual/Retrieving-Node.html | 362 |
1 files changed, 362 insertions, 0 deletions
diff --git a/admin/notes/tree-sitter/html-manual/Retrieving-Node.html b/admin/notes/tree-sitter/html-manual/Retrieving-Node.html new file mode 100644 index 00000000000..1bea0dde76b --- /dev/null +++ b/admin/notes/tree-sitter/html-manual/Retrieving-Node.html | |||
| @@ -0,0 +1,362 @@ | |||
| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | ||
| 2 | <html> | ||
| 3 | <!-- Created by GNU Texinfo 6.8, https://www.gnu.org/software/texinfo/ --> | ||
| 4 | <head> | ||
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | ||
| 6 | <!-- This is the GNU Emacs Lisp Reference Manual | ||
| 7 | corresponding to Emacs version 29.0.50. | ||
| 8 | |||
| 9 | Copyright © 1990-1996, 1998-2022 Free Software Foundation, | ||
| 10 | Inc. | ||
| 11 | |||
| 12 | Permission is granted to copy, distribute and/or modify this document | ||
| 13 | under the terms of the GNU Free Documentation License, Version 1.3 or | ||
| 14 | any later version published by the Free Software Foundation; with the | ||
| 15 | Invariant Sections being "GNU General Public License," with the | ||
| 16 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover | ||
| 17 | Texts as in (a) below. A copy of the license is included in the | ||
| 18 | section entitled "GNU Free Documentation License." | ||
| 19 | |||
| 20 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and | ||
| 21 | modify this GNU manual. Buying copies from the FSF supports it in | ||
| 22 | developing GNU and promoting software freedom." --> | ||
| 23 | <title>Retrieving Node (GNU Emacs Lisp Reference Manual)</title> | ||
| 24 | |||
| 25 | <meta name="description" content="Retrieving Node (GNU Emacs Lisp Reference Manual)"> | ||
| 26 | <meta name="keywords" content="Retrieving Node (GNU Emacs Lisp Reference Manual)"> | ||
| 27 | <meta name="resource-type" content="document"> | ||
| 28 | <meta name="distribution" content="global"> | ||
| 29 | <meta name="Generator" content="makeinfo"> | ||
| 30 | <meta name="viewport" content="width=device-width,initial-scale=1"> | ||
| 31 | |||
| 32 | <link href="index.html" rel="start" title="Top"> | ||
| 33 | <link href="Index.html" rel="index" title="Index"> | ||
| 34 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> | ||
| 35 | <link href="Parsing-Program-Source.html" rel="up" title="Parsing Program Source"> | ||
| 36 | <link href="Accessing-Node.html" rel="next" title="Accessing Node"> | ||
| 37 | <link href="Using-Parser.html" rel="prev" title="Using Parser"> | ||
| 38 | <style type="text/css"> | ||
| 39 | <!-- | ||
| 40 | a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em} | ||
| 41 | a.summary-letter {text-decoration: none} | ||
| 42 | blockquote.indentedblock {margin-right: 0em} | ||
| 43 | div.display {margin-left: 3.2em} | ||
| 44 | div.example {margin-left: 3.2em} | ||
| 45 | kbd {font-style: oblique} | ||
| 46 | pre.display {font-family: inherit} | ||
| 47 | pre.format {font-family: inherit} | ||
| 48 | pre.menu-comment {font-family: serif} | ||
| 49 | pre.menu-preformatted {font-family: serif} | ||
| 50 | span.nolinebreak {white-space: nowrap} | ||
| 51 | span.roman {font-family: initial; font-weight: normal} | ||
| 52 | span.sansserif {font-family: sans-serif; font-weight: normal} | ||
| 53 | span:hover a.copiable-anchor {visibility: visible} | ||
| 54 | ul.no-bullet {list-style: none} | ||
| 55 | --> | ||
| 56 | </style> | ||
| 57 | <link rel="stylesheet" type="text/css" href="./manual.css"> | ||
| 58 | |||
| 59 | |||
| 60 | </head> | ||
| 61 | |||
| 62 | <body lang="en"> | ||
| 63 | <div class="section" id="Retrieving-Node"> | ||
| 64 | <div class="header"> | ||
| 65 | <p> | ||
| 66 | Next: <a href="Accessing-Node.html" accesskey="n" rel="next">Accessing Node Information</a>, Previous: <a href="Using-Parser.html" accesskey="p" rel="prev">Using Tree-sitter Parser</a>, Up: <a href="Parsing-Program-Source.html" accesskey="u" rel="up">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> | ||
| 67 | </div> | ||
| 68 | <hr> | ||
| 69 | <span id="Retrieving-Node-1"></span><h3 class="section">37.3 Retrieving Node</h3> | ||
| 70 | |||
| 71 | <span id="index-tree_002dsitter-find-node"></span> | ||
| 72 | <span id="index-tree_002dsitter-get-node"></span> | ||
| 73 | <p>Before we continue, lets go over some conventions of tree-sitter | ||
| 74 | functions. | ||
| 75 | </p> | ||
| 76 | <p>We talk about a node being “smaller” or “larger”, and “lower” or | ||
| 77 | “higher”. A smaller and lower node is lower in the syntax tree and | ||
| 78 | therefore spans a smaller piece of text; a larger and higher node is | ||
| 79 | higher up in the syntax tree, containing many smaller nodes as its | ||
| 80 | children, and therefore spans a larger piece of text. | ||
| 81 | </p> | ||
| 82 | <p>When a function cannot find a node, it returns nil. And for the | ||
| 83 | convenience for function chaining, all the functions that take a node | ||
| 84 | as argument and returns a node accept the node to be nil; in that | ||
| 85 | case, the function just returns nil. | ||
| 86 | </p> | ||
| 87 | <span id="index-treesit_002dnode_002doutdated"></span> | ||
| 88 | <p>Nodes are not automatically updated when the associated buffer is | ||
| 89 | modified. And there is no way to update a node once it is retrieved. | ||
| 90 | Using an outdated node throws <code>treesit-node-outdated</code> error. | ||
| 91 | </p> | ||
| 92 | <span id="Retrieving-node-from-syntax-tree"></span><h3 class="heading">Retrieving node from syntax tree</h3> | ||
| 93 | |||
| 94 | <dl class="def"> | ||
| 95 | <dt id="index-treesit_002dnode_002dat"><span class="category">Function: </span><span><strong>treesit-node-at</strong> <em>beg end &optional parser-or-lang named</em><a href='#index-treesit_002dnode_002dat' class='copiable-anchor'> ¶</a></span></dt> | ||
| 96 | <dd><p>This function returns the <em>smallest</em> node that starts at or after | ||
| 97 | the <var>point</var>. In other words, the start of the node is equal or | ||
| 98 | greater than <var>point</var>. | ||
| 99 | </p> | ||
| 100 | <p>When <var>parser-or-lang</var> is nil, this function uses the first parser | ||
| 101 | in <code>(treesit-parser-list)</code> in the current buffer. If | ||
| 102 | <var>parser-or-lang</var> is a parser object, it use that parser; if | ||
| 103 | <var>parser-or-lang</var> is a language, it finds the first parser using | ||
| 104 | that language in <code>(treesit-parser-list)</code> and use that. | ||
| 105 | </p> | ||
| 106 | <p>If <var>named</var> is non-nil, this function looks for a named node | ||
| 107 | only (see <a href="Language-Definitions.html#tree_002dsitter-named-node">named node</a>). | ||
| 108 | </p> | ||
| 109 | <p>Example: | ||
| 110 | </p><div class="example"> | ||
| 111 | <pre class="example">;; Find the node at point in a C parser's syntax tree. | ||
| 112 | (treesit-node-at (point) 'c) | ||
| 113 | </pre></div> | ||
| 114 | </dd></dl> | ||
| 115 | |||
| 116 | <dl class="def"> | ||
| 117 | <dt id="index-treesit_002dnode_002don"><span class="category">Function: </span><span><strong>treesit-node-on</strong> <em>beg end &optional parser-or-lang named</em><a href='#index-treesit_002dnode_002don' class='copiable-anchor'> ¶</a></span></dt> | ||
| 118 | <dd><p>This function returns the <em>smallest</em> node that covers the span | ||
| 119 | from <var>beg</var> to <var>end</var>. In other words, the start of the node is | ||
| 120 | less or equal to <var>beg</var>, and the end of the node is greater or | ||
| 121 | equal to <var>end</var>. | ||
| 122 | </p> | ||
| 123 | <p><em>Beware</em> that calling this function on an empty line that is not | ||
| 124 | inside any top-level construct (function definition, etc) most | ||
| 125 | probably will give you the root node, because the root node is the | ||
| 126 | smallest node that covers that empty line. Most of the time, you want | ||
| 127 | to use <code>treesit-node-at</code>. | ||
| 128 | </p> | ||
| 129 | <p>When <var>parser-or-lang</var> is nil, this function uses the first parser | ||
| 130 | in <code>(treesit-parser-list)</code> in the current buffer. If | ||
| 131 | <var>parser-or-lang</var> is a parser object, it use that parser; if | ||
| 132 | <var>parser-or-lang</var> is a language, it finds the first parser using | ||
| 133 | that language in <code>(treesit-parser-list)</code> and use that. | ||
| 134 | </p> | ||
| 135 | <p>If <var>named</var> is non-nil, this function looks for a named node only | ||
| 136 | (see <a href="Language-Definitions.html#tree_002dsitter-named-node">named node</a>). | ||
| 137 | </p></dd></dl> | ||
| 138 | |||
| 139 | <dl class="def"> | ||
| 140 | <dt id="index-treesit_002dparser_002droot_002dnode"><span class="category">Function: </span><span><strong>treesit-parser-root-node</strong> <em>parser</em><a href='#index-treesit_002dparser_002droot_002dnode' class='copiable-anchor'> ¶</a></span></dt> | ||
| 141 | <dd><p>This function returns the root node of the syntax tree generated by | ||
| 142 | <var>parser</var>. | ||
| 143 | </p></dd></dl> | ||
| 144 | |||
| 145 | <dl class="def"> | ||
| 146 | <dt id="index-treesit_002dbuffer_002droot_002dnode"><span class="category">Function: </span><span><strong>treesit-buffer-root-node</strong> <em>&optional language</em><a href='#index-treesit_002dbuffer_002droot_002dnode' class='copiable-anchor'> ¶</a></span></dt> | ||
| 147 | <dd><p>This function finds the first parser that uses <var>language</var> in | ||
| 148 | <code>(treesit-parser-list)</code> in the current buffer, and returns the | ||
| 149 | root node of that buffer. If it cannot find an appropriate parser, | ||
| 150 | nil is returned. | ||
| 151 | </p></dd></dl> | ||
| 152 | |||
| 153 | <p>Once we have a node, we can retrieve other nodes from it, or query for | ||
| 154 | information about this node. | ||
| 155 | </p> | ||
| 156 | <span id="Retrieving-node-from-other-nodes"></span><h3 class="heading">Retrieving node from other nodes</h3> | ||
| 157 | |||
| 158 | <span id="By-kinship"></span><h4 class="subheading">By kinship</h4> | ||
| 159 | |||
| 160 | <dl class="def"> | ||
| 161 | <dt id="index-treesit_002dnode_002dparent"><span class="category">Function: </span><span><strong>treesit-node-parent</strong> <em>node</em><a href='#index-treesit_002dnode_002dparent' class='copiable-anchor'> ¶</a></span></dt> | ||
| 162 | <dd><p>This function returns the immediate parent of <var>node</var>. | ||
| 163 | </p></dd></dl> | ||
| 164 | |||
| 165 | <dl class="def"> | ||
| 166 | <dt id="index-treesit_002dnode_002dchild"><span class="category">Function: </span><span><strong>treesit-node-child</strong> <em>node n &optional named</em><a href='#index-treesit_002dnode_002dchild' class='copiable-anchor'> ¶</a></span></dt> | ||
| 167 | <dd><p>This function returns the <var>n</var>’th child of <var>node</var>. If | ||
| 168 | <var>named</var> is non-nil, then it only counts named nodes | ||
| 169 | (see <a href="Language-Definitions.html#tree_002dsitter-named-node">named node</a>). For example, in a node | ||
| 170 | that represents a string: <code>"text"</code>, there are three children | ||
| 171 | nodes: the opening quote <code>"</code>, the string content <code>text</code>, and | ||
| 172 | the enclosing quote <code>"</code>. Among these nodes, the first child is | ||
| 173 | the opening quote <code>"</code>, the first named child is the string | ||
| 174 | content <code>text</code>. | ||
| 175 | </p></dd></dl> | ||
| 176 | |||
| 177 | <dl class="def"> | ||
| 178 | <dt id="index-treesit_002dnode_002dchildren"><span class="category">Function: </span><span><strong>treesit-node-children</strong> <em>node &optional named</em><a href='#index-treesit_002dnode_002dchildren' class='copiable-anchor'> ¶</a></span></dt> | ||
| 179 | <dd><p>This function returns all of <var>node</var>’s children in a list. If | ||
| 180 | <var>named</var> is non-nil, then it only retrieves named nodes. | ||
| 181 | </p></dd></dl> | ||
| 182 | |||
| 183 | <dl class="def"> | ||
| 184 | <dt id="index-treesit_002dnext_002dsibling"><span class="category">Function: </span><span><strong>treesit-next-sibling</strong> <em>node &optional named</em><a href='#index-treesit_002dnext_002dsibling' class='copiable-anchor'> ¶</a></span></dt> | ||
| 185 | <dd><p>This function finds the next sibling of <var>node</var>. If <var>named</var> is | ||
| 186 | non-nil, it finds the next named sibling. | ||
| 187 | </p></dd></dl> | ||
| 188 | |||
| 189 | <dl class="def"> | ||
| 190 | <dt id="index-treesit_002dprev_002dsibling"><span class="category">Function: </span><span><strong>treesit-prev-sibling</strong> <em>node &optional named</em><a href='#index-treesit_002dprev_002dsibling' class='copiable-anchor'> ¶</a></span></dt> | ||
| 191 | <dd><p>This function finds the previous sibling of <var>node</var>. If | ||
| 192 | <var>named</var> is non-nil, it finds the previous named sibling. | ||
| 193 | </p></dd></dl> | ||
| 194 | |||
| 195 | <span id="By-field-name"></span><h4 class="subheading">By field name</h4> | ||
| 196 | |||
| 197 | <p>To make the syntax tree easier to analyze, many language definitions | ||
| 198 | assign <em>field names</em> to child nodes (see <a href="Language-Definitions.html#tree_002dsitter-node-field-name">field name</a>). For example, a <code>function_definition</code> node | ||
| 199 | could have a <code>declarator</code> and a <code>body</code>. | ||
| 200 | </p> | ||
| 201 | <dl class="def"> | ||
| 202 | <dt id="index-treesit_002dchild_002dby_002dfield_002dname"><span class="category">Function: </span><span><strong>treesit-child-by-field-name</strong> <em>node field-name</em><a href='#index-treesit_002dchild_002dby_002dfield_002dname' class='copiable-anchor'> ¶</a></span></dt> | ||
| 203 | <dd><p>This function finds the child of <var>node</var> that has <var>field-name</var> | ||
| 204 | as its field name. | ||
| 205 | </p> | ||
| 206 | <div class="example"> | ||
| 207 | <pre class="example">;; Get the child that has "body" as its field name. | ||
| 208 | (treesit-child-by-field-name node "body") | ||
| 209 | </pre></div> | ||
| 210 | </dd></dl> | ||
| 211 | |||
| 212 | <span id="By-position"></span><h4 class="subheading">By position</h4> | ||
| 213 | |||
| 214 | <dl class="def"> | ||
| 215 | <dt id="index-treesit_002dfirst_002dchild_002dfor_002dpos"><span class="category">Function: </span><span><strong>treesit-first-child-for-pos</strong> <em>node pos &optional named</em><a href='#index-treesit_002dfirst_002dchild_002dfor_002dpos' class='copiable-anchor'> ¶</a></span></dt> | ||
| 216 | <dd><p>This function finds the first child of <var>node</var> that extends beyond | ||
| 217 | <var>pos</var>. “Extend beyond” means the end of the child node >= | ||
| 218 | <var>pos</var>. This function only looks for immediate children of | ||
| 219 | <var>node</var>, and doesn’t look in its grand children. If <var>named</var> is | ||
| 220 | non-nil, it only looks for named child (see <a href="Language-Definitions.html#tree_002dsitter-named-node">named node</a>). | ||
| 221 | </p></dd></dl> | ||
| 222 | |||
| 223 | <dl class="def"> | ||
| 224 | <dt id="index-treesit_002dnode_002ddescendant_002dfor_002drange"><span class="category">Function: </span><span><strong>treesit-node-descendant-for-range</strong> <em>node beg end &optional named</em><a href='#index-treesit_002dnode_002ddescendant_002dfor_002drange' class='copiable-anchor'> ¶</a></span></dt> | ||
| 225 | <dd><p>This function finds the <em>smallest</em> child/grandchild... of | ||
| 226 | <var>node</var> that spans the range from <var>beg</var> to <var>end</var>. It is | ||
| 227 | similar to <code>treesit-node-at</code>. If <var>named</var> is non-nil, it only | ||
| 228 | looks for named child. | ||
| 229 | </p></dd></dl> | ||
| 230 | |||
| 231 | <span id="Searching-for-node"></span><h3 class="heading">Searching for node</h3> | ||
| 232 | |||
| 233 | <dl class="def"> | ||
| 234 | <dt id="index-treesit_002dsearch_002dsubtree"><span class="category">Function: </span><span><strong>treesit-search-subtree</strong> <em>node predicate &optional all backward limit</em><a href='#index-treesit_002dsearch_002dsubtree' class='copiable-anchor'> ¶</a></span></dt> | ||
| 235 | <dd><p>This function traverses the subtree of <var>node</var> (including | ||
| 236 | <var>node</var>), and match <var>predicate</var> with each node along the way. | ||
| 237 | And <var>predicate</var> is a regexp that matches (case-insensitively) | ||
| 238 | against each node’s type, or a function that takes a node and returns | ||
| 239 | nil/non-nil. If a node matches, that node is returned, if no node | ||
| 240 | ever matches, nil is returned. | ||
| 241 | </p> | ||
| 242 | <p>By default, this function only traverses named nodes, if <var>all</var> is | ||
| 243 | non-nil, it traverses all nodes. If <var>backward</var> is non-nil, it | ||
| 244 | traverses backwards. If <var>limit</var> is non-nil, it only traverses | ||
| 245 | that number of levels down in the tree. | ||
| 246 | </p></dd></dl> | ||
| 247 | |||
| 248 | <dl class="def"> | ||
| 249 | <dt id="index-treesit_002dsearch_002dforward"><span class="category">Function: </span><span><strong>treesit-search-forward</strong> <em>start predicate &optional all backward up</em><a href='#index-treesit_002dsearch_002dforward' class='copiable-anchor'> ¶</a></span></dt> | ||
| 250 | <dd><p>This function is somewhat similar to <code>treesit-search-subtree</code>. | ||
| 251 | It also traverse the parse tree and match each node with | ||
| 252 | <var>predicate</var> (except for <var>start</var>), where <var>predicate</var> can be | ||
| 253 | a (case-insensitive) regexp or a function. For a tree like the below | ||
| 254 | where <var>start</var> is marked 1, this function traverses as numbered: | ||
| 255 | </p> | ||
| 256 | <div class="example"> | ||
| 257 | <pre class="example"> o | ||
| 258 | | | ||
| 259 | 3--------4-----------8 | ||
| 260 | | | | | ||
| 261 | o--o-+--1 5--+--6 9---+-----12 | ||
| 262 | | | | | | | | ||
| 263 | o o 2 7 +-+-+ +--+--+ | ||
| 264 | | | | | | | ||
| 265 | 10 11 13 14 15 | ||
| 266 | </pre></div> | ||
| 267 | |||
| 268 | <p>Same as in <code>treesit-search-subtree</code>, this function only searches | ||
| 269 | for named nodes by default. But if <var>all</var> is non-nil, it searches | ||
| 270 | for all nodes. If <var>backward</var> is non-nil, it searches backwards. | ||
| 271 | </p> | ||
| 272 | <p>If <var>up</var> is non-nil, this function will only traverse to siblings | ||
| 273 | and parents. In that case, only 1 3 4 8 would be traversed. | ||
| 274 | </p></dd></dl> | ||
| 275 | |||
| 276 | <dl class="def"> | ||
| 277 | <dt id="index-treesit_002dsearch_002dforward_002dgoto"><span class="category">Function: </span><span><strong>treesit-search-forward-goto</strong> <em>predicate side &optional all backward up</em><a href='#index-treesit_002dsearch_002dforward_002dgoto' class='copiable-anchor'> ¶</a></span></dt> | ||
| 278 | <dd><p>This function jumps to the start or end of the next node in buffer | ||
| 279 | that matches <var>predicate</var>. Parameters <var>predicate</var>, <var>all</var>, | ||
| 280 | <var>backward</var>, and <var>up</var> are the same as in | ||
| 281 | <code>treesit-search-forward</code>. And <var>side</var> controls which side of | ||
| 282 | the matched no do we stop at, it can be <code>start</code> or <code>end</code>. | ||
| 283 | </p></dd></dl> | ||
| 284 | |||
| 285 | <dl class="def"> | ||
| 286 | <dt id="index-treesit_002dinduce_002dsparse_002dtree"><span class="category">Function: </span><span><strong>treesit-induce-sparse-tree</strong> <em>root predicate &optional process-fn limit</em><a href='#index-treesit_002dinduce_002dsparse_002dtree' class='copiable-anchor'> ¶</a></span></dt> | ||
| 287 | <dd><p>This function creates a sparse tree from <var>root</var>’s subtree. | ||
| 288 | </p> | ||
| 289 | <p>Basically, it takes the subtree under <var>root</var>, and combs it so only | ||
| 290 | the nodes that match <var>predicate</var> are left, like picking out grapes | ||
| 291 | on the vine. Like previous functions, <var>predicate</var> can be a regexp | ||
| 292 | string that matches against each node’s type case-insensitively, or a | ||
| 293 | function that takes a node and return nil/non-nil. | ||
| 294 | </p> | ||
| 295 | <p>For example, for a subtree on the left that consist of both numbers | ||
| 296 | and letters, if <var>predicate</var> is “letter only”, the returned tree | ||
| 297 | is the one on the right. | ||
| 298 | </p> | ||
| 299 | <div class="example"> | ||
| 300 | <pre class="example"> a a a | ||
| 301 | | | | | ||
| 302 | +---+---+ +---+---+ +---+---+ | ||
| 303 | | | | | | | | | | | ||
| 304 | b 1 2 b | | b c d | ||
| 305 | | | => | | => | | ||
| 306 | c +--+ c + e | ||
| 307 | | | | | | | ||
| 308 | +--+ d 4 +--+ d | ||
| 309 | | | | | ||
| 310 | e 5 e | ||
| 311 | </pre></div> | ||
| 312 | |||
| 313 | <p>If <var>process-fn</var> is non-nil, instead of returning the matched | ||
| 314 | nodes, this function passes each node to <var>process-fn</var> and uses the | ||
| 315 | returned value instead. If non-nil, <var>limit</var> is the number of | ||
| 316 | levels to go down from <var>root</var>. | ||
| 317 | </p> | ||
| 318 | <p>Each node in the returned tree looks like <code>(<var>tree-sitter | ||
| 319 | node</var> . (<var>child</var> ...))</code>. The <var>tree-sitter node</var> of the root | ||
| 320 | of this tree will be nil if <var>ROOT</var> doesn’t match <var>pred</var>. If | ||
| 321 | no node matches <var>predicate</var>, return nil. | ||
| 322 | </p></dd></dl> | ||
| 323 | |||
| 324 | <span id="More-convenient-functions"></span><h3 class="heading">More convenient functions</h3> | ||
| 325 | |||
| 326 | <dl class="def"> | ||
| 327 | <dt id="index-treesit_002dfilter_002dchild"><span class="category">Function: </span><span><strong>treesit-filter-child</strong> <em>node pred &optional named</em><a href='#index-treesit_002dfilter_002dchild' class='copiable-anchor'> ¶</a></span></dt> | ||
| 328 | <dd><p>This function finds immediate children of <var>node</var> that satisfies | ||
| 329 | <var>pred</var>. | ||
| 330 | </p> | ||
| 331 | <p>Function <var>pred</var> takes the child node as the argument and should | ||
| 332 | return non-nil to indicated keeping the child. If <var>named</var> | ||
| 333 | non-nil, this function only searches for named nodes. | ||
| 334 | </p></dd></dl> | ||
| 335 | |||
| 336 | <dl class="def"> | ||
| 337 | <dt id="index-treesit_002dparent_002duntil"><span class="category">Function: </span><span><strong>treesit-parent-until</strong> <em>node pred</em><a href='#index-treesit_002dparent_002duntil' class='copiable-anchor'> ¶</a></span></dt> | ||
| 338 | <dd><p>This function repeatedly finds the parent of <var>node</var>, and returns | ||
| 339 | the parent if it satisfies <var>pred</var> (which takes the parent as the | ||
| 340 | argument). If no parent satisfies <var>pred</var>, this function returns | ||
| 341 | nil. | ||
| 342 | </p></dd></dl> | ||
| 343 | |||
| 344 | <dl class="def"> | ||
| 345 | <dt id="index-treesit_002dparent_002dwhile"><span class="category">Function: </span><span><strong>treesit-parent-while</strong><a href='#index-treesit_002dparent_002dwhile' class='copiable-anchor'> ¶</a></span></dt> | ||
| 346 | <dd><p>This function repeatedly finds the parent of <var>node</var>, and keeps | ||
| 347 | doing so as long as the parent satisfies <var>pred</var> (which takes the | ||
| 348 | parent as the single argument). I.e., this function returns the | ||
| 349 | farthest parent that still satisfies <var>pred</var>. | ||
| 350 | </p></dd></dl> | ||
| 351 | |||
| 352 | </div> | ||
| 353 | <hr> | ||
| 354 | <div class="header"> | ||
| 355 | <p> | ||
| 356 | Next: <a href="Accessing-Node.html">Accessing Node Information</a>, Previous: <a href="Using-Parser.html">Using Tree-sitter Parser</a>, Up: <a href="Parsing-Program-Source.html">Parsing Program Source</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index.html" title="Index" rel="index">Index</a>]</p> | ||
| 357 | </div> | ||
| 358 | |||
| 359 | |||
| 360 | |||
| 361 | </body> | ||
| 362 | </html> | ||