gwerren.com

Programming notes and tools...

Introducing Multi-Caret

Mon, 04 Feb 2019 Visual StudioMulti-Caret

A little while ago I started writing a plugin for Visual Studio to add support for multi-point editing. Outside of Visual Studio I use the excellent Sublime Text which has this feature. Since I often find myself copying chunks of code out of Visual Studio into Sublime Text, multi-point editing them and copying them back, I thought it would be nice to have this functionality in Visual Studio itself.

For a long time Visual Studio has had basic support for multi-point editing but that only supports editing the same column on a series of consecutive rows (e.g. typing the third character on every selected row) but not the full multi-point editing that Sublime Text supports.

Initially I looked to see if there was an existing plugin that supports this, after all, why re-invent the wheel? I managed to find two plugins but neither truly solved my problem at the time. One was a commercial offering that was no longer supported and the second was the Multi Edit Mode plugin, which whilst useful, is fairly limited in comparison to Sublime Text. In Multi Edit Mode you can only add cursors by Ctrl+Click, not through search methods and it does not support selections.

When I started writing my own plugin I did not realise just how complicated it would be to get all the small parts working well together. The Visual Studio SDK is pretty complex and and not always well documented. The two biggest resources I used during development are two open source plugin projects, one was the previously mentioned Multi Edit Mode and the other is VsVim.

After almost two years (I have a young family so don't get a lot of time for personal projects) I had a good implementation with the features I wanted, however I use ReSharper which was causing a few minor issues (e.g. typing a semi-colon would do strange things since ReSharper overrides that behavior in order to do things like re-formatting).

Reading the ReSharper documentation suggested that I needed to write a companion ReSharper plugin that would detect when my plugin was in multi-edit mode and tell ReSharper not to take over at those times, what I could not find was what points I needed to hook into to achieve this. I reached out to the team at ReSharper who invited me to to join a Slack channel with their developers who were very helpful, they provided me with the information I needed.

The ReSharper team also pointed out that a couple of months before Visual Studio had added native support for multi-point editing! I immediately tried it out feeling both excited (new features - yay!) and disappointed (my plugin is obsolete!). The new Visual Studio support is good but is definitely missing some of the features I love from Sublime Text, most notably:

So now I have decided to throw away most of my existing plugin and look to change it to enhance the new Visual Studio multi-point editing. I suspect that I will still need to write a ReSharper plugin for the paste behavior if nothing else. When this is ready I will share the code and upload the plugin to the Visual Studio marketplace.

I have put the existing code on GitHub in the hope that if nothing else it will serve as a useful reference to some parts of the Visual Studio SDK. It may also be useful for someone wanting to target older versions of Visual Studio for some reason. Feel free to use and modify in any way you wish.

Please see my Multi-Caret project page which has links to the code on GitGub.