Cloning a Procedure

 < Free Open Study > 



Practical Uses for Editor Automation Objects

At this point, you have only scratched the surface with respect to the large number of properties and methods you can use to manipulate code in editor windows. It is beyond the scope of this chapter to investigate each and every one of those properties and methods. You can do that on your own as you find that you have a specific need I have not covered. MSDN for Visual Studio .NET contains a hierarchical chart of all of the objects within the automation model.

Now I want to illustrate how to use some of the objects, methods, and properties that I believe you'll find most useful. Again, you'll place them into reusable code methods so that you can continue to use them in your development of add-in functionality. As I've stated more than once, this will allow you to quickly implement new functionality without concerning yourself with the details of the extensibility model.

Retrieving Code from a Document

The Selection property of the TextSelection object returns an object representing the current selection on the object. For example, if a user selects a block of text in a document or code window, the code snippet in Listing 5-6 will retrieve the selected block and place it into a string object named selCodeBlock.

Listing 5-6: Retrieving Code from the Text Editor

start example
 Dim selCodeBlock As TextSelection = oVB.ActiveDocument.Selection() Dim S As String S = selCodeBlock.Text 
end example

The variable (object) S will now contain the text the user selected in the active document or code window. I illustrated the use of the Selection property in Chapter 3, when I demonstrated the GetCodeFromWindow method shown in Listing 5-7.

Listing 5-7: Retrieving Code from a Document

start example
     Shared Function GetCodeFromWindow() As String         Dim s As String         Dim selCodeBlock As TextSelection = oVB.ActiveDocument.Selection()         Try             GetCodeFromWindow = selCodeBlock.Text         Catch e As System.Exception             MsgBox("Error: " & e.Message, MsgBoxStyle.Critical, _                 "GetCodeFromWindow")         End Try     End Function 
end example

Calling GetCodeFromWindow, as shown in the following code snippet, will return in the variable S the block of code the user selected in the Text Editor window.

 Dim S As String S = GetCodeFromWindow() 

Putting Code Back into the Window

The second method in the pair that retrieves and replaces code in the Code Editor is the PutCodeBack method shown in Listing 5-8.

Listing 5-8: Replacing Code in the Text Editor Window

start example
     Shared Sub PutCodeBack(ByVal s As String)         Dim selCodeBlock As TextSelection         Dim datobj As New System.Windows.Forms.DataObject()         Try             selCodeBlock = CType(oVB.ActiveDocument.Selection(), _                 EnvDTE.TextSelection)             datobj.SetData(System.Windows.Forms.DataFormats.Text, s)             System.Windows.Forms.Clipboard.SetDataObject(datobj)             selCodeBlock.Paste ()         Catch e As System.Exception             MsgBox("Could not put code back in window.", _                 MsgBoxStyle.Critical, _                 "PutCodeBackInWindow")         End Try     End Sub 
end example

This method not only illustrates the use of the TextSelection object, but it also shows how to put text onto the Clipboard. In VB 6.0, you could place code on the Clipboard with this one line of code:

 Clipboard.SetText S 

In VB .NET, placing code on the Clipboard is a little more involved, as shown by this snippet from the PutCodeBack method, which I demonstrated in Chapter 3:

     Dim datobj As New System.Windows.Forms.DataObject()     datobj.SetData(System.Windows.Forms.DataFormats.Text, s) 

Once the code has been placed on the Clipboard, you can use the Paste method of the TextSelection object to replace the code in the ActiveSelection.

Inserting Code at the Top of a Module

In the process of building add-in functionality, you will probably encounter the need to build and insert module-level variables into a code module. This is afairly common requirement for more advanced add-ins. The code in Listing 5-9 illustrates how to do this.

Listing 5-9: Inserting Module-Level Variables

start example
 01     Dim objTextDoc As TextDocument 02     Dim objMovePt As EditPoint 03     Dim objEditPt As EditPoint, iCtr As Integer 04 05     ' Get a handle to the current document and create an EditPoint. 07     objTextDoc = DTE.ActiveDocument.Object 08     objEditPt = objTextDoc.StartPoint.CreateEditPoint 09     objEditPt.LineDown(1) 10 11     ' Insert a new variable line 12     objEditPt.Insert(" Public s As String" & vbCr) 
end example

Line 07 sets a TextDocument object (pointer) to the current document in the text editor. Line 08 moves the insertion point to the top of the document by creating an EditPoint object. Because you want to insert the new variable within the module (in other words, after the module definition line), line 09 moves the insertion point down one line. Line 12 simply uses the EditPoint object's Insert method to insert the new module-level variable. Figure 5-3 shows the sample module before the code to insert the variable is executed.

click to expand
Figure 5-3: Module before the insertion of a module-level variable

Once the code to insert the variable is executed, the code window will appear as shown in Figure 5-4. The inserted line is highlighted. Obviously, this is a very simple example that includes no Imports or Inherits statements. If either of these statements were included, you would need to move down past them before inserting the variable.

click to expand
Figure 5-4: Module after the variable is inserted

Adding a Procedure to the Bottom of the Document

There are many times when you will need to add a new procedure to a class or module. First I illustrate how to do so, and then you will enhance your add-in with some more useful functionality. To add a new procedure to the active code window, use the code in Listing 5-10.

Listing 5-10: Adding a Procedure to the Bottom of a Document

start example
     Dim objTD As TextDocument = oVB.ActiveDocument.Object     Dim objEP As EditPoint = objTD.EndPoint.CreateEditPoint     ' We are past the end of the last line of the document     ' move back in front of the End Module/Class     objEP.LineUp(1)     objEP.Insert("Public Function Test()" & vbCr & _                          " ' test comment" & vbCr & _                          "End Function" & vbCr) 
end example

After you execute the code in Listing 5-10, the new method will be added to the end of the current code window. The new method is highlighted, as shown in Figure 5-5.

click to expand
Figure 5-5: New method added to the end of a module

Now you will create a new method for your library of reusable methods. Again, you will be able to use this one in the future. To create the reusable method, simply remove the hard code that adds Function Test and add a parameter to the procedure definition in which you will encapsulate the code (see Listing 5-11). Obviously, you will make this a Shared procedure so that it can be called from anywhere in the project.

Listing 5-11: AddMethodToEndOfDocument

start example
     Shared Sub AddMethodToEndOfDocument(ByVal NewMethod As String)         Dim objTD As TextDocument = oVB.ActiveDocument.Object         Dim objEP As EditPoint = objTD.EndPoint.CreateEditPoint         ' We are past the end of the last line of the document         ' move back in front of the End Module/Class         objEP.LineUp(1)         objEP.Insert(NewMethod)     End Sub 
end example

Finally, a typical call to create a new method is shown in the following code. Although this is no more creative than the original example, you will go on to add real, usable functionality to your add-in.

     AddMethodToEndOfDocument(" Public Function Test()" & vbCr & _                              " ' test comment" & vbCr & _                              " End Function" & vbCr) 



 < Free Open Study > 



Writing Add-Ins for Visual Studio  .NET
Writing Add-Ins for Visual Studio .NET
ISBN: 1590590260
EAN: 2147483647
Year: 2002
Pages: 172
Authors: Les Smith

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