SLaks.Blog

Making the world a better place, one line of code at a time

Extending Visual Studio 2013, Part 1: Getting Started

Posted on Friday, October 18, 2013

In addition to being an excellent development environment, Visual Studio also has a powerful extensibility system. In this blog post, I will explain how to start writing Visual Studio extensions, so you can make the IDE work the way you want it to.

Getting Started

To use or develop extensions, you need Visual Studio Professional or higher (Express Edition won't work).

First, download and install the Visual Studio SDK (for VS2012, see here; this adds project types for Visual Studio extensions and is required in order to open or create any extension.

Next, you need to decide whether to add features to an existing open source extension or create a brand new extension.

If you want to extend an existing extension, you'll need to create an account on GitHub and fork the project to your account so that you can push your changes. Then, clone the project to your computer (GitHub for Windows is an easy way to do this; SourceTree is more powerful) and start making changes. Make sure to commit to every time you finish working on something so you have a nice history; you can do this directly from Visual Studio or in the aforementioned applications. Finally, once your feature is finished and tested, open a pull request on GitHub to ask the project maintainer to merge in your changes.

Side Note: Tips for contributing with git

The pull request model work best with feature branches – making a separate branch for each contribution. Before starting each feature, you'll want to create a new branch, reset that branch to the latest version of the original (upstream) project, then make changes from there.

This is easiest to do on the git command line. First of all, if you aren't already, use posh-git to get nice tab completion for git commands and branch names. (GitHub for Windows includes this if you use PowerShell; git bash has a similar feature)

First, you need to add the upstream repository as a remote (an external git repository that you can pull from) so that you can pull changes directly from the original project to your machine. This only needs to be done once:

git remote add upstream <url>

upstream is the name of the new remote; <git-url> is the https://github.com/... URL of the original repository. You can find this URL on the right side of the project on GitHub.com:

GitHub clone URL

Once you've added the remote, run the following commands to make a new branch and reset to the original state.
Warning: This will blow away all uncommitted changes! Only do this after committing everything you've been working on.

git fetch upstream
git checkout -b my-awesome-feature
git branch reset upstream/master --hard

You're now ready to begin working on the new branch. If the upstream project changes in the meantime, you can run the following commands to merge in the changes without breaking history: (make sure to commit everything first)

git fetch upstream
git rebase upstream/master

git rebase will rewrite the history of your local branch to include the upstream changes before your new commits. This way, when you make the pull request, all of your commits will extend the tip of the original repository, avoiding complex merge issues.

Since this rewrites history, if you've already pushed the branch to your fork on GitHub, your next push will need the -f flag to overwrite the existing remote history.

Visual Studio Extensibility Basics

Visual Studio is a 10+ year old codebase built on a mix of technologies new and ancient. Most of the older portions of Visual Studio, including the project system and the command dispatcher, are written in native code and support extensibility using ugly COM and GUIDs. Newer parts of Visual Studio are usually written in managed code (the big exception being the new Javascript language service, which is written in C++ around IE's Chakra engine), and are thus easier to extend.

In particular, the editor was completely rewritten in Visual Studio 2010, and is implemented entirely in C# code using WPF. This new editor uses the Managed Extensibility Framework (MEF) to load and execute all of its internal components, making it very friendly for extensions. You can write classes that implement interfaces from the editor and export them using MEF, and the editor will automatically import and run them when it loads appropriate documents. Similarly, you can import interfaces from the editor to gain access to existing services like colorization and error checking for use within your extension.

Next time: Core concepts

Categories: visual-studio-2013, vs-extensions Tweet this post

comments powered by Disqus