2009-08-09 8 views
13

c'è un modo (impostazioni? "Macro"? Estensione?) Che posso semplicemente attivare la struttura in modo che solo la sezione di utilizzo ei miei metodi comprimi in la loro linea di firma, ma i miei commenti (riassunto e doppio taglio commenti) e le classi rimangono espansi?Visual Studio: metodi di compressione, ma non commenti (riepilogo ecc.)

Esempi:

1) Uncollapsed

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) Collapsed (il seguente è il mio risultato desiderato):

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)[...] 
    } 
} 

Questo è come io preferisco vedere il mio file di classe (il modulo compresso). Ho fatto il crollo a mano un milione di volte ormai e penso che ci dovrebbe essere un modo per automatizzare/personalizzare/estendere VS per farlo nel modo che voglio?

Ogni volta che eseguo il debug/colpisco un punto di interruzione, si annulla e incasina le cose. Se crollo tramite il collasso del menu contestuale per delineare ecc., Anche i miei commenti vengono compressi, cosa che non è desiderata.

Apprezzo il tuo aiuto!

risposta

8

Ctrl M, Ctrl O

Crolli alle definizioni.

Da questo punto una macro potrebbe non essere troppo difficile da scrivere.

Qualcosa come trovare /// <summary> ... e attivare la struttura. Quindi schiuma, risciacqua, ripeti.

0

Non c'è niente di integrato in Visual Studio che ti permetta di comprimere le regioni di codice in questo modo. Potrebbe essere possibile con una macro, ma non penso che sarebbe molto semplice scrivere. Visual Studio 2010 potrebbe fornire un certo sollievo consentendo di scrivere un plug-in effettivo che ha una maggiore accessibilità diretta al parser della sintassi, ma questa è pura speculazione a questo punto.

3

Ho creato una macro che consentirà di comprimere i membri. è possibile inserire il filtro personalizzato nella funzione IncludeMember per esempio in questo esempio ho crollo tutto, ma i commenti e le enumerazioni

' 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

Questo è esattamente quello che mi serve, ma non ho idea di come utilizzare la macro. Puoi aiutarmi? Dove inserisco questo codice e come eseguirlo? Sto usando Visual Studio 2012. –

0

So che questa domanda è super vecchio, ma stavo cercando un modo per fare questo io stesso che funziona con VS 2015. mi sono imbattuto in questa macro per Visual Studio estensione che funziona con VS 2013 e il 2015 ...

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

ho scritto questa macro che crolla tutti i metodi, ma lascia i commenti di sintesi, utilizzando le direttive, le classi , ecc. solo.

/// <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); 

Spero che questo aiuti qualcuno a uscire.

0

Estendere John'sanswer per 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); 
Problemi correlati