I'm exporting Scala functions to an external format. For this purpose I use scala.meta
and a StaticAnnotation
. Something like:
@ExportFunctions
object MyFunctions {
def max(x: Int, y: Int): Int = x max y
}
class ExportFunctions extends StaticAnnotation {
inline def apply(defn: Any): Any = meta {
defn match {
case q"object $name extends { ..$earlydefns } with ..$parents { ..$stats }" =>
stats.flatMap{
case defn@Defn.Def(modifiers, fname, tparams, paramss, Some(returnType), body) =>
println(body.syntax)
}
case _ =>
}
defn
}
}
In the implementation of ExportFunctions extends StaticAnnotation
the body of the functions is represented as a desugared tree: x.max(y)
.
However, for the documentation purposes it would be much nicer to have the actual source code. Or at least sugar (x max y
).
Is there a way to preserve the original formatting/sugar?