在VisualStudio2010中,有没有一种方法可以轻松地注释掉CSS中的行?
有人知道在VisualStudio2010中是否有一种方法可以像处理所有其他文件一样(通过单击按钮)突出显示CSS文件中的行并注释掉这些行吗?也许是Visual Studio的扩展?手动注释它们很麻烦。不幸的是,用于注释和取消注释的常规命令(Ctrl+K+C和Ctrl+K+U)不适用于CSS。相反,您需要录制或编写执行此操作的宏,并将其附加到您自己的快捷方式 要对所选文本进行注释(请注意,这是一个快速且肮脏的操作,因此会将其作为单个块进行注释): 更新在VisualStudio2010中,有没有一种方法可以轻松地注释掉CSS中的行?,css,visual-studio,visual-studio-2010,Css,Visual Studio,Visual Studio 2010,有人知道在VisualStudio2010中是否有一种方法可以像处理所有其他文件一样(通过单击按钮)突出显示CSS文件中的行并注释掉这些行吗?也许是Visual Studio的扩展?手动注释它们很麻烦。不幸的是,用于注释和取消注释的常规命令(Ctrl+K+C和Ctrl+K+U)不适用于CSS。相反,您需要录制或编写执行此操作的宏,并将其附加到您自己的快捷方式 要对所选文本进行注释(请注意,这是一个快速且肮脏的操作,因此会将其作为单个块进行注释): 更新 下面这个新命令的工作方式更像常规的comm
下面这个新命令的工作方式更像常规的comment命令,并逐行进行注释。这意味着您不必在手之前选择文本。这也会将所有更改作为一个可撤消的操作进行,并检查文件扩展名,以便您可以将其分配给常规快捷方式,并且它将适用于所有文件
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
2012年10月18日更新根据,有一个扩展,提供CSS注释和取消注释。我建议在上面的宏中使用它,因为除了CSS注释快捷方式之外,它还提供了其他很好的支持。有一个比宏更好的扩展:Web Essentials。看看吧 ctrl-k-ctrl-c不起作用?(我还没有专门为CSS文件使用它,所以不知道它在那里是否有效)+!好的一点-我从来没有注意到这一点,CTRL+K+C不起作用,也没有菜单选项可以注释掉。CTRL+K+C和CTRL+K+U不起作用。你能详细解释一下宏吗?@Jeff Sweet!看起来很容易,但我会让你证明的-D@jeremcc:没问题。当我计算出答案后,我会用一个更好的宏来更新它。@jeremcc:Updated以包含更好的注释宏并包含一个未注释的对应宏。@jeremcc:。这是一个有趣的练习。我对VisualStudio对象模型了解了很多。
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
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