author | wenzelm |
Mon, 04 Nov 2024 22:05:20 +0100 | |
changeset 81346 | 0cdd6729a962 |
parent 81340 | 30f7eb65d679 |
child 81395 | d9f791f75b8b |
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:
45666
diff
changeset
|
20 |
|
80940 | 21 |
val bullet: XML.Body = XML.elem(Markup.BULLET, space) :: space |
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 |
81294
108284c8cbfd
removed obsolete markup for "open_block" (see also d5ad89fda714): Isabelle/Scala directly supports XML.Elem pretty-printing;
wenzelm
parents:
81121
diff
changeset
|
26 |
): XML.Tree = 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 |
|
61871 | 28 |
def brk(width: Int, indent: Int = 0): XML.Tree = |
81120 | 29 |
XML.Elem(Markup.Break(width = width, indent = indent), spaces(width)) |
61871 | 30 |
|
69867 | 31 |
val fbrk: XML.Tree = XML.newline |
65130 | 32 |
def fbreaks(ts: List[XML.Tree]): XML.Body = Library.separate(fbrk, ts) |
61871 | 33 |
|
34 |
val Separator: XML.Body = List(XML.elem(Markup.SEPARATOR, space), fbrk) |
|
75958 | 35 |
def separate(ts: List[XML.Tree], sep: XML.Body = Separator): XML.Body = |
36 |
Library.separate(sep, ts.map(List(_))).flatten |
|
37 |
||
38 |
val comma: XML.Body = List(XML.Text(","), brk(1)) |
|
39 |
def commas(ts: List[XML.Tree]): XML.Body = separate(ts, sep = comma) |
|
40 |
||
41 |
def `enum`(ts: List[XML.Tree], |
|
42 |
bg: String = "(", |
|
43 |
en: String = ")", |
|
44 |
sep: XML.Body = comma, |
|
45 |
indent: Int = 2 |
|
46 |
): XML.Tree = Pretty.block(XML.enclose(bg, en, separate(ts, sep = sep)), indent = indent) |
|
61864 | 47 |
|
48704
85a3de10567d
tuned signature -- make Pretty less dependent on Symbol;
wenzelm
parents:
45666
diff
changeset
|
48 |
|
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
|
49 |
/* 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
|
50 |
|
75393 | 51 |
abstract class Metric { |
81340 | 52 |
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
|
53 |
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
|
54 |
} |
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 |
|
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 |
|
36687 | 57 |
/* markup trees with physical blocks and breaks */ |
58 |
||
62820 | 59 |
private def force_nat(i: Int): Int = i max 0 |
60 |
||
61874 | 61 |
private sealed abstract class Tree { def length: Double } |
62 |
private case class Block( |
|
80800 | 63 |
markup: Markup, |
64 |
markup_body: Option[XML.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
|
65 |
open_block: Boolean, |
80798 | 66 |
consistent: Boolean, |
67 |
indent: Int, |
|
68 |
body: List[Tree], |
|
69 |
length: Double |
|
70 |
) extends Tree |
|
75393 | 71 |
private case class Break(force: Boolean, width: Int, indent: Int) extends Tree { |
72 |
def length: Double = width.toDouble |
|
73 |
} |
|
61874 | 74 |
private case class Str(string: String, length: Double) extends Tree |
36683 | 75 |
|
61874 | 76 |
private val FBreak = Break(true, 1, 0) |
36683 | 77 |
|
80940 | 78 |
private def make_block(body: List[Tree], |
79 |
markup: Markup = Markup.Empty, |
|
80 |
markup_body: Option[XML.Body] = None, |
|
81121
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents:
81120
diff
changeset
|
81 |
open_block: Boolean = false, |
80940 | 82 |
consistent: Boolean = false, |
83 |
indent: Int = 0 |
|
75393 | 84 |
): Tree = { |
62785
70b9c7d4ed7f
more robust pretty printing: permissive treatment of bad values;
wenzelm
parents:
61883
diff
changeset
|
85 |
val indent1 = force_nat(indent) |
70b9c7d4ed7f
more robust pretty printing: permissive treatment of bad values;
wenzelm
parents:
61883
diff
changeset
|
86 |
|
75393 | 87 |
@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
|
88 |
val (line, rest) = |
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
wenzelm
parents:
61875
diff
changeset
|
89 |
Library.take_prefix[Tree]({ case Break(true, _, _) => false case _ => true }, prts) |
73359 | 90 |
val len1 = (line.foldLeft(0.0) { case (l, t) => l + t.length }) max len |
71781 | 91 |
(rest: @unchecked) match { |
61883
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
wenzelm
parents:
61875
diff
changeset
|
92 |
case Break(true, _, ind) :: rest1 => |
62785
70b9c7d4ed7f
more robust pretty printing: permissive treatment of bad values;
wenzelm
parents:
61883
diff
changeset
|
93 |
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
|
94 |
case Nil => len1 |
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
wenzelm
parents:
61875
diff
changeset
|
95 |
} |
c0f34fe6aa61
clarified length of block with pre-existant forced breaks;
wenzelm
parents:
61875
diff
changeset
|
96 |
} |
81121
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents:
81120
diff
changeset
|
97 |
Block(markup, markup_body, 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
|
98 |
} |
36683 | 99 |
|
61874 | 100 |
|
80845 | 101 |
/* no formatting */ |
76086
338adf8d423c
support Pretty.unformatted, similar to ML version;
wenzelm
parents:
75958
diff
changeset
|
102 |
|
80871 | 103 |
def output_content(pure: Boolean, output: XML.Body): String = |
104 |
XML.content(if (pure) Protocol_Message.clean_output(output) else output) |
|
105 |
||
80807 | 106 |
def unbreakable(input: XML.Body): XML.Body = |
76086
338adf8d423c
support Pretty.unformatted, similar to ML version;
wenzelm
parents:
75958
diff
changeset
|
107 |
input flatMap { |
338adf8d423c
support Pretty.unformatted, similar to ML version;
wenzelm
parents:
75958
diff
changeset
|
108 |
case XML.Wrapped_Elem(markup, body1, body2) => |
80807 | 109 |
List(XML.Wrapped_Elem(markup, body1, unbreakable(body2))) |
110 |
case XML.Elem(Markup.Break(width, _), _) => spaces(width) |
|
111 |
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
|
112 |
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
|
113 |
} |
80807 | 114 |
|
80871 | 115 |
def unformatted_string_of(input: XML.Body, pure: Boolean = false): String = |
116 |
output_content(pure, unbreakable(input)) |
|
76086
338adf8d423c
support Pretty.unformatted, similar to ML version;
wenzelm
parents:
75958
diff
changeset
|
117 |
|
338adf8d423c
support Pretty.unformatted, similar to ML version;
wenzelm
parents:
75958
diff
changeset
|
118 |
|
80845 | 119 |
/* formatting */ |
61874 | 120 |
|
75393 | 121 |
private sealed case class Text(tx: XML.Body = Nil, pos: Double = 0.0, nl: Int = 0) { |
61874 | 122 |
def newline: Text = copy(tx = fbrk :: tx, pos = 0.0, nl = nl + 1) |
123 |
def string(s: String, len: Double): Text = |
|
124 |
copy(tx = if (s == "") tx else XML.Text(s) :: tx, pos = pos + len) |
|
125 |
def blanks(wd: Int): Text = string(Symbol.spaces(wd), wd.toDouble) |
|
126 |
def content: XML.Body = tx.reverse |
|
127 |
} |
|
128 |
||
129 |
private def break_dist(trees: List[Tree], after: Double): Double = |
|
130 |
trees match { |
|
131 |
case (_: Break) :: _ => 0.0 |
|
132 |
case t :: ts => t.length + break_dist(ts, after) |
|
133 |
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
|
134 |
} |
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
|
135 |
|
61874 | 136 |
private def force_break(tree: Tree): Tree = |
137 |
tree match { case Break(false, wd, ind) => Break(true, wd, ind) case _ => tree } |
|
71601 | 138 |
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
|
139 |
|
61874 | 140 |
private def force_next(trees: List[Tree]): List[Tree] = |
141 |
trees match { |
|
142 |
case Nil => Nil |
|
143 |
case (t: Break) :: ts => force_break(t) :: ts |
|
144 |
case t :: ts => t :: force_next(ts) |
|
145 |
} |
|
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
|
146 |
|
71601 | 147 |
val default_margin: Double = 76.0 |
148 |
val default_breakgain: Double = default_margin / 20 |
|
36820 | 149 |
|
67547
aefe7a7b330a
clarified breakgain: keeping it constant avoids margin fluctuation in Pretty_Tooltip vs. Pretty_Text_Area;
wenzelm
parents:
65130
diff
changeset
|
150 |
def formatted(input: XML.Body, |
67896 | 151 |
margin: Double = default_margin, |
152 |
breakgain: Double = default_breakgain, |
|
81346 | 153 |
metric: Metric = Codepoint.Metric |
75393 | 154 |
): XML.Body = { |
51569
4e084727faae
maintain integer indentation during formatting -- it needs to be implemented by repeated spaces eventually;
wenzelm
parents:
51568
diff
changeset
|
155 |
val emergencypos = (margin / 2).round.toInt |
36687 | 156 |
|
61874 | 157 |
def make_tree(inp: XML.Body): List[Tree] = |
158 |
inp flatMap { |
|
159 |
case XML.Wrapped_Elem(markup, body1, body2) => |
|
80940 | 160 |
List(make_block(make_tree(body2), markup = markup, markup_body = Some(body1))) |
61874 | 161 |
case XML.Elem(markup, body) => |
162 |
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
|
163 |
case Markup.Block(consistent, indent) => |
81121
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents:
81120
diff
changeset
|
164 |
List( |
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents:
81120
diff
changeset
|
165 |
make_block(make_tree(body), |
81294
108284c8cbfd
removed obsolete markup for "open_block" (see also d5ad89fda714): Isabelle/Scala directly supports XML.Elem pretty-printing;
wenzelm
parents:
81121
diff
changeset
|
166 |
consistent = consistent, indent = indent, open_block = false)) |
61874 | 167 |
case Markup.Break(width, indent) => |
62785
70b9c7d4ed7f
more robust pretty printing: permissive treatment of bad values;
wenzelm
parents:
61883
diff
changeset
|
168 |
List(Break(false, force_nat(width), force_nat(indent))) |
61874 | 169 |
case Markup(Markup.ITEM, _) => |
80941
fd7a70babec1
more markup <expression kind="item"> in Isabelle/Scala, with pro-forma Markup_Kind.setup in Isabelle/ML;
wenzelm
parents:
80940
diff
changeset
|
170 |
List(make_block(make_tree(bullet ::: body), |
fd7a70babec1
more markup <expression kind="item"> in Isabelle/Scala, with pro-forma Markup_Kind.setup in Isabelle/ML;
wenzelm
parents:
80940
diff
changeset
|
171 |
markup = Markup.Expression.item, indent = 2)) |
61874 | 172 |
case _ => |
81121
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents:
81120
diff
changeset
|
173 |
List(make_block(make_tree(body), markup = markup, open_block = true)) |
61874 | 174 |
} |
175 |
case XML.Text(text) => |
|
176 |
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
|
177 |
} |
ed97e877ff2d
more precise pretty printing based on actual font metrics;
wenzelm
parents:
36763
diff
changeset
|
178 |
|
61871 | 179 |
def format(trees: List[Tree], blockin: Int, after: Double, text: Text): Text = |
36687 | 180 |
trees match { |
181 |
case Nil => text |
|
182 |
||
81121
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents:
81120
diff
changeset
|
183 |
case (block: Block) :: ts if block.open_block => |
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents:
81120
diff
changeset
|
184 |
val btext = format(block.body, blockin, break_dist(ts, after), text.copy(tx = Nil)) |
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents:
81120
diff
changeset
|
185 |
val ts1 = if (text.nl < btext.nl) force_next(ts) else ts |
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents:
81120
diff
changeset
|
186 |
val btext1 = btext.copy(tx = XML.Elem(block.markup, btext.content) :: text.tx) |
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents:
81120
diff
changeset
|
187 |
format(ts1, blockin, after, btext1) |
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents:
81120
diff
changeset
|
188 |
|
7cacedbddba7
support for pretty blocks that are "open" and thus have no impact on formatting, only on markup;
wenzelm
parents:
81120
diff
changeset
|
189 |
case Block(markup, markup_body, _, consistent, indent, body, blen) :: ts => |
51569
4e084727faae
maintain integer indentation during formatting -- it needs to be implemented by repeated spaces eventually;
wenzelm
parents:
51568
diff
changeset
|
190 |
val pos1 = (text.pos + indent).ceil.toInt |
36687 | 191 |
val pos2 = pos1 % emergencypos |
61868 | 192 |
val blockin1 = if (pos1 < emergencypos) pos1 else pos2 |
61864 | 193 |
val d = break_dist(ts, after) |
194 |
val body1 = if (consistent && text.pos + blen > margin - d) force_all(body) else body |
|
61871 | 195 |
val btext = |
80800 | 196 |
if (markup.is_empty && markup_body.isEmpty) format(body1, blockin1, d, text) |
197 |
else { |
|
198 |
val btext0 = format(body1, blockin1, d, text.copy(tx = Nil)) |
|
199 |
val elem = |
|
200 |
markup_body match { |
|
201 |
case None => XML.Elem(markup, btext0.content) |
|
202 |
case Some(body1) => XML.Wrapped_Elem(markup, body1, btext0.content) |
|
203 |
} |
|
204 |
btext0.copy(tx = elem :: text.tx) |
|
61871 | 205 |
} |
61864 | 206 |
val ts1 = if (text.nl < btext.nl) force_next(ts) else ts |
36687 | 207 |
format(ts1, blockin, after, btext) |
208 |
||
61871 | 209 |
case Break(force, wd, ind) :: ts => |
210 |
if (!force && |
|
211 |
text.pos + wd <= ((margin - break_dist(ts, after)) max (blockin + breakgain))) |
|
36687 | 212 |
format(ts, blockin, after, text.blanks(wd)) |
61862
e2a9e46ac0fb
support pretty break indent, like underlying ML systems;
wenzelm
parents:
55551
diff
changeset
|
213 |
else format(ts, blockin, after, text.newline.blanks(blockin + ind)) |
36687 | 214 |
|
61871 | 215 |
case Str(s, len) :: ts => format(ts, blockin, after, text.string(s, len)) |
36687 | 216 |
} |
61874 | 217 |
format(make_tree(input), 0, 0.0, Text()).content |
36687 | 218 |
} |
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
|
219 |
|
67547
aefe7a7b330a
clarified breakgain: keeping it constant avoids margin fluctuation in Pretty_Tooltip vs. Pretty_Text_Area;
wenzelm
parents:
65130
diff
changeset
|
220 |
def string_of(input: XML.Body, |
80871 | 221 |
margin: Double = default_margin, |
222 |
breakgain: Double = default_breakgain, |
|
81346 | 223 |
metric: Metric = Codepoint.Metric, |
80871 | 224 |
pure: Boolean = false |
225 |
): String = { |
|
226 |
output_content(pure, formatted(input, margin = margin, breakgain = breakgain, metric = metric)) |
|
227 |
} |
|
36683 | 228 |
} |