-
Notifications
You must be signed in to change notification settings - Fork 35
feat: add command :TypescriptRenameFolder #59
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 === "" || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can validate |
||
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", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to rename these variables to something like |
||
target: string, | ||
opts: Opts = {} | ||
): boolean => { | ||
debugLog(source, target); | ||
const sourceBufnr = vim.fn.bufadd(source); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if these 2 lines are still needed, since we would be loading a folder as a buffer. |
||
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) && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this check may cause issues, since we are not handling the case where the target exists but is not a directory (which should just throw an error). |
||
(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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem to currently work since the combined paths don't include a separator. We can use nvim-lspconfig's // files is an array of absolute paths
const files = vim.fs.find(
(file) => string.find(file, "[.][tj][s]x?$")[0] !== undefined,
{
path: source,
type: "file",
}
); Then for the new path, we can replace for (const file of files) {
debugLog(file);
if (renameFile(file, file.replace(source, target), opts)) {
debugLog("OK");
}
} Though I think the above would look clearer if we change variable names as I mentioned in another comment. |
||
) { | ||
debugLog("OK"); | ||
} | ||
} | ||
return true; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should default to the source file's parent directory and not the source file itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you thing of using
vim.ui.select
and prompting the user to select any of the parent directory to be renamed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about this - it would work well with Telescope / other pickers but might be kind of janky with the default handler, especially if there's a lot of choices. Let's keep it consistent with
:TypescriptRenameFile
for now and consider that as a future improvement.