refactor: separate adapter-to-scheme lookup table
This commit is contained in:
parent
2e6d68453f
commit
1879339442
3 changed files with 11 additions and 11 deletions
|
|
@ -81,7 +81,7 @@ local _connections = {}
|
||||||
---@param allow_retry nil|boolean
|
---@param allow_retry nil|boolean
|
||||||
local function get_connection(url, allow_retry)
|
local function get_connection(url, allow_retry)
|
||||||
local res = parse_url(url)
|
local res = parse_url(url)
|
||||||
res.scheme = config.adapters.ssh
|
res.scheme = config.adapter_to_scheme.ssh
|
||||||
res.path = ""
|
res.path = ""
|
||||||
local key = url_to_str(res)
|
local key = url_to_str(res)
|
||||||
local conn = _connections[key]
|
local conn = _connections[key]
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,10 @@ M.setup = function(opts)
|
||||||
M[k] = v
|
M[k] = v
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.tbl_add_reverse_lookup(M.adapters)
|
M.adapter_to_scheme = {}
|
||||||
|
for k, v in pairs(M.adapters) do
|
||||||
|
M.adapter_to_scheme[v] = k
|
||||||
|
end
|
||||||
M._adapter_by_scheme = {}
|
M._adapter_by_scheme = {}
|
||||||
if type(M.trash) == "string" then
|
if type(M.trash) == "string" then
|
||||||
M.trash = vim.fn.fnamemodify(vim.fn.expand(M.trash), ":p")
|
M.trash = vim.fn.fnamemodify(vim.fn.expand(M.trash), ":p")
|
||||||
|
|
@ -116,7 +119,7 @@ M.get_trash_url = function()
|
||||||
fs.mkdirp(oil_trash_dir)
|
fs.mkdirp(oil_trash_dir)
|
||||||
M.trash = oil_trash_dir
|
M.trash = oil_trash_dir
|
||||||
end
|
end
|
||||||
return M.adapters.files .. fs.os_to_posix_path(M.trash)
|
return M.adapter_to_scheme.files .. fs.os_to_posix_path(M.trash)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param scheme string
|
---@param scheme string
|
||||||
|
|
|
||||||
|
|
@ -156,7 +156,7 @@ M.get_url_for_path = function(dir)
|
||||||
if dir then
|
if dir then
|
||||||
local abspath = vim.fn.fnamemodify(dir, ":p")
|
local abspath = vim.fn.fnamemodify(dir, ":p")
|
||||||
local path = fs.os_to_posix_path(abspath)
|
local path = fs.os_to_posix_path(abspath)
|
||||||
return config.adapters.files .. path
|
return config.adapter_to_scheme.files .. path
|
||||||
else
|
else
|
||||||
local bufname = vim.api.nvim_buf_get_name(0)
|
local bufname = vim.api.nvim_buf_get_name(0)
|
||||||
return M.get_buffer_parent_url(bufname)
|
return M.get_buffer_parent_url(bufname)
|
||||||
|
|
@ -175,7 +175,7 @@ M.get_buffer_parent_url = function(bufname)
|
||||||
local scheme, path = util.parse_url(bufname)
|
local scheme, path = util.parse_url(bufname)
|
||||||
if not scheme then
|
if not scheme then
|
||||||
local parent, basename
|
local parent, basename
|
||||||
scheme = config.adapters.files
|
scheme = config.adapter_to_scheme.files
|
||||||
if bufname == "" then
|
if bufname == "" then
|
||||||
parent = fs.os_to_posix_path(vim.fn.getcwd())
|
parent = fs.os_to_posix_path(vim.fn.getcwd())
|
||||||
else
|
else
|
||||||
|
|
@ -189,7 +189,7 @@ M.get_buffer_parent_url = function(bufname)
|
||||||
local adapter = config.get_adapter_by_scheme(scheme)
|
local adapter = config.get_adapter_by_scheme(scheme)
|
||||||
local parent_url
|
local parent_url
|
||||||
if adapter.get_parent then
|
if adapter.get_parent then
|
||||||
local adapter_scheme = config.adapters[adapter.name]
|
local adapter_scheme = config.adapter_to_scheme[adapter.name]
|
||||||
parent_url = adapter.get_parent(adapter_scheme .. path)
|
parent_url = adapter.get_parent(adapter_scheme .. path)
|
||||||
else
|
else
|
||||||
local parent = pathutil.parent(path)
|
local parent = pathutil.parent(path)
|
||||||
|
|
@ -410,7 +410,7 @@ local function maybe_hijack_directory_buffer(bufnr)
|
||||||
end
|
end
|
||||||
util.rename_buffer(
|
util.rename_buffer(
|
||||||
bufnr,
|
bufnr,
|
||||||
util.addslash(config.adapters.files .. vim.fn.fnamemodify(bufname, ":p"))
|
util.addslash(config.adapter_to_scheme.files .. vim.fn.fnamemodify(bufname, ":p"))
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -504,10 +504,7 @@ M.setup = function(opts)
|
||||||
|
|
||||||
local patterns = {}
|
local patterns = {}
|
||||||
for scheme in pairs(config.adapters) do
|
for scheme in pairs(config.adapters) do
|
||||||
-- We added a reverse lookup to config.adapters, so filter the keys
|
table.insert(patterns, scheme .. "*")
|
||||||
if vim.endswith(scheme, "://") then
|
|
||||||
table.insert(patterns, scheme .. "*")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
local scheme_pattern = table.concat(patterns, ",")
|
local scheme_pattern = table.concat(patterns, ",")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue