aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/printing/Showable.scala
blob: 37de053cb5ad775a688600f1072de853a783e3ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package dotty.tools.dotc
package printing

import core._

import Contexts._, Texts._, Decorators._
import config.Config.summarizeDepth
import scala.util.control.NonFatal

trait Showable extends Any {

  /** The text representation of this showable element.
   *  This normally dispatches to a pattern matching
   *  method in Printers.
   */
  def toText(printer: Printer): Text

  /** A fallback text representation, if the pattern matching
   *  in Printers does not have a case for this showable element
   */
  def fallbackToText(printer: Printer): Text = toString

  /** The string representation of this showable element. */
  def show(implicit ctx: Context): String =
    try toText(ctx.printer).show
    catch {
      case NonFatal(ex) => s"[cannot display due to $ex, raw string = $toString]"
    }

  /** 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)
}