From 0a1e969cb09e953d6b3f3b64b63a050588aa3360 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 26 Mar 2014 12:28:12 +0100 Subject: Summary printing Added general way to produce summaries when shwowing trees or types. Summaries have limited, configrable recusion depth. fix showSummary --- src/dotty/tools/dotc/config/Config.scala | 3 +++ src/dotty/tools/dotc/printing/PlainPrinter.scala | 19 ++++++++++++++++--- src/dotty/tools/dotc/printing/Printer.scala | 5 +++++ src/dotty/tools/dotc/printing/RefinedPrinter.scala | 4 ++-- src/dotty/tools/dotc/printing/Showable.scala | 12 +++++++++++- 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/dotty/tools/dotc/config/Config.scala b/src/dotty/tools/dotc/config/Config.scala index 9ed40cd89..f69566733 100644 --- a/src/dotty/tools/dotc/config/Config.scala +++ b/src/dotty/tools/dotc/config/Config.scala @@ -27,4 +27,7 @@ object Config { * Disadvantage: It might hide inconsistencies, so while debugging it's better to turn it off */ final val newMatch = false + + /** The recursion depth for showing a summarized string */ + final val summarizeDepth = 2 } \ No newline at end of file diff --git a/src/dotty/tools/dotc/printing/PlainPrinter.scala b/src/dotty/tools/dotc/printing/PlainPrinter.scala index 332b7bd21..f10aa7225 100644 --- a/src/dotty/tools/dotc/printing/PlainPrinter.scala +++ b/src/dotty/tools/dotc/printing/PlainPrinter.scala @@ -7,6 +7,7 @@ import Contexts.Context, Scopes.Scope, Denotations.Denotation, Annotations.Annot import StdNames.nme import ast.Trees._, ast.untpd import java.lang.Integer.toOctalString +import config.Config.summarizeDepth import scala.annotation.switch class PlainPrinter(_ctx: Context) extends Printer { @@ -15,7 +16,7 @@ class PlainPrinter(_ctx: Context) extends Printer { protected def maxToTextRecursions = 100 protected final def controlled(op: => Text): Text = - if (ctx.toTextRecursions < maxToTextRecursions) + if (ctx.toTextRecursions < maxToTextRecursions && ctx.toTextRecursions < maxSummarized) try { ctx.toTextRecursions += 1 op @@ -23,12 +24,13 @@ class PlainPrinter(_ctx: Context) extends Printer { ctx.toTextRecursions -= 1 } else { - recursionLimitExceeded() + if (ctx.toTextRecursions >= maxToTextRecursions) + recursionLimitExceeded() "..." } protected def recursionLimitExceeded() = { - ctx.warning("Exceeded recursion depth attempting to print type.") + ctx.warning("Exceeded recursion depth attempting to print.") (new Throwable).printStackTrace } @@ -393,6 +395,17 @@ class PlainPrinter(_ctx: Context) extends Printer { } }.close // todo: override in refined printer + private var maxSummarized = Int.MaxValue + + def summarized[T](depth: Int)(op: => T): T = { + val saved = maxSummarized + maxSummarized = ctx.toTextRecursions + depth + try op + finally maxSummarized = depth + } + + def summarized[T](op: => T): T = summarized(summarizeDepth)(op) + def plain = this } diff --git a/src/dotty/tools/dotc/printing/Printer.scala b/src/dotty/tools/dotc/printing/Printer.scala index af87302e4..65162a10f 100644 --- a/src/dotty/tools/dotc/printing/Printer.scala +++ b/src/dotty/tools/dotc/printing/Printer.scala @@ -88,6 +88,11 @@ abstract class Printer { /** Textual representation of tree */ def toText[T >: Untyped](tree: Tree[T]): Text + /** Perform string or text-producing operation `op` so that only a + * summarized text with given recursion depth is shown + */ + def summarized[T](depth: Int)(op: => T): T + /** A plain printer without any embellishments */ def plain: Printer } diff --git a/src/dotty/tools/dotc/printing/RefinedPrinter.scala b/src/dotty/tools/dotc/printing/RefinedPrinter.scala index 1a65b2978..e0c660e7f 100644 --- a/src/dotty/tools/dotc/printing/RefinedPrinter.scala +++ b/src/dotty/tools/dotc/printing/RefinedPrinter.scala @@ -127,7 +127,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { super.toText(tp) } - override def toText[T >: Untyped](tree: Tree[T]): Text = { + override def toText[T >: Untyped](tree: Tree[T]): Text = controlled { def optDotPrefix(name: Name) = optText(name)(_ ~ ".") @@ -242,7 +242,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) { case SeqLiteral(elems) => "[" ~ toTextGlobal(elems, ",") ~ "]" case tpt: untpd.DerivedTypeTree => - "" + "" case TypeTree(orig) => if (tree.hasType) toText(tree.typeOpt) else toText(orig) case SingletonTypeTree(ref) => diff --git a/src/dotty/tools/dotc/printing/Showable.scala b/src/dotty/tools/dotc/printing/Showable.scala index cb65193e6..550d80060 100644 --- a/src/dotty/tools/dotc/printing/Showable.scala +++ b/src/dotty/tools/dotc/printing/Showable.scala @@ -4,6 +4,7 @@ package printing import core._ import Contexts._, Texts._, Decorators._ +import config.Config.summarizeDepth trait Showable extends Any { @@ -20,4 +21,13 @@ trait Showable extends Any { /** The string representation of this showable element. */ def show(implicit ctx: Context): String = toText(ctx.printer).show -} \ No newline at end of file + + /** The summarized string representation of this showable element. + * Recursion depth is limited to some smallish value. Default is + * Config.summarizeDepth. + */ + def showSummary(depth: Int)(implicit ctx: Context): String = + ctx.printer.summarized(depth)(show) + + def showSummary(implicit ctx: Context): String = showSummary(summarizeDepth) +} -- cgit v1.2.3