fileset: parse glob characters as identifier

It's inconvenient that we have to quote glob patterns as 'glob:"*.rs"'. Suppose
filesets are usually specified in shell, it's better to allow unquoted strings
if possible. This change also means we'll probably abandon #2101 "make the
parsing of string arguments stricter."

Note that we can no longer introduce ? operator or [] subscript syntax in
filesets.

Closes #4053
This commit is contained in:
Yuya Nishihara 2024-07-17 21:18:41 +09:00
parent ff0188d63a
commit 5649ee4f45
4 changed files with 27 additions and 5 deletions

View file

@ -204,7 +204,7 @@ fn test_diff_basic() {
"diff", "diff",
"--config-toml=ui.allow-filesets=true", "--config-toml=ui.allow-filesets=true",
"-s", "-s",
r#"glob:"file[12]""#, "glob:file[12]",
], ],
); );
insta::assert_snapshot!(stdout, @r###" insta::assert_snapshot!(stdout, @r###"

View file

@ -21,6 +21,11 @@ if the expression has no operators nor function calls. For example:
* `jj diff '~"Foo Bar"'` (both shell and inner quotes are required) * `jj diff '~"Foo Bar"'` (both shell and inner quotes are required)
* `jj diff '"Foo(1)"'` (both shell and inner quotes are required) * `jj diff '"Foo(1)"'` (both shell and inner quotes are required)
Glob characters aren't considered meta characters, but shell quotes are still
required:
* `jj diff '~glob:**/*.rs'`
[string-literals]: templates.md#string-literals [string-literals]: templates.md#string-literals
## File patterns ## File patterns

View file

@ -16,22 +16,21 @@ whitespace = _{ " " | "\t" | "\r" | "\n" | "\x0c" }
// XID_CONTINUE: https://www.unicode.org/reports/tr31/#Default_Identifier_Syntax // XID_CONTINUE: https://www.unicode.org/reports/tr31/#Default_Identifier_Syntax
// +, -, ., @, _: commonly used in file name including "." and ".." // +, -, ., @, _: commonly used in file name including "." and ".."
// *, ?, [, ]: glob characters (not extended glob)
// /: path separator // /: path separator
// \: path separator (Windows) // \: path separator (Windows)
// TODO: accept glob characters as identifier?
identifier = @{ identifier = @{
(XID_CONTINUE | "+" | "-" | "." | "@" | "_" | "/" | "\\")+ (XID_CONTINUE | "+" | "-" | "." | "@" | "_" | "*" | "?" | "[" | "]" | "/" | "\\")+
} }
strict_identifier_part = @{ (ASCII_ALPHANUMERIC | "_")+ } strict_identifier_part = @{ (ASCII_ALPHANUMERIC | "_")+ }
strict_identifier = @{ strict_identifier = @{
strict_identifier_part ~ ("-" ~ strict_identifier_part)* strict_identifier_part ~ ("-" ~ strict_identifier_part)*
} }
// TODO: accept glob characters?
// TODO: accept more ASCII meta characters such as "#" and ","? // TODO: accept more ASCII meta characters such as "#" and ","?
bare_string = @{ bare_string = @{
( ASCII_ALPHANUMERIC ( ASCII_ALPHANUMERIC
| " " | "+" | "-" | "." | "@" | "_" | "/" | "\\" | " " | "+" | "-" | "." | "@" | "_" | "*" | "?" | "[" | "]" | "/" | "\\"
| '\u{80}'..'\u{10ffff}' )+ | '\u{80}'..'\u{10ffff}' )+
} }

View file

@ -458,6 +458,10 @@ mod tests {
parse_into_kind(r#"Windows\Path"#), parse_into_kind(r#"Windows\Path"#),
Ok(ExpressionKind::Identifier(r#"Windows\Path"#)) Ok(ExpressionKind::Identifier(r#"Windows\Path"#))
); );
assert_eq!(
parse_into_kind("glob*[chars]?"),
Ok(ExpressionKind::Identifier("glob*[chars]?"))
);
} }
#[test] #[test]
@ -502,6 +506,13 @@ mod tests {
value: "bar".to_owned() value: "bar".to_owned()
}) })
); );
assert_eq!(
parse_into_kind(" foo:glob*[chars]? "),
Ok(ExpressionKind::StringPattern {
kind: "foo",
value: "glob*[chars]?".to_owned()
})
);
assert_eq!( assert_eq!(
parse_into_kind(r#" foo:"bar" "#), parse_into_kind(r#" foo:"bar" "#),
Ok(ExpressionKind::StringPattern { Ok(ExpressionKind::StringPattern {
@ -645,6 +656,13 @@ mod tests {
value: " bar baz".to_owned() value: " bar baz".to_owned()
}) })
); );
assert_eq!(
parse_maybe_bare_into_kind("foo:glob * [chars]?"),
Ok(ExpressionKind::StringPattern {
kind: "foo",
value: "glob * [chars]?".to_owned()
})
);
assert_eq!( assert_eq!(
parse_maybe_bare_into_kind("foo:bar:baz"), parse_maybe_bare_into_kind("foo:bar:baz"),
Err(FilesetParseErrorKind::SyntaxError) Err(FilesetParseErrorKind::SyntaxError)