feat: support %0 as the hovered file for Windows (#761)

This commit is contained in:
三咲雅 · Misaki Masa 2024-03-02 13:00:39 +08:00 committed by GitHub
parent 6a1063d376
commit b39b506e27
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -102,9 +102,7 @@ mod parser {
expanded.push(s); expanded.push(s);
} else if c == '%' && it.peek().is_some_and(|&c| c == '*') { } else if c == '%' && it.peek().is_some_and(|&c| c == '*') {
it.next(); it.next();
for arg in args { expanded.extend(args.iter().skip(1).map(|&s| s.to_owned()));
expanded.push(arg.to_string());
}
} else { } else {
next_string(&mut it, args, &mut s, c); next_string(&mut it, args, &mut s, c);
@ -140,7 +138,9 @@ mod parser {
} else if c == '%' { } else if c == '%' {
match it.peek() { match it.peek() {
Some('*') => { Some('*') => {
s.push_str(&args.join(" ")); if args.len() > 1 {
s.push_str(&args[1..].join(" "));
}
it.next(); it.next();
} }
Some(n) if n.is_ascii_digit() => { Some(n) if n.is_ascii_digit() => {
@ -155,9 +155,8 @@ mod parser {
} }
} }
let pos = pos.parse::<usize>().unwrap(); if let Some(arg) = args.get(pos.parse::<usize>().unwrap()) {
if pos > 0 { s.push_str(arg);
s.push_str(args.get(pos - 1).unwrap_or(&""));
} }
} }
_ => s.push('%'), _ => s.push('%'),
@ -173,49 +172,55 @@ mod parser {
#[test] #[test]
fn test_no_quote() { fn test_no_quote() {
let args = parse("echo abc xyz %0 %2", &["111", "222"]); let args = parse("echo abc xyz %0 %2", &["000", "111", "222"]);
assert_eq!(args, ["echo", "abc", "xyz", "", "222"]); assert_eq!(args, ["echo", "abc", "xyz", "000", "222"]);
let args = parse(" echo abc xyz %1 %2 ", &["111", "222"]); let args = parse(" echo abc xyz %1 %2 ", &["", "111", "222"]);
assert_eq!(args, ["echo", "abc", "xyz", "111", "222"]); assert_eq!(args, ["echo", "abc", "xyz", "111", "222"]);
} }
#[test] #[test]
fn test_single_quote() { fn test_single_quote() {
let args = parse("echo 'abc xyz' '%1' %2", &["111", "222"]); let args = parse("echo 'abc xyz' '%1' %2", &["000", "111", "222"]);
assert_eq!(args, ["echo", "abc xyz", "111", "222"]); assert_eq!(args, ["echo", "abc xyz", "111", "222"]);
let args = parse(r#"echo 'abc ""xyz' '%1' %2"#, &["111", "222"]); let args = parse(r#"echo 'abc ""xyz' '%1' %2"#, &["", "111", "222"]);
assert_eq!(args, ["echo", r#"abc ""xyz"#, "111", "222"]); assert_eq!(args, ["echo", r#"abc ""xyz"#, "111", "222"]);
} }
#[test] #[test]
fn test_double_quote() { fn test_double_quote() {
let args = parse("echo \"abc ' 'xyz\" \"%1\" %2 %3", &["111", "222"]); let args = parse("echo \"abc ' 'xyz\" \"%1\" %2 %3", &["", "111", "222"]);
assert_eq!(args, ["echo", "abc ' 'xyz", "111", "222", ""]); assert_eq!(args, ["echo", "abc ' 'xyz", "111", "222", ""]);
} }
#[test] #[test]
fn test_escaped() { fn test_escaped() {
let args = parse("echo \"a\tbc ' 'x\nyz\" \"\\%1\" %2 %3", &["111", "22 2"]); let args = parse("echo \"a\tbc ' 'x\nyz\" \"\\%1\" %2 %3", &["", "111", "22 2"]);
assert_eq!(args, ["echo", "a\tbc ' 'x\nyz", "%1", "22 2", ""]); assert_eq!(args, ["echo", "a\tbc ' 'x\nyz", "%1", "22 2", ""]);
} }
#[test] #[test]
fn test_percent_star() { fn test_percent_star() {
let args = parse("echo %* xyz", &["111", "222"]); let args = parse("echo %* xyz", &[]);
assert_eq!(args, ["echo", "xyz"]);
let args = parse("echo %* xyz", &["000", "111", "222"]);
assert_eq!(args, ["echo", "111", "222", "xyz"]); assert_eq!(args, ["echo", "111", "222", "xyz"]);
let args = parse("echo '%*' xyz", &["111", "222"]); let args = parse("echo '%*' xyz", &["000", "111", "222"]);
assert_eq!(args, ["echo", "111 222", "xyz"]); assert_eq!(args, ["echo", "111 222", "xyz"]);
let args = parse("echo -C%* xyz", &["111", "222"]); let args = parse("echo -C%* xyz", &[]);
assert_eq!(args, ["echo", "-C", "xyz"]);
let args = parse("echo -C%* xyz", &["000", "111", "222"]);
assert_eq!(args, ["echo", "-C111 222", "xyz"]); assert_eq!(args, ["echo", "-C111 222", "xyz"]);
} }
#[test] #[test]
fn test_env_var() { fn test_env_var() {
let args = parse(" %EDITOR% %* xyz", &["111", "222"]); let args = parse(" %EDITOR% %* xyz", &["000", "111", "222"]);
assert_eq!(args, ["%EDITOR%", "111", "222", "xyz"]); assert_eq!(args, ["%EDITOR%", "111", "222", "xyz"]);
} }
} }