From 76c5ca051e9c784e919e0b85155d147388e8e3d7 Mon Sep 17 00:00:00 2001 From: T0mstone <39707032+T0mstone@users.noreply.github.com> Date: Mon, 27 Nov 2023 16:03:11 +0100 Subject: [PATCH] Fix trailing comma of function call in math mode (#2772) --- crates/typst-syntax/src/ast.rs | 10 ++++++++++ crates/typst/src/eval/call.rs | 5 +++++ tests/ref/math/call.png | Bin 0 -> 1972 bytes tests/typ/math/call.typ | 7 +++++++ 4 files changed, 22 insertions(+) create mode 100644 tests/ref/math/call.png create mode 100644 tests/typ/math/call.typ diff --git a/crates/typst-syntax/src/ast.rs b/crates/typst-syntax/src/ast.rs index 09157bbd1..df865906e 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> { 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 a57cb1127..357d5e485 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 new file mode 100644 index 0000000000000000000000000000000000000000..907a1a2be4a699e22e45e9bfa9d9df98ad46a865 GIT binary patch literal 1972 zcm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|XT_cwj)Nwt41S6541SWBvGUNU>Y1r2)@(DYY~ojG=<4itWVBNj#%J02c2=*f z^VLaaYfY-~^2+A#^A90jBoTpe zpTWWaDlmJYaeP)B2;K#mYi?A;2OWk!=igPe$EvQ2Q_3M_OC;8;1LVD@k zNtv9%DEM~hmV$-gteZW7n|!45r9b`mfQX%cy|ioZw2uDo{+?=oJ|(ynX>|9RUgV#5 N>}c;qsj>A*`7g`qRw)1g literal 0 HcmV?d00001 diff --git a/tests/typ/math/call.typ b/tests/typ/math/call.typ new file mode 100644 index 000000000..0fce16279 --- /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,) $