refactor: remove cache side effects from adapter.list

This commit is contained in:
Steven Arcangeli 2023-08-20 20:56:54 +00:00
parent ffb89bf416
commit f4819d8b43
8 changed files with 86 additions and 53 deletions

View file

@ -1,4 +1,5 @@
local cache = require("oil.cache")
local util = require("oil.util")
local M = {}
---@param url string
@ -7,11 +8,49 @@ M.normalize_url = function(url, callback)
callback(url)
end
local dir_listing = {}
---@param url string
---@param column_defs string[]
---@param cb fun(err?: string, fetch_more?: fun())
---@param cb fun(err?: string, entries?: oil.InternalEntry[], fetch_more?: fun())
M.list = function(url, column_defs, cb)
cb()
local _, path = util.parse_url(url)
local entries = dir_listing[path] or {}
local cache_entries = {}
for _, entry in ipairs(entries) do
local cache_entry = cache.create_entry(url, entry.name, entry.entry_type)
table.insert(cache_entries, cache_entry)
end
cb(nil, cache_entries)
end
M.test_clear = function()
dir_listing = {}
end
---@param path string
---@param entry_type oil.EntryType
---@return oil.InternalEntry
M.test_set = function(path, entry_type)
if path == "/" then
return {}
end
local parent = vim.fn.fnamemodify(path, ":h")
if parent ~= path then
M.test_set(parent, "directory")
end
parent = util.addslash(parent)
if not dir_listing[parent] then
dir_listing[parent] = {}
end
local name = vim.fn.fnamemodify(path, ":t")
local entry = {
name = name,
entry_type = entry_type,
}
table.insert(dir_listing[parent], entry)
local parent_url = "oil-test://" .. parent
return cache.create_and_store_entry(parent_url, entry.name, entry.entry_type)
end
---@param name string
@ -20,22 +59,6 @@ M.get_column = function(name)
return nil
end
---@param path string
---@param entry_type oil.EntryType
M.test_set = function(path, entry_type)
local parent = vim.fn.fnamemodify(path, ":h")
if parent ~= path then
M.test_set(parent, "directory")
end
local url = "oil-test://" .. path
if cache.get_entry_by_url(url) then
-- Already exists
return
end
local name = vim.fn.fnamemodify(path, ":t")
cache.create_and_store_entry("oil-test://" .. parent, name, entry_type)
end
---@param bufnr integer
---@return boolean
M.is_modifiable = function(bufnr)