Visual Studio 2010:突出显示CSS文本和注释

时间:2010-04-01 02:47:29

标签: visual-studio-2010 keyboard-shortcuts

考虑从Visual Studio 2010中的.css文件中删除一段CSS代码。

alt text

通常 Ctrl + E Ctrl + C 将评论您选择的HTML和其他源代码。< / p>

但突出显示CSS代码&amp;执行该快捷方式组合会产生警告消息:

  

组合键绑定到当前不可用的命令(注释选择)。

Visual Studio 2010中是否有工具栏或键盘快捷键可以为您注释突出显示的CSS文本?

4 个答案:

答案 0 :(得分:9)

注释选择命令不适用于css文件。

但是,您可以创建一个简单的宏并为其指定键盘快捷键...

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

要“取消注释”选择使用另一个简单的宏:

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

答案 1 :(得分:2)

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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

嗯,得到ReSharper,一百万次的痛苦会立即消失。