mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
Merge pull request #22484 from gpanders/inspect-tree-fix-cursor
fix(treesitter): maintain cursor position when toggling anonymous nodes
This commit is contained in:
commit
74c9c413e7
@ -507,8 +507,26 @@ function M.inspect_tree(opts)
|
|||||||
a.nvim_buf_set_keymap(b, 'n', 'a', '', {
|
a.nvim_buf_set_keymap(b, 'n', 'a', '', {
|
||||||
desc = 'Toggle anonymous nodes',
|
desc = 'Toggle anonymous nodes',
|
||||||
callback = function()
|
callback = function()
|
||||||
|
local row, col = unpack(a.nvim_win_get_cursor(w))
|
||||||
|
local curnode = pg:get(row)
|
||||||
|
while curnode and not curnode.named do
|
||||||
|
row = row - 1
|
||||||
|
curnode = pg:get(row)
|
||||||
|
end
|
||||||
|
|
||||||
pg.opts.anon = not pg.opts.anon
|
pg.opts.anon = not pg.opts.anon
|
||||||
pg:draw(b)
|
pg:draw(b)
|
||||||
|
|
||||||
|
if not curnode then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local id = curnode.id
|
||||||
|
for i, node in pg:iter() do
|
||||||
|
if node.id == id then
|
||||||
|
a.nvim_win_set_cursor(w, { i, col })
|
||||||
|
end
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
a.nvim_buf_set_keymap(b, 'n', 'I', '', {
|
a.nvim_buf_set_keymap(b, 'n', 'I', '', {
|
||||||
|
@ -146,9 +146,9 @@ local decor_ns = api.nvim_create_namespace('ts.playground')
|
|||||||
---@return string
|
---@return string
|
||||||
local function get_range_str(lnum, col, end_col, end_lnum)
|
local function get_range_str(lnum, col, end_col, end_lnum)
|
||||||
if lnum == end_lnum then
|
if lnum == end_lnum then
|
||||||
return string.format('[%d:%d-%d]', lnum + 1, col + 1, end_col)
|
return string.format('[%d:%d - %d]', lnum + 1, col + 1, end_col)
|
||||||
end
|
end
|
||||||
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
|
||||||
|
|
||||||
--- Write the contents of this Playground into {bufnr}.
|
--- Write the contents of this Playground into {bufnr}.
|
||||||
@ -163,7 +163,8 @@ function TSPlayground: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 line = string.rep(' ', item.depth) .. item.text .. '; ' .. range_str .. lang_str
|
local line =
|
||||||
|
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] = {
|
||||||
|
Loading…
Reference in New Issue
Block a user