Skip to content

Commit 15942df

Browse files
refactor(#2827): multi instance nvim-tree.live-filter (#2849)
* feat(#2827): Multi Instance: Refactor: nvim-tree.live-filter * refactor: all usages going through the explorer * fix: api and filtration * fix: style * Update lua/nvim-tree/api.lua Co-authored-by: Alexander Courtis <[email protected]> * docs: add missing live filter luadocs --------- Co-authored-by: Alexander Courtis <[email protected]>
1 parent e25eb7f commit 15942df

File tree

9 files changed

+80
-59
lines changed

9 files changed

+80
-59
lines changed

lua/nvim-tree.lua

-1
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,6 @@ function M.setup(conf)
840840
require("nvim-tree.view").setup(opts)
841841
require("nvim-tree.lib").setup(opts)
842842
require("nvim-tree.renderer").setup(opts)
843-
require("nvim-tree.live-filter").setup(opts)
844843
require("nvim-tree.marks").setup(opts)
845844
require("nvim-tree.buffers").setup(opts)
846845
require("nvim-tree.help").setup(opts)

lua/nvim-tree/api.lua

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ local actions = require "nvim-tree.actions"
66
local appearance_diagnostics = require "nvim-tree.appearance.diagnostics"
77
local events = require "nvim-tree.events"
88
local help = require "nvim-tree.help"
9-
local live_filter = require "nvim-tree.live-filter"
109
local marks_navigation = require "nvim-tree.marks.navigation"
1110
local marks_bulk_delete = require "nvim-tree.marks.bulk-delete"
1211
local marks_bulk_trash = require "nvim-tree.marks.bulk-trash"
@@ -265,8 +264,8 @@ Api.git.reload = wrap(actions.reloaders.reload_git)
265264
Api.events.subscribe = events.subscribe
266265
Api.events.Event = events.Event
267266

268-
Api.live_filter.start = wrap(live_filter.start_filtering)
269-
Api.live_filter.clear = wrap(live_filter.clear_filter)
267+
Api.live_filter.start = wrap_explorer_member("live_filter", "start_filtering")
268+
Api.live_filter.clear = wrap_explorer_member("live_filter", "clear_filter")
270269

271270
Api.marks.get = wrap_node(wrap_explorer_member("marks", "get_mark"))
272271
Api.marks.list = wrap_explorer_member("marks", "get_marks")

lua/nvim-tree/core.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local events = require "nvim-tree.events"
22
local explorer = require "nvim-tree.explorer"
3-
local live_filter = require "nvim-tree.live-filter"
43
local view = require "nvim-tree.view"
54
local log = require "nvim-tree.log"
65

@@ -45,7 +44,7 @@ function M.get_nodes_starting_line()
4544
if view.is_root_folder_visible(M.get_cwd()) then
4645
offset = offset + 1
4746
end
48-
if live_filter.filter then
47+
if TreeExplorer and TreeExplorer.live_filter.filter then
4948
return offset + 1
5049
end
5150
return offset

lua/nvim-tree/explorer/explore.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ local utils = require "nvim-tree.utils"
22
local builders = require "nvim-tree.explorer.node-builders"
33
local explorer_node = require "nvim-tree.explorer.node"
44
local git = require "nvim-tree.git"
5-
local live_filter = require "nvim-tree.live-filter"
65
local log = require "nvim-tree.log"
76

87
local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
@@ -99,7 +98,7 @@ function M.explore(node, status, parent)
9998
end
10099

101100
parent.sorters:sort(node.nodes)
102-
live_filter.apply_filter(node)
101+
parent.live_filter:apply_filter(node)
103102

104103
log.profile_end(profile)
105104
return node.nodes

lua/nvim-tree/explorer/init.lua

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local watch = require "nvim-tree.explorer.watch"
44
local explorer_node = require "nvim-tree.explorer.node"
55
local Filters = require "nvim-tree.explorer.filters"
66
local Marks = require "nvim-tree.marks"
7+
local LiveFilter = require "nvim-tree.explorer.live-filter"
78
local Sorters = require "nvim-tree.explorer.sorters"
89

910
local M = {}
@@ -15,6 +16,9 @@ M.reload = require("nvim-tree.explorer.reload").reload
1516
---@field absolute_path string
1617
---@field nodes Node[]
1718
---@field open boolean
19+
---@field filters Filters
20+
---@field live_filter LiveFilter
21+
---@field sorters Sorter
1822
---@field marks Marks
1923

2024
local Explorer = {}
@@ -45,6 +49,7 @@ function Explorer.new(path)
4549
}, Explorer)
4650
explorer.watcher = watch.create_watcher(explorer)
4751
explorer.filters = Filters:new(M.config, explorer)
52+
explorer.live_filter = LiveFilter:new(M.config, explorer)
4853
explorer:_load(explorer)
4954
return explorer
5055
end

