refactor(Sidebar): Introduce Utils.is_valid_container to simplify container checks (#1882)

This commit is contained in:
aniaan
2025-04-16 14:18:49 +08:00
committed by GitHub
parent 54a1df28b6
commit dd83343e53
2 changed files with 40 additions and 76 deletions

View File

@@ -304,6 +304,32 @@ function M.is_valid_buf(bufnr)
return #vim.bo[bufnr].buftype == 0 and vim.bo[bufnr].modifiable
end
--- Check if a NUI container is valid:
--- 1. Container must exist
--- 2. Container must have a valid buffer number
--- 3. Container must have a valid window ID (optional, based on check_winid parameter)
--- Always returns a boolean value
---@param container NuiSplit | nil
---@param check_winid boolean? Whether to check window validity, defaults to false
---@return boolean
function M.is_valid_container(container, check_winid)
-- Default check_winid to false if not specified
if check_winid == nil then check_winid = false end
-- First check if container exists
if container == nil then return false end
-- Check buffer validity
if container.bufnr == nil or not api.nvim_buf_is_valid(container.bufnr) then return false end
-- Check window validity if requested
if check_winid then
if container.winid == nil or not api.nvim_win_is_valid(container.winid) then return false end
end
return true
end
---@param name string?
---@return table
function M.get_hl(name)