fix(treesitter): use proper query syntax for inspector (#26274)

This commit is contained in:
Gregory Anders 2023-11-29 08:10:02 -06:00 committed by GitHub
parent a6cba103ce
commit 7bc5ee7f93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,12 +55,12 @@ local function traverse(node, depth, lang, injections, tree)
local text ---@type string
if named then
if field then
text = string.format('%s: (%s)', field, type)
text = string.format('%s: (%s', field, type)
else
text = string.format('(%s)', type)
text = string.format('(%s', type)
end
else
text = string.format('"%s"', type:gsub('\n', '\\n'))
text = string.format('"%s"', type:gsub('\n', '\\n'):gsub('"', '\\"'))
end
table.insert(tree, {
@ -76,6 +76,10 @@ local function traverse(node, depth, lang, injections, tree)
})
traverse(child, depth + 1, lang, injections, tree)
if named then
tree[#tree].text = string.format('%s)', tree[#tree].text)
end
end
return tree
@ -152,12 +156,6 @@ local function get_range_str(lnum, col, end_lnum, end_col)
return string.format('[%d:%d - %d:%d]', lnum + 1, col + 1, end_lnum + 1, end_col)
end
---@param text string
---@return string
local function escape_quotes(text)
return string.format('"%s"', text:sub(2, #text - 1):gsub('"', '\\"'))
end
---@param w integer
---@return boolean closed Whether the window was closed.
local function close_win(w)
@ -183,14 +181,14 @@ end
--- Updates the cursor position in the inspector to match the node under the cursor.
---
--- @param pg TSTreeView
--- @param treeview TSTreeView
--- @param lang string
--- @param source_buf integer
--- @param inspect_buf integer
--- @param inspect_win integer
--- @param pos? { [1]: integer, [2]: integer }
local function set_inspector_cursor(pg, lang, source_buf, inspect_buf, inspect_win, pos)
api.nvim_buf_clear_namespace(inspect_buf, pg.ns, 0, -1)
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)
local cursor_node = vim.treesitter.get_node({
bufnr = source_buf,
@ -203,11 +201,11 @@ local function set_inspector_cursor(pg, lang, source_buf, inspect_buf, inspect_w
end
local cursor_node_id = cursor_node:id()
for i, v in pg:iter() do
for i, v in treeview:iter() do
if v.id == cursor_node_id then
local start = v.depth
local end_col = start + #v.text
api.nvim_buf_set_extmark(inspect_buf, pg.ns, i - 1, start, {
api.nvim_buf_set_extmark(inspect_buf, treeview.ns, i - 1, start, {
end_col = end_col,
hl_group = 'Visual',
})
@ -229,9 +227,8 @@ function TSTreeView:draw(bufnr)
for _, item in self:iter() do
local range_str = get_range_str(item.lnum, item.col, item.end_lnum, item.end_col)
local lang_str = self.opts.lang and string.format(' %s', item.lang) or ''
local text = item.named and item.text or escape_quotes(item.text)
local line =
string.format('%s%s ; %s%s', string.rep(' ', item.depth), text, range_str, lang_str)
string.format('%s%s ; %s%s', string.rep(' ', item.depth), item.text, range_str, lang_str)
if self.opts.lang then
lang_hl_marks[#lang_hl_marks + 1] = {
@ -304,7 +301,7 @@ function M.inspect_tree(opts)
local buf = api.nvim_get_current_buf()
local win = api.nvim_get_current_win()
local pg = assert(TSTreeView:new(buf, opts.lang))
local treeview = assert(TSTreeView:new(buf, opts.lang))
-- Close any existing inspector window
if vim.b[buf].dev_inspect then
@ -341,17 +338,17 @@ function M.inspect_tree(opts)
assert(type(title) == 'string', 'Window title must be a string')
api.nvim_buf_set_name(b, title)
pg:draw(b)
treeview:draw(b)
local cursor = api.nvim_win_get_cursor(win)
set_inspector_cursor(pg, opts.lang, buf, b, w, { cursor[1] - 1, cursor[2] })
set_inspector_cursor(treeview, opts.lang, buf, b, w, { cursor[1] - 1, cursor[2] })
api.nvim_buf_clear_namespace(buf, pg.ns, 0, -1)
api.nvim_buf_clear_namespace(buf, treeview.ns, 0, -1)
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]
local pos = pg:get(row)
local pos = treeview:get(row)
api.nvim_set_current_win(win)
api.nvim_win_set_cursor(win, { pos.lnum + 1, pos.col })
end,
@ -360,21 +357,21 @@ function M.inspect_tree(opts)
desc = 'Toggle anonymous nodes',
callback = function()
local row, col = unpack(api.nvim_win_get_cursor(w)) ---@type integer, integer
local curnode = pg:get(row)
local curnode = treeview:get(row)
while curnode and not curnode.named do
row = row - 1
curnode = pg:get(row)
curnode = treeview:get(row)
end
pg.opts.anon = not pg.opts.anon
pg:draw(b)
treeview.opts.anon = not treeview.opts.anon
treeview:draw(b)
if not curnode then
return
end
local id = curnode.id
for i, node in pg:iter() do
for i, node in treeview:iter() do
if node.id == id then
api.nvim_win_set_cursor(w, { i, col })
break
@ -385,8 +382,8 @@ function M.inspect_tree(opts)
api.nvim_buf_set_keymap(b, 'n', 'I', '', {
desc = 'Toggle language display',
callback = function()
pg.opts.lang = not pg.opts.lang
pg:draw(b)
treeview.opts.lang = not treeview.opts.lang
treeview:draw(b)
end,
})
api.nvim_buf_set_keymap(b, 'n', 'o', '', {
@ -409,10 +406,10 @@ function M.inspect_tree(opts)
return true
end
api.nvim_buf_clear_namespace(buf, pg.ns, 0, -1)
api.nvim_buf_clear_namespace(buf, treeview.ns, 0, -1)
local row = api.nvim_win_get_cursor(w)[1]
local pos = pg:get(row)
api.nvim_buf_set_extmark(buf, pg.ns, pos.lnum, pos.col, {
local pos = treeview:get(row)
api.nvim_buf_set_extmark(buf, treeview.ns, pos.lnum, pos.col, {
end_row = pos.end_lnum,
end_col = math.max(0, pos.end_col),
hl_group = 'Visual',
@ -437,7 +434,7 @@ function M.inspect_tree(opts)
return true
end
set_inspector_cursor(pg, opts.lang, buf, b, w)
set_inspector_cursor(treeview, opts.lang, buf, b, w)
end,
})
@ -449,8 +446,8 @@ function M.inspect_tree(opts)
return true
end
pg = assert(TSTreeView:new(buf, opts.lang))
pg:draw(b)
treeview = assert(TSTreeView:new(buf, opts.lang))
treeview:draw(b)
end,
})
@ -461,7 +458,7 @@ function M.inspect_tree(opts)
if not api.nvim_buf_is_loaded(buf) then
return true
end
api.nvim_buf_clear_namespace(buf, pg.ns, 0, -1)
api.nvim_buf_clear_namespace(buf, treeview.ns, 0, -1)
end,
})
@ -472,7 +469,7 @@ function M.inspect_tree(opts)
if not api.nvim_buf_is_loaded(b) then
return true
end
api.nvim_buf_clear_namespace(b, pg.ns, 0, -1)
api.nvim_buf_clear_namespace(b, treeview.ns, 0, -1)
end,
})