views:

261

answers:

3

Consider a snippet of CSS code from a .css file in Visual Studio 2010 to be commented out.

alt text

Normally Ctrl-E + Ctrl-C will comment your selected HTML and other source code. Highlighting CSS code & executing that shortcut combo results in a warning message:

The key combination is bound to command (Comment Selection) which is not currently available.

Is there a toolbar or keyboard shortcut in Visual Studio 2010 to comment the highlighted CSS text for you?

+4  A: 

Comment Selection command is not available for css files.

However, you can create a simple macro and assign a keyboard shortcut to it...

Sub CommentCSS()
    Dim selection As String
    selection = DTE.ActiveDocument.Selection.Text
    selection = "/*" + selection + " */"        
    DTE.ActiveDocument.Selection.Text = selection
End Sub

To 'uncomment' the selection use another simple macro:

Sub UncommentCSS()
    Dim selection As String
    selection = DTE.ActiveDocument.Selection.Text
    selection = selection.Remove(0, 2)
    selection = selection.Remove(selection.Length - 2, 2)
    DTE.ActiveDocument.Selection.Text = selection
End Sub
kzen
+1  A: 
Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module RecordingModule
    'Document Comment[English Version]
    'Author:kai cui([email protected])
    'BuildTime:2010-7-8 15:26:19
    'Purpose:To patch the bug of Visual Studio 2010 can not make correct comments of css

    '文档注释[简体中文]
    '作者:崔凯([email protected])
    '创建时间:2010年7月8日15:30:24
    '目的:修正Visual Studio 2010对CSS添加的错误注释


    Sub CommentCSS()
        'ref:VSEditor.CommentRegion()
        'source code:
        '    '' CommentRegion 将面向行的注释语法放在
        '    '' 所选内容涉及的每一行的行首,并创建撤消操作
        '    '' 以便对编辑的所有行仅执行一次撤消操作。
        '    ''
        'Sub CommentRegion()
        '    Dim selection As EnvDTE.TextSelection
        '    Dim startPoint As EnvDTE.EditPoint
        '    Dim endPoint As TextPoint
        '    Dim commentStart As String

        '    selection = DTE.ActiveDocument.Selection()
        '    startPoint = selection.TopPoint.CreateEditPoint()
        '    endPoint = selection.BottomPoint
        '    commentStart = Utilities.LineOrientedCommentStart()

        '    DTE.UndoContext.Open("Comment Region")
        '    Try
        '        Do While (True)
        '            Dim line As Integer

        '            line = startPoint.Line
        '            startPoint.Insert(commentStart)
        '            startPoint.LineDown()
        '            startPoint.StartOfLine()
        '            If (line = endPoint.Line) Then
        '                Exit Do
        '            End If
        '        Loop
        '    Finally
        '        ' 如果发生错误,则要确保对撤消上下文进行清理。
        '        ' 否则,编辑器可能会处于永久的撤消上下文中。
        '        DTE.UndoContext.Close()
        '    End Try
        'End Sub
        Dim selection As EnvDTE.TextSelection
        Dim startPoint As EnvDTE.EditPoint
        Dim endPoint As TextPoint
        Dim commentStart As String
        Dim commentEnd As String


        selection = DTE.ActiveDocument.Selection()
        startPoint = selection.TopPoint.CreateEditPoint()
        endPoint = selection.BottomPoint
        commentStart = "/*"
        commentEnd = "*/"

        DTE.UndoContext.Open("Comment Region")
        Try
            Do While (True)
                Dim line As Integer
                line = startPoint.Line
                startPoint.Insert(commentStart)
                startPoint.EndOfLine()
                startPoint.Insert(commentEnd)
                startPoint.LineDown()
                startPoint.StartOfLine()
                If (line = endPoint.Line) Then
                    Exit Do
                End If
            Loop
        Finally
            ' 如果发生错误,则要确保对撤消上下文进行清理。
            ' 否则,编辑器可能会处于永久的撤消上下文中。
            DTE.UndoContext.Close()
        End Try



    End Sub

    Sub UncommentCSS()
        'ref:Find.FindReplace 
        'url:http://msdn.microsoft.com/zh-cn/library/envdte.find.findreplace(v=VS.80).aspx
        'source code:
        'Sub FindReplaceExample()
        '    Dim objTextDoc As TextDocument
        '    Dim objEditPt As EditPoint
        '    Dim iCtr As Integer
        '    Dim objFind As Find

        '    ' Create a new text file.
        '    DTE.ItemOperations.NewFile("General\Text File")

        '    ' Get a handle to the new document and create an EditPoint.
        '    objTextDoc = DTE.ActiveDocument.Object("TextDocument")
        '    objEditPt = objTextDoc.StartPoint.CreateEditPoint
        '    objFind = objTextDoc.DTE.Find

        '    ' Insert ten lines of text.
        '    For iCtr = 1 To 10
        '        objEditPt.Insert("This is a test." & Chr(13))
        '    Next iCtr
        '    objEditPt.StartOfDocument()
        '    objFind.FindReplace(vsFindAction.vsFindActionReplaceAll, "test", vsFindOptions.vsFindOptionsMatchWholeWord, "NEW THING", vsFindTarget.vsFindTargetOpenDocuments, , , vsFindResultsLocation.vsFindResultsNone)
        'End Sub


        Dim objEditPt As EditPoint
        Dim objFind As Find

        Dim selection As EnvDTE.TextSelection
        Dim startPoint As EnvDTE.EditPoint
        Dim endPoint As TextPoint
        Dim commentStart As String
        Dim commentEnd As String

        selection = DTE.ActiveDocument.Selection()
        startPoint = selection.TopPoint.CreateEditPoint()
        endPoint = selection.BottomPoint
        commentStart = "/*"
        commentEnd = "*/"

        objEditPt = selection.TopPoint.CreateEditPoint()
        objFind = selection.DTE.Find

        objEditPt.StartOfDocument()
        objFind.FindReplace(vsFindAction.vsFindActionReplaceAll, commentStart, vsFindOptions.vsFindOptionsMatchWholeWord, "", vsFindTarget.vsFindTargetOpenDocuments, , , vsFindResultsLocation.vsFindResultsNone)
        objFind.FindReplace(vsFindAction.vsFindActionReplaceAll, commentEnd, vsFindOptions.vsFindOptionsMatchWholeWord, "", vsFindTarget.vsFindTargetOpenDocuments, , , vsFindResultsLocation.vsFindResultsNone)
    End Sub


End Module
kai cui
A: 

Option Strict Off

Option Explicit Off

Imports System

Imports EnvDTE

Imports EnvDTE80

Imports EnvDTE90

Imports EnvDTE90a

Imports EnvDTE100

Imports System.Diagnostics

Public Module RecordingModule

'Document Comment[English Version]
'Author:kai cui([email protected])
'BuildTime:2010-7-8 15:26:19
'Purpose:To patch the bug of Visual Studio 2010 can not make correct comments of css

'文档注释[简体中文]
'作者:崔凯([email protected])
'创建时间:2010年7月8日15:30:24
'目的:修正Visual Studio 2010对CSS添加的错误注释


Sub CommentCSS()
    'ref:VSEditor.CommentRegion()
    'source code:
    '    '' CommentRegion 将面向行的注释语法放在
    '    '' 所选内容涉及的每一行的行首,并创建撤消操作
    '    '' 以便对编辑的所有行仅执行一次撤消操作。
    '    ''
    'Sub CommentRegion()
    '    Dim selection As EnvDTE.TextSelection
    '    Dim startPoint As EnvDTE.EditPoint
    '    Dim endPoint As TextPoint
    '    Dim commentStart As String

    '    selection = DTE.ActiveDocument.Selection()
    '    startPoint = selection.TopPoint.CreateEditPoint()
    '    endPoint = selection.BottomPoint
    '    commentStart = Utilities.LineOrientedCommentStart()

    '    DTE.UndoContext.Open("Comment Region")
    '    Try
    '        Do While (True)
    '            Dim line As Integer

    '            line = startPoint.Line
    '            startPoint.Insert(commentStart)
    '            startPoint.LineDown()
    '            startPoint.StartOfLine()
    '            If (line = endPoint.Line) Then
    '                Exit Do
    '            End If
    '        Loop
    '    Finally
    '        ' 如果发生错误,则要确保对撤消上下文进行清理。
    '        ' 否则,编辑器可能会处于永久的撤消上下文中。
    '        DTE.UndoContext.Close()
    '    End Try
    'End Sub
    Dim selection As EnvDTE.TextSelection
    Dim startPoint As EnvDTE.EditPoint
    Dim endPoint As TextPoint
    Dim commentStart As String
    Dim commentEnd As String


    selection = DTE.ActiveDocument.Selection()
    startPoint = selection.TopPoint.CreateEditPoint()
    endPoint = selection.BottomPoint
    commentStart = "/*"
    commentEnd = "*/"

    DTE.UndoContext.Open("Comment Region")
    Try
        Do While (True)
            Dim line As Integer
            line = startPoint.Line
            startPoint.Insert(commentStart)
            startPoint.EndOfLine()
            startPoint.Insert(commentEnd)
            startPoint.LineDown()
            startPoint.StartOfLine()
            If (line = endPoint.Line) Then
                Exit Do
            End If
        Loop
    Finally
        ' 如果发生错误,则要确保对撤消上下文进行清理。
        ' 否则,编辑器可能会处于永久的撤消上下文中。
        DTE.UndoContext.Close()
    End Try



