_diffctx/edges/semantic/
prisma.rs1use std::path::{Path, PathBuf};
2
3use once_cell::sync::Lazy;
4use regex::Regex;
5use rustc_hash::{FxHashMap, FxHashSet};
6
7use crate::config::weights::EDGE_WEIGHTS;
8use crate::types::Fragment;
9
10use super::super::EdgeDict;
11use super::super::base::{self, EdgeBuilder, add_edge, discover_files_by_refs};
12
13fn is_prisma_file(path: &Path) -> bool {
14 base::file_ext(path) == ".prisma"
15}
16
17fn is_prisma_consumer(path: &Path) -> bool {
18 let ext = base::file_ext(path);
19 matches!(ext.as_str(), ".ts" | ".js" | ".tsx" | ".jsx")
20}
21
22static MODEL_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*model\s+(\w+)").unwrap());
23static ENUM_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^\s*enum\s+(\w+)").unwrap());
24static TYPE_REF_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\b([A-Z]\w+)\b").unwrap());
25static CLIENT_RE: Lazy<Regex> =
26 Lazy::new(|| Regex::new(r"(?:prisma\.\w+|@prisma/client)").unwrap());
27
28static PRISMA_BUILTINS: Lazy<FxHashSet<&str>> = Lazy::new(|| {
29 [
30 "String", "Int", "Float", "Boolean", "DateTime", "Json", "Bytes", "BigInt", "Decimal",
31 ]
32 .iter()
33 .copied()
34 .collect()
35});
36
37fn extract_defs(content: &str) -> FxHashSet<String> {
38 let mut defs = FxHashSet::default();
39 defs.extend(MODEL_RE.captures_iter(content).map(|c| c[1].to_string()));
40 defs.extend(ENUM_RE.captures_iter(content).map(|c| c[1].to_string()));
41 defs
42}
43
44fn extract_type_refs(content: &str) -> FxHashSet<String> {
45 TYPE_REF_RE
46 .captures_iter(content)
47 .map(|c| c[1].to_string())
48 .filter(|t| !PRISMA_BUILTINS.contains(t.as_str()))
49 .collect()
50}
51
52pub struct PrismaEdgeBuilder;
53
54impl EdgeBuilder for PrismaEdgeBuilder {
55 fn build(&self, fragments: &[Fragment], _repo_root: Option<&Path>) -> EdgeDict {
56 let schema_frags: Vec<&Fragment> = fragments
57 .iter()
58 .filter(|f| is_prisma_file(Path::new(f.path())))
59 .collect();
60 if schema_frags.is_empty() {
61 return FxHashMap::default();
62 }
63
64 let schema_w = EDGE_WEIGHTS["prisma_schema"].forward;
65 let client_w = EDGE_WEIGHTS["prisma_client"].forward;
66 let schema_rev = EDGE_WEIGHTS["prisma_schema"].reverse_factor;
67 let client_rev = EDGE_WEIGHTS["prisma_client"].reverse_factor;
68
69 let mut name_to_defs: FxHashMap<String, Vec<_>> = FxHashMap::default();
70 for f in &schema_frags {
71 for name in extract_defs(&f.content) {
72 name_to_defs
73 .entry(name.to_lowercase())
74 .or_default()
75 .push(f.id.clone());
76 }
77 }
78
79 let mut edges: EdgeDict = FxHashMap::default();
80
81 for f in &schema_frags {
82 let self_defs = extract_defs(&f.content);
83 for tref in extract_type_refs(&f.content) {
84 if self_defs.contains(&tref) {
85 continue;
86 }
87 if let Some(targets) = name_to_defs.get(&tref.to_lowercase()) {
88 for t in targets {
89 if t != &f.id {
90 add_edge(&mut edges, &f.id, t, schema_w, schema_rev);
91 }
92 }
93 }
94 }
95 }
96
97 let consumer_frags: Vec<&Fragment> = fragments
98 .iter()
99 .filter(|f| is_prisma_consumer(Path::new(f.path())) && CLIENT_RE.is_match(&f.content))
100 .collect();
101 for cf in &consumer_frags {
102 for sf in &schema_frags {
103 if cf.id != sf.id {
104 add_edge(&mut edges, &cf.id, &sf.id, client_w, client_rev);
105 }
106 }
107 }
108
109 edges
110 }
111
112 fn discover_related_files(
113 &self,
114 changed: &[PathBuf],
115 candidates: &[PathBuf],
116 repo_root: Option<&Path>,
117 file_cache: Option<&FxHashMap<PathBuf, String>>,
118 ) -> Vec<PathBuf> {
119 let prisma_changed: Vec<&PathBuf> = changed.iter().filter(|f| is_prisma_file(f)).collect();
120 if prisma_changed.is_empty() {
121 return vec![];
122 }
123 let mut refs = FxHashSet::default();
124 for f in &prisma_changed {
125 if let Some(content) = base::read_file_cached(f, file_cache) {
126 refs.extend(extract_type_refs(&content));
127 }
128 }
129 refs.insert("prisma".to_string());
130 discover_files_by_refs(&refs, changed, candidates, repo_root)
131 }
132}