summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMalo <57839069+MDLC01@users.noreply.github.com>2024-12-16 15:10:42 +0100
committerGitHub <noreply@github.com>2024-12-16 14:10:42 +0000
commit8b1e0d3a233950bd8fd553e118ec6342efb42855 (patch)
tree4eca78ecdc1e16aebd8c02cf1411e8680c79e59c
parent1b10d19d76e2ddf09a63d00a6fc56556d2bbfe08 (diff)
Improve `symbol` `repr` (#5505)
-rw-r--r--crates/typst-library/src/foundations/symbol.rs42
-rw-r--r--tests/suite/symbols/symbol.typ62
2 files changed, 103 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
diff --git a/tests/suite/symbols/symbol.typ b/tests/suite/symbols/symbol.typ
index 30d87e44..f2f6abf8 100644
--- a/tests/suite/symbols/symbol.typ
+++ b/tests/suite/symbols/symbol.typ
@@ -49,3 +49,65 @@
--- symbol-unknown-modifier ---
// Error: 13-20 unknown symbol modifier
#emoji.face.garbage
+
+--- symbol-repr ---
+#test(
+ repr(sym.amp),
+ `symbol("&", ("inv", "⅋"))`.text,
+)
+#test(
+ repr(sym.amp.inv),
+ `symbol("⅋")`.text,
+)
+#test(
+ repr(sym.arrow.double.r),
+ ```
+ symbol(
+ "⇒",
+ ("bar", "⤇"),
+ ("long", "⟹"),
+ ("long.bar", "⟾"),
+ ("not", "⇏"),
+ ("l", "⇔"),
+ ("l.long", "⟺"),
+ ("l.not", "⇎"),
+ )
+ ```.text,
+)
+#test(repr(sym.smash), "symbol(\"⨳\")")
+
+#let envelope = symbol(
+ "🖂",
+ ("stamped", "🖃"),
+ ("stamped.pen", "🖆"),
+ ("lightning", "🖄"),
+ ("fly", "🖅"),
+)
+#test(
+ repr(envelope),
+ ```
+ symbol(
+ "🖂",
+ ("stamped", "🖃"),
+ ("stamped.pen", "🖆"),
+ ("lightning", "🖄"),
+ ("fly", "🖅"),
+ )
+ ```.text,
+)
+#test(
+ repr(envelope.stamped),
+ `symbol("🖃", ("pen", "🖆"))`.text,
+)
+#test(
+ repr(envelope.stamped.pen),
+ `symbol("🖆")`.text,
+)
+#test(
+ repr(envelope.lightning),
+ `symbol("🖄")`.text,
+)
+#test(
+ repr(envelope.fly),
+ `symbol("🖅")`.text,
+)