How to automatically align similar lines of code with spaces in Visual Studio

Is there an existing macro or plugin that will turn this

public string Name { get; set; }
public int Age { get; set; }
public Person Mother { get; set; }

in it?

public string Name   { get; set; }
public int    Age    { get; set; }
public Person Mother { get; set; }

I’ll talk about an algorithm that, in my opinion, is intuitive - (for a specific choice) makes each token on the line as left as possible, but not more left than any token of the same index on any other lines.

+3
source share
2 answers

Here's a primitive Align Properties macro to demonstrate how to implement this type of functionality using Visual Studio macros.

Sub AlignProperties()
    Dim win As EnvDTE.Window = DTE.ActiveWindow
    If win.Type <> EnvDTE.vsWindowType.vsWindowTypeDocument Then
        MsgBox("This macro can only be run in an active text editor window.")
        Exit Sub
    End If

    ' Determine the affected lines.
    Dim startLine As Integer = DTE.ActiveDocument.Selection.TopLine
    Dim endLine As Integer = DTE.ActiveDocument.Selection.BottomLine
    If endLine < startLine Then
        Dim temp As Integer = startLine
        startLine = endLine
        endLine = temp
    End If
    endLine = endLine - 1

    ' Parse the four columns: modifier, type, identifier, and rest.
    Dim regex = New Regex("(\s+)(.*)\s+(.*)\s+(.*)\s+({.*)")
    Dim leading As String = Nothing
    Dim array(endLine - startLine, 3) As String
    Dim widths(3) As Integer
    For i As Integer = 0 To endLine - startLine
        DTE.ActiveDocument.Selection.GotoLine(startLine + i)
        DTE.ActiveDocument.Selection.SelectLine()
        Dim line As String = DTE.ActiveDocument.Selection.text()
        Dim match As Match = regex.Match(line)
        If leading = Nothing Then
            leading = match.Groups(1).ToString()
        End If
        For j As Integer = 0 To 3
            Dim text As String = match.Groups(j + 2).ToString()
            array(i, j) = text
            widths(j) = Math.Max(widths(j), text.Length)
        Next
    Next
    widths(3) = 0

    ' Align the four columns.
    DTE.UndoContext.Open("Align Properties")
    Try
        For i As Integer = 0 To endLine - startLine
            DTE.ActiveDocument.Selection.GotoLine(startLine + i)
            DTE.ActiveDocument.Selection.SelectLine()
            Dim line As String = DTE.ActiveDocument.Selection.text()
            Dim replacement = leading
            For j As Integer = 0 To 3
                Dim padded As String = array(i, j).PadRight(widths(j) + 1)
                replacement = replacement & padded
            Next
            DTE.ActiveDocument.Selection.text() = replacement
        Next
    Finally
        DTE.UndoContext.Close()
    End Try
End Sub

Before:

Before

After:

After

+12
source

, Power Tools " ":

, , Ctrl + Alt +], :

_test = test;
_commandTarget = commandTarget;
_state = state;

:

_test          = test;
_commandTarget = commandTarget;
_state         = state;
+10

Source: https://habr.com/ru/post/1788171/


All Articles