diff --git a/README.md b/README.md index 4fc09cd9..7fc12890 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ require'nvim-tree'.setup { open_on_tab = false, hijack_cursor = false, update_cwd = false, + hijack_unnamed_buffer_when_opening = true, hijack_directories = { enable = true, auto_open = true, diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 0f016293..311a8519 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -84,6 +84,7 @@ function. open_on_tab = false, hijack_cursor = false, update_cwd = false, + hijack_unnamed_buffer_when_opening = true, diagnostics = { enable = false, show_on_dirs = false, @@ -183,6 +184,12 @@ Here is a list of the options available in the setup call: type: `boolean` default: `false` +*nvim-tree.hijack_unnamed_buffer_when_opening* +- |hijack_unnamed_buffer_when_opening|: opens in place of the unnamed + buffer if it's empty. + type: `boolean` + default: `true` + *nvim-tree.hijack_directories* - |hijack_directories|: hijacks new directory buffers when they are opened (`:e dir`). diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index f1b8f74f..565d2d83 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -65,42 +65,6 @@ local function find_existing_windows() ) end -function M.on_enter(netrw_disabled) - local bufnr = api.nvim_get_current_buf() - local bufname = api.nvim_buf_get_name(bufnr) - local buftype = api.nvim_buf_get_option(bufnr, 'filetype') - local ft_ignore = _config.ignore_ft_on_setup - - local stats = luv.fs_stat(bufname) - local is_dir = stats and stats.type == 'directory' - local cwd - if is_dir then - cwd = vim.fn.expand(bufname) - -- INFO: could potentially conflict with rooter plugins - vim.cmd("noautocmd cd "..cwd) - end - - local lines = not is_dir and api.nvim_buf_get_lines(bufnr, 0, -1, false) or {} - local buf_has_content = #lines > 1 or (#lines == 1 and lines[1] ~= "") - - local buf_is_dir = is_dir and netrw_disabled - local buf_is_empty = bufname == "" and not buf_has_content - local should_be_preserved = vim.tbl_contains(ft_ignore, buftype) - local should_open = _config.open_on_setup and not should_be_preserved and (buf_is_dir or buf_is_empty) - local should_hijack = _config.hijack_directories.enable and _config.hijack_directories.auto_open and is_dir and not should_be_preserved - - -- Session that left a NvimTree Buffer opened, reopen with it - local existing_tree_wins = find_existing_windows() - if existing_tree_wins[1] then - api.nvim_set_current_win(existing_tree_wins[1]) - end - - if should_open or should_hijack or existing_tree_wins[1] ~= nil then - lib.init(true, cwd) - end - M.initialized = true -end - local function is_file_readable(fname) local stat = luv.fs_stat(fname) return stat and stat.type == "file" and luv.fs_access(fname, 'R') @@ -205,6 +169,42 @@ function M.place_cursor_on_node() end end +function M.on_enter(netrw_disabled) + local bufnr = api.nvim_get_current_buf() + local bufname = api.nvim_buf_get_name(bufnr) + local buftype = api.nvim_buf_get_option(bufnr, 'filetype') + local ft_ignore = _config.ignore_ft_on_setup + + local stats = luv.fs_stat(bufname) + local is_dir = stats and stats.type == 'directory' + local cwd + if is_dir then + cwd = vim.fn.expand(bufname) + -- INFO: could potentially conflict with rooter plugins + vim.cmd("noautocmd cd "..cwd) + end + + local lines = not is_dir and api.nvim_buf_get_lines(bufnr, 0, -1, false) or {} + local buf_has_content = #lines > 1 or (#lines == 1 and lines[1] ~= "") + + local buf_is_dir = is_dir and netrw_disabled + local buf_is_empty = bufname == "" and not buf_has_content + local should_be_preserved = vim.tbl_contains(ft_ignore, buftype) + local should_open = _config.open_on_setup and not should_be_preserved and (buf_is_dir or buf_is_empty) + local should_hijack = _config.hijack_directories.enable and _config.hijack_directories.auto_open and is_dir and not should_be_preserved + + -- Session that left a NvimTree Buffer opened, reopen with it + local existing_tree_wins = find_existing_windows() + if existing_tree_wins[1] then + api.nvim_set_current_win(existing_tree_wins[1]) + end + + if should_open or should_hijack or existing_tree_wins[1] ~= nil then + lib.init(true, cwd) + end + M.initialized = true +end + local function manage_netrw(disable_netrw, hijack_netrw) if hijack_netrw then vim.cmd "silent! autocmd! FileExplorer *" @@ -288,6 +288,7 @@ local DEFAULT_OPTS = { hijack_cursor = false, update_cwd = false, hide_root_folder = false, + hijack_unnamed_buffer_when_opening = true, update_focused_file = { enable = false, update_cwd = false, @@ -354,6 +355,7 @@ function M.setup(conf) require'nvim-tree.explorer'.setup(opts) require'nvim-tree.git'.setup(opts) require'nvim-tree.view'.setup(opts) + require'nvim-tree.lib'.setup(opts) setup_vim_commands() setup_autocommands(opts) diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index dabba76a..dd9bbf88 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -18,7 +18,11 @@ function M.init(with_open, foldername) TreeExplorer = explorer.Explorer.new(foldername) TreeExplorer:init(function() if with_open then - view.open_in_current_win() + if M.hijack_unnamed_buffer_when_opening then + view.open_in_current_win() + else + view.open() + end end renderer.draw() @@ -143,4 +147,8 @@ M.reload_git = require'nvim-tree.actions.reloaders'.reload_git -- @deprecated: use nvim-tree.actions.find-file.fn M.set_index_and_redraw = require'nvim-tree.actions.find-file'.fn +function M.setup(opts) + M.hijack_unnamed_buffer_when_opening = opts.hijack_unnamed_buffer_when_opening +end + return M