override fun buildChildren()

in intellij/src/main/kotlin/motif/intellij/hierarchy/ScopeHierarchyTreeStructure.kt [73:281]


  override fun buildChildren(descriptor: HierarchyNodeDescriptor): Array<Any> {
    val descriptors: ArrayList<HierarchyNodeDescriptor> = ArrayList(1)
    when (descriptor) {
      is ScopeHierarchyRootDescriptor -> {
        graph.roots.sortedWith(ScopeComparator).forEach { scope ->
          descriptors.add(
              ScopeHierarchyScopeDescriptor(
                  myProject,
                  graph,
                  descriptor,
                  (scope.clazz as IntelliJClass).psiClass,
                  scope,
              ),
          )
        }
      }
      is ScopeHierarchyScopeAncestorDescriptor -> {
        graph.getParentEdges(descriptor.scope).sortedWith(ScopeEdgeParentComparator).forEach { edge
          ->
          descriptors.add(
              ScopeHierarchyScopeAncestorDescriptor(
                  myProject,
                  graph,
                  descriptor,
                  (edge.parent.clazz as IntelliJClass).psiClass,
                  edge.parent,
              ),
          )
        }
      }
      is ScopeHierarchyScopeDescriptor -> {
        graph.getChildEdges(descriptor.scope).sortedWith(ScopeEdgeChildComparator).forEach { edge ->
          descriptors.add(
              ScopeHierarchyScopeDescriptor(
                  myProject,
                  graph,
                  descriptor,
                  (edge.child.clazz as IntelliJClass).psiClass,
                  edge.child,
              ),
          )
        }
      }
      is ScopeHierarchySourcesSectionDescriptor -> {
        getVisibleSources(graph, descriptor.scope).sortedWith(SourceComparator).forEach { source ->
          descriptors.add(ScopeHierarchySourceDescriptor(myProject, graph, descriptor, source))
        }
        if (descriptors.isEmpty()) {
          descriptors.add(
              ScopeHierarchySimpleDescriptor(
                  myProject,
                  graph,
                  descriptor,
                  descriptor.element,
                  LABEL_SCOPE_NO_PROVIDE,
              ),
          )
        }
      }
      is ScopeHierarchySinksSectionDescriptor -> {
        graph.getSinks(descriptor.scope).sortedWith(SinkComparator).forEach { sink ->
          descriptors.add(ScopeHierarchySinkDescriptor(myProject, graph, descriptor, sink))
        }
        if (descriptors.isEmpty()) {
          descriptors.add(
              ScopeHierarchySimpleDescriptor(
                  myProject,
                  graph,
                  descriptor,
                  descriptor.element,
                  LABEL_SCOPE_NO_CONSUME,
              ),
          )
        }
      }
      is ScopeHierarchySourcesAndSinksSectionDescriptor -> {
        descriptors.add(
            ScopeHierarchySourcesSectionDescriptor(
                myProject,
                graph,
                descriptor,
                descriptor.element,
                descriptor.scope,
                true,
            ),
        )
        descriptors.add(
            ScopeHierarchySinksSectionDescriptor(
                myProject,
                graph,
                descriptor,
                descriptor.element,
                descriptor.scope,
                true,
            ),
        )
      }
      is ScopeHierarchyDependenciesSectionDescriptor -> {
        val dependencies: Dependencies? = descriptor.scope.dependencies
        if (dependencies != null) {
          dependencies.methods.sortedWith(MethodComparator).forEach { method ->
            descriptors.add(
                ScopeHierarchyDependencyDescriptor(
                    myProject,
                    graph,
                    descriptor,
                    (method.method as IntelliJMethod).psiMethod,
                    method,
                ),
            )
          }
        }
        if (descriptors.isEmpty()) {
          descriptors.add(
              ScopeHierarchySimpleDescriptor(
                  myProject,
                  graph,
                  descriptor,
                  descriptor.psiElement!!,
                  LABEL_SCOPE_NO_DEPENDENCIES,
              ),
          )
        }
      }
      is ScopeHierarchySinkDetailsDescriptor -> {
        // returns no children
      }
      is ScopeHierarchySourceDetailsDescriptor -> {
        // returns no children
      }
      is ScopeHierarchySinkDescriptor -> {
        graph.getProviders(descriptor.sink).sortedWith(SourceComparator).forEach { source ->
          descriptors.add(
              ScopeHierarchySourceDetailsDescriptor(myProject, graph, descriptor, source),
          )
        }
      }
      is ScopeHierarchySourceDescriptor -> {
        graph.getConsumers(descriptor.source).sortedWith(SinkComparator).forEach { sink ->
          descriptors.add(ScopeHierarchySinkDetailsDescriptor(myProject, graph, descriptor, sink))
        }
      }
      is ScopeHierarchyRootErrorDescriptor -> {
        graph.errors.forEach { error ->
          val errorMessage: ErrorMessage = ErrorMessage.get(error)
          descriptors.add(
              ScopeHierarchyErrorDescriptor(myProject, graph, descriptor, error, errorMessage),
          )
        }
      }
      is ScopeHierarchyUsageSectionDescriptor -> {
        val countSources: Int =
            ScopeHierarchyUtils.getUsageCount(
                project,
                graph,
                descriptor.clazz,
                includeSources = true,
                includeSinks = false,
            )
        val countSinks: Int =
            ScopeHierarchyUtils.getUsageCount(
                project,
                graph,
                descriptor.clazz,
                includeSources = false,
                includeSinks = true,
            )
        if (countSources > 0) {
          descriptors.add(
              ScopeHierarchyUsageSourcesSectionDescriptor(
                  myProject,
                  graph,
                  descriptor,
                  descriptor.clazz,
              ),
          )
        }
        if (countSinks > 0) {
          descriptors.add(
              ScopeHierarchyUsageSinksSectionDescriptor(
                  myProject,
                  graph,
                  descriptor,
                  descriptor.clazz,
              ),
          )
        }
      }
      is ScopeHierarchyUsageSourcesSectionDescriptor -> {
        val elementType: PsiType =
            PsiElementFactory.SERVICE.getInstance(project).createType(descriptor.clazz)
        val type: IrType = IntelliJType(project, elementType)
        graph.getSources(type).sortedWith(SourceComparator).forEach { source ->
          descriptors.add(
              ScopeHierarchySourceDetailsDescriptor(myProject, graph, descriptor, source),
          )
        }
      }
      is ScopeHierarchyUsageSinksSectionDescriptor -> {
        val elementType: PsiType =
            PsiElementFactory.SERVICE.getInstance(project).createType(descriptor.clazz)
        val type: IrType = IntelliJType(project, elementType)
        graph.getSinks(type).sortedWith(SinkComparator).forEach { sink ->
          descriptors.add(ScopeHierarchySinkDetailsDescriptor(myProject, graph, descriptor, sink))
        }
      }
    }
    return descriptors.toTypedArray()
  }