Refactor oil and utils

This commit is contained in:
Martin Larsson 2024-05-26 17:24:10 +02:00
parent 9fc2ef3bb0
commit 061fbf36dc
2 changed files with 73 additions and 38 deletions

View file

@ -9,17 +9,47 @@ function M.add_keymaps(maps)
end
function M.get_bufnr_for_filetype(filetype)
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if vim.bo[buf].filetype == filetype then
return buf
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_loaded(bufnr) and M.is_buf_filetype(bufnr, filetype) then
return bufnr
end
end
return nil
end
function M.is_buf_filetype(bufnr, filetype)
return vim.bo[bufnr].filetype == filetype
return vim.api.nvim_buf_get_option(bufnr, "filetype") == filetype
end
function M.lock_buf_to_window(win_id, bufnr, filetype_check)
local augroup_id = vim.api.nvim_create_augroup("LockWindow" .. win_id, { clear = true })
vim.api.nvim_create_autocmd("BufEnter", {
group = augroup_id,
callback = function()
local current_win = vim.api.nvim_get_current_win()
if current_win ~= win_id then
return
end
local current_buf = vim.api.nvim_win_get_buf(win_id)
if current_buf == bufnr then
return
end
if filetype_check and filetype_check(current_buf) then
bufnr = current_buf
return
end
vim.api.nvim_win_set_buf(win_id, bufnr)
end,
})
end
function M.calculate_split_size(term_size, percentage, min_size, max_size)
local calculated_size = math.floor(term_size * percentage)
return math.min(math.max(calculated_size, min_size), max_size)
end
return M