在Visual Studio 2010中,有没有办法轻松注释掉CSS中的行?

时间:2010-06-24 13:36:58

标签: css visual-studio visual-studio-2010

有人知道Visual Studio 2010中是否有办法在CSS文件中突出显示和注释掉所有其他文件(通过单击按钮)?也许Visual Studio扩展?手动评论它们很麻烦。

2 个答案:

答案 0 :(得分:17)

不幸的是常规命令用于评论和取消注释( Ctrl + K + C Ctrl + 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日更新
根据{{​​3}},有一个扩展名dirq's answer,提供CSS评论和取消注释。我建议在上面的宏中使用它,因为除了CSS注释快捷方式之外它还提供了其他很好的支持。

答案 1 :(得分:9)

有一个可用的扩展程序比宏更好:Web Essentials。看看这个。 http://visualstudiogallery.msdn.microsoft.com/6ed4c78f-a23e-49ad-b5fd-369af0c2107f