Click here to Skip to main content
Click here to Skip to main content

Building a Refactoring Plug-in for VS.NET - the Sequel

By , 31 Aug 2004
Rate this:
Please Sign up or sign in to vote.

Sample Image

Introduction

Based on an existing add-in, this article shows a simple framework for creating add-ins that provide multiple refactoring functions. It shows how to interact with the source code model, automating editing functions in the source, and generating new classes.

Background

A year ago, I wrote an article that provided a Visual Studio add-in to refactor a variable into a property. Since then this add-in has been in general use in my work and I have added several other refactoring functions. To this purpose, I implemented a little framework for my add-ins to make it easier to add other refactoring functions.

The add-in now sports the following features:

  • Make Property - point to a variable and turn it into a property with setter and getter methods.
  • Make Function - select a piece of code, turn it into a function, and insert a call to the function at the original location.
  • Make Collection Class - select a class and generate a typed collection class (based on CollectionBase).
  • Make Mock Object - select a class (even framework classes) and generate a collection class that overrides all available functions and properties to allow you to simulate this class.
  • Move To Super Class - take an element and move it to the superclass.
  • What is This - a simple demonstration to show how to pick up the edit point in the code and work out what it is.

The Plug-in Framework

When you create a new project to build a VS plug-in, VS.NET generates a class called connect. This class contains all the necessary interfaces that will be called by VS.NET. There are three kinds of calls:

  1. initialization and cleanup calls: these happen at startup, when add-ins are loaded or when VS.NET shuts down.
  2. the Query call: this call occurs often, whenever VS.NET needs to know whether to enable your command in its popup menus. Your code has to determine the circumstance and then can respond with a status.
  3. the Exec call: your add-in command has been invoked and you need to run it.

You do not need to have one connect class per command, however if you make multiple ones available in this class (as is done here), the connect call has to work out which command is affected in the Query and Exec calls.

The plug-in framework solves this by creating a base class called VS-Plugin. Each kind of command listed above is implemented as a sub class of VS-Plugin. The connect class is now only aware of a collection of these plug-ins, and when a Query or Exec call comes, it simply iterates through its collection and passes the request on to the appropriate class.

Sample Image

The VSPlugIn class also contains several utility functions to do things such as:

  • what is currently highlighted, a class, a function, etc.
  • the ability to link into the necessary command structures into VS.NET.
  • putting up a dialog to ask the user for a target project.
  • displaying messages in the Output window of VS.NET.
  • etc.

Most plug-ins also have a separate class to do the actual work - e.g., the MakeMockObject plug-in class has a class called MockObjectBuilder. This is simple common sense of separating concerns, the plug-in class' purpose is to understand the request's detail and to deliver the resulting code into the right place, the MockObjectBuilder class' purpose is to generate the Mock class.

How to add another Plug-in

There are only a few steps that need to be done:

1. Create a new VSPlugIn subclass

VSPlugIn has several abstract properties and methods which you must implement:

  • cmdName: the short command, e.g.: "MakeProperty".
  • qualifiedName: the fully qualified command, e.g.: "RefactorAddIn.Connect.MakeProperty".
  • IconId: the ID of the icon to show in the menu, e.g.: 54.
  • position: where in the menu to position the command (1= first).
  • shortDescription: the text to show in the menu.
  • long Description: the text for the tool help.
  • doQueryStatus(): implement the logic to determine if the method currently makes sense and should be active or be inactive (or invisible).
  • doExec(): implement the actual action of the command.

2. Extend the Connect class

The connect class has a method called OnStartupComplete. You must add your plug-in to the other plug-in's collection and call the methods to have it show up in the right tool bars.

CommandBar popup = InsertSubMenu("Code Window", "Refactoring");
CommandBar classViewCommandBar = applicationObject.CommandBars["Class View Item"];

VSPlugIn plugin = new PropertyMaker(applicationObject, addInInstance);
Plugins.Add(plugin);
plugin.InsertCommand(classViewCommandBar,false);
plugin.InsertCommand(popup, false);

The first two lines are already given, they provide two commandbars, one a separate popup menu that is part of the Code Window menu (the one that pops up when you right click in the code window), and the other is the menu that pops up when you right click in the Class Explorer.

You need to add code in similar to the last four lines shown above:

  1. create an instance of your plug-in.
  2. add the plug-in into the plug-ins collection.
  3. if you want the plug-in be available for each commandbar, call the plug-in's InsertCommand method. The boolean should only be true for special circumstances when developing - see below in Points of Interest.

The above example makes the property maker available on both menus. You can target other menubars or leave them out altogether. In this instance, you can still bind to your command via the keyboard mapping available in the Tools->Options dialog (select Folder Environment and item Keyboard) - or you can call your command from macros.

Points of Interest

Programming Visual Studio.NET is not for the faint hearted. Good documentation is not easy to come by. However, I found the book "Inside Microsoft Visual Studio. NET" (Brian Johnson, Craig Skibo, Marc Young, Microsoft Press, ISBN 0-7356-1874-7) to be an excellent help. Still you need to read it several times.

Visual Studio .NET is written in COM, which makes life less pleasant. Here are a few pointers:

  1. Collections start with index 1.
  2. If you ask a collection for something it doesn't have, you won't get a null but an exception. So if you want to check if something is in the collection, you have to write something like this:
    protected bool HasCommand(string commName,out Command command)
    {
        command = null;
        try
        {
            Commands commands = _applicationObject.Commands;
            command = commands.Item(commName,-1);
            return true;
        }
        catch (System.Exception){}
        return false;
    }
  3. Not all commands will work under all circumstances. For instance, the command InsertClass, which inserts a new class into the code model, only works with C++, not for C# or VB.NET. However, InsertMethod does.
  4. Not all windows give their content out. While the documentation makes a lot of effort telling us how to access the tree controls in some of their windows, you need to almost read between the lines to work out that this is not available for some windows, e.g.: the Class View window.
  5. It is highly recommended to peruse and understand other examples of plug-ins. Graceful acknowledgement to Erick Sgarbi and his article on a VS Plug-in where I borrowed several techniques including the one above.
  6. If you are interested in CodeDom (defining code constructs and then generating source code), look at my CollectionBuilder class for an example.
  7. When you publish your add-in to Visual Studio, you create an object called a command. This command is the structure by which other parties in VS.NET can recognize your plug-in and invoke it. Be aware that on shutdown, VS.NET saves this command and reloads it on restart. Hence you do not have to recreate a command every time your add-in is loaded. In fact, you shouldn't, because this gums up the works. The VSPlugIn::InsertCommand method has a boolean parameter called deleteIfExists; if true, it will delete existing commands before creating a new one. This parameter should only be true during developing and debugging your add-in. In default mode (=false), it will try to find and use the existing command and create one only if it doesn't exist yet.
  8. It is also possible to create during debugging multiple instances of controls (the things that make a command visible in a pop up menu). The InsertCommand method tries to flush these out by calling flushControls(). If you don't do this, all kinds of weird things can happen, sending you around the bend.

History

  • Version 1.1 - Based upon feedback I have added several changes
    • Make Property is now configurable in the way the private variable is named.
    • The setup adds a registry entry for HKEY_CURRENTUSER\ Software\ MethodsConsulting\ RefactorAddIn\ PrivatevarCase. Possible values are:
      • underscore - prepends an underscore to the variable
      • m_underscore - prepends an 'm_' to the variable name
      • camel - use camel casing
    • Move To SuperClass now works with Variables, Properties and functions
    • Plus various fixes to smaller annoyances.

Enjoy

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Stephan Meyn
Web Developer
Australia Australia
I am a Software Engineer/Consultant. My work is focussed on helping teams to get more out of their work. So I teach how to do requirements, analysis and design in a format that is easy to understand and apply.
I help with testing too, from starting developers on automated unit testing to running whole testing teams and how they cooperate with development.
 
For really big projects I provide complete methodologies that support all of the lifecycle.
 
For relaxation I paddle a sea kayak around Sydney and the Central Coast or write utilities on rainy days to make my life easier.

