nvim-neo-tree / neo-tree.nvim

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

Working directory changes when sidebar is focused #710

Open showermat opened 1 year ago

showermat commented 1 year ago

Hi, thanks for this exciting plugin!

I'm running into weird behavior that's so obvious I'm sure I'm doing something wrong. With follow_current_file = true, the view in the Neo-tree sidebar follows the currently edited file as long as my cursor stays in the file window. Great! But the moment I switch focus to the Neo-tree window, Neo-tree jumps back to whatever directory it was in when I opened the sidebar. If I move focus back out of the Neo-tree window, it jumps back to the directory containing the file I'm editing.

It feels like Neo-tree is displaying the working directory of the focused window but not changing its own window's working directory to match. I played with the values of bind_to_cwd and cwd_target, but nothing seems to help. I do have autochdir set, but disabling it seems to break file following entirely. Here's a minimal config that reproduces the problem for me:

call plug#begin(stdpath("data") . "/plugged")
Plug 'nvim-lua/plenary.nvim'
Plug 'MunifTanjim/nui.nvim'
Plug 'nvim-neo-tree/neo-tree.nvim', {'branch': 'v2.x'}
call plug#end()

set autochdir

lua << !
require("neo-tree").setup({
    filesystem = { follow_current_file = true },
})
!

Does anything stand out to you that could be causing this? Thanks!

cseickel commented 1 year ago

Yes, this stands out:

set autochdir

That is causing your problem. If you use that, you'll want to disable neo-tree's bind_to_cwd option.

showermat commented 1 year ago

Ah, okay. That wasn't fixing the problem for me, so I read the code -- which is probably what I should have done from the start. I found this snippet: if not is_in_path then return false end.

So it looks like Neo-tree is designed to bail out and not follow if the file is outside the current root. Is there a way to get "universal" following -- something like the pseudocode, if is_in_path then [current behavior] else cwd(dirname(file.path)) end?

nhat-vo commented 1 year ago

@showermat I have tested with follow_current_file = true and :set autochdir, which seems to work well. Is this the desired behavior for you?

showermat commented 1 year ago

In my setup, that doesn't work as I expect. With :set autochdir, follow_current_file = true, and bind_to_cwd = false as prescribed above, the view in Neo-tree does not change at all when I open a new file unless that file is already below the current top-level directory open in Neo-tree. As I mentioned in my previous comment, I think this is due to a mismatch in expectations of in what circumstances Neo-tree should follow the file.

nhat-vo commented 1 year ago

Ah ok I see. I will have a look at how to implement this.

burningalchemist commented 9 months ago

Hey @nhat-vo, is there any update on the issue? 🙂

featheryus commented 1 month ago

I agree with it, Neo-tree windows should follow cwd of active tab, otherwise, when focus neo-tree windows, it will jump back to previous root. Thx. @nhat-vo

Ah, okay. That wasn't fixing the problem for me, so I read the code -- which is probably what I should have done from the start. I found this snippet: if not is_in_path then return false end.

So it looks like Neo-tree is designed to bail out and not follow if the file is outside the current root. Is there a way to get "universal" following -- something like the pseudocode, if is_in_path then [current behavior] else cwd(dirname(file.path)) end?