sem-core 0.8.0

Entity-level semantic diff engine. Extracts functions, classes, and methods from 28 languages via tree-sitter and diffs at the entity level.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use serde::Serialize;

use crate::git::types::{FileChange, FileStatus};

macro_rules! maybe_par_iter {
    ($slice:expr) => {{
        #[cfg(feature = "parallel")]
        {
            $slice.par_iter()
        }
        #[cfg(not(feature = "parallel"))]
        {
            $slice.iter()
        }
    }};
}
use crate::model::change::{ChangeType, SemanticChange};
use crate::model::entity::SemanticEntity;
use crate::model::identity::match_entities;
use crate::parser::plugin::SemanticParserPlugin;
use crate::parser::registry::ParserRegistry;
use std::collections::{HashMap, HashSet};

#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DiffResult {
    pub changes: Vec<SemanticChange>,
    pub file_count: usize,
    pub added_count: usize,
    pub modified_count: usize,
    pub deleted_count: usize,
    pub moved_count: usize,
    pub renamed_count: usize,
    pub reordered_count: usize,
    pub orphan_count: usize,
    pub total_entities_before: usize,
    pub total_entities_after: usize,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BinaryFileChange {
    pub file_path: String,
    pub status: FileStatus,
    pub old_file_path: Option<String>,
}

impl From<&FileChange> for BinaryFileChange {
    fn from(file: &FileChange) -> Self {
        Self {
            file_path: file.file_path.clone(),
            status: file.status.clone(),
            old_file_path: file.old_file_path.clone(),
        }
    }
}

pub fn collect_binary_file_changes(file_changes: &[FileChange]) -> Vec<BinaryFileChange> {
    file_changes
        .iter()
        .filter(|file| lacks_diffable_content(file))
        .map(BinaryFileChange::from)
        .collect()
}

fn lacks_diffable_content(file: &FileChange) -> bool {
    match &file.status {
        FileStatus::Added => file.after_content.is_none(),
        FileStatus::Deleted => file.before_content.is_none(),
        FileStatus::Modified | FileStatus::Renamed => {
            file.before_content.is_none() || file.after_content.is_none()
        }
    }
}

pub fn compute_semantic_diff(
    file_changes: &[FileChange],
    registry: &ParserRegistry,
    commit_sha: Option<&str>,
    author: Option<&str>,
) -> DiffResult {
    // Process files in parallel: each file's entity extraction and matching is independent
    let per_file_changes: Vec<(String, Vec<SemanticChange>, usize, usize)> =
        maybe_par_iter!(file_changes)
            .filter(|file| !lacks_diffable_content(file))
            .filter_map(|file| {
                let content_hint = file
                    .after_content
                    .as_deref()
                    .or(file.before_content.as_deref())
                    .unwrap_or("");
                let resolved = registry.resolve_file_path(&file.file_path);
                let detection_path = resolved.as_deref().unwrap_or(&file.file_path);
                let plugin = registry.get_plugin_with_content(detection_path, content_hint)?;

                let before_entities = if let Some(ref content) = file.before_content {
                    let before_path = file.old_file_path.as_deref().unwrap_or(&file.file_path);
                    let before_resolved = registry.resolve_file_path(before_path);
                    let before_detection = before_resolved.as_deref().unwrap_or(before_path);
                    match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                        plugin.extract_entities(content, before_detection)
                    })) {
                        Ok(entities) => entities,
                        Err(_) => Vec::new(),
                    }
                } else {
                    Vec::new()
                };

                let after_entities = if let Some(ref content) = file.after_content {
                    match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                        plugin.extract_entities(content, detection_path)
                    })) {
                        Ok(entities) => entities,
                        Err(_) => Vec::new(),
                    }
                } else {
                    Vec::new()
                };

                let before_count = before_entities.len();
                let after_count = after_entities.len();

                let mut result = match_entities(
                    &before_entities,
                    &after_entities,
                    &file.file_path,
                    None,
                    commit_sha,
                    author,
                );

                // Suppress parent entities whose modification is already explained
                // by child entity changes (e.g. impl blocks when methods changed).
                suppress_redundant_parents(&mut result.changes, &before_entities, &after_entities);

                // Detect orphan changes (lines that changed outside any entity span).
                let orphans = detect_orphan_changes(
                    file,
                    &before_entities,
                    &after_entities,
                    Some(plugin),
                    detection_path,
                    commit_sha,
                    author,
                );
                result.changes.extend(orphans);

                result.changes.sort_by_key(|change| change.entity_line);

                if result.changes.is_empty() {
                    None
                } else {
                    Some((
                        file.file_path.clone(),
                        result.changes,
                        before_count,
                        after_count,
                    ))
                }
            })
            .collect();

    let mut all_changes: Vec<SemanticChange> = Vec::new();
    let mut files_with_changes: HashSet<String> = HashSet::new();
    let mut total_entities_before: usize = 0;
    let mut total_entities_after: usize = 0;
    for (file_path, changes, before_count, after_count) in per_file_changes {
        files_with_changes.insert(file_path);
        all_changes.extend(changes);
        total_entities_before += before_count;
        total_entities_after += after_count;
    }

    // Single-pass counting. Orphans are first-class changes for the
    // change-type buckets, and orphan_count is cross-cutting metadata.
    let mut added_count = 0;
    let mut modified_count = 0;
    let mut deleted_count = 0;
    let mut moved_count = 0;
    let mut renamed_count = 0;
    let mut reordered_count = 0;
    let mut orphan_count = 0;

    for c in &all_changes {
        if c.entity_type == "orphan" {
            orphan_count += 1;
        }
        match c.change_type {
            ChangeType::Added => added_count += 1,
            ChangeType::Modified => modified_count += 1,
            ChangeType::Deleted => deleted_count += 1,
            ChangeType::Moved => {
                moved_count += 1;
                if c.has_content_change() {
                    modified_count += 1;
                }
            }
            ChangeType::Renamed => {
                renamed_count += 1;
                if c.has_content_change() {
                    modified_count += 1;
                }
            }
            ChangeType::Reordered => {
                reordered_count += 1;
                if c.has_content_change() {
                    modified_count += 1;
                }
            }
        }
    }

    DiffResult {
        changes: all_changes,
        file_count: files_with_changes.len(),
        added_count,
        modified_count,
        deleted_count,
        moved_count,
        renamed_count,
        reordered_count,
        orphan_count,
        total_entities_before,
        total_entities_after,
    }
}

