moesniper 0.7.10

Escape-proof precision file editor for LLM agents. Hex-encoded content, line-range splicing, atomic writes.
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
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
//! Indentation detection and auto-correction for sniper.
//!
//! Handles "stupid indentation" — LLM-generated code with inconsistent spacing,
//! mixed tabs/spaces, off-by-one anomalies, and missing context.
//!
//! Key design decisions for resilience:
//! - Statistical mode, not min: finds the most common indent step, not just the smallest.
//! - Supermajority for tabs vs spaces: 80% threshold prevents one rogue tab from flipping.
//! - Backward scan with anomaly rejection: walks back up to 20 lines to find a reliable
//!   context line, skipping lines whose indent is not a multiple of the detected step.
//! - Brace/colon/continuation awareness: `{`, `}` , `:`, `(`, `[` all affect expected level.
//! - Round-to-nearest for off-by-one: 7 spaces in a 4-space file rounds to level 2, not 1.

use std::collections::HashMap;

/// Represents the indentation style detected from context.
#[derive(Debug, Clone, PartialEq)]
struct IndentStyle {
    /// Number of spaces per indent level (0 if using tabs).
    pub spaces: usize,
    /// Whether this file uses tab indentation.
    pub uses_tabs: bool,
    /// Visual width of one indent level.
    pub width: usize,
}

impl Default for IndentStyle {
    fn default() -> Self {
        Self {
            spaces: 4,
            uses_tabs: false,
            width: 4,
        }
    }
}

impl IndentStyle {
    /// Returns the indentation string for a given nesting level.
    pub fn indent_string(&self, level: usize) -> String {
        if self.uses_tabs {
            "\t".repeat(level)
        } else {
            " ".repeat(self.spaces * level)
        }
    }
}

// ————————————————————————————————————————————————————————————————————————————————
// detect_indent_style — robust statistical detection
// ————————————————————————————————————————————————————————————————————————————————

const SUPERMAJORITY_RATIO: f64 = 0.80;

/// Detects indentation style using statistical mode of indent step sizes.
/// Uses supermajority threshold for tabs-vs-spaces decision.
/// Samples all non-empty lines for accuracy (not just first 20).
fn detect_indent_style(lines: &[String]) -> IndentStyle {
    let mut space_levels: Vec<usize> = Vec::new();
    let mut tab_levels: Vec<usize> = Vec::new();

    for line in lines.iter() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }

        let leading_spaces = line.chars().take_while(|c| *c == ' ').count();
        let leading_tabs = line.chars().take_while(|c| *c == '\t').count();

        if leading_tabs > 0 && leading_spaces == 0 {
            tab_levels.push(leading_tabs);
        } else if leading_spaces > 0 && leading_tabs == 0 {
            space_levels.push(leading_spaces);
        }
        // Mixed leading whitespace (both tabs and spaces) — discard as anomalous
    }

    let total_indented = tab_levels.len() + space_levels.len();
    if total_indented == 0 {
        return IndentStyle::default();
    }

    // Supermajority check: don't flip to tabs unless >80% of indented lines use tabs
    if tab_levels.len() as f64 / total_indented as f64 > SUPERMAJORITY_RATIO {
        // Tab mode: width is fixed at the most common tab count
        let mode_width = mode_usize(&tab_levels).unwrap_or(1).max(1);
        return IndentStyle {
            spaces: 0,
            uses_tabs: true,
            width: mode_width,
        };
    }

    // Space mode: find the indent step with frequency-weighted scoring
    let indent_step = detect_space_step(&space_levels);
    IndentStyle {
        spaces: indent_step,
        uses_tabs: false,
        width: indent_step,
    }
}

/// Find the most common indentation step size from a list of space counts.
///
/// Each candidate step is scored by: how many levels are clean multiples
/// of this step, weighted by frequency. Higher steps are preferred on ties
/// (4-space beats 2-space when both are equally plausible).
fn detect_space_step(levels: &[usize]) -> usize {
    if levels.is_empty() {
        return 4;
    }

    // Count frequency of each distinct space count
    let mut freq: HashMap<usize, usize> = HashMap::new();
    for &l in levels {
        *freq.entry(l).or_default() += 1;
    }

    // Candidate steps to test: each distinct non-zero space count,
    // plus each difference between distinct counts
    let mut candidates: Vec<usize> = freq.keys().copied().filter(|&k| k > 0).collect();
    candidates.sort_unstable();
    let initial_len = candidates.len();
    for i in 0..initial_len.saturating_sub(1) {
        let d = candidates[i + 1] - candidates[i];
        if d > 0 {
            candidates.push(d);
        }
    }
    // Always include 4 as a fallback candidate
    candidates.push(4);
    candidates.sort_unstable();
    candidates.dedup();

    // Score: raw frequency dominates. On tie, SMALLER step wins
    // (indent step is the base unit, not the deepest nesting level).
    let mut best_step = 4;
    let mut best_score: i64 = -1;

    for &candidate in &candidates {
        if !(2..=16).contains(&candidate) {
            continue;
        }
        let raw_freq = freq.get(&candidate).copied().unwrap_or(0) as i64;
        let score = raw_freq * 1000 - candidate as i64;

        if score > best_score {
            best_score = score;
            best_step = candidate;
        }
    }

    best_step
}

fn mode_usize(values: &[usize]) -> Option<usize> {
    let mut counts: HashMap<usize, usize> = HashMap::new();
    for v in values {
        *counts.entry(*v).or_default() += 1;
    }
    counts.into_iter().max_by_key(|(_, c)| *c).map(|(v, _)| v)
}

// ————————————————————————————————————————————————————————————————————————————————
// detect_expected_indent — robust context scanner
// ————————————————————————————————————————————————————————————————————————————————

const MAX_SCAN_BACK: usize = 20;

