2009-08-09 5 views
13

czy istnieje sposób (ustawienia? "Makro"? Rozszerzenie?), Które można po prostu przełączać, tak, aby sekcja użycia i moje metody zwinięte do ich linia podpisu, ale moje komentarze (komentarze sumaryczne i podwójne slash) i klasy pozostają rozszerzone?Visual Studio: metody zwinięcia, ale nie komentarze (podsumowanie itp.)

Przykłady:

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) złożonym stanie (co następuje mój pożądany rezultat):

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

ten sposób wolę widząc moje pliki klas (zwinięty formularz). Robię to już ręcznie milion razy i wydaje mi się, że powinien istnieć sposób automatyzacji/dostosowania/rozszerzania VS tak, jak chcę?

Za każdym razem, gdy debuguję/uderzam punkt przerwania, niepowiadomienie i pomieszanie rzeczy. Jeśli zwinę za pomocą zwijania menu kontekstowego do konspektu itp., To również zapakuje moje komentarze, które nie są pożądane.

Doceń swoją pomoc!

Odpowiedz

8

Ctrl M Ctrl O

zapada się definicje.

Od tego momentu makro może nie być zbyt trudne do napisania.

Coś jak znajdź /// <summary> ... i przełącz konspekt. Następnie spienić, spłukać, powtórzyć.

0

Nie ma nic wbudowanego w Visual Studio, które pozwoli ci zwinąć w ten sposób regiony kodu. To może być możliwe z makrem, ale nie sądzę, że byłoby to bardzo proste do napisania. Visual Studio 2010 może przynieść ulgę, umożliwiając napisanie rzeczywistej wtyczki, która ma bardziej bezpośredni dostęp do parsera składni, ale jest to czysta spekulacja w tym momencie.

3

Utworzyłem makro, które umożliwi zwijanie członków. można umieścić swój własny filtr w funkcji IncludeMember na przykład w tym przykładzie zwinąć wszystko, ale komentarze i teksty stałe

' 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

To jest dokładnie to, czego potrzebuję, ale nie mam pojęcia, jak korzystać z makra. Czy możesz mi pomóc? Gdzie mogę umieścić ten kod i jak go wykonać? Używam Visual Studio 2012. –

0

znam to pytanie jest bardzo stary, ale szukałem sposobu, aby to zrobić sam, który działa z VS 2015 natknąłem się na ten makr Visual Studio rozszerzenia, który działa z VS 2013 i 2015 ...

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

pisałem to makro, które załamuje wszystkich metod, ale pozostawia komentarze podsumowania za pomocą dyrektyw, zajęcia , itp. sam.

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

Mam nadzieję, że to pomoże komukolwiek.

0

Rozszerzanie John'sanswer dla 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); 
Powiązane problemy