"Extract Variable" in Visual Studio without ReSharper
Asked Answered
S

2

26

I don't have ReSharper at my current gig, and I miss the "Extract Variable" refactoring. I understand that it's possible to write your own refactoring "snippet" in VS. Does anybody have one that does this?

Syringa answered 5/4, 2011 at 3:36 Comment(0)
E
4

Have a look at this post, but it does not seem to be a documented feature:

Snippets For Refactoring

Browse to your "Program Files\Microsoft Visual Studio 8\VC#\Snippets\language id\Refactoring" directory and you'll find the snippets used to perform the refactoring techniques in the Refactor menu. (...) If you're going to try modifying these snippets then (...)

Another way would be a write a macro using the EnvDTE programming model. Not easy, but should be feasible.

Edee answered 31/5, 2012 at 13:16 Comment(2)
I can't find the snippet for extracting a variable. Could you name it in your answer?Peso
The original question, as I understood it, is "how to create snippets". These files are in a directory called Refactoring, but they seem to let us add a piece of code, rather than change existing code. At the time of the question, I guess the only complete answer is to write a macro. As of today, your answer is better.Edee
P
20

In Visual Studio 2017, if you select something, on right-click you will find a menu option "Quick Actions and Refactorings", with shortcut ctrl+..
This seems to be somewhat similar to ReSharper's ALT+Enter.

This allows you to extract a variable, as in this example where the text was selected and then ctrl+. was pressed: extracting a constant

Peso answered 5/5, 2017 at 15:32 Comment(4)
Is this VS2017 Enterprise? I don't see this option in the Professional versionWards
works in my VS2017 Enterprise, although it takes ~1min to calculate it... a bit shameful, as for resharper it's 1s in the same projectBossism
@Wards Professional. But you must select the text that you want to extract a variable from.Peso
works for VS2017 Professional as well. Why Ctrl+. though and not Alt+Enter...? Alt+Enter is already used on VS2017 for fix suggestions.Mammalian
E
4

Have a look at this post, but it does not seem to be a documented feature:

Snippets For Refactoring

Browse to your "Program Files\Microsoft Visual Studio 8\VC#\Snippets\language id\Refactoring" directory and you'll find the snippets used to perform the refactoring techniques in the Refactor menu. (...) If you're going to try modifying these snippets then (...)

Another way would be a write a macro using the EnvDTE programming model. Not easy, but should be feasible.

Edee answered 31/5, 2012 at 13:16 Comment(2)
I can't find the snippet for extracting a variable. Could you name it in your answer?Peso
The original question, as I understood it, is "how to create snippets". These files are in a directory called Refactoring, but they seem to let us add a piece of code, rather than change existing code. At the time of the question, I guess the only complete answer is to write a macro. As of today, your answer is better.Edee

© 2022 - 2024 — McMap. All rights reserved.