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

不打扰是莪最后的温柔 提交于 2019-12-18 03:54:19

问题


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 ?


回答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.




回答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.




回答3:


If you like to change the key shortcut of toggle comments to ctrl+/ key. You can Hot command for visual studio and install it. Once you restart Visual Studio, it works!

https://marketplace.visualstudio.com/items?itemName=JustinClareburtMSFT.HotCommandsforVisualStudio




回答4:


In Visual Studio 2019 you can get almost exactly the functionality you are looking for by triple-clicking the first line and then dragging to the bottom line. This will select full lines.

Then use Ctrl-K Ctrl-C as usual. It will insert // and not /**/.




回答5:


Toggle IS NOT the same as Toggle On and Toggle Off.

If I Toggle a group of lines - some of which are commented out, and others are not, then a TOGGLE would comment out the lines that were previously not commented out and Un-Coment out the the line that were previously commented out - with a SINGLE keystroke.



来源:https://stackoverflow.com/questions/4350744/visual-studio-c-toggle-comment-comment-while-not-whole-line-is-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!