nvim-neo-tree / neo-tree.nvim

Neovim plugin to manage the file system and other tree like structures.
MIT License
3.9k stars 225 forks source link

BUG: jump to selected item when opening folder #1310

Closed rick-yao closed 9 months ago

rick-yao commented 10 months ago

Did you check docs and existing issues?

Neovim Version (nvim -v)

NVIM v0.9.5 Build type: Release LuaJIT 2.1.1693350652

Operating System / Version

macos 14.2.1

Describe the Bug

some times when opening folder, cursor will jump to a random item rather than stay at the foler. some times it jump to opened file ,sometimes it jump to item in the folder. i couldn't get exact clue.

Screenshots, Traceback

replicate video

Steps to Reproduce

  1. open the file with neotree
  2. focus on neotree
  3. selecte a folder , press l to open
  4. the cursor jump to opened item , not stay at the folder

Expected Behavior

the cursor should stay at the folder item.

Your Configuration

i am using AstroNvim 3.41.2 with my personal config
{
    "nvim-neo-tree/neo-tree.nvim",
    opts = {
      filesystem = {
        filtered_items = {
          visible = true,
          show_hidden_count = true,
          hide_dotfiles = false,
          hide_gitignored = true,
          hide_by_name = {
            -- '.git',
            -- '.DS_Store',
            -- 'thumbs.db',
          },
          never_show = {},
        },
      },
      window = {
        position = "right",
      },
    },
  }
InfiniteRain commented 10 months ago

experiencing the same thing

InfiniteRain commented 10 months ago

update: downgrading to 3.14 has fixed the issue for me:

use({ "nvim-neo-tree/neo-tree.nvim", tag = "3.14" })
georgeguimaraes commented 10 months ago

Hi, I'm gonna take a look into this. Probably related to the changes I made.

georgeguimaraes commented 10 months ago

Hello, I tried and couldn't reproduce it with neovim 0.9.5 and with neo-tree's repro.lua (as stated in the bug report template):

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
    "folke/tokyonight.nvim",
    -- add any other plugins here
}

local neotree_config = {
    "nvim-neo-tree/neo-tree.nvim",
    dependencies = { "MunifTanjim/nui.nvim", "nvim-tree/nvim-web-devicons", "nvim-lua/plenary.nvim" },
    cmd = { "Neotree" },
    keys = {
        { "<Leader>e", "<Cmd>Neotree<CR>" }, -- change or remove this line if relevant.
    },
    opts = {
        log_level = "trace",
        log_to_file = "/tmp/neotree-repro.log",
        filesystem = {
            filtered_items = {
                visible = true,
                show_hidden_count = true,
                hide_dotfiles = false,
                hide_gitignored = true,
                hide_by_name = {
                    -- '.git',
                    -- '.DS_Store',
                    -- 'thumbs.db',
                },
                never_show = {},
            },
        },
        window = {
            position = "right",
        },
    },
}

table.insert(plugins, neotree_config)
require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

I included your config as well.

It seems that AstroNvim overwrites some commands from neo-tree here. I'd suggest opening an issue there.

rick-yao commented 10 months ago

Thanks for your quick reply ,I'll open an issue at Astronvim.

rick-yao commented 10 months ago

the issue is gone when i fallback to 3.14, issue closed. Both neo-tree and astronvim cannot replicate it. issue closed.

mehalter commented 10 months ago

@georgeguimaraes it does have to do with this commit you made (found with git bisect): https://github.com/nvim-neo-tree/neo-tree.nvim/pull/1288

It seems to cause this bug in the custom hjkl based navigation described in this discussion: https://github.com/nvim-neo-tree/neo-tree.nvim/discussions/163

I'm not sure if this is considered a bug/issue in neo-tree. If not, I can just remove this custom code from AstroNvim since there is no place for unstable code in there.

Thanks in advanced if you take a look at this! No worries if you don't have time!

mehalter commented 10 months ago

