Monthly Archives: December 2011

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

Fixing WPF GridSplitter

Introduction

While working on a WPF tool for work – I was creating ColumnDefinitions in a Grid dynamically (with code) and also dynamically adding GridSplitters to allow users (myself) to resize the columns. Well, it all worked OK, except for the many times that it did not. I thought I made some mistake when creating these GridSplitters, but it seemed like some other people had similar problems with GridSplitters, namely sometimes – they would not work when used with a mouse. When you drag a splitter – it would spring back to original position and stop being dragged or it would jump to some other random (?) position – maybe resizing one of the columns to 0. I tried to fix it by configuring the GridSplitters in different ways – in code behind, between Begin/EndInit blocks, in XAML – nothing helped. I simplified the repro and it seemed to be happening more often when the Grid had many GridSplitters. I checked the same layout in Silverlight and it worked without any problems (well, except for needing to reference another library since GridSplitters are not available in Silverlight using the basic Visual Studio application template). This seemed to indicate the WPF implementation is just somehow broken and it won’t work. Well then I guess I should implement the control myself – how hard would it be? – handle some mouse and keyboard events and resize an associated ColumnDefinition or RowDefinition…

The Fix – SimpleGridSplitter

Continue reading

Tagged , , , , ,