Click here to Skip to main content
15,884,838 members
Articles / Programming Languages / Visual Basic
Tip/Trick

VS2010 Macro - Collapse Lines of Code Between Comments

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
18 Aug 2013CPOL 10.6K   2   3
Visual Studio 2010 Macro : Collapse all sections of code between 2 lines of comment

Introduction

This code will help you to make your code clear and simple in Visual Studio 2010, quickly.

It creates "collapse zone" around comment lines. Watch below for an example.

It is also an example of creating Macro in Visual Studio 2010 that goes through the code.

Illustration

First, you select your code in Visual Studio:

Then, you execute the procedure HideLineBetweenComments.

Each line between comments will be collapsed (and the last section between a 'comment' and 'End Function').

Background

Knowledge of Visual Studio 2010 and Microsoft Visual Studio Macros is recommended.

Using the Code

The main procedure is HideLineBetweenComments.

All the code should be put in the IDE Macro of VS2010 (Tools>Macros>Macros IDE).

The way I found to go through each line in VS2010 is to use split function with the end-of-line character (LF).

VB.NET
Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module MonModule

    Sub HideLineBetweenComments()

        Dim originalCode As String
        originalCode = DTE.ActiveDocument.Selection.Text

        Dim lines() As String
        lines = Split(originalCode, vbLf)

        Dim line As String
        Dim num_ligne As Integer = 1

        Dim selectionSAcacher()

        Dim RechercheDebut As Boolean = True
        Dim RechercheFin As Boolean = False

        Dim ligne_precedante As String = ""

        Dim num_debut As Integer
        Dim num_fin As Integer

        For Each line In lines

            Dim selectionAcacher(2) As Integer

            If RechercheDebut = True Then

                If Left(Replace(line, Chr(32), ""), 1) = Chr(39) Then

                    RechercheDebut = False
                    RechercheFin = True
                    num_debut = DTE.ActiveDocument.Selection.TopPoint.Line + num_ligne

                    GoTo lignesuivante

                End If

            End If

            If RechercheFin = True Then

                If Left(Replace(line, Chr(32), ""), 1) = Chr(39) And _
                Left(Replace(ligne_precedante, Chr(32), ""), 1) <> Chr(39) Then

                    RechercheDebut = False
                    RechercheFin = True
                    num_fin = DTE.ActiveDocument.Selection.TopPoint.Line + num_ligne - 2

                    selectionAcacher(1) = num_debut
                    selectionAcacher(2) = num_fin

                    ReDim Preserve selectionSAcacher(rUbound(selectionSAcacher) + 1)
                    selectionSAcacher(rUbound(selectionSAcacher)) = selectionAcacher

                    num_debut = DTE.ActiveDocument.Selection.TopPoint.Line + num_ligne

                End If


                If Replace(line, Chr(32), "") = "EndFunction" _
                Or Replace(line, Chr(32), "") = "EndSub" Then

                    RechercheDebut = True
                    RechercheFin = False

                    num_fin = DTE.ActiveDocument.Selection.TopPoint.Line + num_ligne - 2

                    selectionAcacher(1) = num_debut
                    selectionAcacher(2) = num_fin

                    ReDim Preserve selectionSAcacher(rUbound(selectionSAcacher) + 1)
                    selectionSAcacher(rUbound(selectionSAcacher)) = selectionAcacher

                End If

            End If

lignesuivante:
            ligne_precedante = line
            num_ligne += 1

        Next line

        For i = 1 To rUbound(selectionSAcacher)

            CACHER(selectionSAcacher(i)(1), selectionSAcacher(i)(2))

        Next

    End Sub

    Sub CACHER(ByVal ligne_debut As Integer, ByVal ligne_fin As Integer)

        ' Permet de cacher une partie du code

        Dim objSel As TextSelection = DTE.ActiveDocument.Selection

        objSel.GotoLine(ligne_debut, False)

        objSel.LineDown(True, ligne_fin - ligne_debut + 1)

        DTE.ExecuteCommand("Edit.HideSelection")

    End Sub

    Sub TU_CACHER()

        Call CACHER(1, 6)

    End Sub

    Function rUbound(ByVal tableau) As Integer

        If Not (tableau Is Nothing) Then
            rUbound = UBound(tableau)
        Else
            rUbound = 0
        End If

    End Function

End Module 

History

  • 18th August, 2013: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
France France
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

 
Suggestion#Region Pin
Mikhail-T19-Aug-13 5:01
Mikhail-T19-Aug-13 5:01 
GeneralRe: #Region Pin
ARNOULT19-Aug-13 8:22
ARNOULT19-Aug-13 8:22 
AnswerRe: #Region Pin
Mikhail-T19-Aug-13 8:42
Mikhail-T19-Aug-13 8:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.