fix: directory hijacking when oil is lazy loaded (#149)

This commit is contained in:
Steven Arcangeli 2023-08-09 00:36:33 -07:00
parent 887bb4a8b6
commit 966eaaadbc
2 changed files with 16 additions and 5 deletions

View file

@ -601,23 +601,25 @@ M.select = function(opts, callback)
end
---@param bufnr integer
---@return boolean
local function maybe_hijack_directory_buffer(bufnr)
local config = require("oil.config")
local util = require("oil.util")
if not config.default_file_explorer then
return
return false
end
local bufname = vim.api.nvim_buf_get_name(bufnr)
if bufname == "" then
return
return false
end
if util.parse_url(bufname) or vim.fn.isdirectory(bufname) == 0 then
return
return false
end
util.rename_buffer(
local replaced = util.rename_buffer(
bufnr,
util.addslash(config.adapter_to_scheme.files .. vim.fn.fnamemodify(bufname, ":p"))
)
return not replaced
end
---@private
@ -1045,7 +1047,12 @@ M.setup = function(opts)
end,
})
maybe_hijack_directory_buffer(0)
local bufnr = vim.api.nvim_get_current_buf()
if maybe_hijack_directory_buffer(bufnr) and vim.v.vim_did_enter == 1 then
-- manually call load on a hijacked directory buffer if vim has already entered
-- (the BufReadCmd will not trigger)
load_oil_buffer(bufnr)
end
end
return M

View file

@ -5,6 +5,8 @@ local timers = {}
local FPS = 20
---@param bufnr integer
---@return boolean
M.is_loading = function(bufnr)
return timers[bufnr] ~= nil
end
@ -56,6 +58,8 @@ M.get_bar_iter = function(opts)
end
end
---@param bufnr integer
---@param is_loading boolean
M.set_loading = function(bufnr, is_loading)
if is_loading then
if timers[bufnr] == nil then