refactor: small perf win by eliminating string object keys

This commit is contained in:
Steven Arcangeli 2023-06-25 10:14:28 -07:00
parent db60c32d93
commit 4a4e0f4013
12 changed files with 151 additions and 111 deletions

View file

@ -1,13 +1,16 @@
local cache = require("oil.cache")
local columns = require("oil.columns")
local config = require("oil.config")
local constants = require("oil.constants")
local fs = require("oil.fs")
local permissions = require("oil.adapters.files.permissions")
local trash = require("oil.adapters.files.trash")
local util = require("oil.util")
local FIELD = require("oil.constants").FIELD
local M = {}
local FIELD_NAME = constants.FIELD_NAME
local FIELD_META = constants.FIELD_META
local function read_link_data(path, cb)
vim.loop.fs_readlink(
path,
@ -44,7 +47,7 @@ local fs_stat_meta_fields = {
stat = function(parent_url, entry, cb)
local _, path = util.parse_url(parent_url)
local dir = fs.posix_to_os_path(path)
vim.loop.fs_stat(fs.join(dir, entry[FIELD.name]), cb)
vim.loop.fs_stat(fs.join(dir, entry[FIELD_NAME]), cb)
end,
}
@ -52,7 +55,7 @@ file_columns.size = {
meta_fields = fs_stat_meta_fields,
render = function(entry, conf)
local meta = entry[FIELD.meta]
local meta = entry[FIELD_META]
local stat = meta.stat
if not stat then
return ""
@ -79,7 +82,7 @@ if not fs.is_windows then
meta_fields = fs_stat_meta_fields,
render = function(entry, conf)
local meta = entry[FIELD.meta]
local meta = entry[FIELD_META]
local stat = meta.stat
if not stat then
return ""
@ -92,7 +95,7 @@ if not fs.is_windows then
end,
compare = function(entry, parsed_value)
local meta = entry[FIELD.meta]
local meta = entry[FIELD_META]
if parsed_value and meta.stat and meta.stat.mode then
local mask = bit.lshift(1, 12) - 1
local old_mode = bit.band(meta.stat.mode, mask)
@ -135,7 +138,7 @@ for _, time_key in ipairs({ "ctime", "mtime", "atime", "birthtime" }) do
meta_fields = fs_stat_meta_fields,
render = function(entry, conf)
local meta = entry[FIELD.meta]
local meta = entry[FIELD_META]
local stat = meta.stat
local fmt = conf and conf.format
local ret
@ -253,7 +256,7 @@ M.list = function(url, column_defs, callback)
if err then
poll(meta_err)
else
local meta = cache_entry[FIELD.meta]
local meta = cache_entry[FIELD_META]
-- Make sure we always get fs_stat info for links
if entry.type == "link" then
read_link_data(fs.join(dir, entry.name), function(link_err, link, link_stat)
@ -262,7 +265,7 @@ M.list = function(url, column_defs, callback)
else
if not meta then
meta = {}
cache_entry[FIELD.meta] = meta
cache_entry[FIELD_META] = meta
end
meta.link = link
meta.link_stat = link_stat

View file

@ -1,5 +1,6 @@
local cache = require("oil.cache")
local config = require("oil.config")
local constants = require("oil.constants")
local fs = require("oil.fs")
local files = require("oil.adapters.files")
local loading = require("oil.loading")
@ -8,9 +9,10 @@ local sshfs = require("oil.adapters.ssh.sshfs")
local pathutil = require("oil.pathutil")
local shell = require("oil.shell")
local util = require("oil.util")
local FIELD = require("oil.constants").FIELD
local M = {}
local FIELD_META = constants.FIELD_META
---@class oil.sshUrl
---@field scheme string
---@field host string
@ -96,7 +98,7 @@ end
local ssh_columns = {}
ssh_columns.permissions = {
render = function(entry, conf)
local meta = entry[FIELD.meta]
local meta = entry[FIELD_META]
return permissions.mode_to_str(meta.mode)
end,
@ -105,7 +107,7 @@ ssh_columns.permissions = {
end,
compare = function(entry, parsed_value)
local meta = entry[FIELD.meta]
local meta = entry[FIELD_META]
if parsed_value and meta.mode then
local mask = bit.lshift(1, 12) - 1
local old_mode = bit.band(meta.mode, mask)
@ -129,7 +131,7 @@ ssh_columns.permissions = {
ssh_columns.size = {
render = function(entry, conf)
local meta = entry[FIELD.meta]
local meta = entry[FIELD_META]
if not meta.size then
return ""
elseif meta.size >= 1e9 then

View file

@ -1,10 +1,13 @@
local cache = require("oil.cache")
local constants = require("oil.constants")
local permissions = require("oil.adapters.files.permissions")
local SSHConnection = require("oil.adapters.ssh.connection")
local util = require("oil.util")
local FIELD = require("oil.constants").FIELD
local SSHFS = {}
local FIELD_TYPE = constants.FIELD_TYPE
local FIELD_META = constants.FIELD_META
local typechar_map = {
l = "link",
d = "directory",
@ -143,7 +146,7 @@ function SSHFS:list_dir(url, path, callback)
end
local cache_entry = cache.create_entry(url, name, type)
entries[name] = cache_entry
cache_entry[FIELD.meta] = meta
cache_entry[FIELD_META] = meta
cache.store_entry(url, cache_entry)
end
end
@ -161,8 +164,8 @@ function SSHFS:list_dir(url, path, callback)
local ok, name, type, meta = pcall(parse_ls_line, line)
if ok and name ~= "." and name ~= ".." then
local cache_entry = entries[name]
if cache_entry[FIELD.type] == "link" then
cache_entry[FIELD.meta].link_stat = {
if cache_entry[FIELD_TYPE] == "link" then
cache_entry[FIELD_META].link_stat = {
type = type,
size = meta.size,
}

View file

@ -1,7 +1,10 @@
local constants = require("oil.constants")
local util = require("oil.util")
local FIELD = require("oil.constants").FIELD
local M = {}
local FIELD_ID = constants.FIELD_ID
local FIELD_NAME = constants.FIELD_NAME
local next_id = 1
-- Map<url, Map<entry name, oil.InternalEntry>>
@ -64,8 +67,8 @@ M.store_entry = function(parent_url, entry)
parent = {}
url_directory[parent_url] = parent
end
local id = entry[FIELD.id]
local name = entry[FIELD.name]
local id = entry[FIELD_ID]
local name = entry[FIELD_NAME]
parent[name] = entry
local tmp_dir = tmp_url_directory[parent_url]
if tmp_dir and tmp_dir[name] then
@ -97,7 +100,7 @@ M.end_update_url = function(parent_url)
return
end
for _, old_entry in pairs(tmp_url_directory[parent_url]) do
local id = old_entry[FIELD.id]
local id = old_entry[FIELD_ID]
parent_url_by_id[id] = nil
entries_by_id[id] = nil
end
@ -146,8 +149,8 @@ M.perform_action = function(action)
local name = vim.fn.fnamemodify(path, ":t")
local entry = url_directory[parent_url][name]
url_directory[parent_url][name] = nil
entries_by_id[entry[FIELD.id]] = nil
parent_url_by_id[entry[FIELD.id]] = nil
entries_by_id[entry[FIELD_ID]] = nil
parent_url_by_id[entry[FIELD_ID]] = nil
elseif action.type == "move" then
local src_scheme, src_path = util.parse_url(action.src_url)
local src_parent_url = util.addslash(src_scheme .. vim.fn.fnamemodify(src_path, ":h"))
@ -165,8 +168,8 @@ M.perform_action = function(action)
url_directory[dest_parent_url] = dest_parent
end
dest_parent[dest_name] = entry
parent_url_by_id[entry[FIELD.id]] = dest_parent_url
entry[FIELD.name] = dest_name
parent_url_by_id[entry[FIELD_ID]] = dest_parent_url
entry[FIELD_NAME] = dest_name
util.update_moved_buffers(action.entry_type, action.src_url, action.dest_url)
elseif action.type == "copy" then
local scheme, path = util.parse_url(action.dest_url)

View file

@ -1,9 +1,13 @@
local config = require("oil.config")
local constants = require("oil.constants")
local util = require("oil.util")
local has_devicons, devicons = pcall(require, "nvim-web-devicons")
local FIELD = require("oil.constants").FIELD
local M = {}
local FIELD_NAME = constants.FIELD_NAME
local FIELD_TYPE = constants.FIELD_TYPE
local FIELD_META = constants.FIELD_META
local all_columns = {}
---@alias oil.ColumnSpec string|table
@ -71,7 +75,7 @@ M.get_metadata_fetcher = function(adapter, column_defs)
return function(parent_url, entry, cb)
cb = util.cb_collect(num_keys, cb)
local meta = {}
entry[FIELD.meta] = meta
entry[FIELD_META] = meta
for k, v in pairs(keyfetches) do
v(parent_url, entry, function(err, value)
if err then
@ -101,7 +105,7 @@ M.render_col = function(adapter, col_def, entry)
-- Make sure all the required metadata exists before attempting to render
if column.meta_fields then
local meta = entry[FIELD.meta]
local meta = entry[FIELD_META]
if not meta then
return EMPTY
end
@ -193,9 +197,9 @@ end
if has_devicons then
M.register("icon", {
render = function(entry, conf)
local type = entry[FIELD.type]
local name = entry[FIELD.name]
local meta = entry[FIELD.meta]
local type = entry[FIELD_TYPE]
local name = entry[FIELD_NAME]
local meta = entry[FIELD_META]
if type == "link" and meta then
if meta.link then
name = meta.link
@ -228,7 +232,7 @@ local default_type_icons = {
}
M.register("type", {
render = function(entry, conf)
local entry_type = entry[FIELD.type]
local entry_type = entry[FIELD_TYPE]
if conf and conf.icons then
return conf.icons[entry_type] or entry_type
else

View file

@ -1,10 +1,13 @@
local M = {}
M.FIELD = {
id = 1,
name = 2,
type = 3,
meta = 4,
}
---Store entries as a list-like table for maximum space efficiency and retrieval speed.
---We use the constants below to index into the table.
---@alias oil.InternalEntry any[]
-- Indexes into oil.InternalEntry
M.FIELD_ID = 1
M.FIELD_NAME = 2
M.FIELD_TYPE = 3
M.FIELD_META = 4
return M

View file

@ -1,7 +1,5 @@
local M = {}
---@alias oil.InternalEntry string[]
---@class oil.Entry
---@field name string
---@field type oil.EntryType

View file

@ -1,6 +1,7 @@
local cache = require("oil.cache")
local columns = require("oil.columns")
local config = require("oil.config")
local constants = require("oil.constants")
local oil = require("oil")
local parser = require("oil.mutator.parser")
local pathutil = require("oil.pathutil")
@ -9,9 +10,11 @@ local Progress = require("oil.mutator.progress")
local Trie = require("oil.mutator.trie")
local util = require("oil.util")
local view = require("oil.view")
local FIELD = require("oil.constants").FIELD
local M = {}
local FIELD_NAME = constants.FIELD_NAME
local FIELD_TYPE = constants.FIELD_TYPE
---@alias oil.Action oil.CreateAction|oil.DeleteAction|oil.MoveAction|oil.CopyAction|oil.ChangeAction
---@class oil.CreateAction
@ -129,17 +132,17 @@ M.create_actions_from_diffs = function(all_diffs)
for i, diff in ipairs(diffs) do
table.insert(actions, {
type = i == #diffs and "move" or "copy",
entry_type = entry[FIELD.type],
entry_type = entry[FIELD_TYPE],
dest_url = diff.dest,
src_url = cache.get_parent_url(id) .. entry[FIELD.name],
src_url = cache.get_parent_url(id) .. entry[FIELD_NAME],
})
end
else
-- DELETE when no create
table.insert(actions, {
type = "delete",
entry_type = entry[FIELD.type],
url = cache.get_parent_url(id) .. entry[FIELD.name],
entry_type = entry[FIELD_TYPE],
url = cache.get_parent_url(id) .. entry[FIELD_NAME],
})
end
else
@ -147,8 +150,8 @@ M.create_actions_from_diffs = function(all_diffs)
for _, diff in ipairs(diffs) do
table.insert(actions, {
type = "copy",
entry_type = entry[FIELD.type],
src_url = cache.get_parent_url(id) .. entry[FIELD.name],
entry_type = entry[FIELD_TYPE],
src_url = cache.get_parent_url(id) .. entry[FIELD_NAME],
dest_url = diff.dest,
})
end

View file

@ -1,11 +1,16 @@
local cache = require("oil.cache")
local columns = require("oil.columns")
local constants = require("oil.constants")
local fs = require("oil.fs")
local util = require("oil.util")
local view = require("oil.view")
local FIELD = require("oil.constants").FIELD
local M = {}
local FIELD_ID = constants.FIELD_ID
local FIELD_NAME = constants.FIELD_NAME
local FIELD_TYPE = constants.FIELD_TYPE
local FIELD_META = constants.FIELD_META
---@alias oil.Diff oil.DiffNew|oil.DiffDelete|oil.DiffChange
---@class oil.DiffNew
@ -102,8 +107,8 @@ M.parse_line = function(adapter, line, column_defs)
end
-- Parse the symlink syntax
local meta = entry[FIELD.meta]
local entry_type = entry[FIELD.type]
local meta = entry[FIELD_META]
local entry_type = entry[FIELD_TYPE]
if entry_type == "link" and meta and meta.link then
local name_pieces = vim.split(ret.name, " -> ", { plain = true })
if #name_pieces ~= 2 then
@ -118,7 +123,7 @@ M.parse_line = function(adapter, line, column_defs)
-- Try to keep the same file type
if entry_type ~= "directory" and entry_type ~= "file" and ret._type ~= "directory" then
ret._type = entry[FIELD.type]
ret._type = entry[FIELD_TYPE]
end
return { data = ret, entry = entry, ranges = ranges }
@ -148,7 +153,7 @@ M.parse = function(bufnr)
local original_entries = {}
for _, child in pairs(children) do
if view.should_display(child, bufnr) then
original_entries[child[FIELD.name]] = child[FIELD.id]
original_entries[child[FIELD_NAME]] = child[FIELD_ID]
end
end
local seen_names = {}
@ -193,9 +198,9 @@ M.parse = function(bufnr)
goto continue
end
check_dupe(parsed_entry.name, i)
local meta = entry[FIELD.meta]
local meta = entry[FIELD_META]
if original_entries[parsed_entry.name] == parsed_entry.id then
if entry[FIELD.type] == "link" and not compare_link_target(meta, parsed_entry) then
if entry[FIELD_TYPE] == "link" and not compare_link_target(meta, parsed_entry) then
table.insert(diffs, {
type = "new",
name = parsed_entry.name,
@ -221,7 +226,7 @@ M.parse = function(bufnr)
table.insert(diffs, {
type = "change",
name = parsed_entry.name,
entry_type = entry[FIELD.type],
entry_type = entry[FIELD_TYPE],
column = col_name,
value = parsed_entry[col_name],
})

View file

@ -1,6 +1,13 @@
local config = require("oil.config")
local constants = require("oil.constants")
local M = {}
local FIELD_ID = constants.FIELD_ID
local FIELD_NAME = constants.FIELD_NAME
local FIELD_TYPE = constants.FIELD_TYPE
local FIELD_META = constants.FIELD_META
---@param url string
---@return nil|string
---@return nil|string
@ -84,12 +91,11 @@ end
---@param entry oil.InternalEntry
---@return oil.Entry
M.export_entry = function(entry)
local FIELD = require("oil.constants").FIELD
return {
name = entry[FIELD.name],
type = entry[FIELD.type],
id = entry[FIELD.id],
meta = entry[FIELD.meta],
name = entry[FIELD_NAME],
type = entry[FIELD_TYPE],
id = entry[FIELD_ID],
meta = entry[FIELD_META],
}
end

View file

@ -1,12 +1,17 @@
local cache = require("oil.cache")
local columns = require("oil.columns")
local config = require("oil.config")
local constants = require("oil.constants")
local keymap_util = require("oil.keymap_util")
local loading = require("oil.loading")
local util = require("oil.util")
local FIELD = require("oil.constants").FIELD
local M = {}
local FIELD_ID = constants.FIELD_ID
local FIELD_NAME = constants.FIELD_NAME
local FIELD_TYPE = constants.FIELD_TYPE
local FIELD_META = constants.FIELD_META
-- map of path->last entry under cursor
local last_cursor_entry = {}
@ -14,7 +19,7 @@ local last_cursor_entry = {}
---@param bufnr integer
---@return boolean
M.should_display = function(entry, bufnr)
local name = entry[FIELD.name]
local name = entry[FIELD_NAME]
return not config.view_options.is_always_hidden(name, bufnr)
and (not config.view_options.is_hidden_file(name, bufnr) or config.view_options.show_hidden)
end
@ -349,11 +354,11 @@ end
---@param entry oil.InternalEntry
---@return boolean
local function is_entry_directory(entry)
local type = entry[FIELD.type]
local type = entry[FIELD_TYPE]
if type == "directory" then
return true
elseif type == "link" then
local meta = entry[FIELD.meta]
local meta = entry[FIELD_META]
return meta and meta.link_stat and meta.link_stat.type == "directory"
else
return false
@ -391,7 +396,7 @@ local function render_buffer(bufnr, opts)
if a_isdir ~= b_isdir then
return a_isdir
end
return a[FIELD.name] < b[FIELD.name]
return a[FIELD_NAME] < b[FIELD_NAME]
end)
local jump_idx
@ -414,7 +419,7 @@ local function render_buffer(bufnr, opts)
local cols = M.format_entry_cols(entry, column_defs, col_width, adapter)
table.insert(line_table, cols)
local name = entry[FIELD.name]
local name = entry[FIELD_NAME]
if seek_after_render == name then
seek_after_render_found = true
jump_idx = #line_table
@ -448,7 +453,7 @@ local function render_buffer(bufnr, opts)
if id then
local entry = cache.get_entry_by_id(id)
if entry then
local name = entry[FIELD.name]
local name = entry[FIELD_NAME]
local col = line:find(name, 1, true) or (id_str:len() + 1)
vim.api.nvim_win_set_cursor(winid, { lnum, col - 1 })
end
@ -467,10 +472,10 @@ end
---@param adapter oil.Adapter
---@return oil.TextChunk[]
M.format_entry_cols = function(entry, column_defs, col_width, adapter)
local name = entry[FIELD.name]
local name = entry[FIELD_NAME]
-- First put the unique ID
local cols = {}
local id_key = cache.format_id(entry[FIELD.id])
local id_key = cache.format_id(entry[FIELD_ID])
col_width[1] = id_key:len()
table.insert(cols, id_key)
-- Then add all the configured columns
@ -481,13 +486,13 @@ M.format_entry_cols = function(entry, column_defs, col_width, adapter)
table.insert(cols, chunk)
end
-- Always add the entry name at the end
local entry_type = entry[FIELD.type]
local entry_type = entry[FIELD_TYPE]
if entry_type == "directory" then
table.insert(cols, { name .. "/", "OilDir" })
elseif entry_type == "socket" then
table.insert(cols, { name, "OilSocket" })
elseif entry_type == "link" then
local meta = entry[FIELD.meta]
local meta = entry[FIELD_META]
local link_text
if meta then
if meta.link_stat and meta.link_stat.type == "directory" then

View file

@ -1,11 +1,16 @@
require("plenary.async").tests.add_to_env()
local FIELD = require("oil.constants").FIELD
local view = require("oil.view")
local cache = require("oil.cache")
local constants = require("oil.constants")
local mutator = require("oil.mutator")
local parser = require("oil.mutator.parser")
local test_adapter = require("oil.adapters.test")
local util = require("oil.util")
local view = require("oil.view")
local FIELD_ID = constants.FIELD_ID
local FIELD_NAME = constants.FIELD_NAME
local FIELD_TYPE = constants.FIELD_TYPE
local FIELD_META = constants.FIELD_META
local function set_lines(bufnr, lines)
vim.bo[bufnr].modifiable = true
@ -61,7 +66,7 @@ a.describe("mutator", function()
set_lines(bufnr, {})
local diffs = parser.parse(bufnr)
assert.are.same({
{ name = "a.txt", type = "delete", id = file[FIELD.id] },
{ name = "a.txt", type = "delete", id = file[FIELD_ID] },
}, diffs)
end)
@ -72,19 +77,19 @@ a.describe("mutator", function()
set_lines(bufnr, {})
local diffs = parser.parse(bufnr)
assert.are.same({
{ name = "bar", type = "delete", id = dir[FIELD.id] },
{ name = "bar", type = "delete", id = dir[FIELD_ID] },
}, diffs)
end)
it("detects deleted links", function()
local file = cache.create_and_store_entry("oil-test:///foo/", "a.txt", "link")
file[FIELD.meta] = { link = "b.txt" }
file[FIELD_META] = { link = "b.txt" }
vim.cmd.edit({ args = { "oil-test:///foo/" } })
local bufnr = vim.api.nvim_get_current_buf()
set_lines(bufnr, {})
local diffs = parser.parse(bufnr)
assert.are.same({
{ name = "a.txt", type = "delete", id = file[FIELD.id] },
{ name = "a.txt", type = "delete", id = file[FIELD_ID] },
}, diffs)
end)
@ -156,7 +161,7 @@ a.describe("mutator", function()
local bufnr = vim.api.nvim_get_current_buf()
set_lines(bufnr, {
"a.txt",
string.format("/%d a.txt", file[FIELD.id]),
string.format("/%d a.txt", file[FIELD_ID]),
})
local _, errors = parser.parse(bufnr)
assert.are.same({
@ -183,12 +188,12 @@ a.describe("mutator", function()
vim.cmd.edit({ args = { "oil-test:///foo/" } })
local bufnr = vim.api.nvim_get_current_buf()
set_lines(bufnr, {
string.format("/%d b.txt", file[FIELD.id]),
string.format("/%d b.txt", file[FIELD_ID]),
})
local diffs = parser.parse(bufnr)
assert.are.same({
{ type = "new", id = file[FIELD.id], name = "b.txt", entry_type = "file" },
{ type = "delete", id = file[FIELD.id], name = "a.txt" },
{ type = "new", id = file[FIELD_ID], name = "b.txt", entry_type = "file" },
{ type = "delete", id = file[FIELD_ID], name = "a.txt" },
}, diffs)
end)
@ -198,8 +203,8 @@ a.describe("mutator", function()
vim.cmd.edit({ args = { "oil-test:///foo/" } })
local bufnr = vim.api.nvim_get_current_buf()
set_lines(bufnr, {
string.format("/%d a.txt", bfile[FIELD.id]),
string.format("/%d b.txt", afile[FIELD.id]),
string.format("/%d a.txt", bfile[FIELD_ID]),
string.format("/%d b.txt", afile[FIELD_ID]),
})
local diffs = parser.parse(bufnr)
local first_two = { diffs[1], diffs[2] }
@ -211,22 +216,22 @@ a.describe("mutator", function()
return a.id < b.id
end)
assert.are.same({
{ name = "b.txt", type = "new", id = afile[FIELD.id], entry_type = "file" },
{ name = "a.txt", type = "new", id = bfile[FIELD.id], entry_type = "file" },
{ name = "b.txt", type = "new", id = afile[FIELD_ID], entry_type = "file" },
{ name = "a.txt", type = "new", id = bfile[FIELD_ID], entry_type = "file" },
}, first_two)
assert.are.same({
{ name = "a.txt", type = "delete", id = afile[FIELD.id] },
{ name = "b.txt", type = "delete", id = bfile[FIELD.id] },
{ name = "a.txt", type = "delete", id = afile[FIELD_ID] },
{ name = "b.txt", type = "delete", id = bfile[FIELD_ID] },
}, last_two)
end)
it("views link targets with trailing slashes as the same", function()
local file = cache.create_and_store_entry("oil-test:///foo/", "mydir", "link")
file[FIELD.meta] = { link = "dir/" }
file[FIELD_META] = { link = "dir/" }
vim.cmd.edit({ args = { "oil-test:///foo/" } })
local bufnr = vim.api.nvim_get_current_buf()
set_lines(bufnr, {
string.format("/%d mydir/ -> dir/", file[FIELD.id]),
string.format("/%d mydir/ -> dir/", file[FIELD_ID]),
})
local diffs = parser.parse(bufnr)
assert.are.same({}, diffs)
@ -266,7 +271,7 @@ a.describe("mutator", function()
vim.cmd.edit({ args = { "oil-test:///foo/" } })
local bufnr = vim.api.nvim_get_current_buf()
local diffs = {
{ type = "delete", name = "a.txt", id = file[FIELD.id] },
{ type = "delete", name = "a.txt", id = file[FIELD_ID] },
}
local actions = mutator.create_actions_from_diffs({
[bufnr] = diffs,
@ -285,7 +290,7 @@ a.describe("mutator", function()
vim.cmd.edit({ args = { "oil-test:///foo/" } })
local bufnr = vim.api.nvim_get_current_buf()
local diffs = {
{ type = "new", name = "b.txt", entry_type = "file", id = file[FIELD.id] },
{ type = "new", name = "b.txt", entry_type = "file", id = file[FIELD_ID] },
}
local actions = mutator.create_actions_from_diffs({
[bufnr] = diffs,
@ -305,8 +310,8 @@ a.describe("mutator", function()
vim.cmd.edit({ args = { "oil-test:///foo/" } })
local bufnr = vim.api.nvim_get_current_buf()
local diffs = {
{ type = "delete", name = "a.txt", id = file[FIELD.id] },
{ type = "new", name = "b.txt", entry_type = "file", id = file[FIELD.id] },
{ type = "delete", name = "a.txt", id = file[FIELD_ID] },
{ type = "new", name = "b.txt", entry_type = "file", id = file[FIELD_ID] },
}
local actions = mutator.create_actions_from_diffs({
[bufnr] = diffs,
@ -326,8 +331,8 @@ a.describe("mutator", function()
vim.cmd.edit({ args = { "oil-test:///" } })
local bufnr = vim.api.nvim_get_current_buf()
local diffs = {
{ type = "delete", name = "a.txt", id = file[FIELD.id] },
{ type = "new", name = "b.txt", entry_type = "file", id = file[FIELD.id] },
{ type = "delete", name = "a.txt", id = file[FIELD_ID] },
{ type = "new", name = "b.txt", entry_type = "file", id = file[FIELD_ID] },
{ type = "new", name = "a.txt", entry_type = "file" },
}
local actions = mutator.create_actions_from_diffs({
@ -354,10 +359,10 @@ a.describe("mutator", function()
vim.cmd.edit({ args = { "oil-test:///" } })
local bufnr = vim.api.nvim_get_current_buf()
local diffs = {
{ type = "delete", name = "a.txt", id = afile[FIELD.id] },
{ type = "new", name = "b.txt", entry_type = "file", id = afile[FIELD.id] },
{ type = "delete", name = "b.txt", id = bfile[FIELD.id] },
{ type = "new", name = "a.txt", entry_type = "file", id = bfile[FIELD.id] },
{ type = "delete", name = "a.txt", id = afile[FIELD_ID] },
{ type = "new", name = "b.txt", entry_type = "file", id = afile[FIELD_ID] },
{ type = "delete", name = "b.txt", id = bfile[FIELD_ID] },
{ type = "new", name = "a.txt", entry_type = "file", id = bfile[FIELD_ID] },
}
math.randomseed(2983982)
local actions = mutator.create_actions_from_diffs({
@ -541,9 +546,9 @@ a.describe("mutator", function()
local files = cache.list_url("oil-test:///")
assert.are.same({
["a.txt"] = {
[FIELD.id] = 1,
[FIELD.type] = "file",
[FIELD.name] = "a.txt",
[FIELD_ID] = 1,
[FIELD_TYPE] = "file",
[FIELD_NAME] = "a.txt",
},
}, files)
end)
@ -556,9 +561,9 @@ a.describe("mutator", function()
a.wrap(mutator.process_actions, 2)(actions)
local files = cache.list_url("oil-test:///")
assert.are.same({}, files)
assert.is_nil(cache.get_entry_by_id(file[FIELD.id]))
assert.is_nil(cache.get_entry_by_id(file[FIELD_ID]))
assert.has_error(function()
cache.get_parent_url(file[FIELD.id])
cache.get_parent_url(file[FIELD_ID])
end)
end)
@ -575,15 +580,15 @@ a.describe("mutator", function()
a.wrap(mutator.process_actions, 2)(actions)
local files = cache.list_url("oil-test:///")
local new_entry = {
[FIELD.id] = file[FIELD.id],
[FIELD.type] = "file",
[FIELD.name] = "b.txt",
[FIELD_ID] = file[FIELD_ID],
[FIELD_TYPE] = "file",
[FIELD_NAME] = "b.txt",
}
assert.are.same({
["b.txt"] = new_entry,
}, files)
assert.are.same(new_entry, cache.get_entry_by_id(file[FIELD.id]))
assert.equals("oil-test:///", cache.get_parent_url(file[FIELD.id]))
assert.are.same(new_entry, cache.get_entry_by_id(file[FIELD_ID]))
assert.equals("oil-test:///", cache.get_parent_url(file[FIELD_ID]))
end)
a.it("copies entries", function()
@ -599,9 +604,9 @@ a.describe("mutator", function()
a.wrap(mutator.process_actions, 2)(actions)
local files = cache.list_url("oil-test:///")
local new_entry = {
[FIELD.id] = file[FIELD.id] + 1,
[FIELD.type] = "file",
[FIELD.name] = "b.txt",
[FIELD_ID] = file[FIELD_ID] + 1,
[FIELD_TYPE] = "file",
[FIELD_NAME] = "b.txt",
}
assert.are.same({
["a.txt"] = file,