/// Detects the expected indentation level at the splice site by scanning
/// backwards through up to 20 preceding lines for a reliable context line.
///
/// A line is "reliable" if its leading whitespace is a multiple of the indent
/// step (no off-by-one anomalies) and it is not a closing brace on its own.
///
/// Handles: `{` (+1 level), `}` (−1 level for content after), `:` (+1),
/// `(` and `[` (continuation: +1 level for next line).
fn detect_expected_indent(all_lines: &[String], start_line: usize) -> (IndentStyle, usize) {
    let idx = start_line.saturating_sub(1).min(all_lines.len());
    let window_start = idx.saturating_sub(MAX_SCAN_BACK);
    let window = &all_lines[window_start..idx];

    // Count indented lines in the backward window to assess signal strength.
    let indented_in_window = window
        .iter()
        .filter(|line| {
            let trimmed = line.trim();
            !trimmed.is_empty()
                && (line.chars().take_while(|c| *c == ' ').count() > 0
                    || line.chars().take_while(|c| *c == '\t').count() > 0)
        })
        .count();

    // For style detection, use a broader context to avoid module-docstring bias.
    // When the backward window is at the file top AND has weak signal (< 2 indented lines),
    // include lines AFTER the edit point so we see actual code structure.
    let style = if window_start == 0 && all_lines.len() > idx && indented_in_window < 2 {
        // At file top with weak backward signal: use forward context for style detection
        let forward_end = (idx + MAX_SCAN_BACK).min(all_lines.len());
        let mut combined = Vec::with_capacity(window.len() + forward_end - idx);
        combined.extend_from_slice(window);
        combined.extend_from_slice(&all_lines[idx..forward_end]);
        if combined.len() >= 3 {
            detect_indent_style(&combined)
        } else {
            detect_indent_style(all_lines)
        }
    } else if window.len() >= 3 {
        detect_indent_style(window)
    } else {
        detect_indent_style(all_lines)
    };

    let step = if style.uses_tabs {
        1
    } else {
        style.spaces.max(1)
    };

    let s = start_line.saturating_sub(1);
    let scan_start = s.saturating_sub(MAX_SCAN_BACK);

    // Walk backwards to find the best context line
    let mut best_level: Option<usize> = None;
    let mut best_quality: i32 = -1; // higher = better

    for i in (scan_start..s).rev() {
        if i >= all_lines.len() {
            continue;
        }
        let line = &all_lines[i];
        if line.trim().is_empty() {
            continue;
        }

        let leading = count_leading_whitespace(line);
        let quality = context_quality(leading, step, line);

        if quality > best_quality {
            let level = round_to_nearest_level(leading, step);
            best_level = Some(level);
            best_quality = quality;

            // Perfect match: indent is an exact multiple — stop scanning
            if leading.is_multiple_of(step) {
                break;
            }
        }
    }

    let mut context_level = best_level.unwrap_or(0);

    // Adjust for block starters/enders on the context line
    if let Some(line) = context_line_before(all_lines, start_line) {
        let trimmed = line.trim_end();
        let trimmed_no_comment = strip_trailing_comment(trimmed);

        // Block/continuation opener: `{`, `:`, `(`, `[` → expect indent increase.
        //
        // For `{` we find the LAST brace-like character on the line rather than
        // checking ends_with.  This handles `fn foo() { something();` where `{`
        // is not at end-of-line but still opens a block for lines below.
        // `fn foo() {}` (last brace is `}`) is correctly NOT treated as opener.
        let last_brace_pos = trimmed_no_comment.rfind(['{', '}']);
        let opens_block = last_brace_pos.is_some_and(|pos| {
            trimmed_no_comment.as_bytes()[pos] == b'{'
        });
        if opens_block
            || trimmed_no_comment.ends_with(':')
            || trimmed_no_comment.ends_with('(')
            || trimmed_no_comment.ends_with('[')
        {
            context_level += 1;
        }

        // Block closer: line starts with `}` → subsequent content should be at this level
        // (Already handled: we used `}`-line's own indent as the context level,
        //  which IS the correct level for content after the closing brace.)

        // Ensure context_level is at least the level of the line immediately
        // before the insert point — the backward scan may have found a
        // shallower structural line further up, but the adjacent line is
        // the most direct context.
        let clb_leading = count_leading_whitespace(line);
        let clb_level = round_to_nearest_level(clb_leading, step);
        context_level = context_level.max(clb_level);
    }

    // If there is a line at the insert position and it is indented deeper
    // than the detected context, use its level — the line being replaced
    // is the strongest signal of the expected indent.  Only applies when
    // the backward scan found structural context (best_level is set).
    if best_level.is_some() && s < all_lines.len() && !all_lines[s].trim().is_empty() {
        let at_line = &all_lines[s];
        let at_leading = count_leading_whitespace(at_line);
        let at_level = round_to_nearest_level(at_leading, step);
        context_level = context_level.max(at_level);
    }

    (style, context_level)
}

/// Returns the raw content of the last non-empty line before start_line.
fn context_line_before(all_lines: &[String], start_line: usize) -> Option<&String> {
    let s = start_line.saturating_sub(1);
    for i in (0..s).rev() {
        if i >= all_lines.len() {
            continue;
        }
        if !all_lines[i].trim().is_empty() {
            return Some(&all_lines[i]);
        }
    }
    None
}

/// Quality score for a context line. Higher = more reliable.
/// - Exact multiple of step: +10 (ideal)
/// - Off by 1 or 2: +5 (slightly anomalous, still usable)
/// - Block opener/closer line: +0 (reliable but may need level adjustment)
/// - Deeply indented lines: bonus (more structural, harder to be LLM noise)
#[allow(clippy::cast_possible_truncation)] // .min(5) guarantees value fits in i32
fn context_quality(leading: usize, step: usize, _line: &str) -> i32 {
    if step == 0 {
        return 0;
    }
    let remainder = leading % step;
    let base = if remainder == 0 {
        10
    } else if remainder <= 2 {
        5
    } else {
        0
    };
    // Bonus for deeper indentation: more reliable than top-level lines
    let depth_bonus = (leading / step).min(5) as i32;
    base + depth_bonus
}

/// Rounds a whitespace count to the nearest indent level.
/// 7 spaces in a 4-space file → level 2 (7 rounds to 8, 8/4 = 2).
fn round_to_nearest_level(leading: usize, step: usize) -> usize {
    if step == 0 {
        return 0;
    }
    let exact = leading / step;
    let remainder = leading % step;
    if remainder > step / 2 {
        exact + 1
    } else {
        exact
    }
}

fn count_leading_whitespace(line: &str) -> usize {
    line.chars().take_while(|c| c.is_whitespace()).count()
}

fn strip_trailing_comment(s: &str) -> &str {
    if let Some(pos) = s.find("//") {
        return &s[..pos];
    }
    if let Some(pos) = s.find('#') {
        return &s[..pos];
    }
    s
}

// ————————————————————————————————————————————————————————————————————————————————
// validate_indentation
// ————————————————————————————————————————————————————————————————————————————————

