实际上有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/