在Visual Studio 2010中,有没有办法轻松地注释掉CSS中的行? [英] In Visual Studio 2010, is there a way to easily comment out lines in CSS?
问题描述
有人知道,如果有一种方法在Visual Studio 2010中突出和注释的CSS文件中的行,像你可以与所有其他文件(通过单击按钮)?也许一个Visual Studio扩展?
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.
推荐答案
不幸的是,用于注释和取消注释的常规命令( Ctrl + < kbd> K + C 和 Ctrl + K + U 。
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
取消注释的更新
此宏执行相反的任务。再次,它被实现,所以它将适用于所有文档,如果需要通过检查文件扩展名和推迟到标准的 Edit.UncommentSelection
命令非CSS文件。
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
根据 dirq的回答,有一个扩展名 Web Essentials ,它提供了CSS注释和取消注释。我建议使用这个宏以上的宏,因为它提供了除了CSS注释快捷键之外的其他伟大的支持。
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.
这篇关于在Visual Studio 2010中,有没有办法轻松地注释掉CSS中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!