Skip to content

Commit

Permalink
fix: previewing and editing files on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Nov 4, 2023
1 parent 2e6996b commit 1c884f1
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -527,12 +527,12 @@ M.select = function(opts, callback)
local child = dir .. entry.name
local url = scheme .. child
local is_directory = entry.type == "directory"
or (
entry.type == "link"
and entry.meta
and entry.meta.link_stat
and entry.meta.link_stat.type == "directory"
)
or (
entry.type == "link"
and entry.meta
and entry.meta.link_stat
and entry.meta.link_stat.type == "directory"
)
if is_directory then
url = url .. "/"
-- If this is a new directory BUT we think we already have an entry with this name, disallow
Expand Down Expand Up @@ -568,39 +568,37 @@ M.select = function(opts, callback)
keepalt = true,
emsg_silent = true,
}
-- If we're editing a file on disk, shorten the path prior to :edit so the
-- display name will show up shortened
if adapter.name == "files" and not util.parse_url(normalized_url) then
normalized_url = require("oil.fs").shorten_path(normalized_url)
end
local filename = util.escape_filename(normalized_url)
local filebufnr = vim.fn.bufadd(normalized_url)

-- If we're previewing a file that hasn't been opened yet, make sure it gets deleted after we
-- close the window
if opts.preview and not util.parse_url(filename) then
local bufnr = vim.fn.bufadd(filename)
if vim.fn.bufloaded(bufnr) == 0 then
vim.bo[bufnr].bufhidden = "wipe"
vim.b[bufnr].oil_preview_buffer = true
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
vim.bo[filebufnr].bufhidden = "wipe"
vim.b[filebufnr].oil_preview_buffer = true
end
else
-- The :buffer command doesn't set buflisted=true
vim.bo[filebufnr].buflisted = true
end

local cmd
if opts.preview and preview_win then
vim.api.nvim_set_current_win(preview_win)
cmd = "edit"
cmd = "buffer"
else
if opts.tab then
cmd = "tabedit"
vim.cmd.tabnew({ mods = mods })
cmd = "buffer"
elseif opts.split then
cmd = "split"
cmd = "sbuffer"
else
cmd = "edit"
cmd = "buffer"
end
end
vim.cmd({
cmd = cmd,
args = { filename },
args = { filebufnr },
mods = mods,
})
if opts.preview then
Expand All @@ -617,9 +615,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()
Expand Down Expand Up @@ -752,7 +750,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
Expand Down

0 comments on commit 1c884f1

Please sign in to comment.