在Visual Studio 2010中,有没有一种简单的方法来注释CSS中的行?

20

有人知道在Visual Studio 2010中是否有一种方法可以像其他文件那样(通过点击按钮)突出显示和注释CSS文件中的行吗?也许是一个Visual Studio扩展?手动注释很麻烦。


ctrl-k-ctrl-c不起作用吗?(我没有特别针对CSS文件使用它,所以不知道它是否在那里起作用) - jalf
好的观点 - 我从未注意到这一点,CTRL+K+C不起作用,也没有菜单选项可以注释掉。 - Fenton
2个回答

17

很遗憾,用于注释和取消注释的常规命令(Ctrl+K+CCtrl+K+U)在CSS中不起作用。相反,您需要录制或编写一个可以注释和取消注释的宏,并将其附加到自己的快捷方式上。

要将所选文本注释(请注意,这是快速而粗略的,因此将其作为单个块进行注释):

Sub CssComment()
    DTE.ActiveDocument.Selection.Text = "/*" + DTE.ActiveDocument.Selection.Text + "*/"
End Sub

更新
下面的新方法更像常规注释命令,按行注释。这意味着您不必事先选择文本。此外,它将所有更改作为单个可撤消操作完成,并检查文件扩展名,因此您可以将其分配给常规快捷方式,它将适用于所有文件。

Sub CommentCss()
    Dim ts1 As TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)

    Dim fileName = DTE.ActiveDocument.FullName

    ' We should default to regular commenting if we're not editing CSS.
    ' This allows this macro to be attached to the Ctrl+K+C shortcut
    ' without breaking existing file format commenting.
    If Not fileName.EndsWith(".css") Then
        DTE.ExecuteCommand("Edit.CommentSelection")
        Return
    End If

    Dim weOpenedUndo As Boolean = False
    If Not DTE.UndoContext.IsOpen Then
        DTE.UndoContext.Open("CommentCSS")
        weOpenedUndo = True
    End If

    ts1.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn, True)
    Dim ep1 As EditPoint2 = ts1.TopPoint.CreateEditPoint()
    Dim ep2 As EditPoint2 = ts1.BottomPoint.CreateEditPoint()

    While ep1.Line <= ep2.Line
        Dim text As String = ep1.GetLines(ep1.Line, ep1.Line + 1)
        text = text.Trim()

        If Not text.StartsWith("/*") Or Not text.EndsWith("*/") Then
            ep1.StartOfLine()
            ep1.Insert("/*")
            ep1.EndOfLine()
            ep1.Insert("*/")
        End If
        Dim lineBeforeDown As Integer = ep1.Line
        ep1.LineDown()

        If ep1.Line = lineBeforeDown Then
            Exit While
        End If
    End While

    ts1.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn, True)

    If weOpenedUndo Then
        DTE.UndoContext.Close()
    End If
End Sub

取消注释的更新
该宏执行相反的任务。同样,它的实现方式是通过检查文件扩展名并在非CSS文件中使用标准的Edit.UncommentSelection命令来确保对所有文档都能起作用。

Sub UncommentCss()
    Dim ts1 As TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
    Dim ep1 As EditPoint2 = ts1.TopPoint.CreateEditPoint()
    Dim ep2 As EditPoint2 = ts1.BottomPoint.CreateEditPoint()

    Dim fileName = DTE.ActiveDocument.FullName

    ' We should default to regular commenting if we're not editing CSS.
    ' This allows this macro to be attached to the Ctrl+K+C shortcut
    ' without breaking existing file format commenting.
    If Not fileName.EndsWith(".css") Then
        DTE.ExecuteCommand("Edit.UncommentSelection")
        Return
    End If

    Dim weOpenedUndo As Boolean = False
    If Not DTE.UndoContext.IsOpen Then
        DTE.UndoContext.Open("UncommentCSS")
        weOpenedUndo = True
    End If

    While ep1.Line <= ep2.Line
        ep1.StartOfLine()

        Dim text As String = ep1.GetLines(ep1.Line, ep1.Line + 1)
        text = text.Trim()

        If text.StartsWith("/*") And text.EndsWith("*/") Then
            Dim epEndOfLine As EditPoint2 = ep1.CreateEditPoint()
            epEndOfLine.EndOfLine()
            text = text.Substring(2, text.Length - 4)
            ep1.ReplaceText(epEndOfLine, text, vsEPReplaceTextOptions.vsEPReplaceTextKeepMarkers Or vsEPReplaceTextOptions.vsEPReplaceTextAutoformat)
        End If

        Dim lineBeforeDown As Integer = ep1.Line
        ep1.LineDown()

        If ep1.Line = lineBeforeDown Then
            Exit While
        End If
    End While

    ts1.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn, True)

    If weOpenedUndo Then
        DTE.UndoContext.Close()
    End If
End Sub

2012年10月18日更新
根据dirq的回答,有一个扩展程序Web Essentials可以提供CSS注释和取消注释。我建议使用它而不是上面的宏,因为它除了提供CSS注释快捷方式外还提供其他很好的支持。


CTRL+K+C和CTRL+K+U无法使用。您能详细说明一下宏吗? - Ralf de Kleine
@Jeff Sweet!看起来应该很容易,但我会让你证明它。:-D - jeremcc
@jeremcc:没问题。我制定出更好的宏后会更新我的答案。 - Jeff Yates
@jeremcc:更新以包括更好的注释宏,并包括取消注释的对应项。 - Jeff Yates
@jeremcc:不用谢。这是一个有趣的练习。我学到了很多关于Visual Studio对象模型的知识。 - Jeff Yates
很棒的答案,检查文件类型可以使用相同的快捷方式,这真的很重要 :) 当您取消注释时,格式保持不变,没有任何问题,做得好 +1 - Pricey

9

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接