Macro to wrap selected text with tags in Visual Studio

百般思念 提交于 2019-12-30 04:33:08

问题


I realize that I may be being a bit lazy, but does anyone know of a Visual Studio macro, where I can select some text inside of the Visual Studio IDE, click a button, and have it wrap the selected text with tags? It would generate something like:

<strong>My Selected Text</strong>

I would even be up for creating a macro, just not sure where to exactly start!


回答1:


The code to do so is rather simple:

Sub SurroundWithStrongTag()
    DTE.ActiveDocument.Selection.Text = "<strong>" + DTE.ActiveDocument.Selection.Text + "</strong>"
End Sub

Now, if you don't know much about macros here's how to add it:

  • First you need open the macros IDE, click Tools->Macros->Macros IDE...
  • Next, we will add a module for your custom macros. Right click on "MyMacros" in the Project Explorer, click Add->Add Module..., type in an appropriate name then click "Add".
  • Now paste the function inside the module, making copies for any other tags you want
  • Save and close the macros IDE

To hook the macro up to a button:

  • Click Tools->Customize...
  • Click New..., type in an appropriate name, click OK. An empty toolbar should be visible (you may have to move the window to see it)
  • Click the Commands tab, and select "Macros" in categories
  • Find the macros created before and drag them over to the toolbar
  • Right click the buttons to change settings (such as displaying an icon instead of text)



回答2:


I know this is an old topic, but maybe someone finds this useful.

I have the following set up:

Sub WrapInH1()
    WrapInTag("h1")
End Sub

Sub WrapInP()
    WrapInTag("p")
End Sub

Sub WrapInStrong()
    WrapInTag("strong")
End Sub

Sub WrapInTag()
    WrapInTag("")
End Sub

Sub WrapInTag(ByVal tagText As String)
    EnableAutoComplete(False)

    If tagText.Length = 0 Then
        tagText = InputBox("Enter Tag")
    End If

    Dim text As String
    text = DTE.ActiveDocument.Selection.Text
    text = Regex.Replace(text, vbCrLf & "$", "") 'Remove the vbCrLf at the end of the line, for when you select the line by clicking in the margin, otherwise your closing tag ends up on it's own line at the end...

    DTE.ActiveDocument.Selection.Text = "<" & tagText & ">" & text & "</" & tagText & ">" & vbCrLf
    EnableAutoComplete(True)
End Sub

Private Sub EnableAutoComplete(ByVal enabled As Boolean)
    Dim HTMLprops As Properties
    Dim aProp As EnvDTE.Property
    HTMLprops = DTE.Properties("Texteditor", "HTML Specific")
    aProp = HTMLprops.Item("AutoInsertCloseTag")
    aProp.Value = enabled
End Sub



回答3:


Dim HTMLprops As Properties = DTE.Properties("Texteditor", "HTML Specific")

Dim aProp As EnvDTE.Property = HTMLprops.Item("AutoInsertCloseTag")

aProp.Value = False



回答4:


Original answer

If you want an out of the box solution, Visual Studio 2015 comes with a new shortcut, Shift+Alt+W wraps the current selection with a div. This shortcut leaves the text "div" selected, making it seamlessly changeable to any desired tag. This coupled with the automatic end tag replacement makes for a quick solution.

Example

Shift+Alt+W > strong > Enter


来源:https://stackoverflow.com/questions/598062/macro-to-wrap-selected-text-with-tags-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!