Skip to content

Commit 361356f

Browse files
committed
with-watch: tighten ripgrep and fd parsing
1 parent 9dfc3f2 commit 361356f

1 file changed

Lines changed: 110 additions & 10 deletions

File tree

crates/with-watch/src/analysis.rs

Lines changed: 110 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,37 @@ fn analyze_ripgrep(
15081508
index += 2;
15091509
continue;
15101510
}
1511+
if matches!(
1512+
token,
1513+
"--pre"
1514+
| "--dfa-size-limit"
1515+
| "--encoding"
1516+
| "--engine"
1517+
| "--max-count"
1518+
| "--threads"
1519+
| "--max-depth"
1520+
| "--max-filesize"
1521+
| "--type-clear"
1522+
| "--after-context"
1523+
| "--before-context"
1524+
| "--context"
1525+
| "--color"
1526+
| "--colors"
1527+
| "--context-separator"
1528+
| "--field-context-separator"
1529+
| "--field-match-separator"
1530+
| "--hostname-bin"
1531+
| "--hyperlink-format"
1532+
| "--max-columns"
1533+
| "--path-separator"
1534+
| "--replace"
1535+
| "--sort"
1536+
| "--sortr"
1537+
| "--generate"
1538+
) {
1539+
index += 2;
1540+
continue;
1541+
}
15111542
if let Some(value) = token.strip_prefix("--file=") {
15121543
explicit_patterns = true;
15131544
push_inferred_input(&mut inputs, value, cwd)?;
@@ -1518,9 +1549,34 @@ fn analyze_ripgrep(
15181549
|| token.starts_with("--glob=")
15191550
|| token.starts_with("--iglob=")
15201551
|| token.starts_with("--pre-glob=")
1552+
|| token.starts_with("--pre=")
1553+
|| token.starts_with("--dfa-size-limit=")
1554+
|| token.starts_with("--encoding=")
1555+
|| token.starts_with("--engine=")
1556+
|| token.starts_with("--max-count=")
1557+
|| token.starts_with("--threads=")
1558+
|| token.starts_with("--max-depth=")
1559+
|| token.starts_with("--max-filesize=")
15211560
|| token.starts_with("--type-add=")
15221561
|| token.starts_with("--type=")
15231562
|| token.starts_with("--type-not=")
1563+
|| token.starts_with("--type-clear=")
1564+
|| token.starts_with("--after-context=")
1565+
|| token.starts_with("--before-context=")
1566+
|| token.starts_with("--context=")
1567+
|| token.starts_with("--color=")
1568+
|| token.starts_with("--colors=")
1569+
|| token.starts_with("--context-separator=")
1570+
|| token.starts_with("--field-context-separator=")
1571+
|| token.starts_with("--field-match-separator=")
1572+
|| token.starts_with("--hostname-bin=")
1573+
|| token.starts_with("--hyperlink-format=")
1574+
|| token.starts_with("--max-columns=")
1575+
|| token.starts_with("--path-separator=")
1576+
|| token.starts_with("--replace=")
1577+
|| token.starts_with("--sort=")
1578+
|| token.starts_with("--sortr=")
1579+
|| token.starts_with("--generate=")
15241580
{
15251581
explicit_patterns |= token.starts_with("--regexp=");
15261582
index += 1;
@@ -1812,6 +1868,7 @@ fn analyze_fd(
18121868
let mut base_dir: Option<String> = None;
18131869
let mut deferred_inputs = Vec::new();
18141870
let mut deferred_search_roots = Vec::new();
1871+
let mut extension_filter_present = false;
18151872
let mut positionals = Vec::new();
18161873
let mut positional_only = false;
18171874
let mut index = 1usize;
@@ -1846,6 +1903,11 @@ fn analyze_fd(
18461903
index += 2;
18471904
continue;
18481905
}
1906+
if token == "--extension" {
1907+
extension_filter_present = true;
1908+
index += 2;
1909+
continue;
1910+
}
18491911
if let Some(value) = token.strip_prefix("--search-path=") {
18501912
deferred_search_roots.push(value.to_owned());
18511913
index += 1;
@@ -1861,16 +1923,19 @@ fn analyze_fd(
18611923
index += 1;
18621924
continue;
18631925
}
1926+
if token.starts_with("--extension=") {
1927+
extension_filter_present = true;
1928+
index += 1;
1929+
continue;
1930+
}
18641931
if matches!(
18651932
token,
1866-
"-e" | "-E"
1867-
| "-t"
1933+
"-E" | "-t"
18681934
| "-c"
18691935
| "-d"
18701936
| "-j"
18711937
| "-o"
18721938
| "-S"
1873-
| "--extension"
18741939
| "--exclude"
18751940
| "--type"
18761941
| "--color"
@@ -1894,8 +1959,7 @@ fn analyze_fd(
18941959
index += 2;
18951960
continue;
18961961
}
1897-
if token.starts_with("--extension=")
1898-
|| token.starts_with("--exclude=")
1962+
if token.starts_with("--exclude=")
18991963
|| token.starts_with("--type=")
19001964
|| token.starts_with("--color=")
19011965
|| token.starts_with("--max-depth=")
@@ -1930,6 +1994,14 @@ fn analyze_fd(
19301994
index += 2;
19311995
continue;
19321996
}
1997+
FdShortOption::ExtensionInline => {
1998+
extension_filter_present = true;
1999+
}
2000+
FdShortOption::ExtensionNext => {
2001+
extension_filter_present = true;
2002+
index += 2;
2003+
continue;
2004+
}
19332005
FdShortOption::ValueInline => {}
19342006
FdShortOption::ValueNext => {
19352007
index += 2;
@@ -1956,9 +2028,7 @@ fn analyze_fd(
19562028

19572029
match positionals.len() {
19582030
0 => {}
1959-
1 if deferred_search_roots.is_empty()
1960-
&& path_exists(positionals[0].as_str(), fd_cwd.as_path()) =>
1961-
{
2031+
1 if extension_filter_present && deferred_search_roots.is_empty() => {
19622032
deferred_search_roots.push(positionals.remove(0));
19632033
}
19642034
1 => {}
@@ -1993,6 +2063,8 @@ fn analyze_fd(
19932063
enum FdShortOption<'a> {
19942064
BaseDirectoryInline(&'a str),
19952065
BaseDirectoryNext,
2066+
ExtensionInline,
2067+
ExtensionNext,
19962068
ValueInline,
19972069
ValueNext,
19982070
}
@@ -2008,10 +2080,12 @@ fn parse_fd_short_option(token: &str) -> Option<FdShortOption<'_>> {
20082080
match flag {
20092081
'C' if value.is_empty() => return Some(FdShortOption::BaseDirectoryNext),
20102082
'C' => return Some(FdShortOption::BaseDirectoryInline(value)),
2011-
'E' | 'S' | 'c' | 'd' | 'e' | 'j' | 'o' | 't' if value.is_empty() => {
2083+
'e' if value.is_empty() => return Some(FdShortOption::ExtensionNext),
2084+
'e' => return Some(FdShortOption::ExtensionInline),
2085+
'E' | 'S' | 'c' | 'd' | 'j' | 'o' | 't' if value.is_empty() => {
20122086
return Some(FdShortOption::ValueNext);
20132087
}
2014-
'E' | 'S' | 'c' | 'd' | 'e' | 'j' | 'o' | 't' => {
2088+
'E' | 'S' | 'c' | 'd' | 'j' | 'o' | 't' => {
20152089
return Some(FdShortOption::ValueInline);
20162090
}
20172091
_ => {}
@@ -3621,6 +3695,23 @@ mod tests {
36213695
);
36223696
assert_path_inputs(&rg_with_long_type, &[cwd.path().join("src")]);
36233697

3698+
let rg_with_long_value_flag = analyze_argv(
3699+
&[
3700+
OsString::from("rg"),
3701+
OsString::from("--max-count"),
3702+
OsString::from("1"),
3703+
OsString::from("TODO"),
3704+
OsString::from("src"),
3705+
],
3706+
cwd.path(),
3707+
)
3708+
.expect("analyze");
3709+
assert_eq!(
3710+
rg_with_long_value_flag.adapter_ids,
3711+
vec![CommandAdapterId::Ripgrep]
3712+
);
3713+
assert_path_inputs(&rg_with_long_value_flag, &[cwd.path().join("src")]);
3714+
36243715
let rg_with_inline_replace = analyze_argv(
36253716
&[
36263717
OsString::from("rg"),
@@ -3773,6 +3864,15 @@ mod tests {
37733864
&[cwd.path().join("src")],
37743865
);
37753866

3867+
let analysis_with_single_positional_pattern =
3868+
analyze_argv(&[OsString::from("fd"), OsString::from("src")], cwd.path())
3869+
.expect("analyze");
3870+
assert_eq!(
3871+
analysis_with_single_positional_pattern.adapter_ids,
3872+
vec![CommandAdapterId::Fd]
3873+
);
3874+
assert!(analysis_with_single_positional_pattern.inputs.is_empty());
3875+
37763876
let fallback = analyze_argv(
37773877
&[
37783878
OsString::from("fd"),

0 commit comments

Comments
 (0)