@@ -10,6 +10,7 @@ view.sidebar = { origin = -1, target = -1 }
10
10
view .float = { origin = - 1 , target = - 1 }
11
11
view .items = {}
12
12
view .resync_paths = {}
13
+ view .last_index = 0
13
14
14
15
local function create_leave (ctx )
15
16
vim .cmd .stopinsert ()
@@ -63,6 +64,22 @@ local function create_insert_move(ctx)
63
64
end
64
65
end
65
66
67
+ function view .get_sorted_items ()
68
+ local active_views = {}
69
+
70
+ for _ , current_view in pairs (view .items ) do
71
+ if current_view then
72
+ active_views [# active_views + 1 ] = current_view
73
+ end
74
+ end
75
+
76
+ table.sort (active_views , function (v1 , v2 )
77
+ return v1 .index < v2 .index
78
+ end )
79
+
80
+ return active_views
81
+ end
82
+
66
83
function view .file_icons ()
67
84
if settings .file_icons then
68
85
local ok , module = pcall (require , ' nvim-web-devicons' )
@@ -88,17 +105,18 @@ function view.get(path)
88
105
return found_view
89
106
end
90
107
91
- local index = # view .items + 1
108
+ view .last_index = view .last_index + 1
109
+
92
110
local resolved = util .resolve (path )
93
111
local instance = setmetatable ({
94
- index = index ,
112
+ index = view . last_index ,
95
113
initial = resolved ,
96
114
states = {},
97
115
show_hidden = false ,
98
116
root = entry .new (resolved ),
99
117
}, view )
100
118
101
- view .items [index ] = instance
119
+ view .items [instance . index ] = instance
102
120
103
121
return instance
104
122
end
@@ -224,10 +242,14 @@ function view.resync(path)
224
242
end
225
243
226
244
view .resync_timer = vim .defer_fn (function ()
227
- for _ , current_view in ipairs (view .items ) do
228
- current_view .root :synchronize (view .resync_paths )
229
- current_view :update ()
230
- current_view :render ()
245
+ for _ , current_view in pairs (view .items ) do
246
+ if util .is_directory (current_view .root .path ) then
247
+ current_view .root :synchronize (view .resync_paths )
248
+ current_view :update ()
249
+ current_view :render ()
250
+ else
251
+ current_view :terminate ()
252
+ end
231
253
end
232
254
233
255
if not view .resync_timer :is_closing () then
@@ -299,6 +321,24 @@ function view:buffers()
299
321
end , vim .api .nvim_list_bufs ())
300
322
end
301
323
324
+ function view :terminate ()
325
+ local reopen = # vim .api .nvim_list_wins () == 1
326
+
327
+ for _ , bufnr in ipairs (self :buffers ()) do
328
+ vim .api .nvim_buf_delete (bufnr , { force = true })
329
+ end
330
+
331
+ if not util .is_directory (self .root .path ) then
332
+ self .root :terminate ()
333
+ end
334
+
335
+ if reopen then
336
+ vim .cmd .Carbon ()
337
+ end
338
+
339
+ view .items [self .index ] = nil
340
+ end
341
+
302
342
function view :update ()
303
343
self .cached_lines = nil
304
344
end
0 commit comments