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

  • Agile development principles have gone from something used only by cutting-edge teams to a mainstream approach used by teams large and small. If you're not using agile methods already though, or if you've only been exposed to agile on small projects here and there, you may wonder if agile can ever work in your environment. Read this eBook to learn the fundamentals of agile and how to increase the productivity of your software teams while enabling them to produce higher-quality solutions that better fulfill …

  • Do you know where your data is? Consumer cloud-based file sharing services store your sensitive company data on servers outside of your control, outside of your policy and regulatory guidelines – maybe even outside your country – and not managed by you. The potential for data leakage, security breaches, and harm to your business is enormous. Download this white paper to learn about file sync and share alternatives that allow you to manage and protect your sensitive data while integrating and …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds