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

Move a folder / Rename folder and update imports like TypescriptRenameFile does #53

Open
magoz opened this issue Dec 15, 2022 · 8 comments

Comments

@magoz
Copy link

magoz commented Dec 15, 2022

TypescriptRenameFile, works beautifully, updating all the imports.
Is there a way to move a folder/group of files at once and update all the imports?

If not, how do you guys deal with moving folders when refactoring?

Thanks for your awesome work!

@jose-elias-alvarez
Copy link
Owner

This isn't supported by typescript-language-server, unfortunately. We could work around this on our end, but it's pretty challenging. I tried to implement this in an earlier iteration of this plugin but was never able to get it to work perfectly.

  1. Detect if the user is trying to rename a directory
  2. Recursively scan the directory for files that typescript-language-server can handle (we can approximate this by checking their extension, but it's not perfect)
  3. Send a rename request for those files
  4. For other files, just move them into the new directory

I'm not sure if there's any interest in solving this upstream, but it might be worth opening an issue on the typescript-language-server and asking. I actually don't know how tsserver handles this case, but I suspect they're doing something similar with their file watcher, since it works perfectly in VS Code.

@magoz
Copy link
Author

magoz commented Dec 15, 2022

Thanks for your quick reply!

I've just added an issue in the typescript-language-server repo. Let's see what they say :-)

The recursive TypescriptRenameFile is a good idea. What was the main challenge for it not working reliably?

On a side note, how are you personally dealing with folder refactors? Old good search and replace for now?

@jose-elias-alvarez
Copy link
Owner

The biggest challenges were identifying which files did and didn't need to be handled and the fact a single failure could potentially leave the file tree in a weird, half-updated state.

On a side note, how are you personally dealing with folder refactors? Old good search and replace for now?

To be perfectly honest: I run !code ., perform that single operation in VS Code, and then close it. Definitely not ideal! If the typescript-language-server folks confirm that this is not something they want to handle, we can try to put something together here.

@magoz
Copy link
Author

magoz commented Dec 15, 2022

That makes sense. I haven't found VScode 100% reliable either, but it might be my local setup.
Let's see what they say. I'm happy to help out if they decline to add support for it.

@magoz
Copy link
Author

magoz commented Dec 15, 2022

We got an answer, confirming that it cannot be part of the language server:

Such functionality would have to be implemented mostly in the client so I don't really see the point of adding _typescript.applyRenameFolder. The client can just enumerate all moved files and call _typescript.applyRenameFile on them.

That's because server doesn't physically rename files (even for _typescript.applyRenameFile) as it's not allowed per spec to touch files for which editor has ownership.

I don't have much experience with lua other than crafting my nvim config, but I'm happy to help in any capacity to implement it.

@jose-elias-alvarez
Copy link
Owner

I see, thanks for following up. Their logic makes sense, so I think we can move forward with a solution here. I won't have time to work on this myself for at least a couple of weeks, but if you're interested in working on it, this is the approach I'd take:

Instead of detecting if the user is trying to rename a directory, we can instead create a separate command, e.g. :TypescriptRenameDirectory, based on the existing command. The new command can use vim.fs.find() to create two lists of files:

  1. Files that tsserver can handle (this pattern should be good enough for now)
  2. Files that tsserver can't handle

We can pass the files from 1 to the existing renameFile API, which will take care of sending the request and renaming the files. For the files from 2, we can just rename them ourselves.

Happy to provide guidance if you're interested in working on it, and if not I'll see what I can do once I have the time.

@magoz
Copy link
Author

magoz commented Dec 18, 2022

Thanks for breaking down the new approach, it's really useful. I'll follow it and see how far I can get :-)

I think I'll have some time next week to give it a try. I'll report back in a few days.

@Le-Bit
Copy link

Le-Bit commented Jan 25, 2023

Really interested by this feature, I've began hacking something together, and will open a PR soon.
I reached a somewhat working state, but nvim act weird after the renaming.

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

No branches or pull requests

3 participants