chore: minor migration to the latest functions

remotes/origin/HEAD
Luca Saccarola 2023-07-04 17:09:04 +02:00
parent 6b6aba19c5
commit 29bd36d36e
1 changed files with 16 additions and 22 deletions

View File

@ -19,9 +19,7 @@ end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
local function fileExists(filepath) local function fileExists(filepath)
local file = io.open(filepath, "r") return vim.loop.fs_stat(filepath) ~= nil
if file then io.close(file) end
return file ~= nil
end end
---Performing common file operation tasks ---Performing common file operation tasks
@ -59,6 +57,8 @@ local function fileOp(op)
-- selene: allow(high_cyclomatic_complexity) -- selene: allow(high_cyclomatic_complexity)
-- INFO completion = "dir" allows for completion via cmp-omni -- INFO completion = "dir" allows for completion via cmp-omni
vim.ui.input({ prompt = promptStr, default = prefill, completion = "dir" }, function(newName) vim.ui.input({ prompt = promptStr, default = prefill, completion = "dir" }, function(newName)
-- Clear message area from ui.input prompt
cmd("echomsg ''")
-- VALIDATION OF FILENAME -- VALIDATION OF FILENAME
if not newName then return end -- input has been cancelled if not newName then return end -- input has been cancelled
@ -97,24 +97,22 @@ local function fileOp(op)
local newFilePath = (op == "move-rename") and newName or dir .. "/" .. newName local newFilePath = (op == "move-rename") and newName or dir .. "/" .. newName
if fileExists(newFilePath) then if fileExists(newFilePath) then
vim.notify('File with name "' .. newName .. '" already exists.', logError) vim.notify(("File with name %q already exists."):format(newFilePath), logError)
return return
end end
-- EXECUTE FILE OPERATION -- EXECUTE FILE OPERATION
cmd.update() -- save current file; needed for users with `hidden=false` cmd.update() -- save current file; needed for users with `hidden=false`
if op == "duplicate" then if op == "duplicate" then
cmd.saveas(newFilePath) if vim.loop.fs_copyfile(oldFilePath, newFilePath) then
cmd.edit(newFilePath) cmd.edit(newFilePath)
vim.notify('Duplicated "' .. oldName .. '" as "' .. newName .. '".') vim.notify(("Duplicated %q as %q."):format(oldName, newName))
end
elseif op == "rename" or op == "move-rename" then elseif op == "rename" or op == "move-rename" then
local success, errormsg = os.rename(oldFilePath, newFilePath) if vim.loop.fs_rename(oldFilePath, newFilePath) then
if success then
cmd.edit(newFilePath) cmd.edit(newFilePath)
bwipeout("#") bwipeout("#")
vim.notify('Renamed "' .. oldName .. '" to "' .. newName .. '".') vim.notify(("Renamed %q as %q."):format(oldName, newName))
else
vim.notify("Could not rename file: " .. errormsg, logError)
end end
elseif op == "new" or op == "newFromSel" then elseif op == "new" or op == "newFromSel" then
cmd.edit(newFilePath) cmd.edit(newFilePath)
@ -183,6 +181,8 @@ function M.trashFile(opts)
cmd.update { bang = true } cmd.update { bang = true }
local trash local trash
local home = os.getenv("HOME") local home = os.getenv("HOME")
local oldName = expand("%:t")
local oldFilePath = expand("%:p")
-- Default trash locations -- Default trash locations
if fn.has("linux") == 1 then if fn.has("linux") == 1 then
@ -207,19 +207,13 @@ function M.trashFile(opts)
fn.mkdir(trash, "p") fn.mkdir(trash, "p")
local currentFile = expand("%:p") if fileExists(trash .. oldName) then
local filename = expand("%:t") oldName = oldName .. "~"
if fileExists(trash .. filename) then
filename = filename .. "~"
end end
local success, err = pcall(vim.loop.fs_rename, currentFile, trash .. filename) if vim.loop.fs_rename(oldFilePath, trash .. oldName) then
if success then
bwipeout() bwipeout()
vim.notify(("%q deleted"):format(filename)) vim.notify(("%q deleted"):format(oldName))
else
vim.notify("Could not delete file: " .. err, logError)
end end
end end