vim-esearch icon indicating copy to clipboard operation
vim-esearch copied to clipboard

Renaming files?

Open kantord opened this issue 4 years ago • 4 comments

Is it possible to use this plugin to rename files?

kantord avatar Dec 26 '20 14:12 kantord

There's already a poc implementation in implement-rename-feature branch, but I discovered, that cyclic renames can cause problems. Some file manager plugins with mass renames also have this problem and I hesitate to make this plugin another one that could lead to data loss.

E.g. when you swap file names a -> b and b -> c, the plugin needs to use a temporary file. The same is for 3 files: b -> c, a -> b, c -> a (contents of file c needs to be placed into a tmp file and the written into a). It's not a huge deal to handle parallel edges and cycles in the graph of renames, but I'm still not sure if it's the only potential problem.

Please, let me know if you have any ideas or experience in implementing similar features.

eugen0329 avatar Dec 26 '20 19:12 eugen0329

By the way it'd be nice to use this feature for refactoring using abolish :Subvert command to rename classes along with their filenames using one command.

eugen0329 avatar Dec 26 '20 19:12 eugen0329

Please, let me know if you have any ideas or experience in implementing similar features.

Don't much have any experience with such operations, but I would imagine a safe way of doing this would be separating it into multiple stages.

  1. Write edits to files, so that renaming remains the only job left to do
  2. Observe the rename operations/graph to discover the ultimate list of deleted files, and the list of created files and where their content ultimately comes from
  3. Write the new files
  4. Delete the files that are to be deleted

Technically, there's no renaming this way, rather deletion and new files. So for large files it might be problematic, but that's probably not a concern. But I think that way there's no need for temporary files, and any such cycle should be supported.

Either way, if already have a way to analyze the graph, I would probably first release without support for cycles. I would simply detect cycles and if there is any cycle, the plugin would refuse to rename files.

kantord avatar Dec 27 '20 09:12 kantord

Thanks for your ideas.

list of deleted files

I hesitate to add file deletion feature since:

  • It's less safe in terms of accidental data loss.
  • It's harder to implement undo for it. All operations at the moment are reversible using just u:write<cr>.
  • It's more common to use convenient combination dG:write<cr> to remove all matched lines (with outdated comments, debugger entries etc.) rather than deleting files with matches.

If you need it, you can use the command below. Note that each filename is rendered so that it can be copied and safely pasted into the command line.

au User esearch_win_config command! -buffer -range Rm <line1>,<line2>g/^\S\%>1l/exe '!rm' getline('.')

for large files it might be problematic, but that's probably not a concern

It can be a concern in case of log files with multiple gigabytes size (unless rotated, but even then they are around hundreds of megabytes) or files with test fixtures where media is dumped as base64.

I didn't spend too much time on the task, so I'll investigate already existing mass rename utils and plugins on top of your suggestions.

if already have a way to analyze the graph, I would probably first release without support for cycles

It can be done even without analyzing the graph by checking if there's an intersection between original and changed filenames, so an exception can be thrown. But you're right that it can already be released with cycled renames feature skipped.

eugen0329 avatar Jan 20 '21 17:01 eugen0329