Skip to content

Commit aacc3d5

Browse files
committed
Summary printing
Added general way to produce summaries when shwowing trees or types. Summaries have limited, configrable recusion depth. fix showSummary
1 parent a3f9574 commit aacc3d5

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

src/dotty/tools/dotc/config/Config.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ object Config {
2727
* Disadvantage: It might hide inconsistencies, so while debugging it's better to turn it off
2828
*/
2929
final val newMatch = false
30+
31+
/** The recursion depth for showing a summarized string */
32+
final val summarizeDepth = 2
3033
}

src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Contexts.Context, Scopes.Scope, Denotations.Denotation, Annotations.Annot
77
import StdNames.nme
88
import ast.Trees._, ast.untpd
99
import java.lang.Integer.toOctalString
10+
import config.Config.summarizeDepth
1011
import scala.annotation.switch
1112

1213
class PlainPrinter(_ctx: Context) extends Printer {
@@ -15,20 +16,21 @@ class PlainPrinter(_ctx: Context) extends Printer {
1516
protected def maxToTextRecursions = 100
1617

1718
protected final def controlled(op: => Text): Text =
18-
if (ctx.toTextRecursions < maxToTextRecursions)
19+
if (ctx.toTextRecursions < maxToTextRecursions && ctx.toTextRecursions < maxSummarized)
1920
try {
2021
ctx.toTextRecursions += 1
2122
op
2223
} finally {
2324
ctx.toTextRecursions -= 1
2425
}
2526
else {
26-
recursionLimitExceeded()
27+
if (ctx.toTextRecursions >= maxToTextRecursions)
28+
recursionLimitExceeded()
2729
"..."
2830
}
2931

3032
protected def recursionLimitExceeded() = {
31-
ctx.warning("Exceeded recursion depth attempting to print type.")
33+
ctx.warning("Exceeded recursion depth attempting to print.")
3234
(new Throwable).printStackTrace
3335
}
3436

@@ -393,6 +395,17 @@ class PlainPrinter(_ctx: Context) extends Printer {
393395
}
394396
}.close // todo: override in refined printer
395397

398+
private var maxSummarized = Int.MaxValue
399+
400+
def summarized[T](depth: Int)(op: => T): T = {
401+
val saved = maxSummarized
402+
maxSummarized = ctx.toTextRecursions + depth
403+
try op
404+
finally maxSummarized = depth
405+
}
406+
407+
def summarized[T](op: => T): T = summarized(summarizeDepth)(op)
408+
396409
def plain = this
397410
}
398411

src/dotty/tools/dotc/printing/Printer.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ abstract class Printer {
8888
/** Textual representation of tree */
8989
def toText[T >: Untyped](tree: Tree[T]): Text
9090

91+
/** Perform string or text-producing operation `op` so that only a
92+
* summarized text with given recursion depth is shown
93+
*/
94+
def summarized[T](depth: Int)(op: => T): T
95+
9196
/** A plain printer without any embellishments */
9297
def plain: Printer
9398
}

src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
127127
super.toText(tp)
128128
}
129129

130-
override def toText[T >: Untyped](tree: Tree[T]): Text = {
130+
override def toText[T >: Untyped](tree: Tree[T]): Text = controlled {
131131

132132
def optDotPrefix(name: Name) = optText(name)(_ ~ ".")
133133

@@ -242,7 +242,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
242242
case SeqLiteral(elems) =>
243243
"[" ~ toTextGlobal(elems, ",") ~ "]"
244244
case tpt: untpd.DerivedTypeTree =>
245-
"<derived typetree watching " ~ toText(tpt.watched) ~ ">"
245+
"<derived typetree watching " ~ summarized(toText(tpt.watched)) ~ ">"
246246
case TypeTree(orig) =>
247247
if (tree.hasType) toText(tree.typeOpt) else toText(orig)
248248
case SingletonTypeTree(ref) =>

src/dotty/tools/dotc/printing/Showable.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package printing
44
import core._
55

66
import Contexts._, Texts._, Decorators._
7+
import config.Config.summarizeDepth
78

89
trait Showable extends Any {
910

@@ -20,4 +21,13 @@ trait Showable extends Any {
2021

2122
/** The string representation of this showable element. */
2223
def show(implicit ctx: Context): String = toText(ctx.printer).show
23-
}
24+
25+
/** The summarized string representation of this showable element.
26+
* Recursion depth is limited to some smallish value. Default is
27+
* Config.summarizeDepth.
28+
*/
29+
def showSummary(depth: Int)(implicit ctx: Context): String =
30+
ctx.printer.summarized(depth)(show)
31+
32+
def showSummary(implicit ctx: Context): String = showSummary(summarizeDepth)
33+
}

0 commit comments

Comments
 (0)