summaryrefslogtreecommitdiff
path: root/src/syntax/node.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-01-12 18:55:12 +0100
committerLaurenz <laurmaedje@gmail.com>2021-01-12 18:55:12 +0100
commitd2ba1b705ed7a532266294aa100f19423bb07f4d (patch)
tree1f06bf618f2190a3aabc26d69f0fb819e970341c /src/syntax/node.rs
parent105cda0e698fe86266d706f4e3bacc081e65c2aa (diff)
Group and block expressions, unary plus 🧩
Diffstat (limited to 'src/syntax/node.rs')
-rw-r--r--src/syntax/node.rs48
1 files changed, 14 insertions, 34 deletions
diff --git a/src/syntax/node.rs b/src/syntax/node.rs
index e7091a40..5c4c22b7 100644
--- a/src/syntax/node.rs
+++ b/src/syntax/node.rs
@@ -34,29 +34,14 @@ impl Pretty for Node {
Self::Text(text) => p.push_str(&text),
Self::Heading(heading) => heading.pretty(p),
Self::Raw(raw) => raw.pretty(p),
- Self::Expr(expr) => pretty_expr_node(expr, p),
- }
- }
-}
-
-/// Pretty print an expression in a node context.
-pub fn pretty_expr_node(expr: &Expr, p: &mut Printer) {
- match expr {
- // Prefer bracket calls over expression blocks with just a single paren
- // call.
- //
- // Example: Transforms "{v()}" => "[v]".
- Expr::Call(call) => pretty_bracket_call(call, p, false),
-
- // Remove unncessary nesting of content and expression blocks.
- //
- // Example: Transforms "{[Hi]}" => "Hi".
- Expr::Template(template) => template.pretty(p),
-
- _ => {
- p.push_str("{");
- expr.pretty(p);
- p.push_str("}");
+ Self::Expr(expr) => {
+ if let Expr::Call(call) = expr {
+ // Format bracket calls appropriately.
+ pretty_bracket_call(call, p, false)
+ } else {
+ expr.pretty(p);
+ }
+ }
}
}
}
@@ -178,18 +163,13 @@ mod tests {
use super::super::tests::test_pretty;
#[test]
- fn test_pretty_print_removes_nesting() {
- // Nesting does not matter.
- test_pretty("{{x}}", "{x}");
- test_pretty("{{{x}}}", "{x}");
- }
-
- #[test]
- fn test_pretty_print_prefers_bracket_calls() {
- // All reduces to a simple bracket call.
- test_pretty("{v()}", "[v]");
+ fn test_pretty_print_bracket_calls() {
+ // Top-level call expression formatted as bracket call.
test_pretty("[v]", "[v]");
- test_pretty("{[[v]]}", "[v]");
+
+ // Blocks are preserved.
+ test_pretty("{v()}", "{v()}");
+ test_pretty("{[[v]]}", "{[[v]]}");
}
#[test]