From c866d90c881e38ad1805ab62be5f4ad8b5784996 Mon Sep 17 00:00:00 2001 From: Martin Larsson Date: Sat, 25 May 2024 02:15:43 +0200 Subject: [PATCH] Change so that oil nvim opens as a static window to the left of the terminal, much like a typical solution explorer --- home/.config/nvim/lua/plugs/oil.lua | 48 +++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/home/.config/nvim/lua/plugs/oil.lua b/home/.config/nvim/lua/plugs/oil.lua index 0255fa6..18dcfba 100644 --- a/home/.config/nvim/lua/plugs/oil.lua +++ b/home/.config/nvim/lua/plugs/oil.lua @@ -1,15 +1,57 @@ +local function get_oil_bufnr() + for _, buf in ipairs(vim.api.nvim_list_bufs()) do + if vim.bo[buf].filetype == "oil" then + return buf + end + end + + return nil +end + return { "stevearc/oil.nvim", config = function() local oil = require("oil") - oil.setup() + oil.setup({ + view_options = { + show_hidden = true, + }, + win_options = { + wrap = true, + signcolumn = "no", + cursorcolumn = false, + foldcolumn = "0", + spell = false, + list = false, + conceallevel = 3, + concealcursor = "nvic", + }, + }) require("utils").add_keymaps({ n = { ["o"] = { cmd = function() - oil.toggle_float(vim.fn.getcwd()) - end, + local oil_bufnr = get_oil_bufnr() + 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) + oil.open() + end } } })