Exporting the Macro


Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Chapter 4.  Macros and Visual Studio Extensibility

Exporting the Macro

If you want to share macros, you can export a macro from your IDE and another developer can import that macro into their IDE. This section introduces a pair of macros that allow you to enable or disable all breakpoints with a single macro command; these macros will be used to demonstrate exporting and importing macros. First let's introduce and review the new macros.

Macros for Enabling and Disabling Breakpoints

You can set breakpoints by clicking in the left margin of the code editor. You can right-click a breakpoint to modify its properties (see Figure 4.5) and set break conditions or modify the hit count. The hit count determines whether the breakpoint suspends execution every time or breaks based on some other rule (for example, Figure 4.6 shows the specification of a breakpoint setting that will break when the hit count is a multiple of 5).

Figure 4.5. Breakpoint properties can be used to customize the behavior of break conditions.


Figure 4.6. Customizing the breakpoint hit count.


When you spend the effort to create breakpoints, you might want to disable them without losing the effort expended in creating those breakpoints. The IDE allows you to disable breakpoints without removing them. To leave a breakpoint in place but disable it, right-click the breakpoint and select Disable Breakpoint from the context menu. Repeat the operation, selecting Enable Breakpoint to turn the breakpoint back on. However, if you have multiple breakpoints, it might be easier to enable or disable all breakpoints with a single macro command rather than enabling or disabling the breakpoints in the breakpoints window manually.

All of the breakpoints are accessible in the EnvDTE.DTE.Debugger.Breakpoints collection. (You would only know this if you have read this passage or explored the Extensibility object model.) To disable all breakpoints, we need to create a new macro. This can be accomplished by choosing Tools, Macros, New Macro Command in the VS .NET IDE. The following code would enable all breakpoints.

 Sub EnableAllBreakpoints()   Dim Breakpoint As Breakpoint   For Each Breakpoint In DTE.Debugger.Breakpoints     Breakpoint.Enabled = True   Next End Sub 

Copying the macro to a new macro named DisableAllBreakpoints and rewriting Breakpoint.Enabled = True as Breakpoint.Enabled = False would disable all breakpoints. However, duplicating the code by copying and pasting is not something we want to do even with macros; we need to abstract common behavior into a single procedure. Abstracting common behavior for enabling and disabling yields the following revision:

 Private Sub SetBreakpointState(ByVal Enabled As Boolean)   Dim Breakpoint As Breakpoint   For Each Breakpoint In DTE.Debugger.Breakpoints     Breakpoint.Enabled = Enabled   Next End Sub 

In this version of Visual Studio Macros, we can only pass string arguments to macros. If supporting procedures need arguments, you can pass these arguments as strings and convert them to the type the procedure needs or define a parameterless macro that passes a specific value to the supporting procedure. Applying the second technique to the SetBreakpointState macro yields the code in Listing 4.3.

Listing 4.3 Two macros that are implemented in terms of a common behavior in the macro module
  1:  Private Sub SetBreakpointState(ByVal Enabled As Boolean)  2:  Dim Breakpoint As Breakpoint  3:  For Each Breakpoint In DTE.Debugger.Breakpoints  4:  Breakpoint.Enabled = Enabled  5:  Next  6:  End Sub  7:   8:  Sub EnableBreakpoints()  9:  SetBreakpointState(True)  10:  End Sub  11:   12:  Sub DisableBreakpoints()  13:  SetBreakpointState(False)  14:  End Sub 

Either EnableBreakpoints or DisableBreakpoints can be invoked in any of the usual ways that macros can be invoked, including from the IDE Command window, the Macro Explorer, the Macro IDE, or a keyboard or toolbar shortcut.

Practical Considerations

You can enable or disable all breakpoints from the Command window. If you open the Command window and type Debug.DisableAllBreakpoints or Debug.EnableAllBreakpoints, the IDE will perform these operations for you.

