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

  • Live Event Date: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

  • Specialization and efficiency are always in need. Whether it's replacing an aging roof, getting a haircut, or tuning up a car, most seek the assistance of trusted experts. The same is true in the business world, where an increasing number of companies are seeking the help of others to administer their IT systems and services. This special edition of Unleashing IT highlights a new breed of IT caretaker -- Cisco Powered service providers -- and the business advantages and operational efficiencies they …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds