refactor: cleanup of file movement operations
parent
b0fac50905
commit
bdd2c44031
|
|
@ -43,7 +43,7 @@ end
|
||||||
---@param oldFilePath string
|
---@param oldFilePath string
|
||||||
---@param newFilePath string
|
---@param newFilePath string
|
||||||
function M.moveFile(oldFilePath, newFilePath)
|
function M.moveFile(oldFilePath, newFilePath)
|
||||||
local renamed, _renamedError = vim.loop.fs_rename(oldFilePath, newFilePath)
|
local renamed, _ = vim.loop.fs_rename(oldFilePath, newFilePath)
|
||||||
if renamed then
|
if renamed then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -47,20 +47,19 @@ local function fileOp(op)
|
||||||
completion = "dir", -- allows for completion via cmp-omni
|
completion = "dir", -- allows for completion via cmp-omni
|
||||||
}, function(newName)
|
}, function(newName)
|
||||||
cmd.redraw() -- Clear message area from ui.input prompt
|
cmd.redraw() -- Clear message area from ui.input prompt
|
||||||
|
|
||||||
-- VALIDATION OF FILENAME
|
|
||||||
if not newName then return end -- input has been canceled
|
if not newName then return end -- input has been canceled
|
||||||
|
|
||||||
-- if only directory is entered, move file to that location
|
|
||||||
if op == "move-rename" and newName:find("/$") then newName = newName .. oldName end
|
if op == "move-rename" and newName:find("/$") then newName = newName .. oldName end
|
||||||
|
if op == "new" and newName == "" then newName = "Untitled" end
|
||||||
|
|
||||||
|
-- GUARD Validate filename
|
||||||
local invalidName = newName:find("^%s+$")
|
local invalidName = newName:find("^%s+$")
|
||||||
or newName:find("[\\:]")
|
or newName:find("[\\:]")
|
||||||
or (newName:find("^/") and not op == "move-rename")
|
or (newName:find("^/") and not op == "move-rename")
|
||||||
local sameName = newName == oldName
|
local sameName = newName == oldName
|
||||||
local emptyInput = newName == ""
|
local emptyInput = newName == ""
|
||||||
|
|
||||||
if invalidName or sameName or (emptyInput and op ~= "new") then
|
if invalidName or sameName or emptyInput then
|
||||||
if op == "newFromSel" then
|
if op == "newFromSel" then
|
||||||
cmd.undo() -- undo deletion
|
cmd.undo() -- undo deletion
|
||||||
fn.setreg("z", prevReg) -- restore register content
|
fn.setreg("z", prevReg) -- restore register content
|
||||||
|
|
@ -68,14 +67,11 @@ local function fileOp(op)
|
||||||
if invalidName or emptyInput then
|
if invalidName or emptyInput then
|
||||||
u.notify("Invalid filename.", "error")
|
u.notify("Invalid filename.", "error")
|
||||||
elseif sameName then
|
elseif sameName then
|
||||||
u.notify("Cannot use the same filename.", "error")
|
u.notify("Cannot use the same filename.", "warn")
|
||||||
end
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- exception: new file creaton allows for empty input
|
|
||||||
if emptyInput and op == "new" then newName = "Untitled" end
|
|
||||||
|
|
||||||
-- DETERMINE PATH AND EXTENSION
|
-- DETERMINE PATH AND EXTENSION
|
||||||
local hasPath = newName:find("/")
|
local hasPath = newName:find("/")
|
||||||
if hasPath then
|
if hasPath then
|
||||||
|
|
@ -128,38 +124,30 @@ function M.moveSelectionToNewFile() fileOp("newFromSel") end
|
||||||
function M.moveToFolderInCwd()
|
function M.moveToFolderInCwd()
|
||||||
local oldFilePath = vim.api.nvim_buf_get_name(0)
|
local oldFilePath = vim.api.nvim_buf_get_name(0)
|
||||||
local filename = vim.fs.basename(oldFilePath)
|
local filename = vim.fs.basename(oldFilePath)
|
||||||
local cwd = vim.loop.cwd()
|
|
||||||
|
|
||||||
-- determine destinations in cwd
|
-- determine destinations in cwd
|
||||||
local subfoldersOfCwdAbsPath = vim.fs.find(function(name, path)
|
local subfoldersOfCwd = vim.fs.find(function(name, path)
|
||||||
-- TODO improve ignoring of certain folders
|
local ignoreDirs = (path:find("/%.git/") or path:find("/%.git$") or name == ".git")
|
||||||
return not (
|
|
||||||
(path:find("/%.git/") or path:find("/%.git$") or name == ".git")
|
|
||||||
or (path:find("%.app/") or path:find("%.app$")) -- macos pseudo-apps
|
or (path:find("%.app/") or path:find("%.app$")) -- macos pseudo-apps
|
||||||
or name == "node_modules"
|
or name == "node_modules"
|
||||||
)
|
or name == ".venv"
|
||||||
|
return not ignoreDirs
|
||||||
end, { type = "directory", limit = math.huge })
|
end, { type = "directory", limit = math.huge })
|
||||||
local subfoldersOfCwdRelPath = vim.tbl_map(
|
|
||||||
function(path) return path:sub(#cwd + 2) end,
|
|
||||||
subfoldersOfCwdAbsPath
|
|
||||||
)
|
|
||||||
|
|
||||||
-- prompt user and move
|
-- prompt user and move
|
||||||
local promptStr = "Destination Folder "
|
local promptStr = "Destination Folder "
|
||||||
if mv.lspSupportsRenaming() then promptStr = promptStr .. "& Update Imports" end
|
if mv.lspSupportsRenaming() then promptStr = promptStr .. "& Update Imports" end
|
||||||
vim.ui.select(subfoldersOfCwdRelPath, {
|
vim.ui.select(subfoldersOfCwd, {
|
||||||
prompt = promptStr,
|
prompt = promptStr,
|
||||||
kind = "genghis.moveToFolderInCwd",
|
kind = "genghis.moveToFolderInCwd",
|
||||||
|
format_item = function(path) return path:sub(#vim.loop.cwd() + 2) end, -- only relative path
|
||||||
}, function(destination)
|
}, function(destination)
|
||||||
if not destination then return end
|
if not destination then return end
|
||||||
local newFilePath = cwd .. "/" .. destination .. "/" .. filename
|
local newFilePath = destination .. "/" .. filename
|
||||||
|
|
||||||
-- GUARD conflict
|
-- GUARD
|
||||||
if u.fileExists(newFilePath) then
|
if u.fileExists(newFilePath) then
|
||||||
u.notify(
|
u.notify(("File %q already exists at %q."):format(filename, destination), "error")
|
||||||
("File with name %q already exists at %q."):format(filename, destination),
|
|
||||||
"error"
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -168,7 +156,7 @@ function M.moveToFolderInCwd()
|
||||||
if success then
|
if success then
|
||||||
cmd.edit(newFilePath)
|
cmd.edit(newFilePath)
|
||||||
u.bwipeout("#")
|
u.bwipeout("#")
|
||||||
u.notify(("Moved to %q."):format(destination))
|
u.notify(("Moved %q to %q."):format(filename, destination))
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
@ -240,7 +228,7 @@ function M.trashFile(opts)
|
||||||
u.bwipeout()
|
u.bwipeout()
|
||||||
u.notify(("%q deleted"):format(oldName))
|
u.notify(("%q deleted"):format(oldName))
|
||||||
else
|
else
|
||||||
u.notify(("Trashing %q failed: " .. errMsg):format(oldName), "warn")
|
u.notify(("Trashing %q failed: " .. errMsg):format(oldName), "error")
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue