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

jer*_*mcc 20 css visual-studio-2010 visual-studio

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

Jef*_*tes 17

不幸的是,用于评论和取消注释(Ctrl+ K+ CCtrl+ K+ U)的常规命令不适用于CSS.相反,您需要记录或编写执行此操作的宏并将其附加到您自己的快捷方式.

要注释所选文本(注意,这很快且很脏,因此将其注释为单个块):

Sub CssComment()
    DTE.ActiveDocument.Selection.Text = "/*" + DTE.ActiveDocument.Selection.Text + "*/"
End Sub
Run Code Online (Sandbox Code Playgroud)

更新
下面这个新的更像是常规注释命令和逐行注释.这意味着您不必事先选择文本.这也将所有更改作为单个可撤消操作执行,并检查文件扩展名,以便您可以将其分配给常规快捷方式,它将适用于所有文件.

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
Run Code Online (Sandbox Code Playgroud)

取消注释更新
此宏执行反向任务.同样,它已实现,以便在需要时通过检查文件扩展名并推迟到Edit.UncommentSelection非CSS文件的标准命令,它将适用于所有文档.

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
Run Code Online (Sandbox Code Playgroud)

更新18Oct2012
根据dirq的回答,有一个扩展,Web Essentials提供CSS评论和取消注释.我建议在上面的宏中使用它,因为除了CSS注释快捷方式之外它还提供了其他很好的支持.


dir*_*irq 9

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