From eb5e57c5e3ec0f6221e1c7aae4a9d60e9537b79e Mon Sep 17 00:00:00 2001 From: Philipp Oeschger Date: Fri, 31 May 2024 01:21:23 +0200 Subject: [PATCH 01/20] implement floating window --- lua/oil/init.lua | 34 +++++++++++++++++++++++++++++----- lua/oil/util.lua | 7 +++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 04bd1b72..a5a819f6 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -337,7 +337,7 @@ M.open_float = function(dir) relative = "editor", row = win_opts.row, col = win_opts.col, - width = win_opts.width, + width = vim.api.nvim_win_get_width(winid), height = win_opts.height, title = get_title(), }) @@ -466,8 +466,34 @@ M.open_preview = function(opts, callback) opts.split = vim.o.splitright and "belowright" or "aboveleft" end end + + local preview_win = util.get_preview_win() + local prev_win = vim.api.nvim_get_current_win() + local bufnr = vim.api.nvim_get_current_buf() + + local padding = 2 + if util.is_floating_win() then - return finish("oil preview doesn't work in a floating window") + if preview_win == nil then + local oilconfig = require("oil.config") + local config = vim.api.nvim_win_get_config(0) + local newWidth = math.floor(config.width / 2) - (padding / 2) + + vim.api.nvim_win_set_width(0, newWidth) + local win_opts = { + relative = "editor", + width = newWidth, + height = config.height, + row = config.row, + col = newWidth + config.col + padding, + border = oilconfig.float.border, + zindex = 152, + title = 'Preview' + } + -- local test = vim.b.oil_preview_buffer + preview_win = vim.api.nvim_open_win(bufnr, false, win_opts) + vim.wo[preview_win].previewwindow = true + end end local entry = M.get_cursor_entry() @@ -475,9 +501,6 @@ M.open_preview = function(opts, callback) return finish("Could not find entry under cursor") end - local preview_win = util.get_preview_win() - local prev_win = vim.api.nvim_get_current_win() - local bufnr = vim.api.nvim_get_current_buf() local cmd = preview_win and "buffer" or "sbuffer" local mods = { @@ -503,6 +526,7 @@ M.open_preview = function(opts, callback) end end + util.get_edit_path(bufnr, entry, function(normalized_url) local filebufnr = vim.fn.bufadd(normalized_url) local entry_is_file = not vim.endswith(normalized_url, "/") diff --git a/lua/oil/util.lua b/lua/oil/util.lua index a685b145..e1938de5 100644 --- a/lua/oil/util.lua +++ b/lua/oil/util.lua @@ -658,6 +658,13 @@ end ---@return nil|integer M.get_preview_win = function() + if M.is_floating_win() then + for _, winid in ipairs(vim.api.nvim_list_wins()) do + if vim.api.nvim_win_is_valid(winid) and vim.wo[winid].previewwindow then + return winid + end + end + end for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do if vim.api.nvim_win_is_valid(winid) and vim.wo[winid].previewwindow then return winid From 74727780ff4d55478d258e0d698d52a18c3e0cd6 Mon Sep 17 00:00:00 2001 From: Philipp Oeschger Date: Fri, 31 May 2024 01:21:46 +0200 Subject: [PATCH 02/20] reset width on closing window --- lua/oil/actions.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lua/oil/actions.lua b/lua/oil/actions.lua index cebd6b1c..e84dd7a0 100644 --- a/lua/oil/actions.lua +++ b/lua/oil/actions.lua @@ -53,6 +53,18 @@ M.preview = { local cur_id = vim.w[winid].oil_entry_id if entry.id == cur_id then vim.api.nvim_win_close(winid, true) + if util.is_floating_win() then + local config = require("oil.config") + local total_width = vim.o.columns + local width = total_width - 2 * config.float.padding + if config.float.border ~= "none" then + width = width - 2 -- The border consumes 1 col on each side + end + if config.float.max_width > 0 then + width = math.min(width, config.float.max_width) + end + vim.api.nvim_win_set_width(0, width) + end return end end From 6a2454fef84c118807b0343aeb9291e5a7a6aea7 Mon Sep 17 00:00:00 2001 From: Philipp Oeschger Date: Fri, 31 May 2024 01:46:05 +0200 Subject: [PATCH 03/20] use gap from new config parameter --- lua/oil/config.lua | 2 ++ lua/oil/init.lua | 6 ++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lua/oil/config.lua b/lua/oil/config.lua index 1defbe1e..9d16d426 100644 --- a/lua/oil/config.lua +++ b/lua/oil/config.lua @@ -121,6 +121,8 @@ local default_config = { win_options = { winblend = 0, }, + -- gap between oil window and preview window + preview_gap = 2, -- This is the config that will be passed to nvim_open_win. -- Change values here to customize the layout override = function(conf) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index a5a819f6..27a35eb0 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -471,13 +471,11 @@ M.open_preview = function(opts, callback) local prev_win = vim.api.nvim_get_current_win() local bufnr = vim.api.nvim_get_current_buf() - local padding = 2 - if util.is_floating_win() then if preview_win == nil then local oilconfig = require("oil.config") local config = vim.api.nvim_win_get_config(0) - local newWidth = math.floor(config.width / 2) - (padding / 2) + local newWidth = math.floor(config.width / 2) - (oilconfig.float.preview_gap / 2) vim.api.nvim_win_set_width(0, newWidth) local win_opts = { @@ -485,7 +483,7 @@ M.open_preview = function(opts, callback) width = newWidth, height = config.height, row = config.row, - col = newWidth + config.col + padding, + col = newWidth + config.col + oilconfig.float.preview_gap, border = oilconfig.float.border, zindex = 152, title = 'Preview' From 00bbe54198e0fe5212d92eccc596d1f997452cc3 Mon Sep 17 00:00:00 2001 From: Philipp Oeschger Date: Fri, 31 May 2024 02:27:09 +0200 Subject: [PATCH 04/20] use minimal style for preview in floating --- lua/oil/init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 27a35eb0..673e0869 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -486,9 +486,9 @@ M.open_preview = function(opts, callback) col = newWidth + config.col + oilconfig.float.preview_gap, border = oilconfig.float.border, zindex = 152, - title = 'Preview' + title = 'Preview', + style = 'minimal' } - -- local test = vim.b.oil_preview_buffer preview_win = vim.api.nvim_open_win(bufnr, false, win_opts) vim.wo[preview_win].previewwindow = true end From 9a2da212703ce1a9526484a7c83b668400721e41 Mon Sep 17 00:00:00 2001 From: Philipp Oeschger Date: Fri, 31 May 2024 02:28:17 +0200 Subject: [PATCH 05/20] lower z-index --- lua/oil/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 673e0869..2f094c07 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -485,7 +485,7 @@ M.open_preview = function(opts, callback) row = config.row, col = newWidth + config.col + oilconfig.float.preview_gap, border = oilconfig.float.border, - zindex = 152, + zindex = 45, title = 'Preview', style = 'minimal' } From 61719510bafc7d52f0c66ee7d4d6336e11df9bff Mon Sep 17 00:00:00 2001 From: Philipp Oeschger Date: Sat, 1 Jun 2024 00:29:39 +0200 Subject: [PATCH 06/20] add configuration of preview position in floating window --- lua/oil/actions.lua | 27 ++++++++++----- lua/oil/config.lua | 2 ++ lua/oil/init.lua | 80 +++++++++++++++++++++++++++++++++++++-------- 3 files changed, 87 insertions(+), 22 deletions(-) diff --git a/lua/oil/actions.lua b/lua/oil/actions.lua index e84dd7a0..ce5cd0a0 100644 --- a/lua/oil/actions.lua +++ b/lua/oil/actions.lua @@ -54,16 +54,27 @@ M.preview = { if entry.id == cur_id then vim.api.nvim_win_close(winid, true) if util.is_floating_win() then - local config = require("oil.config") - local total_width = vim.o.columns - local width = total_width - 2 * config.float.padding - if config.float.border ~= "none" then - width = width - 2 -- The border consumes 1 col on each side + local config = require('oil.config') + + local window_conf = vim.api.nvim_win_get_config(0) + + if config.float.preview_split == 'left' then + window_conf.col = window_conf.col - window_conf.width - config.float.preview_gap + end + + if config.float.preview_split == 'above' then + window_conf.row = window_conf.row - window_conf.height - config.float.preview_gap end - if config.float.max_width > 0 then - width = math.min(width, config.float.max_width) + + if config.float.preview_split == 'left' or config.float.preview_split == 'right' then + window_conf.width = window_conf.width * 2 + config.float.preview_gap + end + + if config.float.preview_split == 'above' or config.float.preview_split == 'below' then + window_conf.height = window_conf.height * 2 + config.float.preview_gap end - vim.api.nvim_win_set_width(0, width) + + vim.api.nvim_win_set_config(0, window_conf) end return end diff --git a/lua/oil/config.lua b/lua/oil/config.lua index 9d16d426..3df5e30f 100644 --- a/lua/oil/config.lua +++ b/lua/oil/config.lua @@ -123,6 +123,8 @@ local default_config = { }, -- gap between oil window and preview window preview_gap = 2, + -- preview_split: Split direction: "left", "right", "above", "below". + preview_split = 'below', -- This is the config that will be passed to nvim_open_win. -- Change values here to customize the layout override = function(conf) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 2f094c07..ab40da81 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -270,6 +270,7 @@ M.open_float = function(dir) end local row = math.floor((total_height - height) / 2) local col = math.floor((total_width - width) / 2) - 1 -- adjust for border width + local win_opts = { relative = "editor", width = width, @@ -333,12 +334,13 @@ M.open_float = function(dir) if not vim.api.nvim_win_is_valid(winid) or vim.api.nvim_win_get_buf(winid) ~= winbuf then return end + local cur_win_opts = vim.api.nvim_win_get_config(winid) vim.api.nvim_win_set_config(winid, { relative = "editor", - row = win_opts.row, - col = win_opts.col, - width = vim.api.nvim_win_get_width(winid), - height = win_opts.height, + row = cur_win_opts.row, + col = cur_win_opts.col, + width = cur_win_opts.width, + height = cur_win_opts.height, title = get_title(), }) end, @@ -473,22 +475,72 @@ M.open_preview = function(opts, callback) if util.is_floating_win() then if preview_win == nil then - local oilconfig = require("oil.config") - local config = vim.api.nvim_win_get_config(0) - local newWidth = math.floor(config.width / 2) - (oilconfig.float.preview_gap / 2) + local config = require("oil.config") + -- local default_dims = util.get_floating_win_default_dimensions() + local float_config = vim.api.nvim_win_get_config(0) + + local dimesions_preview = { + width = float_config.width, + height = float_config.height, + col = float_config.col, + row = float_config.row, + } - vim.api.nvim_win_set_width(0, newWidth) - local win_opts = { + local dimesions_oil_window = { + width = float_config.width, + height = float_config.height, + col = float_config.col, + row = float_config.row, + } + + if config.float.preview_split == 'left' or config.float.preview_split == 'right' then + dimesions_preview.width = math.floor(float_config.width / 2) - config.float.preview_gap + dimesions_oil_window.width = dimesions_preview.width + end + + if config.float.preview_split == 'above' or config.float.preview_split == 'below' then + dimesions_preview.height = math.floor(float_config.height / 2) - config.float.preview_gap + dimesions_oil_window.height = dimesions_preview.height + end + + if config.float.preview_split == 'left' then + dimesions_oil_window.col = dimesions_oil_window.col + dimesions_oil_window.width + config.float.preview_gap + end + if config.float.preview_split == 'right' then + dimesions_preview.col = dimesions_preview.col + dimesions_preview.width + config.float.preview_gap + end + + if config.float.preview_split == 'above' then + dimesions_oil_window.row = dimesions_oil_window.row + dimesions_oil_window.height + + config.float.preview_gap + end + if config.float.preview_split == 'below' then + dimesions_preview.row = dimesions_preview.row + dimesions_preview.height + + config.float.preview_gap + end + + local win_opts_oil = { relative = "editor", - width = newWidth, - height = config.height, - row = config.row, - col = newWidth + config.col + oilconfig.float.preview_gap, - border = oilconfig.float.border, + width = dimesions_oil_window.width, + height = dimesions_oil_window.height, + row = dimesions_oil_window.row, + col = dimesions_oil_window.col, + border = config.float.border, zindex = 45, + } + vim.api.nvim_win_set_config(0, win_opts_oil) + local win_opts = { + relative = "editor", + width = dimesions_preview.width, + height = dimesions_preview.height, + row = dimesions_preview.row, + col = dimesions_preview.col, + border = config.float.border, + zindex = 153, title = 'Preview', style = 'minimal' } + preview_win = vim.api.nvim_open_win(bufnr, false, win_opts) vim.wo[preview_win].previewwindow = true end From 5f5dc7b349892afa7702dbba9dc98330a987c4bf Mon Sep 17 00:00:00 2001 From: Philipp Oeschger Date: Sun, 2 Jun 2024 15:51:15 +0200 Subject: [PATCH 07/20] fix in verions earlier than nvim 0.10 --- lua/oil/actions.lua | 5 +++++ lua/oil/config.lua | 2 +- lua/oil/init.lua | 11 +++++++++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lua/oil/actions.lua b/lua/oil/actions.lua index ce5cd0a0..99e757be 100644 --- a/lua/oil/actions.lua +++ b/lua/oil/actions.lua @@ -58,6 +58,11 @@ M.preview = { local window_conf = vim.api.nvim_win_get_config(0) + if vim.fn.has('nvim-0.10') == 0 then + -- read https://github.com/neovim/neovim/issues/24430 for more infos. + window_conf.col = window_conf.col[vim.val_idx] + window_conf.row = window_conf.row[vim.val_idx] + end if config.float.preview_split == 'left' then window_conf.col = window_conf.col - window_conf.width - config.float.preview_gap end diff --git a/lua/oil/config.lua b/lua/oil/config.lua index 3df5e30f..ddd22fbc 100644 --- a/lua/oil/config.lua +++ b/lua/oil/config.lua @@ -124,7 +124,7 @@ local default_config = { -- gap between oil window and preview window preview_gap = 2, -- preview_split: Split direction: "left", "right", "above", "below". - preview_split = 'below', + preview_split = 'left', -- This is the config that will be passed to nvim_open_win. -- Change values here to customize the layout override = function(conf) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index ab40da81..aa8badc3 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -493,13 +493,20 @@ M.open_preview = function(opts, callback) row = float_config.row, } + if vim.fn.has('nvim-0.10') == 0 then + -- read https://github.com/neovim/neovim/issues/24430 for more infos. + dimesions_preview.col = float_config.col[vim.val_idx] + dimesions_preview.row = float_config.row[vim.val_idx] + dimesions_oil_window.col = float_config.col[vim.val_idx] + dimesions_oil_window.row = float_config.row[vim.val_idx] + end if config.float.preview_split == 'left' or config.float.preview_split == 'right' then - dimesions_preview.width = math.floor(float_config.width / 2) - config.float.preview_gap + dimesions_preview.width = math.floor(float_config.width / 2) - (config.float.preview_gap / 2) dimesions_oil_window.width = dimesions_preview.width end if config.float.preview_split == 'above' or config.float.preview_split == 'below' then - dimesions_preview.height = math.floor(float_config.height / 2) - config.float.preview_gap + dimesions_preview.height = math.floor(float_config.height / 2) - (config.float.preview_gap / 2) dimesions_oil_window.height = dimesions_preview.height end From 0d55bd50a1071ef07e6966db5b5a9e7fbf81c002 Mon Sep 17 00:00:00 2001 From: Philipp Oeschger Date: Tue, 4 Jun 2024 00:06:44 +0200 Subject: [PATCH 08/20] close preview on opening floating window Close the any existing preview because otherwise strange errors happen when the preview is open and the floating window is opened at the same time. --- lua/oil/init.lua | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index aa8badc3..d8a9378e 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -245,6 +245,12 @@ M.open_float = function(dir) local layout = require("oil.layout") local util = require("oil.util") local view = require("oil.view") + + local preview_win = util.get_preview_win() + if preview_win ~= nil then + vim.api.nvim_win_close(preview_win, true) + end + local parent_url, basename = M.get_url_for_path(dir) if not parent_url then return @@ -377,9 +383,9 @@ local function update_preview_window(oil_bufnr) local cursor_entry = M.get_cursor_entry() local preview_win_id = util.get_preview_win() if - cursor_entry - and preview_win_id - and cursor_entry.id ~= vim.w[preview_win_id].oil_entry_id + cursor_entry + and preview_win_id + and cursor_entry.id ~= vim.w[preview_win_id].oil_entry_id then M.open_preview() end @@ -791,9 +797,9 @@ M.select = function(opts, callback) return finish(err) end if - opts.close - and vim.api.nvim_win_is_valid(prev_win) - and prev_win ~= vim.api.nvim_get_current_win() + opts.close + and vim.api.nvim_win_is_valid(prev_win) + and prev_win ~= vim.api.nvim_get_current_win() then vim.api.nvim_win_call(prev_win, function() M.close() @@ -968,7 +974,7 @@ local function restore_alt_buf() -- If we are editing the same buffer that we started oil from, set the alternate to be -- what it was before we opened oil local has_orig_alt, alt_buffer = - pcall(vim.api.nvim_win_get_var, 0, "oil_original_alternate") + pcall(vim.api.nvim_win_get_var, 0, "oil_original_alternate") if has_orig_alt and vim.api.nvim_buf_is_valid(alt_buffer) then vim.fn.setreg("#", alt_buffer) end @@ -1179,11 +1185,11 @@ M.setup = function(opts) local winid = vim.api.nvim_get_current_win() -- If the user issued a :wq or similar, we should quit after saving local quit_after_save = vim.endswith(last_keys, ":wq\r") - or vim.endswith(last_keys, ":x\r") - or vim.endswith(last_keys, "ZZ") + or vim.endswith(last_keys, ":x\r") + or vim.endswith(last_keys, "ZZ") local quit_all = vim.endswith(last_keys, ":wqa\r") - or vim.endswith(last_keys, ":wqal\r") - or vim.endswith(last_keys, ":wqall\r") + or vim.endswith(last_keys, ":wqal\r") + or vim.endswith(last_keys, ":wqall\r") local bufname = vim.api.nvim_buf_get_name(params.buf) if vim.endswith(bufname, "/") then vim.cmd.doautocmd({ args = { "BufWritePre", params.file }, mods = { silent = true } }) From 2910802ec5379e0ac5a16c27784016cfe6c518b6 Mon Sep 17 00:00:00 2001 From: Philipp Oeschger Date: Tue, 4 Jun 2024 00:11:54 +0200 Subject: [PATCH 09/20] reset formatting changes --- lua/oil/init.lua | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index d8a9378e..c9b70a7e 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -383,9 +383,9 @@ local function update_preview_window(oil_bufnr) local cursor_entry = M.get_cursor_entry() local preview_win_id = util.get_preview_win() if - cursor_entry - and preview_win_id - and cursor_entry.id ~= vim.w[preview_win_id].oil_entry_id + cursor_entry + and preview_win_id + and cursor_entry.id ~= vim.w[preview_win_id].oil_entry_id then M.open_preview() end @@ -797,9 +797,9 @@ M.select = function(opts, callback) return finish(err) end if - opts.close - and vim.api.nvim_win_is_valid(prev_win) - and prev_win ~= vim.api.nvim_get_current_win() + opts.close + and vim.api.nvim_win_is_valid(prev_win) + and prev_win ~= vim.api.nvim_get_current_win() then vim.api.nvim_win_call(prev_win, function() M.close() @@ -974,7 +974,7 @@ local function restore_alt_buf() -- If we are editing the same buffer that we started oil from, set the alternate to be -- what it was before we opened oil local has_orig_alt, alt_buffer = - pcall(vim.api.nvim_win_get_var, 0, "oil_original_alternate") + pcall(vim.api.nvim_win_get_var, 0, "oil_original_alternate") if has_orig_alt and vim.api.nvim_buf_is_valid(alt_buffer) then vim.fn.setreg("#", alt_buffer) end @@ -1185,11 +1185,11 @@ M.setup = function(opts) local winid = vim.api.nvim_get_current_win() -- If the user issued a :wq or similar, we should quit after saving local quit_after_save = vim.endswith(last_keys, ":wq\r") - or vim.endswith(last_keys, ":x\r") - or vim.endswith(last_keys, "ZZ") + or vim.endswith(last_keys, ":x\r") + or vim.endswith(last_keys, "ZZ") local quit_all = vim.endswith(last_keys, ":wqa\r") - or vim.endswith(last_keys, ":wqal\r") - or vim.endswith(last_keys, ":wqall\r") + or vim.endswith(last_keys, ":wqal\r") + or vim.endswith(last_keys, ":wqall\r") local bufname = vim.api.nvim_buf_get_name(params.buf) if vim.endswith(bufname, "/") then vim.cmd.doautocmd({ args = { "BufWritePre", params.file }, mods = { silent = true } }) From 12f3cb7e24a184efcdd465070005e3bc72c337a7 Mon Sep 17 00:00:00 2001 From: Philipp Oeschger Date: Tue, 4 Jun 2024 00:12:26 +0200 Subject: [PATCH 10/20] remove empty line --- lua/oil/init.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index c9b70a7e..56306e33 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -589,7 +589,6 @@ M.open_preview = function(opts, callback) end end - util.get_edit_path(bufnr, entry, function(normalized_url) local filebufnr = vim.fn.bufadd(normalized_url) local entry_is_file = not vim.endswith(normalized_url, "/") From 83cd535d0ac4c6d0af2d1a50fa1e1c9453ac1232 Mon Sep 17 00:00:00 2001 From: Philipp Oeschger Date: Tue, 4 Jun 2024 00:23:42 +0200 Subject: [PATCH 11/20] change z-index of preview window to floating window z-index --- lua/oil/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 56306e33..ea65d973 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -549,7 +549,7 @@ M.open_preview = function(opts, callback) row = dimesions_preview.row, col = dimesions_preview.col, border = config.float.border, - zindex = 153, + zindex = 45, title = 'Preview', style = 'minimal' } From edea969b3fe2f299568fbd4c035b86822b9b5f84 Mon Sep 17 00:00:00 2001 From: Philipp Oeschger Date: Tue, 4 Jun 2024 00:24:01 +0200 Subject: [PATCH 12/20] add configurations to oil.txt --- doc/oil.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/oil.txt b/doc/oil.txt index 6485a04d..e91e9b84 100644 --- a/doc/oil.txt +++ b/doc/oil.txt @@ -138,6 +138,10 @@ CONFIG *oil-confi win_options = { winblend = 0, }, + -- gap between oil window and preview window + preview_gap = 2, + -- preview_split: Split direction: "left", "right", "above", "below". + preview_split = 'left', -- This is the config that will be passed to nvim_open_win. -- Change values here to customize the layout override = function(conf) From ea8dc94c075e0663a66ef8c70bcbcb298ab04d3a Mon Sep 17 00:00:00 2001 From: Philipp Oeschger Date: Fri, 7 Jun 2024 23:12:56 +0200 Subject: [PATCH 13/20] formatting --- lua/oil/actions.lua | 12 ++++++------ lua/oil/config.lua | 4 ++-- lua/oil/init.lua | 48 +++++++++++++++++++++++---------------------- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/lua/oil/actions.lua b/lua/oil/actions.lua index 99e757be..380ad602 100644 --- a/lua/oil/actions.lua +++ b/lua/oil/actions.lua @@ -54,28 +54,28 @@ M.preview = { if entry.id == cur_id then vim.api.nvim_win_close(winid, true) if util.is_floating_win() then - local config = require('oil.config') + local config = require("oil.config") local window_conf = vim.api.nvim_win_get_config(0) - if vim.fn.has('nvim-0.10') == 0 then + if vim.fn.has("nvim-0.10") == 0 then -- read https://github.com/neovim/neovim/issues/24430 for more infos. window_conf.col = window_conf.col[vim.val_idx] window_conf.row = window_conf.row[vim.val_idx] end - if config.float.preview_split == 'left' then + if config.float.preview_split == "left" then window_conf.col = window_conf.col - window_conf.width - config.float.preview_gap end - if config.float.preview_split == 'above' then + if config.float.preview_split == "above" then window_conf.row = window_conf.row - window_conf.height - config.float.preview_gap end - if config.float.preview_split == 'left' or config.float.preview_split == 'right' then + if config.float.preview_split == "left" or config.float.preview_split == "right" then window_conf.width = window_conf.width * 2 + config.float.preview_gap end - if config.float.preview_split == 'above' or config.float.preview_split == 'below' then + if config.float.preview_split == "above" or config.float.preview_split == "below" then window_conf.height = window_conf.height * 2 + config.float.preview_gap end diff --git a/lua/oil/config.lua b/lua/oil/config.lua index ddd22fbc..4df7878f 100644 --- a/lua/oil/config.lua +++ b/lua/oil/config.lua @@ -1,6 +1,6 @@ local default_config = { -- Oil will take over directory buffers (e.g. `vim .` or `:e src/`) - -- Set to false if you still want to use netrw. + -- Set to false if you still want to use netrw.conf default_file_explorer = true, -- Id is automatically added at the beginning, and name at the end -- See :help oil-columns @@ -124,7 +124,7 @@ local default_config = { -- gap between oil window and preview window preview_gap = 2, -- preview_split: Split direction: "left", "right", "above", "below". - preview_split = 'left', + preview_split = "left", -- This is the config that will be passed to nvim_open_win. -- Change values here to customize the layout override = function(conf) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index ea65d973..95f507e6 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -246,11 +246,6 @@ M.open_float = function(dir) local util = require("oil.util") local view = require("oil.view") - local preview_win = util.get_preview_win() - if preview_win ~= nil then - vim.api.nvim_win_close(preview_win, true) - end - local parent_url, basename = M.get_url_for_path(dir) if not parent_url then return @@ -499,37 +494,45 @@ M.open_preview = function(opts, callback) row = float_config.row, } - if vim.fn.has('nvim-0.10') == 0 then + if vim.fn.has("nvim-0.10") == 0 then -- read https://github.com/neovim/neovim/issues/24430 for more infos. dimesions_preview.col = float_config.col[vim.val_idx] dimesions_preview.row = float_config.row[vim.val_idx] dimesions_oil_window.col = float_config.col[vim.val_idx] dimesions_oil_window.row = float_config.row[vim.val_idx] end - if config.float.preview_split == 'left' or config.float.preview_split == 'right' then - dimesions_preview.width = math.floor(float_config.width / 2) - (config.float.preview_gap / 2) + if config.float.preview_split == "left" or config.float.preview_split == "right" then + dimesions_preview.width = math.floor(float_config.width / 2) + - (config.float.preview_gap / 2) dimesions_oil_window.width = dimesions_preview.width end - if config.float.preview_split == 'above' or config.float.preview_split == 'below' then - dimesions_preview.height = math.floor(float_config.height / 2) - (config.float.preview_gap / 2) + if config.float.preview_split == "above" or config.float.preview_split == "below" then + dimesions_preview.height = math.floor(float_config.height / 2) + - (config.float.preview_gap / 2) dimesions_oil_window.height = dimesions_preview.height end - if config.float.preview_split == 'left' then - dimesions_oil_window.col = dimesions_oil_window.col + dimesions_oil_window.width + config.float.preview_gap + if config.float.preview_split == "left" then + dimesions_oil_window.col = dimesions_oil_window.col + + dimesions_oil_window.width + + config.float.preview_gap end - if config.float.preview_split == 'right' then - dimesions_preview.col = dimesions_preview.col + dimesions_preview.width + config.float.preview_gap + if config.float.preview_split == "right" then + dimesions_preview.col = dimesions_preview.col + + dimesions_preview.width + + config.float.preview_gap end - if config.float.preview_split == 'above' then - dimesions_oil_window.row = dimesions_oil_window.row + dimesions_oil_window.height + - config.float.preview_gap + if config.float.preview_split == "above" then + dimesions_oil_window.row = dimesions_oil_window.row + + dimesions_oil_window.height + + config.float.preview_gap end - if config.float.preview_split == 'below' then - dimesions_preview.row = dimesions_preview.row + dimesions_preview.height + - config.float.preview_gap + if config.float.preview_split == "below" then + dimesions_preview.row = dimesions_preview.row + + dimesions_preview.height + + config.float.preview_gap end local win_opts_oil = { @@ -550,8 +553,8 @@ M.open_preview = function(opts, callback) col = dimesions_preview.col, border = config.float.border, zindex = 45, - title = 'Preview', - style = 'minimal' + title = "Preview", + style = "minimal", } preview_win = vim.api.nvim_open_win(bufnr, false, win_opts) @@ -564,7 +567,6 @@ M.open_preview = function(opts, callback) return finish("Could not find entry under cursor") end - local cmd = preview_win and "buffer" or "sbuffer" local mods = { vertical = opts.vertical, From 5841e2f0e5f80525f90de1db601a557e69b96c9c Mon Sep 17 00:00:00 2001 From: Philipp Oeschger Date: Sat, 8 Jun 2024 01:25:03 +0200 Subject: [PATCH 14/20] add auto configuration --- lua/oil/actions.lua | 11 +++++++++-- lua/oil/config.lua | 4 ++-- lua/oil/init.lua | 23 ++++++++++++++++++----- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/lua/oil/actions.lua b/lua/oil/actions.lua index 380ad602..4e9ee87f 100644 --- a/lua/oil/actions.lua +++ b/lua/oil/actions.lua @@ -63,7 +63,10 @@ M.preview = { window_conf.col = window_conf.col[vim.val_idx] window_conf.row = window_conf.row[vim.val_idx] end - if config.float.preview_split == "left" then + if + config.float.preview_split == "left" + or (config.float.preview_split == "auto" and not vim.o.splitright) + then window_conf.col = window_conf.col - window_conf.width - config.float.preview_gap end @@ -71,7 +74,11 @@ M.preview = { window_conf.row = window_conf.row - window_conf.height - config.float.preview_gap end - if config.float.preview_split == "left" or config.float.preview_split == "right" then + if + config.float.preview_split == "left" + or config.float.preview_split == "right" + or config.float.preview_split == "auto" + then window_conf.width = window_conf.width * 2 + config.float.preview_gap end diff --git a/lua/oil/config.lua b/lua/oil/config.lua index 4df7878f..3c8ad995 100644 --- a/lua/oil/config.lua +++ b/lua/oil/config.lua @@ -123,8 +123,8 @@ local default_config = { }, -- gap between oil window and preview window preview_gap = 2, - -- preview_split: Split direction: "left", "right", "above", "below". - preview_split = "left", + -- preview_split: Split direction: "auto", "left", "right", "above", "below". + preview_split = "auto", -- This is the config that will be passed to nvim_open_win. -- Change values here to customize the layout override = function(conf) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 95f507e6..d683e539 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -501,7 +501,11 @@ M.open_preview = function(opts, callback) dimesions_oil_window.col = float_config.col[vim.val_idx] dimesions_oil_window.row = float_config.row[vim.val_idx] end - if config.float.preview_split == "left" or config.float.preview_split == "right" then + if + config.float.preview_split == "left" + or config.float.preview_split == "right" + or config.float.preview_split == "auto" + then dimesions_preview.width = math.floor(float_config.width / 2) - (config.float.preview_gap / 2) dimesions_oil_window.width = dimesions_preview.width @@ -513,12 +517,18 @@ M.open_preview = function(opts, callback) dimesions_oil_window.height = dimesions_preview.height end - if config.float.preview_split == "left" then + if + config.float.preview_split == "left" + or (config.float.preview_split == "auto" and not vim.o.splitright) + then dimesions_oil_window.col = dimesions_oil_window.col + dimesions_oil_window.width + config.float.preview_gap end - if config.float.preview_split == "right" then + if + config.float.preview_split == "right" + or (config.float.preview_split == "auto" and vim.o.splitright) + then dimesions_preview.col = dimesions_preview.col + dimesions_preview.width + config.float.preview_gap @@ -553,12 +563,15 @@ M.open_preview = function(opts, callback) col = dimesions_preview.col, border = config.float.border, zindex = 45, + focusable = false, + noautocmd = true, title = "Preview", style = "minimal", } - preview_win = vim.api.nvim_open_win(bufnr, false, win_opts) - vim.wo[preview_win].previewwindow = true + preview_win = vim.api.nvim_open_win(bufnr, true, win_opts) + vim.api.nvim_set_option_value("previewwindow", true, { scope = "local", win = preview_win }) + vim.api.nvim_set_current_win(prev_win) end end From 7e08910104fb4d823a6d6ec8e49d14b433e8fd4b Mon Sep 17 00:00:00 2001 From: Philipp Oeschger Date: Fri, 14 Jun 2024 22:02:52 +0200 Subject: [PATCH 15/20] update oil doc --- doc/oil.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/oil.txt b/doc/oil.txt index e91e9b84..fca840fe 100644 --- a/doc/oil.txt +++ b/doc/oil.txt @@ -140,8 +140,8 @@ CONFIG *oil-confi }, -- gap between oil window and preview window preview_gap = 2, - -- preview_split: Split direction: "left", "right", "above", "below". - preview_split = 'left', + -- preview_split: Split direction: "auto", "left", "right", "above", "below". + preview_split = 'auto', -- This is the config that will be passed to nvim_open_win. -- Change values here to customize the layout override = function(conf) From cf4dd642a7a02e7e06dc278c8fb3b022aa5f4097 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Mon, 17 Jun 2024 22:43:42 -0400 Subject: [PATCH 16/20] refactor: move logic into layout.lua and eliminate flicker --- README.md | 6 +- doc/oil.txt | 4 +- lua/oil/actions.lua | 36 +----------- lua/oil/config.lua | 2 +- lua/oil/init.lua | 131 +++++++------------------------------------- lua/oil/layout.lua | 83 ++++++++++++++++++++++++++++ 6 files changed, 115 insertions(+), 147 deletions(-) diff --git a/README.md b/README.md index d96ed189..9bbfa445 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ You can open a directory with `:edit ` or `:Oil `. To open oil in a ```lua require("oil").setup({ -- Oil will take over directory buffers (e.g. `vim .` or `:e src/`) - -- Set to false if you still want to use netrw. + -- Set to false if you still want to use netrw default_file_explorer = true, -- Id is automatically added at the beginning, and name at the end -- See :help oil-columns @@ -247,6 +247,10 @@ require("oil").setup({ win_options = { winblend = 0, }, + -- gap between oil window and preview window + preview_gap = 2, + -- preview_split: Split direction: "auto", "left", "right", "above", "below". + preview_split = "auto", -- This is the config that will be passed to nvim_open_win. -- Change values here to customize the layout override = function(conf) diff --git a/doc/oil.txt b/doc/oil.txt index fca840fe..b2b0502b 100644 --- a/doc/oil.txt +++ b/doc/oil.txt @@ -17,7 +17,7 @@ CONFIG *oil-confi >lua require("oil").setup({ -- Oil will take over directory buffers (e.g. `vim .` or `:e src/`) - -- Set to false if you still want to use netrw. + -- Set to false if you still want to use netrw default_file_explorer = true, -- Id is automatically added at the beginning, and name at the end -- See :help oil-columns @@ -141,7 +141,7 @@ CONFIG *oil-confi -- gap between oil window and preview window preview_gap = 2, -- preview_split: Split direction: "auto", "left", "right", "above", "below". - preview_split = 'auto', + preview_split = "auto", -- This is the config that will be passed to nvim_open_win. -- Change values here to customize the layout override = function(conf) diff --git a/lua/oil/actions.lua b/lua/oil/actions.lua index 4e9ee87f..36712dea 100644 --- a/lua/oil/actions.lua +++ b/lua/oil/actions.lua @@ -54,39 +54,9 @@ M.preview = { if entry.id == cur_id then vim.api.nvim_win_close(winid, true) if util.is_floating_win() then - local config = require("oil.config") - - local window_conf = vim.api.nvim_win_get_config(0) - - if vim.fn.has("nvim-0.10") == 0 then - -- read https://github.com/neovim/neovim/issues/24430 for more infos. - window_conf.col = window_conf.col[vim.val_idx] - window_conf.row = window_conf.row[vim.val_idx] - end - if - config.float.preview_split == "left" - or (config.float.preview_split == "auto" and not vim.o.splitright) - then - window_conf.col = window_conf.col - window_conf.width - config.float.preview_gap - end - - if config.float.preview_split == "above" then - window_conf.row = window_conf.row - window_conf.height - config.float.preview_gap - end - - if - config.float.preview_split == "left" - or config.float.preview_split == "right" - or config.float.preview_split == "auto" - then - window_conf.width = window_conf.width * 2 + config.float.preview_gap - end - - if config.float.preview_split == "above" or config.float.preview_split == "below" then - window_conf.height = window_conf.height * 2 + config.float.preview_gap - end - - vim.api.nvim_win_set_config(0, window_conf) + local layout = require("oil.layout") + local win_opts = layout.get_fullscreen_win_opts() + vim.api.nvim_win_set_config(0, win_opts) end return end diff --git a/lua/oil/config.lua b/lua/oil/config.lua index 3c8ad995..df4d7afb 100644 --- a/lua/oil/config.lua +++ b/lua/oil/config.lua @@ -1,6 +1,6 @@ local default_config = { -- Oil will take over directory buffers (e.g. `vim .` or `:e src/`) - -- Set to false if you still want to use netrw.conf + -- Set to false if you still want to use netrw default_file_explorer = true, -- Id is automatically added at the beginning, and name at the end -- See :help oil-columns diff --git a/lua/oil/init.lua b/lua/oil/init.lua index d683e539..7baa4d8a 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -256,32 +256,7 @@ M.open_float = function(dir) local bufnr = vim.api.nvim_create_buf(false, true) vim.bo[bufnr].bufhidden = "wipe" - local total_width = vim.o.columns - local total_height = layout.get_editor_height() - local width = total_width - 2 * config.float.padding - if config.float.border ~= "none" then - width = width - 2 -- The border consumes 1 col on each side - end - if config.float.max_width > 0 then - width = math.min(width, config.float.max_width) - end - local height = total_height - 2 * config.float.padding - if config.float.max_height > 0 then - height = math.min(height, config.float.max_height) - end - local row = math.floor((total_height - height) / 2) - local col = math.floor((total_width - width) / 2) - 1 -- adjust for border width - - local win_opts = { - relative = "editor", - width = width, - height = height, - row = row, - col = col, - border = config.float.border, - zindex = 45, - } - win_opts = config.float.override(win_opts) or win_opts + local win_opts = layout.get_fullscreen_win_opts() local original_winid = vim.api.nvim_get_current_win() local winid = vim.api.nvim_open_win(bufnr, true, win_opts) @@ -448,6 +423,8 @@ end --- split "aboveleft"|"belowright"|"topleft"|"botright" Split modifier M.open_preview = function(opts, callback) opts = opts or {} + local config = require("oil.config") + local layout = require("oil.layout") local util = require("oil.util") local function finish(err) @@ -476,91 +453,25 @@ M.open_preview = function(opts, callback) if util.is_floating_win() then if preview_win == nil then - local config = require("oil.config") - -- local default_dims = util.get_floating_win_default_dimensions() - local float_config = vim.api.nvim_win_get_config(0) - - local dimesions_preview = { - width = float_config.width, - height = float_config.height, - col = float_config.col, - row = float_config.row, - } - - local dimesions_oil_window = { - width = float_config.width, - height = float_config.height, - col = float_config.col, - row = float_config.row, - } - - if vim.fn.has("nvim-0.10") == 0 then - -- read https://github.com/neovim/neovim/issues/24430 for more infos. - dimesions_preview.col = float_config.col[vim.val_idx] - dimesions_preview.row = float_config.row[vim.val_idx] - dimesions_oil_window.col = float_config.col[vim.val_idx] - dimesions_oil_window.row = float_config.row[vim.val_idx] - end - if - config.float.preview_split == "left" - or config.float.preview_split == "right" - or config.float.preview_split == "auto" - then - dimesions_preview.width = math.floor(float_config.width / 2) - - (config.float.preview_gap / 2) - dimesions_oil_window.width = dimesions_preview.width - end - - if config.float.preview_split == "above" or config.float.preview_split == "below" then - dimesions_preview.height = math.floor(float_config.height / 2) - - (config.float.preview_gap / 2) - dimesions_oil_window.height = dimesions_preview.height - end - - if - config.float.preview_split == "left" - or (config.float.preview_split == "auto" and not vim.o.splitright) - then - dimesions_oil_window.col = dimesions_oil_window.col - + dimesions_oil_window.width - + config.float.preview_gap - end - if - config.float.preview_split == "right" - or (config.float.preview_split == "auto" and vim.o.splitright) - then - dimesions_preview.col = dimesions_preview.col - + dimesions_preview.width - + config.float.preview_gap - end - - if config.float.preview_split == "above" then - dimesions_oil_window.row = dimesions_oil_window.row - + dimesions_oil_window.height - + config.float.preview_gap - end - if config.float.preview_split == "below" then - dimesions_preview.row = dimesions_preview.row - + dimesions_preview.height - + config.float.preview_gap - end + local root_win_opts, preview_win_opts = + layout.split_window(0, config.float.preview_split, config.float.preview_gap) local win_opts_oil = { relative = "editor", - width = dimesions_oil_window.width, - height = dimesions_oil_window.height, - row = dimesions_oil_window.row, - col = dimesions_oil_window.col, + width = root_win_opts.width, + height = root_win_opts.height, + row = root_win_opts.row, + col = root_win_opts.col, border = config.float.border, zindex = 45, } vim.api.nvim_win_set_config(0, win_opts_oil) local win_opts = { relative = "editor", - width = dimesions_preview.width, - height = dimesions_preview.height, - row = dimesions_preview.row, - col = dimesions_preview.col, + width = preview_win_opts.width, + height = preview_win_opts.height, + row = preview_win_opts.row, + col = preview_win_opts.col, border = config.float.border, zindex = 45, focusable = false, @@ -587,7 +498,6 @@ M.open_preview = function(opts, callback) split = opts.split, } - local is_visual_mode = util.is_visual_mode() -- HACK Switching windows takes us out of visual mode. -- Switching with nvim_set_current_win causes the previous visual selection (as used by `gv`) to -- not get set properly. So we have to switch windows this way instead. @@ -596,15 +506,16 @@ M.open_preview = function(opts, callback) vim.cmd.wincmd({ args = { "w" }, count = winnr }) end - if preview_win then - if is_visual_mode then - hack_set_win(preview_win) - else - vim.api.nvim_set_current_win(preview_win) + util.get_edit_path(bufnr, entry, function(normalized_url) + local is_visual_mode = util.is_visual_mode() + if preview_win then + if is_visual_mode then + hack_set_win(preview_win) + else + vim.api.nvim_set_current_win(preview_win) + end end - end - util.get_edit_path(bufnr, entry, function(normalized_url) local filebufnr = vim.fn.bufadd(normalized_url) local entry_is_file = not vim.endswith(normalized_url, "/") diff --git a/lua/oil/layout.lua b/lua/oil/layout.lua index b4e3fed3..1ed257ab 100644 --- a/lua/oil/layout.lua +++ b/lua/oil/layout.lua @@ -93,6 +93,89 @@ M.calculate_height = function(desired_height, opts) ) end +---@class (exact) conform.WinLayout +---@field width integer +---@field height integer +---@field row integer +---@field col integer + +---@return vim.api.keyset.win_config +M.get_fullscreen_win_opts = function() + local config = require("oil.config") + + local total_width = M.get_editor_width() + local total_height = M.get_editor_height() + local width = total_width - 2 * config.float.padding + if config.float.border ~= "none" then + width = width - 2 -- The border consumes 1 col on each side + end + if config.float.max_width > 0 then + width = math.min(width, config.float.max_width) + end + local height = total_height - 2 * config.float.padding + if config.float.max_height > 0 then + height = math.min(height, config.float.max_height) + end + local row = math.floor((total_height - height) / 2) + local col = math.floor((total_width - width) / 2) - 1 -- adjust for border width + + local win_opts = { + relative = "editor", + width = width, + height = height, + row = row, + col = col, + border = config.float.border, + zindex = 45, + } + return config.float.override(win_opts) or win_opts +end + +---@param winid integer +---@param direction "above"|"below"|"left"|"right"|"auto" +---@param gap integer +---@return conform.WinLayout root_dim New dimensions of the original window +---@return conform.WinLayout new_dim New dimensions of the new window +M.split_window = function(winid, direction, gap) + if direction == "auto" then + direction = vim.o.splitright and "right" or "left" + end + + local float_config = vim.api.nvim_win_get_config(winid) + local dim_root = { + width = float_config.width, + height = float_config.height, + col = float_config.col, + row = float_config.row, + } + if vim.fn.has("nvim-0.10") == 0 then + -- read https://github.com/neovim/neovim/issues/24430 for more infos. + dim_root.col = float_config.col[vim.val_idx] + dim_root.row = float_config.row[vim.val_idx] + end + local dim_new = vim.deepcopy(dim_root) + + if direction == "left" or direction == "right" then + dim_new.width = math.floor(float_config.width / 2) - math.ceil(gap / 2) + dim_root.width = dim_new.width + else + dim_new.height = math.floor(float_config.height / 2) - math.ceil(gap / 2) + dim_root.height = dim_new.height + end + + if direction == "left" then + dim_root.col = dim_root.col + dim_root.width + gap + elseif direction == "right" then + dim_new.col = dim_new.col + dim_new.width + gap + elseif direction == "above" then + dim_root.row = dim_root.row + dim_root.height + gap + elseif direction == "below" then + dim_new.row = dim_new.row + dim_new.height + gap + end + + return dim_root, dim_new +end + M.calculate_dims = function(desired_width, desired_height, opts) local width = M.calculate_width(desired_width, opts) local height = M.calculate_height(desired_height, opts) From 9397a3f037d908f39f6a4b584be1100243d5af42 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Mon, 17 Jun 2024 22:50:05 -0400 Subject: [PATCH 17/20] fix: floating preview window title is file name --- lua/oil/init.lua | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 7baa4d8a..8e54fb6b 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -451,6 +451,15 @@ M.open_preview = function(opts, callback) local prev_win = vim.api.nvim_get_current_win() local bufnr = vim.api.nvim_get_current_buf() + local entry = M.get_cursor_entry() + if not entry then + return finish("Could not find entry under cursor") + end + local entry_title = entry.name + if entry.type == "directory" then + entry_title = entry_title .. "/" + end + if util.is_floating_win() then if preview_win == nil then local root_win_opts, preview_win_opts = @@ -476,21 +485,21 @@ M.open_preview = function(opts, callback) zindex = 45, focusable = false, noautocmd = true, - title = "Preview", style = "minimal", } + if vim.fn.has("nvim-0.9") == 1 then + win_opts.title = entry_title + end + preview_win = vim.api.nvim_open_win(bufnr, true, win_opts) vim.api.nvim_set_option_value("previewwindow", true, { scope = "local", win = preview_win }) vim.api.nvim_set_current_win(prev_win) + elseif vim.fn.has("nvim-0.9") == 1 then + vim.api.nvim_win_set_config(preview_win, { title = entry_title }) end end - local entry = M.get_cursor_entry() - if not entry then - return finish("Could not find entry under cursor") - end - local cmd = preview_win and "buffer" or "sbuffer" local mods = { vertical = opts.vertical, From 54a6cb8ae6b152d456aa8bdcd13f94448f07e2bf Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Mon, 17 Jun 2024 22:52:17 -0400 Subject: [PATCH 18/20] doc: clarify default_file_explorer --- README.md | 2 +- doc/oil.txt | 2 +- lua/oil/config.lua | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9bbfa445..59cad480 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ You can open a directory with `:edit ` or `:Oil `. To open oil in a ```lua require("oil").setup({ -- Oil will take over directory buffers (e.g. `vim .` or `:e src/`) - -- Set to false if you still want to use netrw + -- Set to false if you want some other plugin (e.g. netrw) to open when you edit directories. default_file_explorer = true, -- Id is automatically added at the beginning, and name at the end -- See :help oil-columns diff --git a/doc/oil.txt b/doc/oil.txt index b2b0502b..233cdc9f 100644 --- a/doc/oil.txt +++ b/doc/oil.txt @@ -17,7 +17,7 @@ CONFIG *oil-confi >lua require("oil").setup({ -- Oil will take over directory buffers (e.g. `vim .` or `:e src/`) - -- Set to false if you still want to use netrw + -- Set to false if you want some other plugin (e.g. netrw) to open when you edit directories. default_file_explorer = true, -- Id is automatically added at the beginning, and name at the end -- See :help oil-columns diff --git a/lua/oil/config.lua b/lua/oil/config.lua index df4d7afb..43734fbf 100644 --- a/lua/oil/config.lua +++ b/lua/oil/config.lua @@ -1,6 +1,6 @@ local default_config = { -- Oil will take over directory buffers (e.g. `vim .` or `:e src/`) - -- Set to false if you still want to use netrw + -- Set to false if you want some other plugin (e.g. netrw) to open when you edit directories. default_file_explorer = true, -- Id is automatically added at the beginning, and name at the end -- See :help oil-columns From 8fb06b1307c99da0694f82f66e43f9db2232b2f2 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Mon, 17 Jun 2024 22:53:42 -0400 Subject: [PATCH 19/20] refactor: don't need a preview_gap option --- README.md | 2 -- doc/oil.txt | 2 -- lua/oil/config.lua | 2 -- lua/oil/init.lua | 2 +- 4 files changed, 1 insertion(+), 7 deletions(-) diff --git a/README.md b/README.md index 59cad480..a11f1277 100644 --- a/README.md +++ b/README.md @@ -247,8 +247,6 @@ require("oil").setup({ win_options = { winblend = 0, }, - -- gap between oil window and preview window - preview_gap = 2, -- preview_split: Split direction: "auto", "left", "right", "above", "below". preview_split = "auto", -- This is the config that will be passed to nvim_open_win. diff --git a/doc/oil.txt b/doc/oil.txt index 233cdc9f..379702df 100644 --- a/doc/oil.txt +++ b/doc/oil.txt @@ -138,8 +138,6 @@ CONFIG *oil-confi win_options = { winblend = 0, }, - -- gap between oil window and preview window - preview_gap = 2, -- preview_split: Split direction: "auto", "left", "right", "above", "below". preview_split = "auto", -- This is the config that will be passed to nvim_open_win. diff --git a/lua/oil/config.lua b/lua/oil/config.lua index 43734fbf..bc63007a 100644 --- a/lua/oil/config.lua +++ b/lua/oil/config.lua @@ -121,8 +121,6 @@ local default_config = { win_options = { winblend = 0, }, - -- gap between oil window and preview window - preview_gap = 2, -- preview_split: Split direction: "auto", "left", "right", "above", "below". preview_split = "auto", -- This is the config that will be passed to nvim_open_win. diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 8e54fb6b..0f38b71d 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -463,7 +463,7 @@ M.open_preview = function(opts, callback) if util.is_floating_win() then if preview_win == nil then local root_win_opts, preview_win_opts = - layout.split_window(0, config.float.preview_split, config.float.preview_gap) + layout.split_window(0, config.float.preview_split, config.float.padding) local win_opts_oil = { relative = "editor", From dccec5e8117d984bc6bd1a1965f236ac697aebc4 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Mon, 17 Jun 2024 23:01:09 -0400 Subject: [PATCH 20/20] refactor: only find preview win in current tabpage --- lua/oil/util.lua | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lua/oil/util.lua b/lua/oil/util.lua index e1938de5..a685b145 100644 --- a/lua/oil/util.lua +++ b/lua/oil/util.lua @@ -658,13 +658,6 @@ end ---@return nil|integer M.get_preview_win = function() - if M.is_floating_win() then - for _, winid in ipairs(vim.api.nvim_list_wins()) do - if vim.api.nvim_win_is_valid(winid) and vim.wo[winid].previewwindow then - return winid - end - end - end for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do if vim.api.nvim_win_is_valid(winid) and vim.wo[winid].previewwindow then return winid