diff options
| author | T0mstone <39707032+T0mstone@users.noreply.github.com> | 2023-11-27 16:03:11 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-27 16:03:11 +0100 |
| commit | 76c5ca051e9c784e919e0b85155d147388e8e3d7 (patch) | |
| tree | 915d0e65114d3c47f19102e4c25a7e4b24af5501 | |
| parent | 1845e7c4f2692a998a2879601db518847117a7e2 (diff) | |
Fix trailing comma of function call in math mode (#2772)
| -rw-r--r-- | crates/typst-syntax/src/ast.rs | 10 | ||||
| -rw-r--r-- | crates/typst/src/eval/call.rs | 5 | ||||
| -rw-r--r-- | tests/ref/math/call.png | bin | 0 -> 1972 bytes | |||
| -rw-r--r-- | tests/typ/math/call.typ | 7 |
4 files changed, 22 insertions, 0 deletions
diff --git a/crates/typst-syntax/src/ast.rs b/crates/typst-syntax/src/ast.rs index 09157bbd..df865906 100644 --- a/crates/typst-syntax/src/ast.rs +++ b/crates/typst-syntax/src/ast.rs @@ -1571,6 +1571,16 @@ impl<'a> Args<'a> { pub fn items(self) -> impl DoubleEndedIterator<Item = Arg<'a>> { self.0.children().filter_map(SyntaxNode::cast) } + + /// Whether there is a comma at the end. + pub fn trailing_comma(self) -> bool { + self.0 + .children() + .rev() + .skip(1) + .find(|n| !n.kind().is_trivia()) + .is_some_and(|n| n.kind() == SyntaxKind::Comma) + } } /// An argument to a function call. diff --git a/crates/typst/src/eval/call.rs b/crates/typst/src/eval/call.rs index a57cb112..357d5e48 100644 --- a/crates/typst/src/eval/call.rs +++ b/crates/typst/src/eval/call.rs @@ -26,6 +26,8 @@ impl Eval for ast::FuncCall<'_> { let in_math = in_math(callee); let callee_span = callee.span(); let args = self.args(); + let trailing_comma = args.trailing_comma(); + if vm.engine.route.exceeding() { bail!(span, "maximum function call depth exceeded"); } @@ -133,6 +135,9 @@ impl Eval for ast::FuncCall<'_> { } body += arg; } + if trailing_comma { + body += TextElem::packed(','); + } return Ok(Value::Content( callee.display().spanned(callee_span) + LrElem::new(TextElem::packed('(') + body + TextElem::packed(')')) diff --git a/tests/ref/math/call.png b/tests/ref/math/call.png Binary files differnew file mode 100644 index 00000000..907a1a2b --- /dev/null +++ b/tests/ref/math/call.png diff --git a/tests/typ/math/call.typ b/tests/typ/math/call.typ new file mode 100644 index 00000000..0fce1627 --- /dev/null +++ b/tests/typ/math/call.typ @@ -0,0 +1,7 @@ +// Test function calls that aren't typst functions + +--- +$ pi(a) $ +$ pi(a,) $ +$ pi(a,b) $ +$ pi(a,b,) $ |
