@@ -14,7 +14,6 @@ local async = require("plenary.async")
14
14
15
15
local Path = require (" plenary.path" )
16
16
local os_sep = Path .path .sep
17
- local scan_dir_async
18
17
19
18
local M = {}
20
19
@@ -114,6 +113,82 @@ local job_complete = function(context)
114
113
context = nil
115
114
end
116
115
116
+ local function create_node (context , node )
117
+ local success3 , item = pcall (file_items .create_item , context , node .path , node .type )
118
+ end
119
+
120
+ local function process_node (context , path )
121
+ on_directory_loaded (context , path )
122
+ end
123
+
124
+ local function get_children_sync (path )
125
+ local children = {}
126
+ scan .scan_dir (path , {
127
+ hidden = true ,
128
+ respect_gitignore = false ,
129
+ add_dirs = true ,
130
+ depth = 1 ,
131
+ on_insert = function (entry , typ )
132
+ table.insert (children , { path = entry , type = typ })
133
+ end ,
134
+ })
135
+ return children
136
+ end
137
+
138
+ local function get_children_async (path , callback )
139
+ local children = {}
140
+ scan .scan_dir_async (path , {
141
+ hidden = true ,
142
+ respect_gitignore = false ,
143
+ add_dirs = true ,
144
+ depth = 1 ,
145
+ on_insert = function (entry , typ )
146
+ table.insert (children , { path = entry , type = typ })
147
+ end ,
148
+ on_exit = function (_ )
149
+ callback (children )
150
+ end ,
151
+ })
152
+ end
153
+
154
+ local function scan_dir_sync (context , path )
155
+ process_node (context , path )
156
+ local children = get_children_sync (path )
157
+ for _ , child in ipairs (children ) do
158
+ create_node (context , child )
159
+ if child .type == " directory" then
160
+ local grandchild_nodes = get_children_sync (child .path )
161
+ if
162
+ grandchild_nodes == nil
163
+ or # grandchild_nodes == 0
164
+ or # grandchild_nodes == 1 and grandchild_nodes [1 ].type == " directory"
165
+ then
166
+ scan_dir_sync (context , child .path )
167
+ end
168
+ end
169
+ end
170
+ end
171
+
172
+ local function scan_dir_async (context , path , callback )
173
+ process_node (context , path )
174
+ get_children_async (path , function (children )
175
+ for _ , child in ipairs (children ) do
176
+ create_node (context , child )
177
+ if child .type == " directory" then
178
+ local grandchild_nodes = get_children_sync (child .path )
179
+ if
180
+ grandchild_nodes == nil
181
+ or # grandchild_nodes == 0
182
+ or # grandchild_nodes == 1 and grandchild_nodes [1 ].type == " directory"
183
+ then
184
+ scan_dir_sync (context , child .path )
185
+ end
186
+ end
187
+ end
188
+ callback (path )
189
+ end )
190
+ end
191
+
117
192
-- async_scan scans all the directories in context.paths_to_load
118
193
-- and adds them as items to render in the UI.
119
194
local function async_scan (context , path )
@@ -198,117 +273,6 @@ local function async_scan(context, path)
198
273
-- end
199
274
end
200
275
201
- local function create_node (context , node )
202
- local success3 , item = pcall (file_items .create_item , context , node .path , node .type )
203
- end
204
-
205
- local function process_node (context , path )
206
- on_directory_loaded (context , path )
207
- end
208
-
209
- local function get_children_old (path )
210
- local children = {}
211
- local success , dir = pcall (vim .loop .fs_opendir , path , nil , 1000 )
212
- if not success then
213
- log .error (" Error opening dir:" , dir )
214
- end
215
- local success2 , stats = pcall (vim .loop .fs_readdir , dir )
216
- if success2 and stats then
217
- for _ , stat in ipairs (stats ) do
218
- local child_path = utils .path_join (path , stat .name )
219
- table.insert (children , { path = child_path , type = stat .type })
220
- end
221
- end
222
- -- vim.loop.fs_closedir(dir)
223
- return children
224
- end
225
-
226
- local function get_children_sync (path )
227
- local children = {}
228
- scan .scan_dir (path , {
229
- hidden = true ,
230
- respect_gitignore = false ,
231
- add_dirs = true ,
232
- depth = 1 ,
233
- on_insert = function (entry , typ )
234
- table.insert (children , { path = entry , type = typ })
235
- end ,
236
- })
237
- return children
238
- end
239
-
240
- local function get_children_async (path , callback )
241
- local children = {}
242
- scan .scan_dir_async (path , {
243
- hidden = true ,
244
- respect_gitignore = false ,
245
- add_dirs = true ,
246
- depth = 1 ,
247
- on_insert = function (entry , typ )
248
- table.insert (children , { path = entry , type = typ })
249
- end ,
250
- on_exit = function (_ )
251
- callback (children )
252
- end ,
253
- })
254
- end
255
-
256
- local function scan_dir_sync (context , path )
257
- process_node (context , path )
258
- local children = get_children_sync (path )
259
- for _ , child in ipairs (children ) do
260
- create_node (context , child )
261
- if child .type == " directory" then
262
- local grandchild_nodes = get_children_sync (child .path )
263
- if
264
- grandchild_nodes == nil
265
- or # grandchild_nodes == 0
266
- or # grandchild_nodes == 1 and grandchild_nodes [1 ].type == " directory"
267
- then
268
- scan_dir_sync (context , child .path )
269
- end
270
- end
271
- end
272
- end
273
-
274
- function scan_dir_async (context , path , callback )
275
- process_node (context , path )
276
- get_children_async (path , function (children )
277
- for _ , child in ipairs (children ) do
278
- create_node (context , child )
279
- if child .type == " directory" then
280
- local grandchild_nodes = get_children_sync (child .path )
281
- if
282
- grandchild_nodes == nil
283
- or # grandchild_nodes == 0
284
- or # grandchild_nodes == 1 and grandchild_nodes [1 ].type == " directory"
285
- then
286
- scan_dir_sync (context , child .path )
287
- end
288
- end
289
- end
290
- callback (path )
291
- end )
292
- end
293
-
294
- local function scan_dir (context , path , callback )
295
- local async = callback ~= nil
296
- process_node (context , path )
297
- local child_nodes = get_children_old (path )
298
- for _ , child in ipairs (child_nodes ) do
299
- create_node (context , child )
300
- if child .type == " directory" then
301
- local grandchild_nodes = get_children_old (child .path )
302
- if
303
- grandchild_nodes == nil
304
- or # grandchild_nodes == 0
305
- or # grandchild_nodes == 1 and grandchild_nodes [1 ].type == " directory"
306
- then
307
- scan_dir (context , child .path )
308
- end
309
- end
310
- end
311
- end
312
276
313
277
local function sync_scan (context , path_to_scan )
314
278
for _ , path in ipairs (context .paths_to_load ) do
0 commit comments