2022-12-08 09:22:57 -07:00
|
|
|
local api = vim.api
|
|
|
|
|
2023-04-01 04:55:04 -07:00
|
|
|
local M = {}
|
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
---@class (private) vim.treesitter.dev.TSTreeView
|
2023-03-04 06:04:05 -07:00
|
|
|
---@field ns integer API namespace
|
2024-02-27 08:20:32 -07:00
|
|
|
---@field opts vim.treesitter.dev.TSTreeViewOpts
|
|
|
|
---@field nodes vim.treesitter.dev.Node[]
|
|
|
|
---@field named vim.treesitter.dev.Node[]
|
2023-06-06 08:23:20 -07:00
|
|
|
local TSTreeView = {}
|
2023-03-23 04:23:51 -07:00
|
|
|
|
2024-02-01 13:34:35 -07:00
|
|
|
---@private
|
2024-02-27 08:20:32 -07:00
|
|
|
---@class (private) vim.treesitter.dev.TSTreeViewOpts
|
2024-02-01 13:34:35 -07:00
|
|
|
---@field anon boolean If true, display anonymous nodes.
|
|
|
|
---@field lang boolean If true, display the language alongside each node.
|
|
|
|
---@field indent number Number of spaces to indent nested lines.
|
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
---@class (private) vim.treesitter.dev.Node
|
2024-01-28 18:53:14 -07:00
|
|
|
---@field node TSNode Treesitter node
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
---@field field string? Node field
|
|
|
|
---@field depth integer Depth of this node in the tree
|
|
|
|
---@field text string? Text displayed in the inspector for this node. Not computed until the
|
|
|
|
--- inspector is drawn.
|
2022-12-08 09:22:57 -07:00
|
|
|
---@field lang string Source language of this node
|
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
---@class (private) vim.treesitter.dev.Injection
|
2023-11-29 07:59:36 -07:00
|
|
|
---@field lang string Source language of this injection
|
|
|
|
---@field root TSNode Root node of the injection
|
|
|
|
|
2022-12-08 09:22:57 -07:00
|
|
|
--- Traverse all child nodes starting at {node}.
|
|
|
|
---
|
|
|
|
--- This is a recursive function. The {depth} parameter indicates the current recursion level.
|
|
|
|
--- {lang} is a string indicating the language of the tree currently being traversed. Each traversed
|
|
|
|
--- node is added to {tree}. When recursion completes, {tree} is an array of all nodes in the order
|
|
|
|
--- they were visited.
|
|
|
|
---
|
|
|
|
--- {injections} is a table mapping node ids from the primary tree to language tree injections. Each
|
|
|
|
--- injected language has a series of trees nested within the primary language's tree, and the root
|
|
|
|
--- node of each of these trees is contained within a node in the primary tree. The {injections}
|
|
|
|
--- table maps nodes in the primary tree to root nodes of injected trees.
|
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param node TSNode Starting node to begin traversal |tsnode|
|
2023-03-04 06:04:05 -07:00
|
|
|
---@param depth integer Current recursion depth
|
2024-02-06 13:51:53 -07:00
|
|
|
---@param field string|nil The field of the current node
|
2022-12-08 09:22:57 -07:00
|
|
|
---@param lang string Language of the tree currently being traversed
|
2024-02-27 08:20:32 -07:00
|
|
|
---@param injections table<string, vim.treesitter.dev.Injection> Mapping of node ids to root nodes
|
2023-11-29 07:59:36 -07:00
|
|
|
--- of injected language trees (see explanation above)
|
2024-02-27 08:20:32 -07:00
|
|
|
---@param tree vim.treesitter.dev.Node[] Output table containing a list of tables each representing a node in the tree
|
2024-02-06 13:51:53 -07:00
|
|
|
local function traverse(node, depth, field, lang, injections, tree)
|
|
|
|
table.insert(tree, {
|
|
|
|
node = node,
|
|
|
|
depth = depth,
|
|
|
|
lang = lang,
|
|
|
|
field = field,
|
|
|
|
})
|
|
|
|
|
2022-12-08 09:22:57 -07:00
|
|
|
local injection = injections[node:id()]
|
|
|
|
if injection then
|
2024-02-06 13:51:53 -07:00
|
|
|
traverse(injection.root, depth + 1, nil, injection.lang, injections, tree)
|
2022-12-08 09:22:57 -07:00
|
|
|
end
|
|
|
|
|
2024-02-06 13:51:53 -07:00
|
|
|
for child, child_field in node:iter_children() do
|
|
|
|
traverse(child, depth + 1, child_field, lang, injections, tree)
|
2022-12-08 09:22:57 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
return tree
|
|
|
|
end
|
|
|
|
|
2023-06-06 08:23:20 -07:00
|
|
|
--- Create a new treesitter view.
|
2022-12-08 09:22:57 -07:00
|
|
|
---
|
2023-02-04 07:58:38 -07:00
|
|
|
---@param bufnr integer Source buffer number
|
2022-12-08 09:51:46 -07:00
|
|
|
---@param lang string|nil Language of source buffer
|
2022-12-08 09:22:57 -07:00
|
|
|
---
|
2024-02-27 08:20:32 -07:00
|
|
|
---@return vim.treesitter.dev.TSTreeView|nil
|
2022-12-08 09:22:57 -07:00
|
|
|
---@return string|nil Error message, if any
|
|
|
|
---
|
2023-03-23 04:23:51 -07:00
|
|
|
---@package
|
2023-06-06 08:23:20 -07:00
|
|
|
function TSTreeView:new(bufnr, lang)
|
2024-09-14 12:57:33 -07:00
|
|
|
local parser = vim.treesitter.get_parser(bufnr or 0, lang, { error = false })
|
2024-09-13 05:09:11 -07:00
|
|
|
if not parser then
|
2024-09-14 12:57:33 -07:00
|
|
|
return nil,
|
|
|
|
string.format(
|
|
|
|
'Failed to create TSTreeView for buffer %s: no parser for lang "%s"',
|
|
|
|
bufnr,
|
|
|
|
lang
|
|
|
|
)
|
2022-12-08 09:22:57 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
-- For each child tree (injected language), find the root of the tree and locate the node within
|
|
|
|
-- the primary tree that contains that root. Add a mapping from the node in the primary tree to
|
|
|
|
-- the root in the child tree to the {injections} table.
|
2023-08-10 06:21:56 -07:00
|
|
|
local root = parser:parse(true)[1]:root()
|
2024-02-27 08:20:32 -07:00
|
|
|
local injections = {} ---@type table<string, vim.treesitter.dev.Injection>
|
2023-11-29 07:16:52 -07:00
|
|
|
|
|
|
|
parser:for_each_tree(function(parent_tree, parent_ltree)
|
|
|
|
local parent = parent_tree:root()
|
|
|
|
for _, child in pairs(parent_ltree:children()) do
|
2023-11-30 09:37:42 -07:00
|
|
|
for _, tree in pairs(child:trees()) do
|
2023-11-29 07:16:52 -07:00
|
|
|
local r = tree:root()
|
|
|
|
local node = assert(parent:named_descendant_for_range(r:range()))
|
|
|
|
local id = node:id()
|
|
|
|
if not injections[id] or r:byte_length() > injections[id].root:byte_length() then
|
|
|
|
injections[id] = {
|
2023-11-30 09:37:42 -07:00
|
|
|
lang = child:lang(),
|
2023-11-29 07:16:52 -07:00
|
|
|
root = r,
|
|
|
|
}
|
|
|
|
end
|
2023-11-30 09:37:42 -07:00
|
|
|
end
|
2023-11-29 07:16:52 -07:00
|
|
|
end
|
|
|
|
end)
|
2022-12-08 09:22:57 -07:00
|
|
|
|
2024-02-06 13:51:53 -07:00
|
|
|
local nodes = traverse(root, 0, nil, parser:lang(), injections, {})
|
2022-12-08 09:22:57 -07:00
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
local named = {} ---@type vim.treesitter.dev.Node[]
|
2022-12-08 09:22:57 -07:00
|
|
|
for _, v in ipairs(nodes) do
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
if v.node:named() then
|
2022-12-08 09:22:57 -07:00
|
|
|
named[#named + 1] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local t = {
|
2023-08-25 11:17:36 -07:00
|
|
|
ns = api.nvim_create_namespace('treesitter/dev-inspect'),
|
2022-12-08 09:22:57 -07:00
|
|
|
nodes = nodes,
|
|
|
|
named = named,
|
2024-02-27 08:20:32 -07:00
|
|
|
---@type vim.treesitter.dev.TSTreeViewOpts
|
2022-12-08 09:22:57 -07:00
|
|
|
opts = {
|
|
|
|
anon = false,
|
|
|
|
lang = false,
|
2023-11-29 09:17:53 -07:00
|
|
|
indent = 2,
|
2022-12-08 09:22:57 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
setmetatable(t, self)
|
|
|
|
self.__index = self
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2023-06-06 08:23:20 -07:00
|
|
|
local decor_ns = api.nvim_create_namespace('ts.dev')
|
2023-02-09 08:20:47 -07:00
|
|
|
|
2024-02-01 13:34:35 -07:00
|
|
|
---@param range Range4
|
2023-02-27 03:49:19 -07:00
|
|
|
---@return string
|
2024-02-01 13:34:35 -07:00
|
|
|
local function range_to_string(range)
|
|
|
|
---@type integer, integer, integer, integer
|
|
|
|
local row, col, end_row, end_col = unpack(range)
|
|
|
|
return string.format('[%d, %d] - [%d, %d]', row, col, end_row, end_col)
|
2023-02-27 03:49:19 -07:00
|
|
|
end
|
|
|
|
|
2023-08-25 11:17:36 -07:00
|
|
|
---@param w integer
|
|
|
|
---@return boolean closed Whether the window was closed.
|
|
|
|
local function close_win(w)
|
|
|
|
if api.nvim_win_is_valid(w) then
|
|
|
|
api.nvim_win_close(w, true)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param w integer
|
|
|
|
---@param b integer
|
2024-10-01 09:07:30 -07:00
|
|
|
---@param opts nil|{ indent?: integer }
|
|
|
|
local function set_dev_options(w, b, opts)
|
2023-08-25 11:17:36 -07:00
|
|
|
vim.wo[w].scrolloff = 5
|
|
|
|
vim.wo[w].wrap = false
|
2024-02-22 13:58:59 -07:00
|
|
|
vim.wo[w].foldmethod = 'expr'
|
|
|
|
vim.wo[w].foldexpr = 'v:lua.vim.treesitter.foldexpr()' -- explicitly set foldexpr
|
|
|
|
vim.wo[w].foldenable = false -- Don't fold on first open InspectTree
|
|
|
|
vim.wo[w].foldlevel = 99
|
2023-08-25 11:17:36 -07:00
|
|
|
vim.bo[b].buflisted = false
|
|
|
|
vim.bo[b].buftype = 'nofile'
|
|
|
|
vim.bo[b].bufhidden = 'wipe'
|
|
|
|
vim.bo[b].filetype = 'query'
|
2024-09-27 03:27:00 -07:00
|
|
|
vim.bo[b].swapfile = false
|
2024-10-01 09:07:30 -07:00
|
|
|
|
|
|
|
opts = opts or {}
|
|
|
|
if opts.indent then
|
|
|
|
vim.bo[b].shiftwidth = opts.indent
|
|
|
|
end
|
2023-08-25 11:17:36 -07:00
|
|
|
end
|
|
|
|
|
2023-10-18 21:18:24 -07:00
|
|
|
--- Updates the cursor position in the inspector to match the node under the cursor.
|
|
|
|
---
|
2024-02-27 08:20:32 -07:00
|
|
|
--- @param treeview vim.treesitter.dev.TSTreeView
|
2023-10-18 21:18:24 -07:00
|
|
|
--- @param lang string
|
|
|
|
--- @param source_buf integer
|
|
|
|
--- @param inspect_buf integer
|
|
|
|
--- @param inspect_win integer
|
2024-06-04 06:06:02 -07:00
|
|
|
--- @param pos? [integer, integer]
|
2023-11-29 07:10:02 -07:00
|
|
|
local function set_inspector_cursor(treeview, lang, source_buf, inspect_buf, inspect_win, pos)
|
|
|
|
api.nvim_buf_clear_namespace(inspect_buf, treeview.ns, 0, -1)
|
2023-10-18 21:18:24 -07:00
|
|
|
|
|
|
|
local cursor_node = vim.treesitter.get_node({
|
|
|
|
bufnr = source_buf,
|
|
|
|
lang = lang,
|
|
|
|
pos = pos,
|
|
|
|
ignore_injections = false,
|
2024-07-28 13:42:18 -07:00
|
|
|
include_anonymous = treeview.opts.anon,
|
2023-10-18 21:18:24 -07:00
|
|
|
})
|
|
|
|
if not cursor_node then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local cursor_node_id = cursor_node:id()
|
2023-11-29 07:10:02 -07:00
|
|
|
for i, v in treeview:iter() do
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
if v.node:id() == cursor_node_id then
|
2023-11-29 09:17:53 -07:00
|
|
|
local start = v.depth * treeview.opts.indent ---@type integer
|
2023-10-18 21:18:24 -07:00
|
|
|
local end_col = start + #v.text
|
2023-11-29 07:10:02 -07:00
|
|
|
api.nvim_buf_set_extmark(inspect_buf, treeview.ns, i - 1, start, {
|
2023-10-18 21:18:24 -07:00
|
|
|
end_col = end_col,
|
|
|
|
hl_group = 'Visual',
|
|
|
|
})
|
|
|
|
api.nvim_win_set_cursor(inspect_win, { i, 0 })
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-06 08:23:20 -07:00
|
|
|
--- Write the contents of this View into {bufnr}.
|
2022-12-08 09:22:57 -07:00
|
|
|
---
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
--- Calling this function computes the text that is displayed for each node.
|
|
|
|
---
|
2023-03-04 06:04:05 -07:00
|
|
|
---@param bufnr integer Buffer number to write into.
|
2023-03-23 04:23:51 -07:00
|
|
|
---@package
|
2023-06-06 08:23:20 -07:00
|
|
|
function TSTreeView:draw(bufnr)
|
2022-12-08 09:22:57 -07:00
|
|
|
vim.bo[bufnr].modifiable = true
|
2023-02-04 07:58:38 -07:00
|
|
|
local lines = {} ---@type string[]
|
2023-02-27 03:49:19 -07:00
|
|
|
local lang_hl_marks = {} ---@type table[]
|
|
|
|
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
for i, item in self:iter() do
|
2024-02-01 13:34:35 -07:00
|
|
|
local range_str = range_to_string({ item.node:range() })
|
2023-02-27 03:49:19 -07:00
|
|
|
local lang_str = self.opts.lang and string.format(' %s', item.lang) or ''
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
|
|
|
|
local text ---@type string
|
|
|
|
if item.node:named() then
|
2024-07-04 10:09:19 -07:00
|
|
|
text = string.format('(%s', item.node:type())
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
else
|
2024-05-03 09:34:02 -07:00
|
|
|
text = string.format('%q', item.node:type()):gsub('\n', 'n')
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
end
|
2024-07-04 10:09:19 -07:00
|
|
|
if item.field then
|
|
|
|
text = string.format('%s: %s', item.field, text)
|
|
|
|
end
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
|
|
|
|
local next = self:get(i + 1)
|
|
|
|
if not next or next.depth <= item.depth then
|
|
|
|
local parens = item.depth - (next and next.depth or 0) + (item.node:named() and 1 or 0)
|
|
|
|
if parens > 0 then
|
|
|
|
text = string.format('%s%s', text, string.rep(')', parens))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
item.text = text
|
|
|
|
|
2023-11-29 09:17:53 -07:00
|
|
|
local line = string.format(
|
|
|
|
'%s%s ; %s%s',
|
|
|
|
string.rep(' ', item.depth * self.opts.indent),
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
text,
|
2023-11-29 09:17:53 -07:00
|
|
|
range_str,
|
|
|
|
lang_str
|
|
|
|
)
|
2023-02-27 03:49:19 -07:00
|
|
|
|
|
|
|
if self.opts.lang then
|
|
|
|
lang_hl_marks[#lang_hl_marks + 1] = {
|
|
|
|
col = #line - #lang_str,
|
|
|
|
end_col = #line,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
lines[i] = line
|
2022-12-08 09:22:57 -07:00
|
|
|
end
|
2023-02-27 03:49:19 -07:00
|
|
|
|
2022-12-08 09:22:57 -07:00
|
|
|
api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
|
2023-02-09 08:20:47 -07:00
|
|
|
|
|
|
|
api.nvim_buf_clear_namespace(bufnr, decor_ns, 0, -1)
|
|
|
|
|
2023-02-27 03:49:19 -07:00
|
|
|
for i, m in ipairs(lang_hl_marks) do
|
|
|
|
api.nvim_buf_set_extmark(bufnr, decor_ns, i - 1, m.col, {
|
|
|
|
hl_group = 'Title',
|
|
|
|
end_col = m.end_col,
|
2023-02-09 08:20:47 -07:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2022-12-08 09:22:57 -07:00
|
|
|
vim.bo[bufnr].modifiable = false
|
|
|
|
end
|
|
|
|
|
2023-06-06 08:23:20 -07:00
|
|
|
--- Get node {i} from this View.
|
2022-12-08 09:22:57 -07:00
|
|
|
---
|
|
|
|
--- The node number is dependent on whether or not anonymous nodes are displayed.
|
|
|
|
---
|
2023-03-04 06:04:05 -07:00
|
|
|
---@param i integer Node number to get
|
2024-02-27 08:20:32 -07:00
|
|
|
---@return vim.treesitter.dev.Node
|
2023-03-23 04:23:51 -07:00
|
|
|
---@package
|
2023-06-06 08:23:20 -07:00
|
|
|
function TSTreeView:get(i)
|
2022-12-08 09:22:57 -07:00
|
|
|
local t = self.opts.anon and self.nodes or self.named
|
|
|
|
return t[i]
|
|
|
|
end
|
|
|
|
|
2023-06-06 08:23:20 -07:00
|
|
|
--- Iterate over all of the nodes in this View.
|
2022-12-08 09:22:57 -07:00
|
|
|
---
|
2024-02-27 08:20:32 -07:00
|
|
|
---@return (fun(): integer, vim.treesitter.dev.Node) Iterator over all nodes in this View
|
2022-12-08 09:22:57 -07:00
|
|
|
---@return table
|
2023-03-04 06:04:05 -07:00
|
|
|
---@return integer
|
2023-03-23 04:23:51 -07:00
|
|
|
---@package
|
2023-06-06 08:23:20 -07:00
|
|
|
function TSTreeView:iter()
|
2022-12-08 09:22:57 -07:00
|
|
|
return ipairs(self.opts.anon and self.nodes or self.named)
|
|
|
|
end
|
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
--- @class vim.treesitter.dev.inspect_tree.Opts
|
|
|
|
--- @inlinedoc
|
|
|
|
---
|
|
|
|
--- The language of the source buffer. If omitted, the filetype of the source
|
|
|
|
--- buffer is used.
|
|
|
|
--- @field lang string?
|
|
|
|
---
|
|
|
|
--- Buffer to draw the tree into. If omitted, a new buffer is created.
|
|
|
|
--- @field bufnr integer?
|
|
|
|
---
|
|
|
|
--- Window id to display the tree buffer in. If omitted, a new window is
|
|
|
|
--- created with {command}.
|
|
|
|
--- @field winid integer?
|
|
|
|
---
|
|
|
|
--- Vimscript command to create the window. Default value is "60vnew".
|
|
|
|
--- Only used when {winid} is nil.
|
|
|
|
--- @field command string?
|
|
|
|
---
|
|
|
|
--- Title of the window. If a function, it accepts the buffer number of the
|
|
|
|
--- source buffer as its only argument and should return a string.
|
|
|
|
--- @field title (string|fun(bufnr:integer):string|nil)
|
2023-04-01 04:55:04 -07:00
|
|
|
|
2023-06-06 08:23:20 -07:00
|
|
|
--- @private
|
|
|
|
---
|
2024-02-27 08:20:32 -07:00
|
|
|
--- @param opts vim.treesitter.dev.inspect_tree.Opts?
|
2023-04-01 04:55:04 -07:00
|
|
|
function M.inspect_tree(opts)
|
|
|
|
vim.validate({
|
|
|
|
opts = { opts, 't', true },
|
|
|
|
})
|
|
|
|
|
|
|
|
opts = opts or {}
|
|
|
|
|
2024-05-27 04:20:03 -07:00
|
|
|
-- source buffer
|
2023-04-01 04:55:04 -07:00
|
|
|
local buf = api.nvim_get_current_buf()
|
2024-05-27 04:20:03 -07:00
|
|
|
|
|
|
|
-- window id for source buffer
|
2023-04-01 04:55:04 -07:00
|
|
|
local win = api.nvim_get_current_win()
|
2023-11-29 07:10:02 -07:00
|
|
|
local treeview = assert(TSTreeView:new(buf, opts.lang))
|
2023-04-01 04:55:04 -07:00
|
|
|
|
2023-08-25 11:17:36 -07:00
|
|
|
-- Close any existing inspector window
|
|
|
|
if vim.b[buf].dev_inspect then
|
|
|
|
close_win(vim.b[buf].dev_inspect)
|
2023-04-01 04:55:04 -07:00
|
|
|
end
|
|
|
|
|
2024-05-27 04:20:03 -07:00
|
|
|
-- window id for tree buffer
|
2023-04-01 04:55:04 -07:00
|
|
|
local w = opts.winid
|
|
|
|
if not w then
|
|
|
|
vim.cmd(opts.command or '60vnew')
|
|
|
|
w = api.nvim_get_current_win()
|
|
|
|
end
|
|
|
|
|
2024-05-27 04:20:03 -07:00
|
|
|
-- tree buffer
|
2023-04-01 04:55:04 -07:00
|
|
|
local b = opts.bufnr
|
|
|
|
if b then
|
|
|
|
api.nvim_win_set_buf(w, b)
|
|
|
|
else
|
|
|
|
b = api.nvim_win_get_buf(w)
|
|
|
|
end
|
|
|
|
|
2023-08-25 11:17:36 -07:00
|
|
|
vim.b[buf].dev_inspect = w
|
|
|
|
vim.b[b].dev_base = win -- base window handle
|
2023-04-29 09:22:26 -07:00
|
|
|
vim.b[b].disable_query_linter = true
|
2024-10-01 09:07:30 -07:00
|
|
|
set_dev_options(w, b, { indent = treeview.opts.indent })
|
2023-04-01 04:55:04 -07:00
|
|
|
|
|
|
|
local title --- @type string?
|
|
|
|
local opts_title = opts.title
|
|
|
|
if not opts_title then
|
|
|
|
local bufname = api.nvim_buf_get_name(buf)
|
|
|
|
title = string.format('Syntax tree for %s', vim.fn.fnamemodify(bufname, ':.'))
|
|
|
|
elseif type(opts_title) == 'function' then
|
|
|
|
title = opts_title(buf)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert(type(title) == 'string', 'Window title must be a string')
|
|
|
|
api.nvim_buf_set_name(b, title)
|
|
|
|
|
2023-11-29 07:10:02 -07:00
|
|
|
treeview:draw(b)
|
2023-04-01 04:55:04 -07:00
|
|
|
|
2023-10-18 21:18:24 -07:00
|
|
|
local cursor = api.nvim_win_get_cursor(win)
|
2023-11-29 07:10:02 -07:00
|
|
|
set_inspector_cursor(treeview, opts.lang, buf, b, w, { cursor[1] - 1, cursor[2] })
|
2023-10-18 21:18:24 -07:00
|
|
|
|
2023-11-29 07:10:02 -07:00
|
|
|
api.nvim_buf_clear_namespace(buf, treeview.ns, 0, -1)
|
2023-04-01 04:55:04 -07:00
|
|
|
api.nvim_buf_set_keymap(b, 'n', '<CR>', '', {
|
|
|
|
desc = 'Jump to the node under the cursor in the source buffer',
|
|
|
|
callback = function()
|
|
|
|
local row = api.nvim_win_get_cursor(w)[1]
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
local lnum, col = treeview:get(row).node:start()
|
2024-05-27 04:20:03 -07:00
|
|
|
|
|
|
|
-- update source window if original was closed
|
|
|
|
if not api.nvim_win_is_valid(win) then
|
|
|
|
win = vim.fn.win_findbuf(buf)[1]
|
|
|
|
end
|
|
|
|
|
2023-04-01 04:55:04 -07:00
|
|
|
api.nvim_set_current_win(win)
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
api.nvim_win_set_cursor(win, { lnum + 1, col })
|
2023-04-01 04:55:04 -07:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
api.nvim_buf_set_keymap(b, 'n', 'a', '', {
|
|
|
|
desc = 'Toggle anonymous nodes',
|
|
|
|
callback = function()
|
2023-08-25 11:17:36 -07:00
|
|
|
local row, col = unpack(api.nvim_win_get_cursor(w)) ---@type integer, integer
|
2023-11-29 07:10:02 -07:00
|
|
|
local curnode = treeview:get(row)
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
while curnode and not curnode.node:named() do
|
2023-04-01 04:55:04 -07:00
|
|
|
row = row - 1
|
2023-11-29 07:10:02 -07:00
|
|
|
curnode = treeview:get(row)
|
2023-04-01 04:55:04 -07:00
|
|
|
end
|
|
|
|
|
2023-11-29 07:10:02 -07:00
|
|
|
treeview.opts.anon = not treeview.opts.anon
|
|
|
|
treeview:draw(b)
|
2023-04-01 04:55:04 -07:00
|
|
|
|
|
|
|
if not curnode then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
local id = curnode.node:id()
|
2023-11-29 07:10:02 -07:00
|
|
|
for i, node in treeview:iter() do
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
if node.node:id() == id then
|
2023-04-01 04:55:04 -07:00
|
|
|
api.nvim_win_set_cursor(w, { i, col })
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
api.nvim_buf_set_keymap(b, 'n', 'I', '', {
|
|
|
|
desc = 'Toggle language display',
|
|
|
|
callback = function()
|
2023-11-29 07:10:02 -07:00
|
|
|
treeview.opts.lang = not treeview.opts.lang
|
|
|
|
treeview:draw(b)
|
2023-04-01 04:55:04 -07:00
|
|
|
end,
|
|
|
|
})
|
2023-08-25 11:17:36 -07:00
|
|
|
api.nvim_buf_set_keymap(b, 'n', 'o', '', {
|
2023-09-15 03:10:55 -07:00
|
|
|
desc = 'Toggle query editor',
|
2023-08-25 11:17:36 -07:00
|
|
|
callback = function()
|
2023-09-15 03:10:55 -07:00
|
|
|
local edit_w = vim.b[buf].dev_edit
|
|
|
|
if not edit_w or not close_win(edit_w) then
|
|
|
|
M.edit_query()
|
2023-08-25 11:17:36 -07:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
2023-04-01 04:55:04 -07:00
|
|
|
|
2023-06-06 08:23:20 -07:00
|
|
|
local group = api.nvim_create_augroup('treesitter/dev', {})
|
2023-04-01 04:55:04 -07:00
|
|
|
|
|
|
|
api.nvim_create_autocmd('CursorMoved', {
|
|
|
|
group = group,
|
|
|
|
buffer = b,
|
|
|
|
callback = function()
|
2023-10-17 13:34:39 -07:00
|
|
|
if not api.nvim_buf_is_loaded(buf) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2024-05-27 04:20:03 -07:00
|
|
|
w = api.nvim_get_current_win()
|
2023-11-29 07:10:02 -07:00
|
|
|
api.nvim_buf_clear_namespace(buf, treeview.ns, 0, -1)
|
2023-04-01 04:55:04 -07:00
|
|
|
local row = api.nvim_win_get_cursor(w)[1]
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
local lnum, col, end_lnum, end_col = treeview:get(row).node:range()
|
|
|
|
api.nvim_buf_set_extmark(buf, treeview.ns, lnum, col, {
|
|
|
|
end_row = end_lnum,
|
|
|
|
end_col = math.max(0, end_col),
|
2023-04-01 04:55:04 -07:00
|
|
|
hl_group = 'Visual',
|
|
|
|
})
|
|
|
|
|
2024-05-27 04:20:03 -07:00
|
|
|
-- update source window if original was closed
|
|
|
|
if not api.nvim_win_is_valid(win) then
|
|
|
|
win = vim.fn.win_findbuf(buf)[1]
|
|
|
|
end
|
|
|
|
|
2023-04-01 04:55:04 -07:00
|
|
|
local topline, botline = vim.fn.line('w0', win), vim.fn.line('w$', win)
|
|
|
|
|
|
|
|
-- Move the cursor if highlighted range is completely out of view
|
fix(treesitter): fix parens stacking in inspector display (#26304)
When first opened, the tree-sitter inspector traverses all of the nodes
in the buffer to calculate an array of nodes. This traversal is done
only once, and _all_ nodes (both named and anonymous) are included.
Toggling anonymous nodes in the inspector only changes how the tree is
drawn in the buffer, but does not affect the underlying data structure
at all.
When the buffer is traversed and the list of nodes is calculated, we
don't know whether or not anonymous nodes will be displayed in the
inspector or not. Thus, we cannot determine during traversal where to
put closing parentheses. Instead, this must be done when drawing.
When we draw, the tree structure has been flatted into a single array,
so we lose parent-child relationships that would otherwise make
determining the number of closing parentheses straightforward. However,
we can instead rely on the fact that a delta between the depth of a node
and the depth of the successive node _must_ mean that more closing
parentheses are required:
(foo
(bar)
(baz) ↑
│
└ (bar) and (baz) have different depths, so (bar) must have an
extra closing parenthesis
This does not depend on whether or not anonymous nodes are displayed and
so works in both cases.
2023-11-30 06:04:20 -07:00
|
|
|
if lnum < topline and end_lnum < topline then
|
|
|
|
api.nvim_win_set_cursor(win, { end_lnum + 1, 0 })
|
|
|
|
elseif lnum > botline and end_lnum > botline then
|
|
|
|
api.nvim_win_set_cursor(win, { lnum + 1, 0 })
|
2023-04-01 04:55:04 -07:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
api.nvim_create_autocmd('CursorMoved', {
|
|
|
|
group = group,
|
|
|
|
buffer = buf,
|
|
|
|
callback = function()
|
|
|
|
if not api.nvim_buf_is_loaded(b) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2023-11-29 07:10:02 -07:00
|
|
|
set_inspector_cursor(treeview, opts.lang, buf, b, w)
|
2023-04-01 04:55:04 -07:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
api.nvim_create_autocmd({ 'TextChanged', 'InsertLeave' }, {
|
|
|
|
group = group,
|
|
|
|
buffer = buf,
|
|
|
|
callback = function()
|
|
|
|
if not api.nvim_buf_is_loaded(b) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2024-02-04 04:17:21 -07:00
|
|
|
local treeview_opts = treeview.opts
|
2023-11-29 07:10:02 -07:00
|
|
|
treeview = assert(TSTreeView:new(buf, opts.lang))
|
2024-02-04 04:17:21 -07:00
|
|
|
treeview.opts = treeview_opts
|
2023-11-29 07:10:02 -07:00
|
|
|
treeview:draw(b)
|
2023-04-01 04:55:04 -07:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
api.nvim_create_autocmd('BufLeave', {
|
|
|
|
group = group,
|
|
|
|
buffer = b,
|
|
|
|
callback = function()
|
2023-10-17 13:34:39 -07:00
|
|
|
if not api.nvim_buf_is_loaded(buf) then
|
|
|
|
return true
|
|
|
|
end
|
2023-11-29 07:10:02 -07:00
|
|
|
api.nvim_buf_clear_namespace(buf, treeview.ns, 0, -1)
|
2023-04-01 04:55:04 -07:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
api.nvim_create_autocmd('BufLeave', {
|
|
|
|
group = group,
|
|
|
|
buffer = buf,
|
|
|
|
callback = function()
|
|
|
|
if not api.nvim_buf_is_loaded(b) then
|
|
|
|
return true
|
|
|
|
end
|
2023-11-29 07:10:02 -07:00
|
|
|
api.nvim_buf_clear_namespace(b, treeview.ns, 0, -1)
|
2023-04-01 04:55:04 -07:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
api.nvim_create_autocmd('BufHidden', {
|
|
|
|
group = group,
|
|
|
|
buffer = buf,
|
|
|
|
once = true,
|
|
|
|
callback = function()
|
2024-05-27 04:20:03 -07:00
|
|
|
-- close all tree windows
|
|
|
|
for _, window in pairs(vim.fn.win_findbuf(b)) do
|
|
|
|
close_win(window)
|
|
|
|
end
|
2023-08-25 11:17:36 -07:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2023-09-15 03:10:55 -07:00
|
|
|
local edit_ns = api.nvim_create_namespace('treesitter/dev-edit')
|
2023-08-25 11:17:36 -07:00
|
|
|
|
|
|
|
---@param query_win integer
|
|
|
|
---@param base_win integer
|
2023-09-16 10:05:59 -07:00
|
|
|
---@param lang string
|
|
|
|
local function update_editor_highlights(query_win, base_win, lang)
|
2023-08-25 11:17:36 -07:00
|
|
|
local base_buf = api.nvim_win_get_buf(base_win)
|
|
|
|
local query_buf = api.nvim_win_get_buf(query_win)
|
2024-09-14 12:57:33 -07:00
|
|
|
local parser = assert(vim.treesitter.get_parser(base_buf, lang, { error = false }))
|
2023-09-15 03:10:55 -07:00
|
|
|
api.nvim_buf_clear_namespace(base_buf, edit_ns, 0, -1)
|
2023-08-25 11:17:36 -07:00
|
|
|
local query_content = table.concat(api.nvim_buf_get_lines(query_buf, 0, -1, false), '\n')
|
|
|
|
|
|
|
|
local ok_query, query = pcall(vim.treesitter.query.parse, lang, query_content)
|
|
|
|
if not ok_query then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local cursor_word = vim.fn.expand('<cword>') --[[@as string]]
|
|
|
|
-- Only highlight captures if the cursor is on a capture name
|
|
|
|
if cursor_word:find('^@') == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- Remove the '@' from the cursor word
|
|
|
|
cursor_word = cursor_word:sub(2)
|
|
|
|
local topline, botline = vim.fn.line('w0', base_win), vim.fn.line('w$', base_win)
|
|
|
|
for id, node in query:iter_captures(parser:trees()[1]:root(), base_buf, topline - 1, botline) do
|
|
|
|
local capture_name = query.captures[id]
|
|
|
|
if capture_name == cursor_word then
|
|
|
|
local lnum, col, end_lnum, end_col = node:range()
|
2023-09-15 03:10:55 -07:00
|
|
|
api.nvim_buf_set_extmark(base_buf, edit_ns, lnum, col, {
|
2023-08-25 11:17:36 -07:00
|
|
|
end_row = end_lnum,
|
|
|
|
end_col = end_col,
|
|
|
|
hl_group = 'Visual',
|
|
|
|
virt_text = {
|
|
|
|
{ capture_name, 'Title' },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- @private
|
2023-09-16 10:05:59 -07:00
|
|
|
--- @param lang? string language to open the query editor for.
|
2024-09-27 18:08:05 -07:00
|
|
|
--- @return boolean? `true` on success, `nil` on failure
|
|
|
|
--- @return string? error message, if applicable
|
2023-09-16 10:05:59 -07:00
|
|
|
function M.edit_query(lang)
|
2023-08-25 11:17:36 -07:00
|
|
|
local buf = api.nvim_get_current_buf()
|
|
|
|
local win = api.nvim_get_current_win()
|
|
|
|
|
2023-09-15 03:10:55 -07:00
|
|
|
-- Close any existing editor window
|
|
|
|
if vim.b[buf].dev_edit then
|
|
|
|
close_win(vim.b[buf].dev_edit)
|
2023-08-25 11:17:36 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local cmd = '60vnew'
|
2023-09-15 03:10:55 -07:00
|
|
|
-- If the inspector is open, place the editor above it.
|
2023-08-25 11:17:36 -07:00
|
|
|
local base_win = vim.b[buf].dev_base ---@type integer?
|
|
|
|
local base_buf = base_win and api.nvim_win_get_buf(base_win)
|
|
|
|
local inspect_win = base_buf and vim.b[base_buf].dev_inspect
|
|
|
|
if base_win and base_buf and api.nvim_win_is_valid(inspect_win) then
|
|
|
|
vim.api.nvim_set_current_win(inspect_win)
|
|
|
|
buf = base_buf
|
|
|
|
win = base_win
|
|
|
|
cmd = 'new'
|
|
|
|
end
|
|
|
|
vim.cmd(cmd)
|
|
|
|
|
2024-09-14 12:57:33 -07:00
|
|
|
local parser = vim.treesitter.get_parser(buf, lang, { error = false })
|
2024-09-13 05:09:11 -07:00
|
|
|
if not parser then
|
2024-09-14 12:57:33 -07:00
|
|
|
return nil,
|
|
|
|
string.format('Failed to show query editor for buffer %s: no parser for lang "%s"', buf, lang)
|
2023-08-25 11:17:36 -07:00
|
|
|
end
|
2023-09-16 10:05:59 -07:00
|
|
|
lang = parser:lang()
|
2023-08-25 11:17:36 -07:00
|
|
|
|
|
|
|
local query_win = api.nvim_get_current_win()
|
|
|
|
local query_buf = api.nvim_win_get_buf(query_win)
|
|
|
|
|
2023-09-15 03:10:55 -07:00
|
|
|
vim.b[buf].dev_edit = query_win
|
2023-08-25 11:17:36 -07:00
|
|
|
vim.bo[query_buf].omnifunc = 'v:lua.vim.treesitter.query.omnifunc'
|
2024-09-27 03:27:00 -07:00
|
|
|
set_dev_options(query_win, query_buf)
|
2023-08-25 11:17:36 -07:00
|
|
|
|
|
|
|
-- Note that omnifunc guesses the language based on the containing folder,
|
|
|
|
-- so we add the parser's language to the buffer's name so that omnifunc
|
|
|
|
-- can infer the language later.
|
2023-09-15 03:10:55 -07:00
|
|
|
api.nvim_buf_set_name(query_buf, string.format('%s/query_editor.scm', lang))
|
2023-08-25 11:17:36 -07:00
|
|
|
|
2023-09-15 03:10:55 -07:00
|
|
|
local group = api.nvim_create_augroup('treesitter/dev-edit', {})
|
2023-08-25 11:17:36 -07:00
|
|
|
api.nvim_create_autocmd({ 'TextChanged', 'InsertLeave' }, {
|
|
|
|
group = group,
|
|
|
|
buffer = query_buf,
|
2023-09-15 03:10:55 -07:00
|
|
|
desc = 'Update query editor diagnostics when the query changes',
|
2023-08-25 11:17:36 -07:00
|
|
|
callback = function()
|
|
|
|
vim.treesitter.query.lint(query_buf, { langs = lang, clear = false })
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
api.nvim_create_autocmd({ 'TextChanged', 'InsertLeave', 'CursorMoved', 'BufEnter' }, {
|
|
|
|
group = group,
|
|
|
|
buffer = query_buf,
|
2023-09-15 03:10:55 -07:00
|
|
|
desc = 'Update query editor highlights when the cursor moves',
|
2023-08-25 11:17:36 -07:00
|
|
|
callback = function()
|
2023-08-26 13:24:29 -07:00
|
|
|
if api.nvim_win_is_valid(win) then
|
2023-09-16 10:05:59 -07:00
|
|
|
update_editor_highlights(query_win, win, lang)
|
2023-08-26 13:24:29 -07:00
|
|
|
end
|
2023-08-25 11:17:36 -07:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
api.nvim_create_autocmd('BufLeave', {
|
|
|
|
group = group,
|
|
|
|
buffer = query_buf,
|
2023-09-15 03:10:55 -07:00
|
|
|
desc = 'Clear highlights when leaving the query editor',
|
2023-08-25 11:17:36 -07:00
|
|
|
callback = function()
|
2023-09-15 03:10:55 -07:00
|
|
|
api.nvim_buf_clear_namespace(buf, edit_ns, 0, -1)
|
2023-08-25 11:17:36 -07:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
api.nvim_create_autocmd('BufLeave', {
|
|
|
|
group = group,
|
|
|
|
buffer = buf,
|
2023-09-15 03:10:55 -07:00
|
|
|
desc = 'Clear the query editor highlights when leaving the source buffer',
|
2023-08-25 11:17:36 -07:00
|
|
|
callback = function()
|
|
|
|
if not api.nvim_buf_is_loaded(query_buf) then
|
|
|
|
return true
|
2023-04-01 04:55:04 -07:00
|
|
|
end
|
2023-08-25 11:17:36 -07:00
|
|
|
|
2023-09-15 03:10:55 -07:00
|
|
|
api.nvim_buf_clear_namespace(query_buf, edit_ns, 0, -1)
|
2023-08-25 11:17:36 -07:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
api.nvim_create_autocmd('BufHidden', {
|
|
|
|
group = group,
|
|
|
|
buffer = buf,
|
2023-09-15 03:10:55 -07:00
|
|
|
desc = 'Close the editor window when the source buffer is hidden',
|
2023-08-25 11:17:36 -07:00
|
|
|
once = true,
|
|
|
|
callback = function()
|
|
|
|
close_win(query_win)
|
2023-04-01 04:55:04 -07:00
|
|
|
end,
|
|
|
|
})
|
2023-08-25 11:17:36 -07:00
|
|
|
|
|
|
|
api.nvim_buf_set_lines(query_buf, 0, -1, false, {
|
2023-09-20 04:15:23 -07:00
|
|
|
';; Write queries here (see $VIMRUNTIME/queries/ for examples).',
|
|
|
|
';; Move cursor to a capture ("@foo") to highlight matches in the source buffer.',
|
|
|
|
';; Completion for grammar nodes is available (:help compl-omni)',
|
2023-08-25 11:17:36 -07:00
|
|
|
'',
|
|
|
|
'',
|
|
|
|
})
|
|
|
|
vim.cmd('normal! G')
|
|
|
|
vim.cmd.startinsert()
|
2024-09-27 18:08:05 -07:00
|
|
|
|
|
|
|
return true
|
2023-04-01 04:55:04 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|