【问题标题】:How to delete a block but keep the content in Visual Studio?如何删除块但将内容保留在 Visual Studio 中?
【发布时间】:2012-09-27 07:45:26
【问题描述】:

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

原始状态:

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 命令或宏来实现此更改。所以不要通过手动删除大括号和使用行。这可能吗?

【问题讨论】:

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


    【解决方案1】:

    不是单个命令,但这会起作用(需要 Resharper):

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

    【讨论】:

    • 太好了:)。 shift+del,结束,alt+enter,回车。难道这4个步骤都不能写宏吗?
    【解决方案2】:

    如果大括号和 using 块在不同的行中,这个宏也可以工作,只需要添加一个快捷方式:

    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

    【讨论】:

      猜你喜欢
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      • 2017-01-12
      • 2012-03-15
      • 2021-11-26
      • 2020-10-20
      • 2018-11-04
      • 2011-01-02
      相关资源
      最近更新 更多