Skip to content

Commit

Permalink
Merge pull request #138 from SidOfc/patch/removed-view
Browse files Browse the repository at this point in the history
patch/removed view
  • Loading branch information
SidOfc authored Mar 18, 2024
2 parents 9670efb + dc9efd9 commit b4f20d4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
14 changes: 7 additions & 7 deletions lua/carbon/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,29 @@ 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)

table.sort(names, sort_names)

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(
Expand All @@ -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

Expand Down
54 changes: 47 additions & 7 deletions lua/carbon/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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')
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b4f20d4

Please sign in to comment.