KnowDotNet NetRefactor

Add-in: Adding a Method to the End of a Document

by Les Smith
Print this Article Discuss in Forums

How do I add a new method to the end of a document in an add-in that is manipulating a VB.NET project?  In a C# project the CodeModel object has a AddMethod method, but it does not work for a VB project.  In this article, I will show you how to add a method to the end of a VB code window.

The method that I have written will make up for the missing functionality in the CodeModel object in VB applications.  The only requirement for using this method in an add-in is to have an application object named oVS.  It expects to have the method, to be added, passed in as a string variable.

This method uses the EditPoint object.  If you are not familiar with this object, it provides the functionality to manipulate code in the edit buffer that exists behind the visible code window.  Using this functionality, you can avoid the flash of the TextDocument object.

This method does several things.  First, it creates an EditPoint object at the end of the document.  It then determines whether it is working in a Namespace (always in C#), or a simple Class (VB could have a Namespace).  Based on the type of code, which it determines by testing the FileType of the document.  You can do this by checking the file extension of the current documents object filename.  Once the code ensures that it is positioned above the end of the class, it then moves to the start of the line and inserts the new method.

This code can be used in a macro by changing any reference to
oVS with DTE.

   Sub AddMethodToEndOfDocument(ByVal _
      NewMethod
As String)
      
Try
         Dim td As TextDocument = oVS.ActiveDocument.Object
        
Dim ep As EditPoint = td.EndPoint.CreateEditPoint
        
Dim sLine As String

         ' We are past the end of the last line of the document
         ' move back in front of the End Module/Class
         ep.LineUp(1)

        
' if a namespace exists, or we are c#
         ' we must get within the namespace or
         ' the class braces
         If modMain.FileType = 9 Then
            ep.LineUp(1)
        
Else
            ' if vb and there is a namespace, we must
            ' move up one more line
            Do
               sLine = ep.GetText(ep.LineLength)
              
If sLine.Trim.StartsWith("End Class") Then
                  Exit Do
               End If
               ep.LineUp(1)
            
Loop
         End If
         ep.StartOfLine()

        
' now, reformat the document, only works for
         ' vb, as c# editor does not have a FormatDocument
         ' command, only a formatselection...
         Try
            oVS.ExecuteCommand("Edit.FormatDocument")
        
Catch
         End Try
      Catch ex As System.Exception
        
' put error handlng code here
      
End Try
   End Sub

Back to Top

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing