Using Visual Studio Macros and Block Selection/Edit with ReSharper

I love using Macros in Visual Studio. If you need to process multiple lines quickly – like modifying a list of words into a list of C# properties with backing fields etc. – you kind of can do these things with regular expressions search/replace, but it is a bit difficult to come up with multiline match and replace expressions while escaping lots of special characters, marking groups etc. Macros work great because you just record a macro in which you process one line by manually typing the changes, make sure to go to the next line and then replaying the macro multiple times gets all the lines processed the same way and it just looks magical when it works. Just as useful in more simple cases is block selection (Alt+Drag) – when you just need to modify a column of one-line statements – eg. when you have a column of words and you want to modify each line to add each of these words to a collection.
Well, I also love ReSharper for making my code better and teaching me to code better every day, but it just does not work with macros or block select edit. One thing is – it somehow breaks the Ctrl+Shift+R/Ctrl+Shift+P shortcuts to record/play a temporary macro – and it breaks it beyond fix, but it is OK, since you can start/stop recording using the menus/toolbar. What is worse though – it interrupts your recording by doing some automated coding assistance. The same way – it often interrupts editing multiple lines of text when using block selection. A fix is to disable ReSharper, but going to Tools/Options/ReSharper/General/Suspend-Resume every time you want to record or play a macro is really annoying. It turns out there is a command you can execute to switch it, but it does not show up when you customize your toolbars (it was reported as an issue in ReSharper 5.1, but is still not fixed in 6.0 – I guess it was low priority or just overlooked). Well, you can go to you Macros IDE (Alt+F11) and add the below macro, which you can then add to a toolbar (toolbar-Right Click/Customize/Commands/Toolbar/Add Command/Macros/Macros.MyMacros.Shortcuts.Resharper_ToggleSuspended):

Public Module Shortcuts
    Sub Resharper_ToggleSuspended()
        DTE.ExecuteCommand(“Resharper_ToggleSuspended”)
    End Sub
End Module

Now this is enough for me for now, but an ideal solution would be to have a method that first disables ReSharper and then starts recording a macro, so you could have that happen automatically when you start recording a macro…
The next step is to go to Tools/Options/Keyboard, search for Tools.RecordTemporaryMacro and assign some key to it – e.g. Ctrl+Shift+Alt+R. Ctrl+Shift+P should still hopefully work to play back the macro.
Advertisements
Tagged , , , ,

6 thoughts on “Using Visual Studio Macros and Block Selection/Edit with ReSharper

  1. Anonymous says:

    I have the exactly same problem as you describe in your blog post. But your instructions don’t seem to work in my case. Any ideas why that could be?

    I have set Tools.RecordTemporaryMacro once again back to Ctrl+Shift+R (Global), but nothing happens when I press the shortcut. ReSharper is suspended.

    • Anonymous says:

      Me again…

      It is working now that I have changed the shortcut to something different like Ctrl+Shift+Alt+R. And it is still working after resuming ReSharper.

      • xyzzer says:

        Yes, what I meant to say was Ctrl+Shift+R gets owned by ReSharper and you can’t reassign it, so you need to use a different combination for recording macros. You need to disable ReSharper when you run a macro because otherwise in many cases it will interrupt the macro by applying some auto-formatting while the macro is recorded/played.

  2. Anonymous says:

    Thank you very much – way cool.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: