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:

After:

source
share