mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Better function and closure pretty printing
This commit is contained in:
parent
6d26e15fbe
commit
9e78dbe525
@ -372,9 +372,14 @@ impl Pretty for CallArg {
|
|||||||
|
|
||||||
impl Pretty for ClosureExpr {
|
impl Pretty for ClosureExpr {
|
||||||
fn pretty(&self, p: &mut Printer) {
|
fn pretty(&self, p: &mut Printer) {
|
||||||
p.push('(');
|
if let [param] = self.params.as_slice() {
|
||||||
p.join(self.params.iter(), ", ", |item, p| item.pretty(p));
|
param.pretty(p);
|
||||||
p.push_str(") => ");
|
} else {
|
||||||
|
p.push('(');
|
||||||
|
p.join(self.params.iter(), ", ", |item, p| item.pretty(p));
|
||||||
|
p.push(')');
|
||||||
|
}
|
||||||
|
p.push_str(" => ");
|
||||||
self.body.pretty(p);
|
self.body.pretty(p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -405,7 +410,12 @@ impl Pretty for LetExpr {
|
|||||||
fn pretty(&self, p: &mut Printer) {
|
fn pretty(&self, p: &mut Printer) {
|
||||||
p.push_str("let ");
|
p.push_str("let ");
|
||||||
self.binding.pretty(p);
|
self.binding.pretty(p);
|
||||||
if let Some(init) = &self.init {
|
if let Some(Expr::Closure(closure)) = &self.init {
|
||||||
|
p.push('(');
|
||||||
|
p.join(closure.params.iter(), ", ", |item, p| item.pretty(p));
|
||||||
|
p.push_str(") = ");
|
||||||
|
closure.body.pretty(p);
|
||||||
|
} else if let Some(init) = &self.init {
|
||||||
p.push_str(" = ");
|
p.push_str(" = ");
|
||||||
init.pretty(p);
|
init.pretty(p);
|
||||||
}
|
}
|
||||||
@ -514,8 +524,8 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_pretty_print_node() {
|
fn test_pretty_print_markup() {
|
||||||
// Basic text and markup.
|
// Basic stuff.
|
||||||
roundtrip("*");
|
roundtrip("*");
|
||||||
roundtrip("_");
|
roundtrip("_");
|
||||||
roundtrip(" ");
|
roundtrip(" ");
|
||||||
@ -525,7 +535,7 @@ mod tests {
|
|||||||
roundtrip("= *Ok*");
|
roundtrip("= *Ok*");
|
||||||
roundtrip("- Ok");
|
roundtrip("- Ok");
|
||||||
|
|
||||||
// Raw.
|
// Raw node.
|
||||||
roundtrip("``");
|
roundtrip("``");
|
||||||
roundtrip("`nolang 1`");
|
roundtrip("`nolang 1`");
|
||||||
roundtrip("```lang 1```");
|
roundtrip("```lang 1```");
|
||||||
@ -556,7 +566,7 @@ mod tests {
|
|||||||
roundtrip("{20%}");
|
roundtrip("{20%}");
|
||||||
roundtrip("{0.5fr}");
|
roundtrip("{0.5fr}");
|
||||||
roundtrip(r#"{"hi"}"#);
|
roundtrip(r#"{"hi"}"#);
|
||||||
test_parse(r#"{"let's \" go"}"#, r#"{"let's \" go"}"#);
|
roundtrip(r#"{"let's \" go"}"#);
|
||||||
roundtrip("{hi}");
|
roundtrip("{hi}");
|
||||||
|
|
||||||
// Arrays.
|
// Arrays.
|
||||||
@ -597,11 +607,12 @@ mod tests {
|
|||||||
roundtrip("#v(1)");
|
roundtrip("#v(1)");
|
||||||
roundtrip("#v(1, 2)[*Ok*]");
|
roundtrip("#v(1, 2)[*Ok*]");
|
||||||
roundtrip("#v(1, f[2])");
|
roundtrip("#v(1, f[2])");
|
||||||
|
roundtrip("{x => x + 1}");
|
||||||
roundtrip("{(a, b) => a + b}");
|
roundtrip("{(a, b) => a + b}");
|
||||||
|
|
||||||
// Control flow.
|
// Control flow.
|
||||||
roundtrip("#let x = 1 + 2");
|
roundtrip("#let x = 1 + 2");
|
||||||
test_parse("#let f(x) = y", "#let f = (x) => y");
|
roundtrip("#let f(x) = y");
|
||||||
roundtrip("#if x [y] else [z]");
|
roundtrip("#if x [y] else [z]");
|
||||||
roundtrip("#if x {} else if y {} else {}");
|
roundtrip("#if x {} else if y {} else {}");
|
||||||
roundtrip("#while x {y}");
|
roundtrip("#while x {y}");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user