fix: allow converting a file to directory and vice-versa (#117)

If you rename a file to have a `/` at the end, oil will detect the
change as a delete + new directory. Similarly, if you delete the
trailing `/` from a directory, it will delete the dir and create a new
file. This should help with the case where you want to create a new file
but forgot to add the trailing `/` and now you have a file instead.
This commit is contained in:
Steven Arcangeli 2023-09-09 18:33:43 -07:00
parent d4eb4f3bbf
commit 926ae067eb
2 changed files with 20 additions and 0 deletions

View file

@ -207,6 +207,12 @@ M.parse = function(bufnr)
entry_type = "link",
link = parsed_entry.link_target,
})
elseif entry[FIELD_TYPE] ~= parsed_entry._type then
table.insert(diffs, {
type = "new",
name = parsed_entry.name,
entry_type = parsed_entry._type,
})
else
original_entries[parsed_entry.name] = nil
end

View file

@ -200,6 +200,20 @@ a.describe("mutator", function()
}, diffs)
end)
it("detects a new trailing slash as a delete + create", function()
local file = test_adapter.test_set("/foo", "file")
vim.cmd.edit({ args = { "oil-test:///" } })
local bufnr = vim.api.nvim_get_current_buf()
set_lines(bufnr, {
string.format("/%d foo/", file[FIELD_ID]),
})
local diffs = parser.parse(bufnr)
assert.are.same({
{ type = "new", name = "foo", entry_type = "directory" },
{ type = "delete", id = file[FIELD_ID], name = "foo" },
}, diffs)
end)
it("detects renamed files that conflict", function()
local afile = test_adapter.test_set("/foo/a.txt", "file")
local bfile = test_adapter.test_set("/foo/b.txt", "file")