lua/nvim-tree/live-filter.lua renamed to lua/nvim-tree/explorer/live-filter.lua

+61-44
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,34 @@ local view = require "nvim-tree.view"
22
local utils = require "nvim-tree.utils"
33
local Iterator = require "nvim-tree.iterators.node-iterator"
44

5-
local M = {
6-
filter = nil,
7-
}
5+
---@class LiveFilter
6+
---@field explorer Explorer
7+
---@field prefix string
8+
---@field always_show_folders boolean
9+
---@field filter string
10+
local LiveFilter = {}
11+
12+
---@param opts table
13+
---@param explorer Explorer
14+
function LiveFilter:new(opts, explorer)
15+
local o = {
16+
explorer = explorer,
17+
prefix = opts.live_filter.prefix,
18+
always_show_folders = opts.live_filter.always_show_folders,
19+
filter = nil,
20+
}
21+
setmetatable(o, self)
22+
self.__index = self
23+
return o
24+
end
825

926
local function redraw()
1027
require("nvim-tree.renderer").draw()
1128
end
1229

1330
---@param node_ Node|nil
14-
local function reset_filter(node_)
15-
node_ = node_ or require("nvim-tree.core").get_explorer()
31+
local function reset_filter(self, node_)
32+
node_ = node_ or self.explorer
1633

1734
if node_ == nil then
1835
return
@@ -36,7 +53,7 @@ end
3653
local overlay_bufnr = 0
3754
local overlay_winnr = 0
3855

