Renaming Files with Variables - Visual Studio Code







We are all spoiled by the IDE. I want more pampering! Introducing the free Visual Studio Code extension for renaming files and multi-replace variables.



There are many extensions and built-in features for refactoring. The main feature is renaming (variable, class, method, file, etc.). We are all well acquainted with such opportunities, they raise our mood every day!



We love the rules, adhere to style guides. We name the files based on the contents. In the app-component.ts file, in my opinion, the AppComponent class should lie. And renaming the AppComponent class to RootComponent , the next step is to change the file name to root-component.ts ... And if it is, for example, an Angular component, then you have to change the name of three more files, CSS selectors, instance names! Faced with this? The simplest renaming operation often requires incredible efforts from us.



Have you seen such a code?



// src/models/animal.ts class Animal { run(): void; jump(): void; catchMouse(): void; meow(): void; }
      
      





Agree that over time, our classes can evolve? It's not about OOP or SOLID (there are no other animals in the project), it's a great class - but the name does not reflect reality. Methods of this class shout that this is a cat! And in fact, we must give the class a chance at a good name. But what about all instance variables, hundreds of const animal = new Cat (); ? And if we already have whole modules with the keyword animal .







In general, the topic of proper naming is always relevant. But there is still not much automated routine.



To have fun, I wrote a small cli on node.js for the simplest replacement of lines in files, but also changing file names ( @ justerest / multi-replace ). And I was very surprised by the usability of this crutch! I am no longer afraid to make a mistake in the name of the component or module, because with one command I can rename the whole collection of Angular components.







After a while, I also discovered that this tool can serve as a great alternative to schematics. Just copy the most similar file (component) and rename it! It inspired me to write a multi-replace extension for my favorite Visual Studio Code editor.



multi-replace runs on primitive String.ptototype.replace () without any analysis of code or file types. The implementation is very simple - minimum code, maximum benefit. It is possible to rename files and text inside a folder, together with a folder, copy changed files to a new folder.



I want to promote and assert myself to hear criticism, suggestions or alternative solutions for renaming from the Habrovsk citizens.



Thank!



All Articles