diff options
author | Gregory Anders <greg@gpanders.com> | 2023-03-02 14:06:11 -0700 |
---|---|---|
committer | Gregory Anders <greg@gpanders.com> | 2023-03-02 14:11:15 -0700 |
commit | 2eeafc43c43697d18d5634f8f81bf9e60a880e17 (patch) | |
tree | 16480b2d75e7504249fd8944a8e65fb123f37a84 /runtime/lua/vim/treesitter.lua | |
parent | f449121764c19cebda7b8b2c970b76bc8121bae7 (diff) | |
download | rneovim-2eeafc43c43697d18d5634f8f81bf9e60a880e17.tar.gz rneovim-2eeafc43c43697d18d5634f8f81bf9e60a880e17.tar.bz2 rneovim-2eeafc43c43697d18d5634f8f81bf9e60a880e17.zip |
fix(treesitter): maintain cursor position when toggling anonymous nodes
When toggling anonymous nodes in the :InspectTree window, keep the
cursor fixed relative to the node within the tree. This prevents the
cursor from jumping.
Diffstat (limited to 'runtime/lua/vim/treesitter.lua')
-rw-r--r-- | runtime/lua/vim/treesitter.lua | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua index 09e3814a17..b2909f2154 100644 --- a/runtime/lua/vim/treesitter.lua +++ b/runtime/lua/vim/treesitter.lua @@ -506,8 +506,26 @@ function M.inspect_tree(opts) a.nvim_buf_set_keymap(b, 'n', 'a', '', { desc = 'Toggle anonymous nodes', 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: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, }) a.nvim_buf_set_keymap(b, 'n', 'I', '', { |