Merge pull request #78 from stevearc/stevearc-detect-normal-file

feat: convert oil://path/to/file.lua to normal file path
This commit is contained in:
Steven Arcangeli 2023-03-18 00:15:53 -07:00 committed by GitHub
commit 90622106cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 6 deletions

View file

@ -180,7 +180,17 @@ M.normalize_url = function(url, callback)
vim.loop.fs_stat(
realpath,
vim.schedule_wrap(function(stat_err, stat)
if not stat or stat.type == "directory" then
local is_directory
if stat then
is_directory = stat.type == "directory"
elseif vim.endswith(realpath, "/") then
is_directory = true
else
local filetype = vim.filetype.match({ filename = vim.fs.basename(realpath) })
is_directory = filetype == nil
end
if is_directory then
local norm_path = util.addslash(fs.os_to_posix_path(realpath))
callback(scheme .. norm_path)
else

View file

@ -394,15 +394,15 @@ M.select = function(opts)
local scheme, dir = util.parse_url(bufname)
local child = dir .. entry.name
local url = scheme .. child
if
entry.type == "directory"
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"
)
then
if is_directory then
url = url .. "/"
-- If this is a new directory BUT we think we already have an entry with this name, disallow
-- entry. This prevents the case of MOVE /foo -> /bar + CREATE /foo.
-- If you enter the new /foo, it will show the contents of the old /foo.
@ -614,6 +614,17 @@ local function load_oil_buffer(bufnr)
-- have BufReadCmd called for it
return
end
-- If the renamed buffer doesn't have a scheme anymore, this is a normal file.
-- Finish setting it up as a normal buffer.
local new_scheme = util.parse_url(new_url)
if not new_scheme then
loading.set_loading(bufnr, false)
vim.cmd.doautocmd({ args = { "BufReadPre", new_url }, mods = { emsg_silent = true } })
vim.cmd.doautocmd({ args = { "BufReadPost", new_url }, mods = { emsg_silent = true } })
return
end
bufname = new_url
end
if vim.endswith(bufname, "/") then

View file

@ -85,8 +85,10 @@ a.describe("Alternate buffer", function()
vim.cmd.edit({ args = { "foo" } })
oil.open()
test_util.wait_for_autocmd("BufReadPost")
a.util.sleep(10)
assert.equals("foo", vim.fn.expand("#"))
vim.wait(1000, function()
return oil.get_cursor_entry()
end, 10)
vim.api.nvim_win_set_cursor(0, { 1, 1 })
oil.select()
test_util.wait_for_autocmd("BufReadPost")
@ -97,8 +99,10 @@ a.describe("Alternate buffer", function()
vim.cmd.edit({ args = { "foo" } })
oil.open()
test_util.wait_for_autocmd("BufReadPost")
a.util.sleep(10)
assert.equals("foo", vim.fn.expand("#"))
vim.wait(1000, function()
return oil.get_cursor_entry()
end, 10)
vim.api.nvim_win_set_cursor(0, { 1, 1 })
oil.select({ preview = true })
test_util.wait_for_autocmd("BufReadPost")

View file

@ -2,6 +2,7 @@ require("plenary.async").tests.add_to_env()
local fs = require("oil.fs")
local files = require("oil.adapters.files")
local cache = require("oil.cache")
local test_util = require("tests.test_util")
local function throwiferr(err, ...)
if err then
@ -308,4 +309,27 @@ a.describe("files adapter", function()
["a/"] = true,
})
end)
a.it("Editing a new oil://path/ creates an oil buffer", function()
local tmpdir_url = "oil://" .. vim.fn.fnamemodify(tmpdir.path, ":p") .. "/"
vim.cmd.edit({ args = { tmpdir_url } })
test_util.wait_for_autocmd("BufReadPost")
local new_url = "oil://" .. vim.fn.fnamemodify(tmpdir.path, ":p") .. "newdir"
vim.cmd.edit({ args = { new_url } })
test_util.wait_for_autocmd("BufReadPost")
assert.equals("oil", vim.bo.filetype)
-- The normalization will add a '/'
assert.equals(new_url .. "/", vim.api.nvim_buf_get_name(0))
end)
a.it("Editing a new oil://file.rb creates a normal buffer", function()
local tmpdir_url = "oil://" .. vim.fn.fnamemodify(tmpdir.path, ":p") .. "/"
vim.cmd.edit({ args = { tmpdir_url } })
test_util.wait_for_autocmd("BufReadPost")
local new_url = "oil://" .. vim.fn.fnamemodify(tmpdir.path, ":p") .. "file.rb"
vim.cmd.edit({ args = { new_url } })
test_util.wait_for_autocmd("BufReadPost")
assert.equals("ruby", vim.bo.filetype)
assert.equals(vim.fn.fnamemodify(tmpdir.path, ":p") .. "file.rb", vim.api.nvim_buf_get_name(0))
end)
end)