'Visual Studio'에 해당되는 글 1건

  1. 2009/11/04 김성민 VSMacro/ 선택된 라인들에서 중복된 라인들은 삭제하고 나머지를 정렬하기
제목 그대로 선택된 라인들에서 중복된 라인들은 삭제하고 나머지를 정렬하기 위한 매크로입니다.

Visual Assist에 선택된 라인 정렬 기능이 있기는 한데, 중복 라인 삭제는 없어서, 어쩔 수 없이 만들었습니다. 주 목적은 아무래도 C++/include 구문 정리가 되겠습니다.

Collection 객체에 정렬 함수가 따로 없다는 것이 좀 의외군요.

Function Strip(ByVal strLine As String)
    If Len(strLine) > 0 Then       
        nBegin = 1
        nEnd = Len(strLine)
        For i = 1 To Len(strLine)
            c = Mid(strLine, i, 1)
            If c <> " " And c <> Tab And c <> Lf And c <> Cr Then
                nBegin = i
                Exit For
            End If
        Next
        For i = 1 To Len(strLine)
            c = Mid(strLine, Len(strLine) - i + 1, 1)
            If c <> " " And c <> Tab And c <> Lf And c <> Cr Then
                nEnd = Len(strLine) - i + 1
                Exit For
            End If
        Next
        Return Mid(strLine, nBegin, nEnd - nBegin + 1)
    Else
        Return ""
    End If
End Function

Sub SortCollection(ByRef oCollection As Collection, Optional ByVal bSortAscending As Boolean = True)
    Dim lSort1 As Integer
    Dim lSort2 As Integer
    Dim vTempItem1 As Object
    Dim vTempItem2 As Object
    Dim bSwap As Boolean

    For lSort1 = 1 To oCollection.Count - 1
        For lSort2 = lSort1 + 1 To oCollection.Count
            If bSortAscending Then
                If oCollection(lSort1) > oCollection(lSort2) Then
                    bSwap = True
                Else
                    bSwap = False
                End If
            Else
                If oCollection(lSort1) < oCollection(lSort2) Then
                    bSwap = True
                Else
                    bSwap = False
                End If
            End If
            If bSwap Then
                vTempItem1 = oCollection(lSort1)
                vTempItem2 = oCollection(lSort2)
                oCollection.Add(vTempItem1, Nothing, lSort2)
                oCollection.Add(vTempItem2, Nothing, lSort1)
                oCollection.Remove(lSort1 + 1)
                oCollection.Remove(lSort2 + 1)
            End If
        Next
    Next
End Sub

Sub SortAndRemoveDuplicatedLine()
    Dim objLines As New Collection
    Dim objSel As TextSelection = ActiveDocument().Selection
    Dim objRanges As TextRanges = objSel.TextRanges
    Dim objStartPt As EditPoint = objRanges.Item(1).StartPoint.CreateEditPoint()
    Dim objStream As New StringBuilder

    For Each strLine In objSel.Text.Split(Lf)
        strLine = Strip(strLine)
        If objLines.Contains(strLine) = False Then
            objLines.Add(strLine, strLine)
        End If
    Next

    SortCollection(objLines)

    For Each strLine In objLines
        objStream.AppendLine(strLine)
    Next

    objSel.Text = ""
    objStartPt.Insert(objStream.ToString())
End Sub

이 포스트 내용에 대한 이후 업데이트는 http://serious-code.net/moin.cgi/VisualStudioMacro 페이지에서만 이루어질 예정입니다. :)
2009/11/04 16:33 2009/11/04 16:33
받은 트랙백이 없고, 댓글이 없습니다.

댓글+트랙백 RSS :: http://serious-code.net/tc/rss/response/17

댓글+트랙백 ATOM :: http://serious-code.net/tc/atom/response/17