From 1488f0d96b1cb820dd12f05a7bf5283a631a7c4d Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Wed, 22 Jan 2025 16:53:10 -0800 Subject: [PATCH] fix: preview sometimes causes oil buffers to be stuck in unloaded state (#563) --- lua/oil/init.lua | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lua/oil/init.lua b/lua/oil/init.lua index f5eff2f..812b414 100644 --- a/lua/oil/init.lua +++ b/lua/oil/init.lua @@ -30,6 +30,8 @@ local M = {} ---@field filter_action? fun(action: oil.Action): boolean When present, filter out actions as they are created ---@field filter_error? fun(action: oil.ParseError): boolean When present, filter out errors from parsing a buffer +local load_oil_buffer + ---Get the entry on a specific line (1-indexed) ---@param bufnr integer ---@param lnum integer @@ -568,6 +570,12 @@ M.open_preview = function(opts, callback) vim.api.nvim_echo({ { err, "Error" } }, true, {}) end + -- If we called open_preview during an autocmd, then the edit command may not trigger the + -- BufReadCmd to load the buffer. So we need to do it manually. + if util.is_oil_bufnr(filebufnr) then + load_oil_buffer(filebufnr) + end + vim.api.nvim_set_option_value("previewwindow", true, { scope = "local", win = 0 }) vim.api.nvim_win_set_var(0, "oil_preview", true) for k, v in pairs(config.preview_win.win_options) do @@ -994,7 +1002,7 @@ local function restore_alt_buf() end ---@param bufnr integer -local function load_oil_buffer(bufnr) +load_oil_buffer = function(bufnr) local config = require("oil.config") local keymap_util = require("oil.keymap_util") local loading = require("oil.loading") @@ -1008,6 +1016,11 @@ local function load_oil_buffer(bufnr) util.rename_buffer(bufnr, bufname) end + -- Early return if we're already loading or have already loaded this buffer + if loading.is_loading(bufnr) or vim.b[bufnr].filetype ~= nil then + return + end + local adapter = assert(config.get_adapter_by_scheme(scheme)) if vim.endswith(bufname, "/") then