Custom Wrap Macro for Visual Studio

This content may be moving. Please update your bookmark to http://vdir.us/go/vs-wrap-macro.

As an update to my last post, Using VS Macros to Speed Up Text Entry, here is a block of code that allows you to enter any string as the HTML/XML node name that wraps the current selection. Note that you’ll need to setup the Constants class to hold the shared variable.

Public Class Constants
    Public Shared CustomDefaultValue As String = "p"
End Class

Public Module BasicMacros
    Public Sub WrapInCustom()
        ' Get the input
        Dim str As String = InputBox("Wrap the selection in what?", "Wrap in Custom", Constants.CustomDefaultValue)

        ' Empty string == cancel!
        If (String.IsNullOrWhiteSpace(str)) Then Return

        ' Store the value so that it is easy to repeat
        Constants.CustomDefaultValue = str

        ' Get the current selection
        Dim ts As TextSelection = CType(DTE.ActiveDocument.Selection, TextSelection)

        ' Add the value
        ts.Text = "<" + str + ">" + ts.Text + ""
    End Sub
End Module

One thought on “Custom Wrap Macro for Visual Studio

  1. Wouldn’t you want to escape any characters in the ts.Text if they are special characters to HTML? For example convert the character “<" in the text to "&lt".

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>