6

I have a class A in file B.cs. Now I want to rename this class to match the file name (B).

Is there a refactoring or context action in VS or ReSharper which automatically can do this renaming for me without typing in the new name matching file name?

Example

Before renaming:

File B.cs:

class A {}

After automatically renaming:

File B.cs:

class B{}

Update

The opposite (automatically rename file to match class name) is not an option because all these file renamings are implemented as DELETE+ADD. But then I would lose change history of that file in Perforce because all VS-Plug-ins for VS-Perforce-connection do not support move/add+move/delete actions for renaming.

Dmitry Osinovskiy
  • 9,999
  • 1
  • 47
  • 38
thersch
  • 1,316
  • 12
  • 19
  • 1
    Is it too hard to use Resharper renaming feature to rename the class by copy-pasting the filename in the box? IE: Do you have 50 classes to rename or just 2-3? – Pacane Jul 16 '12 at 15:49
  • I need to rename many (~100) classes after our naming scheme was changed. So I renamed the files in Perforce at first (because renaming in VS would removing the change history). And now I try to adjust the class names to new naming schema. And of course the class names are much longer than just `A` or `B`. – thersch Jul 16 '12 at 15:57
  • This isn't a direct answer, but DevExpress' CodeRush/Refactor product *does* have this as an available refactor. – Dan Puzey Jul 18 '12 at 11:40
  • [this question](http://stackoverflow.com/questions/3573024/renaming-files-in-perforce) seems to be the same. There is an accepted answer, but I can't tell if it will actually solve your problem. – comecme Jul 18 '12 at 13:30
  • @comecme Of course Perforce client P4V does not delete file history. I'm using it for that. But then I want to rename class name to match file name. – thersch Jul 18 '12 at 14:05

4 Answers4

3

In Visual Studio, you can right click on the class name inside the .cs file and rename it by choosing "Refactor --> Rename". It will give you the option to refactor it.

Alex Mendez
  • 5,120
  • 1
  • 25
  • 23
  • 2
    I would also suggest downloading "ReSharper" - with that you can just put the cursor on it and Alt+Enter+Enter. – SimpleVar Jul 16 '12 at 15:11
  • Thanks. But I do not want to type in the name. VS/ReSharper sould automatically get the file name. I updated my question by that point. – thersch Jul 16 '12 at 15:12
  • @YoryeNathan I don't mean *Rename file to matching type name* context action. I mean the opposite like **"Rename type to matching file name"**. – thersch Jul 16 '12 at 15:15
  • @thersch I'm not aware of a feature for that, and I also can't really think of a good-enough need for that. – SimpleVar Jul 16 '12 at 17:52
  • @YoryeNathan If you would need to rename ~100 classes and want to keep change history you would also need such a feature. :-) – thersch Jul 17 '12 at 08:54
  • @thersch But the code goes by class name, not by file name - that means nothing to the infrastructure, really. I can't see why the file name and class name should be different in the first place. – SimpleVar Jul 17 '12 at 12:38
  • @YoryeNathan Of course the application also compiles and runs if file names are different to class names. But the readability of code is harder and it is confusing. (We use Solution Explorer for manual navigations.) – thersch Jul 17 '12 at 16:25
  • @thersch I meant that the class name itself should be a meaningful one - probably the one you want to rename it to just now - in the first place. Having the file name meaningful as well is a good bonus. – SimpleVar Jul 17 '12 at 19:26
  • @YoryeNathan You are right if you only use Ctrl+Click/F12 and ReSharper's Ctrl+T navigation. But we also use Solution Explorer for navigation to classes frequently. And Solution Explorer has **file names**. So for our team it's a must to rename files too. – thersch Jul 18 '12 at 07:28
  • 1
    @thersch It's not a question about whether files should be renamed or not - they should. It's a matter of which name comes first - the file after the class or the class after the file, and it will probably come from the class, not the file, so... I don't see how you got yourself into this trouble. When the class name is good, you just Alt+Enter+Enter and the file name matches it. – SimpleVar Jul 18 '12 at 07:53
  • @YoryeNathan Ok I could change class name **before** file name and use Alt+Enter+Enter to automatically change the file name. I could also check option *Synchronize file names accordingly to changes* in renaming dialog (Ctrl+R,Ctrl+R). **But then I would lose change history in Perforce** because VS does a delete/add for a rename. Because of that lack the order of renaming class and file does not matter. – thersch Jul 18 '12 at 08:04
2

We recently did a large refactor of several files, changing them from "Controllers" to "Services". After plodding through a few of them with ReSharper, I decided I should rename "BaseController" to "BaseService". Ding! ReSharper offered to rename all subclasses and replace "Controller" with "Service".

Ishmael
  • 30,970
  • 4
  • 37
  • 49
1

With Resharper you can use Ctrl+R,O

enter image description here

P.S. I use Resharper 6.1

Andrei Schneider
  • 3,618
  • 1
  • 33
  • 41
  • It was a typo. It should have been Ctrl+R/Ctrl+O – Andrei Schneider Jul 16 '12 at 15:14
  • Isn't it Ctrl+R, O then? Or is it two different sequences of keys presses? –  Jul 16 '12 at 15:17
  • I don't mean "Move to another file". I mean an action like "Rename type to matching file name". – thersch Jul 16 '12 at 15:19
  • 1
    Just delete the old file then..? – Pacane Jul 16 '12 at 15:20
  • 1
    To thersch, as I know Resharper doesn't have such a feature out of the box. But it is very extensible, so it is not a problem to make a plugin with such functionality. – Andrei Schneider Jul 16 '12 at 15:22
  • @Pacane Deleting file is not an option. I have improved my (possible poor) question by adding an example with *before* and *after* states. – thersch Jul 16 '12 at 15:29
  • 1
    @AndreiSchneider Thanks for the hint to writing a plug-in. But I think it is to much time for me to learn ReSharper's plug-in interface. I tried Live Template because there is a macro *Current file name without extension*. But this would change just the one place of class name and not update all other references to this class name. – thersch Jul 16 '12 at 15:47
0

Is there a refactoring or context action in VS or ReSharper which automatically can do this renaming for me without typing in the new name matching file name?

With ReShaper 8.1 it is possible. Just put a cursor on the class name, hit alt + Enter to bring up the options then Select 'Rename file to match type name' and you are good.

enter image description here

iAM
  • 1,365
  • 15
  • 25
  • 1
    This is the wrong direction. I searched after a "Rename type name to match file name". – thersch Dec 05 '16 at 07:59
  • It's right direction if you are using resharper V 8.1. As you can see in the gif image above. If is not working for you, check which version of resharper you are using. – iAM Dec 05 '16 at 08:19
  • 1
    I wanted to change the class name, not the file name. Your gif image keeps the class name with "A" and changes the file name from "B" to "A". – thersch Dec 05 '16 at 09:01
  • 1
    To change class name, use resharper rename feature. Put cursor on a what you want to rename (in your case it's a class name) then Ctrl + R + R to rename the class name. This will rename class and all the places where it has been used. Note: this can be done from anywhere where the class name is used, it's not must to do it from class file. – iAM Dec 05 '16 at 09:49
  • @iAM No it's not the right direction. You didn't even bother to actually read the question title: **Automatically rename CLASS to match FILE name**. And that is the far more frequent usecase. You have a file with a class in it, you want to change the file name and consequently automatically change the class name and all its references. – adamency Nov 03 '22 at 13:41