You might have a better idea than me @georgeguimaraes if the PR introduced a bug or if the code in the linked comment (https://github.com/nvim-neo-tree/neo-tree.nvim/discussions/163#discussioncomment-4747082) has just always had a bug in it and this just exposed it. The code there seems relatively straightforward and that it should work, but I don't have a full understanding of the neo-tree internals like yourself. So it would be difficult for me to identify which side of the equation the bug would be considered

cseickel commented 10 months ago

Looking at the video, it does not seem random at all. From what I can see, it is jumping to the currently active file in the buffer to the left.

More importantly, when I use the provided config and just add the custom h/l mappings, i don't see the same behavior:

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
    "folke/tokyonight.nvim",
    -- add any other plugins here
}

local neotree_config = {
    "nvim-neo-tree/neo-tree.nvim",
    dependencies = { "MunifTanjim/nui.nvim", "nvim-tree/nvim-web-devicons", "nvim-lua/plenary.nvim" },
    cmd = { "Neotree" },
    keys = {
        { "<Leader>e", "<Cmd>Neotree<CR>" }, -- change or remove this line if relevant.
    },
    opts = {
        log_level = "trace",
        log_to_file = "/tmp/neotree-repro.log",
        filesystem = {
            filtered_items = {
                visible = true,
                show_hidden_count = true,
                hide_dotfiles = false,
                hide_gitignored = true,
                hide_by_name = {
                    -- '.git',
                    -- '.DS_Store',
                    -- 'thumbs.db',
                },
                never_show = {},
            },
            window = {
                mappings = {
                    h = function(state)
                      local node = state.tree:get_node()
                      if (node.type == "directory" or node:has_children()) and node:is_expanded() then
                        state.commands.toggle_node(state)
                      else
                        require("neo-tree.ui.renderer").focus_node(state, node:get_parent_id())
                      end
                    end,
                    l = function(state)
                      local node = state.tree:get_node()
                      if node.type == "directory" or node:has_children() then
                        if not node:is_expanded() then
                          state.commands.toggle_node(state)
                        else
                          require("neo-tree.ui.renderer").focus_node(state, node:get_child_ids()[1])
                        end
                      end
                    end,
                },
            }
        },
        window = {
            position = "right",
        },
    },
}

table.insert(plugins, neotree_config)
require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

I am using nvim version 0.9.4 on linux.

mehalter commented 10 months ago

@cseickel it actually doesn't happen 100% of the time. It took me a while to replicate it. Also it doesn't always jump to the currently active file. Sometimes it will jump to a different directory I have recently toggled. I will see if I can get a specific set of actions to replicate it, but so far I have struggled to consistently replicate it without just sitting and doing a lot of actions in the file tree until it happens

georgeguimaraes commented 10 months ago

I've managed to replicate it with a minimal config (with hl keys). It does not happen all the time, but it does trigger an out of order renderer.position.save that I still don't know why.

Investigating.

mehalter commented 10 months ago

Thanks so much for taking some time to investigate this @georgeguimaraes !

georgeguimaraes commented 9 months ago

@mehalter I've proposed a fix. It's hard for me to debug it, because the issue doesn't happen a lot here. If you could try it, it would help a lot:

https://github.com/nvim-neo-tree/neo-tree.nvim/pull/1354

rizkyilhampra commented 8 months ago

sorry to make this issue is resurrect. i'm still experience with the issue. i was reinstalling the plugin but it's still occour. if you don't mind let me add some information here. i hope is helpfull. thanks

Neovim Version

NVIM v0.10.0-dev-2501+gdbf6be296 Build type: RelWithDebInfo LuaJIT 2.1.1707061634

OS

arch linux

Configuration

