Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collapse all subs & functions in VS.net

Status
Not open for further replies.

shatch

Programmer
Jul 9, 2004
346
GB
Hi there

I dont know if its useful for everyone, but its something that i find so useful, especially on large classes and pages of code. It just collapses all of the functions (like regions) so you can just see the declaration.
Its esentially a macro which you can add to the VS IDE, steps below

1. Go to tools, macros, macro IDE

2. In My Macros create a module and insert the following code:

<CODE>
Public Sub CloseAllRegions()
ToggleAllRegions("C")
End Sub
Public Sub OpenAllRegions()
ToggleAllRegions("O")
End Sub
Public Sub ToggleAllRegions(ByVal Switch As String)
' in walking up through the code, if we do not see
' #endregion/end region before we get to the
' #region, then the region was closed, otherwise
' it was already open, and each time a #region is
' found, set open = false as we are only looking
' for closed regions and if we toggle an open region
' it will close...
Dim cmd As String = "Edit.ToggleOutliningExpansion"

Try
Dim open As Boolean = False
Dim ts As TextSelection = DTE.ActiveDocument.Selection
ts.EndOfDocument()
Dim ep As EditPoint = ts.ActivePoint.CreateEditPoint
Dim line As String

Do While Not ep.AtStartOfDocument
ts.StartOfLine()
ts.LineUp(True)
line = ts.Text.ToLower.Trim
If line.StartsWith("end sub") Or _
line.StartsWith("end function") Then
open = True
ElseIf line.IndexOf("sub ") Or line.IndexOf("function ") Then
If Switch = "C" Then
If open Then
DTE.ExecuteCommand(cmd)
End If
Else
If Not open Then
DTE.ExecuteCommand(cmd)
End If
End If
open = False
End If
ep = ts.ActivePoint.CreateEditPoint
Loop
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Sub
</CODE>

Then, you can run these commands from the macro explorer and neaten up your code :)
 
Or, you can just press:

<CTRL> M - O to collapse

-or-

<CTRL> M - P to un-collapse

Nearly all VS items already have shortcuts defined. Take a walk through your menus, and you'll see the shortcuts listed beside the options. To your point, though, it is possible to write macros to do anything that isn't already available as a shortcut.

;-)

-p

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Paul has a point that alot of shortcuts are already built in, but its nice to also know that you can specify custom macros for advanced functionality. For instance:

<CTRL> M-O will collaps to the region level.

<CTRL> M-P seems to only take away the ability to collaps at the highest level.

<CTRL> M-M will toggle, but you need your cursor on the region/sub/function that you want to open/collapse (so it does what clicking on the plus icon does pretty much).

So if you want to be able to only collapse subs/functions, but you still want to be able to view them without collapsing all the way to the region, then Shatch's code is the right way to go.

Shatch: I tried your code, but had some difficulty getting it to work 100%. I ended up modifying it a bit, and here's what I came up with. The only thing it doesn't do is un-collapse the subs/functions, but that's not any functionality that I'd be looking for anyway. I also took it a step further and created a new toolbar and assigned buttons to each of the functions, so instead of having to bring up the macro ide you can just click a button (hey, and assign a shortcut key if you want) to launch it. Here's the code below:
Code:
 Public Sub ToggleSubsFunctions()

        Dim cmd As String = "Edit.ToggleOutliningExpansion"

        Try
            Dim open As Boolean = False
            Dim ts As TextSelection = DTE.ActiveDocument.Selection
            ts.EndOfDocument()
            Dim ep As EditPoint = ts.ActivePoint.CreateEditPoint
            Dim line As String

            Do While Not ep.AtStartOfDocument
                ts.StartOfLine()
                ts.LineUp(True)
                line = ts.Text.ToLower.Trim
                If line.StartsWith("end sub") Or _
                    line.StartsWith("end function") Then
                    open = True
                ElseIf line.IndexOf("sub ") Or line.IndexOf("function ") Then
                    If open Then
                        DTE.ExecuteCommand(cmd)
                        open = False

                    End If
                End If
                ep = ts.ActivePoint.CreateEditPoint
            Loop
        Catch ex As System.Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Sub ToggleRegions()
        Dim cmd As String = "Edit.ToggleOutliningExpansion"

        Try
            Dim ts As TextSelection = DTE.ActiveDocument.Selection
            ts.EndOfDocument()
            Dim ep As EditPoint = ts.ActivePoint.CreateEditPoint
            Dim line As String

            Do While Not ep.AtStartOfDocument
                ts.StartOfLine()
                ts.LineUp(True)
                line = ts.Text.ToLower.Trim
                If line.IndexOf("windows form designer generated code") > 0 Then
                    'do nothing
                ElseIf line.StartsWith("#region") Then
                    DTE.ExecuteCommand(cmd)
                End If
                ep = ts.ActivePoint.CreateEditPoint
            Loop
        Catch ex As System.Exception
            MsgBox(ex.Message)
        End Try
    End Sub

D'Arcy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top