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