From dfcc305e212773de1f516d6992f1e695ca05ac48 Mon Sep 17 00:00:00 2001 From: kiyan Date: Tue, 31 Aug 2021 22:10:47 +0200 Subject: [PATCH 1/7] chore: refacto setup part 1 refacto setup for code entrypoint following options switched boolean values as options to the setup function: - `nvim_tree_disable_netrw` -> `disable_netrw` - `nvim_tree_hijack_netrw` -> `hijack_netrw` - `nvim_tree_auto_open` -> `open_on_setup` - `nvim_tree_auto_close` -> `auto_close` - `nvim_tree_tab_open` -> `tab_open` - `nvim-tree-update-cwd` -> `update_cwd` - `nvim_tree_hijack_cursor` -> `hijack_cursor` - `nvim_tree_system_open_command` -> `system_open.cmd` - `nvim_tree_system_open_command_args` -> `system_open.args` - `nvim_tree_follow` -> `update_focused_file.enable` - `nvim_tree_follow_update_path` -> `update_focused_file.update_cwd` Also added new option `update_focused_file.ignore_list` which will ignore filepath or filetypes that matches one entry of the list when updating the path if update_cwd is true. --- lua/nvim-tree.lua | 213 ++++++++++++++++++++++++++++----------- lua/nvim-tree/config.lua | 3 +- plugin/tree.vim | 49 --------- 3 files changed, 157 insertions(+), 108 deletions(-) delete mode 100644 plugin/tree.vim diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 2c8b4e028a0..95917c2ddfd 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -1,13 +1,26 @@ local luv = vim.loop +local api = vim.api + local lib = require'nvim-tree.lib' local config = require'nvim-tree.config' local colors = require'nvim-tree.colors' local renderer = require'nvim-tree.renderer' local fs = require'nvim-tree.fs' -local utils = require'nvim-tree.utils' local view = require'nvim-tree.view' -local api = vim.api +local _config = { + is_windows = vim.fn.has('win32') == 1 or vim.fn.has('win32unix') == 1, + is_macos = vim.fn.has('mac') == 1 or vim.fn.has('macunix') == 1, + is_unix = vim.fn.has('unix') == 1, + update_focused_file = { + enable = false, + update_cwd = false, + ignore_list = {}, + }, + system_open = {}, + ignore_ft_on_setup = {}, + open_on_setup = {}, +} local M = {} @@ -22,7 +35,7 @@ function M.toggle() if view.win_open() then view.close() else - if vim.g.nvim_tree_follow == 1 then + if _config.update_focused_file.enable then M.find_file(true) end if not view.win_open() then @@ -58,7 +71,7 @@ function M.tab_change() end) end -local function gen_go_to(mode) +local function go_to(mode) local icon_state = config.get_icon_state() local flags = mode == 'prev_git_item' and 'b' or '' local icons = table.concat(vim.tbl_values(icon_state.icons.git_icons), '\\|') @@ -88,8 +101,8 @@ local keypress_funcs = { last_sibling = function(node) lib.sibling(node, math.huge) end, prev_sibling = function(node) lib.sibling(node, -1) end, next_sibling = function(node) lib.sibling(node, 1) end, - prev_git_item = gen_go_to('prev_git_item'), - next_git_item = gen_go_to('next_git_item'), + prev_git_item = go_to('prev_git_item'), + next_git_item = go_to('next_git_item'), dir_up = lib.dir_up, close = function() M.close() end, preview = function(node) @@ -97,27 +110,31 @@ local keypress_funcs = { return lib.open_file('preview', node.absolute_path) end, system_open = function(node) - if vim.g.nvim_tree_system_open_command == nil then - if vim.fn.has('win32') == 1 or vim.fn.has('win32unix') == 1 then - vim.g.nvim_tree_system_open_command = 'cmd' - vim.g.nvim_tree_system_open_command_args = {'/c', 'start', '""'} - elseif vim.fn.has('mac') == 1 or vim.fn.has('macunix') == 1 then - vim.g.nvim_tree_system_open_command = 'open' - elseif vim.fn.has('unix') == 1 then - vim.g.nvim_tree_system_open_command = 'xdg-open' + if not _config.system_open.cmd then + if _config.is_windows then + _config.system_open = { + cmd = "cmd", + args = {'/c', 'start', '""'} + } + elseif _config.is_macos then + _config.system_open.cmd = 'open' + elseif _config.is_linux then + _config.system_open.cmd = 'xdg-open' else - error('\nNvimTree system_open: cannot open file with system application. Unrecognized platform.\nPlease fill g:nvim_tree_system_open_command with the name of the system file launcher.') + require'nvim-tree.utils'.echo_warning("Cannot open file with system application. Unrecognized platform.") return end end - local process = {} - process.args = vim.g.nvim_tree_system_open_command_args or {} + local process = { + cmd = _config.system_open.cmd, + args = _config.system_open.args, + errors = '\n', + stderr = luv.new_pipe(false) + } table.insert(process.args, node.link_to or node.absolute_path) - process.errors = '\n' - process.stderr = luv.new_pipe(false) - process.handle, process.pid = luv.spawn(vim.g.nvim_tree_system_open_command, - {args = process.args, stdio = {nil, nil, process.stderr}}, + process.handle, process.pid = luv.spawn(process.cmd, + { args = process.args, stdio = { nil, nil, process.stderr }}, function(code) process.stderr:read_stop() process.stderr:close() @@ -129,7 +146,7 @@ local keypress_funcs = { end ) if not process.handle then - error("\n" .. process.pid .. "\nNvimTree system_open: failed to spawn process using '" .. vim.g.nvim_tree_system_open_command .. "'.") + error("\n" .. process.pid .. "\nNvimTree system_open: failed to spawn process using '" .. process.cmd .. "'.") return end luv.read_start(process.stderr, @@ -190,29 +207,31 @@ function M.hijack_current_window() end end -function M.on_enter() +function M.on_enter(opts) 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 = vim.g.nvim_tree_auto_ignore_ft or {} + local ft_ignore = _config.ignore_ft_on_setup local stats = luv.fs_stat(bufname) local is_dir = stats and stats.type == 'directory' - - local disable_netrw = vim.g.nvim_tree_disable_netrw or 1 - local hijack_netrw = vim.g.nvim_tree_hijack_netrw or 1 if is_dir then lib.Tree.cwd = vim.fn.expand(bufname) end - local netrw_disabled = hijack_netrw == 1 or disable_netrw == 1 + + local netrw_disabled = opts.disable_netrw or opts.hijack_netrw + 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 should_open = vim.g.nvim_tree_auto_open == 1 + + local should_open = _config.open_on_setup and ((is_dir and netrw_disabled) or (bufname == "" and not buf_has_content)) and not vim.tbl_contains(ft_ignore, buftype) + if should_open then M.hijack_current_window() end + lib.init(should_open, should_open) end @@ -221,10 +240,18 @@ local function is_file_readable(fname) return stat and stat.type == "file" and luv.fs_access(fname, 'R') end -local function update_base_dir_with_filepath(filepath) - if vim.g.nvim_tree_follow_update_path ~= 1 then +local function update_base_dir_with_filepath(filepath, bufnr) + if not _config.update_focused_file.update_cwd then return end + + local ft = api.nvim_buf_get_option(bufnr, 'filetype') or "" + for _, value in pairs(_config.update_focused_file.ignore_list) do + if vim.fn.stridx(filepath, value) ~= -1 or vim.fn.stridx(ft, value) ~= -1 then + return + end + end + if not vim.startswith(filepath, lib.Tree.cwd or vim.loop.cwd()) then lib.change_dir(vim.fn.fnamemodify(filepath, ':p:h')) end @@ -232,6 +259,7 @@ end function M.find_file(with_open) local bufname = vim.fn.bufname() + local bufnr = api.nvim_get_current_buf() local filepath = vim.fn.fnamemodify(bufname, ':p') if with_open then @@ -242,7 +270,7 @@ function M.find_file(with_open) if not is_file_readable(filepath) then return end - update_base_dir_with_filepath(filepath) + update_base_dir_with_filepath(filepath, bufnr) lib.set_index_and_redraw(filepath) end @@ -270,22 +298,6 @@ function M.on_leave() end, 50) end -local function update_root_dir() - local bufname = api.nvim_buf_get_name(api.nvim_get_current_buf()) - if not is_file_readable(bufname) or not lib.Tree.cwd then return end - - -- this logic is a hack - -- depending on vim-rooter or autochdir, it would not behave the same way when those two are not enabled - -- until i implement multiple workspaces/project, it should stay like this - if bufname:match(utils.path_to_matching_str(lib.Tree.cwd)) then - return - end - local new_cwd = luv.cwd() - if lib.Tree.cwd == new_cwd then return end - - lib.change_dir(new_cwd) -end - function M.open_on_directory() local buf = api.nvim_get_current_buf() local bufname = api.nvim_buf_get_name(buf) @@ -303,13 +315,6 @@ function M.open_on_directory() view.replace_window() end -function M.buf_enter() - update_root_dir() - if vim.g.nvim_tree_follow == 1 then - M.find_file(false) - end -end - function M.reset_highlight() colors.setup() renderer.render_hl(view.View.bufnr) @@ -334,8 +339,100 @@ function M.place_cursor_on_node() api.nvim_win_set_cursor(0, {cursor[1], idx}) end -view.setup() -colors.setup() -vim.defer_fn(M.on_enter, 1) +local function manage_netrw(disable_netrw, hijack_netrw) + if disable_netrw then + vim.g.loaded_netrw = 1 + vim.g.loaded_netrwPlugin = 1 + elseif hijack_netrw then + vim.cmd "silent! autocmd! FileExplorer *" + end +end + +local function setup_vim_commands() + vim.cmd [[ + command! NvimTreeOpen lua require'nvim-tree'.open() + command! NvimTreeClose lua require'nvim-tree'.close() + command! NvimTreeToggle lua require'nvim-tree'.toggle() + command! NvimTreeFocus lua require'nvim-tree'.focus() + command! NvimTreeRefresh lua require'nvim-tree'.refresh() + command! NvimTreeClipboard lua require'nvim-tree'.print_clipboard() + command! NvimTreeFindFile lua require'nvim-tree'.find_file() + command! -nargs=1 NvimTreeResize lua require'nvim-tree'.resize() + ]] +end + +local function setup_autocommands(opts) + vim.cmd "augroup NvimTree" + vim.cmd [[ + """ reset highlights when colorscheme is changed + au ColorScheme * lua require'nvim-tree'.reset_highlight() + + au BufWritePost * lua require'nvim-tree'.refresh() + au User FugitiveChanged,NeogitStatusRefreshed lua require'nvim-tree'.refresh() + + """ deletes the existing buffer when saved in a session to avoid conflicts + au SessionLoadPost * lua require'nvim-tree.view'._wipe_rogue_buffer() + ]] + + if vim.g.nvim_tree_lsp_diagnostics ~= 1 then + vim.cmd "au User LspDiagnosticsChanged lua require'nvim-tree.diagnostics'.update()" + end + if opts.auto_close then + vim.cmd "au WinClosed * lua require'nvim-tree'.on_leave()" + end + if opts.tab_open then + vim.cmd "au TabEnter * lua require'nvim-tree'.tab_change()" + end + if opts.hijack_cursor then + vim.cmd "au CursorMoved NvimTree lua require'nvim-tree'.place_cursor_on_node()" + end + if opts.update_cwd then + vim.cmd "au DirChanged * lua require'nvim-tree.lib'.change_dir(vim.loop.cwd())" + end + if opts.update_focused_file.enable then + vim.cmd "au BufEnter * lua require'nvim-tree'.find_file(false)" + end + + vim.cmd "augroup end" +end + +local DEFAULT_OPTS = { + disable_netrw = true, + hijack_netrw = true, + open_on_setup = false, + auto_close = false, + tab_open = false, + hijack_cursor = false, + update_cwd = false, + update_focused_file = { + enable = false, + update_cwd = false, + ignore_list = {} + }, + ignore_ft_on_setup = {}, + system_open = { + cmd = nil, + args = {} + }, +} + +function M.setup(conf) + local opts = vim.tbl_deep_extend('force', DEFAULT_OPTS, conf or {}) + + manage_netrw(opts.disable_netrw, opts.hijack_netrw) + + _config.update_focused_file = opts.update_focused_file + _config.system_open = opts.system_open + _config.open_on_setup = opts.open_on_setup + _config.ignore_ft_on_setup = opts.ignore_ft_on_setup + + require'nvim-tree.colors'.setup() + require'nvim-tree.view'.setup() + + setup_autocommands(opts) + setup_vim_commands() + + M.on_enter(opts) +end return M diff --git a/lua/nvim-tree/config.lua b/lua/nvim-tree/config.lua index 8a876626394..0faa3a0cdd5 100644 --- a/lua/nvim-tree/config.lua +++ b/lua/nvim-tree/config.lua @@ -58,8 +58,9 @@ function M.get_icon_state() end end + local has_devicons = pcall(require, 'nvim-web-devicons') return { - show_file_icon = show_icons.files == 1 and vim.g.nvim_web_devicons == 1, + show_file_icon = show_icons.files == 1 and has_devicons, show_folder_icon = show_icons.folders == 1, show_git_icon = show_icons.git == 1, show_folder_arrows = show_icons.folder_arrows == 1, diff --git a/plugin/tree.vim b/plugin/tree.vim deleted file mode 100644 index 05080c301b0..00000000000 --- a/plugin/tree.vim +++ /dev/null @@ -1,49 +0,0 @@ -if !has('nvim-0.5') || exists('g:loaded_tree') | finish | endif - -let s:save_cpo = &cpo -set cpo&vim - -if get(g:, 'nvim_tree_disable_netrw', 1) == 1 - let g:loaded_netrw = 1 - let g:loaded_netrwPlugin = 1 -endif - -augroup NvimTree - if get(g:, 'nvim_tree_hijack_netrw', 1) == 1 && get(g:, 'nvim_tree_disable_netrw', 1) == 0 - silent! autocmd! FileExplorer * - endif - au BufWritePost * lua require'nvim-tree'.refresh() - if get(g:, 'nvim_tree_lsp_diagnostics', 0) == 1 - au User LspDiagnosticsChanged lua require'nvim-tree.diagnostics'.update() - endif - au BufEnter * lua require'nvim-tree'.buf_enter() - if get(g:, 'nvim_tree_auto_close') == 1 - au WinClosed * lua require'nvim-tree'.on_leave() - endif - au ColorScheme * lua require'nvim-tree'.reset_highlight() - au User FugitiveChanged,NeogitStatusRefreshed lua require'nvim-tree'.refresh() - if get(g:, 'nvim_tree_tab_open') == 1 - au TabEnter * lua require'nvim-tree'.tab_change() - endif - au SessionLoadPost * lua require'nvim-tree.view'._wipe_rogue_buffer() - if get(g:, 'nvim_tree_hijack_cursor', 1) == 1 - au CursorMoved NvimTree lua require'nvim-tree'.place_cursor_on_node() - endif - if get(g:, 'nvim_tree_update_cwd') == 1 - au DirChanged * lua require'nvim-tree.lib'.change_dir(vim.loop.cwd()) - endif -augroup end - -command! NvimTreeOpen lua require'nvim-tree'.open() -command! NvimTreeClose lua require'nvim-tree'.close() -command! NvimTreeToggle lua require'nvim-tree'.toggle() -command! NvimTreeFocus lua require'nvim-tree'.focus() -command! NvimTreeRefresh lua require'nvim-tree'.refresh() -command! NvimTreeClipboard lua require'nvim-tree'.print_clipboard() -command! NvimTreeFindFile lua require'nvim-tree'.find_file(true) -command! -nargs=1 NvimTreeResize lua require'nvim-tree'.resize() - -let &cpo = s:save_cpo -unlet s:save_cpo - -let g:loaded_tree = 1 From 54592abcfaa8a56edcddc1e1b41d451720e0c8e2 Mon Sep 17 00:00:00 2001 From: kiyan Date: Fri, 3 Sep 2021 19:25:31 +0200 Subject: [PATCH 2/7] add deprecation warning --- lua/nvim-tree.lua | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 95917c2ddfd..b2c6e8e3c70 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -435,4 +435,27 @@ function M.setup(conf) M.on_enter(opts) end +local out_config = { + "nvim_tree_disable_netrw", + "nvim_tree_hijack_netrw", + "nvim_tree_auto_open", + "nvim_tree_auto_close", + "nvim_tree_tab_open", + "nvim_tree_update_cwd", + "nvim_tree_hijack_cursor", + "nvim_tree_system_open_command", + "nvim_tree_system_open_command_args", + "nvim_tree_follow", + "nvim_tree_follow_update_path", +} + +local x = vim.tbl_filter(function(v) + return vim.fn.exists('g:'..v) ~= 0 +end, out_config) + +if #x > 0 then + local msg = "following options are now set in the setup, please read the new documentation for the setup function: " + require'nvim-tree.utils'.echo_warning(msg..table.concat(x, " | ")) +end + return M From 47936dfb7b6918a881995a50dd44bde6aa9a23ab Mon Sep 17 00:00:00 2001 From: kiyan Date: Fri, 3 Sep 2021 19:48:44 +0200 Subject: [PATCH 3/7] update readme --- README.md | 59 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 53e37a422b1..1784c5f23a1 100644 --- a/README.md +++ b/README.md @@ -21,40 +21,75 @@ Install with [packer](https://github.com/wbthomason/packer.nvim): ```lua use { 'kyazdani42/nvim-tree.lua', - requires = 'kyazdani42/nvim-web-devicons' + requires = 'kyazdani42/nvim-web-devicons', + config = function() require'nvim-tree'.setup {} end } ``` ## Setup +Options are currently being migrating into the setup function, you need to run `require'nvim-tree'.setup()` in your personal configurations. +Setup should be run in a lua file or in a lua heredoc (`:help lua-heredoc`) if using in a vim file. +Note that options under the `g:` command should be set **BEFORE** running the setup function. + +```lua +-- following options are the default +require'nvim-tree'.setup { + -- disables netrw completely + disable_netrw = true, + -- hijack netrw window on startup + hijack_netrw = true, + -- open the tree when running this setup function + open_on_setup = false, + -- will not open on setup if the filetype is in this list + ignore_ft_on_setup = {}, + -- closes neovim automatically when the tree is the last **WINDOW** in the view + auto_close = false, + -- opens the tree when changing/opening a new tab if the tree wasn't previously opened + tab_open = false, + -- hijack the cursor in the tree to put it at the start of the filename + hijack_cursor = false, + -- updates the root directory of the tree on `DirChanged` (when your run `:cd` usually) + update_cwd = false, + -- update the focused file on `BufEnter`, un-collapses the folders recursively until it finds the file + update_focused_file = { + -- enables the feature + enable = false, + -- update the root directory of the tree to the one of the folder containing the file if the file is not under the current root directory + -- only relevant when `update_focused_file.enable` is true + update_cwd = false, + -- list of buffer names / filetypes that will not update the cwd if the file isn't found under the current root directory + -- only relevant when `update_focused_file.update_cwd` is true and `update_focused_file.enable` is true + ignore_list = {} + }, + -- configuration options for the system open command (`s` in the tree by default) + system_open = { + -- the command to run this, leaving nil should work in most cases + cmd = nil, + -- the command arguments as a list + args = {} + }, +} +``` + ```vim let g:nvim_tree_side = 'right' "left by default let g:nvim_tree_width = 40 "30 by default, can be width_in_columns or 'width_in_percent%' let g:nvim_tree_ignore = [ '.git', 'node_modules', '.cache' ] "empty by default let g:nvim_tree_gitignore = 1 "0 by default -let g:nvim_tree_auto_open = 1 "0 by default, opens the tree when typing `vim $DIR` or `vim` -let g:nvim_tree_auto_close = 1 "0 by default, closes the tree when it's the last window -let g:nvim_tree_auto_ignore_ft = [ 'startify', 'dashboard' ] "empty by default, don't auto open tree on specific filetypes. let g:nvim_tree_quit_on_open = 1 "0 by default, closes the tree when you open a file -let g:nvim_tree_follow = 1 "0 by default, this option allows the cursor to be updated when entering a buffer -let g:nvim_tree_follow_update_path = 1 "0 by default, will update the path of the current dir if the file is not inside the tree. let g:nvim_tree_indent_markers = 1 "0 by default, this option shows indent markers when folders are open let g:nvim_tree_hide_dotfiles = 1 "0 by default, this option hides files and folders starting with a dot `.` let g:nvim_tree_git_hl = 1 "0 by default, will enable file highlight for git attributes (can be used without the icons). let g:nvim_tree_highlight_opened_files = 1 "0 by default, will enable folder and file icon highlight for opened files/directories. let g:nvim_tree_root_folder_modifier = ':~' "This is the default. See :help filename-modifiers for more options -let g:nvim_tree_tab_open = 1 "0 by default, will open the tree when entering a new tab and the tree was previously open let g:nvim_tree_auto_resize = 0 "1 by default, will resize the tree to its saved width when opening a file -let g:nvim_tree_disable_netrw = 0 "1 by default, disables netrw -let g:nvim_tree_hijack_netrw = 0 "1 by default, prevents netrw from automatically opening when opening directories (but lets you keep its other utilities) let g:nvim_tree_add_trailing = 1 "0 by default, append a trailing slash to folder names let g:nvim_tree_group_empty = 1 " 0 by default, compact folders that only contain a single folder into one node in the file tree let g:nvim_tree_lsp_diagnostics = 1 "0 by default, will show lsp diagnostics in the signcolumn. See :help nvim_tree_lsp_diagnostics let g:nvim_tree_disable_window_picker = 1 "0 by default, will disable the window picker. -let g:nvim_tree_hijack_cursor = 0 "1 by default, when moving cursor in the tree, will position the cursor at the start of the file on the current line let g:nvim_tree_icon_padding = ' ' "one space by default, used for rendering the space between the icon and the filename. Use with caution, it could break rendering if you set an empty string depending on your font. let g:nvim_tree_symlink_arrow = ' >> ' " defaults to ' ➛ '. used as a separator between symlinks' source and target. -let g:nvim_tree_update_cwd = 1 "0 by default, will update the tree cwd when changing nvim's directory (DirChanged event). Behaves strangely with autochdir set. let g:nvim_tree_respect_buf_cwd = 1 "0 by default, will change cwd of nvim-tree to that of new buffer's when opening nvim-tree. let g:nvim_tree_refresh_wait = 500 "1000 by default, control how often the tree can be refreshed, 1000 means the tree can be refresh once per 1000ms. let g:nvim_tree_window_picker_exclude = { @@ -148,7 +183,7 @@ highlight NvimTreeFolderIcon guibg=blue - type `]c` to go to next git item - type `[c` to go to prev git item - type `-` to navigate up to the parent directory of the current file/directory -- type `s` to open a file with default system application or a folder with default file manager (if you want to change the command used to do it see `:h g:nvim_tree_system_open_command` and `:h g:nvim_tree_system_open_command_args`) +- type `s` to open a file with default system application or a folder with default file manager (if you want to change the command used to do it see `:h nvim-tree.setup` under `system_open`) - if the file is a directory, `` will open the directory otherwise it will open the file in the buffer near the tree - if the file is a symlink, `` will follow the symlink (if the target is a file) - `` will open the file in a vertical split From 2b80047080ab6c6f272ef71b1697f28e16766447 Mon Sep 17 00:00:00 2001 From: kiyan Date: Fri, 3 Sep 2021 19:54:15 +0200 Subject: [PATCH 4/7] schedule on enter to avoid running before vim first buffer has loaded --- lua/nvim-tree.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index b2c6e8e3c70..2e2ed50a2ff 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -432,7 +432,8 @@ function M.setup(conf) setup_autocommands(opts) setup_vim_commands() - M.on_enter(opts) + -- scheduling to make sure current buffer has initialized before running buffer checks for auto open + vim.schedule(function() M.on_enter(opts) end) end local out_config = { From 2a0cb2b15f6af003ac1b9c2e6e16a6b438d403ac Mon Sep 17 00:00:00 2001 From: kiyan Date: Sat, 4 Sep 2021 11:45:35 +0200 Subject: [PATCH 5/7] update docs --- README.md | 2 +- doc/nvim-tree-lua.txt | 177 ++++++++++++++++++++++++------------------ 2 files changed, 102 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index 1784c5f23a1..2a28fb6102b 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ use { ## Setup -Options are currently being migrating into the setup function, you need to run `require'nvim-tree'.setup()` in your personal configurations. +Options are currently being migrated into the setup function, you need to run `require'nvim-tree'.setup()` in your personal configurations. Setup should be run in a lua file or in a lua heredoc (`:help lua-heredoc`) if using in a vim file. Note that options under the `g:` command should be set **BEFORE** running the setup function. diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 66a433968e9..ce42c518096 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -58,6 +58,107 @@ Print clipboard content for both cut and copy Resize the NvimTree window to the given size. Example: `:NvimTreeresize 50` resizes the window to the width of 50. +============================================================================== +SETUP *nvim-tree-setup* + +To configure the tree (and make it runnable), you should call the setup +function. + +> + require'nvim-tree'.setup { + disable_netrw = true, + hijack_netrw = true, + open_on_setup = false, + ignore_ft_on_setup = {}, + auto_close = false, + tab_open = false, + hijack_cursor = false, + update_cwd = false, + update_focused_file = { + enable = false, + update_cwd = false, + ignore_list = {} + }, + system_open = { + cmd = nil, + args = {} + }, + } +< + +As options are currently being migrated, configuration of global options in +*nvim-tree-options* should be done BEFORE the setup call. + +Here is a list of the options available in the setup call: +- |disable_netrw|: completely disable netrw + type: `boolean` + default: `true` + +- |hijack_netrw|: hijack netrw windows (overriden if |disable_netrw| is `true`) + type: `boolean` + default: `true` + +- |open_on_setup|: will automatically the tree when running setup if current + buffer is a directory, is empty or is unnamed. + type: `boolean` + default: `false` + +- |ignore_ft_on_setup|: list of filetypes that will make |open_on_setup| not + open. You can use this option if you don't want the tree to open in some + scenarios (eg using vim startify). + type: `{string}` + default: `{}` + +- |auto_close|: force closing neovim when the tree is the last window in the view. + type: `boolean` + default: `false` + +- |tab_open|: opens the tree automatically when switching tabpage or opening a new + tabpage if the tree was previously open. + type: `boolean` + default: `false` + +- |hijack_cursor|: keeps the cursor on the first letter of the filename when + moving in the tree. + type: `boolean` + default: `false` + +- |update_cwd|: changes the tree root directory on `DirChanged` and refreshes + the tree. + type: `boolean` + default: `false` + +- |update_focused_file|: update the focused file on `BufEnter`, un-collapses + the folders recursively until it finds the file + + - |update_focused_file.enable|: enable this feature. + type: `boolean` + default: `false` + + - |update_focused_file.update_cwd|: update the root directory of the tree to the one + of the folder containing the file if the file is not under the current root + directory. Only relevant when |update_focused_file.enable| is `true` + type: `boolean` + default: `false` + + - |update_focused_file.ignore_list|: list of buffer names and filetypes that will not + update the root dir of the tree if the file isn't found under the current root + directory. Only relevant when |update_focused_file.update_cwd| is `true` and + |update_focused_file.enable| is `true`. + type: `{string}` + default: `{}` + +- |system_open|: configuration options for the system open command + + - |system_open.cmd|: the command to run, leaving nil should work but + useful if you want to override the default command with another one. + type: `string` + default: `nil` + + - |system_open.args|: the command arguments as a list + type: `{string}` + default: `{}` + ============================================================================== OPTIONS *nvim-tree-options* @@ -166,58 +267,12 @@ You can enable file highlight for git attributes by setting this property. This can be used with or without the icons. -|g:nvim_tree_follow| *g:nvim_tree_follow* - -Can be `0` or `1`. When `1`, will update the cursor to update to the correct -location in the tree on |BufEnter|. -Default is 0 - -|g:nvim_tree_follow_update_path| *g:nvim_tree_follow_update_path* - -Can be `0` or `1`. When `1`, will update the path of the current dir if the -file is not inside the tree. Works only with |g:nvim_tree_follow| = 1. -Default is 0 - -|g:nvim_tree_auto_open| *g:nvim_tree_auto_open* - -Can be `0` or `1`. When `1`, will open the tree when the package is loaded. -It's not relying on VimEnter anymore. -Default is 0 - -|g:nvim_tree_auto_close| *g:nvim_tree_auto_close* - -Can be `0` or `1`. When `1`, will bind |BufEnter| to automatically -close the tree if it's the last window. -Default is 0 - -|g:nvim_tree_auto_ignore_ft| *g:nvim_tree_auto_ignore_ft* - -Don't auto open the tree on specific filetypes. -Useful when you don't want to open tree on plugins like 'Startify' -Default is {} -> - example: let g.nvim_tree_auto_ignore_ft = {'startify', 'dashboard'} - |g:nvim_tree_quit_on_open| *g:nvim_tree_quit_on_open* Can be `0` or `1`. When `1`, will close the tree when a file is opened. Applies to: `edit`, `vsplit`, `split`, `tabnew`. Default is 0 -|g:nvim_tree_system_open_command| *g:nvim_tree_system_open_command* - -A string containing the command used to open a file/folder with default system -application. If left unset it will be automatically filled with the right -command, depending on the operating system. If your operating system isn't -recognized or if you want to use another command you can edit it. -Default: depends on the operating system - -|g:nvim_tree_system_open_command_args| *g:nvim_tree_system_open_command_args* - -An array of strings containing the arguments to be passed to the command -specified in |g:nvim_tree_system_open_command|. -Default: unset if not using Windows - |g:nvim_tree_disable_keybindings| *g:nvim_tree_disable_keybindings* Can be `0` or `1`. When `1`, will disable all keybindings by the plugin. @@ -246,30 +301,12 @@ In what format to show root folder. See `:help filename-modifiers` for available options. Default is `:~` -|g:nvim_tree_tab_open| *g:nvim_tree_tab_open* - -Can be 0 or 1. When 1, will open the tree when entering a new tab if the -tree was previously open. -Default is 0 - |g:nvim_tree_auto_resize| *g:nvim_tree_auto_resize* Can be 0 or 1. When 1, it will resize the tree to it's saved width when opening a new file. Default is 1 -|g:nvim_tree_hijack_netrw| *g:nvim_tree_hijack_netrw* - -Can be 0 or 1. When 1, disable netrw buffers when nvim-tree start but keeps -existing netrw functionnalities accross buffers (like `gx`). -1 by default. - -|g:nvim_tree_disable_netrw| *g:nvim_tree_disable_netrw* - -Can be 0 or 1. When 1, completely disable netrw and all related -functionnalities. -1 by default. - |g:nvim_tree_add_trailing| *g:nvim_tree_add_trailing* Can be 0 or 1. When 1, appends a trailing slash to folder names. @@ -324,12 +361,6 @@ selectable. The default table is } < -|g:nvim_tree_hijack_cursor| *g:nvim_tree_hijack_cursor* - -Can be 0 or 1. 1 by default. -When 1, moving cursor in the tree will position the cursor at the start -of the file on the current line. - |g:nvim_tree_icon_padding| *g:nvim_tree_icon_padding* One space by default, used for rendering the space between the icon and the filename. Use with caution, it could break rendering if you set an empty string depending on your font. @@ -338,12 +369,6 @@ One space by default, used for rendering the space between the icon and the file Defaults to ' ➛ '. Used as a separator between symlinks' source and target. -|g:nvim_tree_update_cwd| - -Can be 0 or 1. 0 by default. -Will update the tree cwd when changing nvim's directory (DirChanged event). -WARNING: Behaves strangely with autochdir set. - |g:nvim_tree_respect_buf_cwd| *g:nvim_tree_respect_buf_cwd* Can be 0 or 1. 0 by default. From 09f1d488685e843d5b5b512cf69e9456f353f204 Mon Sep 17 00:00:00 2001 From: kiyan Date: Sat, 4 Sep 2021 11:49:34 +0200 Subject: [PATCH 6/7] correct typo --- doc/nvim-tree-lua.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index ce42c518096..86a896ea3cc 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -98,7 +98,7 @@ Here is a list of the options available in the setup call: type: `boolean` default: `true` -- |open_on_setup|: will automatically the tree when running setup if current +- |open_on_setup|: will automatically open the tree when running setup if current buffer is a directory, is empty or is unnamed. type: `boolean` default: `false` From 83a238af87e967d4c6f6cb157c1198a0193f9f18 Mon Sep 17 00:00:00 2001 From: kiyan Date: Sat, 4 Sep 2021 11:53:16 +0200 Subject: [PATCH 7/7] rename tab open -> open on tab --- README.md | 4 ++-- doc/nvim-tree-lua.txt | 6 +++--- lua/nvim-tree.lua | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2a28fb6102b..67e63a2b51b 100644 --- a/README.md +++ b/README.md @@ -42,11 +42,11 @@ require'nvim-tree'.setup { -- open the tree when running this setup function open_on_setup = false, -- will not open on setup if the filetype is in this list - ignore_ft_on_setup = {}, + ignore_ft_on_setup = {}, -- closes neovim automatically when the tree is the last **WINDOW** in the view auto_close = false, -- opens the tree when changing/opening a new tab if the tree wasn't previously opened - tab_open = false, + open_on_tab = false, -- hijack the cursor in the tree to put it at the start of the filename hijack_cursor = false, -- updates the root directory of the tree on `DirChanged` (when your run `:cd` usually) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 86a896ea3cc..74ca0314f7e 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -69,9 +69,9 @@ function. disable_netrw = true, hijack_netrw = true, open_on_setup = false, - ignore_ft_on_setup = {}, + ignore_ft_on_setup = {}, auto_close = false, - tab_open = false, + open_on_tab = false, hijack_cursor = false, update_cwd = false, update_focused_file = { @@ -113,7 +113,7 @@ Here is a list of the options available in the setup call: type: `boolean` default: `false` -- |tab_open|: opens the tree automatically when switching tabpage or opening a new +- |open_on_tab|: opens the tree automatically when switching tabpage or opening a new tabpage if the tree was previously open. type: `boolean` default: `false` diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 2e2ed50a2ff..8f7a68e45c2 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -380,7 +380,7 @@ local function setup_autocommands(opts) if opts.auto_close then vim.cmd "au WinClosed * lua require'nvim-tree'.on_leave()" end - if opts.tab_open then + if opts.open_on_tab then vim.cmd "au TabEnter * lua require'nvim-tree'.tab_change()" end if opts.hijack_cursor then @@ -400,8 +400,8 @@ local DEFAULT_OPTS = { disable_netrw = true, hijack_netrw = true, open_on_setup = false, + open_on_tab = false, auto_close = false, - tab_open = false, hijack_cursor = false, update_cwd = false, update_focused_file = {