| author | wenzelm | 
| Fri, 28 Jun 2024 13:20:18 +0200 | |
| changeset 80443 | ab0dd21dd0ca | 
| parent 76086 | 338adf8d423c | 
| child 80798 | f0c754a98e52 | 
| permissions | -rw-r--r-- | 
| 36683 | 1 | /* Title: Pure/General/pretty.scala | 
| 2 | Author: Makarius | |
| 3 | ||
| 36687 | 4 | Generic pretty printing module. | 
| 36683 | 5 | */ | 
| 6 | ||
| 7 | package isabelle | |
| 8 | ||
| 71601 | 9 | import scala.annotation.tailrec | 
| 10 | ||
| 36683 | 11 | |
| 75393 | 12 | object Pretty {
 | 
| 61871 | 13 | /* XML constructors */ | 
| 14 | ||
| 61874 | 15 | val space: XML.Body = List(XML.Text(Symbol.space)) | 
| 16 | def spaces(n: Int): XML.Body = | |
| 17 | if (n == 0) Nil | |
| 18 | else if (n == 1) space | |
| 19 | else List(XML.Text(Symbol.spaces(n))) | |
| 48704 
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
 wenzelm parents: 
45666diff
changeset | 20 | |
| 75958 | 21 | def block(body: XML.Body, consistent: Boolean = false, indent: Int = 2): XML.Tree = | 
| 61871 | 22 | XML.Elem(Markup.Block(consistent, indent), body) | 
| 48704 
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
 wenzelm parents: 
45666diff
changeset | 23 | |
| 61871 | 24 | def brk(width: Int, indent: Int = 0): XML.Tree = | 
| 25 | XML.Elem(Markup.Break(width, indent), spaces(width)) | |
| 26 | ||
| 69867 | 27 | val fbrk: XML.Tree = XML.newline | 
| 65130 | 28 | def fbreaks(ts: List[XML.Tree]): XML.Body = Library.separate(fbrk, ts) | 
| 61871 | 29 | |
| 30 | val Separator: XML.Body = List(XML.elem(Markup.SEPARATOR, space), fbrk) | |
| 75958 | 31 | def separate(ts: List[XML.Tree], sep: XML.Body = Separator): XML.Body = | 
| 32 | Library.separate(sep, ts.map(List(_))).flatten | |
| 33 | ||
| 34 |   val comma: XML.Body = List(XML.Text(","), brk(1))
 | |
| 35 | def commas(ts: List[XML.Tree]): XML.Body = separate(ts, sep = comma) | |
| 36 | ||
| 37 | def `enum`(ts: List[XML.Tree], | |
| 38 |     bg: String = "(",
 | |
| 39 | en: String = ")", | |
| 40 | sep: XML.Body = comma, | |
| 41 | indent: Int = 2 | |
| 42 | ): XML.Tree = Pretty.block(XML.enclose(bg, en, separate(ts, sep = sep)), indent = indent) | |
| 61864 | 43 | |
| 48704 
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
 wenzelm parents: 
45666diff
changeset | 44 | |
| 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: 
51470diff
changeset | 45 | /* 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: 
51470diff
changeset | 46 | |
| 75393 | 47 |   abstract class Metric {
 | 
| 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: 
51470diff
changeset | 48 | val unit: 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: 
51470diff
changeset | 49 | 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: 
51470diff
changeset | 50 | } | 
| 
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: 
51470diff
changeset | 51 | |
| 75393 | 52 |   object Default_Metric extends Metric {
 | 
| 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: 
51470diff
changeset | 53 | val unit = 1.0 | 
| 
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: 
51470diff
changeset | 54 | def apply(s: String): Double = s.length.toDouble | 
| 
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: 
51470diff
changeset | 55 | } | 
| 
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: 
51470diff
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: 
51470diff
changeset | 57 | |
| 36687 | 58 | /* markup trees with physical blocks and breaks */ | 
| 59 | ||
| 62820 | 60 | private def force_nat(i: Int): Int = i max 0 | 
| 61 | ||
| 61874 | 62 |   private sealed abstract class Tree { def length: Double }
 | 
| 63 | private case class Block( | |
| 61871 | 64 | markup: Option[(Markup, Option[XML.Body])], | 
| 65 | consistent: Boolean, indent: Int, body: List[Tree], length: Double) extends Tree | |
| 75393 | 66 |   private case class Break(force: Boolean, width: Int, indent: Int) extends Tree {
 | 
| 67 | def length: Double = width.toDouble | |
| 68 | } | |
| 61874 | 69 | private case class Str(string: String, length: Double) extends Tree | 
| 36683 | 70 | |
| 61874 | 71 | private val FBreak = Break(true, 1, 0) | 
| 36683 | 72 | |
| 61874 | 73 | private def make_block( | 
| 75393 | 74 | markup: Option[(Markup, Option[XML.Body])], | 
| 75 | consistent: Boolean, | |
| 76 | indent: Int, | |
| 77 | body: List[Tree] | |
| 78 |   ): Tree = {
 | |
| 62785 
70b9c7d4ed7f
more robust pretty printing: permissive treatment of bad values;
 wenzelm parents: 
61883diff
changeset | 79 | val indent1 = force_nat(indent) | 
| 
70b9c7d4ed7f
more robust pretty printing: permissive treatment of bad values;
 wenzelm parents: 
61883diff
changeset | 80 | |
| 75393 | 81 |     @tailrec def body_length(prts: List[Tree], len: Double): Double = {
 | 
| 61883 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 82 | val (line, rest) = | 
| 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 83 |         Library.take_prefix[Tree]({ case Break(true, _, _) => false case _ => true }, prts)
 | 
| 73359 | 84 |       val len1 = (line.foldLeft(0.0) { case (l, t) => l + t.length }) max len
 | 
| 71781 | 85 |       (rest: @unchecked) match {
 | 
| 61883 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 86 | case Break(true, _, ind) :: rest1 => | 
| 62785 
70b9c7d4ed7f
more robust pretty printing: permissive treatment of bad values;
 wenzelm parents: 
61883diff
changeset | 87 | body_length(Break(false, indent1 + ind, 0) :: rest1, len1) | 
| 61883 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 88 | case Nil => len1 | 
| 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 89 | } | 
| 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 90 | } | 
| 62785 
70b9c7d4ed7f
more robust pretty printing: permissive treatment of bad values;
 wenzelm parents: 
61883diff
changeset | 91 | Block(markup, consistent, indent1, body, body_length(body, 0.0)) | 
| 61883 
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
 wenzelm parents: 
61875diff
changeset | 92 | } | 
| 36683 | 93 | |
| 61874 | 94 | |
| 76086 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 95 | /* unformatted output */ | 
| 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 96 | |
| 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 97 |   def unformatted(input: XML.Body): XML.Body = {
 | 
| 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 98 |     input flatMap {
 | 
| 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 99 | case XML.Wrapped_Elem(markup, body1, body2) => | 
| 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 100 | List(XML.Wrapped_Elem(markup, body1, unformatted(body2))) | 
| 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 101 | case XML.Elem(markup, body) => | 
| 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 102 |         markup match {
 | 
| 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 103 | case Markup.Block(_, _) => unformatted(body) | 
| 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 104 | case Markup.Break(width, _) => XML.string(Symbol.spaces(width)) | 
| 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 105 | case _ => List(XML.Elem(markup, unformatted(body))) | 
| 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 106 | } | 
| 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 107 | case XML.Text(text) => XML.string(split_lines(text).mkString(Symbol.space)) | 
| 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 108 | } | 
| 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 109 | } | 
| 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 110 | |
| 
338adf8d423c
support Pretty.unformatted, similar to ML version;
 wenzelm parents: 
75958diff
changeset | 111 | |
| 61874 | 112 | /* formatted output */ | 
| 113 | ||
| 75393 | 114 |   private sealed case class Text(tx: XML.Body = Nil, pos: Double = 0.0, nl: Int = 0) {
 | 
| 61874 | 115 | def newline: Text = copy(tx = fbrk :: tx, pos = 0.0, nl = nl + 1) | 
| 116 | def string(s: String, len: Double): Text = | |
| 117 | copy(tx = if (s == "") tx else XML.Text(s) :: tx, pos = pos + len) | |
| 118 | def blanks(wd: Int): Text = string(Symbol.spaces(wd), wd.toDouble) | |
| 119 | def content: XML.Body = tx.reverse | |
| 120 | } | |
| 121 | ||
| 122 | private def break_dist(trees: List[Tree], after: Double): Double = | |
| 123 |     trees match {
 | |
| 124 | case (_: Break) :: _ => 0.0 | |
| 125 | case t :: ts => t.length + break_dist(ts, after) | |
| 126 | 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: 
36687diff
changeset | 127 | } | 
| 
379f5b1e7f91
replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
 wenzelm parents: 
36687diff
changeset | 128 | |
| 61874 | 129 | private def force_break(tree: Tree): Tree = | 
| 130 |     tree match { case Break(false, wd, ind) => Break(true, wd, ind) case _ => tree }
 | |
| 71601 | 131 | 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: 
51569diff
changeset | 132 | |
| 61874 | 133 | private def force_next(trees: List[Tree]): List[Tree] = | 
| 134 |     trees match {
 | |
| 135 | case Nil => Nil | |
| 136 | case (t: Break) :: ts => force_break(t) :: ts | |
| 137 | case t :: ts => t :: force_next(ts) | |
| 138 | } | |
| 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: 
51569diff
changeset | 139 | |
| 71601 | 140 | val default_margin: Double = 76.0 | 
| 141 | val default_breakgain: Double = default_margin / 20 | |
| 36820 | 142 | |
| 67547 
aefe7a7b330a
clarified breakgain: keeping it constant avoids margin fluctuation in Pretty_Tooltip vs. Pretty_Text_Area;
 wenzelm parents: 
65130diff
changeset | 143 | def formatted(input: XML.Body, | 
| 67896 | 144 | margin: Double = default_margin, | 
| 145 | breakgain: Double = default_breakgain, | |
| 75393 | 146 | metric: Metric = Default_Metric | 
| 147 |   ): XML.Body = {
 | |
| 51569 
4e084727faae
maintain integer indentation during formatting -- it needs to be implemented by repeated spaces eventually;
 wenzelm parents: 
51568diff
changeset | 148 | val emergencypos = (margin / 2).round.toInt | 
| 36687 | 149 | |
| 61874 | 150 | def make_tree(inp: XML.Body): List[Tree] = | 
| 151 |       inp flatMap {
 | |
| 152 | case XML.Wrapped_Elem(markup, body1, body2) => | |
| 153 | List(make_block(Some(markup, Some(body1)), false, 0, make_tree(body2))) | |
| 154 | case XML.Elem(markup, body) => | |
| 155 |           markup match {
 | |
| 156 | case Markup.Block(consistent, indent) => | |
| 157 | List(make_block(None, consistent, indent, make_tree(body))) | |
| 158 | case Markup.Break(width, indent) => | |
| 62785 
70b9c7d4ed7f
more robust pretty printing: permissive treatment of bad values;
 wenzelm parents: 
61883diff
changeset | 159 | List(Break(false, force_nat(width), force_nat(indent))) | 
| 61874 | 160 | case Markup(Markup.ITEM, _) => | 
| 161 | List(make_block(None, false, 2, | |
| 162 | make_tree(XML.elem(Markup.BULLET, space) :: space ::: body))) | |
| 163 | case _ => | |
| 164 | List(make_block(Some((markup, None)), false, 0, make_tree(body))) | |
| 165 | } | |
| 166 | case XML.Text(text) => | |
| 167 | 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: 
36763diff
changeset | 168 | } | 
| 
ed97e877ff2d
more precise pretty printing based on actual font metrics;
 wenzelm parents: 
36763diff
changeset | 169 | |
| 61871 | 170 | def format(trees: List[Tree], blockin: Int, after: Double, text: Text): Text = | 
| 36687 | 171 |       trees match {
 | 
| 172 | case Nil => text | |
| 173 | ||
| 61871 | 174 | case Block(markup, consistent, indent, body, blen) :: ts => | 
| 51569 
4e084727faae
maintain integer indentation during formatting -- it needs to be implemented by repeated spaces eventually;
 wenzelm parents: 
51568diff
changeset | 175 | val pos1 = (text.pos + indent).ceil.toInt | 
| 36687 | 176 | val pos2 = pos1 % emergencypos | 
| 61868 | 177 | val blockin1 = if (pos1 < emergencypos) pos1 else pos2 | 
| 61864 | 178 | val d = break_dist(ts, after) | 
| 179 | val body1 = if (consistent && text.pos + blen > margin - d) force_all(body) else body | |
| 61871 | 180 | val btext = | 
| 181 |             markup match {
 | |
| 182 | case None => format(body1, blockin1, d, text) | |
| 183 | case Some((m, markup_body)) => | |
| 184 | val btext0 = format(body1, blockin1, d, text.copy(tx = Nil)) | |
| 185 | val elem = | |
| 186 |                   markup_body match {
 | |
| 187 | case None => XML.Elem(m, btext0.content) | |
| 188 | case Some(b) => XML.Wrapped_Elem(m, b, btext0.content) | |
| 189 | } | |
| 190 | btext0.copy(tx = elem :: text.tx) | |
| 191 | } | |
| 61864 | 192 | val ts1 = if (text.nl < btext.nl) force_next(ts) else ts | 
| 36687 | 193 | format(ts1, blockin, after, btext) | 
| 194 | ||
| 61871 | 195 | case Break(force, wd, ind) :: ts => | 
| 196 | if (!force && | |
| 197 | text.pos + wd <= ((margin - break_dist(ts, after)) max (blockin + breakgain))) | |
| 36687 | 198 | format(ts, blockin, after, text.blanks(wd)) | 
| 61862 
e2a9e46ac0fb
support pretty break indent, like underlying ML systems;
 wenzelm parents: 
55551diff
changeset | 199 | else format(ts, blockin, after, text.newline.blanks(blockin + ind)) | 
| 36687 | 200 | |
| 61871 | 201 | case Str(s, len) :: ts => format(ts, blockin, after, text.string(s, len)) | 
| 36687 | 202 | } | 
| 61874 | 203 | format(make_tree(input), 0, 0.0, Text()).content | 
| 36687 | 204 | } | 
| 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: 
36689diff
changeset | 205 | |
| 67547 
aefe7a7b330a
clarified breakgain: keeping it constant avoids margin fluctuation in Pretty_Tooltip vs. Pretty_Text_Area;
 wenzelm parents: 
65130diff
changeset | 206 | def string_of(input: XML.Body, | 
| 67896 | 207 | margin: Double = default_margin, | 
| 208 | breakgain: Double = default_breakgain, | |
| 209 | metric: Metric = Default_Metric): String = | |
| 67547 
aefe7a7b330a
clarified breakgain: keeping it constant avoids margin fluctuation in Pretty_Tooltip vs. Pretty_Text_Area;
 wenzelm parents: 
65130diff
changeset | 210 | XML.content(formatted(input, margin = margin, breakgain = breakgain, metric = metric)) | 
| 36683 | 211 | } |