0

Visual Studio: Zeilen alphabetisch sortieren/ordnen mit Makro

Hab letztens beim googeln eine Möglichkeit gefunden im Visual Studio mehrere Zeilen alphabetisch zu ordnen. Mein Standard-Texteditor (der portable Proton) kann das auch. Visual Studio 2010 von Haus aus nicht. Aber mit Hilfe eines einfachen Makros wird es um die Funktion erweitert:

#Region "Sub SortSelectionAndSmartFormat"
   ' Sortiert und formatiert die aktuelle Auswahl.
   Sub SortSelectionAndSmartFormat()
      If (DTE.ActiveDocument Is Nothing) Then
         Exit Sub
      End If

      If (DTE.ActiveDocument.ReadOnly) Then
         Exit Sub
      End If

      Dim currentTextSelection As TextSelection
      currentTextSelection = DTE.ActiveDocument.Selection

      If (currentTextSelection.TextRanges.Count = 1) Then
         Exit Sub
      End If

      If (DTE.UndoContext.IsOpen) Then
         DTE.UndoContext.Close()
      End If

      Try
         DTE.UndoContext.Open("SortSelectionAndSmartFormat")
         currentTextSelection.SmartFormat()
      Catch
      End Try

      Try
         Dim currentLines() As System.String = Split(currentTextSelection.Text, vbCrLf)

         System.Array.Sort(currentLines)
         currentTextSelection.Delete()

         For Each i As System.String In currentLines
            If Not i = System.String.Empty Then
               currentTextSelection.Insert(i.Trim(vbCrLf) & vbCrLf)
            End If
         Next
         DTE.UndoContext.Close()

      Catch
      End Try

      Exit Sub
   End Sub
#End Region

Einfach als Makro einfügen und bei Bedarf in die Toolbar integrieren oder als Tastenkombination hinzufügen.

(Original link: http://blogs.compactframework.de/Torsten.Weber/2007/12/13/zeilenweise+Sortierung+Markierter+Bereich+In+Visual+Studio+Vs+UltraEdit+Datei+Sortieren.aspx)

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert