Skip to content

Commit

Permalink
Handling files which already exist in the subdirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
timtatt committed Jul 7, 2024
1 parent 1bdd9b8 commit 391b42c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lua/oil/adapters/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,15 @@ local function list_windows_drives(url, column_defs, cb)
end
end

---@param url string
M.file_exists = function(url)
local _, path = util.parse_url(url)
assert(path)
local dir = fs.posix_to_os_path(path)

return fs.file_exists(dir)
end

---@param url string
---@param column_defs string[]
---@param cb fun(err?: string, entries?: oil.InternalEntry[], fetch_more?: fun())
Expand Down
6 changes: 6 additions & 0 deletions lua/oil/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ M.mkdirp = function(dir, mode)
end
end

--- @param path string
--- @return boolean
M.file_exists = function(path)
return uv.fs_stat(path) ~= nil
end

---@param dir string
---@param cb fun(err: nil|string, entries: nil|{type: oil.EntryType, name: string})
M.listdir = function(dir, cb)
Expand Down
1 change: 1 addition & 0 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ local M = {}
---@class (exact) oil.Adapter
---@field name string The unique name of the adapter (this will be set automatically)
---@field list fun(path: string, column_defs: string[], cb: fun(err?: string, entries?: oil.InternalEntry[], fetch_more?: fun())) Async function to list a directory.
---@field file_exists fun(path: string) Returns true if the file at path exists
---@field is_modifiable fun(bufnr: integer): boolean Return true if this directory is modifiable (allows for directories with read-only permissions).
---@field get_column fun(name: string): nil|oil.ColumnDefinition If the adapter has any adapter-specific columns, return them when fetched by name.
---@field get_parent? fun(bufname: string): string Get the parent url of the given buffer
Expand Down
28 changes: 28 additions & 0 deletions lua/oil/mutator/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,31 @@ M.parse = function(bufnr)
-- mac and windows use case-insensitive filesystems
name = name:lower()
end
local has_subdir = string.find(name, fs.sep)
local subdir_name = has_subdir
and string.match(name, string.format("([^%s]+)%s", fs.sep, fs.sep))
or nil
if seen_names[name] then
table.insert(errors, { message = "Duplicate filename", lnum = i - 1, end_lnum = i, col = 0 })
elseif has_subdir and adapter.file_exists(parent_url .. name) then
table.insert(
errors,
{ message = "Duplicate file in nested path", lnum = i - 1, end_lnum = i, col = 0 }
)
elseif
has_subdir
and seen_names[subdir_name]
and not adapter.file_exists(parent_url .. subdir_name)
then
table.insert(errors, {
message = "Cannot move a file to a directory which has been renamed in the same action",
lnum = i - 1,
end_lnum = i,
col = 0,
})
elseif has_subdir then
seen_names[subdir_name] = true
seen_names[name] = true
else
seen_names[name] = true
end
Expand Down Expand Up @@ -216,6 +239,11 @@ M.parse = function(bufnr)
err_message = "No filename found"
elseif not entry then
err_message = "Could not find existing entry (was the ID changed?)"
elseif
adapter.name ~= "files"
and (parsed_entry.name:match("/") or parsed_entry.name:match(fs.sep))
then
err_message = "Filename cannot contain path separator"
end
if err_message then
table.insert(errors, {
Expand Down

0 comments on commit 391b42c

Please sign in to comment.