Last active
June 5, 2018 10:22
-
-
Save MasseGuillaume/f9c1b37d75b443711d92929fa2b34928 to your computer and use it in GitHub Desktop.
scala.meta + Ammonite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val tree = "a.b().c".parse[Term].get | |
pretty(tree) | |
Term.Select( | |
Term.Apply( | |
Term.Select( | |
Term.Name("a"), | |
Term.Name("b") | |
), | |
List() | |
), | |
Term.Name("c") | |
) | |
pretty(tree, showFields = true) | |
Term.Select( | |
qual = Term.Apply( | |
fun = Term.Select( | |
qual = Term.Name("a"), | |
name = Term.Name("b") | |
), | |
args = List() | |
), | |
name = Term.Name("c") | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cat ~/.ammonite/predef.sc | |
import $ivy.`org.typelevel::paiges-core:0.2.1` | |
import $ivy.`org.scalameta::contrib:3.7.3` | |
import $ivy.`org.scalameta::testkit:3.7.3` | |
import scala.meta._ | |
import scala.meta.Token._ | |
import scala.meta.testkit.Corpus | |
import org.typelevel.paiges._ | |
def pretty(tree: Tree, showFields: Boolean = false): String = { | |
def wrapList(args: List[Doc]): Doc = { | |
wrap(Doc.text("List") + Doc.char('('), args, Doc.char(')')) | |
} | |
def wrap(prefix: Doc, args: List[Doc], suffix: Doc): Doc = { | |
val body = Doc.intercalate(Doc.char(',') + Doc.line, args) | |
body.tightBracketBy(prefix, suffix) | |
} | |
def prettyList(vs: List[Tree]): Doc = | |
wrapList(vs.map(v => prettyFromProduct(v))) | |
def prettyFromProduct(tree0: Tree): Doc = { | |
tree0 match { | |
case _ if tree0.tokens.isEmpty => Doc.empty | |
case v: Term.Name => Doc.text(v.structure) | |
case t: Type.Name => Doc.text(t.structure) | |
case _ => { | |
val args = | |
tree0.productFields.zip(tree0.productIterator.toList).map{ case (k, v) => | |
val rhs = | |
v match { | |
case v: Term.Name => Doc.text(v.structure) | |
case t: Tree => prettyFromProduct(t) | |
case o: Option[_] => | |
o match { | |
case Some(t: Tree) => | |
wrap( | |
Doc.text("Some") + Doc.char('('), | |
List(prettyFromProduct(t)), | |
Doc.char(')') | |
) | |
case None => Doc.text("None") | |
case _ => throw new Exception("cannot handle: " + o) | |
} | |
case vs: List[_] => | |
vs match { | |
case Nil => Doc.text("Nil") | |
case (h : Tree) :: _ => { | |
prettyList(vs.asInstanceOf[List[Tree]]) | |
} | |
case (h : List[_]) :: _ => { | |
val vsT = vs.asInstanceOf[List[List[Tree]]] | |
wrapList(vsT.map(prettyList)) | |
} | |
case _ => throw new Exception("cannot handle: " + vs) | |
} | |
case _ => Doc.text(v.toString) | |
} | |
if (showFields) Doc.text(k) + Doc.text(" = ") + rhs | |
else rhs | |
} | |
wrap(Doc.text(tree0.productPrefix) + Doc.char('('), args, Doc.char(')')) | |
} | |
} | |
} | |
prettyFromProduct(tree).render(1) | |
} | |
repl.pprinter() = { | |
val nl = System.lineSeparator | |
pprint.copy(additionalHandlers = { | |
case tree: Tree => pprint.Tree.Literal(nl + pretty(tree)) | |
case tokens: Tokens => pprint.Tree.Literal(tokens.structure) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment