infiniloom_engine/index/
patterns.rs

1//! Shared regex patterns for import extraction across index modules.
2//!
3//! These patterns are used by both the full index builder and the lazy context builder
4//! to extract import statements from source code.
5
6use once_cell::sync::Lazy;
7use regex::Regex;
8
9/// Python: `import module` or `import module.submodule`
10pub static PYTHON_IMPORT: Lazy<Regex> = Lazy::new(|| Regex::new(r"^\s*import\s+(\S+)").unwrap());
11
12/// Python: `from module import name`
13pub static PYTHON_FROM_IMPORT: Lazy<Regex> =
14    Lazy::new(|| Regex::new(r"^\s*from\s+(\S+)\s+import").unwrap());
15
16/// JavaScript/TypeScript: `import ... from 'module'` or `import ... from "module"`
17pub static JS_IMPORT: Lazy<Regex> =
18    Lazy::new(|| Regex::new(r#"import\s+.*\s+from\s+['"]([^'"]+)['"]"#).unwrap());
19
20/// JavaScript/TypeScript: multi-line import statements
21pub static JS_IMPORT_MULTILINE: Lazy<Regex> =
22    Lazy::new(|| Regex::new(r#"(?s)import\s+.*?\s+from\s+['"]([^'"]+)['"]"#).unwrap());
23
24/// JavaScript/TypeScript: `require('module')` or `require("module")`
25pub static JS_REQUIRE: Lazy<Regex> =
26    Lazy::new(|| Regex::new(r#"require\s*\(\s*['"]([^'"]+)['"]\s*\)"#).unwrap());
27
28/// Rust: `use crate::module;` or `use std::collections::HashMap;`
29pub static RUST_USE: Lazy<Regex> = Lazy::new(|| Regex::new(r"^\s*use\s+([^;]+);").unwrap());
30
31/// Go: `import "module"` or `import alias "module"` or `import ( "module" )`
32pub static GO_IMPORT: Lazy<Regex> =
33    Lazy::new(|| Regex::new(r#"import\s+(?:\(\s*)?(?:[\w.]+\s+)?["']([^"']+)["']"#).unwrap());
34
35/// Java: `import package.Class;`
36pub static JAVA_IMPORT: Lazy<Regex> = Lazy::new(|| Regex::new(r"^\s*import\s+([\w.]+);").unwrap());
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn test_python_import() {
44        assert!(PYTHON_IMPORT.is_match("import os"));
45        assert!(PYTHON_IMPORT.is_match("import os.path"));
46        assert!(PYTHON_IMPORT.is_match("  import json"));
47    }
48
49    #[test]
50    fn test_python_from_import() {
51        assert!(PYTHON_FROM_IMPORT.is_match("from os import path"));
52        assert!(PYTHON_FROM_IMPORT.is_match("from typing import List"));
53    }
54
55    #[test]
56    fn test_js_import() {
57        assert!(JS_IMPORT.is_match("import { foo } from 'module'"));
58        assert!(JS_IMPORT.is_match("import foo from \"module\""));
59    }
60
61    #[test]
62    fn test_js_require() {
63        assert!(JS_REQUIRE.is_match("require('module')"));
64        assert!(JS_REQUIRE.is_match("const x = require(\"module\")"));
65    }
66
67    #[test]
68    fn test_rust_use() {
69        assert!(RUST_USE.is_match("use std::collections::HashMap;"));
70        assert!(RUST_USE.is_match("  use crate::module;"));
71    }
72
73    #[test]
74    fn test_go_import() {
75        assert!(GO_IMPORT.is_match("import \"fmt\""));
76        assert!(GO_IMPORT.is_match("import f \"fmt\""));
77    }
78
79    #[test]
80    fn test_java_import() {
81        assert!(JAVA_IMPORT.is_match("import java.util.List;"));
82        assert!(JAVA_IMPORT.is_match("import com.example.MyClass;"));
83    }
84}