2010-04-01 131 views
7

請考慮Visual Studio 2010中.css文件的CSS代碼片段,以便將其註釋掉。Visual Studio 2010:突出顯示CSS文本和註釋

alt text

通常按Ctrl +Ë按Ctrl +ç評論您選擇的HTML和其他源代碼。

但是突出CSS代碼&執行中的警告消息的快捷方式組合的結果:

組合鍵被綁定到命令(註釋選擇),這是當前不可用。

在Visual Studio 2010中是否有工具欄或鍵盤快捷鍵來爲您評論突出顯示的CSS文本?

回答

8

註釋選擇命令不適用於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 
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([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 
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 
0

恩,得到ReSharper和一百萬痛苦會立即消失。

+0

嗯,並且據我所見,ReSharper中沒有CSS文件的註釋快捷方式。 – 2013-03-27 23:36:09

相關問題