39-
local function remove_overlay()
56+
local function remove_overlay(self)
4057
if view.View.float.enable and view.View.float.quit_on_focus_loss then
4158
-- return to normal nvim-tree float behaviour when filter window is closed
4259
vim.api.nvim_create_autocmd("WinLeave", {
@@ -55,28 +72,27 @@ local function remove_overlay()
5572
overlay_bufnr = 0
5673
overlay_winnr = 0
5774

58-
if M.filter == "" then
59-
M.clear_filter()
75+
if self.filter == "" then
76+
self:clear_filter()
6077
end
6178
end
6279

6380
---@param node Node
6481
---@return boolean
65-
local function matches(node)
66-
local explorer = require("nvim-tree.core").get_explorer()
67-
if not explorer or not explorer.filters.config.enable then
82+
local function matches(self, node)
83+
if not self.explorer.filters.config.enable then
6884
return true
6985
end
7086

7187
local path = node.absolute_path
7288
local name = vim.fn.fnamemodify(path, ":t")
73-
return vim.regex(M.filter):match_str(name) ~= nil
89+
return vim.regex(self.filter):match_str(name) ~= nil
7490
end
7591

7692
---@param node_ Node|nil
77-
function M.apply_filter(node_)
78-
if not M.filter or M.filter == "" then
79-
reset_filter(node_)
93+
function LiveFilter:apply_filter(node_)
94+
if not self.filter or self.filter == "" then
95+
reset_filter(self, node_)
8096
return
8197
end
8298

@@ -101,49 +117,53 @@ function M.apply_filter(node_)
101117

102118
node.hidden_stats.live_filter = filtered_nodes
103119

104-
local has_nodes = nodes and (M.always_show_folders or #nodes > filtered_nodes)
105-
local ok, is_match = pcall(matches, node)
120+
local has_nodes = nodes and (self.always_show_folders or #nodes > filtered_nodes)
121+
local ok, is_match = pcall(matches, self, node)
106122
node.hidden = not (has_nodes or (ok and is_match))
107123
end
108124

109-
iterate(node_ or require("nvim-tree.core").get_explorer())
125+
iterate(node_ or self.explorer)
110126
end
111127

112-
local function record_char()
128+
local function record_char(self)
113129
vim.schedule(function()
114-
M.filter = vim.api.nvim_buf_get_lines(overlay_bufnr, 0, -1, false)[1]
115-
M.apply_filter()
130+
self.filter = vim.api.nvim_buf_get_lines(overlay_bufnr, 0, -1, false)[1]
131+
self:apply_filter()
116132
redraw()
117133
end)
118134
end
119135

120-
local function configure_buffer_overlay()
136+
local function configure_buffer_overlay(self)
121137
overlay_bufnr = vim.api.nvim_create_buf(false, true)
122138

123139
vim.api.nvim_buf_attach(overlay_bufnr, true, {
124-
on_lines = record_char,
140+
on_lines = function()
141+
return record_char(self)
142+
end,
125143
})
126144

127145
vim.api.nvim_create_autocmd("InsertLeave", {
128-
callback = remove_overlay,
146+
callback = function()
147+
return remove_overlay(self)
148+
end,
129149
once = true,
130150
})
131151

132152
vim.api.nvim_buf_set_keymap(overlay_bufnr, "i", "<CR>", "<cmd>stopinsert<CR>", {})
133153
end
134154

135155
---@return integer
136-
local function calculate_overlay_win_width()
156+
local function calculate_overlay_win_width(self)
137157
local wininfo = vim.fn.getwininfo(view.get_winnr())[1]
138158

139159
if wininfo then
140-
return wininfo.width - wininfo.textoff - #M.prefix
160+
return wininfo.width - wininfo.textoff - #self.prefix
141161
end
142162

143163
return 20
144164
end
145165

146-
local function create_overlay()
166+
local function create_overlay(self)
147167
if view.View.float.enable then
148168
-- don't close nvim-tree float when focus is changed to filter window
149169
vim.api.nvim_clear_autocmds {
@@ -153,12 +173,12 @@ local function create_overlay()
153173
}
154174
end
155175

156-
configure_buffer_overlay()
176+
configure_buffer_overlay(self)
157177
overlay_winnr = vim.api.nvim_open_win(overlay_bufnr, true, {
158178
col = 1,
159179
row = 0,
160180
relative = "cursor",
161-
width = calculate_overlay_win_width(),
181+
width = calculate_overlay_win_width(self),
162182
height = 1,
163183
border = "none",
164184
style = "minimal",
@@ -170,29 +190,31 @@ local function create_overlay()
170190
vim.api.nvim_buf_set_option(overlay_bufnr, "modifiable", true) ---@diagnostic disable-line: deprecated
171191
end
172192

173-
vim.api.nvim_buf_set_lines(overlay_bufnr, 0, -1, false, { M.filter })
193+
vim.api.nvim_buf_set_lines(overlay_bufnr, 0, -1, false, { self.filter })
174194
vim.cmd "startinsert"
175-
vim.api.nvim_win_set_cursor(overlay_winnr, { 1, #M.filter + 1 })
195+
vim.api.nvim_win_set_cursor(overlay_winnr, { 1, #self.filter + 1 })
176196
end
177197

178-
function M.start_filtering()
198+
function LiveFilter:start_filtering()
179199
view.View.live_filter.prev_focused_node = require("nvim-tree.lib").get_node_at_cursor()
180-
M.filter = M.filter or ""
200+
self.filter = self.filter or ""
181201

182202
redraw()
183203
local row = require("nvim-tree.core").get_nodes_starting_line() - 1
184-
local col = #M.prefix > 0 and #M.prefix - 1 or 1
204+
local col = #self.prefix > 0 and #self.prefix - 1 or 1
185205
view.set_cursor { row, col }
186206
-- needs scheduling to let the cursor move before initializing the window
187-
vim.schedule(create_overlay)
207+
vim.schedule(function()
208+
return create_overlay(self)
209+
end)
188210
end
189211

190-
function M.clear_filter()
212+
function LiveFilter:clear_filter()
191213
local node = require("nvim-tree.lib").get_node_at_cursor()
192214
local last_node = view.View.live_filter.prev_focused_node
193215

194-
M.filter = nil
195-
reset_filter()
216+
self.filter = nil
217+
reset_filter(self)
196218
redraw()
197219

198220
if node then
@@ -202,9 +224,4 @@ function M.clear_filter()
202224
end
203225
end
204226

205-
function M.setup(opts)
206-
M.prefix = opts.live_filter.prefix
207-
M.always_show_folders = opts.live_filter.always_show_folders
208-
end
209-
210-
return M
227+
return LiveFilter

lua/nvim-tree/explorer/reload.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local utils = require "nvim-tree.utils"
22
local builders = require "nvim-tree.explorer.node-builders"
33
local explorer_node = require "nvim-tree.explorer.node"
4-
local live_filter = require "nvim-tree.live-filter"
54
local git = require "nvim-tree.git"
65
local log = require "nvim-tree.log"
76

@@ -183,7 +182,7 @@ function M.reload(node, git_status)
183182
end
184183

185184
explorer.sorters:sort(node.nodes)
186-
live_filter.apply_filter(node)
185+
explorer.live_filter:apply_filter(node)
187186
log.profile_end(profile)
188187
return node.nodes
189188
end

lua/nvim-tree/renderer/builder.lua

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local core = require "nvim-tree.core"
2-
local live_filter = require "nvim-tree.live-filter"
32
local notify = require "nvim-tree.notify"
43
local utils = require "nvim-tree.utils"
54
local view = require "nvim-tree.view"
@@ -389,7 +388,8 @@ end
389388

390389
---@private
391390
function Builder:get_nodes_number(nodes)
392-
if not live_filter.filter then
391+
local explorer = core.get_explorer()
392+
if not explorer or not explorer.live_filter.filter then
393393
return #nodes
394394
end
395395

@@ -436,15 +436,16 @@ end
436436

437437
---@private
438438
function Builder:build_header()
439+
local explorer = core.get_explorer()
439440
if view.is_root_folder_visible(core.get_cwd()) then
440441
local root_name = self:format_root_name(M.opts.renderer.root_folder_label)
441442
table.insert(self.lines, root_name)
442443
self:insert_highlight({ "NvimTreeRootFolder" }, 0, string.len(root_name))
443444
self.index = 1
444445
end
445446

446-
if live_filter.filter then
447-
local filter_line = string.format("%s/%s/", M.opts.live_filter.prefix, live_filter.filter)
447+
if explorer and explorer.live_filter.filter then
448+
local filter_line = string.format("%s/%s/", M.opts.live_filter.prefix, explorer.live_filter.filter)
448449
table.insert(self.lines, filter_line)
449450
local prefix_length = string.len(M.opts.live_filter.prefix)
450451
self:insert_highlight({ "NvimTreeLiveFilterPrefix" }, 0, prefix_length)

lua/nvim-tree/utils.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ function M.find_node(nodes, fn)
112112
end)
113113
:iterate()
114114
i = require("nvim-tree.view").is_root_folder_visible() and i or i - 1
115-
i = require("nvim-tree.live-filter").filter and i + 1 or i
115+
local explorer = require("nvim-tree.core").get_explorer()
116+
if explorer and explorer.live_filter.filter then
117+
i = i + 1
118+
end
116119
return node, i
117120
end
118121

0 commit comments

Comments
 (0)