2009-08-09 43 views
13

有沒有一種方法(設置?「宏」?擴展?),我可以簡單地切換大綱,以便只有使用部分和我的方法摺疊他們的簽名行,但我的意見(總結和雙斜槓評論)和課程保持擴大?Visual Studio:摺疊方法,但不是評論(總結等)

實例:

1)未收縮

using System; 
using MachineGun; 

namespace Animals 
{ 

    /// <summary> 
    /// Angry animal 
    /// Pretty Fast, too 
    /// </summary> 
    public partial class Lion 
    { 
     // 
     // Dead or Alive 
     public Boolean Alive; 

     /// <summary> 
     /// Bad bite 
     /// </summary> 
     public PieceOfAnimal Bite(Animal animalToBite) 
     { 
       return animalToBite.Shoulder; 
     } 

     /// <summary> 
     /// Fatal bite 
     /// </summary> 
     public PieceOfAnimal Kill(Animal animalToKill) 
     { 
       return animalToKill.Head; 
     } 
    } 
} 

2)摺疊(下面是我所希望的結果):

using[...] 

namespace Animals 
{ 

    /// <summary> 
    /// Angry animal 
    /// Pretty Fast, too 
    /// </summary> 
    public partial class Lion 
    { 
     // 
     // Dead or Alive 
     public Boolean Alive; 

     /// <summary> 
     /// Bad bite 
     /// </summary> 
     public PieceOfAnimal Bite(Animal animalToBite)[...] 

     /// <summary> 
     /// Fatal bite 
     /// </summary> 
     public PieceOfAnimal Kill(Animal animalToKill)[...] 
    } 
} 

這是我更喜歡看到我類文件(摺疊形式)。到目前爲止,我一直在做100萬次的崩潰,我認爲應該有一種方法來自動化/定製/擴展VS,以便按照我的要求進行操作。每當我調試/命中一個斷點時,它都會使事情崩潰和混亂。如果我通過上下文菜單的摺疊進行摺疊來勾勒等等,它也會摺疊我不想要的評論。

感謝您的幫助!

回答

8

Ctrl M,Ctrl O

摺疊到定義。

從這一點,宏可能不會太難寫。

就像找到/// <summary> ...並切換大綱。然後起泡,沖洗,重複。

0

Visual Studio沒有內置任何內容,它允許您以此方式摺疊代碼區域。這可能是一個宏,但我認爲寫起來不會很簡單。 Visual Studio 2010可以通過允許您編寫一個實際的插件,讓語法分析器具有更直接的可訪問性,從而提供一些解決方案,但這是純粹的推測。

3

我創建了一個可以摺疊成員的宏。您可以將您的自定義過濾器在功能IncludeMember例如,在這個例子中,我崩潰的一切,但意見和枚舉

' filter some mebers. for example using statemets cannot be collapsed so exclude them. 
Function IncludeMember(ByVal member As EnvDTE.CodeElement) 

    If member.Kind = vsCMElement.vsCMElementIDLImport Then 
     Return False 
    ElseIf member.Kind = vsCMElement.vsCMElementEnum Then 
     Return False ' I do not want to colapse enums 
    End If 

    Return True 

End Function 

Sub CollapseNodes() 

    ' activate working window 
    DTE.Windows.Item(DTE.ActiveDocument.Name).Activate() 

    ' expand everything to start 

    Try 
     DTE.ExecuteCommand("Edit.StopOutlining") 
    Catch 
    End Try 

    Try 
     DTE.ExecuteCommand("Edit.StartAutomaticOutlining") 
    Catch 
    End Try 


    ' get text of document and replace all new lines with \r\n 
    Dim objTextDoc As TextDocument 
    Dim objEditPt As EnvDTE.EditPoint 
    Dim text As String 
    ' Get a handle to the new document and create an EditPoint. 
    objTextDoc = DTE.ActiveDocument.Object("TextDocument") 
    objEditPt = objTextDoc.StartPoint.CreateEditPoint 
    ' Get all Text of active document 
    text = objEditPt.GetText(objTextDoc.EndPoint) 
    text = System.Text.RegularExpressions.Regex.Replace(_ 
        text, _ 
        "(\r\n?|\n\r?)", ChrW(13) & ChrW(10) _ 
       ) 

    ' add new line to text so that lines of visual studio match with index of array 
    Dim lines As String() = System.Text.RegularExpressions.Regex.Split(vbCrLf & text, vbCrLf) 

    ' list where whe will place all colapsable items 
    Dim targetLines As New System.Collections.Generic.List(Of Integer) 

    ' regex that we will use to check if a line contains a #region 
    Dim reg As New System.Text.RegularExpressions.Regex(" *#region(|$)") 

    Dim i As Integer 
    For i = 1 To lines.Length - 1 

     If reg.Match(lines(i)).Success Then 
      targetLines.Add(i) 
     End If 

    Next 


    Dim fileCM As FileCodeModel 
    Dim elts As EnvDTE.CodeElements 
    Dim elt As EnvDTE.CodeElement 

    Dim projectItem = DTE.ActiveDocument.ProjectItem 

    Dim temp = projectItem.Collection.Count 

    Dim b = DirectCast(DirectCast(projectItem.Document, EnvDTE.Document).ActiveWindow, EnvDTE.Window).ContextAttributes 

    fileCM = projectItem.FileCodeModel 
    elts = fileCM.CodeElements 
    For i = 1 To elts.Count 
     elt = elts.Item(i) 
     CollapseE(elt, elts, i, targetLines) 
    Next 

    ' now that we have the lines that we will plan to collapse sort them. it is important to go in order 
    targetLines.Sort() 

    ' go in reverse order so that we can collapse nested regions 
    For i = targetLines.Count - 1 To 0 Step -1 

     GotoLine(targetLines(i) & "") 
     DTE.ExecuteCommand("Edit.ToggleOutliningExpansion") 

    Next 


