From edff2ae6803928e59eae96e2f75cd62a7e24c76f Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 9 Jun 2021 10:45:49 +0200 Subject: [PATCH] Remove template pretty printing Was buggy and pretty useless anyway since it couldn't peek into function templates. --- src/pretty.rs | 163 +++++++--------------------- src/syntax/expr.rs | 14 ++- tests/ref/code/call-invalid.png | Bin 2834 -> 2975 bytes tests/ref/code/call.png | Bin 5972 -> 6150 bytes tests/ref/code/dict.png | Bin 1612 -> 1602 bytes tests/typ/code/call-invalid.typ | 1 - tests/typ/code/call.typ | 5 +- tests/typ/code/dict.typ | 2 +- tests/typ/library/basic.typ | 2 +- tools/support/typst.tmLanguage.json | 2 +- 10 files changed, 50 insertions(+), 139 deletions(-) diff --git a/src/pretty.rs b/src/pretty.rs index 9987e5063..dc1e284ca 100644 --- a/src/pretty.rs +++ b/src/pretty.rs @@ -17,37 +17,12 @@ where p.finish() } -/// Pretty print an item with a expression map and return the resulting string. -pub fn pretty_with_map(item: &T, map: &ExprMap) -> String -where - T: PrettyWithMap + ?Sized, -{ - let mut p = Printer::new(); - item.pretty_with_map(&mut p, Some(map)); - p.finish() -} - /// Pretty print an item. pub trait Pretty { /// Pretty print this item into the given printer. fn pretty(&self, p: &mut Printer); } -/// Pretty print an item with an expression map that applies to it. -pub trait PrettyWithMap { - /// Pretty print this item into the given printer. - fn pretty_with_map(&self, p: &mut Printer, map: Option<&ExprMap>); -} - -impl Pretty for T -where - T: PrettyWithMap, -{ - fn pretty(&self, p: &mut Printer) { - self.pretty_with_map(p, None); - } -} - /// A buffer into which items are printed. pub struct Printer { buf: String, @@ -103,16 +78,16 @@ impl Write for Printer { } } -impl PrettyWithMap for Tree { - fn pretty_with_map(&self, p: &mut Printer, map: Option<&ExprMap>) { +impl Pretty for Tree { + fn pretty(&self, p: &mut Printer) { for node in self { - node.pretty_with_map(p, map); + node.pretty(p); } } } -impl PrettyWithMap for Node { - fn pretty_with_map(&self, p: &mut Printer, map: Option<&ExprMap>) { +impl Pretty for Node { + fn pretty(&self, p: &mut Printer) { match self { // TODO: Handle escaping. Self::Text(text) => p.push_str(text), @@ -122,18 +97,13 @@ impl PrettyWithMap for Node { Self::Strong(_) => p.push('*'), Self::Emph(_) => p.push('_'), Self::Raw(raw) => raw.pretty(p), - Self::Heading(heading) => heading.pretty_with_map(p, map), - Self::List(list) => list.pretty_with_map(p, map), + Self::Heading(heading) => heading.pretty(p), + Self::List(list) => list.pretty(p), Self::Expr(expr) => { - if let Some(map) = map { - let value = &map[&(expr as *const _)]; - value.pretty(p); - } else { - if expr.has_short_form() { - p.push('#'); - } - expr.pretty(p); + if expr.has_short_form() { + p.push('#'); } + expr.pretty(p); } } } @@ -195,20 +165,20 @@ impl Pretty for RawNode { } } -impl PrettyWithMap for HeadingNode { - fn pretty_with_map(&self, p: &mut Printer, map: Option<&ExprMap>) { +impl Pretty for HeadingNode { + fn pretty(&self, p: &mut Printer) { for _ in 0 .. self.level { p.push('#'); } p.push(' '); - self.body.pretty_with_map(p, map); + self.body.pretty(p); } } -impl PrettyWithMap for ListNode { - fn pretty_with_map(&self, p: &mut Printer, map: Option<&ExprMap>) { +impl Pretty for ListNode { + fn pretty(&self, p: &mut Printer) { p.push_str("- "); - self.body.pretty_with_map(p, map); + self.body.pretty(p); } } @@ -278,7 +248,7 @@ impl Pretty for Named { impl Pretty for TemplateExpr { fn pretty(&self, p: &mut Printer) { p.push('['); - self.tree.pretty_with_map(p, None); + self.tree.pretty(p); p.push(']'); } } @@ -488,7 +458,6 @@ impl Pretty for Value { Value::Relative(v) => v.pretty(p), Value::Linear(v) => v.pretty(p), Value::Color(v) => v.pretty(p), - // TODO: Handle like text when directly in template. Value::Str(v) => v.pretty(p), Value::Array(v) => v.pretty(p), Value::Dict(v) => v.pretty(p), @@ -529,29 +498,7 @@ impl Pretty for DictValue { impl Pretty for TemplateValue { fn pretty(&self, p: &mut Printer) { - p.push('['); - for part in self { - part.pretty(p); - } - p.push(']'); - } -} - -impl Pretty for TemplateNode { - fn pretty(&self, p: &mut Printer) { - match self { - Self::Tree { tree, map } => tree.pretty_with_map(p, Some(map)), - Self::Str(s) => p.push_str(s), - Self::Func(func) => func.pretty(p), - } - } -} - -impl Pretty for TemplateFunc { - fn pretty(&self, p: &mut Printer) { - p.push_str("'); + p.push_str("