visual studio C ++切换注释?注释而不是整行选择? [英] visual studio C++ toggle comment ? comment while not whole line is selected?

查看:581
本文介绍了visual studio C ++切换注释?注释而不是整行选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2个问题其实:



1)在选定的行上快速切换 切换注释可用于所有使用notepad ++开始使用的iDE



2) ctrl-k,ctrl-c (引用来自某个地方很好的措辞):


C#:每一行,其中一些文本是
被选择的注释在
双线开头。如果
没有选择,则光标所在行的
是注释的。



C ++:如果没有选择或
被选中,它
的行为如上。然而,如果选择
a行的部分,并且没有注释是作为选择的一部分选择的
(例如
选择在
代码行中间的东西),则选择是
包围/ *和* /。


因为我在C ++中编写代码我发现这个行为恼人 -

解决方案

每一行都选择了一些文本在行首注释,双斜杠。



如果是多行选择:
我的解决方案取消注释,只有选择中的所有行注释。我发现它更直观。






解决方案:



strong>工具 - > - > 宏... IDE 宏资源管理器,请右键单击,然后点击新宏项目...



例如 MyMacroProject ,然后点击添加



在新的宏项目中右键单击 Module1



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

p>

 选项严格关闭
选项显式关闭
导入EnvDTE
导入System.Text.RegularExpressions

Public Module Module1
Sub ToggleComCommentLine()
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)

'只有没有注释行时才会取消注释
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

'遍历行
对于lineIndex = firstLine到lastLine
sel.GotoLine(lineIndex,True)
Dim line As String = sel.Text
Dim m As Match = Regex.Match(line,^(\s *)(//)(。*)$)
allLinesCommented Then
sel.Text = m.Groups(1).Value& m.Groups(3).Value
ElseIf不是m.Success然后
sel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
sel.Text =//
结束如果
下一个

选择所有受影响的行
sel.GotoLine(firstLine,True)
sel.LineDown(true,lastLine - firstLine)
sel.EndOfLine (True)
End Sub
结束模块

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



将宏绑定到一个键:



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



显示包含以下命令的命令
ToggleCommentLine



选择Macros.MyMacroProject.Module1 .ToggleCommentLine。



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



$ b

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 ?

解决方案

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.

这篇关于visual studio C ++切换注释?注释而不是整行选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