End Sub

Sub UncommentCSS()
    'ref:Find.FindReplace 
    'url:http://msdn.microsoft.com/zh-cn/library/envdte.find.findreplace(v=VS.80).aspx
    'source code:
    'Sub FindReplaceExample()
    '    Dim objTextDoc As TextDocument
    '    Dim objEditPt As EditPoint
    '    Dim iCtr As Integer
    '    Dim objFind As Find

    '    ' Create a new text file.
    '    DTE.ItemOperations.NewFile("General\Text File")

    '    ' Get a handle to the new document and create an EditPoint.
    '    objTextDoc = DTE.ActiveDocument.Object("TextDocument")
    '    objEditPt = objTextDoc.StartPoint.CreateEditPoint
    '    objFind = objTextDoc.DTE.Find

    '    ' Insert ten lines of text.
    '    For iCtr = 1 To 10
    '        objEditPt.Insert("This is a test." & Chr(13))
    '    Next iCtr
    '    objEditPt.StartOfDocument()
    '    objFind.FindReplace(vsFindAction.vsFindActionReplaceAll, "test", vsFindOptions.vsFindOptionsMatchWholeWord, "NEW THING", vsFindTarget.vsFindTargetOpenDocuments, , , vsFindResultsLocation.vsFindResultsNone)
    'End Sub


    Dim objEditPt As EditPoint
    Dim objFind As Find

    Dim selection As EnvDTE.TextSelection
    Dim startPoint As EnvDTE.EditPoint
    Dim endPoint As TextPoint
    Dim commentStart As String
    Dim commentEnd As String

    selection = DTE.ActiveDocument.Selection()
    startPoint = selection.TopPoint.CreateEditPoint()
    endPoint = selection.BottomPoint
    commentStart = "/*"
    commentEnd = "*/"

    objEditPt = selection.TopPoint.CreateEditPoint()
    objFind = selection.DTE.Find

    objEditPt.StartOfDocument()
    objFind.FindReplace(vsFindAction.vsFindActionReplaceAll, commentStart, vsFindOptions.vsFindOptionsMatchWholeWord, "", vsFindTarget.vsFindTargetOpenDocuments, , , vsFindResultsLocation.vsFindResultsNone)
    objFind.FindReplace(vsFindAction.vsFindActionReplaceAll, commentEnd, vsFindOptions.vsFindOptionsMatchWholeWord, "", vsFindTarget.vsFindTargetOpenDocuments, , , vsFindResultsLocation.vsFindResultsNone)
End Sub

End Module

kai cui