feat(treesitter): expand the API

This commit is contained in:
Lewis Russell
2023-02-26 16:53:33 +00:00
committed by GitHub
parent ed58580dfe
commit 774e59f3f9
4 changed files with 160 additions and 19 deletions

View File

@@ -171,6 +171,14 @@ The following new APIs or features were added.
• |vim.treesitter.foldexpr()| can be used for 'foldexpr' to use treesitter for folding.
• Expanded the TSNode API with:
- |TSNode:tree()|
- |TSNode:has_changes()|
- |TSNode:extra()|
- |TSNode:equal()|
Additionally |TSNode:range()| now takes an optional {include_bytes} argument.
==============================================================================
CHANGED FEATURES *news-changes*

View File

@@ -136,9 +136,16 @@ TSNode:end_() *TSNode:end_()*
Get the node's end position. Return three values: the row, column and
total byte count (all zero-based).
TSNode:range() *TSNode:range()*
Get the range of the node. Return four values: the row, column of the
start position, then the row, column of the end position.
TSNode:range({include_bytes}) *TSNode:range()*
Get the range of the node.
Return four or six values:
- start row
- start column
- start byte (if {include_bytes} is `true`)
- end row
- end column
- end byte (if {include_bytes} is `true`)
TSNode:type() *TSNode:type()*
Get the node's type as a string.
@@ -155,6 +162,13 @@ TSNode:missing() *TSNode:missing()*
Check if the node is missing. Missing nodes are inserted by the parser in
order to recover from certain kinds of syntax errors.
TSNode:extra() *TSNode:extra()*
Check if the node is extra. Extra nodes represent things like comments,
which are not required by the grammar but can appear anywhere.
TSNode:has_changes() *TSNode:has_changes()*
Check if a syntax node has been edited.
TSNode:has_error() *TSNode:has_error()*
Check if the node is a syntax error or contains any syntax errors.
@@ -171,6 +185,8 @@ TSNode:id() *TSNode:id()*
Note: The `id` is not guaranteed to be unique for nodes from different
trees.
TSNode:tree() *TSNode:tree()*
Get the |TSTree| of the node.
*TSNode:descendant_for_range()*
TSNode:descendant_for_range({start_row}, {start_col}, {end_row}, {end_col})
Get the smallest node within this node that spans the given range of (row,
@@ -180,6 +196,9 @@ TSNode:descendant_for_range({start_row}, {start_col}, {end_row}, {end_col})
TSNode:named_descendant_for_range({start_row}, {start_col}, {end_row}, {end_col})
Get the smallest named node within this node that spans the given range of
(row, column) positions
*TSNode:equal()*
TSNode:equal({node})
Check if {node} refers to the same node within the same tree.
==============================================================================
TREESITTER QUERIES *treesitter-query*

View File

@@ -2,6 +2,7 @@
---@class TSNode
---@field id fun(self: TSNode): integer
---@field tree fun(self: TSNode): TSTree
---@field range fun(self: TSNode): integer, integer, integer, integer
---@field start fun(self: TSNode): integer, integer, integer
---@field end_ fun(self: TSNode): integer, integer, integer
@@ -9,6 +10,7 @@
---@field symbol fun(self: TSNode): integer
---@field named fun(self: TSNode): boolean
---@field missing fun(self: TSNode): boolean
---@field extra fun(self: TSNode): boolean
---@field child_count fun(self: TSNode): integer
---@field named_child_count fun(self: TSNode): integer
---@field child fun(self: TSNode, integer): TSNode
@@ -21,7 +23,8 @@
---@field next_named_sibling fun(self: TSNode): TSNode
---@field prev_named_sibling fun(self: TSNode): TSNode
---@field named_children fun(self: TSNode): TSNode[]
---@field has_error fun(self: TSNode): boolean
---@field has_changes fun(self: TSNode): boolean
---@field equal fun(self: TSNode, other: TSNode): boolean
---@field iter_children fun(self: TSNode): fun(): TSNode, string
local TSNode = {}
@@ -41,8 +44,11 @@ function TSNode:_rawquery(query, captures, start, end_) end
---@class TSParser
---@field parse fun(self: TSParser, tree, source: integer|string): TSTree, integer[]
---@field reset fun(self: TSParser)
---@field included_ranges fun(self: TSParser): integer[]
---@field set_included_ranges fun(self: TSParser, ranges: integer[][])
---@field set_timeout fun(self: TSParser, timeout: integer)
---@field timeout fun(self: TSParser): integer
---@class TSTree
---@field root fun(self: TSTree): TSNode