Skip to content
This repository was archived by the owner on Aug 12, 2023. It is now read-only.

feat: add command :TypescriptRenameFolder #59

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Le-Bit
Copy link

@Le-Bit Le-Bit commented Jan 25, 2023

No description provided.

@Le-Bit Le-Bit force-pushed the feat/rename-folder branch from a3b6a47 to df41f30 Compare January 25, 2023 13:57
@Le-Bit Le-Bit force-pushed the feat/rename-folder branch from df41f30 to 35a4a67 Compare January 25, 2023 13:59
@jose-elias-alvarez
Copy link
Owner

Thanks! At a glance this approach looks alright - I will test it out and give a more thorough review this weekend.

Copy link
Owner

@jose-elias-alvarez jose-elias-alvarez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the delay in reviewing this. The overall logic looks okay but there's one key issue with the current implementation as well as several other areas for improvement.

(opts) => {
const sourceFile = vim.api.nvim_buf_get_name(bufnr);
vim.ui.input(
{ prompt: "Old path: ", default: sourceFile },

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.

Copy link
Author

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.

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.

{ prompt: "Old path: ", default: sourceFile },
(sourceInput) => {
if (
sourceInput === "" ||

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can validate sourceInput and targetInput after prompting for both, since we should validate that they are not the same.

opts: Opts = {}
): boolean => {
debugLog(source, target);
const sourceBufnr = vim.fn.bufadd(source);

Choose a reason for hiding this comment

The 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.


if (
util.path.exists(target) &&
util.path.is_dir(target) &&

Choose a reason for hiding this comment

The 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).

debugLog(file);
if (
file.endsWith(".ts") &&
renameFile(source + file, target + file, opts)

Choose a reason for hiding this comment

The 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 utils.path.join, or we can use vim.fs.find():

  // 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 source with target in the current path:

  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.

}

export const renameFolder = (
source: string,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to rename these variables to something like sourceDir and targetDir to avoid confusion.

@h16rkim
Copy link

h16rkim commented Jul 11, 2023

I really need this feature :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants