_diffctx/edges/semantic/
erlang.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, add_edges_from_ids, discover_files_by_refs};
12
13fn is_erlang_file(path: &Path) -> bool {
14 let ext = base::file_ext(path);
15 ext == ".erl" || ext == ".hrl"
16}
17
18static MODULE_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^-module\((\w+)\)").unwrap());
19static INCLUDE_RE: Lazy<Regex> =
20 Lazy::new(|| Regex::new(r#"(?m)^-include(?:_lib)?\(\s*"([^"]+)""#).unwrap());
21static BEHAVIOUR_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^-behaviou?r\((\w+)\)").unwrap());
22static IMPORT_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^-import\((\w+),").unwrap());
23static FUNC_DEF_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^([a-z]\w*)\s*\(").unwrap());
24static REMOTE_CALL_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\b([a-z]\w*):(\w+)\s*\(").unwrap());
25
26fn extract_refs(content: &str) -> FxHashSet<String> {
27 let mut refs = FxHashSet::default();
28 refs.extend(INCLUDE_RE.captures_iter(content).map(|c| c[1].to_string()));
29 refs.extend(
30 BEHAVIOUR_RE
31 .captures_iter(content)
32 .map(|c| c[1].to_string()),
33 );
34 refs.extend(IMPORT_RE.captures_iter(content).map(|c| c[1].to_string()));
35 refs.extend(
36 REMOTE_CALL_RE
37 .captures_iter(content)
38 .map(|c| c[1].to_string()),
39 );
40 refs
41}
42
43fn extract_modules(content: &str) -> FxHashSet<String> {
44 MODULE_RE
45 .captures_iter(content)
46 .map(|c| c[1].to_string())
47 .collect()
48}
49
50fn extract_func_defs(content: &str) -> FxHashSet<String> {
51 FUNC_DEF_RE
52 .captures_iter(content)
53 .map(|c| c[1].to_string())
54 .collect()
55}
56
57pub struct ErlangEdgeBuilder;
58
59impl EdgeBuilder for ErlangEdgeBuilder {
60 fn build(&self, fragments: &[Fragment], repo_root: Option<&Path>) -> EdgeDict {
61 let frags: Vec<&Fragment> = fragments
62 .iter()
63 .filter(|f| is_erlang_file(Path::new(f.path())))
64 .collect();
65 if frags.is_empty() {
66 return FxHashMap::default();
67 }
68
69 let include_w = EDGE_WEIGHTS["erlang_include"].forward;
70 let behaviour_w = EDGE_WEIGHTS["erlang_behaviour"].forward;
71 let call_w = EDGE_WEIGHTS["erlang_call"].forward;
72 let reverse_factor = EDGE_WEIGHTS["erlang_include"].reverse_factor;
73
74 let idx = base::FragmentIndex::new(fragments, repo_root);
75 let mut mod_to_frags: FxHashMap<String, Vec<_>> = FxHashMap::default();
76 let mut fn_to_frags: FxHashMap<String, Vec<_>> = FxHashMap::default();
77 for f in &frags {
78 for m in extract_modules(&f.content) {
79 mod_to_frags
80 .entry(m.to_lowercase())
81 .or_default()
82 .push(f.id.clone());
83 }
84 for name in extract_func_defs(&f.content) {
85 fn_to_frags
86 .entry(name.to_lowercase())
87 .or_default()
88 .push(f.id.clone());
89 }
90 }
91
92 let mut edges: EdgeDict = FxHashMap::default();
93
94 for f in &frags {
95 let self_fns = extract_func_defs(&f.content);
96 for cap in INCLUDE_RE.captures_iter(&f.content) {
97 base::link_by_name(&f.id, &cap[1], &idx, &mut edges, include_w, reverse_factor);
98 }
99 for cap in BEHAVIOUR_RE.captures_iter(&f.content) {
100 if let Some(targets) = mod_to_frags.get(&cap[1].to_lowercase()) {
101 add_edges_from_ids(&mut edges, &f.id, targets, behaviour_w, reverse_factor);
102 }
103 }
104 for cap in IMPORT_RE.captures_iter(&f.content) {
105 if let Some(targets) = mod_to_frags.get(&cap[1].to_lowercase()) {
106 add_edges_from_ids(&mut edges, &f.id, targets, include_w, reverse_factor);
107 }
108 }
109 for cap in REMOTE_CALL_RE.captures_iter(&f.content) {
110 let module = &cap[1];
111 if let Some(targets) = mod_to_frags.get(&module.to_lowercase()) {
112 for t in targets {
113 if t != &f.id {
114 add_edge(&mut edges, &f.id, t, call_w, reverse_factor);
115 }
116 }
117 }
118 }
119 for id in &f.identifiers {
120 if self_fns.contains(id) {
121 continue;
122 }
123 if let Some(targets) = fn_to_frags.get(&id.to_lowercase()) {
124 for t in targets {
125 if t != &f.id {
126 add_edge(&mut edges, &f.id, t, call_w, reverse_factor);
127 }
128 }
129 }
130 }
131 }
132 edges
133 }
134
135 fn discover_related_files(
136 &self,
137 changed: &[PathBuf],
138 candidates: &[PathBuf],
139 repo_root: Option<&Path>,
140 file_cache: Option<&FxHashMap<PathBuf, String>>,
141 ) -> Vec<PathBuf> {
142 let erl_changed: Vec<&PathBuf> = changed.iter().filter(|f| is_erlang_file(f)).collect();
143 if erl_changed.is_empty() {
144 return vec![];
145 }
146 let mut refs = FxHashSet::default();
147 for f in &erl_changed {
148 if let Some(content) = base::read_file_cached(f, file_cache) {
149 refs.extend(extract_refs(&content));
150 }
151 }
152 discover_files_by_refs(&refs, changed, candidates, repo_root)
153 }
154}