From a6c1d45dd6c26f7871f87564baf3860e0e5ac60c Mon Sep 17 00:00:00 2001 From: kiyan Date: Sat, 30 Oct 2021 12:25:09 +0200 Subject: [PATCH] chore: refacto populate filtering move `nvim_tree_ignore` and `nvim_tree_hide_dotfiles` to setup --- README.md | 45 +++++--------------- doc/nvim-tree-lua.txt | 31 ++++++++------ lua/nvim-tree.lua | 5 +++ lua/nvim-tree/lib.lua | 4 +- lua/nvim-tree/populate.lua | 82 ++++++++++++++++++++---------------- plugin/nvim-tree-startup.lua | 6 ++- 6 files changed, 83 insertions(+), 90 deletions(-) diff --git a/README.md b/README.md index c2fba538..c572b656 100644 --- a/README.md +++ b/README.md @@ -36,31 +36,20 @@ Note that options under the `g:` command should be set **BEFORE** running the se ```lua -- following options are the default +-- each of these are documented in `:help nvim-tree.OPTION_NAME` 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 open_on_tab = false, - -- hijacks new directory buffers when they are opened. + hijack_cursor = false, + update_cwd = false, update_to_buf_dir = { - -- enable the feature enable = true, - -- allow to open the tree if it was previously closed auto_open = true, }, - -- 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 you run `:cd` usually) - update_cwd = false, - -- show lsp diagnostics in the signcolumn diagnostics = { enable = false, icons = { @@ -70,53 +59,39 @@ require'nvim-tree'.setup { error = "", } }, - -- 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 = {} }, - + filters = { + dotfiles = false, + custom = {} + }, view = { - -- width of the window, can be either a number (columns) or a string in `%`, for left or right side placement width = 30, - -- height of the window, can be either a number (columns) or a string in `%`, for top or bottom side placement height = 30, - -- Hide the root path of the current folder on top of the tree hide_root_folder = false, - -- side of the tree, can be one of 'left' | 'right' | 'top' | 'bottom' side = 'left', - -- if true the tree will resize itself after opening a file auto_resize = false, mappings = { - -- custom only false will merge the list with the default mappings - -- if true, it will only use your list to set the mappings custom_only = false, - -- list of mappings to set on the tree manually list = {} } } } ``` +These additional options must be set **BEFORE** calling `require'nvim-tree'` or calling setup. +They are being migrated to the setup function bit by bit, check [this issue](https://github.com/kyazdani42/nvim-tree.lua/issues/674) if you encounter any problems related to configs not working after update. ```vim -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_quit_on_open = 1 "0 by default, closes the tree when you open a file 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 @@ -219,7 +194,7 @@ highlight NvimTreeFolderIcon guibg=blue - `` will open the file in a horizontal split - `` will open the file in a new tab - `` will open the file as a preview (keeps the cursor in the tree) -- `I` will toggle visibility of folders hidden via |g:nvim_tree_ignore| +- `I` will toggle visibility of hidden folders / files - `H` will toggle visibility of dotfiles (files/folders starting with a `.`) - `R` will refresh the tree - Double left click acts like `` diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 99b2751b..faf2438f 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -110,6 +110,10 @@ function. custom_only = false, list = {} } + }, + filters = { + dotfiles = false, + custom = {} } } < @@ -261,10 +265,22 @@ Here is a list of the options available in the setup call: type: `boolean` default: false - - |view,mappings.list|: a list of keymaps that will extend or override the default keymaps + - |view.mappings.list|: a list of keymaps that will extend or override the default keymaps type: list of `{ key: table of strings or string, mode: string (vim-mode), cb: callback function as a string }` default: {} +*nvim-tree.filters* +|filters|: filtering options + + - |filters.dotfiles|: do not show `dotfiles` (files starting with a `.`) + type: `boolean` + default: `false` + + - |filters.custom|: custom list of string that will not be shown. + type: `{string}` + default: `{}` + + ============================================================================== OPTIONS *nvim-tree-options* @@ -280,13 +296,6 @@ width of the window, can be *width_in_columns* or *'width_in_percent%'* where the window will open (default to 'left') - 'left' or 'right' -|g:nvim_tree_ignore| *g:nvim_tree_ignore* - -An array of strings that the tree won't load and display. -useful to hide large data/cache folders. -> - example: let g:nvim_tree_ignore = [ '.git', 'node_modules' ] - |g:nvim_tree_gitignore| *g:nvim_tree_gitignore* Determines whether to include in g:nvim_tree_ignore @@ -384,12 +393,6 @@ Default is 0 Can be `0` or `1`. When `1`, will display indent markers when folders are open Default is 0 -|g:nvim_tree_hide_dotfiles| *g:nvim_tree_hide_dotfiles* - -Can be `0` or `1`. When `1`, will hide dotfiles, files or folders which start -with the `.` character. -Default is 0 - |g:nvim_tree_root_folder_modifier| *g:nvim_tree_root_folder_modifier* In what format to show root folder. See `:help filename-modifiers` for diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 1cde3fa9..16f7103d 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -436,6 +436,10 @@ local DEFAULT_OPTS = { error = "", } }, + filters = { + dotfiles = false, + custom_filter = {} + } } function M.setup(conf) @@ -464,6 +468,7 @@ function M.setup(conf) require'nvim-tree.colors'.setup() require'nvim-tree.view'.setup(opts.view or {}) require'nvim-tree.diagnostics'.setup(opts) + require'nvim-tree.populate'.setup(opts) setup_autocommands(opts) setup_vim_commands() diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 474074de..b2cbdfa2 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -561,12 +561,12 @@ function M.parent_node(node, should_close) end function M.toggle_ignored() - pops.show_ignored = not pops.show_ignored + pops.config.filter_ignored = not pops.config.filter_ignored return M.refresh_tree() end function M.toggle_dotfiles() - pops.show_dotfiles = not pops.show_dotfiles + pops.config.filter_dotfiles = not pops.config.filter_dotfiles return M.refresh_tree() end diff --git a/lua/nvim-tree/populate.lua b/lua/nvim-tree/populate.lua index 28377965..7f360137 100644 --- a/lua/nvim-tree/populate.lua +++ b/lua/nvim-tree/populate.lua @@ -1,13 +1,11 @@ local config = require'nvim-tree.config' local git = require'nvim-tree.git' -local icon_config = config.get_icon_state() local api = vim.api local luv = vim.loop local M = { - show_ignored = false, - show_dotfiles = vim.g.nvim_tree_hide_dotfiles ~= 1, + ignore_list = {} } local utils = require'nvim-tree.utils' @@ -116,47 +114,42 @@ local function node_comparator(a, b) return a.name:lower() <= b.name:lower() end -local function gen_ignore_check(cwd) - if not cwd then cwd = luv.cwd() end - local ignore_list = {} +---Check if the given path should be ignored. +---@param path string Absolute path +---@return boolean +local function should_ignore(path) + local basename = utils.path_basename(path) - if vim.g.nvim_tree_ignore and #vim.g.nvim_tree_ignore > 0 then - for _, entry in pairs(vim.g.nvim_tree_ignore) do - ignore_list[entry] = true + if M.config.filter_dotfiles then + if basename:sub(1, 1) == '.' then + return true end end - ---Check if the given path should be ignored. - ---@param path string Absolute path - ---@return boolean - return function(path) - local basename = utils.path_basename(path) - - if not M.show_ignored then - if vim.g.nvim_tree_gitignore == 1 then - if git.should_gitignore(path) then return true end - end - - local relpath = utils.path_relative(path, cwd) - if ignore_list[relpath] == true or ignore_list[basename] == true then - return true - end - - local idx = path:match(".+()%.[^.]+$") - if idx then - if ignore_list['*'..string.sub(path, idx)] == true then return true end - end - end - - if not M.show_dotfiles then - if basename:sub(1, 1) == '.' then return true end - end - + if not M.config.filter_ignored then return false end -end -local should_ignore = gen_ignore_check() + if vim.g.nvim_tree_gitignore == 1 then + if git.should_gitignore(path) then + return true + end + end + + local relpath = utils.path_relative(path, vim.loop.cwd()) + if M.ignore_list[relpath] == true or M.ignore_list[basename] == true then + return true + end + + local idx = path:match(".+()%.[^.]+$") + if idx then + if M.ignore_list['*'..string.sub(path, idx)] == true then + return true + end + end + + return false +end function M.refresh_entries(entries, cwd, parent_node) local handle = luv.fs_scandir(cwd) @@ -351,6 +344,7 @@ function M.populate(entries, cwd, parent_node) utils.merge_sort(entries, node_comparator) + local icon_config = config.get_icon_state() if (not icon_config.show_git_icon) and vim.g.nvim_tree_git_hl ~= 1 then return end @@ -360,4 +354,18 @@ function M.populate(entries, cwd, parent_node) end end +function M.setup(opts) + M.config = { + filter_ignored = true, + filter_dotfiles = opts.filters.dotfiles, + } + + local custom_filter = opts.filters.custom + if custom_filter and #custom_filter > 0 then + for _, entry in pairs(custom_filter) do + M.ignore_list[entry] = true + end + end +end + return M diff --git a/plugin/nvim-tree-startup.lua b/plugin/nvim-tree-startup.lua index acbf0bc5..e6692d8b 100644 --- a/plugin/nvim-tree-startup.lua +++ b/plugin/nvim-tree-startup.lua @@ -23,6 +23,8 @@ local out_config = { "nvim_tree_bindings", "nvim_tree_disable_keybindings", "nvim_tree_disable_default_keybindings", + "nvim_tree_hide_dotfiles", + "nvim_tree_ignore" } local x = vim.tbl_filter(function(v) @@ -30,6 +32,6 @@ local x = vim.tbl_filter(function(v) end, out_config) if #x > 0 then - local msg = "following options are now set in the setup (:help nvim-tree.setup): " - require'nvim-tree.utils'.echo_warning(msg..table.concat(x, " | ")) + local msg = "Following options were moved to setup (:help nvim-tree.setup): " + require'nvim-tree.utils'.echo_warning(msg..table.concat(x, ", ")) end