开发者

Visual Studio 2010: highlight CSS text and comment

开发者 https://www.devze.com 2022-12-25 13:27 出处:网络
Consider a snippet of CSS code from a .css file in Visual Studio 2010 to be commented out. Normally Ctrl + E , Ctrl + C will comment your selected HTML and other source code.

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

Visual Studio 2010: highlight CSS text and comment

Normally Ctrl + E , Ctrl + C will comment your selected HTML and other source code.

But 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 h开发者_StackOverflowighlighted CSS text for you?


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


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(finersoft@gmail.com)
    'BuildTime:2010-7-8 15:26:19
    'Purpose:To patch the bug of Visual Studio 2010 can not make correct comments of css

    '文档注释[简体中文]
    '作者:崔凯(finersoft@gmail.com)
    '创建时间: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


Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module RecordingModule

    Sub CommentCode()
        Dim objSel As TextSelection = DTE.ActiveDocument.Selection

        If objSel.IsEmpty Then
            singleLine = True
            'Dim ts1 As TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
            'Dim ep1 As EditPoint2 = ts1.TopPoint.CreateEditPoint()
            'Dim ep2 As EditPoint2 = ts1.BottomPoint.CreateEditPoint()
            'text = ep1.GetLines(ep1.Line, ep2.Line + 1)
        End If


        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 fileName.EndsWith(".cs") Then
            If objSel.Text.Trim().StartsWith("//") Then
                DTE.ExecuteCommand("Edit.UncommentSelection")
                Return
            Else
                DTE.ExecuteCommand("Edit.CommentSelection")
                Return
            End If
        End If

        If fileName.EndsWith(".css") Then
            ' enable undo
            Dim weOpenedUndo As Boolean = False
            If Not DTE.UndoContext.IsOpen Then
                DTE.UndoContext.Open("comment/uncomment code")
                weOpenedUndo = True
            End If

            ' locate cursor
            Dim startPoint As VirtualPoint
            startPoint = objSel.ActivePoint
            Dim iCol As Int32 = startPoint.DisplayColumn
            Dim iRow As Int32 = startPoint.Line

            ' locate selection beginning and end
            Dim ep1 As EditPoint2 = objSel.TopPoint.CreateEditPoint()
            Dim ep2 As EditPoint2 = objSel.BottomPoint.CreateEditPoint()

            ' in case no text is selected
            If objSel.IsEmpty Then
                objSel.SelectLine()
            End If

            ' process selection
            If objSel.Text.Trim().StartsWith("/*") Then
                ' uncomment
                objSel.MoveToPoint(objSel.TopPoint)
                objSel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
                objSel.Delete(2)
                While ep1.Line < ep2.Line
                    objSel.MoveToDisplayColumn(ep1.Line, 0)
                    ep1.LineDown()
                End While
                objSel.EndOfLine()
                objSel.DeleteLeft(2)
            Else
                ' comment
                objSel.MoveToPoint(objSel.TopPoint)
                objSel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
                objSel.Text = "/*"
                While ep1.Line < ep2.Line
                    objSel.MoveToDisplayColumn(ep1.Line, 0)
                    ep1.LineDown()
                End While
                objSel.EndOfLine()
                objSel.Text = "*/"
            End If

            ' move back to the original cursor point
            objSel.MoveToDisplayColumn(iRow, iCol)

            ' close the undo
            If weOpenedUndo Then
                DTE.UndoContext.Close()
            End If

        End If
    End Sub

End Module


um, get ReSharper and a million pains will go away instantly.

0

精彩评论

暂无评论...
验证码 换一张
取 消