views:

115

answers:

1

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.

+7  A: 

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
Jeff Yates
CTRL+K+C and CTRL+K+U doesn't work. Could you elaborate on the macro?
rdkleine
@rdkleine: Working on it :)
Jeff Yates
@Jeff Sweet! Seems like it would be pretty easy, but I'll let you prove it. :-D
jeremcc
Nice! Thanks for the example
rdkleine
@Jeff Totally works! Thanks!
jeremcc
@jeremcc: No problem. I'll update my answer with a better macro when I've worked it out.
Jeff Yates
@jeremcc: Updated to include better comment macro and to include an uncomment counterpart.
Jeff Yates
@Jeff Wow, I wish I could upvote ten times. Thanks!
jeremcc
@jeremcc: You're welcome. It was an interesting exercise to work on. I learned quite a bit about the Visual Studio object model.
Jeff Yates