Skip to main content

Calling Built-in, or Add-in functionality from another Visual Studio Add-in or Macro

As a part of the Visual Studio add-in I'm creating for code formatting I'm calling 2 built-in functions.

  • "Edit.RemoveAndSort"
  • "Edit.FormatDocument"

These 2 are functions I use all the time as I code to keep things neat and tidy. I've found it great to automate these within my addin so that I don't have to worry quite so much about accidentally messing up indents and such.

What I got to thinking though, was what if I could automate calls outside of built-in functionality? One of the main purposes of the add-in is to appease StyleCop...not that it's necessary. But, what about after doing a full format, I call stylecop on the individual document? Or if I run over the entire solution, telling stylecop to run over everything? And what about other add-ins that have been mentioned previously such as GhostDoc?

After asking a question about such a thing on the MSDN forums I actually found that it was a lot easier than I expected. An MSDN Moderator, Wesley Yao, pointed me to this post on 'Dr. eX's Blog' about the EnableVSIPLogging registry entry.

Using this and finding the canonical name, I can call any menu item functionality installed into Visual Studio's menu system.

  1.     EnvDTE.Window ideWindow = projectItem.Open(Constants.vsViewKindCode);
  2.     ideWindow.Activate();
  3.     ideWindow.SetFocus();
  4.     ideWindow.DTE.ExecuteCommand("Tools.RunStyleCop", String.Empty);

I'll be toying with this a bit to see about hooking into more functionality in order to make a well-rounded code-formatting solution.

I've still got a ways to go before doing any sort of release on the add-in. I need to look into mocking some of the visual studio interfaces for unit tests and I need to look into separating some of the functionality out into a class library so I can more easily backport it to 2005 and port it to 2010. I also need to research into VSPackage which I believe doesn't require any sort of port. I also need to do a UI for choosing which tasks to enable.