Skip to content

Instantly share code, notes, and snippets.

@paulp
Created February 10, 2016 17:23
Show Gist options
  • Save paulp/8be9068ed2fc1b280cc5 to your computer and use it in GitHub Desktop.
Save paulp/8be9068ed2fc1b280cc5 to your computer and use it in GitHub Desktop.
def treeDoc(t: Tree): Doc = t match {
case PackageDef(mods, pid, xs) => mods <> "package" <+> pid <+> xs.inBracesBlock
case Import(expr, selectors) => "import" <+> expr <> dot <> selectors
case NamedImport(name) => name
case TypeIdent(pre, name) => (pre :+ name).joinDotted
case TermIdent(pre, name) => (pre :+ name).joinDotted
case ClassDef(mods, keyword, name, tparams, vparamss, impl) => linebreak <> mods <> keyword <+> name <> tparams <> tight(vparamss) <> doTemplate(line <> "extends", impl)
case TypeParam(mods, name, tparams, constraints) => mods <> name <> tparams <> constraints
case ValueParam(mods, name, tpt, defaultValue) => mods <> name <> tpt <> opt(space <> assign, defaultValue)
case x: Name => x.value
case Singleton(tpt) => tpt <> dot <> "type"
case Throw(expr) => "throw" <+> expr
case WildcardImport => "_"
case SelectorList(selector :: Nil) => selector
case SelectorList(selectors) => selectors.inBracesList
case SuperCall(tpt, argss) => tpt <> tight(argss)
case xs: FragmentList[_] => xs.doc
case ParenExpr(expr) => Seq[Doc](expr).inParens
case AnnotationExpr(fun, args) => "@" <> fun <> args <> linebreak
case BinaryOp(lhs, name, targs, rhs) => (lhs <+> name <> targs.optBrackets <+> rhs).inParens
case PostfixOp(lhs, name, targs) => (lhs <+> name <> targs.optBrackets).inParens
case Ascription(tpt) => opt(colon, tpt)
case SelfTypeTree(name, tpt) => name <> tpt <+> "=>" <> linebreak
case Match(scrutinee, CaseDefList(cases)) => scrutinee <+> "match" <+> cases.inBracesBlock
case StableIdPattern(id) => id
case VariablePattern(name) => name
case ConstructorPattern(name, targs, args) => name <> targs <> args
case Guard(expr) => opt(space <> "if", expr)
case CaseDef(pat, guard, rhs) => words("case", pat, guard, "=>", rhs)
case Block(stats) => stats.inBracesBlock
case Select(qual, name) => qual <> dot <> name
case Varargs(tpt) => tpt <> "*"
case ByName(tpt) => "=>" <+> tpt
case ProductTypeTree(tps) => tps.inParens
case ProductTermTree(xs) => xs.inParens
case Typed(expr, tpt) => expr <> tpt
case TypeMember(mods, name, tparams, constraints) => mods <> "type" <+> name <> tparams <> constraints
case New(template) => doTemplate("new", template)
case AppliedTypeTree(tpt, args) => tpt <> args
case CompoundTypeTree(left, right) => left <+> "with" <+> right
case TypeAlias(tpt) => words('=', tpt)
case FunctionTypeTree(in, out) => words(in, "=>", out)
case Literal(Constant(x: String)) => "\"" + x + "\""
case Literal(Constant(x)) => s"$x"
case UpperBound(tpt) => space <> "<:" <+> tpt
case LowerBound(tpt) => space <> ">:" <+> tpt
case ContextBound(tpt) => ":" <+> tpt
case Apply(fun, args) => fun <> args
case TypeApply(fun, args) => fun <> args
case VarianceModifier(str) => str
case AccessModifier(access, NoName) => access.doc <> space
case AccessModifier(access, qual) => access.doc <> qual.doc.inBrackets <> space
case OtherModifier(str) => str <> space
case ValueMember(mods, keyword, name, tpt, rhs) => mods <> keyword <+> name <> tpt <+> opt(assign, rhs)
case DefDef(mods, name, tparams, vparamss, tpt, rhs) => mods <> "def" <+> name <> tparams <> tight(vparamss) <> tpt <> opt(space <> assign, rhs)
case EarlyDefs(early) => early.inBracesBlock
case This() => "this"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment