From 381b5a440dd988128857f5c966d2679c544ed0af Mon Sep 17 00:00:00 2001 From: Lucas Eras Date: Mon, 8 Jan 2024 17:30:13 -0600 Subject: [PATCH 1/7] feat: do not close preview when cd into dir --- lua/oil/init.lua | 12 ++++++++++++ lua/oil/view.lua | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 06157109..47c30110 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -529,6 +529,7 @@ M.select = function(opts, callback) vim.api.nvim_win_close(preview_win, true) end local prev_win = vim.api.nvim_get_current_win() + local keep_preview_alive local scheme, dir = util.parse_url(bufname) assert(scheme and dir) @@ -602,6 +603,14 @@ M.select = function(opts, callback) vim.bo[filebufnr].buflisted = true end + -- If a preview window is open, and we are selecting a directory, + -- persist the preview window + if not opts.preview and preview_win then + if vim.endswith(normalized_url, "/") then + keep_preview_alive = true + end + end + local cmd if opts.preview and preview_win then vim.api.nvim_set_current_win(preview_win) @@ -643,6 +652,9 @@ M.select = function(opts, callback) M.close() end) end + if keep_preview_alive then + M.select({preview = true}) + end finish() end) end diff --git a/lua/oil/view.lua b/lua/oil/view.lua index 339c03af..162c7826 100644 --- a/lua/oil/view.lua +++ b/lua/oil/view.lua @@ -426,6 +426,31 @@ M.initialize = function(bufnr) end, }) + vim.api.nvim_create_autocmd("BufWinEnter", { + desc = "Update preview window when displaying a buffer", + group = "Oil", + buffer = bufnr, + callback = function() + local oil = require("oil") + if vim.wo.previewwindow then + return + end + + if vim.api.nvim_get_current_buf() ~= bufnr then + return + end + local entry = oil.get_cursor_entry() + if entry then + local preview_win_id = util.get_preview_win() + if preview_win_id then + if entry.id ~= vim.w[preview_win_id].oil_entry_id then + oil.select({ preview = true }) + end + end + end + end, + }) + -- Watch for TextChanged and update the trash original path extmarks local adapter = util.get_adapter(bufnr) if adapter and adapter.name == "trash" then From 5220fa5e3a90c137e81e960295e6f604315b2816 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Tue, 9 Jan 2024 18:03:55 -0800 Subject: [PATCH 2/7] refactor: add helper method to run function after oil buffer loads --- lua/oil/util.lua | 22 ++++++++++++++++++++++ lua/oil/view.lua | 1 + 2 files changed, 23 insertions(+) diff --git a/lua/oil/util.lua b/lua/oil/util.lua index be6afdeb..b19c003f 100644 --- a/lua/oil/util.lua +++ b/lua/oil/util.lua @@ -774,4 +774,26 @@ M.get_visual_range = function() return { start_lnum = start_lnum, end_lnum = end_lnum } end +---@param bufnr integer +---@param callback fun() +M.run_after_load = function(bufnr, callback) + if bufnr == 0 then + bufnr = vim.api.nvim_get_current_buf() + end + if vim.b[bufnr].oil_ready then + callback() + else + local autocmd_id + autocmd_id = vim.api.nvim_create_autocmd("User", { + pattern = "OilEnter", + callback = function(args) + if args.data.buf == bufnr then + callback() + vim.api.nvim_del_autocmd(autocmd_id) + end + end, + }) + end +end + return M diff --git a/lua/oil/view.lua b/lua/oil/view.lua index 162c7826..30180ee7 100644 --- a/lua/oil/view.lua +++ b/lua/oil/view.lua @@ -710,6 +710,7 @@ end --- refetch nil|boolean Defaults to true ---@param callback nil|fun(err: nil|string) M.render_buffer_async = function(bufnr, opts, callback) + vim.b[bufnr].oil_ready = false opts = vim.tbl_deep_extend("keep", opts or {}, { refetch = true, }) From 6d9af2df45441be1bdb9dc5a40a5c57404bc7409 Mon Sep 17 00:00:00 2001 From: Lucas Eras Date: Wed, 17 Jan 2024 21:17:03 -0600 Subject: [PATCH 3/7] Keep preview window open --- lua/oil/init.lua | 47 +++++++++++++++++++++++++++++------------------ lua/oil/view.lua | 25 ------------------------- 2 files changed, 29 insertions(+), 43 deletions(-) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 47c30110..1ccf31b7 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -384,6 +384,12 @@ M.open = function(dir) if config.buf_options.buflisted ~= nil then vim.api.nvim_buf_set_option(0, "buflisted", config.buf_options.buflisted) end + + -- If preview window exists, update its content + local preview_win_id = util.get_preview_win() + if preview_win_id then + M.select({ preview = true }) + end end ---Restore the buffer that was present when oil was opened @@ -523,13 +529,8 @@ M.select = function(opts, callback) end end - -- Close the preview window if we're not previewing the selection local preview_win = util.get_preview_win() - if not opts.preview and preview_win then - vim.api.nvim_win_close(preview_win, true) - end local prev_win = vim.api.nvim_get_current_win() - local keep_preview_alive local scheme, dir = util.parse_url(bufname) assert(scheme and dir) @@ -589,28 +590,21 @@ M.select = function(opts, callback) emsg_silent = true, } local filebufnr = vim.fn.bufadd(normalized_url) + local entry_is_file = not vim.endswith(normalized_url, "/") if opts.preview then -- If we're previewing a file that hasn't been opened yet, make sure it gets deleted after -- we close the window - if not vim.endswith(normalized_url, "/") and vim.fn.bufloaded(filebufnr) == 0 then + if entry_is_file and vim.fn.bufloaded(filebufnr) == 0 then vim.bo[filebufnr].bufhidden = "wipe" vim.b[filebufnr].oil_preview_buffer = true end - elseif not vim.endswith(normalized_url, "/") then + elseif entry_is_file then -- The :buffer command doesn't set buflisted=true -- So do that for non-diretory-buffers vim.bo[filebufnr].buflisted = true end - -- If a preview window is open, and we are selecting a directory, - -- persist the preview window - if not opts.preview and preview_win then - if vim.endswith(normalized_url, "/") then - keep_preview_alive = true - end - end - local cmd if opts.preview and preview_win then vim.api.nvim_set_current_win(preview_win) @@ -630,6 +624,12 @@ M.select = function(opts, callback) args = { filebufnr }, mods = mods, }) + + if not opts.preview and preview_win and entry_is_file then + vim.api.nvim_win_close(preview_win, true) + vim.api.nvim_set_current_win(prev_win) + end + if opts.preview then vim.api.nvim_set_option_value("previewwindow", true, { scope = "local", win = 0 }) vim.w.oil_entry_id = entry.id @@ -652,9 +652,20 @@ M.select = function(opts, callback) M.close() end) end - if keep_preview_alive then - M.select({preview = true}) - end + + util.run_after_load(0, function() + local oil = require("oil") + local cursor_entry = oil.get_cursor_entry() + if cursor_entry then + local preview_win_id = util.get_preview_win() + if preview_win_id then + if cursor_entry.id ~= vim.w[preview_win_id].oil_entry_id then + M.select({ preview = true }) + end + end + end + end) + finish() end) end diff --git a/lua/oil/view.lua b/lua/oil/view.lua index 30180ee7..dafa3dc9 100644 --- a/lua/oil/view.lua +++ b/lua/oil/view.lua @@ -426,31 +426,6 @@ M.initialize = function(bufnr) end, }) - vim.api.nvim_create_autocmd("BufWinEnter", { - desc = "Update preview window when displaying a buffer", - group = "Oil", - buffer = bufnr, - callback = function() - local oil = require("oil") - if vim.wo.previewwindow then - return - end - - if vim.api.nvim_get_current_buf() ~= bufnr then - return - end - local entry = oil.get_cursor_entry() - if entry then - local preview_win_id = util.get_preview_win() - if preview_win_id then - if entry.id ~= vim.w[preview_win_id].oil_entry_id then - oil.select({ preview = true }) - end - end - end - end, - }) - -- Watch for TextChanged and update the trash original path extmarks local adapter = util.get_adapter(bufnr) if adapter and adapter.name == "trash" then From 70a6379f81ccf0398b3b19ed123edd0f6af5ced9 Mon Sep 17 00:00:00 2001 From: Lucas Eras Date: Wed, 17 Jan 2024 21:41:54 -0600 Subject: [PATCH 4/7] Remove some test logic --- lua/oil/view.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/oil/view.lua b/lua/oil/view.lua index dafa3dc9..339c03af 100644 --- a/lua/oil/view.lua +++ b/lua/oil/view.lua @@ -685,7 +685,6 @@ end --- refetch nil|boolean Defaults to true ---@param callback nil|fun(err: nil|string) M.render_buffer_async = function(bufnr, opts, callback) - vim.b[bufnr].oil_ready = false opts = vim.tbl_deep_extend("keep", opts or {}, { refetch = true, }) From fc99dec013af375e81643848ce8e6e42c1246e5d Mon Sep 17 00:00:00 2001 From: Lucas Eras Date: Sun, 21 Jan 2024 17:22:38 -0600 Subject: [PATCH 5/7] Use `run_after_load` when moving to parent directory --- lua/oil/init.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 1ccf31b7..014a2479 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -386,10 +386,12 @@ M.open = function(dir) end -- If preview window exists, update its content - local preview_win_id = util.get_preview_win() - if preview_win_id then - M.select({ preview = true }) - end + util.run_after_load(0, function() + local preview_win_id = util.get_preview_win() + if preview_win_id then + M.select({ preview = true }) + end + end) end ---Restore the buffer that was present when oil was opened From 80ee62751d50a660f46b0dc29e9844e967e82b0f Mon Sep 17 00:00:00 2001 From: Lucas Eras Date: Sun, 21 Jan 2024 17:23:24 -0600 Subject: [PATCH 6/7] Remove unnecessary update of current window --- lua/oil/init.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index 014a2479..f4bac840 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -629,7 +629,6 @@ M.select = function(opts, callback) if not opts.preview and preview_win and entry_is_file then vim.api.nvim_win_close(preview_win, true) - vim.api.nvim_set_current_win(prev_win) end if opts.preview then From a6e226561f65590683a7fdb031b3ee324c263d59 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Sun, 21 Jan 2024 20:30:45 -0800 Subject: [PATCH 7/7] refactor: create helper function for updating preview --- lua/oil/init.lua | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index f4bac840..871c4f60 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -366,6 +366,23 @@ M.toggle_float = function(dir) end end +---@param oil_bufnr? integer +local function update_preview_window(oil_bufnr) + oil_bufnr = oil_bufnr or 0 + local util = require("oil.util") + util.run_after_load(oil_bufnr, function() + local cursor_entry = M.get_cursor_entry() + if cursor_entry then + local preview_win_id = util.get_preview_win() + if preview_win_id then + if cursor_entry.id ~= vim.w[preview_win_id].oil_entry_id then + M.select({ preview = true }) + end + end + end + end) +end + ---Open oil browser for a directory ---@param dir nil|string When nil, open the parent of the current buffer, or the cwd if current buffer is not a file M.open = function(dir) @@ -386,12 +403,7 @@ M.open = function(dir) end -- If preview window exists, update its content - util.run_after_load(0, function() - local preview_win_id = util.get_preview_win() - if preview_win_id then - M.select({ preview = true }) - end - end) + update_preview_window() end ---Restore the buffer that was present when oil was opened @@ -654,18 +666,7 @@ M.select = function(opts, callback) end) end - util.run_after_load(0, function() - local oil = require("oil") - local cursor_entry = oil.get_cursor_entry() - if cursor_entry then - local preview_win_id = util.get_preview_win() - if preview_win_id then - if cursor_entry.id ~= vim.w[preview_win_id].oil_entry_id then - M.select({ preview = true }) - end - end - end - end) + update_preview_window() finish() end)