use regex::Regex;
use std::sync::LazyLock;
pub static AT_PATTERN_REGEX: LazyLock<Regex> = LazyLock::new(|| {
match Regex::new(r#"@(?:\"([^\"]+)\"|'([^']+)'|([^\s"'\[\](){}<>|\\^`]+))"#) {
Ok(regex) => regex,
Err(error) => panic!("Failed to compile @ pattern regex: {error}"),
}
});
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AtPatternMatch<'a> {
pub full_match: &'a str,
pub path: &'a str,
pub start: usize,
pub end: usize,
}
pub fn find_at_patterns(text: &str) -> Vec<AtPatternMatch<'_>> {
AT_PATTERN_REGEX
.captures_iter(text)
.filter_map(|cap| {
let full_match = cap.get(0)?;
let path_part = cap.get(1).or_else(|| cap.get(2)).or_else(|| cap.get(3))?;
Some(AtPatternMatch {
full_match: full_match.as_str(),
path: path_part.as_str(),
start: full_match.start(),
end: full_match.end(),
})
})
.collect()
}