Refactor oil and utils
This commit is contained in:
parent
9fc2ef3bb0
commit
061fbf36dc
2 changed files with 73 additions and 38 deletions
|
|
@ -1,18 +1,21 @@
|
||||||
local utils = require("utils")
|
local utils = require("utils")
|
||||||
|
|
||||||
local function lock_oil_buf_to_window(win_id, bufnr)
|
local oil = nil
|
||||||
local augroup_id = vim.api.nvim_create_augroup("LockWindowToBuffer" .. win_id, { clear = true })
|
local oil_window = nil
|
||||||
|
|
||||||
|
local function lock_oil_buf_to_window(bufnr)
|
||||||
|
local augroup_id = vim.api.nvim_create_augroup("LockOil" .. oil_window, { clear = true })
|
||||||
|
|
||||||
-- Create an autocommand group to manage the buffer lock
|
-- Create an autocommand group to manage the buffer lock
|
||||||
vim.api.nvim_create_autocmd("BufEnter", {
|
vim.api.nvim_create_autocmd("BufEnter", {
|
||||||
group = augroup_id,
|
group = augroup_id,
|
||||||
callback = function()
|
callback = function()
|
||||||
local current_win = vim.api.nvim_get_current_win()
|
local current_win = vim.api.nvim_get_current_win()
|
||||||
if current_win ~= win_id then
|
if current_win ~= oil_window then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local current_buf = vim.api.nvim_win_get_buf(win_id)
|
local current_buf = vim.api.nvim_win_get_buf(oil_window)
|
||||||
if current_buf == bufnr then
|
if current_buf == bufnr then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
@ -22,15 +25,43 @@ local function lock_oil_buf_to_window(win_id, bufnr)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_win_set_buf(win_id, bufnr)
|
vim.api.nvim_win_set_buf(oil_window, bufnr)
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function toggle_oil_window()
|
||||||
|
if oil_window and vim.api.nvim_win_is_valid(oil_window) then
|
||||||
|
vim.api.nvim_win_close(oil_window, true)
|
||||||
|
oil_window = nil
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Calculate the desired width (e.g., 20% of the terminal width)
|
||||||
|
local term_width = vim.api.nvim_get_option("columns")
|
||||||
|
local width_percentage = 0.175
|
||||||
|
local min_width = 30
|
||||||
|
local max_width = 50
|
||||||
|
local calculated_width = math.floor(term_width * width_percentage)
|
||||||
|
local final_width = math.min(math.max(calculated_width, min_width), max_width)
|
||||||
|
|
||||||
|
-- Open a vertical split with the calculated width on the left and open oil.nvim
|
||||||
|
vim.cmd("topleft vertical " .. final_width .. "vnew")
|
||||||
|
oil_window = vim.api.nvim_get_current_win()
|
||||||
|
vim.api.nvim_win_set_option(oil_window, "winfixwidth", true)
|
||||||
|
|
||||||
|
vim.api.nvim_win_set_option(oil_window, "winhighlight",
|
||||||
|
"Normal:Utility,FloatBorder:Utility")
|
||||||
|
|
||||||
|
oil.open()
|
||||||
|
local oil_buf_id = vim.api.nvim_get_current_buf()
|
||||||
|
lock_oil_buf_to_window(oil_buf_id)
|
||||||
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"stevearc/oil.nvim",
|
"stevearc/oil.nvim",
|
||||||
config = function()
|
config = function()
|
||||||
local oil = require("oil")
|
oil = require("oil")
|
||||||
oil.setup({
|
oil.setup({
|
||||||
view_options = {
|
view_options = {
|
||||||
show_hidden = true,
|
show_hidden = true,
|
||||||
|
|
@ -50,33 +81,7 @@ return {
|
||||||
require("utils").add_keymaps({
|
require("utils").add_keymaps({
|
||||||
n = {
|
n = {
|
||||||
["<leader>o"] = {
|
["<leader>o"] = {
|
||||||
cmd = function()
|
cmd = toggle_oil_window,
|
||||||
local oil_bufnr = utils.get_bufnr_for_filetype("oil")
|
|
||||||
if oil_bufnr then
|
|
||||||
vim.api.nvim_buf_delete(oil_bufnr, { force = true })
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Calculate the desired width (e.g., 20% of the terminal width)
|
|
||||||
local term_width = vim.api.nvim_get_option("columns")
|
|
||||||
local width_percentage = 0.175
|
|
||||||
local min_width = 30
|
|
||||||
local max_width = 50
|
|
||||||
local calculated_width = math.floor(term_width * width_percentage)
|
|
||||||
local final_width = math.min(math.max(calculated_width, min_width), max_width)
|
|
||||||
|
|
||||||
-- Open a vertical split with the calculated width on the left and open oil.nvim
|
|
||||||
vim.cmd("topleft vertical " .. final_width .. "vnew")
|
|
||||||
local win_id = vim.api.nvim_get_current_win()
|
|
||||||
vim.api.nvim_win_set_option(win_id, "winfixwidth", true)
|
|
||||||
|
|
||||||
vim.api.nvim_win_set_option(win_id, "winhighlight",
|
|
||||||
"Normal:Utility,FloatBorder:Utility")
|
|
||||||
|
|
||||||
oil.open()
|
|
||||||
local oil_buf_id = vim.api.nvim_get_current_buf()
|
|
||||||
lock_oil_buf_to_window(win_id, oil_buf_id)
|
|
||||||
end
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -9,17 +9,47 @@ function M.add_keymaps(maps)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_bufnr_for_filetype(filetype)
|
function M.get_bufnr_for_filetype(filetype)
|
||||||
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
|
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
|
||||||
if vim.bo[buf].filetype == filetype then
|
if vim.api.nvim_buf_is_loaded(bufnr) and M.is_buf_filetype(bufnr, filetype) then
|
||||||
return buf
|
return bufnr
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.is_buf_filetype(bufnr, filetype)
|
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
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue