Skip to content

Add inParens, inSquareParens and inBlock #4707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 72 additions & 102 deletions library/src/scala/tasty/util/ShowSourceCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
indent -= 1
}

def inParens(body: => Unit): Buffer = {
this += "("
body
this += ")"
}

def inSquareParens(body: => Unit): Buffer = {
this += "["
body
this += "]"
}
Copy link
Contributor

@liufengyun liufengyun Jun 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inBrackets to match inParens? Or inSquare?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change in next pr


def inBlock(body: => Unit): Buffer = {
this += " {"
indented {
this += lineBreak()
body
}
this += lineBreak() += "}"
}

Copy link
Contributor

@liufengyun liufengyun Jun 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inBraces to match inParens?

edit: inBlock is fine as it is.

def result(): String = sb.result()

def lineBreak(): String = "\n" + (" " * indent)
Expand All @@ -60,12 +81,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
case _ =>
this += "package "
printType(name.tpe)
this += " {"
indented {
this += lineBreak()
printTrees(stats1, lineBreak())
}
this += lineBreak() += "}"
inBlock(printTrees(stats1, lineBreak()))
}

case Import(expr, selectors) =>
Expand Down Expand Up @@ -110,15 +126,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
def printParent(parent: Parent): Unit = parent match {
case parent @ Term.TypeApply(fun, targs) =>
printParent(fun)
this += "["
printTypeOrBoundsTrees(targs, ", ")
this += "]"
inSquareParens(printTypeOrBoundsTrees(targs, ", "))

case parent @ Term.Apply(fun, args) =>
printParent(fun)
this += "("
printTrees(args, ", ")
this += ")"
inParens(printTrees(args, ", "))

case parent @ Term.Select(Term.New(tpt), _, _) =>
printTypeTree(tpt)
Expand Down Expand Up @@ -218,17 +230,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
}

case Term.While(cond, body) =>
this += "while ("
printTree(cond)
this += ") "
this += "while "
inParens(printTree(cond)) += " "
printTree(body)

case Term.DoWhile(body, cond) =>
this += "do "
printTree(body)
this += " while ("
printTree(cond)
this += ")"
printTree(body) += " while "
inParens(printTree(cond))

case ddef @ DefDef(name, targs, argss, tpt, rhs) =>
printDefAnnotations(ddef)
Expand Down Expand Up @@ -300,9 +309,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
case _ => args
}

this += "("
printTrees(args1, ", ")
this += ")"
inParens(printTrees(args1, ", "))

case Term.TypeApply(fn, args) =>
printTree(fn)
Expand All @@ -311,9 +318,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
// type bounds already printed in `fn`
this
case _ =>
this += "["
printTypeOrBoundsTrees(args, ", ")
this += "]"
inSquareParens(printTypeOrBoundsTrees(args, ", "))
}

case Term.Super(qual, idOpt) =>
Expand All @@ -324,7 +329,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
this += "super"
for (id <- idOpt) {
val Id(name) = id
this += "[" += name += "]"
inSquareParens(this += name)
}
this

Expand All @@ -333,21 +338,21 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
case Types.Repeated(_) =>
printTree(term)
case _ =>
this += "("
printTree(term)
this += ": "
def printTypeOrAnnots(tpe: Type): Unit = tpe match {
case Type.AnnotatedType(tp, annot) if tp == term.tpe =>
printAnnotation(annot)
case Type.AnnotatedType(tp, annot) =>
printTypeOrAnnots(tp)
this += " "
printAnnotation(annot)
case tpe =>
printType(tpe)
inParens {
printTree(term)
this += ": "
def printTypeOrAnnots(tpe: Type): Unit = tpe match {
case Type.AnnotatedType(tp, annot) if tp == term.tpe =>
printAnnotation(annot)
case Type.AnnotatedType(tp, annot) =>
printTypeOrAnnots(tp)
this += " "
printAnnotation(annot)
case tpe =>
printType(tpe)
}
printTypeOrAnnots(tpt.tpe)
}
printTypeOrAnnots(tpt.tpe)
this += ")"
}

case Term.Assign(lhs, rhs) =>
Expand All @@ -365,11 +370,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
case Term.Lambda(_, _) =>
// Decompile lambda from { def annon$(...) = ...; closure(annon$, ...)}
val DefDef(_, _, args :: Nil, _, Some(rhs)) :: Nil = stats
this += "("
printArgsDefs(args)
this += " => "
printTree(rhs)
this += ")"
inParens {
printArgsDefs(args)
this += " => "
printTree(rhs)
}
case _ =>
this += "{"
indented {
Expand All @@ -390,32 +395,24 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
this

case Term.If(cond, thenp, elsep) =>
this += "if ("
printTree(cond)
this += ") "
this += "if "
inParens(printTree(cond))
this += " "
printTree(thenp)
this+= " else "
printTree(elsep)

case Term.Match(selector, cases) =>
printTree(selector)
this += " match {"
indented {
this += lineBreak()
printCases(cases, lineBreak())
}
this += lineBreak() += "}"
this += " match"
inBlock(printCases(cases, lineBreak()))

case Term.Try(body, cases, finallyOpt) =>
this += "try "
printTree(body)
if (cases.nonEmpty) {
this += " catch {"
indented {
this += lineBreak()
printCases(cases, lineBreak())
}
this += lineBreak() += "}"
this += " catch"
inBlock(printCases(cases, lineBreak()))
}
finallyOpt match {
case Some(t) =>
Expand Down Expand Up @@ -552,9 +549,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
printSeparated(xs)
}

this += "["
printSeparated(targs)
this += "]"
inSquareParens(printSeparated(targs))
}
}

Expand Down Expand Up @@ -583,9 +578,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
this += ", "
printSeparated(xs)
}
this += "["
printSeparated(tparams)
this += "]"
inSquareParens(printSeparated(tparams))
if (isMember) {
this += " = "
printTypeOrBoundsTree(body)
Expand All @@ -597,8 +590,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
}
}

def printArgsDefs(args: List[ValDef]): Unit = {
this += "("
def printArgsDefs(args: List[ValDef]): Unit = inParens {
args match {
case Nil =>
case arg :: _ =>
Expand All @@ -616,7 +608,6 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
}

printSeparated(args)
this += ")"
}

def printAnnotations(trees: List[Term]): Buffer = {
Expand Down Expand Up @@ -685,14 +676,10 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
case Term.TypeApply(Term.Select(extractor, "unapply" | "unapplySeq", _), _) => printTree(extractor)
case _ => throw new MatchError(fun.show)
}
this += "("
printPatterns(patterns, ", ")
this += ")"
inParens(printPatterns(patterns, ", "))

case Pattern.Alternative(trees) =>
this += "("
printPatterns(trees, " | ")
this += ")"
inParens(printPatterns(trees, " | "))

case Pattern.TypeTest(tpt) =>
this += "_: "
Expand All @@ -716,9 +703,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
case Constant.Char(v) => this += '\'' += escapedChar(v) += '\''
case Constant.String(v) => this += '"' += escapedString(v) += '"'
case Constant.ClassTag(v) =>
this += "classOf["
printType(v)
this += "]"
this += "classOf"
inSquareParens(printType(v))
}

def printTypeOrBoundsTree(tpt: TypeOrBoundsTree): Buffer = tpt match {
Expand Down Expand Up @@ -771,18 +757,11 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty

case TypeTree.Refined(tpt, refinements) =>
printTypeTree(tpt)
this += " {"
indented {
this += lineBreak()
printTrees(refinements, "; ")
}
this += lineBreak() += "}"
inBlock(printTrees(refinements, "; "))

case TypeTree.Applied(tpt, args) =>
printTypeTree(tpt)
this += "["
printTypeOrBoundsTrees(args, ", ")
this += "]"
inSquareParens(printTypeOrBoundsTrees(args, ", "))

case TypeTree.Annotated(tpt, annot) =>
val Annotation(ref, args) = annot
Expand Down Expand Up @@ -873,9 +852,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
this += "_*"
case _ =>
printType(tp)
this += "["
printTypesOrBounds(args, ", ")
this += "]"
inSquareParens(printTypesOrBounds(args, ", "))
}

case Type.AnnotatedType(tp, annot) =>
Expand Down Expand Up @@ -914,9 +891,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
}

case Type.TypeLambda(paramNames, tparams, body) =>
this += "["
printMethodicTypeParams(paramNames, tparams)
this += "] => "
inSquareParens(printMethodicTypeParams(paramNames, tparams))
this += " => "
printTypeOrBound(body)

case Type.ParamRef(lambda, idx) =>
Expand Down Expand Up @@ -948,9 +924,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
val Annotation(ref, args) = annot
this += "@"
printTypeTree(ref)
this += "("
printTrees(args, ", ")
this += ")"
inParens(printTrees(args, ", "))
}

def printDefAnnotations(definition: Definition): Buffer = {
Expand All @@ -971,14 +945,10 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
def printRefinement(tpe: Type): Buffer = {
def printMethodicType(tp: TypeOrBounds): Unit = tp match {
case tp @ Type.MethodType(paramNames, params, res) =>
this += "("
printMethodicTypeParams(paramNames, params)
this += ")"
inParens(printMethodicTypeParams(paramNames, params))
printMethodicType(res)
case tp @ Type.TypeLambda(paramNames, params, res) =>
this += "["
printMethodicTypeParams(paramNames, params)
this += "]"
inSquareParens(printMethodicTypeParams(paramNames, params))
printMethodicType(res)
case Type.ByNameType(t) =>
this += ": "
Expand Down