Use custom plugins to maximize your productivity in Visual Studio.net
Original connection: http://msdn.microsoft.com/msdnmag/issues/02/02/vside/print.asp
Author: Leo A. Notenboom
Translation: Zhang Xiang kilxy@hotmail.com This article is assumed to be familiar with C #, Visual Basic .NET and CLR
Difficulty 1 2 3
Summary
For developers, no matter how powerful integrated development environment (IDE) requires a function of having custom plugins. Based on this reason, Visual Studio. Net IDE provides an expandable, added convenient and has no number of custom plug-in features. This custom plugin can be written in Visual Basic, C, C # or other language used by other .NET. This article explains how to add an example of a custom plugin in Visual Studio. NET. This example will demonstrate how to complete a custom text editing feature through a custom plugin, which implements two functions, one is simple inserted into the current date in the text, and the other is more complex functionality to achieve a restriction text. Finally, you will learn how to add a page in the Options dialog box.
If you start using Microsoft Visual Studio .NET you should know that he is full of new features and techniques. Although, you can think that the integrated development environment (IDE) is a simple text editor written code, it is very powerful. It provides the framework to support the insertion of the development tool and is a single seamless development. Of course, IDE does not meet all people, you or if I want it may not be here. Fortunately, IDE has provided scalable features, which allows you to expand anything you need. Start
Plugins and macro are two ways to extend IDE. Macros can be recorded and executed immediately. Therefore, this is a powerful method for exploring the object model. Macro uses a .vsmacros file to be distributed in the form of a .vsmacros file, load it into the macro editor by double-clicking this file. After the macro is available, the macro can be modified very easy by the user.
On the other hand, the plugin is compiled and cannot be modified after the distribution. This protects your knowledge property. Through the plugin, you can create a tool window These operations are just like operating local features for Visual Studio.net. The plugin can dynamically change the menu, the status of the toolbar event, and add information in the Help dialog. The plugin is deployed by Microsoft's installation file (.msi), which can be easily installed and uninstalled by the Add / Remove Program dialog box in the control panel.
Steps to create a plug-in have hidden in the online help of Visual Studio.Net, and also hidden in Visual Studio.net Automation Examples Web Site. I don't want to introduce each particular process here, but I can review the process I have selected by the plugin.
Creating a plugin first To create a project, under the New Projects dialog box Other Projects (Other Projects), you will find the Extensibility Projects. Select Visual Studio.NET Add-IN. Figure 1 shows the new project dialog box at a point. Click the OK button to start the wizard.
Figure 1 Create a new project
For my plug-in, the first three steps in the wizard are easier. The first step, I chose the C # language as the development language of the plugin. In the second step, I chose Microsoft Visual Studio.NET as the host (Host) of the plugin. In your plugin you can of course include vsmacros IDE. In the third step, the plugin is named "Text Editing Utilites" and gives the appropriate description. figure 2
In the fourth step in the wizard, as shown in Figure 2, some operations are required.
l Select "Yes, create tool menu items".
l Determine "My external program is not available ..." is not selected. I am not ready to provide a mode user interface in this example, but if you really want more to extend your plugin feature, you can choose this.
l Determine "I hope that my external program is loaded when the host program starts" is not selected. This makes it easy to debug. The plug-in user can change this option in the operation of future plug-in management.
l I chose "My external program is not just ..." option. I am a real user in my computer, and I confirm that this example is also for more developers. This option is only the user who has changed the plugin has been registered.
Step 5, you can contain some help information. Select the check box and enter any contact information below. Now you can complete the wizard and start our plugin.
The method of the Connect object is listed below. These methods are incomplete and can be found in the connection.cs file. Part of these new projects.
l Connect :: Connect. This is a constructor, here you can do simple initialization.
l Connect :: onConnection. This method is called when IDE really loads your plugin. Here you can initialize your plugin, and must provide command information for IDE, make keyboard bindings, and more.
l Connect :: querystatus. This method is called when the IDE determines when the IDE is appropriate when the IDE is appropriate.
l Connect :: EXEC. This method is that the true execution command of the IDE is called.
Now, all the basic contents are here, let us start developing a simple plug-in example.
A simple editing function: Insert Date
I created a simple insert date, the exact function is: insert the current date in the current cursor location or the current selection text. This is the example of the Visual Studio .NET already contains an example of this function with macros. So you can see the same functions through two different ways through macro and plug-ins. This example is very simple, just modify the EXEC function listed in the previous example. Through the previous chapter I have created a basic plugin, the name of this plugin is "TextUtil" is more exactly "TextUtil.connect.TextUtil". In the Exec function I replaced the code for the wizard automatically generated.
Handled = True;
Replace
Handled = INSERTDATE ();
Increase the following INSERTDATA () function, you can add this function anywhere in the Connect object.
Private bool insertdata ()
{
ApplicationObject.actiVedocument! = null)
(TextSelection) ApplicationObject.actiVedocument.selection .Text
= DATETIME.NOW.TOSTRING ("YYY-MM-DD");
Return True;
}
The INSERTDATA function uses System.DateTime and Sytem.String objects. About System.String introduction can be found in the help. Here I noticed three lines of code about Commandbars in OnConnection, this three-line code is to create a menu item in the Tools menu during the process of running. In addition to the name plugin has been completed. Press F5 to test the plugin. A new IDE instance is running, in this instance, this plugin will appear in the Tools | Add-Explorer Administration dialog. The plugin can be loaded by selecting the leftmost check box in the re-adder management dialog box. Now you can open any text file, then enter "TextUtil.command.TextUtil" in the command window of Visual Studio .Net, you will insert the current date in the current cursor. In fact, the automatic variation has been filled in the command before you complete the input.
When you press F5 to start running an IDE new instance, your plugin is in debug mode. We can discover all exception information in debug mode. Any exception appears in a normal operation, they may not be available. As much as possible to get an abnormal information, then the cause of the error will discover as soon as possible.
Change the name of the command
The wizard has specified the default name (TextUtil) for the menu command. So this name does not have a distinction, I will add more menu commands to change the name of the menu command. At the same time, because I encountered a chaotic situation when changing the name of the command, we must confirm that our plugin will not have the same command name.
The first change is to call AddNameCommand in the onConnection function.
Command command = commands.addnamedcommand (AddinInstance,
"Insertdate",
"INSERT CURRENT DATE",
"INSERT CURRENT DATE",
True, 59, Ref contexTguids,
(int) vscommandstatus.vscommandstatussupported
(int) vscommandstatus.vscommandstatusenable);
Changed in QuertysTatus as follows:
IF (commandname.tolower () == "textusconnect.insertdate")
The wizard should be registered with the plugin information before the initial run load, but the change in the name affects the registration information. This means that we need new plug-in projects and installation items when adding the plug-in function. Then run the installer.
Figure 3 Operating plugins (description: Running the macro translator: may be wrong)
Now "textusconnect.insertdate" command can work according to my thoughts (Figure 3). But how did he work? Let us continue to look down. How is the plugin work?
InsertDate code given to you in the previous section is quite simple, and the automatic completion of the function is incapable.
I will start with the object browser (Object Browser) because he can learn the object very easy to learn the object so he is a very good assistant. You can click Right click on the object you want to view and then select Go to Definition to view the members of the class. Final Results See Figure 4, you can view the prototype of any member in the member listed, or select a member to press F1 to help.
Figure 4 Object Browser
The ApplicationObject object describes the plug-in host (Host) application, in this example, host (Host) application is Visual Studio .NET IDE. ApplicationObject is implemented in the OnConnection method. Find "DTE" in the online help, even declare the "_dte" type of variables in the code automatically generated. You will see many interesting members at the application level. One member is an ActiveDocument property (which describes the documentation of the current focus. This document is the document you want to perform insertdate. A very useful rule is that a very useful rule is that the document that is focused on whether the form is focusing regardless of whether the form has focus.
ActiveDocument.Selection property returns a description of the object selected in the document. Because it is an ordinary object in C #, I have enforce the type to convert into TextSelectioni. ActiveDocument is an ordinary class because Document (Translator: Document class similar to VC ) is not necessary based on text, it is better than a form design Document (translator: people who have learned VC may be more familiar with the inheritance relationship of the Document class Unfamiliar people can see books about this. As the final result Selection attribute is also a normal class, here I enforce type to convert it into the TEXTSELECTION type that we actually operate in your code.
TextSelection describes a view of a file (view), and the action of this file and tools in the tools are available in the settings of the settings. He provides a lot of properties and methods you might think about to modify files, and he can affect the view (view), currently selected content, insertion. If you have recorded a macro, you can see using the TextSelection object to capture the content.
INSERTDATE simple setup TEXT attribute value is the current date. Like TEXT, all the actions can set the same type of value. This means that all obtained content must be replaced. If you don't choose content, you will replace it at the location of the cursor, but you have to pay attention to the current state is insert or override mode.
Another method is to use the INSERT function, which allows you to control the location of the text and describe an action that cannot be done. Use this method to replace the Text property to specify the code of the insertion content as follows:
(TextSelection) ApplicationObject.ActiveDocument.selection .insert (
DateTime.now.toString ("YYYY-MM-DD"),
(int) envdte.vsinsertflags.vsinsertflagscollapseetoee);
This vsinsertflags pointed out what the text has been inserted. The documentation of VsInsertFlags is ignored in the online gang group, so I contain this part in Figure 5.
Now you should understand how the plugin works, let us make a keyboard bonding and add a menu for this command.
Figure 5 vsinsertflags value
VsinsertFlagsCollapseToend
The Selection's Current
Contents Are Deleted Before
Performing the insertion, and
The TextSelection is Left Empty
At the end of the newlyInserted text.
vsinsertflagscollapsetostart
The Selection's Current
Contents Are Deleted Before
Performing the insertion, and
The TextSelection is Left Empty
At the beginning of the newly
INSERTED TEXT.
vsinsertflagscontainnewtext
The Selection's Current
Contents are replaced with the
INSERTED text, and the
TextSelection is Left containing
The new text.
vsinsertflagsinsertatstart
The Inserted Text Is Placed At
The beginning of the beginning of there
TextSerection, and the
Resulting TextSerection
Contains Both the new and
Previous text.
VsinsertFlagsInsertatend
The Inserted Text Is Placed At
The end of the textselection,
And the resulting
TextSelection Contains Both
The New and Previous Text.