26

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.

1
  • Just found out that "Move class to new file" is also available in plural! Right click a file in solution explorer -> Refactor -> Move types into matching files. Really useful when refactoring legacy code files with many types in them. Sorry - no shortcut for this command also...
    – seldary
    Jun 4, 2012 at 6:59

1 Answer 1

45

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.
6
  • 2
    I think "that sucks" would be better expressed as "I might send JetBrains a feature request". I wouldn't say it's "way too many", it's just an extra quick-fix (three keystrokes). It would be nice to configure the default action for quick-fixes, but given the alternative is to do it myself by hand, I know what I'll chose. Jan 10, 2011 at 21:29
  • It's "better" in Eclipse because Java doesn't give you any choice in the matter - each class is its own file. C# lets you choose. If the classes are reasonably small and closely related (say composites or strategies), it makes sense to keep them grouped together in the same file. Nov 2, 2012 at 13:11
  • doesn't seem to do anything when I try Shift + Ctrl + R in VS 2012 via ReSharper 8.1 Nov 4, 2013 at 5:53
  • @CoffeeAddict I can't explain that; it's practically the main "refactor" command. Goto Tools->Options->Keyboard and search for "ReSharper.ReSharper_RefactorThis" and see what keystrokes it's mapped to. Maybe you have another extension that is taking over? Nov 4, 2013 at 9:23
  • If you fully qualify your class name var p = new Models.Person(); You can 1) alt-enter to create the new class - this gives would create it in the Models namespace 2) alt-enter to break it into it's own file - for me, this then opens that new file 3) then alt-enter and move it to the proper location in the project, based on namespace
    – Andy V
    Aug 19, 2015 at 17:45

Your Answer

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

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