2012-09-27 61 views
0

我將通過一個例子證明我想:如何刪除塊但在Visual Studio中保留內容?

一部開拓創新的狀態:

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的這種變化命令或者一個宏。所以不要通過手工刪除花括號和使用線。這可能嗎?

回答

0

沒有一個命令,但是這會工作(需要ReSharper的):

  • 刪除 「使用」 行(轉變 + 德爾
  • 把光標放在{和命中alt + enter
  • 選擇「移除支架」。
+0

這是很棒:)。 shift + del,結束,alt +回車,輸入。難道不可能在這4個步驟中寫一個宏嗎? –

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

相關問題