Fix trailing comma of function call in math mode (#2772)

This commit is contained in:
T0mstone 2023-11-27 16:03:11 +01:00 committed by GitHub
parent 1845e7c4f2
commit 76c5ca051e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 0 deletions

View File

@ -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.

View File

@ -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(')'))

BIN
tests/ref/math/call.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

7
tests/typ/math/call.typ Normal file
View File

@ -0,0 +1,7 @@
// Test function calls that aren't typst functions
---
$ pi(a) $
$ pi(a,) $
$ pi(a,b) $
$ pi(a,b,) $