/// Validates that replacement lines match the detected indentation style.
pub fn validate_indentation(
    all_lines: &[String],
    start_line: usize,
    _end_line: usize,
    replacement_lines: &[String],
) -> (bool, Option<String>, Option<String>) {
    let (style, expected_level) = detect_expected_indent(all_lines, start_line);
    let expected_indent = style.indent_string(expected_level);

    let mut has_content = false;
    let mut min_leading = usize::MAX;

    for line in replacement_lines.iter().filter(|l| !l.trim().is_empty()) {
        has_content = true;
        let trimmed = line.trim_start();
        if trimmed.starts_with(')') || trimmed.starts_with('}') || trimmed.starts_with(']') {
            continue;
        }
        let leading = line.chars().take_while(|c| c.is_whitespace()).count();
        min_leading = min_leading.min(leading);
    }

    if !has_content {
        return (true, None, None);
    }

    let expected_spaces = expected_indent.len();
    let style_desc = if style.uses_tabs {
        format!("{} tab(s)", expected_level)
    } else {
        format!("{} space(s)", expected_spaces)
    };

    if min_leading < expected_spaces {
        let diff = expected_spaces - min_leading;
        let warning = format!(
            "INDENTATION WARNING: Replacement has {} leading {}, expected {} (diff: {})",
            min_leading,
            if style.uses_tabs {
                "tab(s)"
            } else {
                "space(s)"
            },
            style_desc,
            diff
        );

        let fix = replacement_lines
            .iter()
            .map(|line| {
                if line.trim().is_empty() {
                    line.clone()
                } else {
                    let stripped = line.trim_start();
                    format!("{}{}", expected_indent, stripped)
                }
            })
            .collect::<Vec<_>>()
            .join("\n");

        (false, Some(warning), Some(fix))
    } else {
        (true, None, None)
    }
}

// ————————————————————————————————————————————————————————————————————————————————
// auto_indent_content
// ————————————————————————————————————————————————————————————————————————————————

/// Adjusts indentation of content to match the surrounding context.
///
/// Finds the minimum leading whitespace in the content, computes the delta to
/// the expected indent from context, and shifts all content lines by that delta.
/// This preserves internal indentation structure (multi-level content) while
/// fixing the base level.
///
/// If the content is already at or above the expected indent level (i.e. the
/// LLM sent correctly indented content), the content is returned unchanged.
pub fn auto_indent_content(
    all_lines: &[String],
    start_line: usize,
    _end_line: usize,
    content: &str,
) -> String {
    let (style, expected_level) = detect_expected_indent(all_lines, start_line);
    let expected_indent = style.indent_string(expected_level);

    if expected_indent.is_empty() {
        return content.to_string();
    }

    let content_lines: Vec<&str> = content.lines().collect();

    // Compute minimum leading whitespace, skipping closer tokens
    // (}, ), ]) — closer tokens are at a different indent level by design
    // and should not drag down the min_leading for body content.
    // This mirrors validate_indentation's closer-token skip.
    let is_closer = |l: &&str| -> bool {
        let t = l.trim();
        t.starts_with('}') || t.starts_with(')') || t.starts_with(']')
    };
    let min_leading = content_lines
        .iter()
        .filter(|l| !l.trim().is_empty() && !is_closer(l))
        .map(|l| l.chars().take_while(|c| c.is_whitespace()).count())
        .min()
        .unwrap_or(0);

    // If content starts with a closer token (}, ), ]), the whole block
    // should be at one level less indentation than body content — the
    // closer closes the current block, so content after it is at the
    // block level.  This mirrors validate_indentation's closer skip.
    let first_nonempty = content_lines.iter().find(|l| !l.trim().is_empty());
    let starts_with_closer = first_nonempty.is_some_and(|l| {
        let t = l.trim();
        t.starts_with('}') || t.starts_with(')') || t.starts_with(']')
    });
    let effective_level = if starts_with_closer {
        expected_level.saturating_sub(1)
    } else {
        expected_level
    };
    let effective_indent = style.indent_string(effective_level);

    if min_leading >= effective_indent.len() {
        return content.to_string();
    }

    content_lines
        .iter()
        .map(|line| {
            if line.trim().is_empty() {
                (*line).to_string()
            } else {
                let leading = line.chars().take_while(|c| c.is_whitespace()).count();
                let overhang = leading - min_leading;
                let indent_char = if style.uses_tabs { '\t' } else { ' ' };
                // BUG FIX: base indent uses style char, overhang is always spaces
                let base = indent_char.to_string().repeat(effective_indent.len());
                let overhang_spaces = " ".repeat(overhang);
                format!(
                    "{}{}{}",
                    base,
                    overhang_spaces,
                    line.trim_start()
                )
            }
        })
        .collect::<Vec<_>>()
        .join("\n")
}

// ————————————————————————————————————————————————————————————————————————————————
// needs_indent_fix
// ————————————————————————————————————————————————————————————————————————————————

/// Returns true if the content's minimum indentation is less than expected.
pub fn needs_indent_fix(
    all_lines: &[String],
    start_line: usize,
    _end_line: usize,
    content: &str,
) -> bool {
    let (style, expected_level) = detect_expected_indent(all_lines, start_line);
    let expected_indent = style.indent_string(expected_level);

    if expected_indent.is_empty() {
        return false;
    }

    // Skip closer tokens for min_leading (same rationale as auto_indent_content)
    let is_closer = |l: &&str| -> bool {
        let t = l.trim();
        t.starts_with('}') || t.starts_with(')') || t.starts_with(']')
    };
    let min_leading = content
        .lines()
        .filter(|l| !l.trim().is_empty() && !is_closer(l))
        .map(|l| l.chars().take_while(|c| c.is_whitespace()).count())
        .min()
        .unwrap_or(0);

    min_leading < expected_indent.len()
}

// ————————————————————————————————————————————————————————————————————————————————
// tests
// ————————————————————————————————————————————————————————————————————————————————

#[cfg(test)]
mod tests {
    use super::*;

    // ============================================================
    // detect_indent_style — statistical mode detection
    // ============================================================

    #[test]
    fn test_detect_spaces_indent() {
        let lines = vec![
            "    def foo():".to_string(),
            "        pass".to_string(),
            "    def bar():".to_string(),
        ];
        let style = detect_indent_style(&lines);
        assert_eq!(style.spaces, 4);
        assert!(!style.uses_tabs);
    }

    #[test]
    fn test_detect_tabs_indent() {
        let lines: Vec<String> = (0..50).map(|_| "\tfn foo() {".to_string()).collect();
        let style = detect_indent_style(&lines);
        assert!(style.uses_tabs);
    }

    #[test]
    fn test_detect_2_space_indent() {
        let lines = vec!["  def foo():".to_string(), "    pass".to_string()];
        let style = detect_indent_style(&lines);
        assert_eq!(style.spaces, 2);
    }

