Search code examples
c++visual-studio-2010commentscode-folding

How to auto-collapse certain comments in Visual Studio 2010?


A colleague of mine uses a abomination text editor that routinely leaves comment blocks all over the code. Needless to say, this is driving me rather mad. The comment blocks look like this:

/* EasyCODE ) */
/* EasyCODE ( 0 
WndProc */
/* EasyCODE F */

i.e. they all start with EasyCODE and most of them span several lines. Thankfully, VS2010 can collapse comment blocks, so I don't have to see them all the time.

Is there a way to automate that? A way to automatically collapse all those horrible EasyCODE blocks would be godsent!


Solution

  • Here is a macro that should do it. There are some weirder EasyCode comments that it doesn't catch but it mostly does the trick.

    Imports System
    Imports EnvDTE
    Imports EnvDTE80
    Imports EnvDTE90
    Imports EnvDTE90a ' remove for VS2008
    Imports EnvDTE100 ' remove for VS2008
    Imports System.Diagnostics
    Imports System.Collections.Generic
    
    Public Module HideEasyCODEComments
        ''
        '' Collapse all EasyCODE comment blocks
        ''
        Sub ToggleSummaryCommentsOutlineExpansion()
            If (DTE.ActiveDocument Is Nothing) Then
                Exit Sub
            End If
    
            If (DTE.UndoContext.IsOpen) Then
                DTE.UndoContext.Close()
            End If
    
            DTE.SuppressUI = True
    
            Try
                DTE.UndoContext.Open("ToggleSummaryCommentsOutline")
            Catch
            End Try
    
            Dim objSelection As TextSelection = DTE.ActiveDocument.Selection
            Dim line As Integer = objSelection.CurrentLine
            objSelection.StartOfDocument()
    
            ' find all EasyCODE blocks
            While objSelection.FindText("^.*\/\* EasyCODE.*((\n.*\*\/)|(\n.*\/\*.*)|(\n\/\/.*))*", vsFindOptions.vsFindOptionsRegularExpression)
                DTE.ExecuteCommand("Edit.HideSelection")
            End While
            objSelection.StartOfDocument()
            objSelection.GotoLine(line)
    
            DTE.UndoContext.Close()
            DTE.SuppressUI = False
        End Sub
    
    End Module
    

    Create a new macro in the macro IDE (Tools->Macros->Macro IDE), paste the above code into it, then assign a keyboard shortcut to it (Tools->Options->Environment->Keyboard, search for it in the listbox). Hit the keyboard shortcut and all EasyCode comments will be gone.

    Have fun!