From f547c97072881069417acd3b79b08fb7ecf40ba2 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 31 Oct 2022 15:47:49 +0100 Subject: [PATCH] Reduce usage of `finalize` --- src/library/math/mod.rs | 15 ++++++--------- src/library/text/link.rs | 26 ++++++++++++++------------ src/library/text/raw.rs | 10 ++++++---- src/model/show.rs | 1 - src/model/vm.rs | 4 ++-- tests/typ/style/show-node.typ | 2 +- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/library/math/mod.rs b/src/library/math/mod.rs index b23ed7cfe..63d5f4195 100644 --- a/src/library/math/mod.rs +++ b/src/library/math/mod.rs @@ -103,16 +103,13 @@ impl Show for MathNode { &self, _: Tracked, styles: StyleChain, - mut realized: Content, + realized: Content, ) -> SourceResult { - let mut map = StyleMap::new(); - map.set_family(styles.get(Self::FAMILY).clone(), styles); - - if self.display() { - realized = realized.spaced(styles.get(Self::ABOVE), styles.get(Self::BELOW)); - } - - Ok(realized.styled_with_map(map)) + Ok(if self.display() { + realized.spaced(styles.get(Self::ABOVE), styles.get(Self::BELOW)) + } else { + realized + }) } } diff --git a/src/library/text/link.rs b/src/library/text/link.rs index 98a867a89..11d559562 100644 --- a/src/library/text/link.rs +++ b/src/library/text/link.rs @@ -73,17 +73,21 @@ impl Show for LinkNode { } fn realize(&self, _: Tracked, _: StyleChain) -> SourceResult { - Ok(self.body.clone().unwrap_or_else(|| match &self.dest { - Destination::Url(url) => { - let mut text = url.as_str(); - for prefix in ["mailto:", "tel:"] { - text = text.trim_start_matches(prefix); + Ok(self + .body + .clone() + .unwrap_or_else(|| match &self.dest { + Destination::Url(url) => { + let mut text = url.as_str(); + for prefix in ["mailto:", "tel:"] { + text = text.trim_start_matches(prefix); + } + let shorter = text.len() < url.len(); + Content::Text(if shorter { text.into() } else { url.clone() }) } - let shorter = text.len() < url.len(); - Content::Text(if shorter { text.into() } else { url.clone() }) - } - Destination::Internal(_) => Content::Empty, - })) + Destination::Internal(_) => Content::Empty, + }) + .styled(TextNode::LINK, Some(self.dest.clone()))) } fn finalize( @@ -93,8 +97,6 @@ impl Show for LinkNode { mut realized: Content, ) -> SourceResult { let mut map = StyleMap::new(); - map.set(TextNode::LINK, Some(self.dest.clone())); - if let Smart::Custom(fill) = styles.get(Self::FILL) { map.set(TextNode::FILL, fill); } diff --git a/src/library/text/raw.rs b/src/library/text/raw.rs index 6d19deff5..85e8133cd 100644 --- a/src/library/text/raw.rs +++ b/src/library/text/raw.rs @@ -105,7 +105,12 @@ impl Show for RawNode { realized = Content::block(realized); } - Ok(realized) + let mut map = StyleMap::new(); + map.set(TextNode::OVERHANG, false); + map.set(TextNode::HYPHENATE, Smart::Custom(Hyphenate(false))); + map.set(TextNode::SMART_QUOTES, false); + + Ok(realized.styled_with_map(map)) } fn finalize( @@ -116,9 +121,6 @@ impl Show for RawNode { ) -> SourceResult { let mut map = StyleMap::new(); map.set_family(styles.get(Self::FAMILY).clone(), styles); - map.set(TextNode::OVERHANG, false); - map.set(TextNode::HYPHENATE, Smart::Custom(Hyphenate(false))); - map.set(TextNode::SMART_QUOTES, false); if self.block { realized = realized.spaced(styles.get(Self::ABOVE), styles.get(Self::BELOW)); diff --git a/src/model/show.rs b/src/model/show.rs index 86659463e..cc84e6db8 100644 --- a/src/model/show.rs +++ b/src/model/show.rs @@ -28,7 +28,6 @@ pub trait Show: 'static { /// this for effects that should work even in the face of a user-defined /// show rule, for example: /// - Application of general settable properties - /// - Attaching things like semantics to a heading /// /// Defaults to just the realized content. #[allow(unused_variables)] diff --git a/src/model/vm.rs b/src/model/vm.rs index 614e2a9f3..6207ffd5e 100644 --- a/src/model/vm.rs +++ b/src/model/vm.rs @@ -12,7 +12,7 @@ use crate::{LangItems, World}; pub struct Vm<'a> { /// The core context. pub world: Tracked<'a, dyn World>, - /// The route of source ids the machine took to reach its current location. + /// The route of source ids the VM took to reach its current location. pub route: Tracked<'a, Route>, /// The current location. pub location: Option, @@ -52,7 +52,7 @@ impl<'a> Vm<'a> { } } - return Err("cannot access file system from here".into()); + Err("cannot access file system from here".into()) } /// The language items. diff --git a/tests/typ/style/show-node.typ b/tests/typ/style/show-node.typ index 33895addd..b35ab4c49 100644 --- a/tests/typ/style/show-node.typ +++ b/tests/typ/style/show-node.typ @@ -23,7 +23,7 @@ A [= Heading] C #set heading(around: none) Where is -= There are not headings around here! += There are no headings around here! my heading? ---