Idea scala Plugin to support macro

Hi Community!

Help me please. I have some problem.

for example:

trait Make[A, B]

@identity
case class A(f1: B, f2: Int)

case class B(b1:String)

It should be like this:

object A {

object f1 extends Make[A, B] {
object b1 extends Make[B, String]
}

object f2 extends Make[A, Int]

}

I don't know how it make (. How get fields from ScClassParameterImpl? It is possible?

private def generateInnerObject(obj: ScObject): Seq[String] = {

val clazz = obj.fakeCompanionClassOrCompanionClass.asInstanceOf[ScClass]
val fields = clazz.allVals.collect({ case (f: ScClassParameterImpl, _) => f }).filter(_.isCaseClassVal)

fields.map({ i =>
val innerFields = ...
if (clazz.typeParameters.isEmpty)
s"""object ${i.name} extends org.jetbrains.example.injector.Make[${clazz.qualifiedName}, ${i.getType(TypingContext.empty).map(_.canonicalText).getOrElse("Any")}]
${innerFields.mkString(";")}
"""
else
s"""object ${i.name} extends org.jetbrains.example.injector.Make[${clazz.qualifiedName}, ${i.typeElement.get.calcType}]
${innerFields.mkString(";")}
"""
})

}

Thank you. I'm sorry for my english :)

1 comment
Comment actions Permalink
Official comment

To extract parameters from class use clazz.parameters instead of allVals. Look at this example:

val clazz: ScClass = ???
implicit val typeSystem = clazz.typeSystem
val parameters = clazz.parameters
val parameter = parameters.head
val innerParameters = parameter.getType(TypingContext.empty).getOrAny.extractClass(clazz.getProject) match {
case Some(clazz: ScClass) => clazz.parameters // inner parameters
case _ => Seq.empty
}

Please sign in to leave a comment.