Executing Custom Actions with AutoCode
One of the main features of AutoCode is generating in-line code. But AutoCode can also be used to execute IDE actions in a quick and easy way. For example, commenting a piece of code, formatting the current document or opening any of the multiple Visual Studio Windows (Error List, Immediate Window, Test Results, etc.) are tasks that can be executed using AutoCode instead of using the mouse through the IDE menu. Although many of these tasks can be invoked using shortcuts, these shortcuts are not always very intuitive and therefore difficult to remember. In my case I always end up using the mouse instead of the shortcut. AutoCode allows us define custom commands to invoke any IDE actions using a more intuitive shortcut. An Example to Invoke “Close All But This” For example, to close all the editor windows but the active one we need to move the mouse to the current document tab, right click the mouse and then click the “Close All But This” option. It would be faster just to invoke an AutoCode command like, for example, ‘ca’ for “Close All”. It is, Ctrl+Enter and then ‘ca’. The following command does exactly that by invoking the “File.CloseAllButThis” IDE command. <?xml version="1.0"?>
<Commands xmlns="http://schemas.devprojects.net/AutoCode/v3.0">
<Command name="CloseAllButThis">
<CommandBehavior>
<CommandLine shortcut="ca" />
</CommandBehavior>
<CommandCode language="csharp">
<Execute>
DTE.ExecuteCommand("File.CloseAllButThis", "");
</Execute>
</CommandCode>
</Command>
</Commands>
Using the <Execute> Tag to Execute Code
The <Execute> tag allows us to execute any script of code we want. In this case we are invoking the “File.CloseAllButThis” IDE command, but we can execute any other IDE command or furthermore, we can execute any piece of code.
In the example we are using csharp as the script language, but we can use also VB.BET like in the following example. Notice the language attribute (and also the missing semicolon at the end of the sentence :) <CommandCode language="vb">
<Execute>
DTE.ExecuteCommand("File.CloseAllButThis", "")
</Execute>
</CommandCode>
Other IDE Commands
Modifying this simple command, any of the IDE commands can be easily invoked from the AutoCode input box. Just replace the name, shortcut and command name.
Here are some examples of other useful IDE commands:
Edit.CommentSelection for commenting selected code Edit.UncommentSelection for uncommenting selected code View.TaskList to show the TaskList Window Debug.Immediate to show the Immediate Window Edit.FormatDocument to format the whole document
All IDE actions that have an associated command can be used. To know the exactly name of the IDE command you can use the Options menu:
- Open the Tools->Options window
- Select Environment->Keyboard
- On the “Show commands containing” textbox write any word of the command and the list of commands containing that word will appear
|