Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper Shortcuts for Create Class and Move Class To New File

Tags:

.net

resharper

What are the Resharper 4 shortcuts to

  1. Create a class from usage? e.g. I type "var p = new Person();", and I want to now create the person class.

  2. Move this class to its own file? When the Person class exists in the same file next to my Order class, what is the shortcut to move it.

I can't seem to find these shortcuts on the cheatsheet or the Internet.

like image 808
Lance Fisher Avatar asked Dec 24 '08 20:12

Lance Fisher


People also ask

What is the keyboard shortcut to set ReSharper?

Choose Tools | Options from the menu and then go to Environment | Keyboard page. Use the shortcut tables to find the alias of the command, to which you are going to assign a new shortcut. If some command does not have a default shortcut, you can find its alias under the title of the corresponding help page.

What shortcut key is used to rename method in ReSharper?

Press Ctrl+Shift+R and then choose Rename. Right-click and choose Refactor | Rename from the context menu. Choose ReSharper | Refactor | Rename… from the main menu.

How do I extract a class in C++?

Press Ctrl+Shift+R and then choose Extract Class. Right-click and choose Refactor | Extract Class from the context menu.


1 Answers

Type the line out:

var p = new Person(); 

Person will be highlighted in red as an error by ReSharper. Put the caret on it and press ALT+ENTER to invoke the quick-fix context menu. Select Create class 'Person'.

The cursor will then be on the new class' name, so press ALT+ENTER again to invoke the context-sensitive quick-fix menu again and select Move to another file to match type name.

That's just two actions - really quick and easy. After a while, it (like most R# commands) becomes muscle memory. Like driving, walking or chewing gum.

FOR BONUS POINTS
The above is all you need to do what you wanted, but you can take it a step or two further:

  1. If you'd rather the class was moved to a different namespace, you can press SHIFT+CTRL+R and select Modify Namespace....

  2. If you'd rather the class was moved to a different project entirely, you can press SHIFT+CTRL+R and select Move to Folder....

The great thing is - ReSharper will make all necessary changes to namespaces to make sure things still compile. With one gotcha - only if the project you move the classes to is referenced by the one you move them from. You have two choices

  1. Go ahead with the refactoring and use ReSharper quick-fixes to both add the reference and import namespaces in one go (if it's a new class, I'd do this because it'll be the only usage).
  2. Add the reference manually before moving them and it'll do it all for you.
like image 188
Neil Barnwell Avatar answered Oct 04 '22 10:10

Neil Barnwell