my neo-tree configuration (click me) ```lua local global = require('aquila.core.global') return { "nvim-neo-tree/neo-tree.nvim", branch = "v3.x", dependencies = { "nvim-tree/nvim-web-devicons", -- not strictly required, but recommended "MunifTanjim/nui.nvim", "nvim-lua/plenary.nvim", }, config = function() require("neo-tree").setup({ close_if_last_window = true, window = { width = 35, mappings = { ["h"] = function(state) local node = state.tree:get_node() if node.type == 'directory' and node:is_expanded() then require 'neo-tree.sources.filesystem'.toggle_directory(state, node) else require 'neo-tree.ui.renderer'.focus_node(state, node:get_parent_id()) end end, ["l"] = function(state) local node = state.tree:get_node() if node.type == 'directory' then if not node:is_expanded() then require 'neo-tree.sources.filesystem'.toggle_directory(state, node) elseif node:has_children() then require 'neo-tree.ui.renderer'.focus_node(state, node:get_child_ids()[1]) end elseif node.type == 'file' then require("neo-tree.sources.filesystem.commands").open(state) end end, -- copy to system clipboard ["Y"] = function(state) local currentNode = state.tree:get_node() local path = currentNode.path vim.fn.setreg("+", path) vim.notify("Copied to system clipboard", "info", { title = "NeoTree", timeout = 1000, }) end, -- paste from system clipboard ["P"] = function(state) local clipboardPath = vim.fn.getreg("+") if clipboardPath == "" then return end local currentNode = state.tree:get_node() local inputs = require("neo-tree.ui.inputs") local confirmationMessage = "Are you sure you want to paste " .. clipboardPath inputs.confirm(confirmationMessage, function(confirmed) if not confirmed then return end local success, error = pcall(function() local sourcePath = vim.fn.fnameescape(clipboardPath) local destinationPath = vim.fn.fnameescape(currentNode.path) if currentNode.type == 'directory' then vim.fn.system { "cp", "-r", sourcePath, destinationPath } elseif currentNode.type == 'file' then vim.fn.system { "cp", sourcePath, destinationPath } end end) if not success then local errorNotification = { title = "NeoTree", timeout = 1000, } vim.notify("Failed to paste from system clipboard", "error", errorNotification) vim.cmd [[echohl ErrorMsg]] return end -- reset clipboard vim.fn.setreg("+", "") require("neo-tree.sources.manager").refresh(state.name) require 'neo-tree.ui.renderer'.focus_node(state, currentNode.id) local successNotification = { title = "NeoTree", timeout = 1000, } vim.notify("Pasted from system clipboard", "info", successNotification) end) end } }, default_component_configs = { git_status = { symbols = { added = global.icons.git.added, modified = global.icons.git.modified, } }, }, filesystem = { use_libuv_file_watcher = true, follow_current_file = { enabled = true, leave_dirs_open = true }, filtered_items = { hide_dotfiles = false, hide_gitignored = false, hide_by_name = { "node_modules", ".git" }, never_show = { ".DS_Store", "thumbs.db" }, }, commands = { delete = function(state) local inputs = require("neo-tree.ui.inputs") local path = state.tree:get_node().path local msg = "Are you sure you want to trash " .. path inputs.confirm(msg, function(confirmed) if not confirmed then return end local success, error = pcall(function() vim.fn.system { "trash", vim.fn.fnameescape(path) } end) if not success then local errorNotification = { title = "NeoTree", timeout = 1000, } vim.notify("Failed to delete to trash", "error", errorNotification) msg = "Skip trash? (permanent delete)" inputs.confirm(msg, function(confirmed_delete_to_trash) if not confirmed_delete_to_trash then return end if state.tree:get_node().type == "directory" then vim.fn.system { "rm", "-rf", vim.fn.fnameescape(path) } else vim.fn.system { "rm", vim.fn.fnameescape(path) } end require("neo-tree.sources.manager").refresh(state.name) end) end require("neo-tree.sources.manager").refresh(state.name) end) end, } }, }) vim.keymap.set("n", "e", 'Neotree toggle') vim.keymap.set("n", "E", 'Neotree reveal') end } ```

NeoTree Version

v3.17

Description

open folder with l or enter key, make cursor random jump, it will jump some line above or below from old cursor position(before open folder) . but it's randomly happen, i think it's happen when i moving fast in neo-tree but i'm not sure with this.

pysan3 commented 8 months ago

@rizkyilhampra Could you join the discussion here? https://github.com/nvim-neo-tree/neo-tree.nvim/discussions/1374

It might be caused by an upstream issue in neovim and maybe not...

Do you remember when you installed that neovim? If it's newer than November, it might be neo-tree's position restore mechanism conflicting with https://github.com/neovim/neovim/issues/27720.

pysan3 commented 8 months ago

Sorry, wrong link. Updated.

rizkyilhampra commented 8 months ago

@pysan3 yes my neovim version is newer than november, to be exact the last i'm updated i think is yesterday.

thanks for give me some related information, i will check it out and following the related infomation.