End Sub 

'' Helper to OutlineCode. Recursively outlines members of elt. 
'' 
Sub CollapseE(ByVal elt As EnvDTE.CodeElement, ByVal elts As EnvDTE.CodeElements, ByVal loc As Integer, ByRef targetLines As System.Collections.Generic.List(Of Integer)) 
    Dim epStart As EnvDTE.EditPoint 
    Dim epEnd As EnvDTE.EditPoint 

    epStart = elt.GetStartPoint(vsCMPart.vsCMPartWholeWithAttributes).CreateEditPoint() 
    epEnd = elt.GetEndPoint(vsCMPart.vsCMPartWholeWithAttributes).CreateEditPoint() ' Copy it because we move it later. 
    epStart.EndOfLine() 
    If ((elt.IsCodeType()) And (elt.Kind <> EnvDTE.vsCMElement.vsCMElementDelegate) Or elt.Kind = EnvDTE.vsCMElement.vsCMElementNamespace) Then 
     Dim i As Integer 
     Dim mems As EnvDTE.CodeElements 

     mems = elt.Members 
     For i = 1 To mems.Count 

      CollapseE(mems.Item(i), mems, i, targetLines) 

     Next 

    End If 


    If (epStart.LessThan(epEnd)) Then 
     If IncludeMember(elt) Then 
      targetLines.Add(epStart.Line) 
     End If 
    End If 



End Sub 
+2

這正是我需要的,但我不知道如何使用宏。你能幫我嗎?我在哪裏放這個代碼,以及如何執行它?我正在使用Visual Studio 2012。 – 2015-07-02 12:16:05

0

我知道這個問題是超級老了,但我一直在尋找一種方法來此做自己的作品與2015年VS我遇到了這個宏的Visual Studio擴展,在VS 2013和2015年的作品...

https://marketplace.visualstudio.com/items?itemName=VisualStudioPlatformTeam.MacrosforVisualStudio

我寫了這個宏,摺疊所有方法,但留下的總結評論,使用指令,類等等。

/// <reference path="C:\Users\johnc\AppData\Local\Microsoft\VisualStudio\14.0\Macros\dte.js" /> 
var selection = dte.ActiveDocument.Selection; 

dte.ExecuteCommand("Edit.ExpandAllOutlining"); 
dte.ActiveDocument.Selection.StartOfDocument(); 
dte.ExecuteCommand("Edit.NextMethod"); 

var startLine = selection.CurrentLine; 
dte.ExecuteCommand("Edit.CollapseCurrentRegion"); 
dte.ExecuteCommand("Edit.NextMethod"); 

do { 
    dte.ExecuteCommand("Edit.CollapseCurrentRegion"); 
    dte.ExecuteCommand("Edit.NextMethod"); 
} while (startLine != selection.CurrentLine); 

希望這可以幫助別人。

0

擴展John'sanswer爲VS2017:

var selection = dte.ActiveDocument.Selection; 

dte.ExecuteCommand("Edit.CollapsetoDefinitions"); 
dte.ActiveDocument.Selection.StartOfDocument(); 
dte.ActiveDocument.Selection.FindText("/// <summary>") 

var startLine = selection.CurrentLine; 
do { 
    dte.ExecuteCommand("Edit.FindNext"); 
} while (startLine != selection.CurrentLine);