如何删除块但保留Visual Studio中的内容?

时间:2012-09-27 07:45:27

标签: visual-studio-2010 visual-studio resharper

我将通过一个我想要的例子来展示:

原始状态:

using (var myobject = new DisposableObject())
{
  //some code that no longer use myobject
  int x = 5;
  int y = x+5;
}

期望的状态:

  //some code that no longer use myobject
  int x = 5;
  int y = x+5;

我想通过键盘组合或resharper命令或宏来实现此更改。所以不要通过手动删除大括号和使用行。这可能吗?

2 个答案:

答案 0 :(得分:0)

没有一个命令,但这可行(需要Resharper):

  • 删除“使用”行( shift + del
  • 将光标放在{上并点击 alt + 输入
  • 选择“删除大括号”。

答案 1 :(得分:0)

如果大括号和使用块位于不同的行中,这个宏也有效,只需要添加一个快捷方式:

Sub deleteusingblock()
    DTE.ExecuteCommand("Edit.LineDelete")
    DTE.ActiveDocument.Selection.EndOfLine()
    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

(来源:克里斯的回答:Delete Matching Braces in Visual Studio