The example in this section was written to demonstrate the process of implementing macros and some of the capabilities of the DTE object. Because the capability of managing breakpoints already exists, we will add some additional behavior to our custom breakpoint management to make it a worthwhile endeavor.

Sending Information to the Output Window

In addition to the DTE.Debugger object, the DTE object contains a Windows collection. The Windows collection contains a reference to all of the windows in the IDE; therefore to send information to the Output window, all we need to do is request a reference to that window and invoke behaviors it has defined.

The revision in Listing 4.4 demonstrates how to get a reference to the Output window and send status information to it as we are enabling or disabling breakpoints automatically.

Listing 4.4 Enabling and disabling breakpoints with logging information sent to the IDE's Output window
  1:  Option Strict Off  2:  Option Explicit Off  3:   4:  Imports EnvDTE  5:  Imports System.Diagnostics  6:   7:  Public Module Debugger  8:   9:  Function GetOutputWindow() As OutputWindow  10:  Return _  11:  DTE.Windows.Item(_  12:  Constants.vsWindowKindOutput).Object  13:  End Function  14:   15:  Function GetActivePane() As OutputWindowPane  16:  Return GetOutputWindow.ActivePane  17:  End Function  18:   19:  Private Sub WriteState(ByVal BreakPoint As Breakpoint)  20:  GetActivePane.OutputString(_  21:  String.Format("Breakpoint ({0}) enabled={1} ", _  22:  BreakPoint.Name, BreakPoint.Enabled) & vbCrLf)  23:  End Sub  24:   25:  Private Sub SetBreakpointState(ByVal Enabled _  26:  As Boolean)  27:   28:  Dim Breakpoint As Breakpoint  29:  For Each Breakpoint In DTE.Debugger.Breakpoints  30:  Breakpoint.Enabled = Enabled  31:  WriteState(Breakpoint)  32:  Next  33:   34:  End Sub  35:   36:  Sub EnableBreakPoints()  37:  SetBreakpointState(True)  38:  End Sub  39:   40:  Sub DisableBreakpoints()  41:  SetBreakpointState(False)  42:  End Sub  43:   44:  Sub EnableAllBreakpoints()  45:  Dim Breakpoint As Breakpoint  46:  For Each Breakpoint In _  47:  DTE.Debugger.Breakpoints  48:  Breakpoint.Enabled = True  49:  Next  50:  End Sub  51:  End Module 

The procedures EnableBreakpoints, DisableBreakpoints, and EnableAllBreakpoints you have seen already. Line 31 introduces a call to WriteState in the private SetBreakpointState method. WriteState references an object GetActivePane, which returns DTE.Windows.Item(Constants.vsWindowKindOutput).Object cast to an OutputWindowPane on line 16 by using it as a return argument for the GetActivePane function. An OutputWindowPane has a method OutputString that allows us to send text to the Output window. Lines 19 through 23 get the Output window pane and send formatted output to it indicating the name and state of the Breakpoint whose state was modified.

Query methods GetOutputWindow and GetActivePanewere written to preclude writing a monolithic statement to access, type cast, and invoke a method on the Output window. This is a good technique for shortening and clarifying long statements.

Exporting Macro Modules

The complete set of macros in the Debugger module represent code that you might reasonably want to share with other developers. To do so, we need to export the module.

To export a macro module, open the Macros IDE from the Tools, Macros menu in the VS .NET IDE. Select the macro module that you want to export in the Macros IDE and choose File, Export modulename, where modulename is Debugger in this example. The Macros IDE will display an Export File dialog box that will allow you to select the location where the file will be exported.

Importing Macro Modules

To import a macro module, choose File, Add Existing Item from the Macros IDE. Public subroutines with no arguments or an Optional string argument with an initial value will be displayed in the Macro Explorer as macro entry points that can be invoked from VS .NET IDE or run from the Macros IDE.


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
Year: 2001
Pages: 222

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net