diff options
| author | Malo <57839069+MDLC01@users.noreply.github.com> | 2024-12-16 15:10:42 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-16 14:10:42 +0000 |
| commit | 8b1e0d3a233950bd8fd553e118ec6342efb42855 (patch) | |
| tree | 4eca78ecdc1e16aebd8c02cf1411e8680c79e59c /crates | |
| parent | 1b10d19d76e2ddf09a63d00a6fc56556d2bbfe08 (diff) | |
Improve `symbol` `repr` (#5505)
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/typst-library/src/foundations/symbol.rs | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/crates/typst-library/src/foundations/symbol.rs b/crates/typst-library/src/foundations/symbol.rs index fcb3a3ce..88030528 100644 --- a/crates/typst-library/src/foundations/symbol.rs +++ b/crates/typst-library/src/foundations/symbol.rs @@ -246,10 +246,50 @@ impl Debug for List { impl crate::foundations::Repr for Symbol { fn repr(&self) -> EcoString { - eco_format!("\"{}\"", self.get()) + match &self.0 { + Repr::Single(c) => eco_format!("symbol(\"{}\")", *c), + Repr::Complex(variants) => { + eco_format!("symbol{}", repr_variants(variants.iter().copied(), "")) + } + Repr::Modified(arc) => { + let (list, modifiers) = arc.as_ref(); + if modifiers.is_empty() { + eco_format!("symbol{}", repr_variants(list.variants(), "")) + } else { + eco_format!("symbol{}", repr_variants(list.variants(), modifiers)) + } + } + } } } +fn repr_variants<'a>( + variants: impl Iterator<Item = (&'a str, char)>, + applied_modifiers: &str, +) -> String { + crate::foundations::repr::pretty_array_like( + &variants + .filter(|(variant, _)| { + // Only keep variants that can still be accessed, i.e., variants + // that contain all applied modifiers. + parts(applied_modifiers).all(|am| variant.split('.').any(|m| m == am)) + }) + .map(|(variant, c)| { + let trimmed_variant = variant + .split('.') + .filter(|&m| parts(applied_modifiers).all(|am| m != am)); + if trimmed_variant.clone().all(|m| m.is_empty()) { + eco_format!("\"{c}\"") + } else { + let trimmed_modifiers = trimmed_variant.collect::<Vec<_>>().join("."); + eco_format!("(\"{}\", \"{}\")", trimmed_modifiers, c) + } + }) + .collect::<Vec<_>>(), + false, + ) +} + impl Serialize for Symbol { fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where |
