nvim-tree.lua icon indicating copy to clipboard operation
nvim-tree.lua copied to clipboard

When I open/close a folder in nvim-tree my view resets and the cursor while being on the same folder now is at the bottom of the screen

Open Nikita128 opened this issue 1 year ago • 11 comments

It's very annoying and I can't seem to find the reason for it. I've checked my configs and they seem ok.

Peek 2023-11-23 11-18

On the video above. I go to the top of my tree, then I scroll down enough until my general view moves down as well. Then I just press "l" button which just works like that:

function deployHJKLMappings(bufnr)
    local api = require("nvim-tree.api")
    vim.keymap.set("n", "l", api.node.open.edit, opts("Edit Or Open", bufnr))
end

When I expand or collapse a folder the view just shifts to the top while the cursor being on the bottom :(

Here's my config for nvim-tree:

require "nvimtree/hjkl_style"

local api = require "nvim-tree.api"

-- disable netrw at the very start of your init.lua
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

-- set termguicolors to enable highlight groups vim.opt.termguicolors = true
local function on_attach(bufnr)
    -- default mappings
    api.config.mappings.default_on_attach(bufnr)
    -- custom mappings
    vim.keymap.set('n', '<C-t>', api.tree.change_root_to_parent,
                   opts('Up', bufnr))
    vim.keymap.set('n', '?', api.tree.toggle_help, opts('Help', bufnr))
    -- hjkl style
    deployHJKLMappings(bufnr)

    vim.keymap.set('n', '<leader>h', api.tree.toggle_hidden_filter,
                   opts('Toggle Dotfiles', bufnr))

    vim.keymap.set('n', 't', api.node.open.tab, opts('Toggle Dotfiles', bufnr))
end

-- pass to setup along with your other options
require("nvim-tree").setup {
    sort_by = "case_sensitive",
    view = {width = 30, centralize_selection = false},
    renderer = {group_empty = true},
    filters = {dotfiles = false},

    on_attach = on_attach
}

vim.keymap.set('n', '<space>e', function()
    local file_dir = vim.fn.expand('%:p:h')
    local cwd = vim.fn.getcwd()
    local diplayed_dir

    if string.find(file_dir, cwd, 0) ~= nil then
        diplayed_dir = cwd
    else
        diplayed_dir = file_dir
    end

    api.tree.toggle({path = diplayed_dir, find_file = true})
end)

As you can see I tried using centralize_selection flag and it doesn't help.

General info:

NVIM v0.10.0-dev-43b0e27 Build type: RelWithDebInfo LuaJIT 2.1.1699392533 nvim-tree version HEAD detached at fa00b57

Nikita128 avatar Nov 23 '23 10:11 Nikita128

Are all those mappings and settings critical for reproducing this behaviour?

gegoune avatar Nov 23 '23 13:11 gegoune

no, I just included them so it would be clear that there is nothing in those mappings that causes this behavior (at least to me it seems like it)

Nikita128 avatar Nov 23 '23 13:11 Nikita128

Do you mind providing as minimal reproducer as possible, it will help with the issue. Thanks.

gegoune avatar Nov 23 '23 13:11 gegoune

I'm having the same issue. I can reproduce it by running nvim -nu nvt-min.lua using nvt-min.lua from the "Clean Room" Replication wiki page.

uri avatar Nov 28 '23 20:11 uri

That's true.

  1. nvim -nu nvt-min.lua
  2. :NvimTreeOpen
  3. :unm <buffer> <C-e>
  4. <C-e> couple of times to scroll viewport down
  5. :lua require('nvim-tree.api').node.open.edit()

Scrolls root node back to top.

gegoune avatar Nov 28 '23 20:11 gegoune

I had same issue too for last two weeks. After that I realized it started happening on one of nvim v0.10.x versions. I don't have this issue on stable v0.9.4.

mitjacotic avatar Nov 29 '23 22:11 mitjacotic

I had same issue too for last two weeks. After that I realized it started happening on one of nvim v0.10.x versions. I don't have this issue on stable v0.9.4.

I was also on v0.10.x and switching to the latest stable version fixes the problem for me as well.

uri avatar Dec 01 '23 19:12 uri

same issue for long time

y1rn avatar Dec 22 '23 08:12 y1rn

Looks like the actions here resolves it:

require"nvim-tree".setup{ filters = { dotfiles = true, git_clean = true, no_buffer = false, },

actions = {
  change_dir = { enable = false }
},
}

pbower avatar Jan 26 '24 05:01 pbower

on my machine, this seems intermittent. I have not find specific variable that sometimes making the tree scrolled to bottom.

https://github.com/nvim-tree/nvim-tree.lua/assets/25895873/d0d91b53-007c-49d2-a2de-2c0d95bed886

on the screen recording, I don't use @/pbower 's solution to disable change_dir action. but disabling change_dir won't fix for me

ybbond avatar Feb 02 '24 05:02 ybbond

Confirming: this is happening on nvim 0.10

If that's the case I'd prefer that we waited some time before investigating this. 0.10 is a very large release, containing many significant change and has had several volatile breaks. Once it's stable we can investigate further.

alex-courtis avatar Feb 03 '24 04:02 alex-courtis