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: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Due to internal controls and regulations, the amount of long term archival data is increasing every year. Since magnetic tape does not need to be periodically operated or connected to a power source, there will be no data loss because of performance degradation due to the drive actuator. Read this white paper to learn about a series of tests that determined magnetic tape is a reliable long-term storage solution for up to 30 years.

Most Popular Programming Stories

More for Developers

RSS Feeds