Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8274465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:44:23+00:00 2026-06-08T07:44:23+00:00

If I were to have something like: namespace SomeNameSpace { #region Usings using System.Collections.Generic;

  • 0

If I were to have something like:

namespace SomeNameSpace
{
    #region Usings

    using System.Collections.Generic;
    using System.Security;

    #endregion

    /// <summary> Implements a dictionary with several keys.                               </summary>
    /// <typeparam name="Value"> What type of elements we will be storing in the dictionary</typeparam>
    public class MultiKeyDic<Value>
    {

         /// <summary>  a very long summary that can be
         /// collapsed and expanded. </summary>
         public int SomeInt {get;set;}


         public void someMethod()
         {

         }
    }
}

How can I create a macro that will find all the places that can be expandable (Nodes). If I would like to collapse all the nodes I will have to collapse the nodes in the order of someMethod(), summary of SomeInt , class MultiKeyDic, summary of class MultiKeyDic, #region Usings and finally namespace.

I know the command ctrl+M+O collapses everything, but I do not want to collapse everything. For example I might not want to collapse the comments. If I collapse everything and then expand the comments that expands the parent node too.


So far I have created this macro that will find most of the nodes:

Sub VisitAllNodes()
    Dim i As Integer
    Dim fileCM As FileCodeModel
    Dim elts As EnvDTE.CodeElements
    Dim elt As EnvDTE.CodeElement

    fileCM = DTE.ActiveDocument.ProjectItem.FileCodeModel
    elts = fileCM.CodeElements
    For i = 1 To elts.Count
        elt = elts.Item(i)
        CollapseE(elt, elts, i)
    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)
    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)) Then
        Dim i As Integer
        Dim mems As EnvDTE.CodeElements

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

            Dim temp As EnvDTE.CodeElement = mems.Item(i)
            Dim t As String = [Enum].GetName(GetType(EnvDTE.vsCMElement), temp.Kind)
            MsgBox("Found member (" & t & ") at line# " & temp.StartPoint.Line)

            CollapseE(mems.Item(i), mems, i)
        Next
    ElseIf (elt.Kind = EnvDTE.vsCMElement.vsCMElementNamespace) Then
        Dim i As Integer
        Dim mems As EnvDTE.CodeElements

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

            Dim temp As EnvDTE.CodeElement = mems.Item(i)
            Dim t As String = [Enum].GetName(GetType(EnvDTE.vsCMElement), temp.Kind)
            MsgBox("Found member (" & t & ") at line# " & temp.StartPoint.Line)

            CollapseE(mems.Item(i), mems, i)
        Next
    End If

    'Return
    ' collapse the element

    If (epStart.LessThan(epEnd)) Then
        loc = loc + 1
        If (loc <= elts.Count) Then
            epEnd.MoveToPoint(elts.Item(loc).GetStartPoint(vsCMPart.vsCMPartHeader))
            epEnd.LineUp()
            epEnd.EndOfLine()
        End If
        epStart.OutlineSection(epEnd)
    End If
End Sub

It looks more complicated than what it is. Run it on any document and it will display all the properties, classes, enums, etc., but for some reason it does not find the comments nor regions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:44:24+00:00Added an answer on June 8, 2026 at 7:44 am

    the first Function IncludeMember is used to determine what type of members to exclude. for example in this example I do not collapse namespaces and using directives:

    ' 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.vsCMElementNamespace 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()
    
            ActivateWorkingWindow()
    
            ' go in reverse order so that we can collapse nested regions
            For i = targetLines.Count - 1 To 0 Step -1
    
    
                DTE.ExecuteCommand("Edit.Goto", 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have something like this in my code: namespace A { namespace B {
Let's say I have something like this: <?php namespace Twitter; class Twitter { function
I have something like this: namespace MyNamespace { public partial class MyClass: UserControl {
I do something like this and get errors from compiler using namespace std; ///define
i have an object that is something like this with a full namespace. MyDomainModel.Model.Application
Using VB.net, I have a namespace which I'd like to rename for the future.
I have a WPF application like this. namespace WpfApplication1 { /// <summary> /// Interaction
I have something like this: #include <iostream> namespace N { typedef std::pair<int, double> MyPair;
I have something like this: function showFunction () { // need position and place
I have something like the code below: for(int i=0;i<10;i++){ button=new JButton(buttons[i]); button.addActionListener(new ActionListener(){ public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.