Comments and Discussions

 
GeneralArticle how to write Visual studio plug-in PinmemberCiget5-Nov-09 21:27 
QuestionHow could add CommandBar into JS Editor? Pinmembervsion.zhao9-Jan-07 23:36 
QuestionVS2005 support coming up next? PinmemberSaurweinAndreas17-Feb-06 6:27 
AnswerRe: VS2005 support coming up next? Pinmemberjshallard18-Jun-06 15:29 
QuestionVB.net support? Pinmemberiammudman24-Oct-05 8:35 
GeneralInstallation problem PinmemberTim.Hill13-Feb-05 11:00 
GeneralMultiple property generation Pinsusssolo21-Nov-04 21:59 
GeneralUML Pinmemberdshorter12-Nov-04 0:19 
GeneralRe: UML PinmemberStephan Meyn2-Nov-04 0:38 
GeneralExcellent Article! Pinmemberjaxterama22-Sep-04 19:23 
GeneralRe: Excellent Article! PinmemberStephan Meyn24-Sep-04 15:13 
GeneralRe: Excellent Article! Pinmembercomputerguru9238224-Jan-06 16:55 
GeneralNaming of variable Pinmembercvollmer29-Aug-04 23:40 
GeneralRe: Naming of variable PinmemberStephan Meyn30-Aug-04 3:12 
GeneralRe: Naming of variable PinmemberLakshmipathy10-Aug-06 19:31 
General"Move to super class" PinmemberGeneD27-Aug-04 17:25 
GeneralRe: "Move to super class" PinmemberStephan Meyn28-Aug-04 0:33 
GeneralRe: "Move to super class" PinmemberGeneD28-Aug-04 6:55 
GeneralRe: "Move to super class" PinmemberStephan Meyn28-Aug-04 13:21 
GeneralRe: "Move to super class" PinmemberGeneD28-Aug-04 20:19 
GeneralRe: "Move to super class" PinmemberStephan Meyn28-Aug-04 21:49 
GeneralRe: "Move to super class" PinmemberGeneD28-Aug-04 22:45 
GeneralRe: "Move to super class" PinmemberStephan Meyn29-Aug-04 12:20 
QuestionAny Additional Requests? PinmemberStephan Meyn20-Aug-04 11:23 
AnswerRe: Any Additional Requests? PinmemberNinjaPenguin24-Aug-04 1:49 
GeneralRe: Any Additional Requests? PinmemberStephan Meyn26-Aug-04 12:24 
GeneralRe: Any Additional Requests? PinmemberCarlos J. Quintero5-Oct-04 10:10 
AnswerRe: Any Additional Requests? Pinmembercvollmer30-Aug-04 23:50 
GeneralRe: Any Additional Requests? PinmemberStephan Meyn31-Aug-04 23:19 
GeneralRe: Any Additional Requests? Pinmemberjingsong_tang@yahoo.com24-Sep-04 10:19 
AnswerRe: Any Additional Requests? Pinmemberdanmanthemooseman5-Sep-09 6:18 
GeneralNice article Pinmemberjohn_sh20-Aug-04 6:27 
GeneralRe: Nice article PinmemberStephan Meyn20-Aug-04 11:19 
GeneralVB.Net PinmemberTim Downey20-Aug-04 2:28 
GeneralRe: VB.Net PinmemberStephan Meyn20-Aug-04 11:07 
GeneralRe: VB.Net PinmemberTim Downey23-Aug-04 2:44 
GeneralRe: VB.Net (and VC++) PinmemberClaus Brod24-Aug-04 1:03 
GeneralRe: VB.Net (and VC++) PinmemberStephan Meyn26-Aug-04 12:21 
QuestionContext Menu is greyed out? Pinmembersparkesyuk18-Aug-04 23:06 
AnswerRe: Context Menu is greyed out? PinmemberStephan Meyn19-Aug-04 21:53 
Questionhow to add the refactoring add-in? Pinmemberohadyn18-Aug-04 4:11 
AnswerRe: how to add the refactoring add-in? PinmemberStephan Meyn18-Aug-04 11:50 
QuestionVS 2002? PinmemberGary Thom16-Aug-04 3:31 
AnswerRe: VS 2002? PinmemberStephan Meyn16-Aug-04 10:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 1 Sep 2004
Article Copyright 2004 by Stephan Meyn
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid