From 0a2f6b0b6ba558a88c77a6b262af647760e6eca8 Mon Sep 17 00:00:00 2001 From: kiyan Date: Fri, 24 Dec 2021 14:50:20 +0100 Subject: [PATCH] fix(dirchange): do not change dir when switching windows - Avoid changing dir when switching windows, except when changing tabpage. - Load nvim-tree with passed directory when opening with `nvim DIR` Fixes #858 #720 --- lua/nvim-tree.lua | 2 +- lua/nvim-tree/lib.lua | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index a91db7db..291224a2 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -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) diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 7e2fae66..7fc80d5e 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -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)