VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "Modules"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

' Private internal collection to hold Module objects.
Private p_colModules As Collection

' Private variable to store reference to parent object.
Private p_cvbCustomVBProject As CallTree.CustomVBProject

Function Add(strModuleName As String, _
             lngType As vbext_ComponentType, _
             codCodeModule As VBIDE.CodeModule) As CallTree.Module
    ' Add Module object to Modules collection and return Module
    ' object. Set Name, ModuleType, and CodeModule properties.
    Dim modModule As CallTree.Module
    
    ' Create new Module object.
    Set modModule = New Module
    
    ' Add to private collection.
    p_colModules.Add modModule, strModuleName
    
    ' Set properties for module.
    With modModule
        .Name = strModuleName
        .ModuleType = lngType
        Set .CodeModule = codCodeModule
        ' Return text of module.
        On Error Resume Next
        .FullText = .CodeModule.Lines(1, .CodeModule.CountOfLines)
        On Error GoTo 0
        ' Set Module object's parent to CustomVBProject object.
        ' The Modules collection and Module object both have
        ' a CustomVBProject object as their parent.
        Set .Parent = Me.Parent
        ' Add module's procedures to its Procedures collection.
        AddProcsToCollection modModule
    End With
    
    ' Return reference to new Module object.
    Set Add = modModule
End Function

Property Get Count() As Long
    Count = p_colModules.Count
End Property

Property Get Item(varIndex As Variant) As Module
Attribute Item.VB_UserMemId = 0
    On Error Resume Next
    Set Item = p_colModules.Item(varIndex)
End Property

Private Sub Class_Initialize()
    ' Create new collection to hold Modules collection.
    Set p_colModules = New Collection
End Sub

Public Property Set Parent(cvbCustomVBProject As CallTree.CustomVBProject)
    ' Set Parent property to refer to CustomVBProject object.
    If p_cvbCustomVBProject Is Nothing Then
        Set p_cvbCustomVBProject = cvbCustomVBProject
    End If
End Property

Public Property Get Parent() As CallTree.CustomVBProject
    ' Retrieve object reference stored in Parent property.
    Set Parent = p_cvbCustomVBProject
End Property

Private Function AddProcsToCollection(modModule As CallTree.Module)
    ' Parse module in order to add procedures to module's
    ' Procedures collection.
    Dim prcProc         As CallTree.Procedure
    Dim lngLines        As Long
    Dim lngDeclLines    As Long
    Dim lngCount        As Long
    Dim lngLoop         As Long
    Dim lngItem         As Long
    Dim lngProcCount    As Long
    Dim lngProcType     As Long
    Dim strProcName     As String
    Dim strTemp         As String
    
    ' Add procedures to each module's Procedures collection.
    With modModule.CodeModule
        ' Count lines in module.
        lngLines = .CountOfLines
        ' Count lines in Declaration section in module.
        lngDeclLines = .CountOfDeclarationLines
        ' Determine name and type of first procedure.
        strProcName = .ProcOfLine(lngDeclLines + 1, lngProcType)
        ' Make sure module contains procedures.
        If Len(strProcName) <> 0 Then
            ' Initialize counter.
            lngLoop = lngDeclLines + 1
            Do
                ' Store name of procedure.
                strProcName = .ProcOfLine(lngLoop, lngProcType)
                ' Compare procedure name with name of last added procedure
                ' to determine whether we've reached a new procedure yet.
                If strProcName <> strTemp Then
                    On Error Resume Next
                    Set prcProc = modModule.Procedures(strProcName)
                    If Err <> 0 Then
                        ' Create new Procedure object.
                        Set prcProc = modModule.Procedures.Add(strProcName)
                    End If
                    On Error GoTo 0
                    
                    ' Set properties for new Procedure object.
                    With prcProc
                        .Name = strProcName
                        ' Specify procedure's start line.
                        .StartLine = lngLoop
                        .ModuleName = modModule.Name
                        .ProcType = lngProcType
                        .ProcText = GetProcText(modModule.CodeModule, _
                            strProcName, lngProcType)
                        .CommentText = GetCommentText(modModule.CodeModule, _
                            strProcName, lngProcType)
                        .Scope = ProcScope(strProcName, lngProcType, modModule.CodeModule)
                        ' Procedure's parent is module in which it resides.
                        Set .Parent = modModule
                    End With
                    ' Store name of last added procedure.
                    strTemp = strProcName
                End If
                ' Increment counter.
                lngLoop = lngLoop + 1
            ' Loop until last line in module is reached.
            Loop Until lngLoop >= lngLines
        End If
    End With
End Function

Private Function GetProcText(cmdModule As VBIDE.CodeModule, _
                             strProcName As String, _
                             lngProcType As Long) As String
    ' Returns body of procedure.
    Dim lngStartLine    As Long
    Dim lngBodyLine     As Long
    Dim lngEndProc      As Long
    Dim lngCount        As Long

    With cmdModule
        ' Count lines in procedure.
        lngCount = .ProcCountLines(strProcName, lngProcType)
        ' Determine start line.
        lngStartLine = .ProcStartLine(strProcName, lngProcType)
        ' Determine body line.
        lngBodyLine = .ProcBodyLine(strProcName, lngProcType)
        ' Determine line number of last line in procedure.
        lngEndProc = (lngBodyLine + lngCount - 1) - Abs(lngBodyLine - lngStartLine)
        ' Return body of procedure.
        GetProcText = .Lines(lngBodyLine, (lngEndProc - lngBodyLine) + 1)
    End With
End Function

Private Function GetCommentText(cmdModule As VBIDE.CodeModule, _
                                strProcName As String, _
                                lngProcType As Long) As String
    ' Returns text of comment at beginning of procedure, to be used
    ' as a description for the procedure. This is not failproof,
    ' of course, but it's about the best that we can do.
    Dim lngBodyLine             As Long
    Dim lngEndOfProc            As Long
    Dim lngCountExtBodyLines    As Long
    Dim lngEndLine              As Long
    Dim lngEndCol               As Long
    Dim strLine                 As String
    Dim strComment              As String
    Dim blnFind                 As Boolean

    With cmdModule
        ' Get number of body line for this procedure.
        lngBodyLine = .ProcBodyLine(strProcName, lngProcType)
        ' Get number of last line in procedure.
        lngEndOfProc = .ProcCountLines(strProcName, lngProcType) _
            + lngBodyLine
        ' Specify end of procedure as ending line for search.
        lngEndLine = lngEndOfProc
        
        ' Find first comment, starting with line after body line.
        blnFind = .Find("'", lngBodyLine + 1, 0, _
            lngEndLine, lngEndCol)
        ' If comment was found, check whether subsequent lines
        ' also include comments.
        If blnFind Then
            strLine = .Lines(lngEndLine, 1)
            If InStr(strLine, "'") = 1 Then
            ElseIf Asc(Mid$(strLine, InStr(strLine, "'") - 1, 1)) <> 32 Then
                Exit Function
            End If
            ' Store first line.
            strComment = Trim$(.Lines(lngEndLine, 1))
            Do
                ' Increment line number.
                lngEndLine = lngEndLine + 1
                ' Trim space from left of line.
                strLine = Trim$(.Lines(lngEndLine, 1))
                ' Check whether first character is
                ' an apostrophe ("'").
                If Left(strLine, 1) = "'" Then
                    ' Store line containing comment.
                    strComment = strComment & vbCrLf & _
                        Trim$(.Lines(lngEndLine, 1))
                Else
                    ' If line is not a comment, exit loop.
                    Exit Do
                End If
            Loop
            ' Replace apostrophes and carriage return/line feed.
            strComment = Replace(strComment, "'", " ")
            strComment = Replace(strComment, vbCrLf, " ")
            ' Trim white space from string.
            strComment = TrimSpace(strComment)
        Else
            strComment = ""
        End If
    End With
    GetCommentText = strComment
End Function

Public Function ProcScope(strProcName As String, _
                          lngProcType As Long, _
                          cmdCode As CodeModule) As opgScope
    ' Determines whether a procedure is public.
    Dim lngBodyLine     As Long
    Dim strBodyLine     As String
    Dim astrBodyLine()  As String

    With cmdCode
        ' Get line number for procedure's body line.
        lngBodyLine = .ProcBodyLine(strProcName, lngProcType)
        ' Get text of body line.
        strBodyLine = .Lines(lngBodyLine, 1)
        ' Split body line into an array.
        astrBodyLine = Split(strBodyLine)
        ' Examine first element to determine whether procedure is
        ' Public, Private, or Friend.
        Select Case astrBodyLine(0)
            Case "Public"
                ProcScope = PUBLIC_SCOPE
            Case "Friend"
                ProcScope = FRIEND_SCOPE
            Case "Private"
                ProcScope = PRIVATE_SCOPE
            Case Else
                ' If scope is not specified, procedures are public
                ' by default.
                ProcScope = PUBLIC_SCOPE
        End Select
    End With
End Function

' NewEnum must return the IUnknown interface of a
' collection's enumerator.
Public Function NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
    ' NewEnum function enables For Each...Next for
    ' enumerating this collection.
   Set NewEnum = p_colModules.[_NewEnum]
End Function



