Skip to content

Commit

Permalink
feat: add api.tree.toggle_enable_filters (#2706)
Browse files Browse the repository at this point in the history
* feat: toggle filters

* naming refactoring

* change name to enable

* fix default opt

* fix api name

* update doc

* remove default keybinding, toggle live filter

* add API doc

---------

Co-authored-by: Alexander Courtis <[email protected]>
  • Loading branch information
DeH4er and alex-courtis authored Mar 16, 2024
1 parent 707b24a commit f7c09bd
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ Following is the default configuration. See |nvim-tree-opts| for details.
show_on_open_dirs = true,
},
filters = {
enable = true,
git_ignored = true,
dotfiles = false,
git_clean = false,
Expand Down Expand Up @@ -1242,6 +1243,11 @@ Only relevant when |modified.show_on_dirs| is `true`.

File / folder filters that may be toggled.

*nvim-tree.filters.enable*
Enable / disable all filters including live filter.
Toggle via |nvim-tree-api.tree.toggle_enable_filters()|
Type: `boolean`, Default: `true`

*nvim-tree.filters.git_ignored*
Ignore files based on `.gitignore`. Requires |git.enable| `= true`
Toggle via |nvim-tree-api.tree.toggle_gitignore_filter()|, default `I`
Expand Down Expand Up @@ -1702,6 +1708,10 @@ tree.expand_all() *nvim-tree-api.tree.expand_all()*
Recursively expand all nodes in the tree.
Folder: only the nodes underneath that folder.

*nvim-tree-api.tree.toggle_enable_filters()*
tree.toggle_enable_filters()
Toggle |nvim-tree.filters.enable| all filters.

*nvim-tree-api.tree.toggle_gitignore_filter()*
tree.toggle_gitignore_filter()
Toggle |nvim-tree.filters.git_ignored| filter.
Expand Down Expand Up @@ -2757,6 +2767,7 @@ highlight group is not, hard linking as follows: >
|nvim-tree.filesystem_watchers.ignore_dirs|
|nvim-tree.filters.custom|
|nvim-tree.filters.dotfiles|
|nvim-tree.filters.enable|
|nvim-tree.filters.exclude|
|nvim-tree.filters.git_clean|
|nvim-tree.filters.git_ignored|
Expand Down Expand Up @@ -2966,6 +2977,7 @@ highlight group is not, hard linking as follows: >
|nvim-tree-api.tree.search_node()|
|nvim-tree-api.tree.toggle()|
|nvim-tree-api.tree.toggle_custom_filter()|
|nvim-tree-api.tree.toggle_enable_filters()|
|nvim-tree-api.tree.toggle_git_clean_filter()|
|nvim-tree-api.tree.toggle_gitignore_filter()|
|nvim-tree-api.tree.toggle_help()|
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
show_on_open_dirs = true,
},
filters = {
enable = true,
git_ignored = true,
dotfiles = false,
git_clean = false,
Expand Down
5 changes: 5 additions & 0 deletions lua/nvim-tree/actions/tree/modifiers/toggles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ function M.dotfiles()
reload()
end

function M.enable()
filters.config.enable = not filters.config.enable
reload()
end

return M
1 change: 1 addition & 0 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Api.tree.find_file = wrap(actions.tree.find_file.fn)
Api.tree.search_node = wrap(actions.finders.search_node.fn)
Api.tree.collapse_all = wrap(actions.tree.modifiers.collapse_all.fn)
Api.tree.expand_all = wrap_node(actions.tree.modifiers.expand_all.fn)
Api.tree.toggle_enable_filters = wrap(actions.tree.modifiers.toggles.enable)
Api.tree.toggle_gitignore_filter = wrap(actions.tree.modifiers.toggles.git_ignored)
Api.tree.toggle_git_clean_filter = wrap(actions.tree.modifiers.toggles.git_clean)
Api.tree.toggle_no_buffer_filter = wrap(actions.tree.modifiers.toggles.no_buffer)
Expand Down
5 changes: 5 additions & 0 deletions lua/nvim-tree/explorer/filters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ end
---@param status table from prepare
---@return boolean
function M.should_filter(path, status)
if not M.config.enable then
return false
end

-- exclusions override all filters
if is_excluded(path) then
return false
Expand All @@ -160,6 +164,7 @@ end

function M.setup(opts)
M.config = {
enable = opts.filters.enable,
filter_custom = true,
filter_dotfiles = opts.filters.dotfiles,
filter_git_ignored = opts.filters.git_ignored,
Expand Down
5 changes: 5 additions & 0 deletions lua/nvim-tree/live-filter.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils"
local Iterator = require "nvim-tree.iterators.node-iterator"
local filters = require "nvim-tree.explorer.filters"

local M = {
filter = nil,
Expand Down Expand Up @@ -56,6 +57,10 @@ end
---@param node Node
---@return boolean
local function matches(node)
if not filters.config.enable then
return true
end

local path = node.absolute_path
local name = vim.fn.fnamemodify(path, ":t")
return vim.regex(M.filter):match_str(name) ~= nil
Expand Down

0 comments on commit f7c09bd

Please sign in to comment.