如何在Visual Studio中移动自动完成的结束标记

Hug*_*ugh 10 html autocomplete visual-studio

我想让Visual Studio将自动完成的结束标记向右移动一个单词(或更多).例如,给定以下HTML:

<p>I need to emphasize some text.</p>
Run Code Online (Sandbox Code Playgroud)

如果我<em>在"强调"一词之前键入,Visual Studio会自动填充如下:

<p>I need to <em></em>emphasize some text.</p>
Run Code Online (Sandbox Code Playgroud)

然后我需要移动结束</em>以获得我想要的东西:

<p>I need to <em>emphasize</em> some text.</p>
Run Code Online (Sandbox Code Playgroud)

有没有办法让Visual Studio自动完成最后一步?

w4g*_*n3r 6

你的问题让我想到如果存在这种功能会有多酷.幸运的是,在VS中实现宏非常简单.下面是宏的代码.您可以使用VS中的自定义工具轻松将其绑定到CTRL + ALT + Right.

(注意:我只是把它扔得很快,因为它是星期五晚上)

Sub MoveClosingTag()
    Dim ts As EnvDTE.TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
    Dim start As EditPoint = ts.ActivePoint.CreateEditPoint()
    Dim tag As String

    ts.WordRight(True)
    If ts.Text = "</" Then
        Do Until ts.ActivePoint.AtEndOfLine
            ts.CharRight(True)
            If ts.Text.EndsWith(">") Then Exit Do
        Loop
        tag = ts.Text
        If tag.EndsWith(">") Then
            ts.Delete()
            ts.WordRight(False)
            ts.Insert(tag, EnvDTE.vsInsertFlags.vsInsertFlagsCollapseToStart)
        Else
            ts.MoveToPoint(start)
        End If
    Else
        ts.MoveToPoint(start)
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)