fn suppress_redundant_parents(
    changes: &mut Vec<SemanticChange>,
    before: &[SemanticEntity],
    after: &[SemanticEntity],
) {
    if changes.len() < 2 {
        return;
    }

    const CONTAINER_TYPES: &[&str] = &[
        "impl",
        "trait",
        "module",
        "class",
        "interface",
        "protocol",
        "mixin",
        "extension",
        "namespace",
        "export",
        "package",
        "field",
        "variable",
        "svelte_instance_script",
        "svelte_module_script",
        "object",
    ];

    let before_by_id: HashMap<&str, &SemanticEntity> =
        before.iter().map(|e| (e.id.as_str(), e)).collect();
    let after_by_id: HashMap<&str, &SemanticEntity> =
        after.iter().map(|e| (e.id.as_str(), e)).collect();

    let mut before_children: HashMap<&str, Vec<&SemanticEntity>> = HashMap::new();
    for e in before {
        if let Some(ref pid) = e.parent_id {
            before_children.entry(pid.as_str()).or_default().push(e);
        }
    }
    let mut after_children: HashMap<&str, Vec<&SemanticEntity>> = HashMap::new();
    for e in after {
        if let Some(ref pid) = e.parent_id {
            after_children.entry(pid.as_str()).or_default().push(e);
        }
    }

    let changed_ids: HashSet<&str> = changes.iter().map(|c| c.entity_id.as_str()).collect();

    let mut suppress: HashSet<String> = HashSet::new();
    for change in changes.iter() {
        if !matches!(
            change.change_type,
            ChangeType::Modified | ChangeType::Added | ChangeType::Deleted
        ) {
            continue;
        }
        if !CONTAINER_TYPES.contains(&change.entity_type.as_str()) {
            continue;
        }
        let eid = change.entity_id.as_str();
        let b_children = before_children
            .get(eid)
            .map(|v| v.as_slice())
            .unwrap_or(&[]);
        let a_children = after_children.get(eid).map(|v| v.as_slice()).unwrap_or(&[]);

        let has_changed_child = b_children
            .iter()
            .any(|c| changed_ids.contains(c.id.as_str()))
            || a_children
                .iter()
                .any(|c| changed_ids.contains(c.id.as_str()));
        if !has_changed_child {
            continue;
        }

        // Added/Deleted: suppress unconditionally; the children carry the detail.
        // Modified: only suppress if the container's own declaration is unchanged
        // and the value type didn't transition.
        let should_suppress = if change.change_type == ChangeType::Modified {
            match (before_by_id.get(eid), after_by_id.get(eid)) {
                (Some(bp), Some(ap)) if bp.entity_type == ap.entity_type => {
                    let before_own = strip_children_content(&bp.content, bp.start_line, b_children);
                    let after_own = strip_children_content(&ap.content, ap.start_line, a_children);
                    before_own == after_own
                }
                _ => false,
            }
        } else {
            true
        };

        if should_suppress {
            suppress.insert(change.entity_id.clone());
        }
    }

    // Suppress an old parent that a Moved child left behind when the old
    // parent itself appears as a change — handles the parent-rename case
    // where the parent itself failed to match.
    for change in changes.iter() {
        if change.change_type == ChangeType::Moved {
            if let Some(ref old_pid) = change.old_parent_id {
                if changed_ids.contains(old_pid.as_str()) {
                    suppress.insert(old_pid.clone());
                }
            }
        }
    }

    if !suppress.is_empty() {
        changes.retain(|c| !suppress.contains(&c.entity_id));
    }

    // Drop a Moved child whose key is unchanged and whose old parent matches
    // a Renamed entity — the child only "moved" because the parent renamed.
    let renamed_before_ids: HashSet<&str> = changes
        .iter()
        .filter(|c| c.change_type == ChangeType::Renamed)
        .filter_map(|c| {
            let old_name = c.old_entity_name.as_deref()?;
            let after_entity = after_by_id.get(c.entity_id.as_str())?;
            before
                .iter()
                .find(|e| {
                    e.name == old_name
                        && e.entity_type == after_entity.entity_type
                        && e.parent_id == after_entity.parent_id
                })
                .map(|e| e.id.as_str())
        })
        .collect();

    if !renamed_before_ids.is_empty() {
        changes.retain(|c| {
            !(c.change_type == ChangeType::Moved
                && c.old_entity_name.is_none()
                && c.old_parent_id
                    .as_deref()
                    .map_or(false, |pid| renamed_before_ids.contains(pid)))
        });
    }
}

