Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: jump to selected item when opening folder #1310

Closed
3 tasks done
rick-yao opened this issue Jan 16, 2024 · 17 comments · Fixed by #1355
Closed
3 tasks done

BUG: jump to selected item when opening folder #1310

rick-yao opened this issue Jan 16, 2024 · 17 comments · Fixed by #1355
Labels
bug Something isn't working
Milestone

Comments

@rick-yao
Copy link

Did you check docs and existing issues?

  • I have read all the docs.
  • I have searched the existing issues.
  • I have searched the existing discussions.

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",
      },
    },
  }
@rick-yao rick-yao added the bug Something isn't working label Jan 16, 2024
@InfiniteRain
Copy link

experiencing the same thing

@InfiniteRain
Copy link

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

use({ "nvim-neo-tree/neo-tree.nvim", tag = "3.14" })

@georgeguimaraes
Copy link
Contributor

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

@georgeguimaraes
Copy link
Contributor

georgeguimaraes commented Jan 16, 2024

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
Copy link
Author

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

@rick-yao
Copy link
Author

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

@mehalter
Copy link
Contributor

@georgeguimaraes it does have to do with this commit you made (found with git bisect): #1288

It seems to cause this bug in the custom hjkl based navigation described in this discussion: #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
Copy link
Contributor

You might have a better idea than me @georgeguimaraes if the PR introduced a bug or if the code in the linked comment (#163 (reply in thread)) 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

@rick-yao rick-yao reopened this Jan 17, 2024
@cseickel
Copy link
Contributor

cseickel commented Jan 17, 2024

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
Copy link
Contributor

@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
Copy link
Contributor

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
Copy link
Contributor

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

@georgeguimaraes
Copy link
Contributor

@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:

#1354

@rizkyilhampra
Copy link

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)
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", "<Leader>e", '<cmd>Neotree toggle<CR>')
      vim.keymap.set("n", "<Leader>E", '<cmd>Neotree reveal<CR>')
  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
Copy link
Collaborator

pysan3 commented Mar 3, 2024

@rizkyilhampra Could you join the discussion here? #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 neovim/neovim#27720.

@pysan3
Copy link
Collaborator

pysan3 commented Mar 3, 2024

Sorry, wrong link. Updated.

@rizkyilhampra
Copy link

rizkyilhampra commented Mar 3, 2024

@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.

@pysan3 pysan3 added this to the v4.0 milestone Mar 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
7 participants