Skip to main content

vtcode_commons/
at_pattern.rs

1//! Utilities for parsing @ symbol patterns in user input
2
3use regex::Regex;
4use std::sync::LazyLock;
5
6/// Regex to match @ followed by a potential file path or URL
7/// Handles both quoted paths (with spaces) and unquoted paths
8pub static AT_PATTERN_REGEX: LazyLock<Regex> = LazyLock::new(|| {
9    match Regex::new(r#"@(?:\"([^\"]+)\"|'([^']+)'|([^\s"'\[\](){}<>|\\^`]+))"#) {
10        Ok(regex) => regex,
11        Err(error) => panic!("Failed to compile @ pattern regex: {error}"),
12    }
13});
14
15/// A parsed match of an @ pattern
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct AtPatternMatch<'a> {
18    /// The full text of the match (e.g., "@file.txt")
19    pub full_match: &'a str,
20    /// The extracted path or URL part (e.g., "file.txt")
21    pub path: &'a str,
22    /// Start position in the original string
23    pub start: usize,
24    /// End position in the original string
25    pub end: usize,
26}
27
28/// Find all @ patterns in the given text
29pub fn find_at_patterns(text: &str) -> Vec<AtPatternMatch<'_>> {
30    AT_PATTERN_REGEX
31        .captures_iter(text)
32        .filter_map(|cap| {
33            let full_match = cap.get(0)?;
34            let path_part = cap.get(1).or_else(|| cap.get(2)).or_else(|| cap.get(3))?;
35
36            Some(AtPatternMatch {
37                full_match: full_match.as_str(),
38                path: path_part.as_str(),
39                start: full_match.start(),
40                end: full_match.end(),
41            })
42        })
43        .collect()
44}