fn strip_children_content(
    content: &str,
    parent_start_line: usize,
    children: &[&SemanticEntity],
) -> String {
    let mut line_starts = vec![0];
    for (idx, ch) in content.char_indices() {
        if ch == '\n' {
            line_starts.push(idx + ch.len_utf8());
        }
    }

    let mut excluded_ranges: Vec<(usize, usize)> = Vec::new();
    for child in children {
        let start_idx = child.start_line.saturating_sub(parent_start_line);
        let end_idx = child.end_line.saturating_sub(parent_start_line);
        let search_start = line_starts.get(start_idx).copied().unwrap_or(0);
        let search_end = line_starts
            .get(end_idx.saturating_add(1))
            .copied()
            .unwrap_or(content.len())
            .min(content.len());

        if !child.content.is_empty() && search_start <= search_end {
            let search_window = &content[search_start..search_end];
            if search_window.starts_with(&child.content) {
                excluded_ranges.push((search_start, search_start + child.content.len()));
                continue;
            }

            if let Some(relative_start) = search_window.find(&child.content) {
                let start = search_start + relative_start;
                excluded_ranges.push((start, start + child.content.len()));
                continue;
            }
        }
    }

    if excluded_ranges.is_empty() {
        return normalize_content_for_parent_suppression(content);
    }

    excluded_ranges.sort_unstable();
    let mut merged_ranges: Vec<(usize, usize)> = Vec::new();
    for (start, end) in excluded_ranges {
        if let Some((_, merged_end)) = merged_ranges.last_mut() {
            if start <= *merged_end {
                *merged_end = (*merged_end).max(end);
                continue;
            }
        }
        merged_ranges.push((start, end));
    }

    let mut stripped = String::with_capacity(content.len());
    let mut cursor = 0;
    for (start, end) in merged_ranges {
        if cursor < start {
            stripped.push_str(&content[cursor..start]);
        }
        cursor = end.max(cursor);
    }
    if cursor < content.len() {
        stripped.push_str(&content[cursor..]);
    }

    normalize_content_for_parent_suppression(&stripped)
}

fn normalize_content_for_parent_suppression(content: &str) -> String {
    content
        .lines()
        .map(|l| l.trim())
        .filter(|l| !l.is_empty())
        .collect::<Vec<_>>()
        .join(" ")
}

