From 357bc2688d231e0daf8468a63985e20c1609ceb7 Mon Sep 17 00:00:00 2001 From: Chris Grieser <73286100+chrisgrieser@users.noreply.github.com> Date: Sun, 11 Feb 2024 16:28:13 +0100 Subject: [PATCH] refactor(moveToFolderInCwd): re-organize --- lua/genghis/init.lua | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lua/genghis/init.lua b/lua/genghis/init.lua index 2395482..1332e38 100644 --- a/lua/genghis/init.lua +++ b/lua/genghis/init.lua @@ -122,37 +122,39 @@ 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 currentFolderOfFile = vim.fs.dirname(oldFilePath) .. "/" - local filename = vim.fs.basename(oldFilePath) + local curFilePath = vim.api.nvim_buf_get_name(0) + local parentOfCurFile = vim.fs.dirname(curFilePath) .. "/" + local filename = vim.fs.basename(curFilePath) local supportsImportUpdates = mv.lspSupportsRenaming() + local cwd = vim.loop.cwd() .. "/" -- determine destinations in cwd - local subfoldersOfCwd = vim.fs.find(function(name, path) + local foldersInCwd = vim.fs.find(function(name, path) local fullPath = path .. "/" .. name .. "/" local ignoreDirs = fullPath:find("/%.git/") or fullPath:find("%.app/") -- macos pseudo-folders or fullPath:find("/node_modules/") or fullPath:find("/%.venv/") - or fullPath == currentFolderOfFile + or fullPath == parentOfCurFile return not ignoreDirs end, { type = "directory", limit = math.huge }) - table.insert(subfoldersOfCwd, vim.loop.cwd() .. "/") -- sort by modification time - table.sort(subfoldersOfCwd, function(a, b) + table.sort(foldersInCwd, function(a, b) local aMtime = vim.loop.fs_stat(a).mtime.sec local bMtime = vim.loop.fs_stat(b).mtime.sec return aMtime > bMtime end) + -- insert cwd at bottom, since modification of is likely due to subfolders + if cwd ~= parentOfCurFile then table.insert(foldersInCwd, cwd) end -- prompt user and move local promptStr = "Choose Destination Folder" if supportsImportUpdates then promptStr = promptStr .. " (with updated imports)" end - vim.ui.select(subfoldersOfCwd, { + vim.ui.select(foldersInCwd, { prompt = promptStr, kind = "genghis.moveToFolderInCwd", - format_item = function(path) return path:sub(#vim.loop.cwd() + 1) end, -- only relative path + format_item = function(path) return path:sub(#cwd) end, -- only relative path }, function(destination) if not destination then return end local newFilePath = destination .. "/" .. filename @@ -163,8 +165,8 @@ function M.moveToFolderInCwd() return end - mv.sendWillRenameToLsp(oldFilePath, newFilePath) - local success = mv.moveFile(oldFilePath, newFilePath) + mv.sendWillRenameToLsp(curFilePath, newFilePath) + local success = mv.moveFile(curFilePath, newFilePath) if success then cmd.edit(newFilePath) u.bwipeout("#")