Visual Studio customization with lightweight extensions and Visual Commander

Visual Studio is a very capable and mature IDE, plus there are 5,942 items in Visual Studio Gallery from 3rd parties, extending functionality even further in all possible directions. Still, if it is not written by you it can’t be perfect :)

The hardest part when writing a Visual Studio extension is to find the right interface for the task. In this article I will show most “popular” interfaces to help you get started with Visual Studio customization using code. When there is no public API to perform your task, I’ll show possible workarounds.

All samples below are available as commands and extensions for Visual Commander. You can use this code as part of your own extension or you can run it directly with Visual Commander. Generally, for relatively simple functionality Visual Commander helps you write, run, customize and share VS extension code faster than with a full VSIX package. Most of the code will also run unmodified for all Visual Studio versions from VS 2010 to VS 2015 (when creating a full VSIX package you should carefully package it to provide compatibility with different versions of Visual Studio). Visual Commander also provides the common VCmd menu to run and organize multiple commands and extensions.

The most basic task for an extension is to modify selected text in the code editor. Block comment and Insert date and time commands demonstrate access to the current selection as string:

EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
ts.Text = "/* " + ts.Text + " */";

You can run existing Visual Studio commands from your code. They are easier to discover than APIs and easier to invoke. See for example Close the current document tab and activate next:

if (IsCommandAvailable("Window.NextTab"))
	EnvDTE.Window w = DTE.ActiveWindow;
else if (IsCommandAvailable("File.Close"))

Another very common requirement is to perform some task automatically on a Visual Studio event. See the list of Common Visual Studio environment events and see Open a file from the solution directory on opening a solution, Run Cppcheck on the saved file samples:

public void SetSite(EnvDTE80.DTE2 DTE_, Microsoft.VisualStudio.Shell.Package package)
	events = DTE.Events;
	documentEvents = events.DocumentEvents;
	documentEvents.DocumentSaved += OnDocumentSaved;

private void OnDocumentSaved(EnvDTE.Document doc)
	if(doc.Language == "C/C++")

You can create custom commands to programmatically setup different Visual Studio options. For example, Toggle line numbers for C# files and Set font size:

dim v = DTE.Properties("TextEditor", "CSharp").Item("ShowLineNumbers").Value
DTE.Properties("TextEditor", "CSharp").Item("ShowLineNumbers").Value = Not v

To analyze code, you can use Visual Studio code model, for example Move the caret to the beginning of the containing function, Copy to the clipboard properties of the selected class in Visual Studio text editor and Copy current file, line, method. Alternatively, you can use Roslyn, for example Create a typed variable from the current method invocation:

Microsoft.CodeAnalysis.CSharp.Syntax.InvocationExpressionSyntax invocationExpressionNode = 
if (invocationExpressionNode != null)
	Microsoft.CodeAnalysis.SemanticModel semanticModel = 
	Microsoft.CodeAnalysis.IMethodSymbol methodSymbol = 
		semanticModel.GetSymbolInfo(invocationExpressionNode).Symbol as 
		methodSymbol.ReturnType.ToString() + " v = ");

Visual Studio is a .NET, Windows, WPF application and your code runs as a part of it. If there is no public API for your needs, you can directly enumerate and modify WPF UI controls (Hide Sign in and Feedback smiley buttons in Visual Studio 2013/2015), subscribe to Application events (AutoSave files when Visual Studio loses focus), modify the MainWindow (Add version overlay to the Visual Studio 2015 taskbar icon) and simulate keyboard input with SendKeys (Toggle CodeLens on/off). As a last resort you can use .NET reflection to access internal Visual Studio assemblies.

I hope you are now feeling empowered to spend some time and add a feature to Visual Studio that you always wanted. Don’t hesitate to share your code and ask questions in comments.

Posted in Visual Studio tips, Vlasov Studio tools | Tagged | Leave a comment

Sync Block Edit v1.2 adds support for Visual Studio 2015

Sync Block Edit is a free extension that lets you easily update duplicate blocks of text in multiple files from Visual Studio 2010+.

v1.2 adds support for Visual Studio 2015.

Download the installer.

Posted in Vlasov Studio tools | Tagged , | Leave a comment

Code reference hyperlinks in Visual Studio 2015

Visual Studio 2015 allows you to create links to classes and method in other files from a comment in your C# code:

When the cursor is inside the cref attribute, pressing F12 or selecting Go To Definition opens the corresponding file with the referenced method.

When typing the referenced class or method, IntelliSense autocomplete is available. You can specify an external namespace for the target if needed. The Go To Definition command works with any tag name (e.g. <a>), but for proper XML documentation it is recommended to use <see> or <seealso>.

(This feature was first described by Peter Macej here).

Posted in Visual Studio tips | Tagged | Leave a comment

Tabs Studio v3.7.0 adds support for SQL Server Management Studio 2016

Tabs Studio is a Visual Studio and SSMS extension empowering you to work comfortably with any number of open documents.

v3.7.0 adds support for SQL Server Management Studio 2016 and introduces the AfterClose add-in to activate the next tab (instead of the last selected document) after closing the active one. See the full what’s new list.

Download the installer.

Posted in Vlasov Studio tools | Tagged , , , , , | Leave a comment

Smooth Caret v1.2 adds support for VsVim

The Smooth Caret extension adds smooth caret movement animation for text editors in Visual Studio 2010/2012/2013/2015. It looks similar to the smooth typing feature in Microsoft Office 2013.

v1.2 adds support for VsVim, increases animation smoothness and improves responsiveness.

Download the installer.

Posted in Vlasov Studio tools | Tagged , , , , | Leave a comment

Visual Commander v2.2 adds command execution debugging

Visual Commander is a freemium extension for Visual Studio 2010+ allowing you to automate repetitive tasks in the IDE.

Visual Commander Professional v2.2 adds the ability to debug command execution and automatically converts friendly command names for keyboard binding to camel case.

Download the installer.

Posted in Vlasov Studio tools | Tagged , , , , | Leave a comment

Visual Commander v2.1 adds friendly command names for keyboard bindings

Visual Commander is a freemium extension for Visual Studio 2010+ allowing you to automate repetitive tasks in the IDE.

Visual Commander v2.1 adds friendly command names for keyboard bindings to the professional edition and fixes the inability to edit C# snippets with the professional edition in Visual Studio 2015:

Friendly command names for keyboard bindings

Download the installer.

Posted in Vlasov Studio tools | Tagged , , , , | 1 Comment