Write Code Refactoring Extensions In Visual Studio 2015

In this article you will learn how to write Code Refactoring Extensions in Visual Studio 2015.

Microsoft has launched Visual Studio 2015 with many improvements and new features as well which includes simple access to online resources like Team Explorer, Salesforce, Cross platform mobile apps in C# using xamarin, cross platform html apps with Cordova, cross platform mobile apps in c# with unity, universal windows apps for any Windows 10 device, ASP.NET 5 in web, device preview menu bar to see UI for different screen sizes look, diagnostic features, cross platform debugging support, live code analysis and much more.

One of these great features also includes Extensibility templates which provide the ability to write new code refactorings, code analyzers, diagnostic and code fixations, etc. When we will add our own created code refactorings they will start to appear in the Quick Actions menu when we are working on the source code in IDE.

You will need to install Visual Studio Extensibility templates, when we install all components of VS 2015 they are automatically been installed on the machine with VS 2015. In case it was not installed you have to install it yourself first.

If you didn’t installed all the components of Visual Studio then you will have to install the templates by going in to Tools, then Extensions and Updates.

Updates

When the popup opens, go and click on Online from left pane and then search for .Net Compiler SDK:

SDK

After installation, restart visual studio and now you will be able to see Code Refactoring VISX template under C# >> Extensibility when creating new Project.

When you are done with installation of templates, you can find the Extensibility option in Project templates list when you go for creating new project, and on selecting 'Extensibility' you will see the templates available under it, from there select Code Refactoring (VSIX) project.

project

Now you can create project for it, give a name to your project and click Ok. You are done with your first Code Refactoring Extension.
By default the project contains a simple example extension that reverses the declaration type name.

If we right click on type declaration line we can see Quick Action in Menu, clicking on it will show the Reverse extension button and clicking it will update the class name by reversing the characters:

class