mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Rename into_block
to pack
This commit is contained in:
parent
88e50a55af
commit
c5e05ac0ea
@ -35,7 +35,6 @@ use once_cell::sync::Lazy;
|
|||||||
use syntect::easy::HighlightLines;
|
use syntect::easy::HighlightLines;
|
||||||
use syntect::highlighting::{FontStyle, Highlighter, Style as SynStyle, Theme, ThemeSet};
|
use syntect::highlighting::{FontStyle, Highlighter, Style as SynStyle, Theme, ThemeSet};
|
||||||
use syntect::parsing::SyntaxSet;
|
use syntect::parsing::SyntaxSet;
|
||||||
|
|
||||||
use unicode_segmentation::UnicodeSegmentation;
|
use unicode_segmentation::UnicodeSegmentation;
|
||||||
|
|
||||||
use crate::diag::{At, Error, StrResult, Trace, Tracepoint, TypResult};
|
use crate::diag::{At, Error, StrResult, Trace, Tracepoint, TypResult};
|
||||||
@ -255,11 +254,7 @@ impl Eval for RawNode {
|
|||||||
|
|
||||||
fn eval(&self, _: &mut EvalContext) -> TypResult<Self::Output> {
|
fn eval(&self, _: &mut EvalContext) -> TypResult<Self::Output> {
|
||||||
let code = self.highlighted();
|
let code = self.highlighted();
|
||||||
Ok(if self.block {
|
Ok(if self.block { Template::Block(code.pack()) } else { code })
|
||||||
Template::Block(code.into_block())
|
|
||||||
} else {
|
|
||||||
code
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -352,7 +347,7 @@ impl Eval for MathNode {
|
|||||||
fn eval(&self, _: &mut EvalContext) -> TypResult<Self::Output> {
|
fn eval(&self, _: &mut EvalContext) -> TypResult<Self::Output> {
|
||||||
let text = Template::Text(self.formula.trim().into()).monospaced();
|
let text = Template::Text(self.formula.trim().into()).monospaced();
|
||||||
Ok(if self.display {
|
Ok(if self.display {
|
||||||
Template::Block(text.into_block())
|
Template::Block(text.pack())
|
||||||
} else {
|
} else {
|
||||||
text
|
text
|
||||||
})
|
})
|
||||||
@ -364,7 +359,7 @@ impl Eval for HeadingNode {
|
|||||||
|
|
||||||
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
|
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
|
||||||
Ok(Template::block(library::HeadingNode {
|
Ok(Template::block(library::HeadingNode {
|
||||||
child: self.body().eval(ctx)?.into_block(),
|
child: self.body().eval(ctx)?.pack(),
|
||||||
level: self.level(),
|
level: self.level(),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@ -375,7 +370,7 @@ impl Eval for ListNode {
|
|||||||
|
|
||||||
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
|
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
|
||||||
Ok(Template::block(library::ListNode {
|
Ok(Template::block(library::ListNode {
|
||||||
child: self.body().eval(ctx)?.into_block(),
|
child: self.body().eval(ctx)?.pack(),
|
||||||
kind: library::Unordered,
|
kind: library::Unordered,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@ -386,7 +381,7 @@ impl Eval for EnumNode {
|
|||||||
|
|
||||||
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
|
fn eval(&self, ctx: &mut EvalContext) -> TypResult<Self::Output> {
|
||||||
Ok(Template::block(library::ListNode {
|
Ok(Template::block(library::ListNode {
|
||||||
child: self.body().eval(ctx)?.into_block(),
|
child: self.body().eval(ctx)?.pack(),
|
||||||
kind: library::Ordered(self.number()),
|
kind: library::Ordered(self.number()),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
@ -27,18 +27,18 @@ use crate::util::EcoString;
|
|||||||
/// represented as a `Styled(Text("Hello"), [TextNode::STRONG: true])`
|
/// represented as a `Styled(Text("Hello"), [TextNode::STRONG: true])`
|
||||||
/// template.
|
/// template.
|
||||||
///
|
///
|
||||||
/// 2. A `Sequence` template simply combines multiple templates and will be
|
/// 2. A `Sequence` template combines multiple arbitrary templates and is the
|
||||||
/// layouted as a [flow](FlowNode). So, when you write `[Hi] + [you]` in
|
/// representation of a "flow" of content. So, when you write `[Hi] + [you]`
|
||||||
/// Typst, this type's [`Add`] implementation is invoked and the two
|
/// in Typst, this type's [`Add`] implementation is invoked and the two
|
||||||
/// templates are combined into a single [`Sequence`](Self::Sequence)
|
/// [`Text`](Self::Text) templates are combined into a single
|
||||||
/// template.
|
/// [`Sequence`](Self::Sequence) template.
|
||||||
///
|
///
|
||||||
/// A sequence may contain nested sequences (meaning this variant effectively
|
/// A sequence may contain nested sequences (meaning this variant effectively
|
||||||
/// allows nodes to form trees). All nested sequences can equivalently be
|
/// allows templates to form trees). All nested sequences can equivalently be
|
||||||
/// represented as a single flat sequence, but allowing nesting doesn't hurt
|
/// represented as a single flat sequence, but allowing nesting doesn't hurt
|
||||||
/// since we can just recurse into the nested sequences during packing. Also,
|
/// since we can just recurse into the nested sequences. Also, in theory,
|
||||||
/// in theory, this allows better complexity when adding (large) sequence
|
/// this allows better complexity when adding large sequence nodes just like
|
||||||
/// nodes (just like for a text rope).
|
/// for something like a text rope.
|
||||||
#[derive(Debug, PartialEq, Clone, Hash)]
|
#[derive(Debug, PartialEq, Clone, Hash)]
|
||||||
pub enum Template {
|
pub enum Template {
|
||||||
/// A word space.
|
/// A word space.
|
||||||
@ -116,8 +116,16 @@ impl Template {
|
|||||||
self.styled(TextNode::MONOSPACE, true)
|
self.styled(TextNode::MONOSPACE, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lift to a type-erased block-level template.
|
/// Repeat this template `n` times.
|
||||||
pub fn into_block(self) -> PackedNode {
|
pub fn repeat(&self, n: i64) -> StrResult<Self> {
|
||||||
|
let count = usize::try_from(n)
|
||||||
|
.map_err(|_| format!("cannot repeat this template {} times", n))?;
|
||||||
|
|
||||||
|
Ok(Self::Sequence(vec![self.clone(); count]))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convert to a type-erased block-level node.
|
||||||
|
pub fn pack(self) -> PackedNode {
|
||||||
if let Template::Block(packed) = self {
|
if let Template::Block(packed) = self {
|
||||||
packed
|
packed
|
||||||
} else {
|
} else {
|
||||||
@ -133,14 +141,6 @@ impl Template {
|
|||||||
packer.walk(self, StyleMap::new());
|
packer.walk(self, StyleMap::new());
|
||||||
packer.into_root()
|
packer.into_root()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Repeat this template `n` times.
|
|
||||||
pub fn repeat(&self, n: i64) -> StrResult<Self> {
|
|
||||||
let count = usize::try_from(n)
|
|
||||||
.map_err(|_| format!("cannot repeat this template {} times", n))?;
|
|
||||||
|
|
||||||
Ok(Self::Sequence(vec![self.clone(); count]))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Template {
|
impl Default for Template {
|
||||||
|
@ -267,6 +267,8 @@ impl Debug for PackedNode {
|
|||||||
|
|
||||||
impl PartialEq for PackedNode {
|
impl PartialEq for PackedNode {
|
||||||
fn eq(&self, other: &Self) -> bool {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
// We cast to thin pointers for comparison because we don't want to
|
||||||
|
// compare vtables (which can be different across codegen units).
|
||||||
std::ptr::eq(
|
std::ptr::eq(
|
||||||
Arc::as_ptr(&self.node) as *const (),
|
Arc::as_ptr(&self.node) as *const (),
|
||||||
Arc::as_ptr(&other.node) as *const (),
|
Arc::as_ptr(&other.node) as *const (),
|
||||||
|
@ -54,7 +54,7 @@ impl<L: ListKind> Layout for ListNode<L> {
|
|||||||
gutter: Spec::default(),
|
gutter: Spec::default(),
|
||||||
children: vec![
|
children: vec![
|
||||||
PackedNode::default(),
|
PackedNode::default(),
|
||||||
Template::Text(self.kind.label()).into_block(),
|
Template::Text(self.kind.label()).pack(),
|
||||||
PackedNode::default(),
|
PackedNode::default(),
|
||||||
self.child.clone(),
|
self.child.clone(),
|
||||||
],
|
],
|
||||||
|
@ -228,5 +228,5 @@ castable! {
|
|||||||
castable! {
|
castable! {
|
||||||
PackedNode,
|
PackedNode,
|
||||||
Expected: "template",
|
Expected: "template",
|
||||||
Value::Template(template) => template.into_block(),
|
Value::Template(template) => template.pack(),
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,7 @@ castable! {
|
|||||||
Value::Relative(v) => Self::Spacing(SpacingKind::Linear(v.into())),
|
Value::Relative(v) => Self::Spacing(SpacingKind::Linear(v.into())),
|
||||||
Value::Linear(v) => Self::Spacing(SpacingKind::Linear(v)),
|
Value::Linear(v) => Self::Spacing(SpacingKind::Linear(v)),
|
||||||
Value::Fractional(v) => Self::Spacing(SpacingKind::Fractional(v)),
|
Value::Fractional(v) => Self::Spacing(SpacingKind::Fractional(v)),
|
||||||
Value::Template(v) => Self::Node(v.into_block()),
|
Value::Template(v) => Self::Node(v.pack()),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Performs stack layout.
|
/// Performs stack layout.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user