    #[test]
    fn test_detect_8_space_indent() {
        let lines = vec![
            "        fn main() {".to_string(),
            "                println!();".to_string(),
        ];
        let style = detect_indent_style(&lines);
        assert_eq!(style.spaces, 8);
    }

    #[test]
    fn test_detect_indent_empty_file_defaults_spaces() {
        let lines: Vec<String> = vec![];
        let style = detect_indent_style(&lines);
        assert_eq!(style.spaces, 4);
        assert!(!style.uses_tabs);
    }

    #[test]
    fn test_supermajority_one_tab_does_not_flip_space_file() {
        // 1 tab line in 29 space lines — should NOT flip to tabs
        let mut lines: Vec<String> = (0..29).map(|_| "    fn foo() {".to_string()).collect();
        lines.push("\tfn rogue() {}".to_string());
        let style = detect_indent_style(&lines);
        assert!(
            !style.uses_tabs,
            "Single rogue tab should not flip a space-indented file"
        );
    }

    #[test]
    fn test_detect_step_from_level_differences() {
        let lines = vec![
            "class Foo:".to_string(),
            "    def a(self):".to_string(),
            "        pass".to_string(),
            "    def b(self):".to_string(),
            "        return 1".to_string(),
        ];
        let style = detect_indent_style(&lines);
        assert_eq!(style.spaces, 4);
    }

    #[test]
    fn test_detect_indent_with_anomalous_spacing() {
        // LLM might produce 0, 3, 7, 4, 8 spaces — mode of diffs should still be ~4
        let lines = vec![
            "fn main() {".to_string(),
            "   let x = 1;".to_string(),      // 3 spaces (anomaly)
            "    let y = 2;".to_string(),     // 4 spaces
            "       let z = 3;".to_string(),  // 7 spaces (anomaly)
            "        let w = 4;".to_string(), // 8 spaces
        ];
        let style = detect_indent_style(&lines);
        assert!(!style.uses_tabs);
        // Diffs: 3-0=3, 4-3=1, 7-4=3, 8-7=1. Most common diff not reliable.
        // GCD of [1,3] = 1 → falls to 4. Still acceptable.
        assert!(style.spaces >= 1);
    }

    // ============================================================
    // detect_expected_indent — robust context scanner
    // ============================================================

    #[test]
    fn test_expected_indent_after_colon() {
        let all_lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        let (_, level) = detect_expected_indent(&all_lines, 2);
        assert_eq!(level, 1);
    }

    #[test]
    fn test_expected_indent_after_brace() {
        let all_lines = vec!["fn main() {\n".to_string(), "    let x = 1;\n".to_string()];
        let (_, level) = detect_expected_indent(&all_lines, 2);
        assert_eq!(level, 1, "Content after `{{` should be indent level 1");
    }

    /// G1 edge: `{` not at end-of-line still opens a block for subsequent lines.
    #[test]
    fn test_expected_indent_after_brace_not_at_eol() {
        // `{` is not the last character — body starts on same line but block
        // extends below.  The `{` should still increment the expected level.
        let all_lines = vec![
            "fn foo() { let x = 1;\n".to_string(),
            "    let y = 2;\n".to_string(),
        ];
        let (_, level) = detect_expected_indent(&all_lines, 2);
        assert_eq!(level, 1, "`{{` not at EOL still opens a block for next line");
    }

    /// G1 edge: self-contained `fn foo() {}` — brace opens AND closes on same line,
    /// so the next line should NOT be indented (block is already closed).
    #[test]
    fn test_expected_indent_self_contained_block_no_indent() {
        let all_lines = vec![
            "fn foo() {}\n".to_string(),
            "fn bar() {\n".to_string(),
        ];
        let (_, level) = detect_expected_indent(&all_lines, 2);
        // `fn foo() {}` has last brace as `}` → no increment.
        // `fn bar() {` on line 2 is at file top (only 1 prior non-empty)
        // → context_level derived from line 1's indent (level 0).
        assert_eq!(level, 0, "Self-contained block on prior line must not increment level");
    }

    #[test]
    fn test_expected_indent_after_closing_brace() {
        let all_lines = vec![
            "fn outer() {\n".to_string(),
            "    fn inner() {\n".to_string(),
            "        pass\n".to_string(),
            "    }\n".to_string(),
            "    // editing here\n".to_string(),
        ];
        let (_, level) = detect_expected_indent(&all_lines, 5);
        assert_eq!(
            level, 1,
            "Content after `}}` at outer level should be level 1, not 2"
        );
    }

    #[test]
    fn test_expected_indent_top_of_file() {
        let all_lines = vec!["fn main() {\n".to_string(), "    let x = 1;\n".to_string()];
        let (_, level) = detect_expected_indent(&all_lines, 1);
        assert_eq!(level, 0);
    }

    #[test]
    fn test_expected_indent_out_of_bounds() {
        let all_lines = vec!["fn main() {\n".to_string(), "    let x = 1;\n".to_string()];
        let (_, level) = detect_expected_indent(&all_lines, 10);
        assert_eq!(level, 1);
    }

    #[test]
    fn test_expected_indent_empty_lines() {
        let all_lines: Vec<String> = vec![];
        let (style, level) = detect_expected_indent(&all_lines, 1);
        assert_eq!(level, 0);
        assert_eq!(style.spaces, 4);
        assert!(!style.uses_tabs);
    }

    #[test]
    fn test_expected_indent_start_line_zero() {
        let all_lines = vec!["fn main() {\n".to_string(), "    let x = 1;\n".to_string()];
        let (_, level) = detect_expected_indent(&all_lines, 0);
        assert_eq!(level, 0);
    }

    #[test]
    fn test_expected_indent_deeply_nested() {
        let all_lines = vec![
            "class Foo:\n".to_string(),
            "    def bar(self):\n".to_string(),
            "        if True:\n".to_string(),
            "            pass\n".to_string(),
        ];
        let (_, level) = detect_expected_indent(&all_lines, 4);
        assert_eq!(level, 3);
    }

    #[test]
    fn test_expected_indent_skips_blank_lines() {
        let all_lines = vec![
            "fn outer() {\n".to_string(),
            "    let x = 1;\n".to_string(),
            "\n".to_string(),
            "\n".to_string(),
            "    // editing here\n".to_string(),
        ];
        let (_, level) = detect_expected_indent(&all_lines, 5);
        assert_eq!(level, 1, "Blank lines must be skipped to find real context");
    }

