From 2eeafc43c43697d18d5634f8f81bf9e60a880e17 Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Thu, 2 Mar 2023 14:06:11 -0700 Subject: [PATCH 1/2] 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. --- runtime/lua/vim/treesitter.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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', '', { From 86ff239240e955ef6da95bc9c8814cfd4492f5aa Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Thu, 2 Mar 2023 14:15:18 -0700 Subject: [PATCH 2/2] refactor(treesitter): use string.format to create lines --- runtime/lua/vim/treesitter/playground.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/runtime/lua/vim/treesitter/playground.lua b/runtime/lua/vim/treesitter/playground.lua index fd5b687195..992433961f 100644 --- a/runtime/lua/vim/treesitter/playground.lua +++ b/runtime/lua/vim/treesitter/playground.lua @@ -146,9 +146,9 @@ local decor_ns = api.nvim_create_namespace('ts.playground') ---@return string local function get_range_str(lnum, col, end_col, end_lnum) 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 - 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 --- Write the contents of this Playground into {bufnr}. @@ -163,7 +163,8 @@ function TSPlayground: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 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 lang_hl_marks[#lang_hl_marks + 1] = {