/// Detect changes in lines that fall outside any entity span.
/// These are things like use statements, crate-level attributes, standalone
/// comments, and macro invocations that aren't tracked as entities.
fn detect_orphan_changes(
    file: &FileChange,
    before_entities: &[SemanticEntity],
    after_entities: &[SemanticEntity],
    plugin: Option<&dyn SemanticParserPlugin>,
    detection_path: &str,
    commit_sha: Option<&str>,
    author: Option<&str>,
) -> Vec<SemanticChange> {
    let before_text = file.before_content.as_deref().unwrap_or("");
    let after_text = file.after_content.as_deref().unwrap_or("");

    // Build covered line sets from entity spans
    let before_covered: HashSet<usize> = before_entities
        .iter()
        .flat_map(|e| e.start_line..=e.end_line)
        .collect();
    let after_covered: HashSet<usize> = after_entities
        .iter()
        .flat_map(|e| e.start_line..=e.end_line)
        .collect();

    let before_orphans = orphan_segments(before_text, &before_covered);
    let after_orphans = orphan_segments(after_text, &after_covered);
    let mut changes = Vec::new();

    for (before_idx, after_idx) in orphan_segment_change_pairs(&before_orphans, &after_orphans) {
        let before_orphan = before_idx.and_then(|idx| before_orphans.get(idx));
        let after_orphan = after_idx.and_then(|idx| after_orphans.get(idx));
        let before_content = orphan_content(before_orphan);
        let after_content = orphan_content(after_orphan);

        // Skip if orphan content is unchanged, including blank-only segments.
        if before_content == after_content {
            continue;
        }

        let change_type = if before_content.is_none() {
            ChangeType::Added
        } else if after_content.is_none() {
            ChangeType::Deleted
        } else {
            ChangeType::Modified
        };

        let current_orphan = match change_type {
            ChangeType::Deleted => before_orphan,
            _ => after_orphan.or(before_orphan),
        };
        let Some(current_orphan) = current_orphan else {
            continue;
        };
        let span_label = if change_type == ChangeType::Deleted {
            "oldL"
        } else {
            "L"
        };
        let orphan_id = format!(
            "{}::orphan::{}@{}{}-{}",
            file.file_path,
            change_type,
            span_label,
            current_orphan.start_line,
            current_orphan.end_line
        );

        changes.push(SemanticChange {
            id: format!("change::{orphan_id}"),
            entity_id: orphan_id,
            change_type,
            entity_type: "orphan".to_string(),
            entity_name: "module-level".to_string(),
            entity_line: current_orphan.start_line,
            start_line: current_orphan.start_line,
            end_line: current_orphan.end_line,
            old_start_line: before_orphan.map(|orphan| orphan.start_line),
            old_end_line: before_orphan.map(|orphan| orphan.end_line),
            parent_name: None,
            file_path: file.file_path.clone(),
            old_entity_name: None,
            old_file_path: None,
            old_parent_id: None,
            before_content: before_content.map(str::to_string),
            after_content: after_content.map(str::to_string),
            commit_sha: commit_sha.map(String::from),
            author: author.map(String::from),
            timestamp: None,
            structural_change: orphan_structural_change(
                before_content,
                after_content,
                plugin,
                detection_path,
            ),
        });
    }

    changes
}