    #[test]
    fn test_expected_indent_one_misindented_line_does_not_poison() {
        let all_lines = vec![
            "fn main() {\n".to_string(),
            "    let a = 1;\n".to_string(),
            "  let bad = 2;\n".to_string(),
            "    let b = 3;\n".to_string(),
            "    let c = 4;\n".to_string(),
            "    let d = 5;\n".to_string(),
            "    let e = 6;\n".to_string(),
            "  // editing here\n".to_string(),
        ];
        let (_, level) = detect_expected_indent(&all_lines, 8);
        assert_eq!(
            level, 1,
            "A single misindented line must not poison context detection"
        );
    }

    #[test]
    fn test_expected_indent_off_by_one_rounding() {
        let all_lines = vec![
            "fn main() {\n".to_string(),
            "   let x = 1;\n".to_string(), // 3 spaces, should be 4
        ];
        let (_, level) = detect_expected_indent(&all_lines, 2);
        assert_eq!(
            level, 1,
            "3 spaces in a 4-space file should round to level 1, not 0"
        );
    }

    #[test]
    fn test_expected_indent_scan_back_multiple_lines() {
        // The immediate preceding line is blank+misindented, but a good line is further back
        let all_lines = vec![
            "fn main() {\n".to_string(),
            "    let a = 1;\n".to_string(),
            "    let b = 2;\n".to_string(),
            "     let c = 3;\n".to_string(), // 5 spaces — anomaly
            "\n".to_string(),                // blank
            "// editing here\n".to_string(),
        ];
        let (_, level) = detect_expected_indent(&all_lines, 6);
        assert_eq!(
            level, 1,
            "Must scan past blank+anomaly to find the real context"
        );
    }

    #[test]
    fn test_indent_step_from_window_not_global_file() {
        let mut lines: Vec<String> = vec![
            "fn main() {\n".to_string(),
            "    let x = 1;\n".to_string(),
            "    let y = 2;\n".to_string(),
        ];
        for _ in 0..100 {
            lines.push("        deep_body();\n".to_string());
        }
        lines.push("    // editing here\n".to_string());

        let (style, _) = detect_expected_indent(&lines, 5);
        assert_eq!(
            style.spaces, 4,
            "Edit near 4-space context should detect 4-space step, not 8 (deep body bias)"
        );
    }

    #[test]
    fn test_expected_indent_after_trailing_comma() {
        let mut all_lines: Vec<String> = (0..20).map(|_| "    let x = 1;\n".to_string()).collect();
        all_lines.push("    my_function(\n".to_string());
        all_lines.push("        arg1,\n".to_string());
        all_lines.push("        arg2,\n".to_string());
        let (_, level) = detect_expected_indent(&all_lines, 24);
        assert_eq!(
            level, 2,
            "Comma means same-level continuation, not deeper indent"
        );
    }

    // ============================================================
    // auto_indent_content
    // ============================================================

    #[test]
    fn test_auto_indent_unindented_content() {
        let all_lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        let content = "print('hello')";
        let fixed = auto_indent_content(&all_lines, 2, 2, content);
        assert_eq!(fixed, "    print('hello')");
    }

    #[test]
    fn test_auto_indent_strips_existing_whitespace() {
        let all_lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        let content = "  print('hello')";
        let fixed = auto_indent_content(&all_lines, 2, 2, content);
        assert_eq!(fixed, "    print('hello')");
    }

    #[test]
    fn test_auto_indent_multiline_content() {
        let all_lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        let content = "print('hello')\nprint('world')";
        let fixed = auto_indent_content(&all_lines, 2, 2, content);
        assert_eq!(fixed, "    print('hello')\n    print('world')");
    }

    #[test]
    fn test_auto_indent_preserves_blank_lines() {
        let all_lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        let content = "print('a')\n\nprint('b')";
        let fixed = auto_indent_content(&all_lines, 2, 2, content);
        assert_eq!(fixed, "    print('a')\n\n    print('b')");
    }

    #[test]
    fn test_auto_indent_mixed_existing_indent() {
        let all_lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        let content = "  line1\n    line2\nline3";
        let fixed = auto_indent_content(&all_lines, 2, 2, content);
        // min_leading=0, delta=4. Overhangs preserved: (2-0=2→6), (4-0=4→8), (0-0=0→4)
        assert_eq!(fixed, "      line1\n        line2\n    line3");
    }

    #[test]
    fn test_auto_indent_preserves_multilevel_structure() {
        let all_lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        // Content already has correct base indent with internal nesting — leave it alone
        let content = "    let x = 1;\n        if true {\n            run();\n        }\n    }";
        let fixed = auto_indent_content(&all_lines, 2, 2, content);
        assert_eq!(fixed, content);
    }

    #[test]
    fn test_auto_indent_shifts_underindented_multilevel() {
        let all_lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        // Content has internal structure but base indent is too shallow (2 spaces vs 4)
        let content = "  let x = 1;\n      if true {\n          run();\n      }\n  }";
        let fixed = auto_indent_content(&all_lines, 2, 2, content);
        // Min leading = 2, delta = 2. Every line gets 2 extra spaces.
        assert_eq!(
            fixed,
            "    let x = 1;\n        if true {\n            run();\n        }\n    }"
        );
    }

    #[test]
    fn test_auto_indent_no_indent_needed() {
        let all_lines = vec!["fn main() {\n".to_string(), "    let x = 1;\n".to_string()];
        let content = "println!(\"hello\");";
        let fixed = auto_indent_content(&all_lines, 1, 1, content);
        assert_eq!(fixed, "println!(\"hello\");");
    }

    #[test]
    fn test_auto_indent_with_tabs() {
        // File with tab-indented functions. Line 31 (last line) is "\t\tpass"
        // which is inside a `{` block from line 30 → expected level is 2.
        let lines: Vec<String> = (0..30)
            .map(|_| "\tfn foo() {".to_string())
            .chain(std::iter::once("\t\tpass".to_string()))
            .collect();
        let content = "print('hello')";
        let fixed = auto_indent_content(&lines, 31, 31, content);
        // Content replaces line 31, inside the `{` block → level 2
        assert_eq!(fixed, "\t\tprint('hello')");
    }

    #[test]
    fn test_auto_indent_tabs_strips_existing_spaces() {
        let lines: Vec<String> = (0..30)
            .map(|_| "\tfn foo() {".to_string())
            .chain(std::iter::once("\t\tpass".to_string()))
            .collect();
        // Content already has 4 spaces of indent, expected is 2 tabs (len=2).
        // Guard: min_leading(4) >= expected_indent.len()(2) → content left unchanged.
        // Style mismatch is handled by validate_indentation, not auto_indent.
        let content = "    print('hello')";
        let fixed = auto_indent_content(&lines, 31, 31, content);
        assert_eq!(fixed, "    print('hello')");
    }

