Skip to content

Commit

Permalink
fix(dirchange): do not change dir when switching windows
Browse files Browse the repository at this point in the history
- Avoid changing dir when switching windows, except when
changing tabpage.
- Load nvim-tree with passed directory when opening with `nvim DIR`

Fixes #858 #720
  • Loading branch information
kyazdani42 committed Dec 24, 2021
1 parent e49bd8f commit 0a2f6b0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ function M.on_enter(opts)
M.hijack_current_window()
end

lib.init(should_open)
lib.init(should_open, lib.Tree.cwd)
end

local function is_file_readable(fname)
Expand Down
7 changes: 6 additions & 1 deletion lua/nvim-tree/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,17 @@ function M.collapse_all()
M.redraw()
end

local current_tab = api.nvim_get_current_tabpage()

function M.change_dir(name)
local foldername = name == '..' and vim.fn.fnamemodify(M.Tree.cwd, ':h') or name
local no_cwd_change = vim.fn.expand(foldername) == M.Tree.cwd
if no_cwd_change then
local new_tab = api.nvim_get_current_tabpage()
local is_window = vim.v.event.scope == "window" and new_tab == current_tab
if no_cwd_change or is_window then
return
end
current_tab = new_tab

vim.cmd('lcd '..vim.fn.fnameescape(foldername))
M.init(false, foldername)
Expand Down

0 comments on commit 0a2f6b0

Please sign in to comment.