Better debug representation for template

This commit is contained in:
Laurenz 2022-02-03 14:27:20 +01:00
parent 20a1fd8bc7
commit f9d3802492
2 changed files with 57 additions and 25 deletions

View File

@ -7,7 +7,6 @@ use std::ops::{Add, AddAssign};
use super::{Property, StyleMap, Styled}; use super::{Property, StyleMap, Styled};
use crate::diag::StrResult; use crate::diag::StrResult;
use crate::geom::SpecAxis;
use crate::layout::{Layout, PackedNode}; use crate::layout::{Layout, PackedNode};
use crate::library::prelude::*; use crate::library::prelude::*;
use crate::library::{ use crate::library::{
@ -41,26 +40,28 @@ use crate::Context;
/// since we can just recurse into the nested sequences. Also, in theory, /// since we can just recurse into the nested sequences. Also, in theory,
/// this allows better complexity when adding large sequence nodes just like /// this allows better complexity when adding large sequence nodes just like
/// for something like a text rope. /// for something like a text rope.
#[derive(Debug, PartialEq, Clone, Hash)] #[derive(PartialEq, Clone, Hash)]
pub enum Template { pub enum Template {
/// A word space. /// A word space.
Space, Space,
/// A line break. /// A line break.
Linebreak, Linebreak,
/// Horizontal spacing.
Horizontal(SpacingKind),
/// Plain text.
Text(EcoString),
/// An inline node.
Inline(PackedNode),
/// A paragraph break. /// A paragraph break.
Parbreak, Parbreak,
/// Vertical spacing.
Vertical(SpacingKind),
/// A block node.
Block(PackedNode),
/// A column break. /// A column break.
Colbreak, Colbreak,
/// A page break. /// A page break.
Pagebreak, Pagebreak,
/// Plain text.
Text(EcoString),
/// Spacing.
Spacing(SpecAxis, SpacingKind),
/// An inline node.
Inline(PackedNode),
/// A block node.
Block(PackedNode),
/// A page node. /// A page node.
Page(PageNode), Page(PageNode),
/// A template with attached styles. /// A template with attached styles.
@ -145,25 +146,56 @@ impl Default for Template {
} }
} }
impl Debug for Template {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Space => f.pad("Space"),
Self::Linebreak => f.pad("Linebreak"),
Self::Horizontal(kind) => write!(f, "Horizontal({kind:?})"),
Self::Text(text) => write!(f, "Text({text:?})"),
Self::Inline(node) => {
f.write_str("Inline(")?;
node.fmt(f)?;
f.write_str(")")
}
Self::Parbreak => f.pad("Parbreak"),
Self::Vertical(kind) => write!(f, "Vertical({kind:?})"),
Self::Block(node) => {
f.write_str("Block(")?;
node.fmt(f)?;
f.write_str(")")
}
Self::Colbreak => f.pad("Colbreak"),
Self::Pagebreak => f.pad("Pagebreak"),
Self::Page(page) => page.fmt(f),
Self::Styled(sub, map) => {
map.fmt(f)?;
sub.fmt(f)
}
Self::Sequence(seq) => f.debug_list().entries(seq).finish(),
}
}
}
impl Add for Template { impl Add for Template {
type Output = Self; type Output = Self;
fn add(self, rhs: Self) -> Self::Output { fn add(self, rhs: Self) -> Self::Output {
Self::Sequence(match (self, rhs) { Self::Sequence(match (self, rhs) {
(Self::Sequence(mut left), Self::Sequence(right)) => { (Self::Sequence(mut lhs), Self::Sequence(rhs)) => {
left.extend(right); lhs.extend(rhs);
left lhs
} }
(Self::Sequence(mut left), right) => { (Self::Sequence(mut lhs), rhs) => {
left.push(right); lhs.push(rhs);
left lhs
} }
(left, Self::Sequence(mut right)) => { (lhs, Self::Sequence(mut rhs)) => {
right.insert(0, left); rhs.insert(0, lhs);
right rhs
} }
(left, right) => { (lhs, rhs) => {
vec![left, right] vec![lhs, rhs]
} }
}) })
} }
@ -276,14 +308,14 @@ impl Packer {
Template::Text(text) => { Template::Text(text) => {
self.push_inline(Styled::new(ParChild::text(text), styles)); self.push_inline(Styled::new(ParChild::text(text), styles));
} }
Template::Spacing(SpecAxis::Horizontal, kind) => { Template::Horizontal(kind) => {
// Just like a line break, explicit horizontal spacing eats up // Just like a line break, explicit horizontal spacing eats up
// surrounding text spaces. // surrounding text spaces.
self.par.last.hard(); self.par.last.hard();
self.push_inline(Styled::new(ParChild::Spacing(kind), styles)); self.push_inline(Styled::new(ParChild::Spacing(kind), styles));
self.par.last.hard(); self.par.last.hard();
} }
Template::Spacing(SpecAxis::Vertical, kind) => { Template::Vertical(kind) => {
// Explicit vertical spacing ends the current paragraph and then // Explicit vertical spacing ends the current paragraph and then
// discards the paragraph break. // discards the paragraph break.
self.parbreak(None, false); self.parbreak(None, false);

View File

@ -8,7 +8,7 @@ pub struct HNode;
#[class] #[class]
impl HNode { impl HNode {
fn construct(_: &mut EvalContext, args: &mut Args) -> TypResult<Template> { fn construct(_: &mut EvalContext, args: &mut Args) -> TypResult<Template> {
Ok(Template::Spacing(SpecAxis::Horizontal, args.expect("spacing")?)) Ok(Template::Horizontal(args.expect("spacing")?))
} }
} }
@ -18,7 +18,7 @@ pub struct VNode;
#[class] #[class]
impl VNode { impl VNode {
fn construct(_: &mut EvalContext, args: &mut Args) -> TypResult<Template> { fn construct(_: &mut EvalContext, args: &mut Args) -> TypResult<Template> {
Ok(Template::Spacing(SpecAxis::Vertical, args.expect("spacing")?)) Ok(Template::Vertical(args.expect("spacing")?))
} }
} }