删除Visual Studio中的匹配大括号

lak*_*tak 8 c# editor visual-studio

在Visual Studio中,我可以使用Control+]快捷方式从/跳到开/关大括号.

是否有一个快捷方式允许我一次删除两个大括号(可能有一个宏/扩展名)?

例如

foo = ( 1 + bar() + 2 );
Run Code Online (Sandbox Code Playgroud)

当我在第一个开口支架上时,我想删除它和它的匹配支架

foo = 1 + bar() + 2;
Run Code Online (Sandbox Code Playgroud)

Jar*_*Par 5

Visual Studio 没有固有的方法来执行此操作。为此,您需要实现一个宏。

如果您选择宏路线,您将需要熟悉该Edit.GoToBrace命令。该命令会将您从当前的大括号跳转到匹配的大括号。请注意,它实际上会在匹配的大括号之后转储您,因此您可能需要向后查找一个字符才能找到要删除的元素。

将其实现为宏的最佳方法是

  • 保存当前插入符位置
  • 执行Edit.GoToBrace
  • 删除插入符号左侧的大括号
  • 删除原始插入符号位置处的大括号


lak*_*tak 3

它并不像 JaredPar 建议的那么简单,但我也不是宏专家。

这适用于 ()、{} 和 []

Sub DeleteMatchingBrace()
    Dim sel As TextSelection = DTE.ActiveDocument.Selection
    Dim ap As VirtualPoint = sel.ActivePoint

    If (sel.Text() <> "") Then Exit Sub
    ' reposition
    DTE.ExecuteCommand("Edit.GoToBrace") : DTE.ExecuteCommand("Edit.GoToBrace") 

    If (ap.DisplayColumn <= ap.LineLength) Then sel.CharRight(True)

    Dim c As String = sel.Text
    Dim isRight As Boolean = False
    If (c <> "(" And c <> "[" And c <> "{") Then
        sel.CharLeft(True, 1 + IIf(c = "", 0, 1))
        c = sel.Text
        sel.CharRight()
        If (c <> ")" And c <> "]" And c <> "}") Then Exit Sub
        isRight = True
    End If

    Dim line = ap.Line
    Dim pos = ap.DisplayColumn
    DTE.ExecuteCommand("Edit.GoToBrace")
    If (isRight) Then sel.CharRight(True) Else sel.CharLeft(True)

    sel.Text = ""
    If (isRight And line = ap.Line) Then pos = pos - 1
    sel.MoveToDisplayColumn(line, pos)
    sel.CharLeft(True)
    sel.Text = ""
End Sub
Run Code Online (Sandbox Code Playgroud)

然后在VS中添加该宏的快捷方式