    #[test]
    fn test_needs_indent_fix_unindented_content() {
        let all_lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        assert!(needs_indent_fix(&all_lines, 2, 2, "print('hello')"));
    }

    #[test]
    fn test_needs_indent_fix_already_indented() {
        let all_lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        assert!(!needs_indent_fix(&all_lines, 2, 2, "    print('hello')"));
    }

    #[test]
    fn test_needs_indent_fix_partial_indent() {
        let all_lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        assert!(needs_indent_fix(&all_lines, 2, 2, "  print('hello')"));
    }

    #[test]
    fn test_needs_indent_fix_tab_content() {
        let lines: Vec<String> = (0..30)
            .map(|_| "\tfn foo() {".to_string())
            .chain(std::iter::once("\t\tpass".to_string()))
            .collect();
        // Unindented content needs fix (expected level 2)
        assert!(needs_indent_fix(&lines, 31, 31, "print('hello')"));
        // Content at level 2 (two tabs) is correct
        assert!(!needs_indent_fix(&lines, 31, 31, "\t\tprint('hello')"));
    }

    #[test]
    fn test_needs_indent_fix_top_level() {
        let all_lines = vec!["fn main() {\n".to_string(), "    let x = 1;\n".to_string()];
        assert!(!needs_indent_fix(&all_lines, 1, 1, "println!(\"hello\");"));
    }

    // ============================================================
    // validate_indentation
    // ============================================================

    #[test]
    fn test_validate_indentation_missing() {
        let all_lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        let replacement = vec!["print('hello')".to_string()];
        let (valid, warning, fix) = validate_indentation(&all_lines, 2, 2, &replacement);
        assert!(!valid);
        assert!(warning.is_some());
        assert!(warning.unwrap().contains("4 space"));
        assert_eq!(fix.unwrap(), "    print('hello')");
    }

    #[test]
    fn test_validate_indentation_correct() {
        let all_lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        let replacement = vec!["    print('hello')".to_string()];
        let (valid, warning, _fix) = validate_indentation(&all_lines, 2, 2, &replacement);
        assert!(valid);
        assert!(warning.is_none());
    }

    #[test]
    fn test_validate_indentation_partial_fix_strips_existing() {
        let all_lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        let replacement = vec!["  print('hello')".to_string()];
        let (valid, _warning, fix) = validate_indentation(&all_lines, 2, 2, &replacement);
        assert!(!valid);
        assert_eq!(fix.unwrap(), "    print('hello')");
    }

    #[test]
    fn test_validate_indentation_empty_replacement() {
        let all_lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        let replacement: Vec<String> = vec!["".to_string()];
        let (valid, warning, _fix) = validate_indentation(&all_lines, 2, 2, &replacement);
        assert!(valid);
        assert!(warning.is_none());
    }

    #[test]
    fn test_validate_indentation_whitespace_only_line() {
        let all_lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        let replacement = vec!["    ".to_string(), "print('x')".to_string()];
        let (valid, _warning, _fix) = validate_indentation(&all_lines, 2, 2, &replacement);
        assert!(!valid);
    }

    #[test]
    fn test_validate_indentation_closer_tokens_at_lower_indent() {
        let all_lines = vec![
            "fn outer() {\n".to_string(),
            "    if true {\n".to_string(),
            "        do_thing();\n".to_string(),
            "    }\n".to_string(),
            "}\n".to_string(),
        ];
        let replacement = vec![
            "        more_code();\n".to_string(),
            "    }\n".to_string(),
            "}\n".to_string(),
        ];
        let (valid, warning, _fix) = validate_indentation(&all_lines, 3, 5, &replacement);
        assert!(
            valid,
            "Closer tokens at lower indent are correct, got warning: {:?}",
            warning
        );
    }

    #[test]
    fn test_validate_indentation_closer_tokens_at_lower_indent_single_line() {
        let all_lines = vec![
            "fn outer() {\n".to_string(),
            "    let x = 1;\n".to_string(),
            "}\n".to_string(),
        ];
        let replacement = vec!["}".to_string()];
        let (valid, _warning, _fix) = validate_indentation(&all_lines, 2, 2, &replacement);
        assert!(valid, "Single closing brace at lower indent is correct");
    }

    // ============================================================
    // helper functions
    // ============================================================

    #[test]
    fn test_indent_string_spaces() {
        let style = IndentStyle {
            spaces: 4,
            uses_tabs: false,
            width: 4,
        };
        assert_eq!(style.indent_string(0), "");
        assert_eq!(style.indent_string(1), "    ");
        assert_eq!(style.indent_string(2), "        ");
    }

    #[test]
    fn test_indent_string_tabs() {
        let style = IndentStyle {
            spaces: 0,
            uses_tabs: true,
            width: 4,
        };
        assert_eq!(style.indent_string(0), "");
        assert_eq!(style.indent_string(1), "\t");
        assert_eq!(style.indent_string(2), "\t\t");
    }

    #[test]
    fn test_round_to_nearest_level() {
        assert_eq!(round_to_nearest_level(0, 4), 0);
        assert_eq!(round_to_nearest_level(4, 4), 1);
        assert_eq!(round_to_nearest_level(6, 4), 1); // equidistant → round down
        assert_eq!(round_to_nearest_level(2, 4), 0); // equidistant → round down
        assert_eq!(round_to_nearest_level(7, 4), 2); // 7 > 6, closer to 8 → level 2
        assert_eq!(round_to_nearest_level(3, 4), 1); // 3 is closer to 4 than 0
    }

    #[test]
    fn test_strip_trailing_comment() {
        assert_eq!(
            strip_trailing_comment("    let x = 1; // comment"),
            "    let x = 1; "
        );
        assert_eq!(strip_trailing_comment("    # python comment"), "    ");
        assert_eq!(strip_trailing_comment("    let x = 1;"), "    let x = 1;");
        // # anywhere starts a comment (aggressive strip is safe for indent detection)
        assert_eq!(strip_trailing_comment("x = obj#method"), "x = obj");
    }

    // ============================================================
    // BUG-HUNTING: edge cases that existing tests miss
    // ============================================================

    // --- BUG 1: auto_indent_content doesn't dedent closing braces ---

