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    Regex::new(r#"@(?:\"([^\"]+)\"|'([^']+)'|([^\s"'\[\](){}<>|\\^`]+))"#)
10        .expect("Failed to compile @ pattern regex")
11});
12
13/// A parsed match of an @ pattern
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct AtPatternMatch<'a> {
16    /// The full text of the match (e.g., "@file.txt")
17    pub full_match: &'a str,
18    /// The extracted path or URL part (e.g., "file.txt")
19    pub path: &'a str,
20    /// Start position in the original string
21    pub start: usize,
22    /// End position in the original string
23    pub end: usize,
24}
25
26/// Find all @ patterns in the given text
27pub fn find_at_patterns(text: &str) -> Vec<AtPatternMatch<'_>> {
28    AT_PATTERN_REGEX
29        .captures_iter(text)
30        .filter_map(|cap| {
31            let full_match = cap.get(0)?;
32            let path_part = cap.get(1).or_else(|| cap.get(2)).or_else(|| cap.get(3))?;
33
34            Some(AtPatternMatch {
35                full_match: full_match.as_str(),
36                path: path_part.as_str(),
37                start: full_match.start(),
38                end: full_match.end(),
39            })
40        })
41        .collect()
42}