Tag Archives: regular expressions

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.
Tagged , , , ,