Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

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

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...