如何使用Visual Studio中的空格自动对齐类似的代码行

Pet*_*ery 4 c# vb.net visual-studio

是否有现有的宏或插件可以解决这个问题

public string Name { get; set; }
public int Age { get; set; }
public Person Mother { get; set; }
Run Code Online (Sandbox Code Playgroud)

进入这个?

public string Name   { get; set; }
public int    Age    { get; set; }
public Person Mother { get; set; }
Run Code Online (Sandbox Code Playgroud)

我将继续描述我认为直观明显的算法- (对于特定的选择)使每个令牌尽可能地留在一行上,但不要比任何其他任何令牌上的任何令牌更左边.线.

Ric*_*key 12

这是一个原始的"对齐属性"宏,用于演示如何使用Visual Studio宏完成此类功能.

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
Run Code Online (Sandbox Code Playgroud)

之前:

之前

后:

后


Sim*_*ens 10

不是一个完整的解决方案,但Productivity Power Tools有类似的称为"对齐分配":

通过在键入Ctrl + Alt +]时对齐分配,此扩展对于使代码更具可读性非常有用,因此它需要:

_test = test;
_commandTarget = commandTarget;
_state = state;
Run Code Online (Sandbox Code Playgroud)

把它变成这个:

_test          = test;
_commandTarget = commandTarget;
_state         = state;
Run Code Online (Sandbox Code Playgroud)