|
|
Add-ins: Retrieving Code From a Window and Putting It Back | | How do I retrieve the selected code from a Code Window, or Text Document, modify it, and replace the selection in the window? This is one of the most elementary, but often used features required in an add-in. The code developed in this article will provide that functionality.
First, I will show the code for retrieving the selected block of code or text. The only requirement to use the following code is that oVS be set to the application object of the IDE. The function returns the selected block of code. The TextSelection object is a fundamental object for manipulating text in a code window. You can look up this object in MSDN to see its many methods and properties. I also deal extensively with the TextSelection and EditPoint objects in my book on Writing Add-ins for Visual Studio .NET, published by Apress. You can purchase it very reasonably at Amazon.Com.
Public Function GetCodeFromWindow() As String
Dim selCodeBlock As TextSelection
Try
selCodeBlock = CType(oVS.ActiveDocument.Selection(), _
EnvDTE.TextSelection)
GetCodeFromWindow = selCodeBlock.Text
Catch ex As System.Exception
' Provide your error handling code
End Try
End Function
|
Once you have the code, you can modify it as required and call the following function to replace the selection in the code window with your modified code.
Friend Sub PutCodeBack(ByVal s As String)
Dim selCodeBlock As TextSelection
Try
selCodeBlock = CType(oVS.ActiveDocument.Selection(), _
EnvDTE.TextSelection)
selCodeBlock.Delete()
selCodeBlock.Insert(s, 1)
' This code will reindent the entire document
' after an insertion to ensure it is indented properly
' it will only work for VB, as the C# editor only has
' the ability to format a selection, and placing the
' code back in the window loses the selection
Try
oVS.ExecuteCommand("Edit.FormatDocument")
Catch
End Try
Catch ex As System.Exception
' Provide your error handling code
End Try
End Sub
|
There is something else you probably want to consider. Some programmers actually don't know how to select a block of code properly, which is a pet peeve of mine, as an add-in developer. The proper way to select a block of code is to place the cursor in the left margin of the window, so that the cursor becomes and "arrow", and drag it down to select all of the lines that they desire to select. However, for some strange reason, some programmers, especially beginners, or those who have not advanced, as they should, will place the cursor at the end of a line and drag "backwards" to the beginning of the line or to the start of a block. If they do this, the last line of the selected block will probably not have the new line characters (vbCrLF) at the end of the line. This can mess up your operation. The following function can be called, passing the retrieved code block, and it will guarantee that your block ends with the proper new line characters.
Public Function IsStringTerminatedByCRLF(ByVal s As String) _
As String
' Returns passed string with a crlf appended
' if the last char is not
' a cr,lf, or crlf, otherwise returns s.
Dim sLC As String = s.Substring(s.Length - 1, 1)
If sLC = vbCr Or sLC = vbLf Then
Return s
Else
Return s & vbCrLf
End If
End Function
|
All three of the methods shown above can be used in a macro instead of an add-in. You only need replace any reference to the application object (oVS) with DTE. Also, the two functions would have to be changed to Subs or be called by a Sub macro, because a Macro Function cannot be called directly by running it from the Macro Explorer.
Back to Top
|
|