All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dotty.tools.scaladoc.transformers.ImplicitMembersExtensionTransformer.scala Maven / Gradle / Ivy

There is a newer version: 3.6.3-RC1-bin-20241114-6a7d5d3-NIGHTLY
Show newest version
package dotty.tools.scaladoc
package transformers

class ImplicitMembersExtensionTransformer(using DocContext) extends(Module => Module):
  override def apply(original: Module): Module =
    val classlikeMap = original.members

    def retrieveCompanion(m: Member) = m.companion.flatMap { (_, dri) =>
     val res = classlikeMap.get(dri)
      if res.isEmpty then
        report.warning(s"Companion for class ${m.name} exists but is missing in classlike map")
      res
    }

    def expandMember(outerMembers: Seq[Member])(c: Member): Member =
      val companion = retrieveCompanion(c)

      val allParents = c.parents.flatMap(p => classlikeMap.get(p.dri))

      val parentCompanions = allParents.flatMap(retrieveCompanion)

      // TODO (#220): We can expand this on generic etc
      val implictSources = outerMembers ++ companion.toSeq ++ parentCompanions

      val applicableDRIs = c.parents.map(_.dri).toSet + c.dri

      val MyDri = c.dri
      def collectApplicableMembers(source: Member): Seq[Member] = source.members.flatMap {
        case m @ Member(_, _, _, Kind.Extension(ExtensionTarget(_, _, _, _, MyDri, _), _), Origin.RegularlyDefined) =>
          val kind = m.kind match
            case Kind.Extension(_, d) => d
            case _ => Kind.Def(Nil)

          Seq(m.withOrigin(Origin.ExtensionFrom(source.name, source.dri)).withKind(kind))
        case m @ Member(_, _, _, conversionProvider: ImplicitConversionProvider, Origin.RegularlyDefined) =>
          conversionProvider.conversion match
            case Some(ImplicitConversion(MyDri, to)) =>
              classlikeMap.get(to).toSeq.flatMap { owner =>
                val newMembers = owner.members.filter(_.origin match
                  case Origin.RegularlyDefined => true
                  case _ => false
                )
                newMembers.map(_.withOrigin(Origin.ImplicitlyAddedBy(m.name, m.dri)))
              }
            case _ =>
              Nil
        case other =>
          None
      }

      val newImplicitMembers = implictSources.flatMap(collectApplicableMembers).distinct
      val expandedMembers = c.members.map(expandMember(outerMembers ++ Seq(c)))
      c.withMembers(newImplicitMembers ++ expandedMembers)

    original.updatePackages(_.map(expandMember(Nil)(_)))




© 2015 - 2024 Weber Informatics LLC | Privacy Policy