1use rayon::prelude::*;
2use serde::Serialize;
3
4use crate::git::types::FileChange;
5use crate::model::change::{ChangeType, SemanticChange};
6use crate::model::entity::SemanticEntity;
7use crate::model::identity::match_entities;
8use crate::parser::registry::ParserRegistry;
9use std::collections::{HashMap, HashSet};
10
11#[derive(Debug, Clone, Serialize)]
12#[serde(rename_all = "camelCase")]
13pub struct DiffResult {
14 pub changes: Vec<SemanticChange>,
15 pub file_count: usize,
16 pub added_count: usize,
17 pub modified_count: usize,
18 pub deleted_count: usize,
19 pub moved_count: usize,
20 pub renamed_count: usize,
21 pub reordered_count: usize,
22 pub orphan_count: usize,
23}
24
25pub fn compute_semantic_diff(
26 file_changes: &[FileChange],
27 registry: &ParserRegistry,
28 commit_sha: Option<&str>,
29 author: Option<&str>,
30) -> DiffResult {
31 let per_file_changes: Vec<(String, Vec<SemanticChange>)> = file_changes
33 .par_iter()
34 .filter_map(|file| {
35 let content_hint = file.after_content.as_deref()
36 .or(file.before_content.as_deref())
37 .unwrap_or("");
38 let resolved = registry.resolve_file_path(&file.file_path);
39 let detection_path = resolved.as_deref().unwrap_or(&file.file_path);
40 let plugin = registry.get_plugin_with_content(detection_path, content_hint)?;
41
42 let before_entities = if let Some(ref content) = file.before_content {
43 let before_path = file.old_file_path.as_deref().unwrap_or(&file.file_path);
44 let before_resolved = registry.resolve_file_path(before_path);
45 let before_detection = before_resolved.as_deref().unwrap_or(before_path);
46 match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
47 plugin.extract_entities(content, before_detection)
48 })) {
49 Ok(entities) => entities,
50 Err(_) => Vec::new(),
51 }
52 } else {
53 Vec::new()
54 };
55
56 let after_entities = if let Some(ref content) = file.after_content {
57 match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
58 plugin.extract_entities(content, detection_path)
59 })) {
60 Ok(entities) => entities,
61 Err(_) => Vec::new(),
62 }
63 } else {
64 Vec::new()
65 };
66
67 let sim_fn = |a: &crate::model::entity::SemanticEntity,
68 b: &crate::model::entity::SemanticEntity|
69 -> f64 { plugin.compute_similarity(a, b) };
70
71 let mut result = match_entities(
72 &before_entities,
73 &after_entities,
74 &file.file_path,
75 Some(&sim_fn),
76 commit_sha,
77 author,
78 );
79
80 suppress_redundant_parents(&mut result.changes, &before_entities, &after_entities);
83
84 let orphans = detect_orphan_changes(
86 file,
87 &before_entities,
88 &after_entities,
89 commit_sha,
90 author,
91 );
92 result.changes.extend(orphans);
93
94 result.changes.sort_by_key(|change| change.entity_line);
95
96 if result.changes.is_empty() {
97 None
98 } else {
99 Some((file.file_path.clone(), result.changes))
100 }
101 })
102 .collect();
103
104 let mut all_changes: Vec<SemanticChange> = Vec::new();
105 let mut files_with_changes: HashSet<String> = HashSet::new();
106 for (file_path, changes) in per_file_changes {
107 files_with_changes.insert(file_path);
108 all_changes.extend(changes);
109 }
110
111 let mut added_count = 0;
113 let mut modified_count = 0;
114 let mut deleted_count = 0;
115 let mut moved_count = 0;
116 let mut renamed_count = 0;
117 let mut reordered_count = 0;
118 let mut orphan_count = 0;
119
120 for c in &all_changes {
121 if c.entity_type == "orphan" {
122 orphan_count += 1;
123 continue;
124 }
125 match c.change_type {
126 ChangeType::Added => added_count += 1,
127 ChangeType::Modified => modified_count += 1,
128 ChangeType::Deleted => deleted_count += 1,
129 ChangeType::Moved => moved_count += 1,
130 ChangeType::Renamed => renamed_count += 1,
131 ChangeType::Reordered => reordered_count += 1,
132 }
133 }
134
135 DiffResult {
136 changes: all_changes,
137 file_count: files_with_changes.len(),
138 added_count,
139 modified_count,
140 deleted_count,
141 moved_count,
142 renamed_count,
143 reordered_count,
144 orphan_count,
145 }
146}
147
148fn suppress_redundant_parents(
155 changes: &mut Vec<SemanticChange>,
156 before: &[SemanticEntity],
157 after: &[SemanticEntity],
158) {
159 if changes.len() < 2 {
160 return;
161 }
162
163 const CONTAINER_TYPES: &[&str] = &[
164 "impl", "trait", "module", "class", "interface", "mixin",
165 "extension", "namespace", "export", "package",
166 "svelte_instance_script", "svelte_module_script",
167 ];
168
169 let before_by_id: HashMap<&str, &SemanticEntity> =
170 before.iter().map(|e| (e.id.as_str(), e)).collect();
171 let after_by_id: HashMap<&str, &SemanticEntity> =
172 after.iter().map(|e| (e.id.as_str(), e)).collect();
173
174 let mut before_children: HashMap<&str, Vec<&SemanticEntity>> = HashMap::new();
175 for e in before {
176 if let Some(ref pid) = e.parent_id {
177 before_children.entry(pid.as_str()).or_default().push(e);
178 }
179 }
180 let mut after_children: HashMap<&str, Vec<&SemanticEntity>> = HashMap::new();
181 for e in after {
182 if let Some(ref pid) = e.parent_id {
183 after_children.entry(pid.as_str()).or_default().push(e);
184 }
185 }
186
187 let changed_ids: HashSet<&str> = changes.iter().map(|c| c.entity_id.as_str()).collect();
188
189 let mut suppress: HashSet<String> = HashSet::new();
190 for change in changes.iter() {
191 if !matches!(change.change_type, ChangeType::Modified | ChangeType::Added | ChangeType::Deleted) {
192 continue;
193 }
194 if !CONTAINER_TYPES.contains(&change.entity_type.as_str()) {
195 continue;
196 }
197 let eid = change.entity_id.as_str();
198 let b_children = before_children.get(eid).map(|v| v.as_slice()).unwrap_or(&[]);
199 let a_children = after_children.get(eid).map(|v| v.as_slice()).unwrap_or(&[]);
200
201 let has_changed_child = b_children.iter().any(|c| changed_ids.contains(c.id.as_str()))
202 || a_children.iter().any(|c| changed_ids.contains(c.id.as_str()));
203 if !has_changed_child {
204 continue;
205 }
206
207 let should_suppress = if change.change_type == ChangeType::Modified {
210 match (before_by_id.get(eid), after_by_id.get(eid)) {
211 (Some(bp), Some(ap)) => {
212 let before_own = strip_children_content(&bp.content, bp.start_line, b_children);
213 let after_own = strip_children_content(&ap.content, ap.start_line, a_children);
214 before_own == after_own
215 }
216 _ => false,
217 }
218 } else {
219 true
220 };
221
222 if should_suppress {
223 suppress.insert(change.entity_id.clone());
224 }
225 }
226
227 if !suppress.is_empty() {
228 changes.retain(|c| {
229 !(matches!(c.change_type, ChangeType::Modified | ChangeType::Added | ChangeType::Deleted)
230 && suppress.contains(&c.entity_id)
231 && CONTAINER_TYPES.contains(&c.entity_type.as_str()))
232 });
233 }
234}
235
236fn strip_children_content(content: &str, parent_start_line: usize, children: &[&SemanticEntity]) -> String {
237 let lines: Vec<&str> = content.lines().collect();
238 let mut excluded: HashSet<usize> = HashSet::new();
239 for child in children {
240 let start_idx = child.start_line.saturating_sub(parent_start_line);
241 let end_idx = child.end_line.saturating_sub(parent_start_line);
242 for i in start_idx..=end_idx.max(start_idx) {
243 if i < lines.len() {
244 excluded.insert(i);
245 }
246 }
247 }
248 lines.iter().enumerate()
249 .filter(|(i, _)| !excluded.contains(i))
250 .map(|(_, l)| l.trim())
251 .filter(|l| !l.is_empty())
252 .collect::<Vec<_>>()
253 .join(" ")
254}
255
256fn detect_orphan_changes(
260 file: &FileChange,
261 before_entities: &[SemanticEntity],
262 after_entities: &[SemanticEntity],
263 commit_sha: Option<&str>,
264 author: Option<&str>,
265) -> Vec<SemanticChange> {
266 let before_text = file.before_content.as_deref().unwrap_or("");
267 let after_text = file.after_content.as_deref().unwrap_or("");
268
269 let before_covered: HashSet<usize> = before_entities
271 .iter()
272 .flat_map(|e| e.start_line..=e.end_line)
273 .collect();
274 let after_covered: HashSet<usize> = after_entities
275 .iter()
276 .flat_map(|e| e.start_line..=e.end_line)
277 .collect();
278
279 let before_orphan: String = before_text
281 .lines()
282 .enumerate()
283 .filter(|(i, _)| !before_covered.contains(&(i + 1)))
284 .map(|(_, l)| l)
285 .collect::<Vec<_>>()
286 .join("\n");
287 let after_orphan: String = after_text
288 .lines()
289 .enumerate()
290 .filter(|(i, _)| !after_covered.contains(&(i + 1)))
291 .map(|(_, l)| l)
292 .collect::<Vec<_>>()
293 .join("\n");
294
295 if before_orphan == after_orphan {
297 return Vec::new();
298 }
299
300 let change_type = if before_orphan.trim().is_empty() {
301 ChangeType::Added
302 } else if after_orphan.trim().is_empty() {
303 ChangeType::Deleted
304 } else {
305 ChangeType::Modified
306 };
307
308 vec![SemanticChange {
309 id: format!("{}::orphan", file.file_path),
310 entity_id: format!("{}::orphan", file.file_path),
311 change_type,
312 entity_type: "orphan".to_string(),
313 entity_name: "module-level".to_string(),
314 entity_line: 0,
315 parent_name: None,
316 file_path: file.file_path.clone(),
317 old_entity_name: None,
318 old_file_path: None,
319 old_parent_id: None,
320 before_content: if before_orphan.is_empty() {
321 None
322 } else {
323 Some(before_orphan)
324 },
325 after_content: if after_orphan.is_empty() {
326 None
327 } else {
328 Some(after_orphan)
329 },
330 commit_sha: commit_sha.map(String::from),
331 author: author.map(String::from),
332 timestamp: None,
333 structural_change: Some(true),
334 }]
335}
336
337#[cfg(test)]
338mod tests {
339 use super::*;
340 use crate::git::types::{FileChange, FileStatus};
341 use crate::parser::plugins::create_default_registry;
342
343 fn modified_file(path: &str, before: &str, after: &str) -> FileChange {
344 FileChange {
345 file_path: path.to_string(),
346 status: FileStatus::Modified,
347 old_file_path: None,
348 before_content: Some(before.to_string()),
349 after_content: Some(after.to_string()),
350 }
351 }
352
353 #[test]
354 fn test_parent_suppressed_when_only_child_modified() {
355 let before = "class UserService:\n def get_user(self, user_id):\n return db.find(user_id)\n";
356 let after = "class UserService:\n def get_user(self, user_id):\n return db.find(user_id, include_deleted=False)\n";
357
358 let registry = create_default_registry();
359 let result = compute_semantic_diff(&[modified_file("svc.py", before, after)], ®istry, None, None);
360
361 let names: Vec<&str> = result.changes.iter().map(|c| c.entity_name.as_str()).collect();
362 assert!(
363 result.changes.iter().any(|c| c.entity_name == "get_user"),
364 "expected method get_user in changes, got: {names:?}"
365 );
366 assert!(
367 !result.changes.iter().any(|c| c.entity_name == "UserService" && c.change_type == ChangeType::Modified),
368 "class should be suppressed when only the method body changed, got: {names:?}"
369 );
370 }
371
372 #[test]
373 fn test_parent_not_suppressed_when_own_declaration_changes() {
374 let before = "class UserService:\n def get_user(self, user_id):\n return db.find(user_id)\n";
375 let after = "class UserService(BaseService):\n def get_user(self, user_id):\n return db.find(user_id, include_deleted=False)\n";
376
377 let registry = create_default_registry();
378 let result = compute_semantic_diff(&[modified_file("svc.py", before, after)], ®istry, None, None);
379
380 let names: Vec<&str> = result.changes.iter().map(|c| c.entity_name.as_str()).collect();
381 assert!(
382 result.changes.iter().any(|c| c.entity_name == "get_user"),
383 "expected method get_user in changes, got: {names:?}"
384 );
385 assert!(
386 result.changes.iter().any(|c| c.entity_name == "UserService" && c.change_type == ChangeType::Modified),
387 "class should remain Modified when its own declaration changed, got: {names:?}"
388 );
389 }
390}