#![cfg_attr(not(feature = "embed-candle"), allow(dead_code))]
const STRIP: &[char] = &[
'`', '\'', '"', ',', '.', ';', ':', '?', '!', '(', ')', '[', ']', '{', '}',
];
fn has_between(t: &str, mid: char) -> bool {
let c: Vec<char> = t.chars().collect();
(1..c.len().saturating_sub(1))
.any(|i| c[i] == mid && c[i - 1].is_alphanumeric() && c[i + 1].is_alphanumeric())
}
fn camel(t: &str) -> bool {
let c: Vec<char> = t.chars().collect();
if c.windows(2)
.any(|w| w[0].is_ascii_lowercase() && w[1].is_ascii_uppercase())
{
return true;
}
for i in 0..c.len() {
if c[i].is_ascii_uppercase() {
let mut j = i + 1;
while j < c.len() && c[j].is_ascii_lowercase() {
j += 1;
}
if j > i + 1 && j < c.len() && c[j].is_ascii_uppercase() {
return true;
}
}
}
false
}
fn dotted(t: &str) -> bool {
let c: Vec<char> = t.chars().collect();
let word = |ch: char| ch.is_ascii_alphabetic() || ch == '_';
(1..c.len().saturating_sub(1)).any(|i| c[i] == '.' && word(c[i - 1]) && word(c[i + 1]))
}
fn call(raw: &str) -> bool {
let c: Vec<char> = raw.chars().collect();
(1..c.len()).any(|i| c[i] == '(' && c[i - 1].is_alphabetic())
}
fn shaped(raw: &str, t: &str) -> bool {
t.contains("::")
|| t.contains('@')
|| t.contains('=')
|| t.starts_with("--")
|| has_between(t, '_')
|| camel(t)
|| call(raw)
|| dotted(t)
}
pub(crate) fn identifiers(query: &str) -> Vec<String> {
let mut in_tick = vec![false; query.len()];
let mut open: Option<usize> = None;
for (i, ch) in query.char_indices() {
if ch == '`' {
match open.take() {
Some(s) => in_tick[s..i].iter_mut().for_each(|b| *b = true),
None => open = Some(i),
}
}
}
let mut out: Vec<String> = Vec::new();
let mut after_flag = false;
let mut pos = 0;
for raw in query.split_whitespace() {
let start = query[pos..].find(raw).map(|o| pos + o).unwrap_or(pos);
pos = start + raw.len();
let t = raw.trim_matches(STRIP);
if t.is_empty() {
after_flag = false;
continue;
}
let ticked = in_tick[start..start + raw.len()].iter().any(|b| *b);
let is_ident = ticked || shaped(raw, t);
let flag_value = after_flag && t.contains([',', '=', '.', '/']);
if (is_ident || flag_value) && !out.iter().any(|o| o == t) {
out.push(t.to_string());
}
after_flag = is_ident && t.starts_with("--");
}
out
}
#[cfg(test)]
mod tests {
use super::identifiers;
#[test]
fn the_dispatch_cases() {
assert_eq!(
identifiers("what does `snapshot_list` return"),
["snapshot_list"]
);
assert_eq!(
identifiers("why use --features failpoints,benches,keychain"),
["--features", "failpoints,benches,keychain"]
);
assert_eq!(
identifiers("RecallRequest::from_text defaults"),
["RecallRequest::from_text"]
);
assert_eq!(
identifiers("where does keyring@3.6.3 store it"),
["keyring@3.6.3"]
);
assert!(identifiers("why 1.92").is_empty());
assert!(identifiers("who is the product for").is_empty());
}
#[test]
fn the_other_shapes() {
assert_eq!(
identifiers("does moveToState(CREATED) stop it?"),
["moveToState(CREATED"]
);
assert_eq!(identifiers("what's in store.rs"), ["store.rs"]);
assert_eq!(identifiers("set max_bytes"), ["max_bytes"]);
assert_eq!(
identifiers("is MnemoError::Replaced safe"),
["MnemoError::Replaced"]
);
assert!(identifiers("v0.14.0 and 2.5 s").is_empty());
assert!(identifiers("it's done, isn't it?").is_empty());
}
#[test]
fn a_flags_value_counts_only_when_it_looks_like_one() {
assert_eq!(identifiers("what does --shared do"), ["--shared"]);
assert_eq!(identifiers("--wait for a writer"), ["--wait"]);
assert_eq!(
identifiers("run with --model-dir models/arctic"),
["--model-dir", "models/arctic"]
);
assert_eq!(identifiers("--wait 2.5s please"), ["--wait", "2.5s"]);
assert_eq!(
identifiers("--filter area=decisions"),
["--filter", "area=decisions"]
);
assert_eq!(
identifiers("--agent cursor_agent"),
["--agent", "cursor_agent"]
);
assert_eq!(
identifiers("--key fact.msrv then a,b"),
["--key", "fact.msrv"]
);
}
#[test]
fn backticks_and_repeats() {
assert_eq!(identifiers("why is `recall` slow"), ["recall"]);
assert_eq!(
identifiers("`a b` and `a`"),
["a", "b"],
"each token inside the ticks, once"
);
assert_eq!(identifiers("max_bytes or max_bytes?"), ["max_bytes"]);
assert!(identifiers("a `plain question").is_empty());
assert!(identifiers("").is_empty());
}
}