rumdl 0.2.2

A fast Markdown linter written in Rust (Ru(st) MarkDown Linter)
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
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
/// Rule MD007: Unordered list indentation
///
/// See [docs/md007.md](../../docs/md007.md) for full documentation, configuration, and examples.
use crate::rule::{LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
use crate::rule_config_serde::RuleConfig;

pub mod md007_config;
use md007_config::MD007Config;

#[derive(Debug, Clone, Default)]
pub struct MD007ULIndent {
    config: MD007Config,
}

impl MD007ULIndent {
    pub fn new(indent: usize) -> Self {
        Self {
            config: MD007Config {
                indent: crate::types::IndentSize::from_const(indent as u8),
                start_indented: false,
                start_indent: crate::types::IndentSize::from_const(2),
                style: md007_config::IndentStyle::TextAligned,
                style_explicit: false,  // Allow auto-detection for programmatic construction
                indent_explicit: false, // Programmatic construction uses default behavior
            },
        }
    }

    pub fn from_config_struct(config: MD007Config) -> Self {
        Self { config }
    }

    /// Convert character position to visual column (accounting for tabs)
    fn char_pos_to_visual_column(content: &str, char_pos: usize) -> usize {
        let mut visual_col = 0;

        for (current_pos, ch) in content.chars().enumerate() {
            if current_pos >= char_pos {
                break;
            }
            if ch == '\t' {
                // Tab moves to next multiple of 4
                visual_col = (visual_col / 4 + 1) * 4;
            } else {
                visual_col += 1;
            }
        }
        visual_col
    }

    /// Pop list-stack entries that a content line at (`bq_depth`, `visual_indent`)
    /// has closed. An open item still contains the line only when the line stays
    /// in the item's blockquote context (or a deeper one) and begins at or past
    /// the item's content column; otherwise the item has ended. Keeping the stack
    /// accurate prevents a later list from being mistaken for a sublist of an item
    /// that already closed (which would, e.g., wrongly extend the ordered-ancestor
    /// exemption past a terminating paragraph, blockquote, or code block).
    /// Visual indentation of a line measured in the same coordinate space the
    /// stack uses for `content_col`: for a blockquoted line that is the width of
    /// the leading whitespace *after* the `>` prefix(es); for any other line it is
    /// the absolute `visual_indent`. Comparing a blockquoted line's absolute indent
    /// (which counts the `>` markers) against a blockquote-relative content column
    /// would otherwise treat in-quote content as if it had dedented out of the item.
    /// Measure the line's indentation in the coordinate space of a blockquote at the
    /// given nesting `depth`: strip exactly `depth` `>` markers (each with one optional
    /// following space or tab) and return the leading whitespace of the remainder as
    /// visual columns. At depth 0 this is the line's own visual indent.
    ///
    /// The remainder may itself begin with deeper `>` markers; the whitespace measured
    /// is whatever precedes them, so an interrupting deeper quote reports the column at
    /// which its `>` begins inside the shallower container. That lets a closed-item check
    /// compare the line against an item using the item's own quote coordinate space,
    /// avoiding any relative-vs-absolute mismatch.
    fn indent_relative_to_depth(
        ctx: &crate::lint_context::LintContext,
        line_info: &crate::lint_context::LineInfo,
        depth: usize,
    ) -> usize {
        if depth == 0 {
            return line_info.visual_indent;
        }
        // The blockquote's pre-parsed `content` has its leading whitespace stripped,
        // so it cannot report the in-quote indentation. Walk the `>` prefix(es) on the
        // raw line (mirroring the list-item indent calculation) and measure the
        // whitespace that follows, which is the indent inside the quote container.
        let line_content = line_info.content(ctx.content);
        let mut remaining = line_content;
        let mut content_start = 0;
        let mut stripped_levels = 0;
        while stripped_levels < depth {
            let trimmed = remaining.trim_start();
            if !trimmed.starts_with('>') {
                break;
            }
            content_start += remaining.len() - trimmed.len();
            content_start += 1;
            let after_gt = &trimmed[1..];
            if let Some(stripped) = after_gt.strip_prefix(' ') {
                content_start += 1;
                remaining = stripped;
            } else if let Some(stripped) = after_gt.strip_prefix('\t') {
                content_start += 1;
                remaining = stripped;
            } else {
                remaining = after_gt;
            }
            stripped_levels += 1;
        }
        let content_after_prefix = &line_content[content_start..];
        let ws_chars = content_after_prefix
            .chars()
            .take_while(|c| *c == ' ' || *c == '\t')
            .count();
        Self::char_pos_to_visual_column(content_after_prefix, ws_chars)
    }

    fn terminate_closed_items(
        ctx: &crate::lint_context::LintContext,
        line_info: &crate::lint_context::LineInfo,
        list_stack: &mut Vec<(usize, usize, bool, usize, usize, bool)>,
        line_bq_depth: usize,
    ) {
        while let Some(&(_, _, _, content_col, item_bq_depth, _)) = list_stack.last() {
            let closed = match item_bq_depth.cmp(&line_bq_depth) {
                // The line has exited a deeper blockquote the item lived in.
                std::cmp::Ordering::Greater => true,
                // The line is in the same or a deeper blockquote than the item.
                // Measure the line's indent in the item's own quote coordinate space
                // and close the item when the line begins left of the item's content.
                // For a same-depth line this is the in-container indent; for a deeper
                // interrupting quote it is the column where that quote's `>` begins
                // inside the item's container, so a `> > quote` left of the item's
                // content (e.g. interrupting `> 1. ordered`) closes it, while a quote
                // indented into the item's content keeps it open.
                std::cmp::Ordering::Equal | std::cmp::Ordering::Less => {
                    content_col > Self::indent_relative_to_depth(ctx, line_info, item_bq_depth)
                }
            };
            if closed {
                list_stack.pop();
            } else {
                break;
            }
        }
    }

    /// Calculate expected indentation for a nested list item.
    ///
    /// This uses per-parent logic rather than document-wide style selection:
    /// - When parent is **ordered**: align with parent's text (handles variable-width markers)
    /// - When parent is **unordered**: use configured indent (fixed-width markers)
    ///
    /// If user explicitly sets `style`, that choice is respected uniformly.
    /// "Do What I Mean" behavior: if user sets `indent` but not `style`, use fixed style.
    fn calculate_expected_indent(
        &self,
        nesting_level: usize,
        parent_info: Option<(bool, usize)>, // (is_ordered, content_visual_col)
    ) -> usize {
        if nesting_level == 0 {
            return 0;
        }

        // If user explicitly set style, respect their choice uniformly
        if self.config.style_explicit {
            return match self.config.style {
                md007_config::IndentStyle::Fixed => nesting_level * self.config.indent.get() as usize,
                md007_config::IndentStyle::TextAligned => {
                    parent_info.map_or(nesting_level * 2, |(_, content_col)| content_col)
                }
            };
        }

        // "Do What I Mean": if indent is explicitly set (but style is not), use fixed style
        // This is the expected behavior when users configure `indent = 4` - they want 4-space increments
        if self.config.indent_explicit {
            match parent_info {
                Some((true, parent_content_col)) => {
                    // Parent is ordered: return text-aligned as primary expected value.
                    // The caller also accepts the fixed indent as an alternative.
                    return parent_content_col;
                }
                _ => {
                    // Parent is unordered or no parent: use fixed indent
                    return nesting_level * self.config.indent.get() as usize;
                }
            }
        }

        // Smart default: per-parent type decision
        match parent_info {
            Some((true, parent_content_col)) => {
                // Parent is ordered: align with parent's text position
                // This handles variable-width markers ("1." vs "10." vs "100.")
                parent_content_col
            }
            Some((false, parent_content_col)) => {
                // Parent is unordered: check if it's at the expected fixed position
                // If yes, continue with fixed style (for pure unordered lists)
                // If no, parent is offset (e.g., inside ordered list), use text-aligned
                let parent_level = nesting_level.saturating_sub(1);
                let expected_parent_marker = parent_level * self.config.indent.get() as usize;
                // Parent's marker column is content column minus marker width (2 for "- ")
                let parent_marker_col = parent_content_col.saturating_sub(2);

                if parent_marker_col == expected_parent_marker {
                    // Parent is at expected fixed position, continue with fixed style
                    nesting_level * self.config.indent.get() as usize
                } else {
                    // Parent is offset, use text-aligned
                    parent_content_col
                }
            }
            None => {
                // No parent found (shouldn't happen at nesting_level > 0)
                nesting_level * self.config.indent.get() as usize
            }
        }
    }
}

impl Rule for MD007ULIndent {
    fn name(&self) -> &'static str {
        "MD007"
    }

    fn description(&self) -> &'static str {
        "Unordered list indentation"
    }

    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
        let mut warnings = Vec::new();
        let mut list_stack: Vec<(usize, usize, bool, usize, usize, bool)> = Vec::new(); // Stack of (marker_visual_col, line_num, is_ordered, content_visual_col, blockquote_depth, exempt) for tracking nesting. `exempt` marks an unordered item that inherited the ordered-ancestor MD007 exemption.

        for (line_idx, line_info) in ctx.lines.iter().enumerate() {
            // Skip if this line is in a code block, front matter, or mkdocstrings
            let is_skipped_region = |info: &crate::lint_context::LineInfo| {
                info.in_code_block || info.in_front_matter || info.in_mkdocstrings || info.in_footnote_definition
            };
            if is_skipped_region(line_info) {
                // The opening line of such a region (e.g. an unindented code fence)
                // breaks out of any open list just like a paragraph would, so the
                // stale list stack must be cleared even though the region's lines
                // are otherwise skipped. Interior lines (code contents, etc.) are
                // immaterial: only act on the region's first non-blank line, using
                // its indentation to decide which items it closed.
                let region_start = line_idx == 0 || !is_skipped_region(&ctx.lines[line_idx - 1]);
                if region_start && !line_info.is_blank {
                    let bq_depth = line_info.blockquote.as_ref().map_or(0, |bq| bq.nesting_level);
                    Self::terminate_closed_items(ctx, line_info, &mut list_stack, bq_depth);
                }
                continue;
            }

            // Check if this line has a list item
            if let Some(list_item) = &line_info.list_item {
                // For blockquoted lists, we need to calculate indentation relative to the blockquote content
                // not the full line. This is because blockquoted lists follow the same indentation rules
                // as regular lists, just within their blockquote context.
                let (content_for_calculation, adjusted_marker_column) = if line_info.blockquote.is_some() {
                    // Find the position after ALL blockquote prefixes (handles nested > > > etc)
                    let line_content = line_info.content(ctx.content);
                    let mut remaining = line_content;
                    let mut content_start = 0;

                    loop {
                        let trimmed = remaining.trim_start();
                        if !trimmed.starts_with('>') {
                            break;
                        }
                        // Account for leading whitespace
                        content_start += remaining.len() - trimmed.len();
                        // Account for '>'
                        content_start += 1;
                        let after_gt = &trimmed[1..];
                        // Handle optional whitespace after '>' (space or tab)
                        if let Some(stripped) = after_gt.strip_prefix(' ') {
                            content_start += 1;
                            remaining = stripped;
                        } else if let Some(stripped) = after_gt.strip_prefix('\t') {
                            content_start += 1;
                            remaining = stripped;
                        } else {
                            remaining = after_gt;
                        }
                    }

                    // Extract the content after the blockquote prefix
                    let content_after_prefix = &line_content[content_start..];
                    // Adjust the marker column to be relative to the content after the prefix
                    let adjusted_col = if list_item.marker_column >= content_start {
                        list_item.marker_column - content_start
                    } else {
                        // This shouldn't happen, but handle it gracefully
                        list_item.marker_column
                    };
                    (content_after_prefix.to_string(), adjusted_col)
                } else {
                    (line_info.content(ctx.content).to_string(), list_item.marker_column)
                };

                // Convert marker position to visual column
                let visual_marker_column =
                    Self::char_pos_to_visual_column(&content_for_calculation, adjusted_marker_column);

                // Calculate content visual column for text-aligned style
                let visual_content_column = if line_info.blockquote.is_some() {
                    // For blockquoted content, we already have the adjusted content
                    let adjusted_content_col =
                        if list_item.content_column >= (line_info.byte_len - content_for_calculation.len()) {
                            list_item.content_column - (line_info.byte_len - content_for_calculation.len())
                        } else {
                            list_item.content_column
                        };
                    Self::char_pos_to_visual_column(&content_for_calculation, adjusted_content_col)
                } else {
                    Self::char_pos_to_visual_column(line_info.content(ctx.content), list_item.content_column)
                };

                // For nesting detection, treat 1-space indent as if it's at column 0
                // because 1 space is insufficient to establish a nesting relationship
                // UNLESS the user has explicitly configured indent=1, in which case 1 space IS valid nesting
                let visual_marker_for_nesting = if visual_marker_column == 1 && self.config.indent.get() != 1 {
                    0
                } else {
                    visual_marker_column
                };

                // Determine blockquote depth for this line
                let bq_depth = line_info.blockquote.as_ref().map_or(0, |bq| bq.nesting_level);

                // Clean up stack - remove items at same or deeper indentation,
                // but only consider items at the same blockquote depth
                while let Some(&(indent, _, _, _, item_bq_depth, _)) = list_stack.last() {
                    if item_bq_depth == bq_depth && indent >= visual_marker_for_nesting {
                        list_stack.pop();
                    } else if item_bq_depth > bq_depth {
                        // Pop items from deeper blockquote contexts that we've left
                        list_stack.pop();
                    } else {
                        break;
                    }
                }

                // The loop above only reconciles items at the same (or deeper)
                // blockquote depth. A list item that enters a deeper blockquote than an
                // ancestor (e.g. `> > - item` after `> 1. ordered`, or `> - item` after
                // a top-level `1. ordered`) starts a separate container when that quote
                // begins left of the ancestor's content. Measured in the ancestor's own
                // quote coordinate space, the deeper quote's marker is then to the left
                // of the ancestor's content column, so the ancestor is closed. Pop it
                // here, otherwise a closed ordered ancestor would linger and wrongly
                // extend its exemption to a later, separately indented unordered list.
                // A deeper quote indented into the ancestor's content is part of that
                // item and keeps it open. Same-depth nesting and items already inside a
                // blockquote are left to the loop above and the exemption check below.
                while let Some(&(_, _, _, content_col, item_bq_depth, _)) = list_stack.last() {
                    if item_bq_depth < bq_depth
                        && content_col > Self::indent_relative_to_depth(ctx, line_info, item_bq_depth)
                    {
                        list_stack.pop();
                    } else {
                        break;
                    }
                }

                // For ordered list items, just track them in the stack
                if list_item.is_ordered {
                    // For ordered lists, we don't check indentation but we need to track for text-aligned children
                    // Use the actual positions since we don't enforce indentation for ordered lists
                    list_stack.push((
                        visual_marker_column,
                        line_idx,
                        true,
                        visual_content_column,
                        bq_depth,
                        false,
                    ));
                    continue;
                }

                // At this point, we know this is an unordered list item.
                //
                // markdownlint applies MD007 to a sublist only if its parent lists
                // are all also unordered. An unordered item that is genuinely nested
                // under an ordered ancestor is therefore exempt from the indentation
                // check, at any depth. Two conditions must both hold:
                //
                //   1. threshold: an ordered ancestor at this blockquote depth has its
                //      content column at or left of this bullet's marker, so the bullet
                //      is indented far enough to be that ordered item's sublist. A
                //      bullet indented less than the ordered content column is a new
                //      top-level list, which markdownlint still checks.
                //   2. chain: the nearest same-depth ancestor is itself ordered, or is
                //      an unordered item that already inherited the exemption. This
                //      stops the exemption from leaking past a non-nested unordered
                //      parent to its children. For `100. ordered` / `   - parent` /
                //      `     - child`, the parent is left of the ordered content column
                //      (not nested, not exempt), so the child resolves against the real
                //      unordered layout and is still checked.
                //
                // The MkDocs flavor is excluded: it deliberately enforces
                // Python-Markdown's stricter continuation indent under ordered parents
                // (insufficient indent there is a real rendering bug, not a style nit).
                let threshold_ok = list_stack
                    .iter()
                    .any(|item| item.4 == bq_depth && item.2 && item.3 <= visual_marker_column);
                let chain_ok = list_stack
                    .iter()
                    .rev()
                    .find(|item| item.4 == bq_depth)
                    .is_some_and(|item| item.2 || item.5);
                if ctx.flavor != crate::config::MarkdownFlavor::MkDocs && threshold_ok && chain_ok {
                    list_stack.push((
                        visual_marker_column,
                        line_idx,
                        false,
                        visual_content_column,
                        bq_depth,
                        true,
                    ));
                    continue;
                }

                // Count only items at the same blockquote depth for nesting level
                let nesting_level = list_stack.iter().filter(|item| item.4 == bq_depth).count();

                // Get parent info for per-parent calculation (only from same blockquote depth)
                let parent_info = list_stack
                    .iter()
                    .rev()
                    .find(|item| item.4 == bq_depth)
                    .map(|&(_, _, is_ordered, content_col, _, _)| (is_ordered, content_col));

                // Calculate expected indent using per-parent logic
                // When start_indented is true, only depth-0 items use the start_indent value.
                // For nested items (depth >= 1), the parent's actual position in the stack
                // already reflects the start_indent shift, so calculate_expected_indent
                // naturally produces the correct result.
                let mut expected_indent = if self.config.start_indented && nesting_level == 0 {
                    self.config.start_indent.get() as usize
                } else {
                    self.calculate_expected_indent(nesting_level, parent_info)
                };

                // When indent is explicitly set and parent is ordered, also accept
                // the fixed indent value (nesting_level * indent). This lets users
                // choose either text-aligned or their configured indent under ordered lists.
                let also_acceptable =
                    if self.config.indent_explicit && parent_info.is_some_and(|(is_ordered, _)| is_ordered) {
                        Some(nesting_level * self.config.indent.get() as usize)
                    } else {
                        None
                    };

                // MkDocs (Python-Markdown) uses 4-space-tab continuation for list items.
                // Under an ordered list item, Python-Markdown requires at least
                // marker_column + 4 spaces for continuation content to be recognized.
                if ctx.flavor == crate::config::MarkdownFlavor::MkDocs
                    && let Some(&(parent_marker_col, _, true, _, _, _)) =
                        list_stack.iter().rev().find(|item| item.4 == bq_depth && item.2)
                {
                    expected_indent = expected_indent.max(parent_marker_col + 4);
                }

                // Add current item to stack
                // Use actual marker position for cleanup logic
                // For text-aligned children, store the EXPECTED content position after fix
                // (not the actual position) to prevent error cascade
                // When accepted via also_acceptable, use that indent for content col
                let accepted_indent = if also_acceptable.is_some_and(|alt| visual_marker_column == alt) {
                    visual_marker_column
                } else {
                    expected_indent
                };
                let expected_content_visual_col = accepted_indent + 2;
                list_stack.push((
                    visual_marker_column,
                    line_idx,
                    false,
                    expected_content_visual_col,
                    bq_depth,
                    false,
                ));

                // A top-level item (depth 0) is expected at column 0 when start_indented
                // is false. Column 0 is already correct, so skip it; any other column
                // (1, 2, or 3) is a misindented top-level list and must be flagged with
                // "Expected 0". Four or more leading spaces form an indented code block,
                // not a list, so such lines never reach here as list items.
                if !self.config.start_indented && nesting_level == 0 && visual_marker_column == 0 {
                    continue;
                }

                if visual_marker_column != expected_indent && also_acceptable != Some(visual_marker_column) {
                    // Use the fixed indent as the suggested value when the alternative was available
                    if let Some(alt) = also_acceptable {
                        expected_indent = alt;
                    }
                    // Generate fix for this list item
                    let fix = {
                        let correct_indent = " ".repeat(expected_indent);

                        // Build the replacement string - need to preserve everything before the list marker
                        // For blockquoted lines, this includes the blockquote prefix
                        let replacement = if line_info.blockquote.is_some() {
                            // Count the blockquote markers
                            let mut blockquote_count = 0;
                            for ch in line_info.content(ctx.content).chars() {
                                if ch == '>' {
                                    blockquote_count += 1;
                                } else if ch != ' ' && ch != '\t' {
                                    break;
                                }
                            }
                            // Build the blockquote prefix (one '>' per level, with spaces between for nested)
                            let blockquote_prefix = if blockquote_count > 1 {
                                (0..blockquote_count)
                                    .map(|_| "> ")
                                    .collect::<String>()
                                    .trim_end()
                                    .to_string()
                            } else {
                                ">".to_string()
                            };
                            // Add correct indentation after the blockquote prefix
                            // Include one space after the blockquote marker(s) as part of the indent
                            format!("{blockquote_prefix} {correct_indent}")
                        } else {
                            correct_indent
                        };

                        // Calculate the byte positions
                        // The range should cover from start of line to the marker position
                        let start_byte = line_info.byte_offset;
                        let mut end_byte = line_info.byte_offset;

                        // Calculate where the marker starts
                        for (i, ch) in line_info.content(ctx.content).chars().enumerate() {
                            if i >= list_item.marker_column {
                                break;
                            }
                            end_byte += ch.len_utf8();
                        }

                        Some(crate::rule::Fix::new(start_byte..end_byte, replacement))
                    };

                    warnings.push(LintWarning {
                        rule_name: Some(self.name().to_string()),
                        message: format!(
                            "Expected {expected_indent} spaces for indent depth {nesting_level}, found {visual_marker_column}"
                        ),
                        line: line_idx + 1, // Convert to 1-indexed
                        column: 1,          // Start of line
                        end_line: line_idx + 1,
                        end_column: visual_marker_column + 1, // End of visual indentation
                        severity: Severity::Warning,
                        fix,
                    });
                }
            } else if !line_info.is_blank {
                // A non-blank, non-list content line that breaks out of the open
                // list terminates every list item whose content begins to its
                // right: an item's children must be indented past its content
                // column, so a line indented less cannot belong to it. Popping
                // these closed items keeps list_stack accurate, so a later list
                // is not mistaken for a sublist of one that has already ended
                // (e.g. a top-level paragraph closing an ordered list, after
                // which a separately indented bullet is a new top-level list).
                //
                // A CommonMark lazy continuation line is the exception: plain
                // paragraph text that immediately follows the item (no blank line
                // between) continues the item's open paragraph and so keeps the
                // list open. Constructs that interrupt a paragraph (ATX heading,
                // thematic break, fenced code, HTML block, HTML comment, div block)
                // end the list even without a blank line, matching markdownlint. A
                // line beginning with
                // a list marker is likewise not lazy paragraph text - it would start
                // a new list item - so it must still terminate stale ancestors (e.g.
                // a deeper bullet that pulldown-cmark treats as item content rather
                // than a sublist).
                //
                // Blockquotes need container awareness: a continuation in the *same*
                // quote (`> text` after `> 1. item`) is lazy, but newly entering a
                // quote (`> text` after a non-quoted item) interrupts the paragraph
                // and ends the list. So compare the previous line's quote depth, and
                // examine the marker on the quote-stripped content.
                let bq_depth = line_info.blockquote.as_ref().map_or(0, |bq| bq.nesting_level);
                let prev_line = line_idx.checked_sub(1).map(|i| &ctx.lines[i]);
                let prev_blank = prev_line.is_none_or(|p| p.is_blank);
                let prev_bq_depth = prev_line
                    .and_then(|p| p.blockquote.as_ref())
                    .map_or(0, |bq| bq.nesting_level);
                let same_container = prev_bq_depth == bq_depth;
                let text = line_info
                    .blockquote
                    .as_ref()
                    .map_or_else(|| line_info.content(ctx.content), |bq| bq.content.as_str());
                let trimmed = text.trim_start();
                let starts_like_list_marker = match trimmed.as_bytes().first() {
                    Some(b'-' | b'*' | b'+') => {
                        matches!(trimmed.as_bytes().get(1), Some(b' ' | b'\t'))
                    }
                    Some(c) if c.is_ascii_digit() => {
                        // CommonMark allows at most 9 digits in an ordered list marker.
                        // A longer digit run is not a marker, so the line can be lazy
                        // paragraph text rather than a list-interrupting item.
                        let after_digits = trimmed.trim_start_matches(|ch: char| ch.is_ascii_digit());
                        let num_digits = trimmed.len() - after_digits.len();
                        let mut rest = after_digits.chars();
                        (1..=9).contains(&num_digits)
                            && matches!(rest.next(), Some('.' | ')'))
                            && matches!(rest.next(), Some(' ' | '\t') | None)
                    }
                    _ => false,
                };
                // Lazy continuation only extends an OPEN paragraph. The previous line
                // must itself be paragraph text (or a list-item line whose paragraph the
                // current line continues), not a closed block such as a fenced code
                // block, heading, thematic break, HTML block/comment, or div marker.
                // After such a block, an unindented line starts a new paragraph and
                // closes the list instead of lazily continuing it.
                let prev_is_open_paragraph = prev_line.is_some_and(|p| {
                    !p.is_blank
                        && !p.in_code_block
                        && p.heading.is_none()
                        && !p.is_horizontal_rule
                        && !p.in_html_block
                        && !p.in_html_comment
                        && !p.is_div_marker
                });
                let is_lazy_paragraph_continuation = !prev_blank
                    && prev_is_open_paragraph
                    && same_container
                    && !starts_like_list_marker
                    && line_info.heading.is_none()
                    && !line_info.is_horizontal_rule
                    && !line_info.in_code_block
                    && !line_info.in_html_block
                    && !line_info.in_html_comment
                    && !line_info.is_div_marker;
                if is_lazy_paragraph_continuation {
                    // Lazy continuation: the list stays open, leave the stack intact.
                    continue;
                }
                Self::terminate_closed_items(ctx, line_info, &mut list_stack, bq_depth);
            }
        }
        Ok(warnings)
    }

    /// Optimized check using document structure
    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
        // Get all warnings with their fixes
        let warnings = self.check(ctx)?;
        let warnings =
            crate::utils::fix_utils::filter_warnings_by_inline_config(warnings, ctx.inline_config(), self.name());

        // If no warnings, return original content
        if warnings.is_empty() {
            return Ok(ctx.content.to_string());
        }

        // Collect all fixes and sort by range start (descending) to apply from end to beginning
        let mut fixes: Vec<_> = warnings
            .iter()
            .filter_map(|w| w.fix.as_ref().map(|f| (f.range.start, f.range.end, &f.replacement)))
            .collect();
        fixes.sort_by(|a, b| b.0.cmp(&a.0));

        // Apply fixes from end to beginning to preserve byte offsets
        let mut result = ctx.content.to_string();
        for (start, end, replacement) in fixes {
            if start < result.len() && end <= result.len() && start <= end {
                result.replace_range(start..end, replacement);
            }
        }

        Ok(result)
    }

    /// Get the category of this rule for selective processing
    fn category(&self) -> RuleCategory {
        RuleCategory::List
    }

    /// Check if this rule should be skipped
    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
        // Fast path: check if document likely has lists
        if ctx.content.is_empty() || !ctx.likely_has_lists() {
            return true;
        }
        // Verify unordered list items actually exist
        !ctx.lines
            .iter()
            .any(|line| line.list_item.as_ref().is_some_and(|item| !item.is_ordered))
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn default_config_section(&self) -> Option<(String, toml::Value)> {
        let default_config = MD007Config::default();
        let json_value = serde_json::to_value(&default_config).ok()?;
        let toml_value = crate::rule_config_serde::json_to_toml_value(&json_value)?;

        if let toml::Value::Table(table) = toml_value {
            if !table.is_empty() {
                Some((MD007Config::RULE_NAME.to_string(), toml::Value::Table(table)))
            } else {
                None
            }
        } else {
            None
        }
    }

    fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
    where
        Self: Sized,
    {
        let mut rule_config = crate::rule_config_serde::load_rule_config::<MD007Config>(config);

        // Check if style and/or indent were explicitly set in the config
        if let Some(rule_cfg) = config.rules.get("MD007") {
            rule_config.style_explicit = rule_cfg.values.contains_key("style");
            rule_config.indent_explicit = rule_cfg.values.contains_key("indent");

            // Warn if both indent and text-aligned style are explicitly set
            // This combination is contradictory: indent implies fixed increments,
            // but text-aligned ignores the indent value and aligns with parent text
            if rule_config.indent_explicit
                && rule_config.style_explicit
                && rule_config.style == md007_config::IndentStyle::TextAligned
            {
                eprintln!(
                    "\x1b[33m[config warning]\x1b[0m MD007: 'indent' has no effect when 'style = \"text-aligned\"'. \
                     Text-aligned style ignores indent and aligns nested items with parent text. \
                     To use fixed {} space increments, either remove 'style' or set 'style = \"fixed\"'.",
                    rule_config.indent.get()
                );
            }
        }

        // MkDocs/Python-Markdown requires 4-space indentation for nested list content.
        // Enforce indent=4 and style=fixed regardless of user config.
        if config.markdown_flavor() == crate::config::MarkdownFlavor::MkDocs {
            if rule_config.indent_explicit && rule_config.indent.get() < 4 {
                eprintln!(
                    "\x1b[33m[config warning]\x1b[0m MD007: MkDocs flavor requires indent >= 4 \
                     (Python-Markdown enforces 4-space indentation). \
                     Overriding indent={} to indent=4.",
                    rule_config.indent.get()
                );
            }
            if rule_config.style_explicit && rule_config.style == md007_config::IndentStyle::TextAligned {
                eprintln!(
                    "\x1b[33m[config warning]\x1b[0m MD007: MkDocs flavor requires style=\"fixed\" \
                     (Python-Markdown uses fixed 4-space indentation). \
                     Overriding style=\"text-aligned\" to style=\"fixed\"."
                );
            }
            if rule_config.indent.get() < 4 {
                rule_config.indent = crate::types::IndentSize::from_const(4);
            }
            rule_config.style = md007_config::IndentStyle::Fixed;
        }

        Box::new(Self::from_config_struct(rule_config))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lint_context::LintContext;
    use crate::rule::Rule;

    #[test]
    fn test_valid_list_indent() {
        let rule = MD007ULIndent::default();
        let content = "* Item 1\n  * Item 2\n    * Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Expected no warnings for valid indentation, but got {} warnings",
            result.len()
        );
    }

    #[test]
    fn test_invalid_list_indent() {
        let rule = MD007ULIndent::default();
        let content = "* Item 1\n   * Item 2\n      * Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].line, 2);
        assert_eq!(result[0].column, 1);
        assert_eq!(result[1].line, 3);
        assert_eq!(result[1].column, 1);
    }

    #[test]
    fn test_mixed_indentation() {
        let rule = MD007ULIndent::default();
        let content = "* Item 1\n  * Item 2\n   * Item 3\n  * Item 4";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].line, 3);
        assert_eq!(result[0].column, 1);
    }

    #[test]
    fn test_fix_indentation() {
        let rule = MD007ULIndent::default();
        let content = "* Item 1\n   * Item 2\n      * Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.fix(&ctx).unwrap();
        // With text-aligned style and non-cascade:
        // Item 2 aligns with Item 1's text (2 spaces)
        // Item 3 aligns with Item 2's expected text position (4 spaces)
        let expected = "* Item 1\n  * Item 2\n    * Item 3";
        assert_eq!(result, expected);
    }

    #[test]
    fn test_md007_in_yaml_code_block() {
        let rule = MD007ULIndent::default();
        let content = r#"```yaml
repos:
-   repo: https://github.com/rvben/rumdl
    rev: v0.5.0
    hooks:
    -   id: rumdl-check
```"#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "MD007 should not trigger inside a code block, but got warnings: {result:?}"
        );
    }

    #[test]
    fn test_blockquoted_list_indent() {
        let rule = MD007ULIndent::default();
        let content = "> * Item 1\n>   * Item 2\n>     * Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Expected no warnings for valid blockquoted list indentation, but got {result:?}"
        );
    }

    #[test]
    fn test_blockquoted_list_invalid_indent() {
        let rule = MD007ULIndent::default();
        let content = "> * Item 1\n>    * Item 2\n>       * Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(
            result.len(),
            2,
            "Expected 2 warnings for invalid blockquoted list indentation, got {result:?}"
        );
        assert_eq!(result[0].line, 2);
        assert_eq!(result[1].line, 3);
    }

    #[test]
    fn test_nested_blockquote_list_indent() {
        let rule = MD007ULIndent::default();
        let content = "> > * Item 1\n> >   * Item 2\n> >     * Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Expected no warnings for valid nested blockquoted list indentation, but got {result:?}"
        );
    }

    #[test]
    fn test_blockquote_list_with_code_block() {
        let rule = MD007ULIndent::default();
        let content = "> * Item 1\n>   * Item 2\n>   ```\n>   code\n>   ```\n>   * Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "MD007 should not trigger inside a code block within a blockquote, but got warnings: {result:?}"
        );
    }

    #[test]
    fn test_properly_indented_lists() {
        let rule = MD007ULIndent::default();

        // Test various properly indented lists
        let test_cases = vec![
            "* Item 1\n* Item 2",
            "* Item 1\n  * Item 1.1\n    * Item 1.1.1",
            "- Item 1\n  - Item 1.1",
            "+ Item 1\n  + Item 1.1",
            "* Item 1\n  * Item 1.1\n* Item 2\n  * Item 2.1",
        ];

        for content in test_cases {
            let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
            let result = rule.check(&ctx).unwrap();
            assert!(
                result.is_empty(),
                "Expected no warnings for properly indented list:\n{}\nGot {} warnings",
                content,
                result.len()
            );
        }
    }

    #[test]
    fn test_under_indented_lists() {
        let rule = MD007ULIndent::default();

        let test_cases = vec![
            ("* Item 1\n * Item 1.1", 1, 2),                   // Expected 2 spaces, got 1
            ("* Item 1\n  * Item 1.1\n   * Item 1.1.1", 1, 3), // Expected 4 spaces, got 3
        ];

        for (content, expected_warnings, line) in test_cases {
            let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
            let result = rule.check(&ctx).unwrap();
            assert_eq!(
                result.len(),
                expected_warnings,
                "Expected {expected_warnings} warnings for under-indented list:\n{content}"
            );
            if expected_warnings > 0 {
                assert_eq!(result[0].line, line);
            }
        }
    }

    #[test]
    fn test_over_indented_lists() {
        let rule = MD007ULIndent::default();

        let test_cases = vec![
            ("* Item 1\n   * Item 1.1", 1, 2),                   // Expected 2 spaces, got 3
            ("* Item 1\n    * Item 1.1", 1, 2),                  // Expected 2 spaces, got 4
            ("* Item 1\n  * Item 1.1\n     * Item 1.1.1", 1, 3), // Expected 4 spaces, got 5
        ];

        for (content, expected_warnings, line) in test_cases {
            let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
            let result = rule.check(&ctx).unwrap();
            assert_eq!(
                result.len(),
                expected_warnings,
                "Expected {expected_warnings} warnings for over-indented list:\n{content}"
            );
            if expected_warnings > 0 {
                assert_eq!(result[0].line, line);
            }
        }
    }

    #[test]
    fn test_custom_indent_2_spaces() {
        let rule = MD007ULIndent::new(2); // Default
        let content = "* Item 1\n  * Item 2\n    * Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_custom_indent_3_spaces() {
        // With smart auto-detection, pure unordered lists with indent=3 use fixed style
        // This provides markdownlint compatibility for the common case
        let rule = MD007ULIndent::new(3);

        // Fixed style with indent=3: level 0 = 0, level 1 = 3, level 2 = 6
        let correct_content = "* Item 1\n   * Item 2\n      * Item 3";
        let ctx = LintContext::new(correct_content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Fixed style expects 0, 3, 6 spaces but got: {result:?}"
        );

        // Wrong indentation (text-aligned style spacing)
        let wrong_content = "* Item 1\n  * Item 2\n    * Item 3";
        let ctx = LintContext::new(wrong_content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(!result.is_empty(), "Should warn: expected 3 spaces, found 2");
    }

    #[test]
    fn test_custom_indent_4_spaces() {
        // With smart auto-detection, pure unordered lists with indent=4 use fixed style
        // This provides markdownlint compatibility (fixes issue #210)
        let rule = MD007ULIndent::new(4);

        // Fixed style with indent=4: level 0 = 0, level 1 = 4, level 2 = 8
        let correct_content = "* Item 1\n    * Item 2\n        * Item 3";
        let ctx = LintContext::new(correct_content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Fixed style expects 0, 4, 8 spaces but got: {result:?}"
        );

        // Wrong indentation (text-aligned style spacing)
        let wrong_content = "* Item 1\n  * Item 2\n    * Item 3";
        let ctx = LintContext::new(wrong_content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(!result.is_empty(), "Should warn: expected 4 spaces, found 2");
    }

    #[test]
    fn test_tab_indentation() {
        let rule = MD007ULIndent::default();

        // Note: Tab at line start = 4 spaces = indented code per CommonMark, not a list item
        // MD007 checks list indentation, so this test now checks actual nested lists
        // Hard tabs within lists should be caught by MD010, not MD007

        // Single wrong indentation (3 spaces instead of 2)
        let content = "* Item 1\n   * Item 2";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1, "Wrong indentation should trigger warning");

        // Fix should correct to 2 spaces
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "* Item 1\n  * Item 2");

        // Multiple indentation errors
        let content_multi = "* Item 1\n   * Item 2\n      * Item 3";
        let ctx = LintContext::new(content_multi, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        // With non-cascade: Item 2 at 2 spaces, content at 4
        // Item 3 aligns with Item 2's expected content at 4 spaces
        assert_eq!(fixed, "* Item 1\n  * Item 2\n    * Item 3");

        // Mixed wrong indentations
        let content_mixed = "* Item 1\n   * Item 2\n     * Item 3";
        let ctx = LintContext::new(content_mixed, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        // With non-cascade: Item 2 at 2 spaces, content at 4
        // Item 3 aligns with Item 2's expected content at 4 spaces
        assert_eq!(fixed, "* Item 1\n  * Item 2\n    * Item 3");
    }

    #[test]
    fn test_mixed_ordered_unordered_lists() {
        let rule = MD007ULIndent::default();

        // MD007 only checks unordered lists, so ordered lists should be ignored
        // Note: 3 spaces is now correct for bullets under ordered items
        let content = r#"1. Ordered item
   * Unordered sub-item (correct - 3 spaces under ordered)
   2. Ordered sub-item
* Unordered item
  1. Ordered sub-item
  * Unordered sub-item"#;

        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 0, "All unordered list indentation should be correct");

        // No fix needed as all indentation is correct
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, content);
    }

    #[test]
    fn test_list_markers_variety() {
        let rule = MD007ULIndent::default();

        // Test all three unordered list markers
        let content = r#"* Asterisk
  * Nested asterisk
- Hyphen
  - Nested hyphen
+ Plus
  + Nested plus"#;

        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "All unordered list markers should work with proper indentation"
        );

        // Test with wrong indentation for each marker type
        let wrong_content = r#"* Asterisk
   * Wrong asterisk
- Hyphen
 - Wrong hyphen
+ Plus
    + Wrong plus"#;

        let ctx = LintContext::new(wrong_content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 3, "All marker types should be checked for indentation");
    }

    #[test]
    fn test_empty_list_items() {
        let rule = MD007ULIndent::default();
        let content = "* Item 1\n* \n  * Item 2";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Empty list items should not affect indentation checks"
        );
    }

    #[test]
    fn test_list_with_code_blocks() {
        let rule = MD007ULIndent::default();
        let content = r#"* Item 1
  ```
  code
  ```
  * Item 2
    * Item 3"#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_list_in_front_matter() {
        let rule = MD007ULIndent::default();
        let content = r#"---
tags:
  - tag1
  - tag2
---
* Item 1
  * Item 2"#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Lists in YAML front matter should be ignored");
    }

    #[test]
    fn test_fix_preserves_content() {
        let rule = MD007ULIndent::default();
        let content = "* Item 1 with **bold** and *italic*\n   * Item 2 with `code`\n     * Item 3 with [link](url)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        // With non-cascade: Item 2 at 2 spaces, content at 4
        // Item 3 aligns with Item 2's expected content at 4 spaces
        let expected = "* Item 1 with **bold** and *italic*\n  * Item 2 with `code`\n    * Item 3 with [link](url)";
        assert_eq!(fixed, expected, "Fix should only change indentation, not content");
    }

    #[test]
    fn test_start_indented_config() {
        let config = MD007Config {
            start_indented: true,
            start_indent: crate::types::IndentSize::from_const(4),
            indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::TextAligned,
            style_explicit: true, // Explicit style for this test
            indent_explicit: false,
        };
        let rule = MD007ULIndent::from_config_struct(config);

        // First level should be indented by start_indent (4 spaces)
        // Level 0: 4 spaces (start_indent)
        // Level 1: 6 spaces (start_indent + indent = 4 + 2)
        // Level 2: 8 spaces (start_indent + 2*indent = 4 + 4)
        let content = "    * Item 1\n      * Item 2\n        * Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Expected no warnings with start_indented config");

        // Wrong first level indentation
        let wrong_content = "  * Item 1\n    * Item 2";
        let ctx = LintContext::new(wrong_content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].line, 1);
        assert_eq!(result[0].message, "Expected 4 spaces for indent depth 0, found 2");
        assert_eq!(result[1].line, 2);
        assert_eq!(result[1].message, "Expected 6 spaces for indent depth 1, found 4");

        // Fix should correct to start_indent for first level
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "    * Item 1\n      * Item 2");
    }

    #[test]
    fn test_start_indented_false_flags_indented_first_level() {
        let rule = MD007ULIndent::default(); // start_indented is false by default

        // When start_indented is false, a top-level item is expected at column 0. A
        // top-level item indented 1-3 spaces is a misindented list and must be flagged
        // with "Expected 0", matching markdownlint-cli2 (which reports Expected: 0;
        // Actual: 3 here).
        let content = "   * Item 1"; // First level at 3 spaces
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.iter().any(|w| w.line == 1 && w.message.contains("Expected 0")),
            "a top-level item indented 3 spaces must be flagged with Expected 0, got: {result:?}"
        );

        // A correctly nested list (0/2/4 spaces) produces no warnings: these are a
        // top-level item and its properly indented descendants, not three first-level
        // items.
        let content = "* Item 1\n  * Item 2\n    * Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "a correctly nested 0/2/4-space list should produce no warnings, got: {result:?}"
        );
    }

    #[test]
    fn test_deeply_nested_lists() {
        let rule = MD007ULIndent::default();
        let content = r#"* L1
  * L2
    * L3
      * L4
        * L5
          * L6"#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty());

        // Test with wrong deep nesting
        let wrong_content = r#"* L1
  * L2
    * L3
      * L4
         * L5
            * L6"#;
        let ctx = LintContext::new(wrong_content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 2, "Deep nesting errors should be detected");
    }

    #[test]
    fn test_excessive_indentation_detected() {
        let rule = MD007ULIndent::default();

        // Test excessive indentation (5 spaces instead of 2)
        let content = "- Item 1\n     - Item 2 with 5 spaces";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1, "Should detect excessive indentation (5 instead of 2)");
        assert_eq!(result[0].line, 2);
        assert!(result[0].message.contains("Expected 2 spaces"));
        assert!(result[0].message.contains("found 5"));

        // Test slightly excessive indentation (3 spaces instead of 2)
        let content = "- Item 1\n   - Item 2 with 3 spaces";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(
            result.len(),
            1,
            "Should detect slightly excessive indentation (3 instead of 2)"
        );
        assert_eq!(result[0].line, 2);
        assert!(result[0].message.contains("Expected 2 spaces"));
        assert!(result[0].message.contains("found 3"));

        // Test insufficient indentation (1 space is treated as level 0, should be 0)
        let content = "- Item 1\n - Item 2 with 1 space";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(
            result.len(),
            1,
            "Should detect 1-space indent (insufficient for nesting, expected 0)"
        );
        assert_eq!(result[0].line, 2);
        assert!(result[0].message.contains("Expected 0 spaces"));
        assert!(result[0].message.contains("found 1"));
    }

    #[test]
    fn test_excessive_indentation_with_4_space_config() {
        // With smart auto-detection, pure unordered lists use fixed style
        // Fixed style with indent=4: level 0 = 0, level 1 = 4, level 2 = 8
        let rule = MD007ULIndent::new(4);

        // Test excessive indentation (5 spaces instead of 4)
        let content = "- Formatter:\n     - The stable style changed";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            !result.is_empty(),
            "Should detect 5 spaces when expecting 4 (fixed style)"
        );

        // Test with correct fixed style alignment (4 spaces for level 1)
        let correct_content = "- Formatter:\n    - The stable style changed";
        let ctx = LintContext::new(correct_content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Should accept correct fixed style indent (4 spaces)");
    }

    #[test]
    fn test_bullets_nested_under_numbered_items() {
        let rule = MD007ULIndent::default();
        let content = "\
1. **Active Directory/LDAP**
   - User authentication and directory services
   - LDAP for user information and validation

2. **Oracle Unified Directory (OUD)**
   - Extended user directory services";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // Should have no warnings - 3 spaces is correct for bullets under numbered items
        assert!(
            result.is_empty(),
            "Expected no warnings for bullets with 3 spaces under numbered items, got: {result:?}"
        );
    }

    #[test]
    fn test_bullets_nested_under_numbered_items_wrong_indent() {
        let rule = MD007ULIndent::default();
        let content = "\
1. **Active Directory/LDAP**
  - Wrong: only 2 spaces";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // Should flag incorrect indentation
        assert_eq!(
            result.len(),
            1,
            "Expected warning for incorrect indentation under numbered items"
        );
        assert!(
            result
                .iter()
                .any(|w| w.line == 2 && w.message.contains("Expected 3 spaces"))
        );
    }

    #[test]
    fn test_regular_bullet_nesting_still_works() {
        let rule = MD007ULIndent::default();
        let content = "\
* Top level
  * Nested bullet (2 spaces is correct)
    * Deeply nested (4 spaces)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // Should have no warnings - standard bullet nesting still uses 2-space increments
        assert!(
            result.is_empty(),
            "Expected no warnings for standard bullet nesting, got: {result:?}"
        );
    }

    #[test]
    fn test_blockquote_with_tab_after_marker() {
        let rule = MD007ULIndent::default();
        let content = ">\t* List item\n>\t  * Nested\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Tab after blockquote marker should be handled correctly, got: {result:?}"
        );
    }

    #[test]
    fn test_blockquote_with_space_then_tab_after_marker() {
        let rule = MD007ULIndent::default();
        let content = "> \t* List item\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // Inside the blockquote the bullet is indented away from column 0, so it is a
        // misindented top-level list and is flagged with "Expected 0", matching
        // markdownlint-cli2 (which flags Expected: 0). The reported actual column
        // reflects rumdl's CommonMark tab-stop expansion rather than a raw char count.
        assert!(
            result.iter().any(|w| w.line == 1 && w.message.contains("Expected 0")),
            "an indented blockquoted top-level item must be flagged with Expected 0, got: {result:?}"
        );
    }

    #[test]
    fn test_blockquote_with_multiple_tabs() {
        let rule = MD007ULIndent::default();
        let content = ">\t\t* List item\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // First-level list item at any indentation is allowed when start_indented=false (default)
        assert!(
            result.is_empty(),
            "First-level list item at any indentation is allowed when start_indented=false, got: {result:?}"
        );
    }

    #[test]
    fn test_nested_blockquote_with_tab() {
        let rule = MD007ULIndent::default();
        let content = ">\t>\t* List item\n>\t>\t  * Nested\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Nested blockquotes with tabs should work correctly, got: {result:?}"
        );
    }

    // Tests for smart style auto-detection (fixes issue #210 while preserving #209 fix)

    #[test]
    fn test_smart_style_pure_unordered_uses_fixed() {
        // Issue #210: Pure unordered lists with custom indent should use fixed style
        let rule = MD007ULIndent::new(4);

        // With fixed style (auto-detected), this should be valid
        let content = "* Level 0\n    * Level 1\n        * Level 2";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Pure unordered with indent=4 should use fixed style (0, 4, 8), got: {result:?}"
        );
    }

    #[test]
    fn test_smart_style_mixed_lists_uses_text_aligned() {
        // Issue #209: Mixed lists should use text-aligned to avoid oscillation
        let rule = MD007ULIndent::new(4);

        // With text-aligned style (auto-detected for mixed), bullets align with parent text
        let content = "1. Ordered\n   * Bullet aligns with 'Ordered' text (3 spaces)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Mixed lists should use text-aligned style, got: {result:?}"
        );
    }

    #[test]
    fn test_smart_style_explicit_fixed_overrides() {
        // When style is explicitly set to fixed, it should be respected even for mixed lists
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(4),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::Fixed,
            style_explicit: true, // Explicit setting
            indent_explicit: false,
        };
        let rule = MD007ULIndent::from_config_struct(config);

        // With explicit fixed style, expect fixed calculations even for mixed lists
        let content = "1. Ordered\n    * Should be at 4 spaces (fixed)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // The bullet is at 4 spaces which matches fixed style level 1
        assert!(
            result.is_empty(),
            "Explicit fixed style should be respected, got: {result:?}"
        );
    }

    #[test]
    fn test_smart_style_explicit_text_aligned_overrides() {
        // When style is explicitly set to text-aligned, it should be respected
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(4),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::TextAligned,
            style_explicit: true, // Explicit setting
            indent_explicit: false,
        };
        let rule = MD007ULIndent::from_config_struct(config);

        // With explicit text-aligned, pure unordered should use text-aligned (not auto-switch to fixed)
        let content = "* Level 0\n  * Level 1 (aligned with 'Level 0' text)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Explicit text-aligned should be respected, got: {result:?}"
        );

        // This would be correct for fixed but wrong for text-aligned
        let fixed_style_content = "* Level 0\n    * Level 1 (4 spaces - fixed style)";
        let ctx = LintContext::new(fixed_style_content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            !result.is_empty(),
            "With explicit text-aligned, 4-space indent should be wrong (expected 2)"
        );
    }

    #[test]
    fn test_smart_style_default_indent_no_autoswitch() {
        // When indent is default (2), no auto-switch happens (both styles produce same result)
        let rule = MD007ULIndent::new(2);

        let content = "* Level 0\n  * Level 1\n    * Level 2";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Default indent should work regardless of style, got: {result:?}"
        );
    }

    #[test]
    fn test_has_mixed_list_nesting_detection() {
        // Test the mixed list detection function directly

        // Pure unordered - no mixed nesting
        let content = "* Item 1\n  * Item 2\n    * Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            !ctx.has_mixed_list_nesting(),
            "Pure unordered should not be detected as mixed"
        );

        // Pure ordered - no mixed nesting
        let content = "1. Item 1\n   2. Item 2\n      3. Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            !ctx.has_mixed_list_nesting(),
            "Pure ordered should not be detected as mixed"
        );

        // Mixed: unordered under ordered
        let content = "1. Ordered\n   * Unordered child";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            ctx.has_mixed_list_nesting(),
            "Unordered under ordered should be detected as mixed"
        );

        // Mixed: ordered under unordered
        let content = "* Unordered\n  1. Ordered child";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            ctx.has_mixed_list_nesting(),
            "Ordered under unordered should be detected as mixed"
        );

        // Separate lists (not nested) - not mixed
        let content = "* Unordered\n\n1. Ordered (separate list)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            !ctx.has_mixed_list_nesting(),
            "Separate lists should not be detected as mixed"
        );

        // Mixed lists inside blockquotes should be detected
        let content = "> 1. Ordered in blockquote\n>    * Unordered child";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            ctx.has_mixed_list_nesting(),
            "Mixed lists in blockquotes should be detected"
        );
    }

    #[test]
    fn test_issue_210_exact_reproduction() {
        // Exact reproduction from issue #210
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(4),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::TextAligned, // Default
            style_explicit: false,                         // Not explicitly set - should auto-detect
            indent_explicit: false,                        // Not explicitly set
        };
        let rule = MD007ULIndent::from_config_struct(config);

        let content = "# Title\n\n* some\n    * list\n    * items\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        assert!(
            result.is_empty(),
            "Issue #210: indent=4 on pure unordered should work (auto-fixed style), got: {result:?}"
        );
    }

    #[test]
    fn test_issue_209_still_fixed() {
        // Verify issue #209 (oscillation) is still fixed when style is explicitly set
        // With issue #236 fix, explicit style must be set to get pure text-aligned behavior
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(3),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::TextAligned,
            style_explicit: true, // Explicit style to test text-aligned behavior
            indent_explicit: false,
        };
        let rule = MD007ULIndent::from_config_struct(config);

        // Mixed list from issue #209 - with explicit text-aligned, no oscillation
        let content = r#"# Header 1

- **Second item**:
  - **This is a nested list**:
    1. **First point**
       - First subpoint
"#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();

        assert!(
            result.is_empty(),
            "Issue #209: With explicit text-aligned style, should have no issues, got: {result:?}"
        );
    }

    // Edge case tests for review findings

    #[test]
    fn test_multi_level_mixed_detection_grandparent() {
        // Test that multi-level mixed detection finds grandparent type differences
        // ordered → unordered → unordered should be detected as mixed
        // because the grandparent (ordered) is different from descendants (unordered)
        let content = "1. Ordered grandparent\n   * Unordered child\n     * Unordered grandchild";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            ctx.has_mixed_list_nesting(),
            "Should detect mixed nesting when grandparent differs in type"
        );

        // unordered → ordered → ordered should also be detected as mixed
        let content = "* Unordered grandparent\n  1. Ordered child\n     2. Ordered grandchild";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            ctx.has_mixed_list_nesting(),
            "Should detect mixed nesting for ordered descendants under unordered"
        );
    }

    #[test]
    fn test_html_comments_skipped_in_detection() {
        // Lists inside HTML comments should not affect mixed detection
        let content = r#"* Unordered list
<!-- This is a comment
  1. This ordered list is inside a comment
     * This nested bullet is also inside
-->
  * Another unordered item"#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            !ctx.has_mixed_list_nesting(),
            "Lists in HTML comments should be ignored in mixed detection"
        );
    }

    #[test]
    fn test_blank_lines_separate_lists() {
        // Blank lines at root level should separate lists, treating them as independent
        let content = "* First unordered list\n\n1. Second list is ordered (separate)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            !ctx.has_mixed_list_nesting(),
            "Blank line at root should separate lists"
        );

        // But nested lists after blank should still be detected if mixed
        let content = "1. Ordered parent\n\n   * Still a child due to indentation";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            ctx.has_mixed_list_nesting(),
            "Indented list after blank is still nested"
        );
    }

    #[test]
    fn test_column_1_normalization() {
        // 1-space indent should be treated as column 0 (root level)
        // This creates a sibling relationship, not nesting
        let content = "* First item\n * Second item with 1 space (sibling)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let rule = MD007ULIndent::default();
        let result = rule.check(&ctx).unwrap();
        // The second item should be flagged as wrong (1 space is not valid for nesting)
        assert!(
            result.iter().any(|w| w.line == 2),
            "1-space indent should be flagged as incorrect"
        );
    }

    #[test]
    fn test_code_blocks_skipped_in_detection() {
        // Lists inside code blocks should not affect mixed detection
        let content = r#"* Unordered list
```
1. This ordered list is inside a code block
   * This nested bullet is also inside
```
  * Another unordered item"#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            !ctx.has_mixed_list_nesting(),
            "Lists in code blocks should be ignored in mixed detection"
        );
    }

    #[test]
    fn test_front_matter_skipped_in_detection() {
        // Lists inside YAML front matter should not affect mixed detection
        let content = r#"---
items:
  - yaml list item
  - another item
---
* Unordered list after front matter"#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            !ctx.has_mixed_list_nesting(),
            "Lists in front matter should be ignored in mixed detection"
        );
    }

    #[test]
    fn test_alternating_types_at_same_level() {
        // Alternating between ordered and unordered at the same nesting level
        // is NOT mixed nesting (they are siblings, not parent-child)
        let content = "* First bullet\n1. First number\n* Second bullet\n2. Second number";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            !ctx.has_mixed_list_nesting(),
            "Alternating types at same level should not be detected as mixed"
        );
    }

    #[test]
    fn test_five_level_deep_mixed_nesting() {
        // Test detection at 5+ levels of nesting
        let content = "* L0\n  1. L1\n     * L2\n       1. L3\n          * L4\n            1. L5";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(ctx.has_mixed_list_nesting(), "Should detect mixed nesting at 5+ levels");
    }

    #[test]
    fn test_very_deep_pure_unordered_nesting() {
        // Test pure unordered list with 10+ levels of nesting
        let mut content = String::from("* L1");
        for level in 2..=12 {
            let indent = "  ".repeat(level - 1);
            content.push_str(&format!("\n{indent}* L{level}"));
        }

        let ctx = LintContext::new(&content, crate::config::MarkdownFlavor::Standard, None);

        // Should NOT be detected as mixed (all unordered)
        assert!(
            !ctx.has_mixed_list_nesting(),
            "Pure unordered deep nesting should not be detected as mixed"
        );

        // Should use fixed style with custom indent
        let rule = MD007ULIndent::new(4);
        let result = rule.check(&ctx).unwrap();
        // With text-aligned default but auto-switch to fixed for pure unordered,
        // the first nested level should be flagged (2 spaces instead of 4)
        assert!(!result.is_empty(), "Should flag incorrect indentation for fixed style");
    }

    #[test]
    fn test_interleaved_content_between_list_items() {
        // Paragraph continuation between list items should not break detection
        let content = "1. Ordered parent\n\n   Paragraph continuation\n\n   * Unordered child";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            ctx.has_mixed_list_nesting(),
            "Should detect mixed nesting even with interleaved paragraphs"
        );
    }

    #[test]
    fn test_esm_blocks_skipped_in_detection() {
        // ESM import/export blocks in MDX should be skipped
        // Note: ESM detection depends on LintContext properly setting in_esm_block
        let content = "* Unordered list\n  * Nested unordered";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            !ctx.has_mixed_list_nesting(),
            "Pure unordered should not be detected as mixed"
        );
    }

    #[test]
    fn test_multiple_list_blocks_pure_then_mixed() {
        // Document with pure unordered list followed by mixed list
        // Detection should find the mixed list and return true
        let content = r#"* Pure unordered
  * Nested unordered

1. Mixed section
   * Bullet under ordered"#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            ctx.has_mixed_list_nesting(),
            "Should detect mixed nesting in any part of document"
        );
    }

    #[test]
    fn test_multiple_separate_pure_lists() {
        // Multiple pure unordered lists separated by blank lines
        // Should NOT be detected as mixed
        let content = r#"* First list
  * Nested

* Second list
  * Also nested

* Third list
  * Deeply
    * Nested"#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            !ctx.has_mixed_list_nesting(),
            "Multiple separate pure unordered lists should not be mixed"
        );
    }

    #[test]
    fn test_code_block_between_list_items() {
        // Code block between list items should not affect detection
        let content = r#"1. Ordered
   ```
   code
   ```
   * Still a mixed child"#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        assert!(
            ctx.has_mixed_list_nesting(),
            "Code block between items should not prevent mixed detection"
        );
    }

    #[test]
    fn test_blockquoted_mixed_detection() {
        // Mixed lists inside blockquotes should be detected
        let content = "> 1. Ordered in blockquote\n>    * Mixed child";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        // Note: Detection depends on correct marker_column calculation in blockquotes
        // This test verifies the detection logic works with blockquoted content
        assert!(
            ctx.has_mixed_list_nesting(),
            "Should detect mixed nesting in blockquotes"
        );
    }

    // Tests for "Do What I Mean" behavior (issue #273)

    #[test]
    fn test_indent_explicit_uses_fixed_style() {
        // When indent is explicitly set but style is not, use fixed style automatically
        // This is the "Do What I Mean" behavior for issue #273
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(4),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::TextAligned, // Default
            style_explicit: false,                         // Style NOT explicitly set
            indent_explicit: true,                         // Indent explicitly set
        };
        let rule = MD007ULIndent::from_config_struct(config);

        // With indent_explicit=true and style_explicit=false, should use fixed style
        // Fixed style with indent=4: level 0 = 0, level 1 = 4, level 2 = 8
        let content = "* Level 0\n    * Level 1\n        * Level 2";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "With indent_explicit=true, should use fixed style (0, 4, 8), got: {result:?}"
        );

        // Text-aligned spacing (2 spaces per level) should now be wrong
        let wrong_content = "* Level 0\n  * Level 1\n    * Level 2";
        let ctx = LintContext::new(wrong_content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            !result.is_empty(),
            "Should flag text-aligned spacing when indent_explicit=true"
        );
    }

    #[test]
    fn test_explicit_style_overrides_indent_explicit() {
        // When both indent and style are explicitly set, style wins
        // This ensures backwards compatibility and respects explicit user choice
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(4),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::TextAligned,
            style_explicit: true,  // Style explicitly set
            indent_explicit: true, // Indent also explicitly set (user will see warning)
        };
        let rule = MD007ULIndent::from_config_struct(config);

        // With explicit text-aligned style, should use text-aligned even with indent_explicit
        let content = "* Level 0\n  * Level 1\n    * Level 2";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Explicit text-aligned style should be respected, got: {result:?}"
        );
    }

    #[test]
    fn test_no_indent_explicit_uses_smart_detection() {
        // When neither is explicitly set, use smart per-parent detection (original behavior)
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(4),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::TextAligned,
            style_explicit: false,
            indent_explicit: false, // Neither explicitly set - use smart detection
        };
        let rule = MD007ULIndent::from_config_struct(config);

        // Pure unordered with neither explicit: per-parent logic applies
        // For pure unordered at expected positions, fixed style is used
        let content = "* Level 0\n    * Level 1";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // This should work with smart detection for pure unordered lists
        assert!(
            result.is_empty(),
            "Smart detection should accept 4-space indent, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_273_exact_reproduction() {
        // Exact reproduction from issue #273:
        // User sets `indent = 4` without setting style, expects 4-space increments
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(4),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::TextAligned, // Default (would use text-aligned)
            style_explicit: false,
            indent_explicit: true, // User explicitly set indent
        };
        let rule = MD007ULIndent::from_config_struct(config);

        let content = r#"* Item 1
    * Item 2
        * Item 3"#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Issue #273: indent=4 should use 4-space increments, got: {result:?}"
        );
    }

    #[test]
    fn test_indent_explicit_with_ordered_parent() {
        // When indent is explicitly set, both text-aligned and fixed indent are accepted
        // under ordered parents, since the user wants their configured indent but
        // text-aligned is also valid for ordered list children.
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(4),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::TextAligned,
            style_explicit: false,
            indent_explicit: true, // User set indent=4
        };
        let rule = MD007ULIndent::from_config_struct(config);

        // 4-space indent under "1. " should pass (matches configured indent)
        let content = "1. Ordered\n    * Bullet with 4-space indent";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "4-space indent under ordered should pass with indent=4: {result:?}"
        );

        // 3-space indent under "1. " should also pass (text-aligned with "1. ")
        let content_3 = "1. Ordered\n   * Bullet with 3-space indent";
        let ctx = LintContext::new(content_3, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "3-space indent under ordered should pass (text-aligned): {result:?}"
        );

        // 2-space indent under "1. " should be wrong (neither text-aligned nor fixed)
        let wrong_content = "1. Ordered\n  * Bullet with 2-space indent";
        let ctx = LintContext::new(wrong_content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            !result.is_empty(),
            "2-space indent under ordered list should be flagged when indent=4: {result:?}"
        );
    }

    #[test]
    fn test_indent_explicit_mixed_list_deep_nesting() {
        // Deep nesting with alternating list types tests the edge case thoroughly:
        // - Bullets under bullets: use configured indent (4)
        // - Bullets under ordered: use text-aligned
        // - Ordered under bullets: N/A (MD007 only checks bullets)
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(4),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::TextAligned,
            style_explicit: false,
            indent_explicit: true,
        };
        let rule = MD007ULIndent::from_config_struct(config);

        // Level 0: bullet (col 0)
        // Level 1: bullet (col 4 - fixed, parent is bullet)
        // Level 2: ordered (col 8 - not checked by MD007)
        // Level 3: bullet - text-aligned=11 (3 chars for "1. " from col 8), fixed=12
        // Both 11 (text-aligned) and 12 (fixed) should be accepted
        let content_text_aligned = r#"* Level 0
    * Level 1 (4-space indent from bullet parent)
        1. Level 2 ordered
           * Level 3 bullet (text-aligned under ordered)"#;
        let ctx = LintContext::new(content_text_aligned, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Text-aligned nesting under ordered should pass: {result:?}"
        );

        let content_fixed = r#"* Level 0
    * Level 1 (4-space indent from bullet parent)
        1. Level 2 ordered
            * Level 3 bullet (fixed indent under ordered)"#;
        let ctx = LintContext::new(content_fixed, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Fixed indent nesting under ordered should also pass: {result:?}"
        );
    }

    #[test]
    fn test_ordered_list_double_digit_markers() {
        // Ordered lists with 10+ items have wider markers ("10." vs "9.")
        // Bullets nested under these must text-align correctly
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(4),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::TextAligned,
            style_explicit: false,
            indent_explicit: true,
        };
        let rule = MD007ULIndent::from_config_struct(config);

        // "10. " = 4 chars, text-aligned = 4, fixed = 4
        let content = "10. Double digit\n    * Bullet at col 4";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Bullet under '10.' should align at column 4: {result:?}"
        );

        // Single digit "1. " = 3 chars, text-aligned = 3, fixed = 4
        // Both should be accepted under ordered parent with explicit indent
        let content_3 = "1. Single digit\n   * Bullet at col 3";
        let ctx = LintContext::new(content_3, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Bullet under '1.' with 3-space indent should pass (text-aligned): {result:?}"
        );

        let content_4 = "1. Single digit\n    * Bullet at col 4";
        let ctx = LintContext::new(content_4, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Bullet under '1.' with 4-space indent should pass (fixed): {result:?}"
        );
    }

    #[test]
    fn test_indent_explicit_pure_unordered_uses_fixed() {
        // Regression test: pure unordered lists should use fixed indent
        // when indent is explicitly configured
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(4),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::TextAligned,
            style_explicit: false,
            indent_explicit: true,
        };
        let rule = MD007ULIndent::from_config_struct(config);

        // Pure unordered with 4-space indent should pass
        let content = "* Level 0\n    * Level 1\n        * Level 2";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Pure unordered with indent=4 should use 4-space increments: {result:?}"
        );

        // Text-aligned (2-space) should fail with indent=4
        let wrong_content = "* Level 0\n  * Level 1\n    * Level 2";
        let ctx = LintContext::new(wrong_content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            !result.is_empty(),
            "2-space indent should be flagged when indent=4 is configured"
        );
    }

    #[test]
    fn test_mkdocs_ordered_list_with_4_space_nested_unordered() {
        // MkDocs (Python-Markdown) requires 4-space continuation for ordered
        // list items. `1. text` has content at column 3, but Python-Markdown
        // needs marker_col + 4 = 4 spaces minimum.
        let rule = MD007ULIndent::default();
        let content = "1. text\n\n    - nested item";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "4-space indent under ordered list should be valid in MkDocs flavor, got: {result:?}"
        );
    }

    #[test]
    fn test_standard_flavor_ordered_list_with_3_space_nested_unordered() {
        // Without MkDocs, `1. text` has content at column 3,
        // so 3-space indent is correct (text-aligned).
        let rule = MD007ULIndent::default();
        let content = "1. text\n\n   - nested item";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "3-space indent under ordered list should be valid in Standard flavor, got: {result:?}"
        );
    }

    #[test]
    fn test_standard_flavor_ordered_list_under_ordered_is_exempt() {
        // markdownlint exempts unordered sublists of an ordered list from MD007
        // ("applies only if parent lists are all also unordered"). A 4-space bullet
        // under `1. text` (content column 3) is a genuine sublist, so it must not be
        // flagged. Verified: markdownlint-cli2 reports 0 MD007 errors here.
        let rule = MD007ULIndent::default();
        let content = "1. text\n\n    - nested item";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "unordered sublist of an ordered list must be exempt in Standard flavor, got: {result:?}"
        );
    }

    #[test]
    fn test_mkdocs_multi_digit_ordered_list() {
        // `10. text` has content at column 4, which already meets
        // the 4-space minimum (marker_col 0 + 4 = 4). No adjustment needed.
        let rule = MD007ULIndent::default();
        let content = "10. text\n\n    - nested item";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "4-space indent under `10.` should be valid in MkDocs flavor, got: {result:?}"
        );
    }

    #[test]
    fn test_mkdocs_triple_digit_ordered_list() {
        // `100. text` has content at column 5, which exceeds
        // the 4-space minimum (marker_col 0 + 4 = 4). No adjustment needed.
        let rule = MD007ULIndent::default();
        let content = "100. text\n\n     - nested item";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "5-space indent under `100.` should be valid in MkDocs flavor, got: {result:?}"
        );
    }

    #[test]
    fn test_mkdocs_insufficient_indent_under_ordered() {
        // In MkDocs, 2-space indent under `1. text` is insufficient.
        // Expected: marker_col(0) + 4 = 4, got: 2.
        let rule = MD007ULIndent::default();
        let content = "1. text\n\n  - nested item";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(
            result.len(),
            1,
            "2-space indent under ordered list should warn in MkDocs flavor"
        );
        assert!(
            result[0].message.contains("Expected 4"),
            "Warning should expect 4 spaces (MkDocs minimum), got: {}",
            result[0].message
        );
    }

    #[test]
    fn test_mkdocs_deeper_nesting_under_ordered() {
        // `1. text` -> `    - sub` (4 spaces) -> `      - subsub` (6 spaces)
        // The sub-item at 4 spaces is correct for MkDocs.
        // The sub-sub-item at 6 spaces: parent is unordered at col 4 with content at col 6,
        // so 6-space indent is text-aligned (correct).
        let rule = MD007ULIndent::default();
        let content = "1. text\n\n    - sub\n      - subsub";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Deeper nesting under ordered list should be valid in MkDocs flavor, got: {result:?}"
        );
    }

    #[test]
    fn test_mkdocs_fix_adjusts_to_4_spaces() {
        // Verify that auto-fix corrects 3-space indent to 4-space in MkDocs
        let rule = MD007ULIndent::default();
        let content = "1. text\n\n   - nested item";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1, "3-space indent should warn in MkDocs");
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(
            fixed, "1. text\n\n    - nested item",
            "Fix should adjust indent to 4 spaces in MkDocs"
        );
    }

    #[test]
    fn test_mkdocs_start_indented_with_ordered_parent() {
        // start_indented mode with MkDocs: the MkDocs adjustment should still apply
        // as a floor on top of the start_indented calculation.
        let config = MD007Config {
            start_indented: true,
            ..Default::default()
        };
        let rule = MD007ULIndent::from_config_struct(config);
        let content = "1. text\n\n    - nested item";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "4-space indent under ordered list with start_indented should be valid in MkDocs, got: {result:?}"
        );
    }

    #[test]
    fn test_mkdocs_ordered_at_nonzero_indent() {
        // Ordered list nested inside an unordered list, with a further unordered child.
        // `- outer` at col 0, `  1. inner` at col 2, `      - deep` at col 6.
        // For `deep`: parent is ordered at marker_col=2, so MkDocs minimum = 2+4 = 6.
        // Text-aligned: content_col of `1. inner` = 5. max(5, 6) = 6.
        let rule = MD007ULIndent::default();
        let content = "- outer\n  1. inner\n      - deep";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "6-space indent under nested ordered list should be valid in MkDocs, got: {result:?}"
        );
    }

    #[test]
    fn test_mkdocs_blockquoted_ordered_list() {
        // Blockquoted ordered list in MkDocs: the indent is relative to
        // the blockquote content, so `> 1. text` with `>     - nested`
        // has 4 spaces of indent within the blockquote context.
        let rule = MD007ULIndent::default();
        let content = "> 1. text\n>\n>     - nested item";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "4-space indent under blockquoted ordered list should be valid in MkDocs, got: {result:?}"
        );
    }

    #[test]
    fn test_mkdocs_ordered_at_nonzero_indent_insufficient() {
        // Same structure but with only 5 spaces for `deep`.
        // MkDocs minimum = marker_col(2) + 4 = 6, but got 5. Should warn.
        let rule = MD007ULIndent::default();
        let content = "- outer\n  1. inner\n     - deep";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::MkDocs, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(
            result.len(),
            1,
            "5-space indent under nested ordered at col 2 should warn in MkDocs (needs 6)"
        );
    }

    #[test]
    fn test_issue_504_indent4_ordered_parent() {
        // Reproduction case from issue #504:
        // With indent=4, nested unordered items under ordered parent
        // should accept 4-space indentation
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(4),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::TextAligned,
            style_explicit: false,
            indent_explicit: true,
        };
        let rule = MD007ULIndent::from_config_struct(config);

        let content = r#"# Things

+ An unordered list
    + An item with 4 spaces, ok.

1. A numbered list
    + A sublist with 4 spaces, not ok
        + A sub item with 4 spaces, ok
    + Why is rumdl expecting 3 spaces for a 4 space indent?
2. Item 2
3. Item 3"#;
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Issue #504: indent=4 with ordered parent should accept 4-space indent: {result:?}"
        );
    }

    #[test]
    fn test_indent2_explicit_with_ordered_parent() {
        // When indent=2 is explicit and parent is "1. " (text-aligned=3),
        // both 2 (fixed) and 3 (text-aligned) should be accepted
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(2),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::TextAligned,
            style_explicit: false,
            indent_explicit: true,
        };
        let rule = MD007ULIndent::from_config_struct(config);

        // 3-space indent should pass (text-aligned with "1. ")
        let content = "1. Ordered\n   * Bullet at 3 spaces";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "indent=2 under '1.' should accept text-aligned (3 spaces): {result:?}"
        );

        // 2-space indent should also pass (matches configured fixed indent)
        let content_2 = "1. Ordered\n  * Bullet at 2 spaces";
        let ctx = LintContext::new(content_2, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "indent=2 under '1.' should accept fixed indent (2 spaces): {result:?}"
        );
    }

    // Issue #638: MD007 must not fire on unordered items nested under an ordered
    // list. markdownlint: "applies to a sublist only if its parent lists are all
    // also unordered." Verified against markdownlint-cli2 v0.18.1 (0 MD007 errors).
    const ISSUE_638_INPUT: &str = "# Title\n\n1. Some text\n   - Indented text\n     - more indented\n";

    #[test]
    fn test_issue_638_unordered_under_ordered_smart_default() {
        let rule = MD007ULIndent::new(2);
        let ctx = LintContext::new(ISSUE_638_INPUT, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "smart default: unordered items under an ordered list must not be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_unordered_under_ordered_indent_explicit() {
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(2),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::TextAligned,
            style_explicit: false,
            indent_explicit: true,
        };
        let rule = MD007ULIndent::from_config_struct(config);
        let ctx = LintContext::new(ISSUE_638_INPUT, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "indent=2 explicit: unordered items under an ordered list must not be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_unordered_under_ordered_style_fixed() {
        // The reporter's exact config: indent = 2, style = "fixed".
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(2),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::Fixed,
            style_explicit: true,
            indent_explicit: true,
        };
        let rule = MD007ULIndent::from_config_struct(config);
        let ctx = LintContext::new(ISSUE_638_INPUT, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "style=fixed: unordered items under an ordered list must not be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_deeper_unordered_chain_under_ordered() {
        // Every unordered item below the ordered ancestor is exempt, at any depth.
        let rule = MD007ULIndent::new(2);
        let content = "1. Ordered\n   - child\n      - grandchild\n         - great-grandchild\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "all unordered descendants of an ordered list are exempt, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_pure_unordered_still_checked() {
        // Guard: the exemption must not leak into pure unordered lists.
        let rule = MD007ULIndent::new(2);
        let content = "- Top\n   - three spaces (wrong, expected 2)\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(
            result.len(),
            1,
            "pure unordered nesting must still be checked, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_exemption_not_applied_after_list_terminated_by_paragraph() {
        // A top-level paragraph terminates the ordered list. The later, separately
        // indented unordered list is NOT a sublist of the (now-closed) ordered item, so
        // the ordered-ancestor exemption must not apply: MD007 flags both the misindented
        // top-level item and its child. Verified against markdownlint-cli2, which reports
        // MD007 on the parent (Expected: 0; Actual: 3) and the child (Expected: 2;
        // Actual: 6).
        let rule = MD007ULIndent::new(2);
        let content = "1. ordered\n\nparagraph\n\n   - parent\n      - child six\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(
            result.len(),
            2,
            "the new top-level list following a terminated ordered list is checked at both levels, got: {result:?}"
        );
        assert!(
            result.iter().any(|w| w.line == 5 && w.message.contains("Expected 0")),
            "the misindented top-level item must be flagged with Expected 0, got: {result:?}"
        );
        assert!(
            result
                .iter()
                .any(|w| w.line == 6 && w.message.contains("Expected 2") && w.message.contains("found 6")),
            "the misindented child must be flagged with Expected 2, found 6, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_lazy_continuation_does_not_terminate_ordered_list() {
        // A non-indented paragraph line that immediately follows the ordered item
        // (no blank line between) is a CommonMark lazy continuation of that item,
        // so the ordered list stays open and its unordered sublist is exempt.
        // markdownlint-cli2 reports 0 MD007 errors here; the stale-ancestor
        // termination must not fire on a lazy continuation line.
        let rule = MD007ULIndent::new(2);
        let content = "1. ordered\nlazy continuation\n   - child\n     - grandchild\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "lazy continuation must not terminate the ordered list; sublist stays exempt, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_heading_interrupts_ordered_list_without_blank() {
        // Unlike a lazy paragraph continuation, an ATX heading interrupts the open
        // paragraph and therefore terminates the ordered list even without an
        // intervening blank line. The following bullets are then a new top-level list,
        // so both the misindented top item and its child are flagged. markdownlint-cli2
        // reports MD007 on the top item (Expected: 0; Actual: 3) and the child
        // (Expected: 2; Actual: 5).
        let rule = MD007ULIndent::new(2);
        let content = "1. ordered\n# heading\n   - child\n     - grandchild\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(
            result.len(),
            2,
            "a heading terminates the ordered list, so the new top-level list and its child are both checked, got: {result:?}"
        );
        assert!(
            result.iter().any(|w| w.line == 3 && w.message.contains("Expected 0")),
            "the misindented top-level item must be flagged with Expected 0, got: {result:?}"
        );
        assert!(
            result.iter().any(|w| w.line == 4 && w.message.contains("Expected 2")),
            "the misindented child must be flagged with Expected 2, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_lazy_continuation_inside_blockquote_keeps_exemption() {
        // Inside a blockquote, a plain continuation line in the same quote is a
        // lazy paragraph continuation of the ordered item, so the list stays open
        // and its sublist remains exempt. markdownlint-cli2 reports 0 MD007 errors;
        // termination must operate in blockquote-content coordinates, not absolute.
        let rule = MD007ULIndent::new(2);
        let content = "> 1. ordered\n> continuation\n>\n>    - child\n>      - grandchild\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "a lazy continuation within the same blockquote must keep the sublist exempt, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_indented_fence_inside_blockquoted_ordered_item_keeps_exemption() {
        // A fenced code block indented to the ordered item's content column, all
        // within a blockquote, is part of that item. The list stays open and the
        // sublist remains exempt. markdownlint-cli2 reports 0 MD007 errors; the
        // skip-region termination must use blockquote-content-relative indent.
        let rule = MD007ULIndent::new(2);
        let content = "> 1. ordered\n>    ```\n>    code\n>    ```\n>    - child\n>      - grandchild\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "an indented fence inside a blockquoted ordered item must keep the sublist exempt, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_fenced_code_block_terminates_ordered_list() {
        // A top-level fenced code block (its opening fence not indented into the
        // item) terminates the ordered list. Because the rule skips code-block
        // lines, the stale ordered ancestor must still be cleared so the exemption
        // does not leak to a later list. markdownlint-cli2 flags the misindented
        // child (Expected: 2; Actual: 6).
        let rule = MD007ULIndent::new(2);
        let content = "1. ordered\n```\ncode\n```\n\n   - parent\n      - child\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.iter().any(|w| w.line == 7),
            "a top-level fenced code block terminates the ordered list; the child must be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_fenced_code_block_inside_item_keeps_exemption() {
        // A fenced code block indented into the ordered item's content column is
        // part of that item, so the list stays open and the sublist remains exempt.
        // markdownlint-cli2 reports 0 MD007 errors; termination must not over-fire
        // on the code block's interior lines.
        let rule = MD007ULIndent::new(2);
        let content = "1. ordered\n   ```\n   code\n   ```\n   - child\n     - grandchild\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "a fenced code block nested inside the item must keep the sublist exempt, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_blockquote_terminates_ordered_list() {
        // A top-level blockquote interrupts the open paragraph and terminates the
        // ordered list (it is not indented into the item's content). The later,
        // separately indented unordered list is therefore NOT a sublist of the
        // closed ordered item, so the ordered-ancestor exemption must not leak:
        // the misindented child must still be flagged. markdownlint-cli2 reports
        // MD007 on the child (Expected: 2; Actual: 6).
        let rule = MD007ULIndent::new(2);
        let content = "1. ordered\n> quote\n\n   - parent\n      - child\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.iter().any(|w| w.line == 5),
            "blockquote terminates the ordered list, so the child must still be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_blockquote_inside_item_keeps_exemption() {
        // When the blockquote is indented into the ordered item's content column it
        // is part of that item, so the list stays open and its unordered sublist
        // remains exempt. markdownlint-cli2 reports 0 MD007 errors here; the
        // termination must not over-fire on a blockquote nested inside the item.
        let rule = MD007ULIndent::new(2);
        let content = "1. ordered\n   > quote inside item\n   - child\n     - grandchild\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "a blockquote nested inside the item must keep the sublist exempt, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_exemption_requires_genuine_nesting_under_ordered() {
        // A wide ordered marker ("100. ") has its content at column 5. An unordered
        // bullet indented only 3 spaces is left of that content column, so it is NOT
        // nested under the ordered item but a new top-level list. The ordered-ancestor
        // exemption must not leak through this non-nested bullet to its child: with
        // the ordered item no longer a genuine ancestor, the misindented child must
        // still be checked. markdownlint-cli2 flags both the parent (Expected: 0) and
        // the child (Expected: 2). The exemption must not suppress the child, and the
        // fix must not flatten the child into a sibling of the parent.
        let rule = MD007ULIndent::new(2);
        let content = "100. ordered\n   - parent\n     - child\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.iter().any(|w| w.line == 3),
            "the child of a non-nested bullet must still be checked, not exempted; got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_paragraph_after_fenced_code_closes_ordered_list() {
        // A fenced code block inside an ordered item is not paragraph text, so an
        // unindented line after the closing fence is NOT a lazy paragraph continuation:
        // it closes the list. The later, separately indented bullet list is therefore a
        // new top-level list, not a sublist of the ordered item, so the ordered-ancestor
        // exemption must not leak: the misindented child must still be flagged.
        // (markdownlint-cli2 also flags the parent with Expected: 0; rumdl does not flag
        // indented top-level list items, a separate pre-existing limitation, so we assert
        // only the child here - the part this fix governs.)
        let rule = MD007ULIndent::new(2);
        let content = "1. ordered\n   ```\n   code\n   ```\nnot lazy text\n   - parent\n     - child\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.iter().any(|w| w.line == 7),
            "fenced code is not paragraph text, so the list closes and the nested child must still be checked, not exempted; got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_overlong_ordered_marker_is_lazy_continuation() {
        // CommonMark ordered list markers allow at most 9 digits. A run of 10+ digits
        // (`1234567890.`) is not a valid marker, so the line is a lazy paragraph
        // continuation of the open ordered item, which keeps the list open. The nested
        // bullets remain a sublist under the ordered item and are exempt from MD007.
        // markdownlint-cli2 reports no MD007 warnings here.
        let rule = MD007ULIndent::new(2);
        let content = "1. ordered\n1234567890. this is continuation text\n   - child\n     - grandchild\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "an overlong digit run is not a valid ordered marker, so the list stays open and the nested bullets are exempt; got: {result:?}"
        );
    }

    #[test]
    fn test_indented_top_level_list_item_is_flagged() {
        // A top-level unordered list item indented 2 or 3 spaces is a misindented list
        // (4+ spaces would be an indented code block, not a list). markdownlint-cli2
        // flags the top item with "Expected: 0". rumdl must flag it too, not only its
        // children. The default config has start_indented = false, so the expected
        // indent for a depth-0 item is column 0.
        let rule = MD007ULIndent::new(2);
        for indent in 2..=3 {
            let pad = " ".repeat(indent);
            let content = format!("{pad}- parent\n{pad}  - child\n");
            let ctx = LintContext::new(&content, crate::config::MarkdownFlavor::Standard, None);
            let result = rule.check(&ctx).unwrap();
            assert!(
                result.iter().any(|w| w.line == 1),
                "a top-level item indented {indent} spaces must be flagged (Expected 0); got: {result:?}"
            );
        }
    }

    #[test]
    fn test_indented_code_block_bullet_is_not_a_list_item() {
        // Four or more leading spaces at the top level form an indented code block, not a
        // list, so MD007 must not fire. Both rumdl and markdownlint-cli2 stay silent.
        let rule = MD007ULIndent::new(2);
        let content = "    - not a list, this is code\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "a 4-space-indented bullet is an indented code block, not a misindented list; got: {result:?}"
        );
    }

    #[test]
    fn test_tab_indent_expands_to_four_column_tabstop() {
        // CommonMark expands a leading tab to the next 4-column tab stop when it helps
        // define block structure. A single-tab-indented sublist therefore sits at visual
        // column 4, which is an over-indent for depth 1 (expected 2). rumdl must report
        // the expanded column (found 4), NOT a raw character count of 1. (markdownlint
        // counts the tab as a single character and reports "Actual 1"; that is incorrect
        // per the CommonMark tab-stop rule, so rumdl deliberately diverges here.)
        let rule = MD007ULIndent::new(2);
        let content = "- a\n\t- b\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        let warning = result
            .iter()
            .find(|w| w.line == 2)
            .expect("a tab-indented sublist at column 4 is over-indented for depth 1 and must be flagged");
        assert!(
            warning.message.contains("found 4"),
            "the tab must expand to the 4-column tab stop (found 4), not be counted as one character; got: {}",
            warning.message
        );
    }

    #[test]
    fn test_tab_completing_two_space_indent_to_tabstop_is_accepted() {
        // Two spaces advance to column 2; a following tab then advances to the next
        // 4-column tab stop, landing the sublist marker at column 4 - exactly the
        // expected indent for depth 2. With correct tab-stop math the line is well
        // indented and must produce no warning. (markdownlint miscounts `  \t` as three
        // characters and false-positives with "Actual 3"; rumdl correctly stays silent.)
        let rule = MD007ULIndent::new(2);
        let content = "- a\n  - b\n  \t- c\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "`  \\t` expands to column 4, the correct depth-2 indent, so no MD007 warning is expected; got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_html_comment_terminates_ordered_list() {
        // An HTML comment is a block construct that interrupts the open paragraph and
        // terminates the ordered list, just like a heading or fenced code block. The
        // later, separately indented unordered list is therefore not a sublist of the
        // closed ordered item, so the ordered-ancestor exemption must not leak: the
        // misindented child must still be flagged. markdownlint-cli2 reports MD007 on
        // the child (Expected: 2; Actual: 6).
        let rule = MD007ULIndent::new(2);
        let content = "1. ordered\n<!-- comment -->\n\n   - parent\n      - child\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.iter().any(|w| w.line == 5),
            "an HTML comment terminates the ordered list, so the child must still be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_blockquoted_list_item_terminates_ordered_list() {
        // A blockquoted list item that begins left of the ordered item's content
        // column starts a new container and terminates the ordered list (the `>` is
        // not indented into the item). The later, separately indented unordered list
        // is therefore not a sublist of the closed ordered item, so the
        // ordered-ancestor exemption must not leak: the misindented child must still
        // be flagged. markdownlint-cli2 reports MD007 on the child
        // (Expected: 2; Actual: 5).
        let rule = MD007ULIndent::new(2);
        let content = "1. ordered\n> - quote list\n\n   - parent\n     - child\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.iter().any(|w| w.line == 5),
            "a blockquoted list item terminates the ordered list, so the child must still be flagged, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_deeper_nested_quote_terminates_blockquoted_ordered_list() {
        // A blockquoted ordered item (`> 1. ordered`) is interrupted by a deeper
        // nested quote (`> > quote`). The inner `>` begins left of the ordered
        // item's content column (in the item's own quote coordinate space), so it
        // is a sibling block that closes the ordered list, not a continuation of
        // it. The unordered list that follows inside the same depth-1 quote is
        // therefore a fresh top-level list, not a sublist of the (closed) ordered
        // item, so the ordered-ancestor exemption must NOT leak to it.
        // markdownlint-cli2 (MD007 only) reports the parent (Expected: 0; Actual: 3)
        // and the child (Expected: 2; Actual: 6).
        let rule = MD007ULIndent::new(2);
        let content = "> 1. ordered\n> > quote\n>\n>    - parent\n>       - child\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.iter().any(|w| w.line == 4),
            "deeper nested quote closes the ordered list, so the misindented parent must be flagged, got: {result:?}"
        );
        assert!(
            result.iter().any(|w| w.line == 5),
            "the child of the fresh unordered list must be flagged, not exempted, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_deeper_quote_list_item_terminates_blockquoted_ordered_list() {
        // Same leak as the deeper-nested-quote case, but the interrupting deeper
        // quote is itself a list item (`> > - quote list`). Its marker begins left
        // of the ordered item's content column (in the item's coordinate space), so
        // it closes the ordered list. The unordered list that follows in the depth-1
        // quote is therefore a fresh top-level list and must not inherit the
        // ordered-ancestor exemption. markdownlint-cli2 reports the parent
        // (Expected: 0; Actual: 3) and the child (Expected: 2; Actual: 6).
        let rule = MD007ULIndent::new(2);
        let content = "> 1. ordered\n> > - quote list\n>\n>    - parent\n>       - child\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.iter().any(|w| w.line == 4),
            "a deeper-quote list item closes the ordered list, so the parent must be flagged, got: {result:?}"
        );
        assert!(
            result.iter().any(|w| w.line == 5),
            "the child of the fresh unordered list must be flagged, not exempted, got: {result:?}"
        );
    }

    #[test]
    fn test_issue_638_deeper_quote_indented_into_item_keeps_exemption() {
        // When the deeper quote is indented to (or past) the ordered item's content
        // column, the `> quote` is a child block of the item, so the ordered list
        // stays open and its unordered sublist remains exempt. The termination must
        // not over-fire. markdownlint-cli2 reports 0 MD007 errors here.
        let rule = MD007ULIndent::new(2);
        let content = "> 1. ordered\n>    > quote inside item\n>    - child\n>      - grandchild\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "a deeper quote indented into the item must keep the sublist exempt, got: {result:?}"
        );
    }

    #[test]
    fn test_indent4_explicit_with_wide_ordered_parent() {
        // When indent=4 and parent is "100. " (text-aligned=5),
        // both 4-space and 5-space indent should be accepted.
        // The list parser may recognize 4-space as valid nesting under "100."
        let config = MD007Config {
            indent: crate::types::IndentSize::from_const(4),
            start_indented: false,
            start_indent: crate::types::IndentSize::from_const(2),
            style: md007_config::IndentStyle::TextAligned,
            style_explicit: false,
            indent_explicit: true,
        };
        let rule = MD007ULIndent::from_config_struct(config);

        // 5-space indent should pass
        let content = "100. Wide ordered\n     * Bullet at 5 spaces";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "indent=4 under '100.' should accept 5-space indent: {result:?}"
        );

        // 4-space indent should also pass (matches configured indent)
        let content_4 = "100. Wide ordered\n    * Bullet at 4 spaces";
        let ctx = LintContext::new(content_4, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "indent=4 under '100.' should accept 4-space indent: {result:?}"
        );
    }
}