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.
Comment Code Block Ctrl+K+C/Ctrl+K+U If you select a block of code and use the key sequence Ctrl+K+C, you'll comment out the section of code. Ctrl+K+U will uncomment the code.
PDT allows you to quickly and easily comment and uncomment code by selecting a line or a block of text and tagging it as a comment. Comments can be added to single lines of code (Ctrl + /) or blocks of code (Ctrl + Shift + /).
You can comment multiple lines by the special beginning tag <! -- and ending tag --> placed before the first line and end of the last line as shown in the given example below.
You can also use standard shortcut key ( CTRL + K + C ) for commenting out the code block and (CTRL + K + U ) for uncommenting the code block.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With