src/Pure/General/pretty.scala
author wenzelm
Tue, 31 Dec 2024 15:09:36 +0100
changeset 81697 1629c2ff4880
parent 81695 66d686f149e7
child 81698 17f1a78af3f5
permissions -rw-r--r--
misc tuning: more uniform;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/pretty.scala
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     3
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
     4
Generic pretty printing module.
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     5
*/
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     6
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     7
package isabelle
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
     8
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 69867
diff changeset
     9
import scala.annotation.tailrec
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 69867
diff changeset
    10
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    11
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    12
object Pretty {
61871
2cb4a2970941 more explicit Pretty.Tree, like in ML;
wenzelm
parents: 61868
diff changeset
    13
  /* XML constructors */
2cb4a2970941 more explicit Pretty.Tree, like in ML;
wenzelm
parents: 61868
diff changeset
    14
61874
wenzelm
parents: 61871
diff changeset
    15
  val space: XML.Body = List(XML.Text(Symbol.space))
wenzelm
parents: 61871
diff changeset
    16
  def spaces(n: Int): XML.Body =
wenzelm
parents: 61871
diff changeset
    17
    if (n == 0) Nil
wenzelm
parents: 61871
diff changeset
    18
    else if (n == 1) space
wenzelm
parents: 61871
diff changeset
    19
    else List(XML.Text(Symbol.spaces(n)))
48704
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    20
80940
334625aec7a4 clarified signature;
wenzelm
parents: 80871
diff changeset
    21
  val bullet: XML.Body = XML.elem(Markup.BULLET, space) :: space
334625aec7a4 clarified signature;
wenzelm
parents: 80871
diff changeset
    22
81121
7cacedbddba7 support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents: 81120
diff changeset
    23
  def block(body: XML.Body,
7cacedbddba7 support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents: 81120
diff changeset
    24
    consistent: Boolean = false,
7cacedbddba7 support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents: 81120
diff changeset
    25
    indent: Int = 2
81395
d9f791f75b8b clarified signature: more robust type XML.Elem;
wenzelm
parents: 81346
diff changeset
    26
  ): XML.Elem = XML.Elem(Markup.Block(consistent = consistent, indent = indent), body)
48704
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    27
81395
d9f791f75b8b clarified signature: more robust type XML.Elem;
wenzelm
parents: 81346
diff changeset
    28
  def string(s: String): XML.Elem = block(XML.string(s), indent = 0)
d9f791f75b8b clarified signature: more robust type XML.Elem;
wenzelm
parents: 81346
diff changeset
    29
d9f791f75b8b clarified signature: more robust type XML.Elem;
wenzelm
parents: 81346
diff changeset
    30
  def brk(width: Int, indent: Int = 0): XML.Elem =
81120
080beab27264 clarified signature;
wenzelm
parents: 80941
diff changeset
    31
    XML.Elem(Markup.Break(width = width, indent = indent), spaces(width))
61871
2cb4a2970941 more explicit Pretty.Tree, like in ML;
wenzelm
parents: 61868
diff changeset
    32
69867
wenzelm
parents: 67896
diff changeset
    33
  val fbrk: XML.Tree = XML.newline
65130
wenzelm
parents: 62820
diff changeset
    34
  def fbreaks(ts: List[XML.Tree]): XML.Body = Library.separate(fbrk, ts)
61871
2cb4a2970941 more explicit Pretty.Tree, like in ML;
wenzelm
parents: 61868
diff changeset
    35
2cb4a2970941 more explicit Pretty.Tree, like in ML;
wenzelm
parents: 61868
diff changeset
    36
  val Separator: XML.Body = List(XML.elem(Markup.SEPARATOR, space), fbrk)
75958
97445e208419 tuned signature;
wenzelm
parents: 75393
diff changeset
    37
  def separate(ts: List[XML.Tree], sep: XML.Body = Separator): XML.Body =
97445e208419 tuned signature;
wenzelm
parents: 75393
diff changeset
    38
    Library.separate(sep, ts.map(List(_))).flatten
97445e208419 tuned signature;
wenzelm
parents: 75393
diff changeset
    39
97445e208419 tuned signature;
wenzelm
parents: 75393
diff changeset
    40
  val comma: XML.Body = List(XML.Text(","), brk(1))
97445e208419 tuned signature;
wenzelm
parents: 75393
diff changeset
    41
  def commas(ts: List[XML.Tree]): XML.Body = separate(ts, sep = comma)
97445e208419 tuned signature;
wenzelm
parents: 75393
diff changeset
    42
97445e208419 tuned signature;
wenzelm
parents: 75393
diff changeset
    43
  def `enum`(ts: List[XML.Tree],
97445e208419 tuned signature;
wenzelm
parents: 75393
diff changeset
    44
    bg: String = "(",
97445e208419 tuned signature;
wenzelm
parents: 75393
diff changeset
    45
    en: String = ")",
97445e208419 tuned signature;
wenzelm
parents: 75393
diff changeset
    46
    sep: XML.Body = comma,
97445e208419 tuned signature;
wenzelm
parents: 75393
diff changeset
    47
    indent: Int = 2
81395
d9f791f75b8b clarified signature: more robust type XML.Elem;
wenzelm
parents: 81346
diff changeset
    48
  ): XML.Elem = Pretty.block(XML.enclose(bg, en, separate(ts, sep = sep)), indent = indent)
61864
3a5992c3410c support for blocks with consistent breaks;
wenzelm
parents: 61862
diff changeset
    49
48704
85a3de10567d tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents: 45666
diff changeset
    50
51492
eaa1c4cc1106 more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
wenzelm
parents: 51470
diff changeset
    51
  /* text metric -- standardized to width of space */
eaa1c4cc1106 more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
wenzelm
parents: 51470
diff changeset
    52
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    53
  abstract class Metric {
81340
30f7eb65d679 clarified signature;
wenzelm
parents: 81294
diff changeset
    54
    def unit: Double
51492
eaa1c4cc1106 more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
wenzelm
parents: 51470
diff changeset
    55
    def apply(s: String): Double
eaa1c4cc1106 more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
wenzelm
parents: 51470
diff changeset
    56
  }
eaa1c4cc1106 more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
wenzelm
parents: 51470
diff changeset
    57
eaa1c4cc1106 more explicit Pretty.Metric, with clear distinction of unit (space width) vs. average char width (for visual adjustments) -- NB: Pretty formatting works via full space characters (despite a981a5c8a505 and 70f7483df9cb);
wenzelm
parents: 51470
diff changeset
    58
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    59
  /* markup trees with physical blocks and breaks */
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
    60
62820
5c678ee5d34a proper type;
wenzelm
parents: 62785
diff changeset
    61
  private def force_nat(i: Int): Int = i max 0
5c678ee5d34a proper type;
wenzelm
parents: 62785
diff changeset
    62
81694
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
    63
  type Markup_Body = (Markup, Option[XML.Body])
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
    64
  val no_markup: Markup_Body = (Markup.Empty, None)
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
    65
  val item_markup: Markup_Body = (Markup.Expression.item, None)
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
    66
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
    67
  def markup_elem(markup: Markup_Body, body: XML.Body): XML.Elem =
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
    68
    markup match {
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
    69
      case (m, None) => XML.Elem(m, body)
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
    70
      case (m1, Some(m2)) => XML.Wrapped_Elem(m1, m2, body)
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
    71
    }
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
    72
61874
wenzelm
parents: 61871
diff changeset
    73
  private sealed abstract class Tree { def length: Double }
81694
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
    74
  private case object End extends Tree {
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
    75
    override def length: Double = 0.0
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
    76
  }
61874
wenzelm
parents: 61871
diff changeset
    77
  private case class Block(
81694
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
    78
    markup: Markup_Body,
81121
7cacedbddba7 support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents: 81120
diff changeset
    79
    open_block: Boolean,
80798
f0c754a98e52 tuned whitespace;
wenzelm
parents: 76086
diff changeset
    80
    consistent: Boolean,
f0c754a98e52 tuned whitespace;
wenzelm
parents: 76086
diff changeset
    81
    indent: Int,
f0c754a98e52 tuned whitespace;
wenzelm
parents: 76086
diff changeset
    82
    body: List[Tree],
f0c754a98e52 tuned whitespace;
wenzelm
parents: 76086
diff changeset
    83
    length: Double
81694
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
    84
  ) extends Tree
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    85
  private case class Break(force: Boolean, width: Int, indent: Int) extends Tree {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    86
    def length: Double = width.toDouble
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    87
  }
61874
wenzelm
parents: 61871
diff changeset
    88
  private case class Str(string: String, length: Double) extends Tree
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    89
61874
wenzelm
parents: 61871
diff changeset
    90
  private val FBreak = Break(true, 1, 0)
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
    91
80940
334625aec7a4 clarified signature;
wenzelm
parents: 80871
diff changeset
    92
  private def make_block(body: List[Tree],
81694
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
    93
    markup: Markup_Body = no_markup,
81121
7cacedbddba7 support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents: 81120
diff changeset
    94
    open_block: Boolean = false,
80940
334625aec7a4 clarified signature;
wenzelm
parents: 80871
diff changeset
    95
    consistent: Boolean = false,
334625aec7a4 clarified signature;
wenzelm
parents: 80871
diff changeset
    96
    indent: Int = 0
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
    97
  ): Tree = {
62785
70b9c7d4ed7f more robust pretty printing: permissive treatment of bad values;
wenzelm
parents: 61883
diff changeset
    98
    val indent1 = force_nat(indent)
70b9c7d4ed7f more robust pretty printing: permissive treatment of bad values;
wenzelm
parents: 61883
diff changeset
    99
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
   100
    @tailrec def body_length(prts: List[Tree], len: Double): Double = {
61883
c0f34fe6aa61 clarified length of block with pre-existant forced breaks;
wenzelm
parents: 61875
diff changeset
   101
      val (line, rest) =
c0f34fe6aa61 clarified length of block with pre-existant forced breaks;
wenzelm
parents: 61875
diff changeset
   102
        Library.take_prefix[Tree]({ case Break(true, _, _) => false case _ => true }, prts)
73359
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 71781
diff changeset
   103
      val len1 = (line.foldLeft(0.0) { case (l, t) => l + t.length }) max len
71781
3fd54f7f52b0 tuned signature -- avoid warnings;
wenzelm
parents: 71601
diff changeset
   104
      (rest: @unchecked) match {
61883
c0f34fe6aa61 clarified length of block with pre-existant forced breaks;
wenzelm
parents: 61875
diff changeset
   105
        case Break(true, _, ind) :: rest1 =>
62785
70b9c7d4ed7f more robust pretty printing: permissive treatment of bad values;
wenzelm
parents: 61883
diff changeset
   106
          body_length(Break(false, indent1 + ind, 0) :: rest1, len1)
61883
c0f34fe6aa61 clarified length of block with pre-existant forced breaks;
wenzelm
parents: 61875
diff changeset
   107
        case Nil => len1
c0f34fe6aa61 clarified length of block with pre-existant forced breaks;
wenzelm
parents: 61875
diff changeset
   108
      }
c0f34fe6aa61 clarified length of block with pre-existant forced breaks;
wenzelm
parents: 61875
diff changeset
   109
    }
81694
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   110
    Block(markup, open_block, consistent, indent1, body, body_length(body, 0.0))
61883
c0f34fe6aa61 clarified length of block with pre-existant forced breaks;
wenzelm
parents: 61875
diff changeset
   111
  }
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
   112
61874
wenzelm
parents: 61871
diff changeset
   113
80845
da20e00050ab tuned comments, following Isabelle/ML;
wenzelm
parents: 80807
diff changeset
   114
  /* no formatting */
76086
338adf8d423c support Pretty.unformatted, similar to ML version;
wenzelm
parents: 75958
diff changeset
   115
80871
b71a040ab128 tuned signature: more operations;
wenzelm
parents: 80845
diff changeset
   116
  def output_content(pure: Boolean, output: XML.Body): String =
b71a040ab128 tuned signature: more operations;
wenzelm
parents: 80845
diff changeset
   117
    XML.content(if (pure) Protocol_Message.clean_output(output) else output)
b71a040ab128 tuned signature: more operations;
wenzelm
parents: 80845
diff changeset
   118
80807
b41c19523a2e tuned signature;
wenzelm
parents: 80806
diff changeset
   119
  def unbreakable(input: XML.Body): XML.Body =
76086
338adf8d423c support Pretty.unformatted, similar to ML version;
wenzelm
parents: 75958
diff changeset
   120
    input flatMap {
81695
66d686f149e7 tuned names;
wenzelm
parents: 81694
diff changeset
   121
      case XML.Wrapped_Elem(markup1, markup2, body) =>
66d686f149e7 tuned names;
wenzelm
parents: 81694
diff changeset
   122
        List(XML.Wrapped_Elem(markup1, markup2, unbreakable(body)))
80807
b41c19523a2e tuned signature;
wenzelm
parents: 80806
diff changeset
   123
      case XML.Elem(Markup.Break(width, _), _) => spaces(width)
b41c19523a2e tuned signature;
wenzelm
parents: 80806
diff changeset
   124
      case XML.Elem(markup, body) => List(XML.Elem(markup, unbreakable(body)))
76086
338adf8d423c support Pretty.unformatted, similar to ML version;
wenzelm
parents: 75958
diff changeset
   125
      case XML.Text(text) => XML.string(split_lines(text).mkString(Symbol.space))
338adf8d423c support Pretty.unformatted, similar to ML version;
wenzelm
parents: 75958
diff changeset
   126
    }
80807
b41c19523a2e tuned signature;
wenzelm
parents: 80806
diff changeset
   127
80871
b71a040ab128 tuned signature: more operations;
wenzelm
parents: 80845
diff changeset
   128
  def unformatted_string_of(input: XML.Body, pure: Boolean = false): String =
b71a040ab128 tuned signature: more operations;
wenzelm
parents: 80845
diff changeset
   129
    output_content(pure, unbreakable(input))
76086
338adf8d423c support Pretty.unformatted, similar to ML version;
wenzelm
parents: 75958
diff changeset
   130
338adf8d423c support Pretty.unformatted, similar to ML version;
wenzelm
parents: 75958
diff changeset
   131
80845
da20e00050ab tuned comments, following Isabelle/ML;
wenzelm
parents: 80807
diff changeset
   132
  /* formatting */
61874
wenzelm
parents: 61871
diff changeset
   133
81694
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   134
  private type State = List[(Markup_Body, List[XML.Tree])]
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   135
  private val init_state: State = List((no_markup, Nil))
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   136
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   137
  private sealed case class Text(
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   138
    state: State = init_state,
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   139
    pos: Double = 0.0,
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   140
    nl: Int = 0
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   141
  ) {
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   142
    def add(t: XML.Tree, len: Double = 0.0): Text =
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   143
      (state: @unchecked) match {
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   144
        case (m, ts) :: rest => copy(state = (m, t :: ts) :: rest, pos = pos + len)
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   145
      }
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   146
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   147
    def push(m: Markup_Body): Text =
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   148
      copy(state = (m, Nil) :: state)
81685
wenzelm
parents: 81395
diff changeset
   149
81694
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   150
    def pop: Text =
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   151
      (state: @unchecked) match {
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   152
        case (m1, ts1) :: (m2, ts2) :: rest =>
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   153
          copy(state = (m2, markup_elem(m1, ts1.reverse) :: ts2) :: rest)
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   154
      }
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   155
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   156
    def result: XML.Body =
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   157
      (state: @unchecked) match {
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   158
        case List((m, ts)) if m == no_markup => ts.reverse
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   159
      }
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   160
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   161
    def reset: Text = copy(state = init_state)
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   162
    def restore(other: Text): Text = copy(state = other.state)
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   163
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   164
    def newline: Text = add(fbrk).copy(pos = 0.0, nl = nl + 1)
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   165
61874
wenzelm
parents: 61871
diff changeset
   166
    def string(s: String, len: Double): Text =
81694
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   167
      if (s.isEmpty) copy(pos = pos + len)
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   168
      else add(XML.Text(s), len = len)
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   169
61874
wenzelm
parents: 61871
diff changeset
   170
    def blanks(wd: Int): Text = string(Symbol.spaces(wd), wd.toDouble)
wenzelm
parents: 61871
diff changeset
   171
  }
wenzelm
parents: 61871
diff changeset
   172
wenzelm
parents: 61871
diff changeset
   173
  private def break_dist(trees: List[Tree], after: Double): Double =
wenzelm
parents: 61871
diff changeset
   174
    trees match {
wenzelm
parents: 61871
diff changeset
   175
      case (_: Break) :: _ => 0.0
wenzelm
parents: 61871
diff changeset
   176
      case t :: ts => t.length + break_dist(ts, after)
wenzelm
parents: 61871
diff changeset
   177
      case Nil => after
36689
379f5b1e7f91 replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm
parents: 36687
diff changeset
   178
    }
379f5b1e7f91 replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm
parents: 36687
diff changeset
   179
61874
wenzelm
parents: 61871
diff changeset
   180
  private def force_break(tree: Tree): Tree =
wenzelm
parents: 61871
diff changeset
   181
    tree match { case Break(false, wd, ind) => Break(true, wd, ind) case _ => tree }
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 69867
diff changeset
   182
  private def force_all(trees: List[Tree]): List[Tree] = trees.map(force_break)
51570
3633828d80fc basic support for Pretty.item, which is considered as logical markup and interpreted in Isabelle/Scala, but ignored elsewhere (TTY, latex etc.);
wenzelm
parents: 51569
diff changeset
   183
61874
wenzelm
parents: 61871
diff changeset
   184
  private def force_next(trees: List[Tree]): List[Tree] =
wenzelm
parents: 61871
diff changeset
   185
    trees match {
wenzelm
parents: 61871
diff changeset
   186
      case Nil => Nil
wenzelm
parents: 61871
diff changeset
   187
      case (t: Break) :: ts => force_break(t) :: ts
wenzelm
parents: 61871
diff changeset
   188
      case t :: ts => t :: force_next(ts)
wenzelm
parents: 61871
diff changeset
   189
    }
51570
3633828d80fc basic support for Pretty.item, which is considered as logical markup and interpreted in Isabelle/Scala, but ignored elsewhere (TTY, latex etc.);
wenzelm
parents: 51569
diff changeset
   190
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 69867
diff changeset
   191
  val default_margin: Double = 76.0
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 69867
diff changeset
   192
  val default_breakgain: Double = default_margin / 20
36820
0cdfce0bf956 clarified Pretty.font_metrics;
wenzelm
parents: 36818
diff changeset
   193
67547
aefe7a7b330a clarified breakgain: keeping it constant avoids margin fluctuation in Pretty_Tooltip vs. Pretty_Text_Area;
wenzelm
parents: 65130
diff changeset
   194
  def formatted(input: XML.Body,
67896
00797fb82869 clarified signature;
wenzelm
parents: 67547
diff changeset
   195
    margin: Double = default_margin,
00797fb82869 clarified signature;
wenzelm
parents: 67547
diff changeset
   196
    breakgain: Double = default_breakgain,
81346
0cdd6729a962 clarified modules;
wenzelm
parents: 81340
diff changeset
   197
    metric: Metric = Codepoint.Metric
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73359
diff changeset
   198
  ): XML.Body = {
51569
4e084727faae maintain integer indentation during formatting -- it needs to be implemented by repeated spaces eventually;
wenzelm
parents: 51568
diff changeset
   199
    val emergencypos = (margin / 2).round.toInt
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   200
61874
wenzelm
parents: 61871
diff changeset
   201
    def make_tree(inp: XML.Body): List[Tree] =
wenzelm
parents: 61871
diff changeset
   202
      inp flatMap {
81694
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   203
        case XML.Wrapped_Elem(markup1, markup2, body) =>
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   204
          List(make_block(make_tree(body), markup = (markup1, Some(markup2)), open_block = true))
61874
wenzelm
parents: 61871
diff changeset
   205
        case XML.Elem(markup, body) =>
wenzelm
parents: 61871
diff changeset
   206
          markup match {
81294
108284c8cbfd removed obsolete markup for "open_block" (see also d5ad89fda714): Isabelle/Scala directly supports XML.Elem pretty-printing;
wenzelm
parents: 81121
diff changeset
   207
            case Markup.Block(consistent, indent) =>
81685
wenzelm
parents: 81395
diff changeset
   208
              List(make_block(make_tree(body), consistent = consistent, indent = indent))
61874
wenzelm
parents: 61871
diff changeset
   209
            case Markup.Break(width, indent) =>
62785
70b9c7d4ed7f more robust pretty printing: permissive treatment of bad values;
wenzelm
parents: 61883
diff changeset
   210
              List(Break(false, force_nat(width), force_nat(indent)))
61874
wenzelm
parents: 61871
diff changeset
   211
            case Markup(Markup.ITEM, _) =>
81694
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   212
              List(make_block(make_tree(bullet ::: body), markup = item_markup, indent = 2))
61874
wenzelm
parents: 61871
diff changeset
   213
            case _ =>
81694
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   214
              List(make_block(make_tree(body), markup = (markup, None), open_block = true))
61874
wenzelm
parents: 61871
diff changeset
   215
          }
wenzelm
parents: 61871
diff changeset
   216
        case XML.Text(text) =>
wenzelm
parents: 61871
diff changeset
   217
          Library.separate(FBreak, split_lines(text).map(s => Str(s, metric(s))))
36817
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   218
      }
ed97e877ff2d more precise pretty printing based on actual font metrics;
wenzelm
parents: 36763
diff changeset
   219
81697
1629c2ff4880 misc tuning: more uniform;
wenzelm
parents: 81695
diff changeset
   220
    def format(trees: List[Tree], before: Int, after: Double, text: Text): Text =
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   221
      trees match {
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   222
        case Nil => text
81697
1629c2ff4880 misc tuning: more uniform;
wenzelm
parents: 81695
diff changeset
   223
        case End :: ts => format(ts, before, after, text.pop)
81121
7cacedbddba7 support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents: 81120
diff changeset
   224
        case (block: Block) :: ts if block.open_block =>
81697
1629c2ff4880 misc tuning: more uniform;
wenzelm
parents: 81695
diff changeset
   225
          format(block.body ::: End :: ts, before, after, text.push(block.markup))
81689
6c3de898b055 tuned signature;
wenzelm
parents: 81688
diff changeset
   226
        case (block: Block) :: ts =>
6c3de898b055 tuned signature;
wenzelm
parents: 81688
diff changeset
   227
          val pos1 = (text.pos + block.indent).ceil.toInt
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   228
          val pos2 = pos1 % emergencypos
81697
1629c2ff4880 misc tuning: more uniform;
wenzelm
parents: 81695
diff changeset
   229
          val before1 = if (pos1 < emergencypos) pos1 else pos2
81687
d92a3649bfd1 tuned: more uniform;
wenzelm
parents: 81685
diff changeset
   230
          val after1 = break_dist(ts, after)
81689
6c3de898b055 tuned signature;
wenzelm
parents: 81688
diff changeset
   231
          val body1 =
6c3de898b055 tuned signature;
wenzelm
parents: 81688
diff changeset
   232
            if (block.consistent && text.pos + block.length > margin - after1) force_all(block.body)
6c3de898b055 tuned signature;
wenzelm
parents: 81688
diff changeset
   233
            else block.body
81687
d92a3649bfd1 tuned: more uniform;
wenzelm
parents: 81685
diff changeset
   234
          val btext1 =
81697
1629c2ff4880 misc tuning: more uniform;
wenzelm
parents: 81695
diff changeset
   235
            if (block.markup == no_markup) format(body1, before1, after1, text)
80800
f52eb69564a4 clarified signature;
wenzelm
parents: 80798
diff changeset
   236
            else {
81697
1629c2ff4880 misc tuning: more uniform;
wenzelm
parents: 81695
diff changeset
   237
              val btext = format(body1, before1, after1, text.reset)
81694
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   238
              val elem = markup_elem(block.markup, btext.result)
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   239
              btext.restore(text.add(elem))
61871
2cb4a2970941 more explicit Pretty.Tree, like in ML;
wenzelm
parents: 61868
diff changeset
   240
            }
81687
d92a3649bfd1 tuned: more uniform;
wenzelm
parents: 81685
diff changeset
   241
          val ts1 = if (text.nl < btext1.nl) force_next(ts) else ts
81697
1629c2ff4880 misc tuning: more uniform;
wenzelm
parents: 81695
diff changeset
   242
          format(ts1, before, after, btext1)
61871
2cb4a2970941 more explicit Pretty.Tree, like in ML;
wenzelm
parents: 61868
diff changeset
   243
        case Break(force, wd, ind) :: ts =>
2cb4a2970941 more explicit Pretty.Tree, like in ML;
wenzelm
parents: 61868
diff changeset
   244
          if (!force &&
81697
1629c2ff4880 misc tuning: more uniform;
wenzelm
parents: 81695
diff changeset
   245
              text.pos + wd <= ((margin - break_dist(ts, after)) max (before + breakgain)))
1629c2ff4880 misc tuning: more uniform;
wenzelm
parents: 81695
diff changeset
   246
            format(ts, before, after, text.blanks(wd))
1629c2ff4880 misc tuning: more uniform;
wenzelm
parents: 81695
diff changeset
   247
          else format(ts, before, after, text.newline.blanks(before + ind))
1629c2ff4880 misc tuning: more uniform;
wenzelm
parents: 81695
diff changeset
   248
        case Str(s, len) :: ts => format(ts, before, after, text.string(s, len))
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   249
      }
81694
75886eea238a more accurate formatting of open_block: markup only, without affecting layout (e.g. via force_next);
wenzelm
parents: 81692
diff changeset
   250
    format(make_tree(input), 0, 0.0, Text()).result
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 36683
diff changeset
   251
  }
36734
d9b10c173330 Pretty.formatted operates directly on XML trees, treating XML.Elem like a pro-forma block of indentation 0, like the ML version;
wenzelm
parents: 36689
diff changeset
   252
67547
aefe7a7b330a clarified breakgain: keeping it constant avoids margin fluctuation in Pretty_Tooltip vs. Pretty_Text_Area;
wenzelm
parents: 65130
diff changeset
   253
  def string_of(input: XML.Body,
80871
b71a040ab128 tuned signature: more operations;
wenzelm
parents: 80845
diff changeset
   254
    margin: Double = default_margin,
b71a040ab128 tuned signature: more operations;
wenzelm
parents: 80845
diff changeset
   255
    breakgain: Double = default_breakgain,
81346
0cdd6729a962 clarified modules;
wenzelm
parents: 81340
diff changeset
   256
    metric: Metric = Codepoint.Metric,
80871
b71a040ab128 tuned signature: more operations;
wenzelm
parents: 80845
diff changeset
   257
    pure: Boolean = false
b71a040ab128 tuned signature: more operations;
wenzelm
parents: 80845
diff changeset
   258
  ): String = {
b71a040ab128 tuned signature: more operations;
wenzelm
parents: 80845
diff changeset
   259
    output_content(pure, formatted(input, margin = margin, breakgain = breakgain, metric = metric))
b71a040ab128 tuned signature: more operations;
wenzelm
parents: 80845
diff changeset
   260
  }
36683
41a1210519fd basic support for symbolic pretty printing;
wenzelm
parents:
diff changeset
   261
}