How to preserve sugar, formatting, and whitespace in macro annotations (inline-meta)?

135 views Asked by At

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 [email protected](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?

1

There are 1 answers

0
Ólafur Páll Geirsson On

As of 3.0.0-M8, scala.meta paradise macro annotations are implemented as tree conversions from scala-reflect trees to scala.meta trees. This approach does not make it possible to get trivia such as formatting or comments so the quick answer to your question is: no, not with macro annotations.

However, scala.meta has an api with first-class support for details such as formatting and comments. Depending on your application, you may not need macro annotations, you can use the scala.meta parser directly as a library.