@@ -2,17 +2,34 @@ local view = require "nvim-tree.view"
22local utils = require " nvim-tree.utils"
33local 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
926local function redraw ()
1027 require (" nvim-tree.renderer" ).draw ()
1128end
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
3653local overlay_bufnr = 0
3754local 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
6178end
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
7490end
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 )
110126end
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 )
118134end
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>" , {})
133153end
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
144164end
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 })
176196end
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 )
188210end
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
203225end
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
0 commit comments