    #[test]
    fn bug_auto_indent_closing_brace_should_not_be_indented() {
        // When inserting `}` after a block body, the closing brace should
        // be at the block's indent level, NOT the body's indent level.
        // auto_indent_content places it at body level because it sees
        // body-level context, not realizing `}` is a dedent token.
        let lines = vec![
            "fn outer() {\n".to_string(),
            "    let x = 1;\n".to_string(),
            "    // insert `}` here\n".to_string(),
        ];
        // Context: editing at line 3, inside the block (indent level 1).
        // The `}` to close fn outer should be at level 0.
        let content = "}";
        let fixed = auto_indent_content(&lines, 3, 3, content);
        // EXPECTED: closing brace should be at block level 0
        assert_eq!(fixed, "}",
            "BUG: auto_indent_content indents closing brace to body level");
    }

    #[test]
    fn bug_auto_indent_closing_brace_in_multi_line() {
        // Multi-line content where first line is a closing brace.
        let lines = vec![
            "fn outer() {\n".to_string(),
            "    if true {\n".to_string(),
            "        do_work();\n".to_string(),
            "    } // closing if\n".to_string(),
            "    // insert here\n".to_string(),
        ];
        // Content: `}` (close outer) then `fn next() {` (new function at level 0)
        let content = "}\nfn next() {";
        let fixed = auto_indent_content(&lines, 5, 5, content);
        // EXPECTED: `}` at level 0, `fn next()` at level 0
        assert_eq!(fixed, "}\nfn next() {",
            "BUG: closing brace and next function should be at level 0");
    }

    // --- BUG 2: auto_indent_content tabs overhang bug ---

    #[test]
    fn bug_auto_indent_tabs_overhang_uses_tabs_for_spaces() {
        // When a tab-indented file receives space-indented content with
        // multi-level internal indentation, the overhang (extra spaces beyond
        // the minimum) is incorrectly converted to tabs instead of spaces.
        let mut lines: Vec<String> = (0..30)
            .map(|_| "\tfn foo() {}".to_string())
            .collect();
        lines.push("\t\tpass".to_string()); // line 31, inside a `{` block → expected level 2

        // Content has base indent 0 but internal line at 4 spaces
        // min_leading=0, overhang for second line = 4-0 = 4
        // expected_indent is "\t\t" (len=2)
        // Since min_leading(0) < expected_indent.len()(2), transform branch activates.
        // indent_char = '\t', repeat for expected_indent.len() + overhang = 2+4 = 6
        // Bug: all 6 chars become tabs, but overhang should remain spaces.
        let content = "outer\n    inner";
        let fixed = auto_indent_content(&lines, 31, 31, content);
        // EXPECTED: 2 tabs for base + 4 spaces for overhang
        assert_eq!(fixed, "\t\touter\n\t\t    inner",
            "BUG: overhang spaces incorrectly converted to tabs");
    }

    // --- BUG 3: auto_indent_content with only whitespace lines ---

    #[test]
    fn bug_auto_indent_whitespace_only_lines() {
        // Content where some lines are only whitespace should preserve them
        // without treating them as content-bearing lines.
        let lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        // Content has a whitespace-only line between two code lines
        let content = "x = 1\n    \ny = 2";
        let fixed = auto_indent_content(&lines, 2, 2, content);
        // Whitespace-only lines: trim() produces empty, so they're skipped
        // for min_leading computation but should be preserved as-is in output.
        // Actually the current code: if line.trim().is_empty() → returns line unchanged
        // Since the whitespace-only line has min_leading not counted, the min_leading
        // is based on "x = 1" (0) and "y = 2" (0), so min_leading=0.
        // expected_indent="    " len=4. Content left as-is because min_leading(0) < 4?
        // No: min_leading(0) >= expected_indent.len()(4)? 0 >= 4 is FALSE.
        // So we enter the transform branch. Whitespace-only line stays as "    ".
        // Non-empty lines get: indent_char(space).repeat(4+overhang) + trimmed
        // "x = 1": overhang = 0-0=0 → "    x = 1"
        // "    ": trim().is_empty() → "    " preserved
        // "y = 2": overhang = 0-0=0 → "    y = 2"
        // Result: "    x = 1\n    \n    y = 2"
        assert_eq!(fixed, "    x = 1\n    \n    y = 2");
    }

    // --- BUG 4: detect_indent_style with all blank lines ---

    #[test]
    fn bug_detect_indent_all_blank_lines() {
        // All lines are blank/empty — should fall back to default (4 spaces).
        let lines: Vec<String> = vec![
            "\n".to_string(),
            "   \n".to_string(),  // whitespace-only
            "\n".to_string(),
            "     \n".to_string(), // whitespace-only
        ];
        let style = detect_indent_style(&lines);
        // All lines trim to empty, so total_indented==0 → default
        assert_eq!(style.spaces, 4);
        assert!(!style.uses_tabs);
    }

    // --- BUG 5: detect_indent_style with zero-indent lines only ---

    #[test]
    fn bug_detect_indent_all_zero_indent() {
        // All non-empty lines start at column 0 — no indentation signals.
        let lines = vec![
            "package main\n".to_string(),
            "\n".to_string(),
            "func main() {\n".to_string(),
            "}\n".to_string(),
        ];
        let style = detect_indent_style(&lines);
        // No leading whitespace on any non-empty line → default
        assert_eq!(style.spaces, 4);
        assert!(!style.uses_tabs);
    }

    // --- BUG 6: needs_indent_fix with tab file and space content ---

    #[test]
    fn bug_needs_indent_fix_tab_file_space_content() {
        let lines: Vec<String> = (0..30)
            .map(|_| "\tfn foo() {}".to_string())
            .chain(std::iter::once("\t\tpass".to_string()))
            .collect();
        // File uses tabs, expected level 2 (two tabs).
        // Content uses 4 spaces — min_leading=4 >= expected_indent.len()(2) → false
        // But the content uses SPACES while the file uses TABS!
        // The indent styles are mismatched even though the widths are similar.
        // This test verifies that needs_indent_fix only checks width, not style.
        // Current behavior: it returns false (4 >= 2), which means the content
        // "appears" correct but actually uses wrong whitespace character.
        assert!(!needs_indent_fix(&lines, 31, 31, "    print('hello')"));
    }

    // --- BUG 7: auto_indent_content with empty file ---

    #[test]
    fn bug_auto_indent_empty_file() {
        // Inserting content into an empty file at line 1.
        let lines: Vec<String> = vec![];
        let content = "fn main() {\n    println!(\"hello\");\n}";
        let fixed = auto_indent_content(&lines, 1, 1, content);
        // Empty file → default indent style (4 spaces), level 0.
        // expected_indent = "" (level 0) → content returned unchanged.
        assert_eq!(fixed, content);
    }

