Block Comment Macro for Visual Studio .NET

Environment:

Here's a block comment macro for Visual Studio .NET (as I wasn't able to find one and I wanted to find the differences in the API). It seems to work okay for me. Feel free to post bug fixes and so forth. I hope someone finds it useful. Good luck.

The code follows:

' This will comment/uncomment out blocks. Blocks are
' commented at the beginning of the line.
' Assign this to a key (e.g. ctrl-/) and it will toggle the
' current line/block of code.
' This will handle both "//" and "'" style comments. Adapted
' to VS.NET by Tim Stubbs
' Original code from the VS example and (idea from) Adam Solesby
' who originally did this for VC 6 (or earlier). There's been
' quite a few changes to VS.NET so this took a little creative
' adaptation. I hate VB :) Disclaimer: Use at own risk. I did
' it for ME!
' I don't need anything other than block commenting tbh, so
' I've not special cased single line selection.
Sub CustomCommentOutVSNET()
  'DESCRIPTION: Comments out a selected block of text.
  Dim win As Window
  win = ActiveWindow()
  If win.Type <> EnvDTE.vsWindowType.vsWindowTypeDocument Then
    MsgBox("This macro can only be run when a text editor _
            window is active.")
  Else
    TypeOfFile = FileType(ActiveDocument)
    ' MsgBox "Type: " + CStr(TypeOfFile)
    If TypeOfFile > 0 And TypeOfFile < 6 Then
      If TypeOfFile > 3 Then
        CommentType = "'"    ' VB bah. humbug.
        CommentWidth = 1
      Else
        CommentType = "//"   ' C++ and java style comments
        CommentWidth = 2
      End If

      StartLine = ActiveDocument.Selection.TopLine
      EndLine = ActiveDocument.Selection.BottomLine
      If EndLine < StartLine Then
        Temp = StartLine
        StartLine = EndLine
        EndLine = Temp
      End If


      For i = StartLine To EndLine

        ActiveDocument.Selection.GoToLine(i)

        ' check for nasties like blank lines or short lines
        ActiveDocument.Selection.EndOfLine(True)
        Dim objActive As VirtualPoint = _
            ActiveDocument.Selection.ActivePoint
        Dim right, line As Integer
        right = objActive.DisplayColumn

        ActiveDocument.Selection.StartOfLine _
          (vsStartOfLineOptionsFirstColumn)
        ActiveDocument.Selection.CharRight _
          (True, CommentWidth)

        line = objActive.Line()
        If (line > i) Then     ' we've shifted line, _
            reset to original line
            ActiveDocument.Selection.GotoLine(i)
            ActiveDocument.Selection.EndOfLine(True)
        End If

        'Enable the following for debugging
        'Dim blah, wibble As String
        'wibble = ActiveDocument.Selection.text()
        'blah = CommentType

         If ActiveDocument.Selection.text() = CommentType _
         Then
            ActiveDocument.Selection.Delete()
         Else
            ActiveDocument.Selection.EndOfLine(True)
            wibble = CommentType + ActiveDocument() _
                     .Selection.text()
            ActiveDocument.Selection.text() = CommentType _
            + ActiveDocument().Selection.text()
         End If

      Next
    Else
      MsgBox("Unable to comment out the highlighted text" _
              + vbLf + _
       "because the file type was unrecognized." + vbLf + _
       "If the file has not yet been saved, " + vbLf + _
       "please save it and try again.")
    End If
  End If
End Sub


Comments

  • Modification makes it work with all text files

    Posted by Legacy on 11/24/2003 12:00am

    Originally posted by: philip stilianos

    Thanks Tim.

    It took a while to work out how to integrate the macro, being my first time doing this.

    When I got the code working, it would not recognise any of the files i was working with, asp, aspx, vb.

    So I inserted a line of code:
    TypeOfFile = 5
    as a kludge to force the macro to recognise any file as commentable. My asp,aspx, vb files, etc are now all commentable.
    If i want a different type of comment character I will have to copy the code to create a new macro and set:
    commenttype ="'" to some other character.

    Philip

    Reply
  • 34 build errors

    Posted by Legacy on 08/15/2002 12:00am

    Originally posted by: Anatoly Kochergin

    I have no idea how this may work.
    It seems like some code is not present here.

    Reply
  • To further explain this is a TOGGLE comment macro

    Posted by Legacy on 08/14/2002 12:00am

    Originally posted by: Tim Stubbs

    ...and hence is more useful than having one macro for comment and another for uncomment. That was the point, apols for any confusion.


    Reply
  • Don't we already have this?

    Posted by Legacy on 08/13/2002 12:00am

    Originally posted by: Arild Fines

    I believe VS.NET has this functionality already - try Ctrl-K followed by Ctrl-C. To uncomment: Ctrl-K Ctrl-U

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's competitive marketplace requires the organization to frequently release and deploy applications at the pace of user demands, with reduced cost, risk, and increased quality. This book defines the basics of application release and deployment, and provides best practices for implementation with resources for a deeper dive. Inside you will find: The business and technical drivers behind automated application release and deployment. Evaluation guides for application release and deployment solutions. …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds