c++ - Visual Studio C++ 切换评论?评论而不选择整行?

标签 c++ visual-studio-2010 comments toggle

实际上有2个问题:

1) 在选定行上切换评论的快捷方式?在我使用的所有 iDE 上都可用,从 notepad++ 开始

2) ctrl-k, ctrl-c 表现出这种行为(引自某个措辞优美的地方):

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 */.

由于我使用 C++ 编写代码,我发现这种行为很烦人 - 我希望能够注释掉部分选中的行 - 有什么解决方法吗?

最佳答案

选择了某些文本的每一行都在行首用双斜杠注释。如果未选择任何内容,则注释光标所在的行。

如果选择多行: My solution uncomments only if all the lines in the selection are commented.我发现它更直观。


解决方案:

工具 -> -> 宏 IDE...

Macro Explorer 中右键单击 Macros 并单击 New Macro Project...

为您的宏命名,例如MyMacroProject 并点击添加

宏浏览器中的新宏项目中右键单击Module1 并点击编辑

将其粘贴到宏编辑器窗口中:

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

保存此文件并关闭宏编辑器窗口。

将你的宏绑定(bind)到一个键:

工具 -> 选项... -> 环境 -> 键盘

显示包含以下内容的命令中输入: ToggleCommentLine

选择 Macros.MyMacroProject.Module1.ToggleCommentLine。

按快捷键:处设置一个键。 ,然后点击分配,然后点击确定

享受吧。

关于c++ - Visual Studio C++ 切换评论?评论而不选择整行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4350744/

相关文章:

php - 在外部对YouTube视频发表评论?

c++ - 编译时出错 : control may reach end of non-void function

c++ - 如何声明包含对外部类的引用的成员变量?

c++ - 在 C++ 中的多个文件中包含单个 header 库

keyboard - 使用德语键盘在 Pycharm 中注释代码

html - 带有评论框的文本溢出

javascript - switch case 语句中的分号

c++ - 构建项目时出现链接器错误

c++ - C++ 中的多重定义 (Visual Basic 2010)

visual-studio-2010 - 在 Visual Studio 2010 中缓慢扩展快速查找窗口错误