From 63688809682af5fcfa6aedec21b1c9ef3aeb2f4c Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 3 Apr 2022 22:31:40 +1000 Subject: [PATCH] add open_on_setup_file option (#1133) --- README.md | 1 + doc/nvim-tree-lua.txt | 13 +++++++++++-- lua/nvim-tree.lua | 13 ++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 44c6b6e9..6cbf98e9 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,7 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS hijack_unnamed_buffer_when_opening = false, ignore_buffer_on_setup = false, open_on_setup = false, + open_on_setup_file = false, open_on_tab = false, sort_by = "name", update_cwd = false, diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 78f0f7dd..015a94f7 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -92,6 +92,7 @@ function. hijack_unnamed_buffer_when_opening = false, ignore_buffer_on_setup = false, open_on_setup = false, + open_on_setup_file = false, open_on_tab = false, sort_by = "name", update_cwd = false, @@ -196,8 +197,16 @@ Here is a list of the options available in the setup call: default: `true` *nvim-tree.open_on_setup* -- |open_on_setup|: will automatically open the tree when running setup if current - buffer is a directory, is empty or is unnamed. +- |open_on_setup|: will automatically open the tree when running setup if + startup buffer is a directory, is empty or is unnamed. nvim-tree window will + be focused. + type: `boolean` + default: `false` + +*nvim-tree.open_on_setup_file* +- |open_on_setup_file|: will automatically open the tree when running setup if + startup buffer is a file. File window will be focused. File will be found if + |update_focused_file.enable| is set. type: `boolean` default: `false` diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 084bd998..466f9f84 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -181,6 +181,7 @@ function M.on_enter(netrw_disabled) local stats = luv.fs_stat(bufname) local is_dir = stats and stats.type == "directory" + local is_file = stats and stats.type == "file" local cwd if is_dir then cwd = vim.fn.expand(bufname) @@ -197,9 +198,14 @@ function M.on_enter(netrw_disabled) local should_open = false local should_focus_other_window = false - if _config.open_on_setup and not should_be_preserved then + local should_find = false + if (_config.open_on_setup or _config.open_on_setup_file) and not should_be_preserved then if buf_is_dir or buf_is_empty then should_open = true + elseif is_file and _config.open_on_setup_file then + should_open = true + should_focus_other_window = true + should_find = _config.update_focused_file.enable elseif _config.ignore_buffer_on_setup then should_open = true should_focus_other_window = true @@ -222,6 +228,9 @@ function M.on_enter(netrw_disabled) if should_focus_other_window then vim.cmd "noautocmd wincmd p" + if should_find then + M.find_file(false) + end end end M.initialized = true @@ -308,6 +317,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS hijack_unnamed_buffer_when_opening = false, ignore_buffer_on_setup = false, open_on_setup = false, + open_on_setup_file = false, open_on_tab = false, sort_by = "name", update_cwd = false, @@ -415,6 +425,7 @@ function M.setup(conf) _config.update_focused_file = opts.update_focused_file _config.open_on_setup = opts.open_on_setup + _config.open_on_setup_file = opts.open_on_setup_file _config.ignore_buffer_on_setup = opts.ignore_buffer_on_setup _config.ignore_ft_on_setup = opts.ignore_ft_on_setup _config.hijack_directories = opts.hijack_directories