tsnode.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. ---@meta
  2. -- luacheck: no unused args
  3. error('Cannot require a meta file')
  4. --- @brief A "treesitter node" represents one specific element of the parsed contents of a buffer,
  5. --- which can be captured by a |Query| for, e.g., highlighting. It is a |userdata| reference to an
  6. --- object held by the treesitter library.
  7. ---
  8. --- An instance `TSNode` of a treesitter node supports the following methods.
  9. ---@nodoc
  10. ---@class TSNode: userdata
  11. local TSNode = {} -- luacheck: no unused
  12. --- Get the node's immediate parent.
  13. --- Prefer |TSNode:child_with_descendant()|
  14. --- for iterating over the node's ancestors.
  15. --- @return TSNode?
  16. function TSNode:parent() end
  17. --- Get the node's next sibling.
  18. --- @return TSNode?
  19. function TSNode:next_sibling() end
  20. --- Get the node's previous sibling.
  21. --- @return TSNode?
  22. function TSNode:prev_sibling() end
  23. --- Get the node's next named sibling.
  24. --- @return TSNode?
  25. function TSNode:next_named_sibling() end
  26. --- Get the node's previous named sibling.
  27. --- @return TSNode?
  28. function TSNode:prev_named_sibling() end
  29. --- Iterates over all the direct children of {TSNode}, regardless of whether
  30. --- they are named or not.
  31. --- Returns the child node plus the eventual field name corresponding to this
  32. --- child node.
  33. --- @return fun(): TSNode, string
  34. function TSNode:iter_children() end
  35. --- Returns a list of all the node's children that have the given field name.
  36. --- @param name string
  37. --- @return TSNode[]
  38. function TSNode:field(name) end
  39. --- Get the node's number of children.
  40. --- @return integer
  41. function TSNode:child_count() end
  42. --- Get the node's child at the given {index}, where zero represents the first
  43. --- child.
  44. --- @param index integer
  45. --- @return TSNode?
  46. function TSNode:child(index) end
  47. --- Get the node's number of named children.
  48. --- @return integer
  49. function TSNode:named_child_count() end
  50. --- Returns a list of the node's named children.
  51. --- @return TSNode[]
  52. function TSNode:named_children() end
  53. --- Check if the node has any of the given node types as its ancestor.
  54. --- @param node_types string[]
  55. --- @return boolean
  56. function TSNode:__has_ancestor(node_types) end
  57. --- Get the node's named child at the given {index}, where zero represents the
  58. --- first named child.
  59. --- @param index integer
  60. --- @return TSNode?
  61. function TSNode:named_child(index) end
  62. --- Get the node's child that contains {descendant} (includes {descendant}).
  63. ---
  64. --- For example, with the following node hierarchy:
  65. ---
  66. --- ```
  67. --- a -> b -> c
  68. ---
  69. --- a:child_with_descendant(c) == b
  70. --- a:child_with_descendant(b) == b
  71. --- a:child_with_descendant(a) == nil
  72. --- ```
  73. --- @param descendant TSNode
  74. --- @return TSNode?
  75. function TSNode:child_with_descendant(descendant) end
  76. --- Get the node's start position. Return three values: the row, column and
  77. --- total byte count (all zero-based).
  78. --- @return integer, integer, integer
  79. function TSNode:start() end
  80. --- Get the node's end position. Return three values: the row, column and
  81. --- total byte count (all zero-based).
  82. --- @return integer, integer, integer
  83. function TSNode:end_() end
  84. --- Get the range of the node.
  85. ---
  86. --- Return four or six values:
  87. ---
  88. --- - start row
  89. --- - start column
  90. --- - start byte (if {include_bytes} is `true`)
  91. --- - end row
  92. --- - end column
  93. --- - end byte (if {include_bytes} is `true`)
  94. --- @param include_bytes false?
  95. --- @return integer, integer, integer, integer
  96. --- @overload fun(self: TSNode, include_bytes: true): integer, integer, integer, integer, integer, integer
  97. function TSNode:range(include_bytes) end
  98. --- Get the node's type as a string.
  99. --- @return string
  100. function TSNode:type() end
  101. --- Get the node's type as a numerical id.
  102. --- @return integer
  103. function TSNode:symbol() end
  104. --- Check if the node is named. Named nodes correspond to named rules in the
  105. --- grammar, whereas anonymous nodes correspond to string literals in the
  106. --- grammar.
  107. --- @return boolean
  108. function TSNode:named() end
  109. --- Check if the node is missing. Missing nodes are inserted by the parser in
  110. --- order to recover from certain kinds of syntax errors.
  111. --- @return boolean
  112. function TSNode:missing() end
  113. --- Check if the node is extra. Extra nodes represent things like comments,
  114. --- which are not required by the grammar but can appear anywhere.
  115. --- @return boolean
  116. function TSNode:extra() end
  117. --- Check if a syntax node has been edited.
  118. --- @return boolean
  119. function TSNode:has_changes() end
  120. --- Check if the node is a syntax error or contains any syntax errors.
  121. --- @return boolean
  122. function TSNode:has_error() end
  123. --- Get an S-expression representing the node as a string.
  124. --- @return string
  125. function TSNode:sexpr() end
  126. --- Get a unique identifier for the node inside its own tree.
  127. ---
  128. --- No guarantees are made about this identifier's internal representation,
  129. --- except for being a primitive Lua type with value equality (so not a
  130. --- table). Presently it is a (non-printable) string.
  131. ---
  132. --- Note: The `id` is not guaranteed to be unique for nodes from different
  133. --- trees.
  134. --- @return string
  135. function TSNode:id() end
  136. --- Get the |TSTree| of the node.
  137. --- @return TSTree
  138. function TSNode:tree() end
  139. --- Get the smallest node within this node that spans the given range of (row,
  140. --- column) positions
  141. --- @param start_row integer
  142. --- @param start_col integer
  143. --- @param end_row integer
  144. --- @param end_col integer
  145. --- @return TSNode?
  146. function TSNode:descendant_for_range(start_row, start_col, end_row, end_col) end
  147. --- Get the smallest named node within this node that spans the given range of
  148. --- (row, column) positions
  149. --- @param start_row integer
  150. --- @param start_col integer
  151. --- @param end_row integer
  152. --- @param end_col integer
  153. --- @return TSNode?
  154. function TSNode:named_descendant_for_range(start_row, start_col, end_row, end_col) end
  155. --- Check if {node} refers to the same node within the same tree.
  156. --- @param node TSNode
  157. --- @return boolean
  158. function TSNode:equal(node) end
  159. --- Return the number of bytes spanned by this node.
  160. --- @return integer
  161. function TSNode:byte_length() end