2010-06-24 14 views
20

¿Alguien sabe si hay una forma en Visual Studio 2010 para resaltar y comentar líneas en archivos CSS como se puede hacer con todos los demás archivos (haciendo clic en un botón)? Tal vez una extensión de Visual Studio? Comentarlos manualmente es engorroso.En Visual Studio 2010, ¿hay alguna manera de comentar fácilmente las líneas en CSS?

+0

ctrl-k-ctrl-c ¿no funciona? (No lo he usado específicamente para archivos CSS, así que no sé si funciona allí) – jalf

+0

+! Buen punto: nunca lo noté, CTRL + K + C no funciona y no hay opción de menú para comentar. – Fenton

Respuesta

17

Desafortunadamente los comandos regulares para comentar y descomentar (Ctrl +K +C y Ctrl +K +T) no funcionan para CSS. En cambio, necesitará grabar o escribir una macro que haga esto y adjuntarla a su propio atajo.

a comentar el texto seleccionado (nota, esto es rápido y sucio, por lo que comenta como un solo bloque):

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

actualización
Este nuevo a continuación funciona más como el comando normal comentario y comentarios línea por línea. Significa que no tiene que seleccionar el texto de antemano. Esto también hace todos los cambios como una operación única que se puede deshacer y verifica la extensión del archivo para que pueda asignar esto al acceso directo normal y funcionará para todos los archivos.

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 

Actualización para descomentando
Esta macro realiza la tarea inversa. Una vez más, está implementado para que funcione para todos los documentos si es necesario, verificando la extensión del archivo y difiriendo el comando estándar Edit.UncommentSelection para archivos que no son CSS.

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 

actualización 18Oct2012
Según dirq's answer, hay una extensión, Web Essentials que proporciona CSS comentando y eliminando el comentario. Yo recomendaría usar esto sobre las macros anteriores, ya que proporciona otra gran ayuda además de los accesos directos de comentarios de CSS.

+0

CTRL + K + C y CTRL + K + U no funciona. ¿Podrías elaborar sobre la macro? –

+0

@rdkleine: trabajando en ello :) –

+0

@Jeff Sweet! Parece que sería bastante fácil, pero te dejaré probarlo. :-D – jeremcc

Cuestiones relacionadas