diff --git a/lua/typescript/commands.lua b/lua/typescript/commands.lua index 5d4b2ec..df19a90 100644 --- a/lua/typescript/commands.lua +++ b/lua/typescript/commands.lua @@ -6,6 +6,8 @@ local ____go_2Dto_2Dsource_2Ddefinition = require("typescript.go-to-source-defin local goToSourceDefinition = ____go_2Dto_2Dsource_2Ddefinition.goToSourceDefinition local ____rename_2Dfile = require("typescript.rename-file") local renameFile = ____rename_2Dfile.renameFile +local ____rename_2Dfolder = require("typescript.rename-folder") +local renameFolder = ____rename_2Dfolder.renameFolder local ____source_2Dactions = require("typescript.source-actions") local addMissingImports = ____source_2Dactions.addMissingImports local fixAll = ____source_2Dactions.fixAll @@ -29,6 +31,31 @@ ____exports.setupCommands = function(bufnr) end, {bang = true} ) + vim.api.nvim_buf_create_user_command( + bufnr, + "TypescriptRenameFolder", + function(opts) + local sourceFile = vim.api.nvim_buf_get_name(bufnr) + vim.ui.input( + {prompt = "Old path: ", default = sourceFile}, + function(sourceInput) + if sourceInput == "" or sourceInput == sourceFile or sourceInput == nil then + return + end + vim.ui.input( + {prompt = "New path: ", default = sourceFile}, + function(targetInput) + if targetInput == "" or targetInput == sourceFile or targetInput == nil then + return + end + renameFolder(sourceInput, targetInput, {force = opts.bang}) + end + ) + end + ) + end, + {bang = true} + ) vim.api.nvim_buf_create_user_command( bufnr, "TypescriptGoToSourceDefinition", diff --git a/lua/typescript/rename-folder.lua b/lua/typescript/rename-folder.lua new file mode 100644 index 0000000..b7d2ea0 --- /dev/null +++ b/lua/typescript/rename-folder.lua @@ -0,0 +1,49 @@ +--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]] +-- Lua Library inline imports +local function __TS__StringEndsWith(self, searchString, endPosition) + if endPosition == nil or endPosition > #self then + endPosition = #self + end + return string.sub(self, endPosition - #searchString + 1, endPosition) == searchString +end + +-- End of Lua Library inline imports +local ____exports = {} +local ____lspconfig = require("lspconfig") +local util = ____lspconfig.util +local ____rename_2Dfile = require("typescript.rename-file") +local renameFile = ____rename_2Dfile.renameFile +local ____utils = require("typescript.utils") +local debugLog = ____utils.debugLog +____exports.renameFolder = function(source, target, opts) + if opts == nil then + opts = {} + end + debugLog(source, target) + local sourceBufnr = vim.fn.bufadd(source) + vim.fn.bufload(sourceBufnr) + if not util.path.is_dir(source) then + debugLog("source is not a directory") + return false + end + if util.path.exists(target) and util.path.is_dir(target) and (opts.force == nil or opts.force == false) then + local status = vim.fn.confirm("Folder exists! Overwrite?", "&Yes\n&No") + if status ~= 1 then + debugLog("user declined to overrwrite file; aborting") + return false + end + end + vim.fn.mkdir( + vim.fn.fnamemodify(target, ":p:h"), + "p" + ) + local files = vim.fn.readdir(source) + for ____, file in ipairs(files) do + debugLog(file) + if __TS__StringEndsWith(file, ".ts") and renameFile(source .. file, target .. file, opts) then + debugLog("OK") + end + end + return true +end +return ____exports diff --git a/src/commands.ts b/src/commands.ts index 3420d38..0786e35 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -1,6 +1,7 @@ import { config } from "@ts/config"; import { goToSourceDefinition } from "@ts/go-to-source-definition"; import { renameFile } from "@ts/rename-file"; +import { renameFolder } from "@ts/rename-folder"; import { addMissingImports, fixAll, @@ -26,6 +27,42 @@ export const setupCommands = (bufnr: number): void => { { bang: true } ); + vim.api.nvim_buf_create_user_command( + bufnr, + "TypescriptRenameFolder", + (opts) => { + const sourceFile = vim.api.nvim_buf_get_name(bufnr); + vim.ui.input( + { prompt: "Old path: ", default: sourceFile }, + (sourceInput) => { + if ( + sourceInput === "" || + sourceInput === sourceFile || + sourceInput === undefined + ) { + return; + } + vim.ui.input( + { prompt: "New path: ", default: sourceFile }, + (targetInput) => { + if ( + targetInput === "" || + targetInput === sourceFile || + targetInput === undefined + ) { + return; + } + renameFolder(sourceInput, targetInput, { + force: opts.bang, + }); + } + ); + } + ); + }, + { bang: true } + ); + vim.api.nvim_buf_create_user_command( bufnr, "TypescriptGoToSourceDefinition", diff --git a/src/rename-folder.ts b/src/rename-folder.ts new file mode 100644 index 0000000..73c5a09 --- /dev/null +++ b/src/rename-folder.ts @@ -0,0 +1,46 @@ +import { util } from "lspconfig"; +import { renameFile } from "@ts/rename-file"; +import { debugLog } from "@ts/utils"; + +interface Opts { + force?: boolean; +} + +export const renameFolder = ( + source: string, + target: string, + opts: Opts = {} +): boolean => { + debugLog(source, target); + const sourceBufnr = vim.fn.bufadd(source); + vim.fn.bufload(sourceBufnr); + + if (!util.path.is_dir(source)) { + debugLog("source is not a directory"); + return false; + } + + if ( + util.path.exists(target) && + util.path.is_dir(target) && + (opts.force === undefined || opts.force === false) + ) { + const status = vim.fn.confirm("Folder exists! Overwrite?", "&Yes\n&No"); + if (status !== 1) { + debugLog("user declined to overrwrite file; aborting"); + return false; + } + } + vim.fn.mkdir(vim.fn.fnamemodify(target, ":p:h"), "p"); + const files = vim.fn.readdir(source); + for (const file of files) { + debugLog(file); + if ( + file.endsWith(".ts") && + renameFile(source + file, target + file, opts) + ) { + debugLog("OK"); + } + } + return true; +}; diff --git a/src/types/lspconfig.d.ts b/src/types/lspconfig.d.ts index 5eb6600..60e2f16 100644 --- a/src/types/lspconfig.d.ts +++ b/src/types/lspconfig.d.ts @@ -6,6 +6,7 @@ declare module "lspconfig" { namespace util { namespace path { const exists: (path: string) => boolean; + const is_dir: (path: string) => boolean; } } } diff --git a/src/types/nvim.d.ts b/src/types/nvim.d.ts index c4466a4..b016b09 100644 --- a/src/types/nvim.d.ts +++ b/src/types/nvim.d.ts @@ -168,6 +168,7 @@ declare namespace vim { const bufload: (bufnr: number) => void; const has: (feature: string) => 0 | 1; const mkdir: (name: string, path?: string, prot?: number) => void; + const readdir: (name: string) => string[]; const fnamemodify: (fname: string, mods: string) => string; }