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).
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)
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