fn orphan_structural_change(
    before_content: Option<&str>,
    after_content: Option<&str>,
    plugin: Option<&dyn SemanticParserPlugin>,
    detection_path: &str,
) -> Option<bool> {
    let plugin = plugin?;
    let before_hash =
        plugin.structural_hash_content(before_content.unwrap_or_default(), detection_path)?;
    let after_hash =
        plugin.structural_hash_content(after_content.unwrap_or_default(), detection_path)?;

    Some(before_hash != after_hash)
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct OrphanSegment {
    start_line: usize,
    end_line: usize,
    content: String,
}

fn orphan_segments(text: &str, covered_lines: &HashSet<usize>) -> Vec<OrphanSegment> {
    let mut segments = Vec::new();
    let mut current_start: Option<usize> = None;
    let mut current_lines: Vec<&str> = Vec::new();
    let mut last_line_number = 0;

    for (i, line) in text.lines().enumerate() {
        let line_number = i + 1;
        last_line_number = line_number;
        if covered_lines.contains(&line_number) {
            if let Some(start_line) = current_start.take() {
                segments.push(OrphanSegment {
                    start_line,
                    end_line: line_number - 1,
                    content: current_lines.join("\n"),
                });
                current_lines.clear();
            }
            continue;
        }

        current_start.get_or_insert(line_number);
        current_lines.push(line);
    }

    if let Some(start_line) = current_start {
        segments.push(OrphanSegment {
            start_line,
            end_line: last_line_number.max(start_line),
            content: current_lines.join("\n"),
        });
    }

    segments
}

fn orphan_content(segment: Option<&OrphanSegment>) -> Option<&str> {
    segment
        .map(|segment| segment.content.as_str())
        .filter(|content| !content.trim().is_empty())
}

fn orphan_segment_change_pairs(
    before: &[OrphanSegment],
    after: &[OrphanSegment],
) -> Vec<(Option<usize>, Option<usize>)> {
    let anchors = orphan_segment_lcs(before, after);
    let mut pairs = Vec::new();
    let mut before_start = 0;
    let mut after_start = 0;

    for (before_anchor, after_anchor) in anchors {
        append_orphan_gap_pairs(
            &mut pairs,
            before_start,
            before_anchor,
            after_start,
            after_anchor,
        );
        before_start = before_anchor + 1;
        after_start = after_anchor + 1;
    }

    append_orphan_gap_pairs(
        &mut pairs,
        before_start,
        before.len(),
        after_start,
        after.len(),
    );

    pairs
}

fn append_orphan_gap_pairs(
    pairs: &mut Vec<(Option<usize>, Option<usize>)>,
    before_start: usize,
    before_end: usize,
    after_start: usize,
    after_end: usize,
) {
    let before_len = before_end.saturating_sub(before_start);
    let after_len = after_end.saturating_sub(after_start);

    if before_len == after_len {
        for i in 0..before_len {
            pairs.push((Some(before_start + i), Some(after_start + i)));
        }
        return;
    }

    for i in 0..before_len {
        pairs.push((Some(before_start + i), None));
    }
    for i in 0..after_len {
        pairs.push((None, Some(after_start + i)));
    }
}

fn orphan_segment_lcs(before: &[OrphanSegment], after: &[OrphanSegment]) -> Vec<(usize, usize)> {
    let mut dp = vec![vec![0; after.len() + 1]; before.len() + 1];

    for i in (0..before.len()).rev() {
        for j in (0..after.len()).rev() {
            dp[i][j] = if orphan_segments_equal(&before[i], &after[j]) {
                dp[i + 1][j + 1] + 1
            } else {
                dp[i + 1][j].max(dp[i][j + 1])
            };
        }
    }

    let mut anchors = Vec::new();
    let mut i = 0;
    let mut j = 0;
    while i < before.len() && j < after.len() {
        if orphan_segments_equal(&before[i], &after[j]) {
            anchors.push((i, j));
            i += 1;
            j += 1;
        } else if dp[i + 1][j] >= dp[i][j + 1] {
            i += 1;
        } else {
            j += 1;
        }
    }

    anchors
}

fn orphan_segments_equal(before: &OrphanSegment, after: &OrphanSegment) -> bool {
    match (orphan_content(Some(before)), orphan_content(Some(after))) {
        (Some(before), Some(after)) => before == after,
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::git::types::{FileChange, FileStatus};
    use crate::parser::plugins::create_default_registry;

    fn modified_file(path: &str, before: &str, after: &str) -> FileChange {
        FileChange {
            file_path: path.to_string(),
            status: FileStatus::Modified,
            old_file_path: None,
            before_content: Some(before.to_string()),
            after_content: Some(after.to_string()),
        }
    }

    fn renamed_file(old_path: &str, new_path: &str, before: &str, after: &str) -> FileChange {
        FileChange {
            file_path: new_path.to_string(),
            status: FileStatus::Renamed,
            old_file_path: Some(old_path.to_string()),
            before_content: Some(before.to_string()),
            after_content: Some(after.to_string()),
        }
    }

    fn entity_span(id: &str, start_line: usize, end_line: usize) -> SemanticEntity {
        SemanticEntity {
            id: id.to_string(),
            file_path: "a.rs".to_string(),
            entity_type: "function".to_string(),
            name: id.to_string(),
            parent_id: None,
            content: String::new(),
            content_hash: String::new(),
            structural_hash: None,
            start_line,
            end_line,
            metadata: None,
        }
    }

    #[test]
    fn orphan_only_change_counts_file_and_orphan() {
        let before = "# old module comment\n\ndef value():\n    return 1\n";
        let after = "# new module comment\n\ndef value():\n    return 1\n";

        let registry = create_default_registry();
        let result = compute_semantic_diff(
            &[modified_file("app.py", before, after)],
            &registry,
            None,
            None,
        );

        assert_eq!(result.changes.len(), 1);
        assert_eq!(result.file_count, 1);
        assert_eq!(result.orphan_count, 1);
        assert_eq!(result.modified_count, 1);
        assert_eq!(result.changes[0].entity_type, "orphan");
        assert_eq!(result.changes[0].change_type, ChangeType::Modified);
        assert_eq!(result.changes[0].structural_change, Some(false));
    }

    #[test]
    fn orphan_code_change_is_structural() {
        let before = "import os\n\ndef value():\n    return 1\n";
        let after = "import sys\n\ndef value():\n    return 1\n";

        let registry = create_default_registry();
        let result = compute_semantic_diff(
            &[modified_file("app.py", before, after)],
            &registry,
            None,
            None,
        );

        assert_eq!(result.changes.len(), 1);
        assert_eq!(result.changes[0].entity_type, "orphan");
        assert_eq!(result.changes[0].change_type, ChangeType::Modified);
        assert_eq!(result.changes[0].structural_change, Some(true));
    }

    #[test]
    fn orphan_shebang_change_is_structural() {
        let before = "#!/usr/bin/env python3\ndef value():\n    return 1\n";
        let after = "#!/usr/bin/env python\ndef value():\n    return 1\n";

        let registry = create_default_registry();
        let result = compute_semantic_diff(
            &[modified_file("script", before, after)],
            &registry,
            None,
            None,
        );

        assert_eq!(result.changes.len(), 1);
        assert_eq!(result.changes[0].entity_type, "orphan");
        assert_eq!(result.changes[0].change_type, ChangeType::Modified);
        assert_eq!(result.changes[0].structural_change, Some(true));
    }

    #[test]
    fn test_parent_suppressed_when_only_child_modified() {
        let before = "class UserService:\n    def get_user(self, user_id):\n        return db.find(user_id)\n";
        let after  = "class UserService:\n    def get_user(self, user_id):\n        return db.find(user_id, include_deleted=False)\n";

        let registry = create_default_registry();
        let result = compute_semantic_diff(
            &[modified_file("svc.py", before, after)],
            &registry,
            None,
            None,
        );

        let names: Vec<&str> = result
            .changes
            .iter()
            .map(|c| c.entity_name.as_str())
            .collect();
        assert!(
            result.changes.iter().any(|c| c.entity_name == "get_user"),
            "expected method get_user in changes, got: {names:?}"
        );
        assert!(
            !result
                .changes
                .iter()
                .any(|c| c.entity_name == "UserService" && c.change_type == ChangeType::Modified),
            "class should be suppressed when only the method body changed, got: {names:?}"
        );
    }

    #[test]
    fn test_protocol_parent_suppressed_when_only_associatedtype_renamed() {
        let before = "protocol Repository {\n    associatedtype Item\n}\n";
        let after = "protocol Repository {\n    associatedtype Canvas\n}\n";

        let registry = create_default_registry();
        let result = compute_semantic_diff(
            &[modified_file("Repository.swift", before, after)],
            &registry,
            None,
            None,
        );

        let names: Vec<&str> = result
            .changes
            .iter()
            .map(|c| c.entity_name.as_str())
            .collect();
        assert!(
            result
                .changes
                .iter()
                .any(|c| c.entity_type == "associatedtype"),
            "expected associatedtype change, got: {names:?}"
        );
        assert!(
            !result
                .changes
                .iter()
                .any(|c| c.entity_name == "Repository" && c.change_type == ChangeType::Modified),
            "protocol should be suppressed when only the associatedtype changed, got: {names:?}"
        );
    }

    #[test]
    fn test_protocol_parent_not_suppressed_when_own_declaration_changes() {
        let before = "protocol Repository {\n    associatedtype Item\n}\n";
        let after = "protocol Repository: Sendable {\n    associatedtype Canvas\n}\n";

        let registry = create_default_registry();
        let result = compute_semantic_diff(
            &[modified_file("Repository.swift", before, after)],
            &registry,
            None,
            None,
        );

        let names: Vec<&str> = result
            .changes
            .iter()
            .map(|c| c.entity_name.as_str())
            .collect();
        assert!(
            result
                .changes
                .iter()
                .any(|c| c.entity_type == "associatedtype"),
            "expected associatedtype change, got: {names:?}"
        );
        assert!(
            result
                .changes
                .iter()
                .any(|c| c.entity_name == "Repository" && c.change_type == ChangeType::Modified),
            "protocol should remain Modified when its own declaration changed, got: {names:?}"
        );
    }

    #[test]
    fn test_parent_not_suppressed_when_own_declaration_changes() {
        let before = "class UserService:\n    def get_user(self, user_id):\n        return db.find(user_id)\n";
        let after  = "class UserService(BaseService):\n    def get_user(self, user_id):\n        return db.find(user_id, include_deleted=False)\n";

        let registry = create_default_registry();
        let result = compute_semantic_diff(
            &[modified_file("svc.py", before, after)],
            &registry,
            None,
            None,
        );

        let names: Vec<&str> = result
            .changes
            .iter()
            .map(|c| c.entity_name.as_str())
            .collect();
        assert!(
            result.changes.iter().any(|c| c.entity_name == "get_user"),
            "expected method get_user in changes, got: {names:?}"
        );
        assert!(
            result
                .changes
                .iter()
                .any(|c| c.entity_name == "UserService" && c.change_type == ChangeType::Modified),
            "class should remain Modified when its own declaration changed, got: {names:?}"
        );
    }

    #[test]
    fn test_nested_typescript_class_field_diff_reports_leaf_method() {
        let before = r#"class L1 {
  L2 = class {
    L3 = class {
      L4 = class {
        method() { return 1; }
      };
    };
  };
}
"#;
        let after = r#"class L1 {
  L2 = class {
    L3 = class {
      L4 = class {
        method() { return 999; }
      };
    };
  };
}
"#;

        let registry = create_default_registry();
        let result = compute_semantic_diff(
            &[modified_file("a.ts", before, after)],
            &registry,
            None,
            None,
        );

        let changes: Vec<_> = result
            .changes
            .iter()
            .map(|c| (c.entity_name.as_str(), c.entity_type.as_str()))
            .collect();
        assert!(
            result
                .changes
                .iter()
                .any(|c| c.entity_id == "a.ts::class::L1::L2::L3::L4::method"),
            "expected method leaf change, got: {changes:?}"
        );
        assert!(
            !result.changes.iter().any(|c| c.entity_type == "field"),
            "field containers should be suppressed when only a nested method changed, got: {changes:?}"
        );
    }

    #[test]
    fn test_nested_typescript_object_literal_diff_reports_leaf_method() {
        let before = r#"export const svc = {
  open(): number { return 1; },
  close(): number { return 0; },
};
"#;
        let after = r#"export const svc = {
  open(): number { return 2; },
  close(): number { return 0; },
};
"#;

        let registry = create_default_registry();
        let result = compute_semantic_diff(
            &[modified_file("service.ts", before, after)],
            &registry,
            None,
            None,
        );

        let changes: Vec<_> = result
            .changes
            .iter()
            .map(|c| (c.entity_name.as_str(), c.entity_type.as_str()))
            .collect();
        assert!(
            result
                .changes
                .iter()
                .any(|c| c.entity_id == "service.ts::variable::svc::open"),
            "expected object-literal method leaf change, got: {changes:?}"
        );
        assert!(
            !result
                .changes
                .iter()
                .any(|c| c.entity_name == "svc" && c.entity_type == "variable"),
            "variable container should be suppressed when only a nested method changed, got: {changes:?}"
        );
    }

    #[test]
    fn test_nested_typescript_object_literal_pair_diff_reports_leaf_methods() {
        let before = r#"export const svc = {
  reset: () => 1,
  flush: function() { return 0; },
};
"#;
        let after = r#"export const svc = {
  reset: () => 2,
  flush: function() { return 3; },
};
"#;

        let registry = create_default_registry();
        let result = compute_semantic_diff(
            &[modified_file("service.ts", before, after)],
            &registry,
            None,
            None,
        );

        let changes: Vec<_> = result
            .changes
            .iter()
            .map(|c| (c.entity_name.as_str(), c.entity_type.as_str()))
            .collect();
        assert!(
            result
                .changes
                .iter()
                .any(|c| c.entity_id == "service.ts::variable::svc::reset"),
            "expected arrow-valued object method change, got: {changes:?}"
        );
        assert!(
            result
                .changes
                .iter()
                .any(|c| c.entity_id == "service.ts::variable::svc::flush"),
            "expected function-valued object method change, got: {changes:?}"
        );
        assert!(
            !result
                .changes
                .iter()
                .any(|c| c.entity_name == "svc" && c.entity_type == "variable"),
            "variable container should be suppressed when only nested function-valued properties changed, got: {changes:?}"
        );
    }

    #[test]
    fn test_inline_typescript_object_literal_keeps_parent_variable_changes() {
        let before = "export const svc = { open() { return 1; }, enabled: true };\n";
        let after = "export let svc = { open() { return 2; }, enabled: false };\n";

        let registry = create_default_registry();
        let result = compute_semantic_diff(
            &[modified_file("service.ts", before, after)],
            &registry,
            None,
            None,
        );

        let changes: Vec<_> = result
            .changes
            .iter()
            .map(|c| (c.entity_name.as_str(), c.entity_type.as_str()))
            .collect();
        assert!(
            result
                .changes
                .iter()
                .any(|c| c.entity_id == "service.ts::variable::svc::open"),
            "expected nested method change, got: {changes:?}"
        );
        assert!(
            result
                .changes
                .iter()
                .any(|c| c.entity_name == "svc" && c.entity_type == "variable"),
            "parent variable change should remain visible, got: {changes:?}"
        );
    }

    #[test]
    fn renamed_file_with_edited_entity_reports_move_not_add_delete() {
        let before = "def foo():\n    return alpha + beta + gamma\n";
        let after = "def foo():\n    return one + two + three\n";

        let registry = create_default_registry();
        let result = compute_semantic_diff(
            &[renamed_file("old.py", "new.py", before, after)],
            &registry,
            None,
            None,
        );

        assert_eq!(result.added_count, 0);
        assert_eq!(result.deleted_count, 0);
        assert_eq!(result.modified_count, 1);
        assert_eq!(result.moved_count, 1);
        assert_eq!(result.changes.len(), 1);
        assert_eq!(result.changes[0].entity_name, "foo");
        assert_eq!(result.changes[0].old_file_path.as_deref(), Some("old.py"));
        assert_eq!(result.changes[0].structural_change, Some(true));
    }

    #[test]
    fn duplicate_markdown_heading_reports_first_section_modification() {
        let before = "# Same Title\n\noriginal content of section A\n\n# Same Title\n\ncontent of section B\n";
        let after = "# Same Title\n\nMODIFIED content of section A\n\n# Same Title\n\ncontent of section B\n";

        let registry = create_default_registry();
        let result = compute_semantic_diff(
            &[modified_file("doc.md", before, after)],
            &registry,
            None,
            None,
        );

        assert_eq!(result.modified_count, 1, "{:?}", result.changes);
        assert_eq!(result.changes.len(), 1, "{:?}", result.changes);

        let change = &result.changes[0];
        assert_eq!(change.change_type, ChangeType::Modified);
        assert_eq!(change.entity_name, "Same Title");
        assert_eq!(change.entity_line, 1);
        assert!(change
            .before_content
            .as_deref()
            .unwrap_or_default()
            .contains("original content of section A"));
        assert!(change
            .after_content
            .as_deref()
            .unwrap_or_default()
            .contains("MODIFIED content of section A"));
    }

    #[test]
    fn orphan_changes_count_toward_change_type_buckets() {
        let before = "def foo():\n    return 1\n\ndef bar():\n    return 2\n";
        let after = "# just a comment\n";

        let registry = create_default_registry();
        let result = compute_semantic_diff(
            &[modified_file("svc.py", before, after)],
            &registry,
            None,
            None,
        );

        assert_eq!(result.added_count, 1);
        assert_eq!(result.deleted_count, 2);
        assert_eq!(result.modified_count, 0);
        assert_eq!(result.orphan_count, 1);
        assert!(result
            .changes
            .iter()
            .any(|c| c.entity_type == "orphan" && c.change_type == ChangeType::Added));
        assert!(result.changes.iter().any(|c| {
            c.entity_type == "orphan"
                && c.change_type == ChangeType::Added
                && c.structural_change == Some(false)
        }));

        let named_bucket_total = result.added_count
            + result.modified_count
            + result.deleted_count
            + result.moved_count
            + result.renamed_count
            + result.reordered_count;
        assert_eq!(named_bucket_total, result.changes.len());
    }

    #[test]
    fn orphan_changes_use_contiguous_line_spans() {
        let file = modified_file(
            "a.rs",
            "use alpha;\nfn foo() {}\nuse beta;\nfn bar() {}\n",
            "use gamma;\nfn foo() {}\nuse delta;\nfn bar() {}\n",
        );
        let entities = vec![entity_span("foo", 2, 2), entity_span("bar", 4, 4)];

        let changes = detect_orphan_changes(&file, &entities, &entities, None, "a.rs", None, None);

        assert_eq!(changes.len(), 2);
        assert_eq!(changes[0].start_line, 1);
        assert_eq!(changes[0].end_line, 1);
        assert_eq!(changes[0].old_start_line, Some(1));
        assert_eq!(changes[0].old_end_line, Some(1));
        assert_eq!(changes[0].before_content.as_deref(), Some("use alpha;"));
        assert_eq!(changes[0].after_content.as_deref(), Some("use gamma;"));
        assert_eq!(changes[1].start_line, 3);
        assert_eq!(changes[1].end_line, 3);
        assert_eq!(changes[1].old_start_line, Some(3));
        assert_eq!(changes[1].old_end_line, Some(3));
        assert_eq!(changes[1].before_content.as_deref(), Some("use beta;"));
        assert_eq!(changes[1].after_content.as_deref(), Some("use delta;"));
    }

    #[test]
    fn blank_only_orphan_segments_are_ignored() {
        let file = modified_file("a.rs", "fn foo() {}\n", "\nfn foo() {}\n");
        let before_entities = vec![entity_span("foo", 1, 1)];
        let after_entities = vec![entity_span("foo", 2, 2)];

        let changes = detect_orphan_changes(
            &file,
            &before_entities,
            &after_entities,
            None,
            "a.rs",
            None,
            None,
        );

        assert!(changes.is_empty());
    }

    #[test]
    fn inserted_orphan_segment_does_not_modify_unchanged_later_segment() {
        let file = modified_file(
            "a.rs",
            "fn foo() {}\nuse a;\nfn bar() {}\n",
            "use x;\nfn foo() {}\nuse a;\nfn bar() {}\n",
        );
        let before_entities = vec![entity_span("foo", 1, 1), entity_span("bar", 3, 3)];
        let after_entities = vec![entity_span("foo", 2, 2), entity_span("bar", 4, 4)];

        let changes = detect_orphan_changes(
            &file,
            &before_entities,
            &after_entities,
            None,
            "a.rs",
            None,
            None,
        );

        assert_eq!(changes.len(), 1);
        assert_eq!(changes[0].change_type, ChangeType::Added);
        assert_eq!(changes[0].start_line, 1);
        assert_eq!(changes[0].end_line, 1);
        assert!(changes[0].old_start_line.is_none());
        assert_eq!(changes[0].before_content, None);
        assert_eq!(changes[0].after_content.as_deref(), Some("use x;"));
    }

    #[test]
    fn uneven_orphan_gaps_are_not_forced_into_modifications() {
        let file = modified_file(
            "a.rs",
            "use a;\nfn foo() {}\nuse old;\nfn mid() {}\nuse c;\nfn bar() {}\n",
            "use a;\nfn foo() {}\nuse new1;\nfn mid() {}\nuse new2;\nfn baz() {}\nuse c;\nfn bar() {}\n",
        );
        let before_entities = vec![
            entity_span("foo", 2, 2),
            entity_span("mid", 4, 4),
            entity_span("bar", 6, 6),
        ];
        let after_entities = vec![
            entity_span("foo", 2, 2),
            entity_span("mid", 4, 4),
            entity_span("baz", 6, 6),
            entity_span("bar", 8, 8),
        ];

        let changes = detect_orphan_changes(
            &file,
            &before_entities,
            &after_entities,
            None,
            "a.rs",
            None,
            None,
        );

        assert_eq!(changes.len(), 3);
        assert_eq!(changes[0].change_type, ChangeType::Deleted);
        assert!(changes[0].entity_id.contains("::deleted@oldL3-3"));
        assert_eq!(changes[0].before_content.as_deref(), Some("use old;"));
        assert_eq!(changes[1].change_type, ChangeType::Added);
        assert_eq!(changes[1].after_content.as_deref(), Some("use new1;"));
        assert_eq!(changes[2].change_type, ChangeType::Added);
        assert_eq!(changes[2].after_content.as_deref(), Some("use new2;"));
    }
}