21

Is it possible to refactor the name of the files in a project and automatically update all the imports, without caring about breaking something each time a file name is changed?

In the same manner as in the JetBrains' products. You change the file name and everything is automatically updated.

4

4 Answers 4

8

Actually you can. Natively on Visual Studio Code (>= v1.28) with import path renaming:

Renaming JavaScript import path in VS Code

Or with some extension like Move TS.

5
  • 1
    I saw that already. IMO not as good as in JetBrains, but close enough. The question was made before v1.28 though, when that was not yet possible.
    – Oleg
    Oct 10, 2018 at 10:20
  • Yeah, I thought about editing the other answer, but it would be too much change, so I wrote a new one. New users that arrive here will not notice the difference, but for <= 1.28 you could still use the TS move extension I put in the end of the answer. I also must agree that JetBrains IDEs have very nice features.
    – Machado
    Oct 10, 2018 at 12:37
  • Thank you very much. I noticed I forgot to mention; to use refactoring just click on the file path and press F2.
    – Machado
    Aug 28, 2020 at 17:04
  • This does not work for python, as far as I tested :/
    – tbrodbeck
    Dec 14, 2020 at 13:31
  • For other not JS-based languages you gona need a language processor or language server extension, like OmniSharp for C# ou IntelliPhense for PHP. There's one for Python but I don't remember it's name, just install it and it should work.
    – Machado
    Dec 15, 2020 at 14:23
3

Sorry, but the answer is just simple: No, you can't.

At least not currently. It's up to the authors of the language modes to implement such things. There is support for the occasional variable refactoring here and there… And there are some additional extensions in the JavaScript space that support method extraction and stuff, but none of them handles filename refactoring.

0

This should be easier with VSCode 1.53 (Jan. 2021):

Improved Refactor Participants UI

Extension can participant when use create, move, rename, or delete files.
This is useful to automatically run refactorings, e.g. when renaming a Java file you also need to rename its public class.

Refactor Participant Preview

There is now unified UI for these participants which allows you to accept, skip, and preview additional changes an extension is making.

0

Note there is now a great extension that basically gives us the JetBrains functionality.

Right-click file, rename it. The sibling files, the class name, the selector, the module that imports it, and the pages that use instances of it ALL update in a second.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.