diff --git a/lua/carbon/health.lua b/lua/carbon/health.lua index 8977d5a..e2bc63a 100644 --- a/lua/carbon/health.lua +++ b/lua/carbon/health.lua @@ -29,21 +29,21 @@ function health.check() end function health.report_views() - vim.health.report_start('view::active') + vim.health.start('view::active') local view_roots = vim.tbl_map(function(item) return item.root - end, view.items) + end, view.get_sorted_items()) table.sort(view_roots) for _, root in ipairs(view_roots) do - vim.health.report_info(root.path) + vim.health.info(root.path) end end function health.report_events() - vim.health.report_start('watcher::events') + vim.health.start('watcher::events') local names = vim.tbl_keys(watcher.events) @@ -51,7 +51,7 @@ function health.report_events() for _, name in ipairs(names) do local callback_count = #vim.tbl_keys(watcher.events[name] or {}) - local reporter = callback_count == 0 and 'report_warn' or 'report_info' + local reporter = callback_count == 0 and 'warn' or 'info' vim.health[reporter]( string.format( @@ -65,14 +65,14 @@ function health.report_events() end function health.report_listeners() - vim.health.report_start('watcher::listeners') + vim.health.start('watcher::listeners') local paths = vim.tbl_keys(watcher.listeners) table.sort(paths, sort_paths) for _, path in ipairs(paths) do - vim.health.report_info(path) + vim.health.info(path) end end diff --git a/lua/carbon/view.lua b/lua/carbon/view.lua index 8691322..3545112 100644 --- a/lua/carbon/view.lua +++ b/lua/carbon/view.lua @@ -10,6 +10,7 @@ view.sidebar = { origin = -1, target = -1 } view.float = { origin = -1, target = -1 } view.items = {} view.resync_paths = {} +view.last_index = 0 local function create_leave(ctx) vim.cmd.stopinsert() @@ -63,6 +64,22 @@ local function create_insert_move(ctx) end end +function view.get_sorted_items() + local active_views = {} + + for _, current_view in pairs(view.items) do + if current_view then + active_views[#active_views + 1] = current_view + end + end + + table.sort(active_views, function(v1, v2) + return v1.index < v2.index + end) + + return active_views +end + function view.file_icons() if settings.file_icons then local ok, module = pcall(require, 'nvim-web-devicons') @@ -88,17 +105,18 @@ function view.get(path) return found_view end - local index = #view.items + 1 + view.last_index = view.last_index + 1 + local resolved = util.resolve(path) local instance = setmetatable({ - index = index, + index = view.last_index, initial = resolved, states = {}, show_hidden = false, root = entry.new(resolved), }, view) - view.items[index] = instance + view.items[instance.index] = instance return instance end @@ -224,10 +242,14 @@ function view.resync(path) end view.resync_timer = vim.defer_fn(function() - for _, current_view in ipairs(view.items) do - current_view.root:synchronize(view.resync_paths) - current_view:update() - current_view:render() + for _, current_view in pairs(view.items) do + if util.is_directory(current_view.root.path) then + current_view.root:synchronize(view.resync_paths) + current_view:update() + current_view:render() + else + current_view:terminate() + end end if not view.resync_timer:is_closing() then @@ -299,6 +321,24 @@ function view:buffers() end, vim.api.nvim_list_bufs()) end +function view:terminate() + local reopen = #vim.api.nvim_list_wins() == 1 + + for _, bufnr in ipairs(self:buffers()) do + vim.api.nvim_buf_delete(bufnr, { force = true }) + end + + if not util.is_directory(self.root.path) then + self.root:terminate() + end + + if reopen then + vim.cmd.Carbon() + end + + view.items[self.index] = nil +end + function view:update() self.cached_lines = nil end