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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
//! Pretty-printing functionality (such as automatic indentation).
use indexmap::IndexSet;
use internal_iterator::{
FromInternalIterator, InternalIterator, IntoInternalIterator, IteratorExt,
};
use smallvec::SmallVec;
use std::borrow::Cow;
use std::fmt::Write as _;
use std::ops::ControlFlow;
use std::rc::Rc;
use std::{fmt, iter, mem};
/// Part of a pretty document, made up of [`Node`]s.
//
// FIXME(eddyb) `Document` might be too long, what about renaming this to `Doc`?
#[derive(Clone, Default, PartialEq)]
pub struct Fragment {
pub nodes: SmallVec<[Node; 8]>,
}
#[derive(Clone, PartialEq)]
pub enum Node {
Text(Option<Styles>, Cow<'static, str>),
/// Anchor (HTML `<a href="#...">`, optionally with `id="..."` when `is_def`),
/// using [`Node::Text`]-like "styled text" nodes for its text contents.
//
// FIXME(eddyb) could this use `Box<Fragment>` instead? may complicate layout
Anchor {
is_def: bool,
anchor: Rc<str>,
text: Box<[(Option<Styles>, Cow<'static, str>)]>,
},
/// Container for [`Fragment`]s, using block layout (indented on separate lines).
IndentedBlock(Vec<Fragment>),
/// Container for [`Fragment`]s, either using inline layout (all on one line)
/// or block layout (indented on separate lines).
InlineOrIndentedBlock(Vec<Fragment>),
/// Require that nodes before and after this node, are separated by some
/// whitespace (either by a single space, or by being on different lines).
///
/// This is similar in effect to a `Text(" ")`, except that it doesn't add
/// leading/trailing spaces when found at the start/end of a line, as the
/// adjacent `\n` is enough of a "breaking space".
///
/// Conversely, `Text(" ")` can be considered a "non-breaking space" (NBSP).
BreakingOnlySpace,
/// Require that nodes before and after this node, go on different lines.
///
/// This is similar in effect to a `Text("\n")`, except that it doesn't
/// introduce a new `\n` when the previous/next node(s) already end/start
/// on a new line (whether from `Text("\n")` or another `ForceLineStart`).
ForceLineSeparation,
// FIXME(eddyb) replace this with something lower-level than layout.
IfBlockLayout(&'static str),
}
#[derive(Copy, Clone, Default, PartialEq)]
pub struct Styles {
/// RGB color.
pub color: Option<[u8; 3]>,
/// `0.0` is fully transparent, `1.0` is fully opaque.
//
// FIXME(eddyb) move this into `color` (which would become RGBA).
pub color_opacity: Option<f32>,
/// `0` corresponds to the default, with positive values meaning thicker,
/// and negative values thinner text, respectively.
///
/// For HTML output, each unit is equivalent to `±100` in CSS `font-weight`.
pub thickness: Option<i8>,
/// `0` corresponds to the default, with positive values meaning larger,
/// and negative values smaller text, respectively.
///
/// For HTML output, each unit is equivalent to `±0.1em` in CSS `font-size`.
pub size: Option<i8>,
pub subscript: bool,
pub superscript: bool,
// FIXME(eddyb) maybe a more general `filter` system would be better?
pub desaturate_and_dim_for_unchanged_multiversion_line: bool,
}
impl Styles {
pub fn color(color: [u8; 3]) -> Self {
Self { color: Some(color), ..Self::default() }
}
pub fn apply(self, text: impl Into<Cow<'static, str>>) -> Node {
Node::Text(Some(self), text.into())
}
// HACK(eddyb) this allows us to control `<sub>`/`<sup>` `font-size` exactly,
// and use the same information for both layout and the CSS we emit.
fn effective_size(&self) -> Option<i8> {
self.size.or(if self.subscript || self.superscript { Some(-2) } else { None })
}
}
/// Color palettes built-in for convenience (colors are RGB, as `[u8; 3]`).
pub mod palettes {
/// Minimalist palette, chosen to work with both light and dark backgrounds.
pub mod simple {
pub const DARK_GRAY: [u8; 3] = [0x44, 0x44, 0x44];
pub const LIGHT_GRAY: [u8; 3] = [0x88, 0x88, 0x88];
pub const RED: [u8; 3] = [0xcc, 0x55, 0x55];
pub const GREEN: [u8; 3] = [0x44, 0x99, 0x44];
pub const BLUE: [u8; 3] = [0x44, 0x66, 0xcc];
pub const YELLOW: [u8; 3] = [0xcc, 0x99, 0x44];
pub const MAGENTA: [u8; 3] = [0xcc, 0x44, 0xcc];
pub const CYAN: [u8; 3] = [0x44, 0x99, 0xcc];
pub const ORANGE: [u8; 3] = [0xcc, 0x77, 0x55];
}
}
impl From<&'static str> for Node {
fn from(text: &'static str) -> Self {
Self::Text(None, text.into())
}
}
impl From<String> for Node {
fn from(text: String) -> Self {
Self::Text(None, text.into())
}
}
impl<T: Into<Node>> From<T> for Fragment {
fn from(x: T) -> Self {
Self { nodes: [x.into()].into_iter().collect() }
}
}
impl Fragment {
pub fn new(fragments: impl IntoIterator<Item = impl Into<Self>>) -> Self {
Self { nodes: fragments.into_iter().flat_map(|fragment| fragment.into().nodes).collect() }
}
/// Perform layout on the [`Fragment`], limiting lines to `max_line_width`
/// columns where possible.
pub fn layout_with_max_line_width(mut self, max_line_width: usize) -> FragmentPostLayout {
// FIXME(eddyb) maybe make this a method on `Columns`?
let max_line_width =
Columns { char_width_tenths: max_line_width.try_into().unwrap_or(u16::MAX) * 10 };
self.approx_layout(MaxWidths { inline: max_line_width, block: max_line_width });
FragmentPostLayout(self)
}
}
// HACK(eddyb) simple wrapper to avoid misuse externally.
pub struct FragmentPostLayout(Fragment);
impl fmt::Display for FragmentPostLayout {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let result = self
.render_to_text_ops()
.filter_map(|op| match op {
TextOp::Text(text) => Some(text),
_ => None,
})
.try_for_each(|text| {
f.write_str(text).map_or_else(ControlFlow::Break, ControlFlow::Continue)
});
match result {
ControlFlow::Continue(()) => Ok(()),
ControlFlow::Break(e) => Err(e),
}
}
}
impl FragmentPostLayout {
/// Flatten the [`Fragment`] to [`TextOp`]s.
pub(super) fn render_to_text_ops(&self) -> impl InternalIterator<Item = TextOp<'_>> {
self.0.render_to_text_ops()
}
/// Flatten the [`Fragment`] to HTML, producing a [`HtmlSnippet`].
//
// FIXME(eddyb) provide a non-allocating version.
pub fn render_to_html(&self) -> HtmlSnippet {
self.render_to_text_ops().collect()
}
}
#[derive(Default)]
pub struct HtmlSnippet {
pub head_deduplicatable_elements: IndexSet<String>,
pub body: String,
}
impl HtmlSnippet {
/// Inject (using JavaScript) the ability to use `?dark` to choose a simple
/// "dark mode" (only different default background and foreground colors),
/// auto-detection using media queries, and `?light` to force-disable it.
pub fn with_dark_mode_support(&mut self) -> &mut Self {
self.head_deduplicatable_elements.insert(
r#"
<script>
(function() {
var params = new URLSearchParams(document.location.search);
var dark = params.has("dark"), light = params.has("light");
if(dark || light) {
if(dark && !light) {
document.documentElement.classList.add("simple-dark-theme");
// HACK(eddyb) forcefully disable Dark Reader, for two reasons:
// - its own detection of websites with built-in dark themes
// (https://github.com/darkreader/darkreader/pull/7995)
// isn't on by default, and the combination is jarring
// - it interacts badly with whole-document-replacement
// (as used by htmlpreview.github.io)
document.documentElement.removeAttribute('data-darkreader-scheme');
document.querySelectorAll('style.darkreader')
.forEach(style => style.disabled = true);
}
} else if(matchMedia("(prefers-color-scheme: dark)").matches) {
// FIXME(eddyb) also use media queries in CSS directly, to ensure dark mode
// still works with JS disabled (sadly that likely requires CSS duplication).
document.location.search += (document.location.search ? "&" : "?") + "dark";
}
})();
</script>
<style>
/* HACK(eddyb) `[data-darkreader-scheme="dark"]` is for detecting Dark Reader,
to avoid transient interactions (see also comment in the `<script>`). */
html.simple-dark-theme:not([data-darkreader-scheme="dark"]) {
background: #16181a;
color: #dbd8d6;
/* Request browser UI elements to be dark-themed if possible. */
color-scheme: dark;
}
</style>
"#
.into(),
);
self
}
/// Combine `head` and `body` into a complete HTML document, which starts
/// with `<!doctype html>`. Ideal for writing out a whole `.html` file.
//
// FIXME(eddyb) provide a non-allocating version.
pub fn to_html_doc(&self) -> String {
let mut html = String::new();
html += "<!doctype html>\n";
html += "<html>\n";
html += "<head>\n";
html += "<meta charset=\"utf-8\">\n";
for elem in &self.head_deduplicatable_elements {
html += elem;
html += "\n";
}
html += "</head>\n";
html += "<body>";
html += &self.body;
html += "</body>\n";
html += "</html>\n";
html
}
}
// FIXME(eddyb) is this impl the best way? (maybe it should be a inherent method)
impl<'a> FromInternalIterator<TextOp<'a>> for HtmlSnippet {
fn from_iter<T>(text_ops: T) -> Self
where
T: IntoInternalIterator<Item = TextOp<'a>>,
{
// HACK(eddyb) using an UUID as a class name in lieu of "scoped <style>".
const ROOT_CLASS_NAME: &str = "spirt-90c2056d-5b38-4644-824a-b4be1c82f14d";
// FIXME(eddyb) consider interning styles into CSS classes, to avoid
// using inline `style="..."` attributes.
let style_elem = "
<style>
SCOPE {
/* HACK(eddyb) reset default margin to something reasonable. */
margin: 1ch;
/* HACK(eddyb) avoid unnecessarily small or thin text. */
font-size: 17px;
font-weight: 500;
}
SCOPE a {
color: unset;
font-weight: 900;
}
SCOPE a:not(:hover) {
text-decoration: unset;
}
SCOPE sub, SCOPE sup {
line-height: 0;
}
/* HACK(eddyb) using a class (instead of an inline style) so that hovering
over a multiversion table cell can disable its desaturation/dimming */
SCOPE:not(:hover) .unchanged {
filter: saturate(0.3) opacity(0.5);
}
</style>
"
.replace("SCOPE", &format!("pre.{ROOT_CLASS_NAME}"));
let push_attr = |body: &mut String, attr, value: &str| {
// Quick sanity check.
assert!(value.chars().all(|c| !(c == '"' || c == '&')));
body.extend([" ", attr, "=\"", value, "\""]);
};
// HACK(eddyb) load-bearing newline after `<pre ...>`, to front-load any
// weird HTML whitespace handling, and allow the actual contents to start
// with empty lines (i.e. `\n\n...`), without e.g. losing the first one.
let mut body = format!("<pre class=\"{ROOT_CLASS_NAME}\">\n");
text_ops.into_internal_iter().for_each(|op| match op {
TextOp::PushStyles(styles) | TextOp::PopStyles(styles) => {
let mut special_tags = [("sub", styles.subscript), ("sup", styles.superscript)]
.into_iter()
.filter(|&(_, cond)| cond)
.map(|(tag, _)| tag);
let tag = special_tags.next().unwrap_or("span");
if let Some(other_tag) = special_tags.next() {
// FIXME(eddyb) support by opening/closing multiple tags.
panic!("`<{tag}>` conflicts with `<{other_tag}>`");
}
body += "<";
if let TextOp::PopStyles(_) = op {
body += "/";
}
body += tag;
if let TextOp::PushStyles(_) = op {
let Styles {
color,
color_opacity,
thickness,
size: _,
subscript,
superscript,
desaturate_and_dim_for_unchanged_multiversion_line,
} = *styles;
let mut css_style = String::new();
if let Some(a) = color_opacity {
let [r, g, b] = color.expect("color_opacity without color");
write!(css_style, "color:rgba({r},{g},{b},{a});").unwrap();
} else if let Some([r, g, b]) = color {
write!(css_style, "color:#{r:02x}{g:02x}{b:02x};").unwrap();
}
if let Some(thickness) = thickness {
write!(css_style, "font-weight:{};", 500 + (thickness as i32) * 100)
.unwrap();
}
if let Some(size) = styles.effective_size() {
write!(css_style, "font-size:{}em;", 1.0 + (size as f64) * 0.1).unwrap();
if !(subscript || superscript) {
// HACK(eddyb) without this, small text is placed too low.
write!(css_style, "vertical-align:middle;").unwrap();
}
}
if !css_style.is_empty() {
push_attr(&mut body, "style", &css_style);
}
if desaturate_and_dim_for_unchanged_multiversion_line {
push_attr(&mut body, "class", "unchanged");
}
}
body += ">";
}
TextOp::PushAnchor { is_def, anchor } => {
body += "<a";
// HACK(eddyb) this avoids `push_attr` because anchors are pre-escaped.
// FIXME(eddyb) should escaping anchors be left to here?
assert!(anchor.chars().all(|c| c != '"'));
if is_def {
write!(body, " id=\"{anchor}\"").unwrap();
}
write!(body, " href=\"#{anchor}\">").unwrap();
}
TextOp::PopAnchor { .. } => body += "</a>",
TextOp::Text(text) => {
// Minimal escaping, just enough to produce valid HTML.
let escape_from = ['&', '<'];
let escape_to = ["&", "<"];
for piece in text.split_inclusive(escape_from) {
let mut chars = piece.chars();
let maybe_needs_escape = chars.next_back();
body += chars.as_str();
if let Some(maybe_needs_escape) = maybe_needs_escape {
match escape_from.iter().position(|&c| maybe_needs_escape == c) {
Some(escape_idx) => body += escape_to[escape_idx],
None => body.push(maybe_needs_escape),
}
}
}
}
});
body += "</pre>";
HtmlSnippet { head_deduplicatable_elements: [style_elem].into_iter().collect(), body }
}
}
// Rendering implementation details (including approximate layout).
/// Fractional number of columns, used here to account for `Node::StyledText`
/// being used to intentionally reduce the size of many "helper" pieces of text,
/// at least for the HTML output (while this may lead to a less consistently
/// formatted plaintext output, making good use of width is far more important
/// for the HTML output, especially when used with `multiversion` tables).
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct Columns {
/// As our `font-size` control granularity is in multiples of `0.1em`,
/// the overall width of a line should end up a multiple of `0.1ch`,
/// i.e. we're counting tenths of a column's width at the default font size.
char_width_tenths: u16,
}
impl Columns {
const ZERO: Self = Self { char_width_tenths: 0 };
fn text_width(text: &str) -> Self {
Self::maybe_styled_text_width(text, None)
}
fn maybe_styled_text_width(text: &str, style: Option<&Styles>) -> Self {
assert!(!text.contains('\n'));
let font_size =
u16::try_from(10 + style.and_then(|style| style.effective_size()).unwrap_or(0))
.unwrap_or(0);
// FIXME(eddyb) use `unicode-width` crate for accurate column count.
Self {
char_width_tenths: text.len().try_into().unwrap_or(u16::MAX).saturating_mul(font_size),
}
}
fn saturating_add(self, other: Self) -> Self {
Self { char_width_tenths: self.char_width_tenths.saturating_add(other.char_width_tenths) }
}
fn saturating_sub(self, other: Self) -> Self {
Self { char_width_tenths: self.char_width_tenths.saturating_sub(other.char_width_tenths) }
}
}
/// The approximate shape of a [`Node`], regarding its 2D placement.
#[derive(Copy, Clone)]
enum ApproxLayout {
/// Only occupies part of a line, (at most) `worst_width` columns wide.
///
/// `worst_width` can exceed the `inline` field of [`MaxWidths`], in which
/// case the choice of inline vs block is instead made by a surrounding node.
Inline {
worst_width: Columns,
/// How much of `worst_width` comes from `Node::IfBlockLayout` - that is,
/// `worst_width` still includes `Node::IfBlockLayout`, so conservative
/// decisions will still be made, but `excess_width_from_only_if_block`
/// can be used to reduce `worst_width` when block layout is no longer
/// a possibility (i.e. by the enclosing `Node::InlineOrIndentedBlock`).
excess_width_from_only_if_block: Columns,
},
/// Needs to occupy multiple lines, but may also have the equivalent of
/// an `Inline` before (`pre_`) and after (`post_`) the multi-line block.
//
// FIXME(eddyb) maybe turn `ApproxLayout` into a `struct` instead?
BlockOrMixed { pre_worst_width: Columns, post_worst_width: Columns },
}
impl ApproxLayout {
fn append(self, other: Self) -> Self {
match (self, other) {
(
Self::Inline { worst_width: a, excess_width_from_only_if_block: a_excess_foib },
Self::Inline { worst_width: b, excess_width_from_only_if_block: b_excess_foib },
) => Self::Inline {
worst_width: a.saturating_add(b),
excess_width_from_only_if_block: a_excess_foib.saturating_add(b_excess_foib),
},
(
Self::BlockOrMixed { pre_worst_width, .. },
Self::BlockOrMixed { post_worst_width, .. },
) => Self::BlockOrMixed { pre_worst_width, post_worst_width },
(
Self::BlockOrMixed { pre_worst_width, post_worst_width: post_a },
Self::Inline { worst_width: post_b, excess_width_from_only_if_block: _ },
) => Self::BlockOrMixed {
pre_worst_width,
post_worst_width: post_a.saturating_add(post_b),
},
(
Self::Inline { worst_width: pre_a, excess_width_from_only_if_block: _ },
Self::BlockOrMixed { pre_worst_width: pre_b, post_worst_width },
) => Self::BlockOrMixed {
pre_worst_width: pre_a.saturating_add(pre_b),
post_worst_width,
},
}
}
}
/// Maximum numbers of columns, available to a [`Node`], for both inline layout
/// and block layout (i.e. multi-line with indentation).
///
/// That is, these are the best-case scenarios across all possible choices of
/// inline vs block for all surrounding nodes (up to the root) that admit both
/// cases, and those choices will be made inside-out based on actual widths.
#[derive(Copy, Clone)]
struct MaxWidths {
inline: Columns,
block: Columns,
}
// FIXME(eddyb) make this configurable.
pub(super) const INDENT: &str = " ";
impl Node {
/// Determine the "rigid" component of the [`ApproxLayout`] of this [`Node`].
///
/// That is, this accounts for the parts of the [`Node`] that don't depend on
/// contextual sizing, i.e. [`MaxWidths`] (see also `approx_flex_layout`).
fn approx_rigid_layout(&self) -> ApproxLayout {
// HACK(eddyb) workaround for the `Self::StyledText` arm not being able
// to destructure through the `Box<(_, Cow<str>)>`.
let text_approx_rigid_layout = |styles: &Option<_>, text: &str| {
let styles = styles.as_ref();
if let Some((pre, non_pre)) = text.split_once('\n') {
let (_, post) = non_pre.rsplit_once('\n').unwrap_or(("", non_pre));
ApproxLayout::BlockOrMixed {
pre_worst_width: Columns::maybe_styled_text_width(pre, styles),
post_worst_width: Columns::maybe_styled_text_width(post, styles),
}
} else {
ApproxLayout::Inline {
worst_width: Columns::maybe_styled_text_width(text, styles),
excess_width_from_only_if_block: Columns::ZERO,
}
}
};
#[allow(clippy::match_same_arms)]
match self {
Self::Text(styles, text) => text_approx_rigid_layout(styles, text),
Self::Anchor { is_def: _, anchor: _, text } => text
.iter()
.map(|(styles, text)| text_approx_rigid_layout(styles, text))
.reduce(ApproxLayout::append)
.unwrap_or(ApproxLayout::Inline {
worst_width: Columns::ZERO,
excess_width_from_only_if_block: Columns::ZERO,
}),
Self::IndentedBlock(_) => ApproxLayout::BlockOrMixed {
pre_worst_width: Columns::ZERO,
post_worst_width: Columns::ZERO,
},
Self::BreakingOnlySpace => ApproxLayout::Inline {
worst_width: Columns::text_width(" "),
excess_width_from_only_if_block: Columns::ZERO,
},
Self::ForceLineSeparation => ApproxLayout::BlockOrMixed {
pre_worst_width: Columns::ZERO,
post_worst_width: Columns::ZERO,
},
&Self::IfBlockLayout(text) => {
// Keep the inline `worst_width`, just in case this node is
// going to be used as part of an inline child of a block.
// NOTE(eddyb) this is currently only the case for the trailing
// comma added by `join_comma_sep`.
let text_layout = Self::Text(None, text.into()).approx_rigid_layout();
let worst_width = match text_layout {
ApproxLayout::Inline { worst_width, excess_width_from_only_if_block: _ } => {
worst_width
}
ApproxLayout::BlockOrMixed { .. } => Columns::ZERO,
};
ApproxLayout::Inline { worst_width, excess_width_from_only_if_block: worst_width }
}
// Layout computed only in `approx_flex_layout`.
Self::InlineOrIndentedBlock(_) => ApproxLayout::Inline {
worst_width: Columns::ZERO,
excess_width_from_only_if_block: Columns::ZERO,
},
}
}
/// Determine the "flexible" component of the [`ApproxLayout`] of this [`Node`],
/// potentially making adjustments in order to fit within `max_widths`.
///
/// That is, this accounts for the parts of the [`Node`] that do depend on
/// contextual sizing, i.e. [`MaxWidths`] (see also `approx_rigid_layout`).
fn approx_flex_layout(&mut self, max_widths: MaxWidths) -> ApproxLayout {
match self {
Self::IndentedBlock(fragments) => {
// Apply one more level of indentation to the block layout.
let indented_block_max_width =
max_widths.block.saturating_sub(Columns::text_width(INDENT));
// Recurse on `fragments`, so they can compute their own layouts.
for fragment in &mut fragments[..] {
fragment.approx_layout(MaxWidths {
inline: indented_block_max_width,
block: indented_block_max_width,
});
}
ApproxLayout::BlockOrMixed {
pre_worst_width: Columns::ZERO,
post_worst_width: Columns::ZERO,
}
}
Self::InlineOrIndentedBlock(fragments) => {
// Apply one more level of indentation to the block layout.
let indented_block_max_width =
max_widths.block.saturating_sub(Columns::text_width(INDENT));
// Maximize the inline width available to `fragments`, usually
// increasing it to the maximum allowed by the block layout.
// However, block layout is only needed if the extra width is
// actually used by `fragments` (i.e. staying within the original
// `max_widths.inline` will keep inline layout).
let inner_max_widths = MaxWidths {
inline: max_widths.inline.max(indented_block_max_width),
block: indented_block_max_width,
};
let mut layout = ApproxLayout::Inline {
worst_width: Columns::ZERO,
excess_width_from_only_if_block: Columns::ZERO,
};
for fragment in &mut fragments[..] {
// Offer the same `inner_max_widths` to each `fragment`.
// Worst case, they all remain inline and block layout is
// needed, but even then, `inner_max_widths` has limited each
// `fragment` to a maximum appropriate for that block layout.
layout = layout.append(fragment.approx_layout(inner_max_widths));
}
// *If* we pick the inline layout, it will not end up using *any*
// `Node::OnlyIfBlock`s, so `excess_width_from_only_if_block` can
// be safely subtracted from the "candidate" inline `worst_width`.
let candidate_inline_worst_width = match layout {
ApproxLayout::Inline { worst_width, excess_width_from_only_if_block } => {
Some(worst_width.saturating_sub(excess_width_from_only_if_block))
}
ApproxLayout::BlockOrMixed { .. } => None,
};
let inline_layout = candidate_inline_worst_width
.filter(|&worst_width| worst_width <= max_widths.inline)
.map(|worst_width| ApproxLayout::Inline {
worst_width,
excess_width_from_only_if_block: Columns::ZERO,
});
layout = inline_layout.unwrap_or(
// Even if `layout` is already `ApproxLayout::BlockOrMixed`,
// always reset it to a plain block, with no pre/post widths.
ApproxLayout::BlockOrMixed {
pre_worst_width: Columns::ZERO,
post_worst_width: Columns::ZERO,
},
);
match layout {
ApproxLayout::Inline { .. } => {
// Leave `self` as `Node::InlineOrIndentedBlock` and
// have that be implied to be in inline layout.
}
ApproxLayout::BlockOrMixed { .. } => {
*self = Self::IndentedBlock(mem::take(fragments));
}
}
layout
}
// Layout computed only in `approx_rigid_layout`.
Self::Text(..)
| Self::Anchor { .. }
| Self::BreakingOnlySpace
| Self::ForceLineSeparation
| Self::IfBlockLayout(_) => ApproxLayout::Inline {
worst_width: Columns::ZERO,
excess_width_from_only_if_block: Columns::ZERO,
},
}
}
}
impl Fragment {
/// Determine the [`ApproxLayout`] of this [`Fragment`], potentially making
/// adjustments in order to fit within `max_widths`.
fn approx_layout(&mut self, max_widths: MaxWidths) -> ApproxLayout {
let mut layout = ApproxLayout::Inline {
worst_width: Columns::ZERO,
excess_width_from_only_if_block: Columns::ZERO,
};
let child_max_widths = |layout| MaxWidths {
inline: match layout {
ApproxLayout::Inline { worst_width, excess_width_from_only_if_block: _ } => {
max_widths.inline.saturating_sub(worst_width)
}
ApproxLayout::BlockOrMixed { post_worst_width, .. } => {
max_widths.block.saturating_sub(post_worst_width)
}
},
block: max_widths.block,
};
// Compute rigid `ApproxLayout`s as long as they remain inline, only
// going back for flexible ones on block boundaries (and at the end),
// ensuring that the `MaxWidths` are as contraining as possible.
let mut next_flex_idx = 0;
for rigid_idx in 0..self.nodes.len() {
match self.nodes[rigid_idx].approx_rigid_layout() {
rigid_layout @ ApproxLayout::Inline { .. } => {
layout = layout.append(rigid_layout);
}
ApproxLayout::BlockOrMixed { pre_worst_width, post_worst_width } => {
// Split the `BlockOrMixed` just before the block, and
// process "recent" flexible nodes in between the halves.
layout = layout.append(ApproxLayout::Inline {
worst_width: pre_worst_width,
excess_width_from_only_if_block: Columns::ZERO,
});
// FIXME(eddyb) what happens if the same node has both
// rigid and flexible `ApproxLayout`s?
while next_flex_idx <= rigid_idx {
layout = layout.append(
self.nodes[next_flex_idx].approx_flex_layout(child_max_widths(layout)),
);
next_flex_idx += 1;
}
layout = layout.append(ApproxLayout::BlockOrMixed {
pre_worst_width: Columns::ZERO,
post_worst_width,
});
}
}
}
// Process all remaining flexible nodes (i.e. after the last line split).
for flex_idx in next_flex_idx..self.nodes.len() {
layout =
layout.append(self.nodes[flex_idx].approx_flex_layout(child_max_widths(layout)));
}
layout
}
}
/// Line-oriented operation (i.e. as if lines are stored separately).
///
/// However, a representation that stores lines separately doesn't really exist,
/// and instead [`LineOp`]s are (statefully) transformed into [`TextOp`]s on the fly
/// (see [`LineOp::interpret_with`]).
#[derive(Copy, Clone)]
enum LineOp<'a> {
PushIndent,
PopIndent,
PushStyles(&'a Styles),
PopStyles(&'a Styles),
PushAnchor { is_def: bool, anchor: &'a str },
PopAnchor { is_def: bool, anchor: &'a str },
// HACK(eddyb) `PushAnchor`+`PopAnchor`, indicating no visible text is needed
// (i.e. this is only for helper anchors, which only need vertical positioning).
EmptyAnchor { is_def: bool, anchor: &'a str },
AppendToLine(&'a str),
StartNewLine,
BreakIfWithinLine(Break),
}
#[derive(Copy, Clone)]
enum Break {
Space,
NewLine,
}
impl Node {
/// Flatten the [`Node`] to [`LineOp`]s.
fn render_to_line_ops(
&self,
directly_in_block: bool,
) -> impl InternalIterator<Item = LineOp<'_>> {
// FIXME(eddyb) a better helper for this may require type-generic closures.
struct RenderToLineOps<'a>(&'a Node, bool);
impl<'a> InternalIterator for RenderToLineOps<'a> {
type Item = LineOp<'a>;
fn try_for_each<T, F>(self, mut f: F) -> ControlFlow<T>
where
F: FnMut(LineOp<'a>) -> ControlFlow<T>,
{
// HACK(eddyb) this is terrible but the `internal_iterator`
// library uses `F` instead of `&mut F` which means it has to
// add an extra `&mut` for every `flat_map` level, causing
// polymorphic recursion...
let f = &mut f as &mut dyn FnMut(_) -> _;
self.0.render_to_line_ops_try_for_each_helper(self.1, f)
}
}
RenderToLineOps(self, directly_in_block)
}
// HACK(eddyb) helper for `render_to_line_ops` returning a `InternalIterator`.
fn render_to_line_ops_try_for_each_helper<'a, T>(
&'a self,
directly_in_block: bool,
mut each_line_op: impl FnMut(LineOp<'a>) -> ControlFlow<T>,
) -> ControlFlow<T> {
let text_render_to_line_ops = |styles: &'a Option<Styles>, text: &'a str| {
let styles = styles.as_ref();
let mut lines = text.split('\n');
styles
.map(LineOp::PushStyles)
.into_internal_iter()
.chain([LineOp::AppendToLine(lines.next().unwrap())])
.chain(
lines
.into_internal()
.flat_map(|line| [LineOp::StartNewLine, LineOp::AppendToLine(line)]),
)
.chain(styles.map(LineOp::PopStyles))
};
match self {
Self::Text(styles, text) => {
text_render_to_line_ops(styles, text).try_for_each(each_line_op)?;
}
&Self::Anchor { is_def, ref anchor, ref text } => {
if text.is_empty() {
each_line_op(LineOp::EmptyAnchor { is_def, anchor })?;
} else {
[LineOp::PushAnchor { is_def, anchor }]
.into_internal_iter()
.chain(
text.into_internal_iter()
.flat_map(|(styles, text)| text_render_to_line_ops(styles, text)),
)
.chain([LineOp::PopAnchor { is_def, anchor }])
.try_for_each(each_line_op)?;
}
}
Self::IndentedBlock(fragments) => {
[LineOp::PushIndent, LineOp::BreakIfWithinLine(Break::NewLine)]
.into_internal_iter()
.chain(fragments.into_internal_iter().flat_map(|fragment| {
fragment
.render_to_line_ops(true)
.chain([LineOp::BreakIfWithinLine(Break::NewLine)])
}))
.chain([LineOp::PopIndent])
.try_for_each(each_line_op)?;
}
// Post-layout, this is only used for the inline layout.
Self::InlineOrIndentedBlock(fragments) => {
fragments
.into_internal_iter()
.flat_map(|fragment| fragment.render_to_line_ops(false))
.try_for_each(each_line_op)?;
}
Self::BreakingOnlySpace => each_line_op(LineOp::BreakIfWithinLine(Break::Space))?,
Self::ForceLineSeparation => each_line_op(LineOp::BreakIfWithinLine(Break::NewLine))?,
&Self::IfBlockLayout(text) => {
if directly_in_block {
text_render_to_line_ops(&None, text).try_for_each(each_line_op)?;
}
}
}
ControlFlow::Continue(())
}
}
impl Fragment {
/// Flatten the [`Fragment`] to [`LineOp`]s.
fn render_to_line_ops(
&self,
directly_in_block: bool,
) -> impl InternalIterator<Item = LineOp<'_>> {
self.nodes
.iter()
.into_internal()
.flat_map(move |node| node.render_to_line_ops(directly_in_block))
}
/// Flatten the [`Fragment`] to [`TextOp`]s.
fn render_to_text_ops(&self) -> impl InternalIterator<Item = TextOp<'_>> {
LineOp::interpret(self.render_to_line_ops(false))
}
}
/// Text-oriented operation (plain text snippets interleaved with style/anchor push/pop).
#[derive(Copy, Clone, PartialEq)]
pub(super) enum TextOp<'a> {
PushStyles(&'a Styles),
PopStyles(&'a Styles),
PushAnchor { is_def: bool, anchor: &'a str },
PopAnchor { is_def: bool, anchor: &'a str },
Text(&'a str),
}
impl<'a> LineOp<'a> {
/// Expand [`LineOp`]s to [`TextOp`]s.
fn interpret(
line_ops: impl InternalIterator<Item = LineOp<'a>>,
) -> impl InternalIterator<Item = TextOp<'a>> {
// FIXME(eddyb) a better helper for this may require type-generic closures.
struct Interpret<I>(I);
impl<'a, I: InternalIterator<Item = LineOp<'a>>> InternalIterator for Interpret<I> {
type Item = TextOp<'a>;
fn try_for_each<T, F>(self, f: F) -> ControlFlow<T>
where
F: FnMut(TextOp<'a>) -> ControlFlow<T>,
{
LineOp::interpret_try_for_each_helper(self.0, f)
}
}
Interpret(line_ops)
}
// HACK(eddyb) helper for `interpret` returning a `InternalIterator`.
fn interpret_try_for_each_helper<T>(
line_ops: impl InternalIterator<Item = LineOp<'a>>,
mut each_text_op: impl FnMut(TextOp<'a>) -> ControlFlow<T>,
) -> ControlFlow<T> {
let mut indent = 0;
enum LineState {
/// This line was just started, lacking any text.
///
/// The first (non-empty) `LineOp::AppendToLine` on that line, or
/// `LineOp::{Push,Pop}{Styles,Anchor}`, needs to materialize
/// `indent` levels of indentation (before emitting its `TextOp`s).
//
// NOTE(eddyb) indentation is not immediatelly materialized in order
// to avoid trailing whitespace on otherwise-empty lines.
Empty,
/// This line has `indent_so_far` levels of indentation, and may have
/// styling applied to it, but lacks any other text.
///
/// Only used by `LineOp::EmptyAnchor` (i.e. helper anchors),
/// to avoid them adding trailing-whitespace-only lines.
//
// NOTE(eddyb) the new line is started by `EmptyAnchor` so that
// there remains separation with the previous (unrelated) line,
// whereas the following lines are very likely related to the
// helper anchor (but if that changes, this would need to be fixed).
// HACK(eddyb) `EmptyAnchor` uses `indent_so_far: 0` to
// allow lower-indentation text to follow on the same line.
OnlyIndentedOrAnchored { indent_so_far: usize },
/// This line has had text emitted (other than indentation).
HasText,
}
let mut line_state = LineState::Empty;
// Deferred `LineOp::BreakIfWithinLine`, which will be materialized
// only between two consecutive `LineOp::AppendToLine { text, .. }`
// (with non-empty `text`), that (would) share the same line.
let mut pending_break_if_within_line = None;
line_ops.try_for_each(move |op| {
// Do not allow (accidental) side-effects from no-op `op`s.
if let LineOp::AppendToLine("") = op {
return ControlFlow::Continue(());
}
if let LineOp::AppendToLine(_)
| LineOp::PushStyles(_)
| LineOp::PopStyles(_)
| LineOp::PushAnchor { .. }
| LineOp::PopAnchor { .. }
| LineOp::EmptyAnchor { .. } = op
{
if let Some(br) = pending_break_if_within_line.take() {
each_text_op(TextOp::Text(match br {
Break::Space => " ",
Break::NewLine => "\n",
}))?;
if matches!(br, Break::NewLine) {
line_state = LineState::Empty;
}
}
let target_indent = match line_state {
// HACK(eddyb) `EmptyAnchor` uses `indent_so_far: 0` to
// allow lower-indentation text to follow on the same line.
LineState::Empty | LineState::OnlyIndentedOrAnchored { indent_so_far: 0 }
if matches!(op, LineOp::EmptyAnchor { .. }) =>
{
Some(0)
}
LineState::Empty | LineState::OnlyIndentedOrAnchored { .. } => Some(indent),
LineState::HasText => None,
};
if let Some(target_indent) = target_indent {
let indent_so_far = match line_state {
LineState::Empty => 0,
// FIXME(eddyb) `EmptyAnchor` doesn't need this, so this
// is perhaps unnecessarily over-engineered? (see above)
LineState::OnlyIndentedOrAnchored { indent_so_far } => {
// Disallow reusing lines already indented too much.
if indent_so_far > target_indent {
each_text_op(TextOp::Text("\n"))?;
line_state = LineState::Empty;
0
} else {
indent_so_far
}
}
LineState::HasText => unreachable!(),
};
for _ in indent_so_far..target_indent {
each_text_op(TextOp::Text(INDENT))?;
}
line_state = LineState::OnlyIndentedOrAnchored { indent_so_far: target_indent };
}
}
match op {
LineOp::PushIndent => {
indent += 1;
}
LineOp::PopIndent => {
assert!(indent > 0);
indent -= 1;
}
LineOp::PushStyles(styles) => each_text_op(TextOp::PushStyles(styles))?,
LineOp::PopStyles(styles) => each_text_op(TextOp::PopStyles(styles))?,
LineOp::PushAnchor { is_def, anchor } => {
each_text_op(TextOp::PushAnchor { is_def, anchor })?;
}
LineOp::PopAnchor { is_def, anchor } => {
each_text_op(TextOp::PopAnchor { is_def, anchor })?;
}
LineOp::EmptyAnchor { is_def, anchor } => {
each_text_op(TextOp::PushAnchor { is_def, anchor })?;
each_text_op(TextOp::PopAnchor { is_def, anchor })?;
}
LineOp::AppendToLine(text) => {
each_text_op(TextOp::Text(text))?;
line_state = LineState::HasText;
}
LineOp::StartNewLine => {
each_text_op(TextOp::Text("\n"))?;
line_state = LineState::Empty;
pending_break_if_within_line = None;
}
LineOp::BreakIfWithinLine(br) => {
let elide = match line_state {
LineState::Empty => true,
LineState::OnlyIndentedOrAnchored { indent_so_far } => {
indent_so_far <= indent
}
LineState::HasText => false,
};
if !elide {
// Merge two pending `Break`s if necessary,
// preferring newlines over spaces.
let br = match (pending_break_if_within_line, br) {
(Some(Break::NewLine), _) | (_, Break::NewLine) => Break::NewLine,
(None | Some(Break::Space), Break::Space) => Break::Space,
};
pending_break_if_within_line = Some(br);
}
}
}
ControlFlow::Continue(())
})
}
}
// Pretty fragment "constructors".
//
// FIXME(eddyb) should these be methods on `Node`/`Fragment`?
/// Constructs the [`Fragment`] corresponding to one of:
/// * inline layout: `header + " " + contents.join(" ")`
/// * block layout: `header + "\n" + indent(contents).join("\n")`
pub fn join_space(
header: impl Into<Node>,
contents: impl IntoIterator<Item = impl Into<Fragment>>,
) -> Fragment {
Fragment::new([
header.into(),
Node::InlineOrIndentedBlock(
contents
.into_iter()
.map(|entry| {
Fragment::new(iter::once(Node::BreakingOnlySpace).chain(entry.into().nodes))
})
.collect(),
),
])
}
/// Constructs the [`Fragment`] corresponding to one of:
/// * inline layout: `prefix + contents.join(", ") + suffix`
/// * block layout: `prefix + "\n" + indent(contents).join(",\n") + ",\n" + suffix`
pub fn join_comma_sep(
prefix: impl Into<Node>,
contents: impl IntoIterator<Item = impl Into<Fragment>>,
suffix: impl Into<Node>,
) -> Fragment {
let mut children: Vec<_> = contents.into_iter().map(Into::into).collect();
if let Some((last_child, non_last_children)) = children.split_last_mut() {
for non_last_child in non_last_children {
non_last_child.nodes.extend([",".into(), Node::BreakingOnlySpace]);
}
// Trailing comma is only needed after the very last element.
last_child.nodes.push(Node::IfBlockLayout(","));
}
Fragment::new([prefix.into(), Node::InlineOrIndentedBlock(children), suffix.into()])
}