Skip to content

Commit b4f20d4

Browse files
authored
Merge pull request #138 from SidOfc/patch/removed-view
patch/removed view
2 parents 9670efb + dc9efd9 commit b4f20d4

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

lua/carbon/health.lua

+7-7
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,29 @@ function health.check()
2929
end
3030

3131
function health.report_views()
32-
vim.health.report_start('view::active')
32+
vim.health.start('view::active')
3333

3434
local view_roots = vim.tbl_map(function(item)
3535
return item.root
36-
end, view.items)
36+
end, view.get_sorted_items())
3737

3838
table.sort(view_roots)
3939

4040
for _, root in ipairs(view_roots) do
41-
vim.health.report_info(root.path)
41+
vim.health.info(root.path)
4242
end
4343
end
4444

4545
function health.report_events()
46-
vim.health.report_start('watcher::events')
46+
vim.health.start('watcher::events')
4747

4848
local names = vim.tbl_keys(watcher.events)
4949

5050
table.sort(names, sort_names)
5151

5252
for _, name in ipairs(names) do
5353
local callback_count = #vim.tbl_keys(watcher.events[name] or {})
54-
local reporter = callback_count == 0 and 'report_warn' or 'report_info'
54+
local reporter = callback_count == 0 and 'warn' or 'info'
5555

5656
vim.health[reporter](
5757
string.format(
@@ -65,14 +65,14 @@ function health.report_events()
6565
end
6666

6767
function health.report_listeners()
68-
vim.health.report_start('watcher::listeners')
68+
vim.health.start('watcher::listeners')
6969

7070
local paths = vim.tbl_keys(watcher.listeners)
7171

7272
table.sort(paths, sort_paths)
7373

7474
for _, path in ipairs(paths) do
75-
vim.health.report_info(path)
75+
vim.health.info(path)
7676
end
7777
end
7878

lua/carbon/view.lua

+47-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ view.sidebar = { origin = -1, target = -1 }
1010
view.float = { origin = -1, target = -1 }
1111
view.items = {}
1212
view.resync_paths = {}
13+
view.last_index = 0
1314

1415
local function create_leave(ctx)
1516
vim.cmd.stopinsert()
@@ -63,6 +64,22 @@ local function create_insert_move(ctx)
6364
end
6465
end
6566

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+
6683
function view.file_icons()
6784
if settings.file_icons then
6885
local ok, module = pcall(require, 'nvim-web-devicons')
@@ -88,17 +105,18 @@ function view.get(path)
88105
return found_view
89106
end
90107

91-
local index = #view.items + 1
108+
view.last_index = view.last_index + 1
109+
92110
local resolved = util.resolve(path)
93111
local instance = setmetatable({
94-
index = index,
112+
index = view.last_index,
95113
initial = resolved,
96114
states = {},
97115
show_hidden = false,
98116
root = entry.new(resolved),
99117
}, view)
100118

101-
view.items[index] = instance
119+
view.items[instance.index] = instance
102120

103121
return instance
104122
end
@@ -224,10 +242,14 @@ function view.resync(path)
224242
end
225243

226244
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
231253
end
232254

233255
if not view.resync_timer:is_closing() then
@@ -299,6 +321,24 @@ function view:buffers()
299321
end, vim.api.nvim_list_bufs())
300322
end
301323

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+
302342
function view:update()
303343
self.cached_lines = nil
304344
end

0 commit comments

Comments
 (0)