Where Am I: A Visual Studio 2010 Extension

One of the pieces of information that I always seem to want to know when I’m coding is the full path of the file I’m currently editing. Whether this is to make sure that I’m editing the correct file (in projects where files have similar names) or just to get a sense of where the heck I am I’m not entirely sure. You can get this information by hovering the mouse pointer over the tab for the current document but the experience sucks. The little popup seems to fade away before I finish reading it and it takes your hands off of the keyboard for what (I feel) should be ambient information. One lunch time at work I decided I wasn’t going to put up with this any more so I created a Visual Studio 2010 Editor Extension called Where Am I.

Where am I

Where Am I is pretty simple, it just adds a border along the bottom of the editor window with the full path of the current file in it. As an added bonus if you click on the extension border, it opens an explorer window with the current file selected (I seem to need to do this a lot too). This is handy for doing things with the Tortoise brand of tools.

The easiest way to try it out is using the Visual Studio Extension Manager (Tools –> Extension Manager) and search for "Where Am I". Within a day of making it available there has been > 50 downloads and one 4-star review. By the way that guy wants me to implement a feature that is better than what I had in mind and also is completely outside of the realm of my knowledge for how to accomplish. If you know how I can get the color-scheme of the current tab in a Visual Studio 2010 editor extension, drop me a line at michael.minutillo@gmail.com

If you’re keen to get a look at the code it is available on http://github.com/wolfbyte/WhereAmI. You’ll need the Visual Studio 2010 SDK to compile it. Where Am I relies on the new MEF extensibility points (new in VS2010) to provide a IWpfTextViewMarginProvider. Visual Studio will call on my margin provider whenever it creates a window that contains interactive text (there are attributes on the provider that dictate this). One of the parameters that Visual Studio provides is a Text View Host that I can use to retrieve information about the document that is currently being edited, including its full path. Then it’s a simple matter to create a WPF control that will be created and added to the bottom of the editor window.

The only tricky bit is that sometimes a filename will change while the editor window is open. In order to account for this I don’t just extract the path and drop it into a TextBlock. Instead I created a wrapper class (FileNameWatcher) for the file name that watches for the FileActionOcurred event and raises INotifyPropertyChanged notifications. This makes it really easy to use WPF data binding to display the text.

If you haven’t given it a go yet I recommend playing around with the new VS2010 Editor Extensibility stuff as it can be quite interesting and automating your development environment is guaranteed to make you more productive by at least 7 :p And once you do get a nifty extension going, upload it to the Visual Studio Gallery. It takes about 2 minutes (less if you already have a LiveID) and makes your extension available inside Visual Studio itself.

Finally, there is a chapter on Visual Studio 2010 Editor Extensibility with MEF towards the back of Professional Visual Studio 2010. If you buy a copy I promise to use the money to feed my children :p

Posted by: Mike Minutillo
Last revised: 27 May, 2011 03:34 PM History


24 Jul, 2011 11:57 AM

This blog post totally saved me today. I'm trying to build a Visual Studio extension to support a custom scripting language, and for days I've been struggling with the simple task of figuring out the filename and line number of the currently selected editor text from a Shell menu callback. If not for this post and sample code, I would never have been able to figure out that the simplest way to get at the filename was

IWpfTextViewHost viewHost;
ITextDocument document;
viewHost.TextView.TextDataModel.DocumentBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document);
return document.FilePath;

The key hangup for me is that the contents of an IPropertyOwner aren't documented in the same way as all other class properties — the MSDN for DocumentBuffer shows all of its CLR properties, but not the ones that come in via IPropertyOwner. Is there someplace that documents what properties these types get from their property owner, or conversely, that if what I wanted was the filename, this is the place to go looking for it?

No new comments are allowed on this post.