From 72305f373011af16b6a6df06c0e8506aefa6706e Mon Sep 17 00:00:00 2001 From: kiyan Date: Tue, 15 Feb 2022 20:38:57 +0100 Subject: [PATCH] fix: schedule find-file on toggle to avoid searching for nvim-tree fixes #986 fixes #987 --- lua/nvim-tree.lua | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 7a98381f..152e5bd1 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -24,12 +24,16 @@ function M.toggle(find_file, no_focus) if view.is_visible() then view.close() else + local previous_buf = api.nvim_get_current_buf() M.open() if TreeExplorer and (_config.update_focused_file.enable or find_file) then - M.find_file(false) + -- if we don't schedule, it will search for NvimTree + vim.schedule(function() + M.find_file(false, previous_buf) + end) end if no_focus then - vim.cmd "wincmd p" + vim.cmd "noautocmd wincmd p" end end end @@ -116,11 +120,11 @@ local function update_base_dir_with_filepath(filepath, bufnr) end end -function M.find_file(with_open) +function M.find_file(with_open, bufnr) if not with_open and not TreeExplorer then return end - local bufname = vim.fn.bufname() - local bufnr = api.nvim_get_current_buf() + bufnr = bufnr or api.nvim_get_current_buf() + local bufname = api.nvim_buf_get_name(bufnr) local filepath = utils.canonical_path(vim.fn.fnamemodify(bufname, ':p')) if not is_file_readable(filepath) then return