Search code examples
visual-studio-2010visual-studio-macros

Count non-whitespace characters for selection in Visual Studio 2010


does anyone know of a tool or extension to Visual Studio 2010 to count non-whitespace (e.g. all characters but not spaces, new lines etc.) for current selection in a document?

Nice to have for code golfing :)

I have a command line tool, but an integrated tool would be very nice. Also I would prefer something to evaluate current selection.


Solution

  • I finally got to creating this crude macro below by first recording a temporary macro in Visual Studio and then modifying it to look like the below:

    Option Strict Off
    Option Explicit Off
    Imports System
    Imports EnvDTE
    Imports EnvDTE80
    Imports EnvDTE90
    Imports EnvDTE90a
    Imports EnvDTE100
    Imports System.Diagnostics
    
    Public Module CountNonWhiteSpaceCharacters
        Sub Count()
            Dim selection As EnvDTE.TextSelection = DTE.ActiveDocument.Selection()
            Dim text As String = selection.Text
    
            text = text.Replace(" ", "")
            text = text.Replace(vbCrLf, "")
            text = text.Replace(vbTab, "")
    
            MsgBox("Count " + text.Length.ToString())
        End Sub    
    End Module
    

    This can be bound to a keybord shortcut if desired. Otherwise, double clicking it in Macro Explorer will run it.