@@ -2,17 +2,34 @@ local view = require "nvim-tree.view"
2
2
local utils = require " nvim-tree.utils"
3
3
local Iterator = require " nvim-tree.iterators.node-iterator"
4
4
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
8
25
9
26
local function redraw ()
10
27
require (" nvim-tree.renderer" ).draw ()
11
28
end
12
29
13
30
--- @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
16
33
17
34
if node_ == nil then
18
35
return
36
53
local overlay_bufnr = 0
37
54
local overlay_winnr = 0
38
55
39
- local function remove_overlay ()
56
+ local function remove_overlay (self )
40
57
if view .View .float .enable and view .View .float .quit_on_focus_loss then
41
58
-- return to normal nvim-tree float behaviour when filter window is closed
42
59
vim .api .nvim_create_autocmd (" WinLeave" , {
@@ -55,28 +72,27 @@ local function remove_overlay()
55
72
overlay_bufnr = 0
56
73
overlay_winnr = 0
57
74
58
- if M .filter == " " then
59
- M . clear_filter ()
75
+ if self .filter == " " then
76
+ self : clear_filter ()
60
77
end
61
78
end
62
79
63
80
--- @param node Node
64
81
--- @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
68
84
return true
69
85
end
70
86
71
87
local path = node .absolute_path
72
88
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
74
90
end
75
91
76
92
--- @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_ )
80
96
return
81
97
end
82
98
@@ -101,49 +117,53 @@ function M.apply_filter(node_)
101
117
102
118
node .hidden_stats .live_filter = filtered_nodes
103
119
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 )
106
122
node .hidden = not (has_nodes or (ok and is_match ))
107
123
end
108
124
109
- iterate (node_ or require ( " nvim-tree.core " ). get_explorer () )
125
+ iterate (node_ or self . explorer )
110
126
end
111
127
112
- local function record_char ()
128
+ local function record_char (self )
113
129
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 ()
116
132
redraw ()
117
133
end )
118
134
end
119
135
120
- local function configure_buffer_overlay ()
136
+ local function configure_buffer_overlay (self )
121
137
overlay_bufnr = vim .api .nvim_create_buf (false , true )
122
138
123
139
vim .api .nvim_buf_attach (overlay_bufnr , true , {
124
- on_lines = record_char ,
140
+ on_lines = function ()
141
+ return record_char (self )
142
+ end ,
125
143
})
126
144
127
145
vim .api .nvim_create_autocmd (" InsertLeave" , {
128
- callback = remove_overlay ,
146
+ callback = function ()
147
+ return remove_overlay (self )
148
+ end ,
129
149
once = true ,
130
150
})
131
151
132
152
vim .api .nvim_buf_set_keymap (overlay_bufnr , " i" , " <CR>" , " <cmd>stopinsert<CR>" , {})
133
153
end
134
154
135
155
--- @return integer
136
- local function calculate_overlay_win_width ()
156
+ local function calculate_overlay_win_width (self )
137
157
local wininfo = vim .fn .getwininfo (view .get_winnr ())[1 ]
138
158
139
159
if wininfo then
140
- return wininfo .width - wininfo .textoff - # M .prefix
160
+ return wininfo .width - wininfo .textoff - # self .prefix
141
161
end
142
162
143
163
return 20
144
164
end
145
165
146
- local function create_overlay ()
166
+ local function create_overlay (self )
147
167
if view .View .float .enable then
148
168
-- don't close nvim-tree float when focus is changed to filter window
149
169
vim .api .nvim_clear_autocmds {
@@ -153,12 +173,12 @@ local function create_overlay()
153
173
}
154
174
end
155
175
156
- configure_buffer_overlay ()
176
+ configure_buffer_overlay (self )
157
177
overlay_winnr = vim .api .nvim_open_win (overlay_bufnr , true , {
158
178
col = 1 ,
159
179
row = 0 ,
160
180
relative = " cursor" ,
161
- width = calculate_overlay_win_width (),
181
+ width = calculate_overlay_win_width (self ),
162
182
height = 1 ,
163
183
border = " none" ,
164
184
style = " minimal" ,
@@ -170,29 +190,31 @@ local function create_overlay()
170
190
vim .api .nvim_buf_set_option (overlay_bufnr , " modifiable" , true ) --- @diagnostic disable-line : deprecated
171
191
end
172
192
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 })
174
194
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 })
176
196
end
177
197
178
- function M . start_filtering ()
198
+ function LiveFilter : start_filtering ()
179
199
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 " "
181
201
182
202
redraw ()
183
203
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
185
205
view .set_cursor { row , col }
186
206
-- 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 )
188
210
end
189
211
190
- function M . clear_filter ()
212
+ function LiveFilter : clear_filter ()
191
213
local node = require (" nvim-tree.lib" ).get_node_at_cursor ()
192
214
local last_node = view .View .live_filter .prev_focused_node
193
215
194
- M .filter = nil
195
- reset_filter ()
216
+ self .filter = nil
217
+ reset_filter (self )
196
218
redraw ()
197
219
198
220
if node then
@@ -202,9 +224,4 @@ function M.clear_filter()
202
224
end
203
225
end
204
226
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