In Visual Studio 2010, is there a way to easily co

2020-02-09 06:32发布

问题:

Does anybody know if there is a way in Visual Studio 2010 to highlight and comment out lines in CSS files like you can with all other files (by clicking a button)? Perhaps a Visual Studio extension? Commenting them manually is cumbersome.

回答1:

Unfortunately the regular commands for commenting and uncommenting (Ctrl+K+C and Ctrl+K+U) don't work for CSS. Instead, you'll need to record or write a macro that does this and attach it to your own shortcut.

To comment the selected text (note, this is quick and dirty and therefore comments it as a single block):

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

Update
This new one below works more like the regular comment command and comments on a line-by-line basis. It means you don't have to select the text before hand. This also does all the changes as a single undoable operation and checks the file extension so that you can assign this to the regular shortcut and it will work for all files.

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

Update for Uncommenting
This macro performs the reverse task. Again, it's implemented so that it will work for all documents if required by checking the file extension and deferring to the standard Edit.UncommentSelection command for non-CSS files.

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

Update 18Oct2012
As per dirq's answer, there is an extension, Web Essentials that provides CSS commenting and uncommenting. I would recommend using this over the macros above as it provides other great support besides just the CSS commenting shortcuts.



回答2:

There's an extension available that works better than the macro: Web Essentials. Check it out. http://visualstudiogallery.msdn.microsoft.com/6ed4c78f-a23e-49ad-b5fd-369af0c2107f