Imports EnvDTE
Imports EnvDTE80
Imports Microsoft.VisualBasic
Imports System
Imports System.Windows.Forms

Public Module TextSelectionMacros

    Private DTE2 As EnvDTE80.DTE2 = CType(DTE, EnvDTE80.DTE2)
    Private output As OutputWindowPane = GetOutputWindowPane("Text Selection Macros")

    Sub DisplayTextSelectionVirtualPoints()
        ' Description: Displays the VirtualPoint objects associated with the TopPoint
        '              and BottomPoint properties of the TextSelection object

        Dim doc As Document = DTE.ActiveDocument

        If Not doc Is Nothing Then
            Dim txtSel As TextSelection = doc.Selection

            output.OutputString(VirtualPointStats(txtSel.TopPoint, "TopPoint (VirtualPoint)") & vbCrLf)
            output.OutputString(VirtualPointStats(txtSel.BottomPoint, "BottomPoint (VirtualPoint)") & vbCrLf & vbCrLf)
        End If
    End Sub

    Sub DisplayTextSelectionEditPoints()
        ' Description: Displays the EditPoint objects associated with the TopPoint
        '              and BottomPoint properties of the TextSelection object

        output.Clear()
        output.OutputString("--- DisplayTextSelectionEditPoints ---" & vbCrLf & vbCrLf)

        Dim doc As Document = DTE.ActiveDocument

        If Not doc Is Nothing Then
            Dim txtSel As TextSelection = doc.Selection

            output.OutputString(EditPointStats(txtSel.TopPoint.CreateEditPoint(), "TopPoint (EditPoint)") & vbCrLf)
            output.OutputString(EditPointStats(txtSel.BottomPoint.CreateEditPoint(), "BottomPoint (EditPoint)") & vbCrLf & vbCrLf)
        End If
    End Sub

    Sub DisplayTextSelectionText()
        ' Description: Displays the selected text

        output.Clear()
        output.OutputString("--- DisplayTextSelectionText ---" & vbCrLf & vbCrLf)

        Dim doc As Document = DTE.ActiveDocument

        If Not doc Is Nothing Then
            Dim txtSel As TextSelection = doc.Selection

            output.OutputString("Selected text: '" & txtSel.Text & "'" & vbCrLf)
        End If
    End Sub

    Sub DisplayTextRangesText()
        ' Description: Displays the text for each TextRange of the selection

        output.Clear()
        output.OutputString("--- DisplayTextRangesText ---" & vbCrLf & vbCrLf)

        Dim doc As Document = DTE.ActiveDocument

        If Not doc Is Nothing Then
            Dim txtSel As TextSelection = doc.Selection
            Dim line As TextRange
            Dim i As Integer = 0

            output.OutputString("TextSelection.Text: " & txtSel.Text & vbCrLf & vbCrLf)

            For Each line In txtSel.TextRanges
                i = i + 1
                output.OutputString("TextRange " & i.ToString() & " text: " & _
                    line.StartPoint.GetText(line.EndPoint) & vbCrLf)
            Next
        End If
    End Sub

    Sub SwapAnchor()
        ' Description: Swaps the AnchorPoint and ActivePoint of the selection

        Dim doc As Document = DTE.ActiveDocument

        If Not doc Is Nothing Then
            Dim txtSel As TextSelection = doc.Selection

            txtSel.SwapAnchor()
        End If
    End Sub

End Module
