Visual Studio C++ 切换注释?评论而不选择整行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4350744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
visual studio C++ toggle comment ? comment while not whole line is selected?
提问by Mr_and_Mrs_D
2 questions actually :
实际上有2个问题:
1) shortcut to toggle commenton selected lines ? Available on all iDEs I used starting with notepad++
1)在选定行上切换注释的快捷方式?在我使用的所有 iDE 上都可用,从记事本++开始
2)the ctrl-k, ctrl-c
exhibits this behavior (quoted from someplace nicely worded):
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#:选择某些文本的每一行都在行首处用双斜线注释。如果未选择任何内容,则注释光标所在的行。
C++:如果没有选择任何内容或选择完整的行,则其行为如上。但是,如果选择了一行的一部分,并且没有选择注释作为选择的一部分(例如,选择代码行中间的某些内容),则选择将被 /* 和 */ 包围。
since I code in C++ I find this behavior annoying - I want to be able to comment out lines partially selected - any workarounds ?
因为我用 C++ 编码,所以我发现这种行为很烦人 - 我希望能够注释掉部分选择的行 - 任何解决方法?
回答by ch0kee
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...
工具->宏->宏 IDE...
In Macro Explorerright click on Macrosand click New Macro Project...
在宏资源管理器中右键单击宏并单击新建宏项目...
Name your macro for e.g. MyMacroProjectand click Add.
将您的宏命名为例如MyMacroProject并单击Add。
Right click on Module1in your new macro project in Macro Explorerand click Edit.
在宏资源管理器的新宏项目中右键单击Module1并单击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
将其输入到包含以下内容的 Show 命令中:ToggleCommentLine
Select Macros.MyMacroProject.Module1.ToggleCommentLine.
选择 Macros.MyMacroProject.Module1.ToggleCommentLine。
Set a key at Press shortcut keys:. , then click Assign, then click OK.
在按快捷键设置一个键:。,然后单击“分配”,然后单击“确定”。
Enjoy.
享受。
回答by Casey
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.
该行为是故意的。如果用户需要对不需要重写整行的单行进行微小的临时更改,则使用 Ctrl+K、Ctrl+C 快捷键对允许他/她仅注释更改而不是整个行线。
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.
至于第一个问题,它是相同的快捷键对:Ctrl+K、Ctrl+C 切换任何评论,Ctrl+K、Ctrl+U 切换任何评论。
Edit 2:
编辑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.
如果您仍然不满意,请从整个番茄软件中获取 Visual Assist X:http: //www.wholetomato.com/它在突出显示文本时为“/”和“*”键添加了附加注释快捷方式映射。
回答by imanzabet
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!
如果您想将切换注释的快捷键更改为ctrl
+/
键。您可以使用 Visual Studio 的 Hot 命令并安装它。重新启动 Visual Studio 后,它就可以工作了!
https://marketplace.visualstudio.com/items?itemName=JustinClareburtMSFT.HotCommandsforVisualStudio
https://marketplace.visualstudio.com/items?itemName=JustinClareburtMSFT.HotCommandsforVisualStudio
回答by BitPusher16
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.
在 Visual Studio 2019 中,您几乎可以通过三次单击第一行然后拖到最后一行来获得您正在寻找的功能。这将选择整行。
Then use Ctrl-K Ctrl-C as usual. It will insert //
and not /**/
.
然后像往常一样使用 Ctrl-K Ctrl-C。它将插入//
而不是/**/
。
回答by pg1
Toggle IS NOT the same as Toggle On and Toggle Off.
Toggle 与 Toggle On 和 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.
如果我切换一组行 - 其中一些被注释掉,而另一些没有,那么 TOGGLE 将注释掉以前没有注释掉的行,并取消注释掉以前注释掉的行 - 用单次击键。