feat: `moveToFolderInCwd` command (offers folder selection)

remotes/origin/HEAD
Chris Grieser 2024-02-03 14:19:01 +01:00
parent 46a851c92b
commit ae0808bed6
3 changed files with 67 additions and 4 deletions

View File

@ -14,7 +14,9 @@ Lightweight and quick file operations without being a full-blown file manager.
* [File Operation Command](#file-operation-command)
* [File Utility Commands](#file-utility-commands)
* [Disable Ex-Commands](#disable-ex-commands)
- [Autocompletion of directories](#autocompletion-of-directories)
- [Cookbook](#cookbook)
* [Use Telescope for `.moveToFolderInCwd`](#use-telescope-for-movetofolderincwd)
* [Autocompletion of directories for `.moveAndRenameFile`](#autocompletion-of-directories-for-moveandrenamefile)
- [How is this different from `vim.eunuch`?](#how-is-this-different-from-vimeunuch)
- [Why that name](#why-that-name)
- [About me](#about-me)
@ -55,6 +57,7 @@ keymap("n", "<leader>yn", genghis.copyFilename)
keymap("n", "<leader>cx", genghis.chmodx)
keymap("n", "<leader>rf", genghis.renameFile)
keymap("n", "<leader>mf", genghis.moveAndRenameFile)
keymap("n", "<leader>mc", genghis.moveToFolderInCwd)
keymap("n", "<leader>nf", genghis.createNewFile)
keymap("n", "<leader>yf", genghis.duplicateFile)
keymap("n", "<leader>df", genghis.trashFile)
@ -72,7 +75,9 @@ Line Mode command; the selection is moved linewise.)
- `.renameFile` or `:Rename`: Rename the current file.
- `.moveAndRenameFile` or `:Move`: Move and Rename the current file. Keeps the
old name if the new path ends with `/`. Works like the UNIX `mv` command. Can be
used with [autocompletion of directories](#autocompletion-of-directories).
- `.moveToFolderInCwd` or `:MoveToFolderInCwd`: Move the current file to an
existing folder in the current working directory. [Can use telescope for the
selection of the destination.](#use-telescope-for-movetofolderincwd)
- `.trashFile{trashCmd = "your_cli"}` or `:Trash`: Move the current file
to the trash location.
* Defaults to `gio trash` on *Linux*, `trash` on *Mac* and *Windows*.
@ -143,7 +148,16 @@ let g:genghis_disable_commands = v:true
## Autocompletion of directories
You can get autocompletion for directories by using `dressing.nvim`, `nvim-cmp`,
and vim's `omnifunc`:
### Use Telescope for `.moveToFolderInCwd`
```lua
require("dressing").setup {
select = {
backend = { "telescope" },
},
}
```
```lua
-- packer

View File

@ -125,6 +125,54 @@ function M.duplicateFile() fileOp("duplicate") end
function M.createNewFile() fileOp("new") end
function M.moveSelectionToNewFile() fileOp("newFromSel") end
function M.moveToFolderInCwd()
local oldFilePath = vim.api.nvim_buf_get_name(0)
local filename = vim.fs.basename(oldFilePath)
local cwd = vim.loop.cwd()
-- determine destinations in cwd
local subfoldersOfCwdAbsPath = vim.fs.find(function(name, path)
-- TODO improve ignoring of certain folders
return not (
(path:find("/%.git/") or path:find("/%.git$") or name == ".git")
or (path:find("%.app/") or path:find("%.app$")) -- macos pseudo-apps
or name == "node_modules"
)
end, { type = "directory", limit = math.huge })
local subfoldersOfCwdRelPath = vim.tbl_map(
function(path) return path:sub(#cwd + 2) end,
subfoldersOfCwdAbsPath
)
-- prompt user and move
local promptStr = "Destination Folder "
if mv.lspSupportsRenaming() then promptStr = promptStr .. "& Update Imports" end
vim.ui.select(subfoldersOfCwdRelPath, {
prompt = promptStr,
kind = "genghis.moveToFolderInCwd",
}, function(destination)
if not destination then return end
local newFilePath = cwd .. "/" .. destination .. "/" .. filename
-- GUARD conflict
if u.fileExists(newFilePath) then
u.notify(
("File with name %q already exists at %q."):format(filename, destination),
"error"
)
return
end
mv.sendWillRenameToLsp(oldFilePath, newFilePath)
local success = mv.moveFile(oldFilePath, newFilePath)
if success then
cmd.edit(newFilePath)
u.bwipeout("#")
u.notify(("Moved to %q."):format(destination))
end
end)
end
--------------------------------------------------------------------------------
---copying file information
@ -176,7 +224,7 @@ function M.trashFile(opts)
if fn.has("linux") == 1 then system = "linux" end
if fn.has("win32") == 1 then system = "windows" end
local trashCmd = opts.trashCmd or defaultTrashCmds[system]
-- Use a trash command
local trashArgs = vim.split(trashCmd, " ")
local oldFilePath = vim.api.nvim_buf_get_name(0)

View File

@ -8,6 +8,7 @@ command("Duplicate", function() genghis.duplicateFile() end, {})
command("Rename", function() genghis.renameFile() end, {})
command("Trash", function() genghis.trashFile() end, {})
command("Move", function() genghis.moveAndRenameFile() end, {})
command("moveToFolderInCwd", function() genghis.moveToFolderInCwd() end, {})
command("CopyFilename", function() genghis.copyFilename() end, {})
command("CopyFilepath", function() genghis.copyFilepath() end, {})
command("CopyRelativePath", function() genghis.copyRelativePath() end, {})