在Visual Studio中删除匹配的花括号 [英] Delete Matching Braces in Visual Studio

查看:228
本文介绍了在Visual Studio中删除匹配的花括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Visual Studio I can jump from/to opening/closing brace with the Control+] shortcut.

是否有一个快捷方式可以让我立即删除两个大括号(也许使用宏/扩展名)?

Is there a shortcut that will allow me to delete both braces at once (maybe with a macro/extension)?

例如

foo = ( 1 + bar() + 2 );

当我在第一个大括号中时,我想删除它及其匹配的大括号以获取

When I am on the first opening brace I would like to delete it and its matching brace to get

foo = 1 + bar() + 2;

推荐答案

它不像JaredPar所建议的那样简单,但我也不是Macro专家.

It's not quite as simple as JaredPar suggested but I'm no Macro expert either.

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

This works for (), {} and []

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

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

这篇关于在Visual Studio中删除匹配的花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