Skip to content

Commit

Permalink
refactor: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Dec 30, 2023
1 parent 0b71c9c commit d2f85f3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lua/genghis/file-movement.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ local u = require("genghis.utils")
---stolen from https://github.com/LazyVim/LazyVim/blob/fecc5faca25c209ed62e3658dd63731e26c0c643/lua/lazyvim/util/init.lua#L304
---@param fromName string
---@param toName string
function M.onRename(fromName, toName)
function M.sendWillRenameToLsp(fromName, toName)
local clients = vim.lsp.get_active_clients { bufnr = 0 }
for _, client in ipairs(clients) do
if client:supports_method("workspace/willRenameFiles") then
Expand Down
22 changes: 10 additions & 12 deletions lua/genghis/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local M = {}

local expand = vim.fn.expand
local fn = vim.fn
local cmd = vim.cmd

Expand All @@ -16,7 +15,7 @@ local function fileOp(op)
local oldName = vim.fs.basename(oldFilePath)
local dir = vim.fs.dirname(oldFilePath) -- same directory, *not* pwd
local oldNameNoExt = oldName:gsub("%.%w+$", "")
local oldExt = expand("%:e")
local oldExt = fn.expand("%:e")
if oldExt ~= "" then oldExt = "." .. oldExt end

local prevReg
Expand All @@ -42,20 +41,18 @@ local function fileOp(op)
prefill = ""
end

-- INFO completion = "dir" allows for completion via cmp-omni
vim.ui.input({
prompt = promptStr,
default = prefill,
completion = "dir",
completion = "dir", -- allows for completion via cmp-omni
}, function(newName)
cmd.redraw() -- Clear message area from ui.input prompt

-- VALIDATION OF FILENAME
if not newName then return end -- input has been canceled

if newName:find("/$") then
newName = newName .. oldName -- use the new directory with the old name
end
-- if only directory is entered, move file to that location
if op == "move-rename" and newName:find("/$") then newName = newName .. oldName end

local invalidName = newName:find("^%s+$")
or newName:find("[\\:]")
Expand Down Expand Up @@ -96,15 +93,15 @@ local function fileOp(op)
end

-- EXECUTE FILE OPERATION
cmd.update() -- save current file; needed for users with `vim.opt.hidden=false`
cmd.update()
if op == "duplicate" then
local success = vim.loop.fs_copyfile(oldFilePath, newFilePath)
if success then
cmd.edit(newFilePath)
u.notify(("Duplicated %q as %q."):format(oldName, newName))
end
elseif op == "rename" or op == "move-rename" then
mv.onRename(oldFilePath, newFilePath)
mv.sendWillRenameToLsp(oldFilePath, newFilePath)
local success = mv.moveFile(oldFilePath, newFilePath)
if success then
cmd.edit(newFilePath)
Expand Down Expand Up @@ -139,11 +136,12 @@ local function copyOp(expandOperation)
or (#clipboardOpt > 0 and clipboardOpt[1]:find("unnamed"))
if useSystemClipb then reg = "+" end

local toCopy = expand(expandOperation)
local toCopy = fn.expand(expandOperation)
fn.setreg(reg, toCopy)
vim.notify(toCopy, vim.log.levels.INFO, { title = "Copied" })
end

-- DOCS for the available modifiers: https://neovim.io/doc/user/builtin.html#expand()
function M.copyFilepath() copyOp("%:p") end
function M.copyFilename() copyOp("%:t") end
function M.copyRelativePath() copyOp("%:~:.") end
Expand All @@ -165,20 +163,20 @@ end
---@param opts? {trashLocation: string}
function M.trashFile(opts)
cmd.update { bang = true }
local trash
local home = os.getenv("HOME")
local oldFilePath = vim.api.nvim_buf_get_name(0)
local oldName = vim.fs.basename(oldFilePath)

-- Default trash locations
local trash
if fn.has("linux") == 1 then
local xdg_data = os.getenv("XDG_DATA_HOME")
trash = xdg_data and xdg_data .. "/Trash/" or home .. "/.local/share/Trash/"
elseif fn.has("macunix") == 1 then
-- INFO macOS moves files to the icloud trash, if they are deleted from
-- icloud folder, otherwise they go the user trash folder
local iCloudPath = home .. "/Library/Mobile Documents/com~apple~CloudDocs"
local isInICloud = fn.expand("%:p:h"):sub(1, #iCloudPath) == iCloudPath
local isInICloud = oldFilePath:sub(1, #iCloudPath) == iCloudPath
trash = isInICloud and iCloudPath .. "/.Trash/" or home .. "/.Trash/"
else
-- TODO better support for windows
Expand Down

0 comments on commit d2f85f3

Please sign in to comment.