    // --- BUG 8: context_line_before with all blank context ---

    #[test]
    fn bug_detect_expected_indent_all_blank_context() {
        // All lines before the edit point are blank.
        let lines: Vec<String> = vec![
            "\n".to_string(),
            "\n".to_string(),
            "\n".to_string(),
            "    let x = 1;\n".to_string(), // first non-blank is at edit point
        ];
        let (style, level) = detect_expected_indent(&lines, 4);
        // Forward context at line 4 shows 4-space indent → style.spaces=4
        // context_line_before returns None (all preceding are blank)
        // context_level = best_level.unwrap_or(0) = 0
        assert_eq!(style.spaces, 4);
        assert_eq!(level, 0);
    }

    // --- BUG 9: context_quality with step=0 ---

    #[test]
    fn bug_context_quality_step_zero() {
        // context_quality with step=0 should return 0 without panicking.
        let q = context_quality(8, 0, "    let x = 1;");
        assert_eq!(q, 0);
    }

    // --- BUG 10: round_to_nearest_level with step=0 ---

    #[test]
    fn bug_round_to_nearest_level_step_zero() {
        assert_eq!(round_to_nearest_level(8, 0), 0);
        assert_eq!(round_to_nearest_level(0, 0), 0);
    }

    // --- BUG 11: validate_indentation all-closer-token content ---

    #[test]
    fn bug_validate_indentation_only_closer_tokens() {
        // Content that is ONLY closing braces should pass validation
        // even if the indent level is "wrong" because closer tokens
        // are at a different level by design.
        let lines = vec![
            "fn outer() {\n".to_string(),
            "    if true {\n".to_string(),
            "        do_work();\n".to_string(),
            "    }\n".to_string(),
            "}\n".to_string(),
        ];
        let replacement = vec!["    }\n".to_string(), "}\n".to_string()];
        let (valid, warning, _fix) = validate_indentation(&lines, 4, 5, &replacement);
        // `}` at level 1 closes `if`, `}` at level 0 closes `outer`.
        // Closer tokens should be skipped for min_leading computation.
        assert!(valid, "Only-closer-token content should be valid, got warning: {:?}", warning);
    }

    // --- BUG 12: detect_indent_style with mixed tabs+spaces per line (discarded) ---

    #[test]
    fn bug_detect_indent_mixed_tabs_spaces_per_line_discarded() {
        // Lines that have BOTH leading tabs AND spaces are discarded.
        // This tests that the supermajority check works correctly when
        // some lines are mixed and discarded.
        let mut lines: Vec<String> = (0..20).map(|_| "    fn foo() {}".to_string()).collect();
        // Add 3 mixed lines (should be discarded)
        lines.push("  \t  fn mixed() {}".to_string());
        lines.push(" \t fn mixed2() {}".to_string());
        lines.push("\t  fn mixed3() {}".to_string());
        // 20 space lines + 0 tab lines (mixed discarded) → supermajority = 20/20 = 1.0 > 0.80
        // Wait, 1.0 > 0.80 would flip to tabs! No — tab_levels.len() is 0 because mixed lines
        // have BOTH tabs and spaces, so they don't count for either.
        let style = detect_indent_style(&lines);
        assert!(!style.uses_tabs, "Mixed lines should be discarded, keeping space style");
        assert_eq!(style.spaces, 4);
    }

    // --- BUG 13: auto_indent_content with content already at correct level ---

    #[test]
    fn bug_auto_indent_already_correct_multilevel() {
        // Content is already at the expected indent level with internal nesting.
        // Should be returned unchanged.
        let lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        let content = "    x = 1\n        if y:\n            z()\n    w = 2";
        let fixed = auto_indent_content(&lines, 2, 2, content);
        // min_leading = 4 (from "    x = 1"), expected_indent.len() = 4
        // 4 >= 4 → content returned unchanged
        assert_eq!(fixed, content);
    }

    // --- BUG 14: auto_indent_content with content having extra whitespace ---

    #[test]
    fn bug_auto_indent_overindented_content() {
        // Content has MORE indentation than expected (e.g., 8 spaces vs expected 4).
        // Should be returned unchanged because min_leading >= expected.
        let lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        let content = "        overindented"; // 8 spaces, expected 4
        let fixed = auto_indent_content(&lines, 2, 2, content);
        // min_leading(8) >= expected_indent.len()(4) → unchanged
        assert_eq!(fixed, content);
    }

    // --- BUG 15: auto_indent_content single line empty ---

    #[test]
    fn bug_auto_indent_single_empty_line() {
        let lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        let content = ""; // empty content
        let fixed = auto_indent_content(&lines, 2, 2, content);
        assert_eq!(fixed, "");
    }

    // --- BUG 16: validate_indentation with replacement having extra indentation ---

    #[test]
    fn bug_validate_indentation_overindented_passes() {
        // Replacement has MORE indentation than expected — passes validation.
        let lines = vec!["def foo():\n".to_string(), "    pass\n".to_string()];
        let replacement = vec!["        print('over')".to_string()]; // 8 spaces vs expected 4
        let (valid, _warning, _fix) = validate_indentation(&lines, 2, 2, &replacement);
        // Only checks min_leading < expected_spaces. 8 >= 4 → valid.
        assert!(valid, "Over-indented content should pass validation");
    }

    // --- BUG 17: detect_indent_style with single indented line ---

    #[test]
    fn bug_detect_indent_single_indented_line() {
        // Only one non-empty, indented line.
        let lines = vec!["    let x = 1;\n".to_string()];
        let style = detect_indent_style(&lines);
        // freq has {4: 1}, candidates include 4. score = 1*1000 - 4 = 996.
        // best_step should be 4.
        assert_eq!(style.spaces, 4);
        assert!(!style.uses_tabs);
    }

    // --- G2 edge: closer token in body of content does not corrupt min_leading ---

    #[test]
    fn bug_auto_indent_closer_mid_content_preserves_body_indent() {
        // Content has body code at level 1 and a closing `}` meant for level 0.
        // The `}` should NOT drag down min_leading, so body code stays at level 1
        // and the `}` stays at its intended level 0 (dedented relative to body).
        let lines = vec!["fn outer() {\n".to_string(), "    // insert here\n".to_string()];
        let content = "    do_thing()\n}";
        let fixed = auto_indent_content(&lines, 2, 2, content);
        // `do_thing()` at level 1 (4 spaces), `}` at level 0 (closes outer).
        assert_eq!(fixed, "    do_thing()\n}",
            "G2: closer in content must not pull down body indentation");
    }
}