visual studio C++ toggle comment ? comment while not whole line is selected?

27,985

Solution 1

Each line where some text is selected is commented at the line-start with double-slash. If nothing is selected, the line where the cursor is is commented.

In case of multiline selection: My solution uncomments only if all the lines in the selection are commented. I found it more intuitive.


Solution:

Tools -> Macros -> Macros IDE...

In Macro Explorer right click on Macros and click New Macro Project...

Name your macro for e.g. MyMacroProject and click Add.

Right click on Module1 in your new macro project in Macro Explorer and click Edit.

Paste this into the macro editor window:

Option Strict Off
Option Explicit Off
Imports EnvDTE
Imports System.Text.RegularExpressions

Public Module Module1
    Sub ToggleCommentLine()
        Dim sel As TextSelection = DTE.ActiveDocument.Selection

        Dim firstLine As Integer = sel.TopPoint.Line
        Dim lastLine As Integer = sel.BottomPoint.Line

        sel.GotoLine(firstLine, True)
        sel.LineDown(True, lastLine - firstLine)
        sel.EndOfLine(True)

        'we un-comment only if there is no commented line
        Dim allLinesCommented As Boolean = True

        Dim lineIndex As Integer = firstLine
        While allLinesCommented And (lineIndex <= lastLine)
            sel.GotoLine(lineIndex, True)
            allLinesCommented = Regex.IsMatch(sel.Text, "^\s*//.*$")
            lineIndex += 1
        End While

        'iterate over the lines
        For lineIndex = firstLine To lastLine
            sel.GotoLine(lineIndex, True)
            Dim line As String = sel.Text
            Dim m As Match = Regex.Match(line, "^(\s*)(//)(.*)$")
            If allLinesCommented Then
                sel.Text = m.Groups(1).Value & m.Groups(3).Value
            ElseIf Not m.Success Then
                sel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
                sel.Text = "//"
            End If
        Next

        'select all the affected lines
        sel.GotoLine(firstLine, True)
        sel.LineDown(True, lastLine - firstLine)
        sel.EndOfLine(True)
    End Sub
End Module

Save this file and close the macro editor window.

Bind your macro to a key:

Tools -> Options... -> Environment -> Keyboard

Type this into Show commands containing: ToggleCommentLine

Select Macros.MyMacroProject.Module1.ToggleCommentLine.

Set a key at Press shortcut keys: . , then click Assign, then click OK.

Enjoy.

Solution 2

The behavior is intentional. If the user needed a tiny temporary change to a single line that did not require the entire line to be re-written, using the Ctrl+K, Ctrl+C shortcut pair allows him/her to comment out just the change and not the entire line.

Edit:

As for question one, it's the same shortcut pair: Ctrl+K, Ctrl+C to toggle any comments on, Ctrl+K, Ctrl+U to toggle any comments off.

Edit 2:

If you are still unsatisfied, get Visual Assist X from whole tomato software: http://www.wholetomato.com/ It adds an additional comment shortcut mapping to the '/' and '*' keys when text is highlighted.

Share:
27,985
Mr_and_Mrs_D
Author by

Mr_and_Mrs_D

Be warned - the Monster isAlife Git, Java, Android and finally Python I was flirting with JEE since a couple years but since 1/2014 we are having an affair I spent the best part of the last year refactoring a widely used mod manager application. Here is the commit message of the release I have been working on, where I detail what I have been doing: https://github.com/wrye-bash/wrye-bash/commit/1cd839fadbf4b7338b1c12457f601066b39d1929 I am interested in code quality and performance (aka in the code as opposed to what the code does) If you find my posts useful you can buy me a coffee TCP walks into a bar &amp; says: “I’d like a beer.” “You’d like a beer?” “Yes, a beer.”

Updated on July 09, 2022

Comments

  • Mr_and_Mrs_D
    Mr_and_Mrs_D almost 2 years

    2 questions actually :

    1) shortcut to toggle comment on selected lines ? Available on all iDEs I used starting with notepad++

    2)the ctrl-k, ctrl-c exhibits this behavior (quoted from someplace nicely worded):

    C#: Each line where some text is selected is commented at the line-start with double-slash. If nothing is selected, the line where the cursor is is commented.

    C++: If nothing is selected or complete lines are selected, it behaves as above. However, if parts of a line are selected, and no comment is selected as part of the selection (ex. select something in the middle of a code line), then the selection is surrounded by /* and */.

    since I code in C++ I find this behavior annoying - I want to be able to comment out lines partially selected - any workarounds ?

  • Mr_and_Mrs_D
    Mr_and_Mrs_D over 13 years
    I do agree it can be useful - it should have another shortcut though -- any ideas about q1 ?
  • Mr_and_Mrs_D
    Mr_and_Mrs_D over 13 years
    Thanks - but toggle comment means to select some lines which are commented and some which are not and toggle their comment status simultaneously
  • Mr_and_Mrs_D
    Mr_and_Mrs_D about 11 years
    Yes - that is what the question is about - you should add this as a comment to the other answer not as an answer
  • static_rtti
    static_rtti over 10 years
    Very cool, thank you! I guess the bounty is for you, I'll just wait a few more days to see if anyone else wants to answer.
  • static_rtti
    static_rtti over 10 years
    Actually I have one remark: you seem to use a C-specific regex to do the work. Wouldn't it be better to use Visual Studio's commenting and uncommenting functions which are far more general and more robust?
  • ch0kee
    ch0kee over 10 years
    Can you mention any use case when this solution is not general or robust enough ? You would like it to exclude lines from comment when is executed inside a multi line comment (/* ... */) ?
  • static_rtti
    static_rtti over 10 years
    any non-C language, for instance. Haskell, HTML, you name it!
  • ch0kee
    ch0kee over 10 years
    I know Haskell but I thought we are talking about C++ now :/
  • Mr_and_Mrs_D
    Mr_and_Mrs_D over 10 years
    @static_rtti: you are right - this solution needs to be made more generic - I would accept it if made more generic and tested by you (I do not have VS installed). ch0kee : "I thought we are talking about C++ now" -->general, reusable code is always better than one shot hack - better rich and healthy than poor and sick as the saying goes. Moreover the question does not talk about toggling comments in C++ - although I was coding in C++ at the time - and everywhere this feature is offered is offered (for all supported languages) ;)
  • static_rtti
    static_rtti over 10 years
    I did mention C++ :) The bounty is for you. However, If you or someone else wants to make it more generic, I think it would be a very useful thing.
  • betontalpfa
    betontalpfa about 8 years
    See Tools->Option->Encironment->Keyboard