Imports EnvDTE
Imports System.Diagnostics

Public Module InvertLine

    ' These macros show different ways to swap
    ' a line around the equal (=) sign. They do nothing
    ' if the line has no equal sign.

    Public Sub InvertAssignmentLine()

        ' Retrieve the text.
        Dim str As String
        Dim i As Integer
        DTE.ActiveDocument.Selection.SelectLine()
        str = DTE.ActiveDocument.Selection.Text

        ' Trim the final hard return.
        str = Left(str, Len(str) - 2)

        ' Find the equal sign.
        i = InStr(str, "=")

        ' Reverse the text if it had an equal sign.
        If i > 0 Then
            str = Mid(str, i + 1) & "=" & Left(str, i - 1)
            DTE.ActiveDocument.Selection.Text = str & vbNewLine
        End If

        ' "De-select" the current line.
        DTE.ActiveDocument.Selection.Collapse()

    End Sub

    Public Sub InvertAssignmentLine_TheDOTNETWay()

        ' Retrieve the text.
        Dim str As String
        Dim i As Integer
        DTE.ActiveDocument.Selection.SelectLine()
        str = DTE.ActiveDocument.Selection.Text

        ' Trim the final hard return.
        str = str.Substring(0, str.Length - 2)

        i = str.IndexOf("=")

        ' Reverse the text if it had an equal sign.
        If i > 0 Then
            str = str.Substring(i + 1) & "=" & str.Substring(1, i - 1)
            DTE.ActiveDocument.Selection.Text = str & vbNewLine
        End If

        ' "De-select" the current line.
        DTE.ActiveDocument.Selection.Collapse()

    End Sub

    Public Sub InvertAssignmentLine_AnotherApproach()

        With DTE.ActiveDocument.Selection

            ' Retrieve the text.
            Dim str As String
            .SelectLine()
            str = .Text

            ' Look for the equal sign.
            Dim i As Integer
            For i = 1 To str.Length
                If str.Chars(i - 1) = "=" Then
                    Exit For
                End If
            Next

            ' Reverse the text if it had an equal sign.
            If i < str.Length Then
                str = str.Substring(i, str.Length - i - 1).Trim & "=" & str.Substring(1, i - 2).Trim
                .Text = str & System.Environment.NewLine
            End If

            ' "De-select" the current line.
            .Collapse()

        End With
    End Sub

End Module
