templater: allow trailing comma

While rewriting the default log template, I find it's annoying that I have
to remove "," from the last line.
This commit is contained in:
Yuya Nishihara 2023-02-07 18:33:26 +09:00
parent ba1c4f5fe5
commit 686c1fb522
3 changed files with 20 additions and 2 deletions

View file

@ -1449,7 +1449,7 @@ fn log_template(settings: &UserSettings) -> String {
working_copies,
git_head,
commit_id.{prefix_format},
if(conflict, label("conflict", "conflict"))
if(conflict, label("conflict", "conflict")),
)
"\n"
if(empty, label("empty", "(empty)") " ")

View file

@ -33,7 +33,7 @@ identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
function = { identifier ~ "(" ~ function_arguments ~ ")" }
function_arguments = {
template ~ ("," ~ template)*
template ~ ("," ~ template)* ~ ("," ~ whitespace*)?
| whitespace*
}

View file

@ -789,6 +789,24 @@ mod tests {
})
}
#[test]
fn test_function_call_syntax() {
// Trailing comma isn't allowed for empty argument
assert!(parse(r#" "".first_line() "#).is_ok());
assert!(parse(r#" "".first_line(,) "#).is_err());
// Trailing comma is allowed for the last argument
assert!(parse(r#" "".contains("") "#).is_ok());
assert!(parse(r#" "".contains("",) "#).is_ok());
assert!(parse(r#" "".contains("" , ) "#).is_ok());
assert!(parse(r#" "".contains(,"") "#).is_err());
assert!(parse(r#" "".contains("",,) "#).is_err());
assert!(parse(r#" "".contains("" , , ) "#).is_err());
assert!(parse(r#" label("","") "#).is_ok());
assert!(parse(r#" label("","",) "#).is_ok());
assert!(parse(r#" label("",,"") "#).is_err());
}
#[test]
fn test_integer_literal() {
let extract = |x: Expression<()>| x.try_into_integer().unwrap().extract(&());