rumdl 0.3.0

A fast Markdown linter and formatter written in Rust
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
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
//! Main processor for code block linting and formatting.
//!
//! This module coordinates language resolution, tool lookup, execution,
//! and result collection for processing code blocks in markdown files.

use super::config::{CodeBlockToolsConfig, FormatMode, LanguageToolConfig, NormalizeLanguage, OnError, OnMissing};
use super::executor::{ExecutorError, FormatCheckOutput, ToolExecutor, ToolOutput};
use super::linguist::LinguistResolver;
use super::registry::{BuiltinLintMode, ToolRegistry, ToolSlot};
use crate::config::MarkdownFlavor;
use crate::rule::{LintWarning, Severity};
use crate::utils::parser_options::rumdl_parser_options_for;
use pulldown_cmark::{CodeBlockKind, Event, Parser, Tag, TagEnd};

/// Special built-in tool name for rumdl's own markdown linting.
/// When this tool is configured for markdown blocks, the processor skips
/// external execution since it's handled by embedded markdown linting.
pub const RUMDL_BUILTIN_TOOL: &str = "rumdl";

/// Embedded Markdown tools are handled in-process, never as external commands.
pub fn is_rumdl_builtin(tool_id: &str) -> bool {
    matches!(tool_id, RUMDL_BUILTIN_TOOL | "rumdl:lint" | "rumdl:format")
}

/// Check if a language is markdown (handles common variations).
fn is_markdown_language(lang: &str) -> bool {
    matches!(lang.to_lowercase().as_str(), "markdown" | "md")
}

/// Strip ANSI escape sequences from tool output.
///
/// Many tools output colored text (e.g. `\x1b[1;31mError\x1b[0m`), which prevents
/// structured parsers from matching patterns like `file:line:col: message`.
fn strip_ansi_codes(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\x1b' {
            if chars.peek() == Some(&'[') {
                chars.next();
                // Consume until we hit an ASCII letter (the terminator)
                while let Some(&next) = chars.peek() {
                    chars.next();
                    if next.is_ascii_alphabetic() {
                        break;
                    }
                }
            }
        } else {
            result.push(c);
        }
    }
    result
}

/// Decode the percent escapes a GitHub Actions workflow command puts in its message.
///
/// `%25` encodes `%` itself, so decoding runs left to right in a single pass: replacing
/// the escapes one after another would turn a literal `%250A` into a line break. A
/// diagnostic occupies one line, so an encoded line break decodes to a space.
fn decode_workflow_command(message: &str) -> String {
    let mut decoded = String::with_capacity(message.len());
    let mut rest = message;

    while let Some(index) = rest.find('%') {
        decoded.push_str(&rest[..index]);
        let escape = rest[index..].get(..3).unwrap_or_default();
        let replacement = if escape.eq_ignore_ascii_case("%25") {
            "%"
        } else if escape.eq_ignore_ascii_case("%0a") || escape.eq_ignore_ascii_case("%0d") {
            " "
        } else if escape.eq_ignore_ascii_case("%3a") {
            ":"
        } else if escape.eq_ignore_ascii_case("%2c") {
            ","
        } else {
            // Not an escape this format defines; the `%` is literal text.
            decoded.push('%');
            rest = &rest[index + 1..];
            continue;
        };
        decoded.push_str(replacement);
        rest = &rest[index + 3..];
    }

    decoded.push_str(rest);
    decoded
}

/// Ensure content handed to an external tool is newline-terminated.
///
/// Code block extraction strips the line ending before the closing fence, but
/// per CommonMark that final newline is part of the block's content. External
/// tools treat their stdin as a complete file, so a tool with an end-of-file
/// newline rule (e.g. yamllint/ryl `new-line-at-end-of-file`, ruff `W292`)
/// would otherwise emit a false positive. Genuinely empty content is left
/// untouched so we never synthesize content where there is none.
fn ensure_trailing_newline(content: &str) -> std::borrow::Cow<'_, str> {
    if content.is_empty() || content.ends_with('\n') {
        std::borrow::Cow::Borrowed(content)
    } else {
        std::borrow::Cow::Owned(format!("{content}\n"))
    }
}

/// Information about a fenced code block for processing.
#[derive(Debug, Clone)]
pub struct FencedCodeBlockInfo {
    /// 0-indexed line number where opening fence starts.
    pub start_line: usize,
    /// 0-indexed line number of the block's last line: its closing fence, or its
    /// last code line when the block is unterminated.
    pub end_line: usize,
    /// Byte offset where the source of the code starts (the first content line,
    /// container prefix included).
    pub content_start: usize,
    /// Byte offset where the source of the code ends: after the line ending of
    /// its last line, or at the end of the document for an unterminated last line.
    pub content_end: usize,
    /// Language tag extracted from info string (first token).
    pub language: String,
    /// Full info string from the fence.
    pub info_string: String,
    /// The fence character used (` or ~).
    pub fence_char: char,
    /// Length of the fence (3 or more).
    pub fence_length: usize,
    /// What precedes every content line for the containers the block sits in:
    /// blockquote markers, list continuation indent and the fence's own indent.
    /// Formatted code is written back behind it.
    pub content_prefix: String,
    /// The code as CommonMark reads it, with every container prefix removed.
    /// This is what a tool receives.
    pub code: String,
    /// Width in characters of the source prefix removed from each line of `code`,
    /// so a tool's column can be placed in the document.
    pub line_prefix_widths: Vec<usize>,
}

impl FencedCodeBlockInfo {
    /// The source that replaces `content_start..content_end` so the block's code
    /// becomes `code`, every line inside the block's containers.
    pub fn source_for(&self, code: &str) -> String {
        apply_content_prefix(code, &self.content_prefix)
    }

    /// The document column of a 1-indexed `column` on 1-indexed `line` of the
    /// code. `None` for a line the code does not have.
    pub fn document_column(&self, line: usize, column: usize) -> Option<usize> {
        let width = self.line_prefix_widths.get(line.checked_sub(1)?)?;
        Some(column + width)
    }
}

/// A diagnostic message from an external tool.
#[derive(Debug, Clone)]
pub struct CodeBlockDiagnostic {
    /// Line number in the original markdown file (1-indexed).
    pub file_line: usize,
    /// Column number (1-indexed, if available).
    pub column: Option<usize>,
    /// Message from the tool.
    pub message: String,
    /// Severity (error, warning, info).
    pub severity: DiagnosticSeverity,
    /// Name of the tool that produced this.
    pub tool: String,
    /// Line where the code block starts (1-indexed, for context).
    pub code_block_start: usize,
}

/// Severity level for diagnostics.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiagnosticSeverity {
    Error,
    Warning,
    Info,
}

impl CodeBlockDiagnostic {
    /// Convert to a LintWarning for integration with rumdl's warning system.
    /// The report for a block whose formatted code was not written back
    /// because it would change the document's structure.
    pub fn refused_write_back(line: usize) -> Self {
        Self {
            file_line: line,
            column: None,
            message:
                "Formatted code cannot be written back without changing the document's structure; block left unchanged"
                    .to_string(),
            severity: DiagnosticSeverity::Error,
            tool: CODE_BLOCK_TOOLS_DIAGNOSTIC_NAME.to_string(),
            code_block_start: line,
        }
    }

    pub fn to_lint_warning(&self) -> LintWarning {
        let severity = match self.severity {
            DiagnosticSeverity::Error => Severity::Error,
            DiagnosticSeverity::Warning => Severity::Warning,
            DiagnosticSeverity::Info => Severity::Info,
        };

        LintWarning {
            message: self.message.clone(),
            line: self.file_line,
            column: self.column.unwrap_or(1),
            end_line: self.file_line,
            end_column: self.column.unwrap_or(1),
            severity,
            fix: None, // External tool diagnostics don't provide fixes
            rule_name: Some(self.tool.clone()),
        }
    }
}

/// Error during code block processing.
#[derive(Debug, Clone)]
pub enum ProcessorError {
    /// Tool execution failed.
    ToolError(ExecutorError),
    /// Tool execution failed with code block location context.
    ToolErrorAt {
        error: ExecutorError,
        line: usize,
        language: String,
    },
    /// No tools configured for language.
    NoToolsConfigured { language: String, line: usize },
    /// Tool binary not found.
    ToolBinaryNotFound {
        tool: String,
        language: String,
        line: usize,
    },
    /// A coverage or configuration policy stopped processing.
    Policy { message: String, line: usize },
    /// Processing was aborted due to on_error = fail.
    Aborted { message: String },
}

impl std::fmt::Display for ProcessorError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::ToolError(e) => write!(f, "{e}"),
            Self::ToolErrorAt { error, line, language } => {
                write!(f, "line {line} ({language}): {error}")
            }
            Self::NoToolsConfigured { language, line } => {
                write!(f, "line {line} ({language}): no tools configured")
            }
            Self::ToolBinaryNotFound { tool, language, line } => {
                write!(
                    f,
                    "line {line} ({language}): tool '{tool}' not found in allowed lookup locations"
                )
            }
            Self::Policy { message, line } => write!(f, "line {line}: {message}"),
            Self::Aborted { message } => write!(f, "Processing aborted: {message}"),
        }
    }
}

impl ProcessorError {
    /// The 1-indexed line of the code block the error is about, when it is about one.
    pub fn line(&self) -> Option<usize> {
        match self {
            Self::ToolErrorAt { line, .. }
            | Self::NoToolsConfigured { line, .. }
            | Self::ToolBinaryNotFound { line, .. }
            | Self::Policy { line, .. } => Some(*line),
            Self::ToolError(_) | Self::Aborted { .. } => None,
        }
    }

    /// The error without the position [`Display`](std::fmt::Display) prefixes it with.
    fn detail(&self) -> String {
        match self {
            Self::ToolErrorAt { error, .. } => error.to_string(),
            Self::NoToolsConfigured { language, .. } => format!("No tools configured for language '{language}'"),
            Self::ToolBinaryNotFound { tool, .. } => {
                format!("Tool binary '{tool}' not found in allowed lookup locations")
            }
            Self::Policy { message, .. } => message.clone(),
            Self::ToolError(_) | Self::Aborted { .. } => self.to_string(),
        }
    }

    /// Report the error as a finding, at its code block when it names one.
    ///
    /// A run that stopped on this error still has to report it where the reader
    /// can find it, in every output format, and under the same name whichever
    /// path (lint, format, LSP) hit it.
    pub fn to_lint_warning(&self) -> LintWarning {
        let line = self.line().unwrap_or(1);
        LintWarning {
            message: self.detail(),
            line,
            column: 1,
            end_line: line,
            end_column: 1,
            severity: Severity::Error,
            fix: None,
            rule_name: Some(CODE_BLOCK_TOOLS_DIAGNOSTIC_NAME.to_string()),
        }
    }
}

/// The name a failure of the code-block-tools machinery itself is reported under.
///
/// Not a rule name and not a tool id: it names the class of problem, so nothing
/// looks it up in the rule registry.
pub const CODE_BLOCK_TOOLS_DIAGNOSTIC_NAME: &str = "code-block-tools";

impl std::error::Error for ProcessorError {}

impl From<ExecutorError> for ProcessorError {
    fn from(e: ExecutorError) -> Self {
        Self::ToolError(e)
    }
}

/// Result of processing a single code block.
#[derive(Debug)]
pub struct CodeBlockResult {
    /// Diagnostics from linting.
    pub diagnostics: Vec<CodeBlockDiagnostic>,
    /// Formatted content (if formatting was requested and succeeded).
    pub formatted_content: Option<String>,
    /// Whether the code block was modified.
    pub was_modified: bool,
}

/// Result of formatting code blocks in a document.
#[derive(Debug)]
pub struct FormatOutput {
    /// The formatted content (may be partially formatted if errors occurred).
    pub content: String,
    /// Whether any messages were collected. Warn-level messages count too.
    pub had_errors: bool,
    /// Error messages for blocks that couldn't be formatted, in the prose form
    /// the terminal shows. Both levels appear here: a message collected under
    /// `on-error = "warn"` is for the reader and nothing more.
    pub error_messages: Vec<String>,
    /// The fail-level subset of the above, as diagnostics rather than prose.
    ///
    /// A tool could not run under a setting of `fail`, so the document was only
    /// partly formatted and the run must not be reported as clean. The lint path
    /// records the same fact as a diagnostic and every output format carries it;
    /// these exist so a format run says it in the same machine-readable place
    /// instead of only in a line of stderr. Messages collected under
    /// `on-error = "warn"` are not failures and are not here.
    pub failures: Vec<CodeBlockDiagnostic>,
}

/// Result of linting the code blocks in a document.
#[derive(Debug, Default)]
pub struct LintOutput {
    /// Findings, including a tool that could not run under `on-error = "fail"`.
    pub diagnostics: Vec<CodeBlockDiagnostic>,
    /// Tools that could not run under `on-error = "warn"`, in the prose form
    /// [`FormatOutput::error_messages`] uses. They are for the reader and are not
    /// findings: `warn` asks to be told and to carry on.
    pub warnings: Vec<String>,
    /// Whether a tool failed to run under any `on-error` setting, so that some
    /// block went unchecked. Such a result says nothing about that block and is
    /// not one to cache.
    pub incomplete: bool,
}

impl FormatOutput {
    /// Whether part of the document went unformatted because a tool could not run.
    pub fn failed(&self) -> bool {
        !self.failures.is_empty()
    }
}

/// Main processor for code block tools.
pub struct CodeBlockToolProcessor<'a> {
    config: &'a CodeBlockToolsConfig,
    flavor: MarkdownFlavor,
    linguist: LinguistResolver,
    registry: ToolRegistry,
    executor: ToolExecutor,
    run_state: std::sync::Arc<super::run_state::RunState>,
    builtin_checks: bool,
    user_aliases: std::collections::HashMap<String, String>,
    /// `config.languages`, keyed by lowercased language name.
    languages: std::collections::HashMap<String, &'a LanguageToolConfig>,
    custom_languages: Vec<String>,
}

impl<'a> CodeBlockToolProcessor<'a> {
    /// Create a new processor with the given configuration and markdown flavor.
    pub fn new(config: &'a CodeBlockToolsConfig, flavor: MarkdownFlavor) -> Self {
        let user_aliases = config
            .language_aliases
            .iter()
            .map(|(k, v)| (k.to_lowercase(), v.to_lowercase()))
            .collect();
        // A fence tag is resolved to a lowercase canonical name, so a `languages` key has
        // to be lowercased to be reachable at all. `BTreeMap` iteration is sorted and
        // uppercase sorts first, so an exact-lowercase key wins over a mixed-case one.
        let languages = config.languages.iter().map(|(k, v)| (k.to_lowercase(), v)).collect();
        let run_state = config.run_state.clone().unwrap_or_default();
        Self {
            builtin_checks: true,
            custom_languages: Vec::new(),
            run_state: std::sync::Arc::clone(&run_state),
            config,
            flavor,
            linguist: LinguistResolver::new(),
            registry: ToolRegistry::new(config.tools.clone()),
            executor: ToolExecutor::new(config.timeout)
                .with_binary_resolution(
                    config.binary_preferences.clone(),
                    super::binary::project_root(&std::env::current_dir().unwrap_or_default()),
                )
                .with_run_state(run_state),
            user_aliases,
            languages,
        }
    }

    /// Invocation accounting and once-per-run policy warnings.
    pub fn run_state(&self) -> &super::run_state::RunState {
        &self.run_state
    }

    fn needs_coverage_scan(&self) -> bool {
        self.config.on_missing_language_tag != OnMissing::Ignore
            || self.config.unknown_language_policy() != OnMissing::Ignore
            || self.config.on_missing_mode_definition != OnMissing::Ignore
            || self.config.on_missing_language_definition == OnMissing::Warn
    }

    fn block_policy(&self, block: &FencedCodeBlockInfo, lint: bool) -> Option<(OnMissing, String, String)> {
        use crate::utils::fence_language::{FenceLanguage, classify};
        match classify(&block.info_string, self.flavor) {
            FenceLanguage::Exempt => return Some((OnMissing::Ignore, String::new(), String::new())),
            FenceLanguage::Language | FenceLanguage::CodeSyntax => {}
            FenceLanguage::Missing => {
                return Some((
                    self.config.on_missing_language_tag,
                    "missing-tag".into(),
                    "Fenced code block has no language tag".into(),
                ));
            }
        }
        let canonical = self.resolve_language(&block.language);
        let known = self.linguist.is_known(&block.language)
            || self
                .custom_languages
                .iter()
                .any(|label| label.eq_ignore_ascii_case(&block.language))
            || self.user_aliases.contains_key(&block.language.to_lowercase())
            || self.languages.contains_key(&block.language.to_lowercase())
            || self.languages.contains_key(&canonical);
        if !known {
            return Some((
                self.config.unknown_language_policy(),
                format!("unknown-tag:{}", block.language.to_lowercase()),
                format!("Unrecognized code block language tag '{}'", block.language),
            ));
        }
        if let Some(language) = self.language_config(&canonical) {
            if !language.enabled {
                return None;
            }
            let (current, opposite, mode) = if lint {
                (&language.lint, &language.format, "lint")
            } else {
                (&language.format, &language.lint, "format")
            };
            if current.is_empty() && !opposite.is_empty() {
                return Some((
                    self.config.on_missing_mode_definition,
                    format!("missing-mode:{canonical}:{mode}"),
                    format!("No {mode} tools configured for language '{canonical}'"),
                ));
            }
        }
        None
    }

    fn policy_diagnostic(
        &self,
        policy: OnMissing,
        key: String,
        message: String,
        line: usize,
    ) -> Result<Option<CodeBlockDiagnostic>, ProcessorError> {
        match policy {
            OnMissing::Ignore => Ok(None),
            OnMissing::Warn => {
                self.run_state.warn(key, message);
                Ok(None)
            }
            OnMissing::Fail => Ok(Some(CodeBlockDiagnostic {
                file_line: line,
                column: None,
                message,
                severity: DiagnosticSeverity::Error,
                tool: CODE_BLOCK_TOOLS_DIAGNOSTIC_NAME.into(),
                code_block_start: line,
            })),
            OnMissing::FailFast => {
                self.run_state.abort();
                Err(ProcessorError::Policy { message, line })
            }
        }
    }

    /// Replay coverage warnings without re-executing tools on a cache hit.
    pub fn replay_cached_warnings(&self, content: &str) {
        for block in self.extract_code_blocks(content) {
            if let Some((policy, key, message)) = self.block_policy(&block, true) {
                if policy == OnMissing::Warn {
                    self.run_state.warn(key, message);
                }
            } else if !block.language.is_empty() && self.config.on_missing_language_definition == OnMissing::Warn {
                let canonical = self.resolve_language(&block.language);
                if self
                    .language_config(&canonical)
                    .is_none_or(|lc| lc.enabled && lc.lint.is_empty() && lc.format.is_empty())
                {
                    self.run_state.warn(
                        format!("missing-definition:{canonical}"),
                        format!("No tools configured for language '{canonical}'"),
                    );
                }
            }
        }
    }

    /// Whether a valid cached lint result represents at least one usable tool.
    pub fn has_lint_work(&self, content: &str) -> bool {
        self.has_work(content, true)
    }

    pub fn has_work(&self, content: &str, lint: bool) -> bool {
        let slot = if lint { ToolSlot::Lint } else { ToolSlot::Format };
        self.extract_code_blocks(content).iter().any(|block| {
            if self.block_policy(block, lint).is_some() || block.content_start == block.content_end {
                return false;
            }
            let canonical = self.resolve_language(&block.language);
            self.language_config(&canonical).is_some_and(|language| {
                language.enabled
                    && (if lint { &language.lint } else { &language.format }).iter().any(|id| {
                        if self.registry.definition_problem(id, slot).is_some() {
                            return false;
                        }
                        if is_rumdl_builtin(id) && is_markdown_language(&canonical) {
                            return self.builtin_checks;
                        }
                        self.registry
                            .resolve(id, slot)
                            .and_then(|tool| tool.command.first())
                            .is_some_and(|binary| self.executor.is_tool_available(binary))
                    })
            })
        })
    }

    /// Share project-declared MD040 language labels with tools-only validation.
    pub fn with_document_config(mut self, config: &crate::config::Config) -> Self {
        let md040: crate::rules::md040_fenced_code_language::md040_config::MD040Config =
            crate::rule_config_serde::load_rule_config(config);
        self.custom_languages = md040.custom_languages;
        self
    }

    /// External commands may read configuration, environment or wrappers that
    /// rumdl cannot validate. A combined file cache must not reuse their verdicts.
    pub fn has_external_lint_work(&self, content: &str) -> bool {
        self.extract_code_blocks(content).iter().any(|block| {
            if self.block_policy(block, true).is_some() || block.content_start == block.content_end {
                return false;
            }
            let canonical = self.resolve_language(&block.language);
            self.language_config(&canonical).is_some_and(|language| {
                language.enabled
                    && language.lint.iter().any(|id| {
                        !(is_rumdl_builtin(id) && is_markdown_language(&canonical))
                            && self.registry.definition_problem(id, ToolSlot::Lint).is_none()
                    })
            })
        })
    }

    pub fn with_builtin_checks(mut self, enabled: bool) -> Self {
        self.builtin_checks = enabled;
        self
    }

    /// Resolve project-local executables relative to this document's project.
    pub fn for_path(mut self, path: &std::path::Path) -> Self {
        self.executor = self.executor.with_binary_resolution(
            self.config.binary_preferences.clone(),
            super::binary::project_root(path),
        );
        self
    }

    /// Configuration for a canonical (lowercase) language name.
    fn language_config(&self, canonical_lang: &str) -> Option<&'a LanguageToolConfig> {
        self.languages.get(canonical_lang).copied()
    }

    /// Report a tool id no registry entry answers to.
    ///
    /// Both the id and the language it was configured under are text out of whichever
    /// file supplied the section (the language through `language-aliases`), so a section
    /// reached through `extends` is described rather than quoted. See
    /// [`crate::config::Config::withheld_rule_values`] for the same policy on rule options.
    fn warn_unknown_tool(&self, tool_id: &str, canonical_lang: &str) {
        if self.config.values_withheld {
            let withheld = crate::config::WITHHELD;
            log::warn!("Unknown tool {withheld} configured for language {withheld}");
        } else {
            log::warn!("Unknown tool '{tool_id}' configured for language '{canonical_lang}'");
        }
    }

    /// Conservatively skip parsing documents without any possible fence.
    /// Fence length and container prefixes must not hide applicable tool work.
    fn has_potential_matching_blocks(&self, content: &str, lint_mode: bool) -> bool {
        self.languages.values().any(|language| {
            language.enabled
                && if lint_mode {
                    !language.lint.is_empty()
                } else {
                    !language.format.is_empty()
                }
        }) && (content.contains("```") || content.contains("~~~"))
    }

    /// Extract all fenced code blocks from content, including those the flavor
    /// adds (MkDocs admonitions and tabs).
    pub fn extract_code_blocks(&self, content: &str) -> Vec<FencedCodeBlockInfo> {
        let mut blocks = extract_fenced_code_blocks(content);

        // For MkDocs flavor, also extract code blocks inside admonitions and tabs
        if self.flavor == MarkdownFlavor::MkDocs {
            let mkdocs_blocks = self.extract_mkdocs_code_blocks(content, &blocks);
            for mb in mkdocs_blocks {
                // Deduplicate: only add if no existing block starts at the same line
                if !blocks.iter().any(|b| b.start_line == mb.start_line) {
                    blocks.push(mb);
                }
            }
            blocks.sort_by_key(|b| b.start_line);
        }

        blocks
    }

    /// Extract fenced code blocks that are inside MkDocs admonitions or tabs.
    ///
    /// pulldown_cmark doesn't parse MkDocs-specific constructs, so indented
    /// code blocks inside `!!!`/`???` admonitions or `===` tabs are missed.
    /// This method manually scans for them, skipping the lines of the
    /// `fenced` blocks the CommonMark parser found: what looks like an
    /// admonition there is code, such as an example in a `markdown` block.
    fn extract_mkdocs_code_blocks(&self, content: &str, fenced: &[FencedCodeBlockInfo]) -> Vec<FencedCodeBlockInfo> {
        use crate::utils::mkdocs_admonitions;
        use crate::utils::mkdocs_tabs;

        let mut blocks = Vec::new();
        let lines: Vec<&str> = content.lines().collect();
        let mut in_fenced_block = vec![false; lines.len()];
        for block in fenced {
            let end = block.end_line.min(lines.len().saturating_sub(1));
            if let Some(span) = in_fenced_block.get_mut(block.start_line..=end) {
                span.fill(true);
            }
        }

        // Track current MkDocs context indent level
        // We only need to know if we're inside any MkDocs block, so a simple stack suffices.
        let mut context_indent_stack: Vec<usize> = Vec::new();

        // Track fence state inside MkDocs context
        let mut in_fence = false;
        let mut fence_start_line: usize = 0;
        let mut fence_content_start: usize = 0;
        let mut fence_char: char = '`';
        let mut fence_length: usize = 0;
        let mut fence_indent_prefix = String::new();
        let mut fence_language = String::new();
        let mut fence_info_string = String::new();

        // Compute byte offsets via pointer arithmetic.
        // `content.lines()` returns slices into the original string,
        // so each line's pointer offset from `content` gives its byte position.
        // This correctly handles \n, \r\n, and empty lines.
        let content_start_ptr = content.as_ptr() as usize;
        let line_offsets: Vec<usize> = lines
            .iter()
            .map(|line| line.as_ptr() as usize - content_start_ptr)
            .collect();

        for (i, line) in lines.iter().enumerate() {
            let line_indent = crate::utils::mkdocs_common::get_line_indent(line);
            let is_admonition = mkdocs_admonitions::is_admonition_start(line);
            let is_tab = mkdocs_tabs::is_tab_marker(line);

            // Pop contexts when the current line is not indented enough to be content.
            // This runs for ALL lines (including new admonition/tab starts) to clean
            // up stale entries before potentially pushing a new context.
            if !line.trim().is_empty() {
                while let Some(&ctx_indent) = context_indent_stack.last() {
                    if line_indent < ctx_indent + 4 {
                        context_indent_stack.pop();
                        if in_fence {
                            in_fence = false;
                        }
                    } else {
                        break;
                    }
                }
            }

            // A line of a fenced block can end a context, but never opens one
            // or a fence: it is code.
            if in_fenced_block[i] {
                continue;
            }

            // Check for admonition start — push new context
            if is_admonition && let Some(indent) = mkdocs_admonitions::get_admonition_indent(line) {
                context_indent_stack.push(indent);
                continue;
            }

            // Check for tab marker — push new context
            if is_tab && let Some(indent) = mkdocs_tabs::get_tab_indent(line) {
                context_indent_stack.push(indent);
                continue;
            }

            // Only look for fences inside a MkDocs context
            if context_indent_stack.is_empty() {
                continue;
            }

            let trimmed = line.trim_start();
            let leading_spaces = line.len() - trimmed.len();

            if !in_fence {
                // Check for fence opening
                let (fc, fl) = if trimmed.starts_with("```") {
                    ('`', trimmed.chars().take_while(|&c| c == '`').count())
                } else if trimmed.starts_with("~~~") {
                    ('~', trimmed.chars().take_while(|&c| c == '~').count())
                } else {
                    continue;
                };

                if fl >= 3 {
                    in_fence = true;
                    fence_start_line = i;
                    fence_char = fc;
                    fence_length = fl;
                    fence_indent_prefix = line.get(..leading_spaces).unwrap_or("").to_string();

                    let after_fence = &trimmed[fl..];
                    fence_info_string = after_fence.trim().to_string();
                    fence_language = fence_info_string.split_whitespace().next().unwrap_or("").to_string();

                    // Content starts at the next line's byte offset
                    fence_content_start = line_offsets.get(i + 1).copied().unwrap_or(content.len());
                }
            } else {
                // Check for fence closing
                let is_closing = if fence_char == '`' {
                    trimmed.starts_with("```")
                        && trimmed.chars().take_while(|&c| c == '`').count() >= fence_length
                        && trimmed.trim_start_matches('`').trim().is_empty()
                } else {
                    trimmed.starts_with("~~~")
                        && trimmed.chars().take_while(|&c| c == '~').count() >= fence_length
                        && trimmed.trim_start_matches('~').trim().is_empty()
                };

                if is_closing {
                    let content_end = line_offsets.get(i).copied().unwrap_or(content.len());

                    if content_end >= fence_content_start {
                        let (code, line_prefix_widths) =
                            strip_indent_from_block(&content[fence_content_start..content_end], &fence_indent_prefix);
                        blocks.push(FencedCodeBlockInfo {
                            start_line: fence_start_line,
                            end_line: i,
                            content_start: fence_content_start,
                            content_end,
                            language: fence_language.clone(),
                            info_string: fence_info_string.clone(),
                            fence_char,
                            fence_length,
                            content_prefix: fence_indent_prefix.clone(),
                            code,
                            line_prefix_widths,
                        });
                    }

                    in_fence = false;
                }
            }
        }

        blocks
    }

    /// Resolve a language tag to its canonical name.
    fn resolve_language(&self, language: &str) -> String {
        let lower = language.to_lowercase();
        if let Some(mapped) = self.user_aliases.get(&lower) {
            return mapped.clone();
        }
        match self.config.normalize_language {
            NormalizeLanguage::Linguist => self.linguist.resolve(&lower),
            NormalizeLanguage::Exact => lower,
        }
    }

    /// Get the effective on_error setting for a language.
    fn get_on_error(&self, language: &str) -> OnError {
        self.language_config(language)
            .and_then(|lc| lc.on_error)
            .unwrap_or(self.config.on_error)
    }

    /// Lint all code blocks in the content.
    ///
    /// Returns diagnostics from all configured linters. Tools that could not run
    /// under `on-error = "warn"` are left out; [`Self::lint_output`] returns them.
    pub fn lint(&self, content: &str) -> Result<Vec<CodeBlockDiagnostic>, ProcessorError> {
        self.lint_output(content).map(|output| output.diagnostics)
    }

    /// Lint all code blocks in the content, with the tool failures `warn` reports.
    ///
    /// Returns `Err` only for `fail-fast` settings. A tool that cannot run under
    /// `on-error = "fail"` stops the document and is reported as a finding at its
    /// block, beside the findings of the blocks checked before it.
    pub fn lint_output(&self, content: &str) -> Result<LintOutput, ProcessorError> {
        // Skip the expensive parse when no tools could possibly produce output.
        // With on_missing=Ignore (default) and no languages with lint tools configured,
        // every block would be skipped, so the parse is wasted work.
        if !self.needs_coverage_scan()
            && self.config.on_missing_language_definition.skips_the_block()
            && !self
                .config
                .languages
                .values()
                .any(|lc| lc.enabled && !lc.lint.is_empty())
        {
            return Ok(LintOutput::default());
        }

        // Quick content check: skip parsing if no configured language appears in the content.
        // This avoids the expensive pulldown-cmark parse when there are no matching code blocks.
        if !self.needs_coverage_scan()
            && self.config.on_missing_language_definition.skips_the_block()
            && !self.has_potential_matching_blocks(content, true)
        {
            return Ok(LintOutput::default());
        }

        let mut incomplete = false;
        let mut all_diagnostics = Vec::new();
        let mut warnings = Vec::new();
        let blocks = self.extract_code_blocks(content);

        for block in blocks {
            if let Some((policy, key, message)) = self.block_policy(&block, true) {
                if let Some(diagnostic) = self.policy_diagnostic(policy, key, message, block.start_line + 1)? {
                    all_diagnostics.push(diagnostic);
                }
                continue;
            }
            if block.language.is_empty() {
                continue;
            }

            let canonical_lang = self.resolve_language(&block.language);

            // Get lint tools for this language
            let lang_config = self.language_config(&canonical_lang);

            // If language is explicitly configured with enabled=false, skip silently
            if let Some(lc) = lang_config
                && !lc.enabled
            {
                continue;
            }

            let lint_tools = match lang_config {
                Some(lc) if !lc.lint.is_empty() => &lc.lint,
                // Defined with format tools only. That is a complete definition:
                // `on-missing-language-definition` speaks for a language the config never
                // gave any tool, not for a mode a defined language does not use.
                Some(lc) if !lc.format.is_empty() => continue,
                _ => {
                    // The language has no tools in either mode
                    match self.config.on_missing_language_definition {
                        // Which languages appear in a document is not known before
                        // reading it, so there is no once-per-run place to say this
                        // and `warn` has nothing to add over `ignore`. Config
                        // validation tells the user that, rather than leaving a
                        // setting quietly doing nothing.
                        OnMissing::Ignore => continue,
                        OnMissing::Warn => {
                            self.run_state.warn(
                                format!("missing-definition:{canonical_lang}"),
                                format!("No tools configured for language '{canonical_lang}'"),
                            );
                            continue;
                        }
                        OnMissing::Fail => {
                            all_diagnostics.push(CodeBlockDiagnostic {
                                file_line: block.start_line + 1,
                                column: None,
                                message: format!("No lint tools configured for language '{canonical_lang}'"),
                                severity: DiagnosticSeverity::Error,
                                tool: CODE_BLOCK_TOOLS_DIAGNOSTIC_NAME.to_string(),
                                code_block_start: block.start_line + 1,
                            });
                            continue;
                        }
                        OnMissing::FailFast => {
                            self.run_state.abort();
                            return Err(ProcessorError::NoToolsConfigured {
                                language: canonical_lang,
                                line: block.start_line + 1,
                            });
                        }
                    }
                }
            };

            // Extract code block content
            if block.content_start >= block.content_end || block.content_end > content.len() {
                continue;
            }
            let code_content = &block.code;

            // Run each lint tool
            for tool_id in lint_tools {
                // Skip built-in "rumdl" tool for markdown - handled separately by embedded markdown linting
                if is_rumdl_builtin(tool_id) && is_markdown_language(&canonical_lang) {
                    if self.builtin_checks {
                        self.run_state.record_execution();
                    }
                    continue;
                }

                if self.registry.definition_problem(tool_id, ToolSlot::Lint).is_some() {
                    continue;
                }
                let Some(resolved_id) = self.registry.resolve_id(tool_id, ToolSlot::Lint) else {
                    self.warn_unknown_tool(tool_id, &canonical_lang);
                    continue;
                };
                let Some(tool_def) = self.registry.get(&resolved_id) else {
                    self.warn_unknown_tool(tool_id, &canonical_lang);
                    continue;
                };

                // Check if tool binary exists before running
                let tool_name = tool_def.command.first().map_or("", String::as_str);
                if !tool_name.is_empty() && !self.executor.is_tool_available(tool_name) {
                    match self.config.on_missing_tool_binary {
                        // `warn` is announced once for the whole run, beside the
                        // configuration that named the tool, rather than once per
                        // block. Here it skips exactly as `ignore` does.
                        OnMissing::Ignore | OnMissing::Warn => {
                            log::debug!("Tool binary '{tool_name}' not found, skipping");
                            continue;
                        }
                        OnMissing::Fail => {
                            all_diagnostics.push(CodeBlockDiagnostic {
                                file_line: block.start_line + 1,
                                column: None,
                                message: format!("Tool binary '{tool_name}' not found in allowed lookup locations"),
                                severity: DiagnosticSeverity::Error,
                                tool: CODE_BLOCK_TOOLS_DIAGNOSTIC_NAME.to_string(),
                                code_block_start: block.start_line + 1,
                            });
                            continue;
                        }
                        OnMissing::FailFast => {
                            self.run_state.abort();
                            return Err(ProcessorError::ToolBinaryNotFound {
                                tool: tool_name.to_string(),
                                language: canonical_lang.clone(),
                                line: block.start_line + 1,
                            });
                        }
                    }
                }

                let tool_input = ensure_trailing_newline(code_content);
                let run = match self.registry.lint_mode(&resolved_id) {
                    // Explicit djLint and Shuck modes prefer verified native checks;
                    // all other formatters and unsupported versions compare output.
                    Some(BuiltinLintMode::FormatCheck) => self
                        .executor
                        .format_check(
                            tool_def,
                            self.registry.native_format_check(&resolved_id).as_ref(),
                            &tool_input,
                            Some(self.config.timeout),
                        )
                        .map(|output| match output {
                            FormatCheckOutput::Formatted(output) => {
                                self.format_check_diagnostics(&output, code_content, tool_id, block.start_line + 1)
                            }
                            FormatCheckOutput::Native { changed: false } => Vec::new(),
                            FormatCheckOutput::Native { changed: true } => {
                                self.unformatted_diagnostic(tool_id, block.start_line + 1)
                            }
                        }),
                    _ => self
                        .executor
                        .lint(tool_def, &tool_input, Some(self.config.timeout))
                        .map(|output| {
                            self.parse_tool_output(
                                &output,
                                tool_id,
                                block.start_line + 1, // Convert to 1-indexed
                            )
                        }),
                };
                match run {
                    Ok(mut diagnostics) => {
                        // A tool counts columns in the code it was given, which
                        // starts after the line's container prefix in the document.
                        for diagnostic in &mut diagnostics {
                            let code_line = diagnostic.file_line.checked_sub(block.start_line + 1);
                            if let (Some(column), Some(line)) = (diagnostic.column.as_mut(), code_line)
                                && let Some(document_column) = block.document_column(line, *column)
                            {
                                *column = document_column;
                            }
                        }
                        all_diagnostics.extend(diagnostics);
                    }
                    Err(error) => match self.get_on_error(&canonical_lang) {
                        // `fail` stops the document here. What the blocks before
                        // this one reported is still true, so it is kept, and the
                        // failure is reported at the block it happened in.
                        OnError::Fail => {
                            let failure = ProcessorError::ToolErrorAt {
                                error,
                                line: block.start_line + 1,
                                language: canonical_lang,
                            };
                            all_diagnostics.push(CodeBlockDiagnostic {
                                file_line: block.start_line + 1,
                                column: None,
                                message: failure.detail(),
                                severity: DiagnosticSeverity::Error,
                                tool: CODE_BLOCK_TOOLS_DIAGNOSTIC_NAME.to_string(),
                                code_block_start: block.start_line + 1,
                            });
                            return Ok(LintOutput {
                                diagnostics: all_diagnostics,
                                warnings,
                                incomplete: true,
                            });
                        }
                        OnError::Warn => {
                            incomplete = true;
                            warnings.push(format!("line {} ({canonical_lang}): {error}", block.start_line + 1));
                        }
                        OnError::Skip => incomplete = true,
                    },
                }
            }
        }

        Ok(LintOutput {
            diagnostics: all_diagnostics,
            warnings,
            incomplete,
        })
    }

    /// Format all code blocks in analysis text (the document's encoding BOM
    /// already removed by the caller, as for `format_with_config`).
    ///
    /// Returns the modified content with formatted code blocks and any errors that occurred.
    /// With `on-missing-*` = `fail`, errors are collected but formatting continues.
    /// With `on-missing-*` = `fail-fast`, returns Err immediately on first error.
    pub fn format(&self, content: &str) -> Result<FormatOutput, ProcessorError> {
        self.format_with_config(content, &crate::config::Config::default(), None)
    }

    /// Format with the document's conflict-marker configuration and per-file exceptions.
    ///
    /// The conflict guard reads the configuration alone. A caller that resolved a
    /// rule selection of its own passes it to `format_for_rules` instead.
    ///
    /// `content` is analysis text: a caller holding a file with a leading byte
    /// order mark removes it first (`utils::bom::strip_bom`) and puts it back on
    /// the result, so a second U+FEFF stays ordinary content.
    pub fn format_with_config(
        &self,
        content: &str,
        config: &crate::config::Config,
        path: Option<&std::path::Path>,
    ) -> Result<FormatOutput, ProcessorError> {
        self.format_guarded(content, None, config, path)
    }

    /// Format under the invocation's rule selection.
    ///
    /// A run that dropped MD092 formats a conflicted document like any other, so
    /// its fenced code is formatted too: the alternative is an outer document
    /// rewritten around code blocks the same run silently left alone.
    pub fn format_for_rules(
        &self,
        content: &str,
        rules: &[Box<dyn crate::rule::Rule>],
        config: &crate::config::Config,
        path: Option<&std::path::Path>,
    ) -> Result<FormatOutput, ProcessorError> {
        self.format_guarded(content, Some(rules), config, path)
    }

    fn format_guarded(
        &self,
        content: &str,
        rules: Option<&[Box<dyn crate::rule::Rule>]>,
        config: &crate::config::Config,
        path: Option<&std::path::Path>,
    ) -> Result<FormatOutput, ProcessorError> {
        let no_output = FormatOutput {
            content: content.to_string(),
            had_errors: false,
            error_messages: Vec::new(),
            failures: Vec::new(),
        };

        let conflicted = match rules {
            Some(rules) => crate::merge_conflict::detect_for_analysis(content, rules, config, path).is_some(),
            None => crate::merge_conflict::detect_configured_for_analysis(content, config, path).is_some(),
        };
        if conflicted {
            return Ok(no_output);
        }

        // Skip the expensive parse when no tools could produce output
        if !self.needs_coverage_scan()
            && self.config.on_missing_language_definition.skips_the_block()
            && !self
                .config
                .languages
                .values()
                .any(|lc| lc.enabled && !lc.format.is_empty())
        {
            return Ok(no_output);
        }

        // Quick content check: skip parsing if no configured language appears in the content
        if !self.needs_coverage_scan()
            && self.config.on_missing_language_definition.skips_the_block()
            && !self.has_potential_matching_blocks(content, false)
        {
            return Ok(no_output);
        }

        let blocks = self.extract_code_blocks(content);

        if blocks.is_empty() {
            return Ok(no_output);
        }

        let mut error_messages = Vec::new();
        let mut failures = Vec::new();
        for block in &blocks {
            if let Some((policy, key, message)) = self.block_policy(block, false)
                && let Some(diagnostic) = self.policy_diagnostic(policy, key, message, block.start_line + 1)?
            {
                error_messages.push(diagnostic.message.clone());
                failures.push(diagnostic);
            }
        }
        // Check fail-fast gaps in source order before reverse-offset execution.
        for block in &blocks {
            if self.block_policy(block, false).is_some() {
                continue;
            }
            let canonical = self.resolve_language(&block.language);
            let language = self.language_config(&canonical);
            if language.is_some_and(|lc| !lc.enabled || lc.format.is_empty() && !lc.lint.is_empty()) {
                continue;
            }
            let Some(language) = language.filter(|lc| !lc.format.is_empty()) else {
                if self.config.on_missing_language_definition == OnMissing::FailFast {
                    self.run_state.abort();
                    return Err(ProcessorError::NoToolsConfigured {
                        language: canonical,
                        line: block.start_line + 1,
                    });
                }
                continue;
            };
            if self.config.on_missing_tool_binary != OnMissing::FailFast {
                continue;
            }
            for id in &language.format {
                if is_rumdl_builtin(id) && is_markdown_language(&canonical) {
                    continue;
                }
                if self.registry.definition_problem(id, ToolSlot::Format).is_some() {
                    continue;
                }
                let Some(tool) = self.registry.resolve(id, ToolSlot::Format) else {
                    continue;
                };
                let Some(binary) = tool.command.first() else {
                    continue;
                };
                if !self.executor.is_tool_available(binary) {
                    self.run_state.abort();
                    return Err(ProcessorError::ToolBinaryNotFound {
                        tool: binary.clone(),
                        language: canonical.clone(),
                        line: block.start_line + 1,
                    });
                }
                // Fallback stages after an available formatter are only known
                // to be needed if it fails; runtime retains that check.
                if language.format_mode == FormatMode::Fallback {
                    break;
                }
            }
        }
        // Execution uses original offsets; coverage policies are checked above
        // in source order, so fail-fast never starts later blocks speculatively.
        let mut planned = Vec::new();
        for (index, block) in blocks.iter().enumerate().rev() {
            if self.block_policy(block, false).is_some() {
                continue;
            }
            if block.language.is_empty() {
                continue;
            }

            let canonical_lang = self.resolve_language(&block.language);

            // Get format tools for this language
            let lang_config = self.language_config(&canonical_lang);

            // If language is explicitly configured with enabled=false, skip silently
            if let Some(lc) = lang_config
                && !lc.enabled
            {
                continue;
            }

            let (format_tools, format_mode) = match lang_config {
                Some(lc) if !lc.format.is_empty() => (&lc.format, lc.format_mode),
                // Defined with lint tools only. See the matching arm in `lint`.
                Some(lc) if !lc.lint.is_empty() => continue,
                _ => {
                    // The language has no tools in either mode
                    match self.config.on_missing_language_definition {
                        // See the matching arm in `lint`.
                        OnMissing::Ignore => continue,
                        OnMissing::Warn => {
                            self.run_state.warn(
                                format!("missing-definition:{canonical_lang}"),
                                format!("No tools configured for language '{canonical_lang}'"),
                            );
                            continue;
                        }
                        OnMissing::Fail => {
                            error_messages.push(format!(
                                "No format tools configured for language '{canonical_lang}' at line {}",
                                block.start_line + 1
                            ));
                            failures.push(CodeBlockDiagnostic {
                                file_line: block.start_line + 1,
                                column: None,
                                message: format!("No format tools configured for language '{canonical_lang}'"),
                                severity: DiagnosticSeverity::Error,
                                tool: CODE_BLOCK_TOOLS_DIAGNOSTIC_NAME.to_string(),
                                code_block_start: block.start_line + 1,
                            });
                            continue;
                        }
                        OnMissing::FailFast => {
                            self.run_state.abort();
                            return Err(ProcessorError::NoToolsConfigured {
                                language: canonical_lang,
                                line: block.start_line + 1,
                            });
                        }
                    }
                }
            };

            // Extract code block content
            if block.content_start >= block.content_end || block.content_end > content.len() {
                continue;
            }
            let code_content_raw = &content[block.content_start..block.content_end];
            let code_content = block.code.clone();

            // In fallback mode the first formatter that succeeds supplies the
            // block. In pipeline mode every formatter runs, each on the output of
            // the last one that succeeded; a formatter that fails, or is skipped,
            // never replaces that output, so the next one starts from it.
            let mut formatted = code_content.clone();
            let mut tool_ran = false;
            for tool_id in format_tools {
                // Skip built-in "rumdl" tool for markdown - handled separately by embedded markdown formatting
                if is_rumdl_builtin(tool_id) && is_markdown_language(&canonical_lang) {
                    if self.builtin_checks {
                        self.run_state.record_execution();
                    }
                    continue;
                }

                if self.registry.definition_problem(tool_id, ToolSlot::Format).is_some() {
                    continue;
                }
                let Some(tool_def) = self.registry.resolve(tool_id, ToolSlot::Format) else {
                    self.warn_unknown_tool(tool_id, &canonical_lang);
                    continue;
                };

                // A linter writes its report to stdout, so running one here replaces the
                // block with that report: `ruff:check` in a format slot turns a clean
                // Python block into the literal text `All checks passed!`. The
                // empty-output guard below does not catch it, because a linter with
                // nothing to say still exits 0 and can still print a summary line.
                // Config validation reports this too, but a warning cannot undo an
                // overwritten block, so the run declines the tool as well.
                if self.registry.fills_format_slot(tool_id) == Some(false) {
                    log::warn!(
                        "Tool '{tool_id}' is a linter and cannot format '{canonical_lang}' code blocks; move it to the lint slot"
                    );
                    continue;
                }

                // Check if tool binary exists before running
                let tool_name = tool_def.command.first().map_or("", String::as_str);
                if !tool_name.is_empty() && !self.executor.is_tool_available(tool_name) {
                    match self.config.on_missing_tool_binary {
                        // See the matching arm in `lint`.
                        OnMissing::Ignore | OnMissing::Warn => {
                            log::debug!("Tool binary '{tool_name}' not found, skipping");
                            continue;
                        }
                        OnMissing::Fail => {
                            error_messages.push(format!(
                                "Tool binary '{tool_name}' not found in allowed lookup locations for language '{canonical_lang}' at line {}",
                                block.start_line + 1
                            ));
                            failures.push(CodeBlockDiagnostic {
                                file_line: block.start_line + 1,
                                column: None,
                                message: format!("Tool binary '{tool_name}' not found in allowed lookup locations"),
                                severity: DiagnosticSeverity::Error,
                                tool: CODE_BLOCK_TOOLS_DIAGNOSTIC_NAME.to_string(),
                                code_block_start: block.start_line + 1,
                            });
                            continue;
                        }
                        OnMissing::FailFast => {
                            self.run_state.abort();
                            return Err(ProcessorError::ToolBinaryNotFound {
                                tool: tool_name.to_string(),
                                language: canonical_lang.clone(),
                                line: block.start_line + 1,
                            });
                        }
                    }
                }

                let tool_input = ensure_trailing_newline(&formatted);
                match self.executor.format(tool_def, &tool_input, Some(self.config.timeout)) {
                    Ok(output) => {
                        // Ensure trailing newline matches original (unindented)
                        formatted = output;
                        if code_content.ends_with('\n') && !formatted.ends_with('\n') {
                            formatted.push('\n');
                        } else if !code_content.ends_with('\n') && formatted.ends_with('\n') {
                            formatted.pop();
                        }
                        tool_ran = true;
                        if format_mode == FormatMode::Fallback {
                            break;
                        }
                    }
                    Err(e) => {
                        let on_error = self.get_on_error(&canonical_lang);
                        match on_error {
                            OnError::Fail => {
                                return Err(ProcessorError::ToolErrorAt {
                                    error: e,
                                    line: block.start_line + 1,
                                    language: canonical_lang,
                                });
                            }
                            OnError::Warn => {
                                error_messages.push(format!("line {} ({}): {e}", block.start_line + 1, canonical_lang));
                            }
                            OnError::Skip => {}
                        }
                    }
                }
            }

            // Replace content if changed and a tool actually ran
            if tool_ran && formatted != code_content && block.source_for(&formatted) != code_content_raw {
                planned.push(WriteBack { index, code: formatted });
            }
        }

        let (result, refused) = write_back_verified(content, &blocks, &planned, |document| {
            self.extract_code_blocks(document)
        });
        for index in refused {
            let block = &blocks[index];
            let diagnostic = CodeBlockDiagnostic::refused_write_back(block.start_line + 1);
            error_messages.push(format!(
                "line {} ({}): {}",
                diagnostic.file_line,
                self.resolve_language(&block.language),
                diagnostic.message
            ));
            failures.push(diagnostic);
        }

        Ok(FormatOutput {
            content: result,
            had_errors: !error_messages.is_empty(),
            error_messages,
            failures,
        })
    }

    /// Diagnostics for a built-in formatter used in a `lint` slot.
    ///
    /// The formatter's own output is the answer: a block that comes back changed is not
    /// formatted. This comparison handles ordinary formatter IDs and the fallback
    /// for native-check IDs whose installed version does not support reliable stdin checks.
    ///
    /// The comparison mirrors the one [`Self::format`] makes before rewriting a
    /// block, so `check` reports exactly the blocks `fmt` would change. A
    /// formatter that emptied a non-empty block never gets here: the executor
    /// reports it as a tool failure, for `on-error` to handle.
    fn format_check_diagnostics(
        &self,
        output: &str,
        code_content: &str,
        tool_id: &str,
        code_block_start_line: usize,
    ) -> Vec<CodeBlockDiagnostic> {
        let mut formatted = output.to_string();
        if code_content.ends_with('\n') && !formatted.ends_with('\n') {
            formatted.push('\n');
        } else if !code_content.ends_with('\n') && formatted.ends_with('\n') {
            formatted.pop();
        }

        if formatted == code_content {
            return Vec::new();
        }

        self.unformatted_diagnostic(tool_id, code_block_start_line)
    }

    fn unformatted_diagnostic(&self, tool_id: &str, code_block_start_line: usize) -> Vec<CodeBlockDiagnostic> {
        vec![CodeBlockDiagnostic {
            file_line: code_block_start_line,
            column: None,
            message: "Code block is not formatted".to_string(),
            severity: DiagnosticSeverity::Warning,
            tool: tool_id.to_string(),
            code_block_start: code_block_start_line,
        }]
    }

    /// Parse tool output into diagnostics.
    ///
    /// This is a basic parser that handles common output formats.
    /// Tools vary widely in their output format, so this is best-effort.
    fn parse_tool_output(
        &self,
        output: &ToolOutput,
        tool_id: &str,
        code_block_start_line: usize,
    ) -> Vec<CodeBlockDiagnostic> {
        let mut diagnostics = Vec::new();
        let mut shellcheck_line: Option<usize> = None;

        // Strip ANSI escape codes and combine stdout + stderr for parsing
        let stdout_clean = strip_ansi_codes(&output.stdout);
        let stderr_clean = strip_ansi_codes(&output.stderr);
        let combined = format!("{stdout_clean}\n{stderr_clean}");

        // State for multi-line "Error: msg" / "at line N column M" pattern
        let mut pending_error: Option<(String, DiagnosticSeverity)> = None;

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

            // Resolve pending "Error: msg" from previous line
            if let Some((ref msg, severity)) = pending_error {
                if let Some((line_num, col)) = Self::parse_at_line_column(line) {
                    diagnostics.push(CodeBlockDiagnostic {
                        file_line: code_block_start_line + line_num,
                        column: Some(col),
                        message: msg.clone(),
                        severity,
                        tool: tool_id.to_string(),
                        code_block_start: code_block_start_line,
                    });
                    pending_error = None;
                    continue;
                }
                // No position line followed; fall back to what the message itself says.
                diagnostics.push(Self::unpositioned_diagnostic(
                    msg,
                    severity,
                    tool_id,
                    code_block_start_line,
                ));
                pending_error = None;
                // Fall through to parse current line
            }

            if let Some(line_num) = self.parse_shellcheck_header(line) {
                shellcheck_line = Some(line_num);
                continue;
            }

            if let Some(line_num) = shellcheck_line
                && let Some(diag) = self.parse_shellcheck_message(line, tool_id, code_block_start_line, line_num)
            {
                diagnostics.push(diag);
                continue;
            }

            // Try pattern: "::warning file=f,line=N,col=M::message" (GitHub annotation)
            if let Some(diag) = Self::parse_github_annotation(line, tool_id, code_block_start_line) {
                diagnostics.push(diag);
                continue;
            }

            // Try pattern: "file:line:col: message" or "file:line: message"
            if let Some(diag) = self.parse_standard_format(line, tool_id, code_block_start_line) {
                diagnostics.push(diag);
                continue;
            }

            // Try pattern: "line:col message" (eslint style)
            if let Some(diag) = self.parse_eslint_format(line, tool_id, code_block_start_line) {
                diagnostics.push(diag);
                continue;
            }

            // Try single-line shellcheck format fallback
            if let Some(diag) = self.parse_shellcheck_format(line, tool_id, code_block_start_line) {
                diagnostics.push(diag);
                continue;
            }

            // Try multi-line "Error: msg" / "Warning: msg" pattern
            if let Some(error_info) = Self::parse_error_line(line) {
                pending_error = Some(error_info);
            }
        }

        // Flush any remaining pending error
        if let Some((msg, severity)) = pending_error {
            diagnostics.push(Self::unpositioned_diagnostic(
                &msg,
                severity,
                tool_id,
                code_block_start_line,
            ));
        }

        // If no diagnostics parsed but tool failed, use combined output as fallback
        if diagnostics.is_empty() && !output.success {
            let lines: Vec<&str> = combined.lines().map(str::trim).filter(|l| !l.is_empty()).collect();

            if lines.is_empty() {
                let exit_code = output.exit_code;
                diagnostics.push(CodeBlockDiagnostic {
                    file_line: code_block_start_line,
                    column: None,
                    message: format!("Tool exited with code {exit_code}"),
                    severity: DiagnosticSeverity::Error,
                    tool: tool_id.to_string(),
                    code_block_start: code_block_start_line,
                });
            } else {
                for line_text in lines {
                    diagnostics.push(Self::unpositioned_diagnostic(
                        line_text,
                        DiagnosticSeverity::Error,
                        tool_id,
                        code_block_start_line,
                    ));
                }
            }
        }

        diagnostics
    }

    /// Build a diagnostic for a message that carried no `file:line:col:` prefix.
    ///
    /// A tool that states the position in prose instead ("parse error: ... at line 3,
    /// column 8") is still telling us where the problem is, so the diagnostic goes there.
    /// Only a message that names no position at all anchors at the fence.
    fn unpositioned_diagnostic(
        message: &str,
        severity: DiagnosticSeverity,
        tool_id: &str,
        code_block_start_line: usize,
    ) -> CodeBlockDiagnostic {
        let (line_offset, column) = Self::parse_position_in_message(message).unwrap_or((0, None));
        CodeBlockDiagnostic {
            file_line: code_block_start_line + line_offset,
            column,
            message: message.to_string(),
            severity,
            tool: tool_id.to_string(),
            code_block_start: code_block_start_line,
        }
    }

    /// Find an "at line N" / "at line N, column M" position stated inside a message.
    ///
    /// The last occurrence wins: the message is a sentence, and a tool that mentions more
    /// than one position ends on the one the diagnostic is about.
    fn parse_position_in_message(message: &str) -> Option<(usize, Option<usize>)> {
        // ASCII-lowercasing preserves byte offsets, so positions found here index `message`
        // itself; only ASCII digits are ever parsed out of it.
        let lower = message.to_ascii_lowercase();
        let after_marker = &lower[lower.rfind("at line ")? + "at line ".len()..];

        let leading_number = |text: &str| -> Option<usize> {
            let end = text.find(|c: char| !c.is_ascii_digit()).unwrap_or(text.len());
            text[..end].parse::<usize>().ok()
        };

        let line_num = leading_number(after_marker)?;
        let after_line = after_marker.trim_start_matches(|c: char| c.is_ascii_digit());
        let after_separator = after_line
            .trim_start()
            .strip_prefix(',')
            .unwrap_or(after_line)
            .trim_start();
        let column = after_separator.strip_prefix("column ").and_then(leading_number);

        Some((line_num, column))
    }

    /// Parse standard "file:line:col: message" format.
    fn parse_standard_format(
        &self,
        line: &str,
        tool_id: &str,
        code_block_start_line: usize,
    ) -> Option<CodeBlockDiagnostic> {
        // Match patterns like "file.py:1:10: E501 message"
        let mut parts = line.rsplitn(4, ':');
        let message = parts.next()?.trim().to_string();
        let part1 = parts.next()?.trim().to_string();
        let part2 = parts.next()?.trim().to_string();
        let part3 = parts.next().map(|s| s.trim().to_string());

        let (line_part, col_part) = if part3.is_some() {
            (part2, Some(part1))
        } else {
            (part1, None)
        };

        if let Ok(line_num) = line_part.parse::<usize>() {
            let column = col_part.and_then(|s| s.parse::<usize>().ok());
            let message = Self::strip_fixable_markers(&message);
            if !message.is_empty() {
                let severity = self.infer_severity(&message);
                return Some(CodeBlockDiagnostic {
                    file_line: code_block_start_line + line_num,
                    column,
                    message,
                    severity,
                    tool: tool_id.to_string(),
                    code_block_start: code_block_start_line,
                });
            }
        }
        None
    }

    /// Parse a GitHub Actions workflow command: `::<level> <k=v>,...::<message>`.
    ///
    /// Emitted by `--format github-annotation-native` (sqlfluff) and by other tools with a
    /// GitHub Actions mode. Only the three annotation levels are diagnostics: `::group::`
    /// and `::endgroup::` bracket the findings and carry none, so they are skipped rather
    /// than reported. A missing `line` anchors the diagnostic at the fence, the same place
    /// an unpositioned message goes.
    fn parse_github_annotation(line: &str, tool_id: &str, code_block_start_line: usize) -> Option<CodeBlockDiagnostic> {
        let body = line.strip_prefix("::")?;
        let (head, message) = body.split_once("::")?;

        let (level, properties) = head.split_once(' ').unwrap_or((head, ""));
        let severity = match level {
            "error" => DiagnosticSeverity::Error,
            "warning" => DiagnosticSeverity::Warning,
            "notice" => DiagnosticSeverity::Info,
            _ => return None,
        };

        let property = |name: &str| -> Option<usize> {
            properties
                .split(',')
                .filter_map(|pair| pair.split_once('='))
                .find(|(key, _)| *key == name)
                .and_then(|(_, value)| value.parse::<usize>().ok())
        };

        let message = Self::strip_fixable_markers(&decode_workflow_command(message));
        if message.is_empty() {
            return None;
        }

        Some(CodeBlockDiagnostic {
            file_line: code_block_start_line + property("line").unwrap_or(0),
            column: property("col"),
            message,
            severity,
            tool: tool_id.to_string(),
            code_block_start: code_block_start_line,
        })
    }

    /// Parse eslint-style "line:col severity message" format.
    fn parse_eslint_format(
        &self,
        line: &str,
        tool_id: &str,
        code_block_start_line: usize,
    ) -> Option<CodeBlockDiagnostic> {
        // Match "1:10 error Message"
        let parts: Vec<&str> = line.splitn(3, ' ').collect();
        if parts.len() >= 2 {
            let loc_parts: Vec<&str> = parts[0].split(':').collect();
            if loc_parts.len() == 2
                && let (Ok(line_num), Ok(col)) = (loc_parts[0].parse::<usize>(), loc_parts[1].parse::<usize>())
            {
                let (sev_part, msg_part) = if parts.len() >= 3 {
                    (parts[1], parts[2])
                } else {
                    (parts[1], "")
                };
                let message = if msg_part.is_empty() {
                    sev_part.to_string()
                } else {
                    msg_part.to_string()
                };
                let message = Self::strip_fixable_markers(&message);
                let severity = match sev_part.to_lowercase().as_str() {
                    "error" => DiagnosticSeverity::Error,
                    "warning" | "warn" => DiagnosticSeverity::Warning,
                    "info" => DiagnosticSeverity::Info,
                    _ => self.infer_severity(&message),
                };
                return Some(CodeBlockDiagnostic {
                    file_line: code_block_start_line + line_num,
                    column: Some(col),
                    message,
                    severity,
                    tool: tool_id.to_string(),
                    code_block_start: code_block_start_line,
                });
            }
        }
        None
    }

    /// Parse shellcheck-style "In - line N: message" format.
    fn parse_shellcheck_format(
        &self,
        line: &str,
        tool_id: &str,
        code_block_start_line: usize,
    ) -> Option<CodeBlockDiagnostic> {
        // Match "In - line 5:" pattern
        if line.starts_with("In ")
            && line.contains(" line ")
            && let Some(line_start) = line.find(" line ")
        {
            let after_line = &line[line_start + 6..];
            if let Some(colon_pos) = after_line.find(':')
                && let Ok(line_num) = after_line[..colon_pos].trim().parse::<usize>()
            {
                let message = Self::strip_fixable_markers(after_line[colon_pos + 1..].trim());
                if !message.is_empty() {
                    let severity = self.infer_severity(&message);
                    return Some(CodeBlockDiagnostic {
                        file_line: code_block_start_line + line_num,
                        column: None,
                        message,
                        severity,
                        tool: tool_id.to_string(),
                        code_block_start: code_block_start_line,
                    });
                }
            }
        }
        None
    }

    /// Parse shellcheck header line to capture line number context.
    fn parse_shellcheck_header(&self, line: &str) -> Option<usize> {
        if line.starts_with("In ")
            && line.contains(" line ")
            && let Some(line_start) = line.find(" line ")
        {
            let after_line = &line[line_start + 6..];
            if let Some(colon_pos) = after_line.find(':') {
                return after_line[..colon_pos].trim().parse::<usize>().ok();
            }
        }
        None
    }

    /// Parse shellcheck message line containing SCXXXX codes.
    fn parse_shellcheck_message(
        &self,
        line: &str,
        tool_id: &str,
        code_block_start_line: usize,
        line_num: usize,
    ) -> Option<CodeBlockDiagnostic> {
        let sc_pos = line.find("SC")?;
        let after_sc = &line[sc_pos + 2..];
        let code_len = after_sc.chars().take_while(char::is_ascii_digit).count();
        if code_len == 0 {
            return None;
        }
        let after_code = &after_sc[code_len..];
        let sev_start = after_code.find('(')? + 1;
        let sev_end = after_code[sev_start..].find(')')? + sev_start;
        let sev = after_code[sev_start..sev_end].trim().to_lowercase();
        let message_start = after_code.find("):")? + 2;
        let message = Self::strip_fixable_markers(after_code[message_start..].trim());
        if message.is_empty() {
            return None;
        }

        let severity = match sev.as_str() {
            "error" => DiagnosticSeverity::Error,
            "warning" | "warn" => DiagnosticSeverity::Warning,
            "info" | "style" => DiagnosticSeverity::Info,
            _ => self.infer_severity(&message),
        };

        Some(CodeBlockDiagnostic {
            file_line: code_block_start_line + line_num,
            column: None,
            message,
            severity,
            tool: tool_id.to_string(),
            code_block_start: code_block_start_line,
        })
    }

    /// Parse `Error: <message>` or `Warning: <message>` lines.
    ///
    /// Used for tools like tombi that output multi-line diagnostics where the
    /// error message and position are on separate lines. Only matches capitalized
    /// prefixes to avoid conflicting with lowercase `error:` in less structured output.
    fn parse_error_line(line: &str) -> Option<(String, DiagnosticSeverity)> {
        let (msg, severity) = if let Some(msg) = line.strip_prefix("Error:") {
            (msg, DiagnosticSeverity::Error)
        } else if let Some(msg) = line.strip_prefix("Warning:") {
            (msg, DiagnosticSeverity::Warning)
        } else {
            return None;
        };
        let msg = msg.trim();
        if msg.is_empty() {
            return None;
        }
        Some((msg.to_string(), severity))
    }

    /// Parse "at line N column M" position lines (case-insensitive).
    ///
    /// Returns (line_number, column_number) if the pattern matches.
    fn parse_at_line_column(line: &str) -> Option<(usize, usize)> {
        let lower = line.to_lowercase();
        let rest = lower.strip_prefix("at line ")?;
        let mut parts = rest.split_whitespace();
        let line_num: usize = parts.next()?.parse().ok()?;
        if parts.next()? != "column" {
            return None;
        }
        let col: usize = parts.next()?.parse().ok()?;
        Some((line_num, col))
    }

    /// Infer severity from message content.
    fn infer_severity(&self, message: &str) -> DiagnosticSeverity {
        let lower = message.to_lowercase();
        if lower.contains("error")
            || lower.starts_with('e') && lower.chars().nth(1).is_some_and(|c| c.is_ascii_digit())
            || lower.starts_with('f') && lower.chars().nth(1).is_some_and(|c| c.is_ascii_digit())
        {
            DiagnosticSeverity::Error
        } else if lower.contains("warning")
            || lower.contains("warn")
            || lower.starts_with('w') && lower.chars().nth(1).is_some_and(|c| c.is_ascii_digit())
        {
            DiagnosticSeverity::Warning
        } else {
            DiagnosticSeverity::Info
        }
    }

    /// Strip "fixable" markers from external tool messages.
    ///
    /// External tools like ruff show `[*]` to indicate fixable issues, but in rumdl's
    /// context these markers can be misleading - the lint tool's fix capability may
    /// differ from what our configured formatter can fix. We strip these markers
    /// to avoid making promises we can't keep.
    fn strip_fixable_markers(message: &str) -> String {
        message
            .replace(" [*]", "")
            .replace("[*] ", "")
            .replace("[*]", "")
            .replace(" (fixable)", "")
            .replace("(fixable) ", "")
            .replace("(fixable)", "")
            .replace(" [fix available]", "")
            .replace("[fix available] ", "")
            .replace("[fix available]", "")
            .replace(" [autofix]", "")
            .replace("[autofix] ", "")
            .replace("[autofix]", "")
            .trim()
            .to_string()
    }
}

/// Every fenced code block CommonMark sees in `content`, in document order,
/// inside containers included.
pub fn extract_fenced_code_blocks(content: &str) -> Vec<FencedCodeBlockInfo> {
    let mut blocks = Vec::new();
    let mut current_block: Option<FencedCodeBlockBuilder> = None;

    let options = rumdl_parser_options_for(content);
    let parser = Parser::new_ext(content, options).into_offset_iter();

    // Byte offset at which each line starts, so a fence's line number is a binary
    // search rather than a newline count over everything before it.
    let line_starts: Vec<usize> = std::iter::once(0)
        .chain(content.match_indices('\n').map(|(i, _)| i + 1))
        .collect();
    let line_of = |offset: usize| line_starts.partition_point(|&start| start <= offset) - 1;

    // Where each footnote definition enclosing the current event starts.
    let mut open_footnotes: Vec<usize> = Vec::new();

    for (event, range) in parser {
        match event {
            Event::Start(Tag::FootnoteDefinition(_)) => open_footnotes.push(range.start),
            Event::End(TagEnd::FootnoteDefinition) => {
                open_footnotes.pop();
            }
            Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(info))) => {
                let info_string = info.to_string();
                let language = info_string.split_whitespace().next().unwrap_or("").to_string();

                let start_line = line_of(range.start);

                // The code starts on the line after the opening fence
                let content_start = content[range.start..]
                    .find('\n')
                    .map_or(content.len(), |i| range.start + i + 1);

                // The event starts at the fence itself, so everything before it
                // on its line belongs to the containers and the fence's indent.
                let fence_line_prefix = continuation_of_footnote_labels(
                    &content[line_starts[start_line]..range.start],
                    open_footnotes
                        .iter()
                        .filter(|&&start| start >= line_starts[start_line])
                        .map(|&start| start - line_starts[start_line]),
                );
                let fence = &content[range.start..];
                let fence_char = if fence.starts_with('~') { '~' } else { '`' };
                let fence_length = fence.chars().take_while(|&c| c == fence_char).count();

                current_block = Some(FencedCodeBlockBuilder {
                    start_line,
                    content_start,
                    language,
                    info_string,
                    fence_char,
                    fence_length,
                    content_prefix: content_prefix_of(&fence_line_prefix),
                    pieces: Vec::new(),
                });
            }
            Event::Text(text) => {
                if let Some(builder) = current_block.as_mut() {
                    builder.pieces.push((range, text.into_string()));
                }
            }
            Event::End(TagEnd::CodeBlock) => {
                if let Some(builder) = current_block.take() {
                    // The block's range can run through the line ending of its
                    // last line, so its last byte is what places it.
                    let end_line = line_of(range.end.saturating_sub(1).max(range.start));

                    // The source of the code runs to the end of its last line,
                    // line ending included. An empty block has no source at all.
                    let content_end = builder
                        .pieces
                        .last()
                        .map_or(builder.content_start, |(last, _)| last.end);
                    let (code, line_prefix_widths) =
                        block_code_from_pieces(content, &builder.pieces, |offset| line_starts[line_of(offset)]);

                    blocks.push(FencedCodeBlockInfo {
                        start_line: builder.start_line,
                        end_line,
                        content_start: builder.content_start,
                        content_end: content_end.max(builder.content_start),
                        language: builder.language,
                        info_string: builder.info_string,
                        fence_char: builder.fence_char,
                        fence_length: builder.fence_length,
                        content_prefix: builder.content_prefix,
                        code,
                        line_prefix_widths,
                    });
                }
            }
            _ => {}
        }
    }

    blocks
}

/// Formatted code waiting to replace the code of a block.
#[derive(Debug, Clone)]
pub struct WriteBack {
    /// The block's position among the blocks extracted from the document.
    pub index: usize,
    pub code: String,
}

/// `content` with each write-back's code in place of its block's code, keeping
/// only the write-backs after which `extract` still reads the document as the
/// same blocks with exactly the new code; and the indices of the blocks whose
/// write-back was refused.
///
/// Whether new code can sit where the old code did depends on more than the
/// block's containers: code with a line that closes the fence ends the block
/// early, and everything after that line becomes document. A refused block
/// keeps its original code. `extract` must be the function `blocks` came from,
/// so an unchanged document reads back as exactly `blocks`, in document order.
///
/// Blocks are expected not to overlap. Write-backs to blocks that do are
/// refused, since replacing one would leave the other's range pointing at
/// different text.
pub fn write_back_verified(
    content: &str,
    blocks: &[FencedCodeBlockInfo],
    write_backs: &[WriteBack],
    extract: impl Fn(&str) -> Vec<FencedCodeBlockInfo>,
) -> (String, Vec<usize>) {
    if write_backs.is_empty() {
        return (content.to_string(), Vec::new());
    }
    let overlaps = |a: &WriteBack, b: &WriteBack| {
        let (a, b) = (&blocks[a.index], &blocks[b.index]);
        a.content_start < b.content_end && b.content_start < a.content_end
    };
    let (mut ordered, mut refused): (Vec<&WriteBack>, Vec<&WriteBack>) = write_backs.iter().partition(|write_back| {
        !write_backs
            .iter()
            .any(|other| other.index != write_back.index && overlaps(write_back, other))
    });
    // Last block first, so each replacement leaves the offsets of the blocks
    // before it valid.
    ordered.sort_by_key(|write_back| std::cmp::Reverse(write_back.index));

    let replace = |document: &mut String, write_back: &WriteBack| {
        let block = &blocks[write_back.index];
        document.replace_range(
            block.content_start..block.content_end,
            &block.source_for(&write_back.code),
        );
    };
    // Extractors differ in whether code keeps CRLF line endings: the CommonMark
    // parser reads them as LF, the MkDocs extractor keeps them. Code a formatter
    // wrote with either line ending is the same code.
    let reads_back = |document: &str, codes: &[&str]| {
        extract(document)
            .iter()
            .map(|block| block.code.replace("\r\n", "\n"))
            .eq(codes.iter().map(|code| code.replace("\r\n", "\n")))
    };
    let original_codes: Vec<&str> = blocks.iter().map(|block| block.code.as_str()).collect();

    let refused_indices = |refused: Vec<&WriteBack>| {
        let mut indices: Vec<usize> = refused.iter().map(|write_back| write_back.index).collect();
        indices.sort_unstable();
        indices
    };

    let mut codes = original_codes.clone();
    let mut result = content.to_string();
    for write_back in &ordered {
        replace(&mut result, write_back);
        codes[write_back.index] = &write_back.code;
    }
    if reads_back(&result, &codes) {
        return (result, refused_indices(refused));
    }

    // At least one write-back changes the document's structure: apply them one
    // at a time and keep each only if the document still reads back.
    let mut codes = original_codes;
    let mut result = content.to_string();
    for write_back in ordered {
        let mut candidate = result.clone();
        replace(&mut candidate, write_back);
        let original = std::mem::replace(&mut codes[write_back.index], &write_back.code);
        if reads_back(&candidate, &codes) {
            result = candidate;
        } else {
            codes[write_back.index] = original;
            refused.push(write_back);
        }
    }
    (result, refused_indices(refused))
}

/// Builder for FencedCodeBlockInfo during parsing.
struct FencedCodeBlockBuilder {
    start_line: usize,
    content_start: usize,
    language: String,
    info_string: String,
    fence_char: char,
    fence_length: usize,
    content_prefix: String,
    /// The parser's text events for the code, each with its source range.
    pieces: Vec<(std::ops::Range<usize>, String)>,
}

/// The fence line prefix with each footnote label on it (`[^note]:` and the
/// whitespace after it, at the given byte offsets) replaced by the four spaces a
/// footnote definition's continuation lines are indented by.
///
/// A label is as wide as its name, but a footnote definition always continues
/// at four columns, so its width on the fence line says nothing about the
/// lines after it.
fn continuation_of_footnote_labels(fence_line_prefix: &str, label_starts: impl Iterator<Item = usize>) -> String {
    let mut prefix = fence_line_prefix.to_string();
    let mut label_starts: Vec<usize> = label_starts.collect();
    label_starts.sort_unstable();
    // Later labels first, so earlier offsets stay valid.
    for start in label_starts.into_iter().rev() {
        let Some(label_end) = footnote_label_end(&prefix[start..]) else {
            continue;
        };
        let rest = &prefix[start + label_end..];
        let end = start + label_end + rest.len() - rest.trim_start_matches([' ', '\t']).len();
        prefix.replace_range(start..end, "    ");
    }
    prefix
}

/// The length of the `[^label]:` that `text` starts with.
fn footnote_label_end(text: &str) -> Option<usize> {
    let mut escaped = false;
    for (i, c) in text.char_indices().skip(2) {
        match c {
            _ if escaped => escaped = false,
            '\\' => escaped = true,
            ']' => return text[i + 1..].starts_with(':').then_some(i + 2),
            _ => {}
        }
    }
    None
}

/// The prefix a content line repeats for the containers around a fence, from
/// what precedes the opening fence on its own line.
///
/// Only blockquote markers, whitespace and list markers can precede a fence, so
/// keeping `>` and turning everything else into spaces gives the continuation of
/// every container. Tabs become the spaces they span on the fence line, so a
/// container that consumes part of a tab still gets exactly its columns.
///
/// A `>` written without its optional space (`>code`, `>- item`) gets one: on a
/// continuation line the parser takes the first space after `>` as that
/// optional space, so without it the line would lose a column to the quote.
fn content_prefix_of(fence_line_prefix: &str) -> String {
    let mut prefix = String::with_capacity(fence_line_prefix.len() + 1);
    let mut column = 0;
    let mut chars = fence_line_prefix.chars().peekable();
    while let Some(c) = chars.next() {
        match c {
            '>' => {
                prefix.push('>');
                if !matches!(chars.peek(), Some(' ' | '\t')) {
                    prefix.push(' ');
                }
                column += 1;
            }
            '\t' => {
                let width = 4 - column % 4;
                prefix.extend(std::iter::repeat_n(' ', width));
                column += width;
            }
            _ => {
                prefix.push(' ');
                column += 1;
            }
        }
    }
    prefix
}

/// The code of a fenced block and the source prefix width of each of its lines,
/// from the parser's text events.
///
/// The parser has already removed what CommonMark does not count as code
/// (blockquote markers, list indentation, the fence's indentation). A line can
/// arrive in several events (a CRLF line ending splits one), and one event can
/// span several lines when a line carries no prefix at all (a blank line in a
/// list item). A line's prefix is whatever precedes its first event on its
/// source line. An event the parser rewrote (spaces standing in for a tab a
/// container split) is placed at the start of its source range.
fn block_code_from_pieces(
    content: &str,
    pieces: &[(std::ops::Range<usize>, String)],
    line_start_of: impl Fn(usize) -> usize,
) -> (String, Vec<usize>) {
    let mut code = String::new();
    let mut widths = Vec::new();
    // A carriage return before a line's first event is its line ending (the
    // parser reports a CRLF blank line as just its `\n`), never prefix.
    let prefix_width = |offset: usize| {
        let before = &content[line_start_of(offset)..offset];
        before.strip_suffix('\r').unwrap_or(before).chars().count()
    };

    for (range, text) in pieces {
        if content.get(range.clone()) == Some(text.as_str()) {
            let mut offset = range.start;
            for segment in text.split_inclusive('\n') {
                if code.is_empty() || code.ends_with('\n') {
                    widths.push(prefix_width(offset));
                }
                code.push_str(segment);
                offset += segment.len();
            }
        } else {
            // The parser writes out as spaces the columns of a tab a container
            // only partly consumed. Those columns are code, so they are not prefix.
            if code.is_empty() || code.ends_with('\n') {
                widths.push(prefix_width(range.start).saturating_sub(text.chars().count()));
            }
            code.push_str(text);
        }
    }
    (code, widths)
}

/// Strip a fence's indentation from each line of a code block's source, and
/// report how much was stripped from each line.
fn strip_indent_from_block(content: &str, indent_prefix: &str) -> (String, Vec<usize>) {
    let mut code = String::with_capacity(content.len());
    let mut widths = Vec::new();
    for line in content.split_inclusive('\n') {
        match line.strip_prefix(indent_prefix) {
            Some(stripped) => {
                code.push_str(stripped);
                widths.push(indent_prefix.chars().count());
            }
            None => {
                code.push_str(line);
                widths.push(0);
            }
        }
    }
    (code, widths)
}

/// Put the containers' prefix back in front of each line of formatted code.
///
/// A blank line takes the prefix without its trailing whitespace: `>` inside a
/// blockquote, nothing at all inside a list item.
fn apply_content_prefix(content: &str, prefix: &str) -> String {
    if prefix.is_empty() {
        return content.to_string();
    }

    let blank_prefix = prefix.trim_end();
    let mut out = String::with_capacity(content.len() + prefix.len());
    for line in content.split_inclusive('\n') {
        if line == "\n" {
            out.push_str(blank_prefix);
        } else {
            out.push_str(prefix);
        }
        out.push_str(line);
    }
    out
}

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

    fn default_config() -> CodeBlockToolsConfig {
        CodeBlockToolsConfig::default()
    }

    #[test]
    fn test_extract_code_blocks() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = r#"# Example

```python
def hello():
    print("Hello")
```

Some text

```rust
fn main() {}
```
"#;

        let blocks = processor.extract_code_blocks(content);

        assert_eq!(blocks.len(), 2);

        assert_eq!(blocks[0].language, "python");
        assert_eq!(blocks[0].fence_char, '`');
        assert_eq!(blocks[0].fence_length, 3);
        assert_eq!(blocks[0].start_line, 2);
        assert_eq!(blocks[0].content_prefix, "");
        assert_eq!(blocks[0].code, "def hello():\n    print(\"Hello\")\n");

        assert_eq!(blocks[1].language, "rust");
        assert_eq!(blocks[1].fence_char, '`');
        assert_eq!(blocks[1].fence_length, 3);
    }

    #[test]
    fn test_extract_code_blocks_with_info_string() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```python title=\"example.py\"\ncode\n```";
        let blocks = processor.extract_code_blocks(content);

        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].language, "python");
        assert_eq!(blocks[0].info_string, "python title=\"example.py\"");
    }

    #[test]
    fn test_extract_code_blocks_tilde_fence() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "~~~bash\necho hello\n~~~";
        let blocks = processor.extract_code_blocks(content);

        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].language, "bash");
        assert_eq!(blocks[0].fence_char, '~');
        assert_eq!(blocks[0].fence_length, 3);
        assert_eq!(blocks[0].content_prefix, "");
        assert_eq!(blocks[0].code, "echo hello\n");
    }

    #[test]
    fn test_extract_code_blocks_with_indent_prefix() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "  - item\n    ```python\n    print('hi')\n    ```";
        let blocks = processor.extract_code_blocks(content);

        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].content_prefix, "    ");
        assert_eq!(blocks[0].code, "print('hi')\n");
        assert_eq!(blocks[0].line_prefix_widths, vec![4]);
    }

    /// Fence line numbers across content where byte offsets, character counts and line
    /// counts all diverge: multibyte text, CRLF endings, a block in a blockquote, and a
    /// final block with no trailing newline.
    #[test]
    fn test_extract_code_blocks_line_numbers() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "# Ünïcödé 标题\r\n\r\n```python\r\nx = 'é'\r\n```\r\n\r\n> ```sh\n> echo 你好\n> ```\n\n~~~rust\nfn main() {}\n~~~";
        let blocks = processor.extract_code_blocks(content);

        let lines: Vec<(usize, usize)> = blocks.iter().map(|b| (b.start_line, b.end_line)).collect();
        assert_eq!(lines, vec![(2, 4), (6, 8), (10, 12)]);
    }

    /// An unterminated block ends on its own last line, not on the line that
    /// follows it, however it is ended.
    #[test]
    fn test_extract_unterminated_code_block_line_numbers() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let cases = [
            ("> ```sh\n> a\nb\n", (0, 1)),
            ("- ```sh\n  a\n\nb\n", (0, 2)),
            ("```sh\na\n", (0, 1)),
            ("```sh\na", (0, 1)),
            ("```sh\r\na\r\n", (0, 1)),
            ("```sh\n", (0, 0)),
        ];
        for (content, expected) in cases {
            let blocks = processor.extract_code_blocks(content);
            let lines: Vec<(usize, usize)> = blocks.iter().map(|b| (b.start_line, b.end_line)).collect();
            assert_eq!(lines, vec![expected], "{content:?}");
        }
    }

    #[test]
    fn test_extract_code_blocks_no_language() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```\nplain code\n```";
        let blocks = processor.extract_code_blocks(content);

        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].language, "");
    }

    #[test]
    fn test_resolve_language_linguist() {
        let mut config = default_config();
        config.normalize_language = NormalizeLanguage::Linguist;
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        assert_eq!(processor.resolve_language("py"), "python");
        assert_eq!(processor.resolve_language("bash"), "shell");
        assert_eq!(processor.resolve_language("js"), "javascript");
    }

    #[test]
    fn test_resolve_language_exact() {
        let mut config = default_config();
        config.normalize_language = NormalizeLanguage::Exact;
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        assert_eq!(processor.resolve_language("py"), "py");
        assert_eq!(processor.resolve_language("BASH"), "bash");
    }

    #[test]
    fn test_resolve_language_user_alias_override() {
        let mut config = default_config();
        config.language_aliases.insert("py".to_string(), "python".to_string());
        config.normalize_language = NormalizeLanguage::Exact;
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        assert_eq!(processor.resolve_language("PY"), "python");
    }

    #[test]
    fn test_indent_strip_and_reapply_roundtrip() {
        let raw = "    def hello():\n        print('hi')";
        let (stripped, widths) = strip_indent_from_block(raw, "    ");
        assert_eq!(stripped, "def hello():\n    print('hi')");
        assert_eq!(widths, vec![4, 4]);

        assert_eq!(apply_content_prefix(&stripped, "    "), raw);
    }

    /// The code a tool receives from a fence inside containers is the code
    /// CommonMark gives that block: no blockquote markers, no list indentation.
    /// Each expectation is cmark's code block text for the same document.
    #[test]
    fn test_extract_code_blocks_strips_container_prefixes() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // (document, code, content prefix, prefix width of each code line)
        let cases: &[(&str, &str, &str, &[usize])] = &[
            ("> ```m\n> a\n>   b\n> ```\n", "a\n  b\n", "> ", &[2, 2]),
            ("> ```m\n> a\n>\n> b\n> ```\n", "a\n\nb\n", "> ", &[2, 1, 2]),
            // The optional space after `>` is not code, so it is part of the prefix.
            (">```m\n>a\n>  b\n>```\n", "a\n b\n", "> ", &[1, 2]),
            ("> > ```m\n> > a\n> > ```\n", "a\n", "> > ", &[4]),
            ("  > ```m\n  > a\n  > ```\n", "a\n", "  > ", &[4]),
            ("- ```m\n  a\n    b\n  ```\n", "a\n  b\n", "  ", &[2, 2]),
            ("1. ```m\n   a\n\n   b\n   ```\n", "a\n\nb\n", "   ", &[3, 0, 3]),
            ("> - ```m\n>   a\n>\n>   b\n>   ```\n", "a\n\nb\n", ">   ", &[4, 1, 4]),
            ("- > ```m\n  > a\n  > b\n  > ```\n", "a\nb\n", "  > ", &[4, 4]),
            (">- ```m\n>   a\n>   ```\n", "a\n", ">   ", &[4]),
            // Blank lines are code too, at the end of a block or as all of it.
            ("> ```m\n> a\n>\n> ```\n", "a\n\n", "> ", &[2, 1]),
            ("> ```m\n>\n> ```\n", "\n", "> ", &[1]),
            // Unclosed: the block runs to the end of its container, last line included.
            ("- ```m\n  a\n  b", "a\nb", "  ", &[2, 2]),
            ("> ```m\n> a\n", "a\n", "> ", &[2]),
            // A tab the quote marker only partly consumes leaves code columns.
            ("> ```m\n>\ta\n> ```\n", "  a\n", "> ", &[0]),
            // The parser ends code lines with LF, and a carriage return is never
            // part of a prefix.
            ("> ```m\r\n> a\r\n>\r\n> b\r\n> ```\r\n", "a\n\nb\n", "> ", &[2, 1, 2]),
            ("```m\r\na\r\n\r\nb\r\n```\r\n", "a\n\nb\n", "", &[0, 0, 0]),
            // A footnote definition continues at four columns, however long the
            // label that opens it on the fence's line.
            ("[^a]: ```m\n    a\n    ```\n", "a\n", "    ", &[4]),
            ("[^long-label]: ```m\n    a\n    ```\n", "a\n", "    ", &[4]),
            ("[^a]:\t```m\n    a\n    ```\n", "a\n", "    ", &[4]),
            ("[^a]:  \t ```m\n    a\n    ```\n", "a\n", "    ", &[4]),
            ("[^a\\]b]: ```m\n    a\n    ```\n", "a\n", "    ", &[4]),
            ("[^a]: Note.\n\n    ```m\n    a\n    ```\n", "a\n", "    ", &[4]),
            ("> [^a]: ```m\n>     a\n>     ```\n", "a\n", ">     ", &[6]),
            ("- [^a]: ```m\n      a\n      ```\n", "a\n", "      ", &[6]),
            ("[^a]: > ```m\n    > a\n    > ```\n", "a\n", "    > ", &[6]),
            ("[^a]: - ```m\n      a\n      ```\n", "a\n", "      ", &[6]),
        ];

        for &(document, code, prefix, widths) in cases {
            let blocks = processor.extract_code_blocks(document);
            assert_eq!(blocks.len(), 1, "{document:?}");
            let block = &blocks[0];
            assert_eq!(block.code, code, "code of {document:?}");
            assert_eq!(block.content_prefix, prefix, "prefix of {document:?}");
            assert_eq!(block.line_prefix_widths, widths, "widths of {document:?}");
            assert_eq!(
                (block.fence_char, block.fence_length),
                ('`', 3),
                "fence of {document:?}"
            );
        }
    }

    /// The source region of a block's code covers its lines exactly, prefixes
    /// and line endings included, in LF and CRLF documents.
    #[test]
    fn test_extract_code_blocks_container_region() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let cases = [
            ("> ```m\n> a\n>\n> b\n> ```\n", "> a\n>\n> b\n"),
            ("- ```m\n  a\n  ```\n", "  a\n"),
            ("- ```m\n  a\n  b", "  a\n  b"),
            ("> ```m\r\n> a\r\n> b\r\n> ```\r\n", "> a\r\n> b\r\n"),
        ];
        for (document, region) in cases {
            let blocks = processor.extract_code_blocks(document);
            assert_eq!(blocks.len(), 1, "{document:?}");
            assert_eq!(
                &document[blocks[0].content_start..blocks[0].content_end],
                region,
                "{document:?}"
            );
        }
    }

    #[test]
    fn test_apply_content_prefix_blank_lines() {
        assert_eq!(apply_content_prefix("a\n\nb", "> "), "> a\n>\n> b");
        assert_eq!(apply_content_prefix("a\n\nb", "  "), "  a\n\n  b");
        assert_eq!(apply_content_prefix("a\n\nb", ">   "), ">   a\n>\n>   b");
        assert_eq!(apply_content_prefix("a\n\nb", ""), "a\n\nb");
    }

    /// A formatter that indents, adds a blank line and changes text, so every
    /// part of the write-back is exercised.
    fn container_formatter_config() -> CodeBlockToolsConfig {
        use super::super::config::{LanguageToolConfig, ToolDefinition};

        let mut config = default_config();
        config.normalize_language = NormalizeLanguage::Exact;
        config.languages.insert(
            "m".to_string(),
            LanguageToolConfig {
                format: vec!["reshape".to_string()],
                ..Default::default()
            },
        );
        config.tools.insert(
            "reshape".to_string(),
            ToolDefinition {
                command: vec![
                    "awk".to_string(),
                    "NR == 1 { print toupper($0); print \"\"; next } { print \"  \" toupper($0) }".to_string(),
                ],
                ..Default::default()
            },
        );
        config
    }

    /// Formatted code that would end its block early is not written back: the
    /// block stays as it was and the refusal is reported, while every other
    /// block is still formatted.
    #[cfg(unix)]
    #[test]
    fn test_format_refuses_write_back_that_changes_structure() {
        use super::super::config::{LanguageToolConfig, ToolDefinition};

        let mut config = container_formatter_config();
        config.languages.insert(
            "f".to_string(),
            LanguageToolConfig {
                format: vec!["fence".to_string()],
                ..Default::default()
            },
        );
        config.tools.insert(
            "fence".to_string(),
            ToolDefinition {
                command: vec!["awk".to_string(), "{ print \"```\"; print toupper($0) }".to_string()],
                ..Default::default()
            },
        );
        for (language, program) in [
            ("c", "{ sub(/\\r$/, \"\"); printf \"%s\\r\\n\", toupper($0) }"),
            ("cf", "{ sub(/\\r$/, \"\"); printf \"```\\r\\n%s\\r\\n\", toupper($0) }"),
        ] {
            config.languages.insert(
                language.to_string(),
                LanguageToolConfig {
                    format: vec![language.to_string()],
                    ..Default::default()
                },
            );
            config.tools.insert(
                language.to_string(),
                ToolDefinition {
                    command: vec!["awk".to_string(), program.to_string()],
                    ..Default::default()
                },
            );
        }
        // (document, document after formatting, lines of the refused blocks)
        let standard_cases: &[(&str, &str, &[usize])] = &[
            ("```f\na\n```\n", "```f\na\n```\n", &[1]),
            ("> ```f\n> a\n> ```\n", "> ```f\n> a\n> ```\n", &[1]),
            (
                "```m\na\n```\n\n```f\na\n```\n\n- ```m\n  b\n  ```\n",
                "```m\nA\n\n```\n\n```f\na\n```\n\n- ```m\n  B\n\n  ```\n",
                &[5],
            ),
            // A longer fence holds the same output.
            ("````f\na\n````\n", "````f\n```\nA\n````\n", &[]),
            // Code written with CRLF line endings reads back as the same code.
            ("```c\na\n```\n", "```c\nA\r\n```\n", &[]),
            ("```c\r\na\r\n```\r\n", "```c\r\nA\r\n```\r\n", &[]),
            ("> ```c\r\n> a\r\n> ```\r\n", "> ```c\r\n> A\r\n> ```\r\n", &[]),
            ("```cf\r\na\r\n```\r\n", "```cf\r\na\r\n```\r\n", &[1]),
        ];
        // The MkDocs extractor keeps CRLF line endings in the code it reads.
        let mkdocs_cases: &[(&str, &str, &[usize])] = &[
            (
                "!!! note\n\n    ```c\n    a\n    ```\n",
                "!!! note\n\n    ```c\n    A\r\n    ```\n",
                &[],
            ),
            (
                "!!! note\r\n\r\n    ```c\r\n    a\r\n    ```\r\n",
                "!!! note\r\n\r\n    ```c\r\n    A\r\n    ```\r\n",
                &[],
            ),
            (
                "!!! note\r\n\r\n    ```cf\r\n    a\r\n    ```\r\n",
                "!!! note\r\n\r\n    ```cf\r\n    a\r\n    ```\r\n",
                &[3],
            ),
        ];

        for (flavor, cases) in [
            (MarkdownFlavor::Standard, standard_cases),
            (MarkdownFlavor::MkDocs, mkdocs_cases),
        ] {
            let processor = CodeBlockToolProcessor::new(&config, flavor);
            for &(document, expected, refused) in cases {
                let output = processor.format(document).expect("format should succeed");
                assert_eq!(output.content, expected, "{document:?}");
                let lines: Vec<usize> = output.failures.iter().map(|failure| failure.file_line).collect();
                assert_eq!(lines, refused, "{document:?}");
                assert_eq!(output.error_messages.len(), refused.len(), "{document:?}");
                assert_eq!(output.had_errors, !refused.is_empty(), "{document:?}");
                assert!(
                    output
                        .failures
                        .iter()
                        .all(|failure| failure.message.contains("block left unchanged")),
                    "{:?}",
                    output.failures
                );
            }
        }
    }

    /// With MkDocs, an admonition example inside a `markdown` block is part of
    /// that block's code, so only the `markdown` block is formatted.
    #[cfg(unix)]
    #[test]
    fn test_format_mkdocs_admonition_example_inside_a_block() {
        use super::super::config::{LanguageToolConfig, ToolDefinition};

        let mut config = default_config();
        config.normalize_language = NormalizeLanguage::Exact;
        for language in ["markdown", "json"] {
            config.languages.insert(
                language.to_string(),
                LanguageToolConfig {
                    format: vec!["replace".to_string()],
                    ..Default::default()
                },
            );
        }
        config.tools.insert(
            "replace".to_string(),
            ToolDefinition {
                command: vec!["awk".to_string(), "END { print \"x\" }".to_string()],
                ..Default::default()
            },
        );
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::MkDocs);

        let long = "a".repeat(200);
        let document =
            format!("# T\n\n```markdown\n!!! note\n\n    ```json\n    {{\"k\": \"{long}\"}}\n    ```\n```\n");
        let output = processor.format(&document).expect("format should succeed");
        assert_eq!(output.content, "# T\n\n```markdown\nx\n```\n");
        assert!(output.failures.is_empty(), "{:?}", output.failures);
    }

    /// Replacing one of two overlapping blocks would leave the other's range
    /// pointing at different text, so neither is written back.
    #[test]
    fn test_write_back_refuses_overlapping_blocks() {
        // The last block, and a block inside it starting two bytes in.
        let with_inner = |mut blocks: Vec<FencedCodeBlockInfo>| {
            let mut inner = blocks[1].clone();
            inner.content_start += 2;
            inner.code = inner.code[2..].to_string();
            blocks.push(inner);
            blocks
        };
        let content = format!("```m\nz\n```\n\n```m\n{}\n```\n", "a".repeat(100));
        let blocks = with_inner(extract_fenced_code_blocks(&content));
        let write_backs = [
            WriteBack {
                index: 0,
                code: "Z\n".to_string(),
            },
            WriteBack {
                index: 1,
                code: "A\n".to_string(),
            },
            WriteBack {
                index: 2,
                code: "B\n".to_string(),
            },
        ];

        let (result, refused) = write_back_verified(&content, &blocks, &write_backs, |document| {
            with_inner(extract_fenced_code_blocks(document))
        });
        assert_eq!(result, content.replacen("```m\nz\n", "```m\nZ\n", 1));
        assert_eq!(refused, [1, 2]);
    }

    /// Formatted code goes back inside the same containers, so the document
    /// still parses as the same block with the formatted code in it.
    #[cfg(unix)]
    #[test]
    fn test_format_writes_back_inside_containers() {
        let config = container_formatter_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // (document, document after formatting, the formatted block's code)
        let cases = [
            (
                "- ```m\n  a\n  b\n  ```\n- next\n",
                "- ```m\n  A\n\n    B\n  ```\n- next\n",
                "A\n\n  B\n",
            ),
            (
                "1. ```m\n   a\n   b\n   ```\n",
                "1. ```m\n   A\n\n     B\n   ```\n",
                "A\n\n  B\n",
            ),
            (
                "> ```m\n> a\n> b\n> ```\n",
                "> ```m\n> A\n>\n>   B\n> ```\n",
                "A\n\n  B\n",
            ),
            (
                "> > ```m\n> > a\n> > b\n> > ```\n",
                "> > ```m\n> > A\n> >\n> >   B\n> > ```\n",
                "A\n\n  B\n",
            ),
            (
                "> - ```m\n>   a\n>   b\n>   ```\n",
                "> - ```m\n>   A\n>\n>     B\n>   ```\n",
                "A\n\n  B\n",
            ),
            (
                "- > ```m\n  > a\n  > b\n  > ```\n",
                "- > ```m\n  > A\n  >\n  >   B\n  > ```\n",
                "A\n\n  B\n",
            ),
            // The added blank line after a one-line block stays inside the quote.
            ("> ```m\n> a\n> ```\n", "> ```m\n> A\n>\n> ```\n", "A\n\n"),
            // Code indented past `>` without a space keeps every column.
            (
                ">```m\n>a\n>  b\n>```\n",
                ">```m\n> A\n>\n>    B\n>```\n",
                "A\n\n   B\n",
            ),
            // The same for a list item that starts right after `>`.
            (
                ">2) ```m\n>    a\n>    b\n>    ```\n",
                ">2) ```m\n>    A\n>\n>      B\n>    ```\n",
                "A\n\n  B\n",
            ),
            // A tab after the marker reaches column 4, so the item's content
            // does too; one space per character would leave the code outside it.
            (
                "1.\t```m\n    a\n    b\n    ```\n",
                "1.\t```m\n    A\n\n      B\n    ```\n",
                "A\n\n  B\n",
            ),
            // A footnote definition continues at four columns, not at the
            // width of the label on the fence's line.
            (
                "[^long-label]: ```m\n    a\n    b\n    ```\n",
                "[^long-label]: ```m\n    A\n\n      B\n    ```\n",
                "A\n\n  B\n",
            ),
            (
                "> [^a]: ```m\n>     a\n>     b\n>     ```\n",
                "> [^a]: ```m\n>     A\n>\n>       B\n>     ```\n",
                "A\n\n  B\n",
            ),
            (
                "- [^a]: ```m\n      a\n      b\n      ```\n",
                "- [^a]: ```m\n      A\n\n        B\n      ```\n",
                "A\n\n  B\n",
            ),
            ("```m\na\nb\n```\n", "```m\nA\n\n  B\n```\n", "A\n\n  B\n"),
        ];

        for (document, expected, code) in cases {
            let output = processor.format(document).expect("format should succeed");
            assert!(
                output.error_messages.is_empty(),
                "{document:?}: {:?}",
                output.error_messages
            );
            assert_eq!(output.content, expected, "{document:?}");

            let blocks = processor.extract_code_blocks(&output.content);
            assert_eq!(blocks.len(), 1, "{expected:?}");
            assert_eq!(blocks[0].code, code, "{expected:?}");
        }
    }

    /// A tool's column counts from the start of the code it was given; the
    /// document column adds the line's container prefix.
    #[cfg(unix)]
    #[test]
    fn test_lint_columns_include_container_prefix() {
        use super::super::config::{LanguageToolConfig, ToolDefinition};

        let mut config = default_config();
        config.normalize_language = NormalizeLanguage::Exact;
        config.languages.insert(
            "m".to_string(),
            LanguageToolConfig {
                lint: vec!["pos".to_string()],
                ..Default::default()
            },
        );
        config.tools.insert(
            "pos".to_string(),
            ToolDefinition {
                command: vec![
                    "sh".to_string(),
                    "-c".to_string(),
                    "cat >/dev/null; echo '-:1:1: one'; echo '-:2:3: two'".to_string(),
                ],
                ..Default::default()
            },
        );
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let cases = [
            ("```m\nx\nyyy\n```\n", [(2, 1), (3, 3)]),
            ("- ```m\n  x\n  yyy\n  ```\n", [(2, 3), (3, 5)]),
            ("> ```m\n> x\n>\n> ```\n", [(2, 3), (3, 4)]),
            ("text\n\n> - ```m\n>   x\n>   yyy\n>   ```\n", [(4, 5), (5, 7)]),
        ];
        for (document, expected) in cases {
            let diagnostics = processor.lint(document).expect("lint should succeed");
            let positions: Vec<(usize, usize)> = diagnostics
                .iter()
                .map(|d| (d.file_line, d.column.expect("positioned diagnostic")))
                .collect();
            assert_eq!(positions, expected, "{document:?}");
        }
    }

    #[test]
    fn test_infer_severity() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        assert_eq!(
            processor.infer_severity("E501 line too long"),
            DiagnosticSeverity::Error
        );
        assert_eq!(
            processor.infer_severity("W291 trailing whitespace"),
            DiagnosticSeverity::Warning
        );
        assert_eq!(
            processor.infer_severity("error: something failed"),
            DiagnosticSeverity::Error
        );
        assert_eq!(
            processor.infer_severity("warning: unused variable"),
            DiagnosticSeverity::Warning
        );
        assert_eq!(
            processor.infer_severity("note: consider using"),
            DiagnosticSeverity::Info
        );
    }

    #[test]
    fn test_parse_standard_format_windows_path() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let output = ToolOutput {
            stdout: "C:\\path\\file.py:2:5: E123 message".to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "ruff:check", 10);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].file_line, 12);
        assert_eq!(diags[0].column, Some(5));
        assert_eq!(diags[0].message, "E123 message");
    }

    #[test]
    fn test_parse_eslint_severity() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let output = ToolOutput {
            stdout: "1:2 error Unexpected token".to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "eslint", 5);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].file_line, 6);
        assert_eq!(diags[0].column, Some(2));
        assert_eq!(diags[0].severity, DiagnosticSeverity::Error);
        assert_eq!(diags[0].message, "Unexpected token");
    }

    #[test]
    fn test_parse_shellcheck_multiline() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let output = ToolOutput {
            stdout: "In - line 3:\necho $var\n ^-- SC2086 (info): Double quote to prevent globbing".to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "shellcheck", 10);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].file_line, 13);
        assert_eq!(diags[0].severity, DiagnosticSeverity::Info);
        assert_eq!(diags[0].message, "Double quote to prevent globbing");
    }

    #[test]
    fn test_lint_no_config() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```python\nprint('hello')\n```";
        let result = processor.lint(content);

        // Should succeed with no diagnostics (no tools configured)
        assert!(result.is_ok());
        assert!(result.unwrap().is_empty());
    }

    #[test]
    fn test_format_no_config() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```python\nprint('hello')\n```";
        let result = processor.format(content);

        // Should succeed with unchanged content (no tools configured)
        assert!(result.is_ok());
        let output = result.unwrap();
        assert_eq!(output.content, content);
        assert!(!output.had_errors);
        assert!(output.error_messages.is_empty());
    }

    #[test]
    fn test_lint_on_missing_language_definition_fail() {
        let mut config = default_config();
        config.on_missing_language_definition = OnMissing::Fail;
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```python\nprint('hello')\n```\n\n```javascript\nconsole.log('hi');\n```";
        let result = processor.lint(content);

        // Should succeed but return diagnostics for both missing language definitions
        assert!(result.is_ok());
        let diagnostics = result.unwrap();
        assert_eq!(diagnostics.len(), 2);
        assert!(diagnostics[0].message.contains("No lint tools configured"));
        assert!(diagnostics[0].message.contains("python"));
        assert!(diagnostics[1].message.contains("javascript"));
    }

    #[test]
    fn test_lint_on_missing_language_definition_fail_fast() {
        let mut config = default_config();
        config.on_missing_language_definition = OnMissing::FailFast;
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```python\nprint('hello')\n```\n\n```javascript\nconsole.log('hi');\n```";
        let result = processor.lint(content);

        // Should fail immediately on first missing language
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, ProcessorError::NoToolsConfigured { .. }));
    }

    #[test]
    fn test_format_on_missing_language_definition_fail() {
        let mut config = default_config();
        config.on_missing_language_definition = OnMissing::Fail;
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```python\nprint('hello')\n```";
        let result = processor.format(content);

        // Should succeed but report errors
        assert!(result.is_ok());
        let output = result.unwrap();
        assert_eq!(output.content, content); // Content unchanged
        assert!(output.had_errors);
        assert!(!output.error_messages.is_empty());
        assert!(output.error_messages[0].contains("No format tools configured"));
    }

    #[test]
    fn test_format_on_missing_language_definition_fail_fast() {
        let mut config = default_config();
        config.on_missing_language_definition = OnMissing::FailFast;
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```python\nprint('hello')\n```";
        let result = processor.format(content);

        // Should fail immediately
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, ProcessorError::NoToolsConfigured { .. }));
    }

    #[test]
    fn test_lint_on_missing_tool_binary_fail() {
        use super::super::config::{LanguageToolConfig, ToolDefinition};

        let mut config = default_config();
        config.on_missing_tool_binary = OnMissing::Fail;

        // Configure a tool with a non-existent binary
        let lang_config = LanguageToolConfig {
            lint: vec!["nonexistent-linter".to_string()],
            ..Default::default()
        };
        config.languages.insert("python".to_string(), lang_config);

        let tool_def = ToolDefinition {
            command: vec!["nonexistent-binary-xyz123".to_string()],
            ..Default::default()
        };
        config.tools.insert("nonexistent-linter".to_string(), tool_def);

        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```python\nprint('hello')\n```";
        let result = processor.lint(content);

        // Should succeed but return diagnostic for missing binary
        assert!(result.is_ok());
        let diagnostics = result.unwrap();
        assert_eq!(diagnostics.len(), 1);
        assert!(diagnostics[0].message.contains("not found in allowed lookup locations"));
    }

    #[test]
    fn test_lint_on_missing_tool_binary_fail_fast() {
        use super::super::config::{LanguageToolConfig, ToolDefinition};

        let mut config = default_config();
        config.on_missing_tool_binary = OnMissing::FailFast;

        // Configure a tool with a non-existent binary
        let lang_config = LanguageToolConfig {
            lint: vec!["nonexistent-linter".to_string()],
            ..Default::default()
        };
        config.languages.insert("python".to_string(), lang_config);

        let tool_def = ToolDefinition {
            command: vec!["nonexistent-binary-xyz123".to_string()],
            ..Default::default()
        };
        config.tools.insert("nonexistent-linter".to_string(), tool_def);

        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```python\nprint('hello')\n```";
        let result = processor.lint(content);

        // Should fail immediately
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, ProcessorError::ToolBinaryNotFound { .. }));
    }

    #[test]
    fn test_format_on_missing_tool_binary_fail() {
        use super::super::config::{LanguageToolConfig, ToolDefinition};

        let mut config = default_config();
        config.on_missing_tool_binary = OnMissing::Fail;

        // Configure a tool with a non-existent binary
        let lang_config = LanguageToolConfig {
            format: vec!["nonexistent-formatter".to_string()],
            ..Default::default()
        };
        config.languages.insert("python".to_string(), lang_config);

        let tool_def = ToolDefinition {
            command: vec!["nonexistent-binary-xyz123".to_string()],
            ..Default::default()
        };
        config.tools.insert("nonexistent-formatter".to_string(), tool_def);

        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```python\nprint('hello')\n```";
        let result = processor.format(content);

        // Should succeed but report errors
        assert!(result.is_ok());
        let output = result.unwrap();
        assert_eq!(output.content, content); // Content unchanged
        assert!(output.had_errors);
        assert!(!output.error_messages.is_empty());
        assert!(output.error_messages[0].contains("not found in allowed lookup locations"));
    }

    #[test]
    fn test_format_on_missing_tool_binary_fail_fast() {
        use super::super::config::{LanguageToolConfig, ToolDefinition};

        let mut config = default_config();
        config.on_missing_tool_binary = OnMissing::FailFast;

        // Configure a tool with a non-existent binary
        let lang_config = LanguageToolConfig {
            format: vec!["nonexistent-formatter".to_string()],
            ..Default::default()
        };
        config.languages.insert("python".to_string(), lang_config);

        let tool_def = ToolDefinition {
            command: vec!["nonexistent-binary-xyz123".to_string()],
            ..Default::default()
        };
        config.tools.insert("nonexistent-formatter".to_string(), tool_def);

        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```python\nprint('hello')\n```";
        let result = processor.format(content);

        // Should fail immediately
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, ProcessorError::ToolBinaryNotFound { .. }));
    }

    #[test]
    fn test_lint_rumdl_builtin_skipped_for_markdown() {
        // Configure the built-in "rumdl" tool for markdown
        // The processor should skip it (handled by embedded markdown linting)
        let mut config = default_config();
        config.languages.insert(
            "markdown".to_string(),
            LanguageToolConfig {
                lint: vec![RUMDL_BUILTIN_TOOL.to_string()],
                ..Default::default()
            },
        );
        config.on_missing_language_definition = OnMissing::Fail;
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```markdown\n# Hello\n```";
        let result = processor.lint(content);

        // Should succeed with no diagnostics - "rumdl" tool is skipped, not treated as unknown
        assert!(result.is_ok());
        assert!(result.unwrap().is_empty());
    }

    #[test]
    fn test_format_rumdl_builtin_skipped_for_markdown() {
        // Configure the built-in "rumdl" tool for markdown
        let mut config = default_config();
        config.languages.insert(
            "markdown".to_string(),
            LanguageToolConfig {
                format: vec![RUMDL_BUILTIN_TOOL.to_string()],
                ..Default::default()
            },
        );
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```markdown\n# Hello\n```";
        let result = processor.format(content);

        // Should succeed with unchanged content - "rumdl" tool is skipped
        assert!(result.is_ok());
        let output = result.unwrap();
        assert_eq!(output.content, content);
        assert!(!output.had_errors);
    }

    #[test]
    fn test_is_markdown_language() {
        // Test the helper function
        assert!(is_markdown_language("markdown"));
        assert!(is_markdown_language("Markdown"));
        assert!(is_markdown_language("MARKDOWN"));
        assert!(is_markdown_language("md"));
        assert!(is_markdown_language("MD"));
        assert!(!is_markdown_language("python"));
        assert!(!is_markdown_language("rust"));
        assert!(!is_markdown_language(""));
    }

    // Issue #423: MkDocs admonition code block detection

    #[test]
    fn test_extract_mkdocs_admonition_code_block() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::MkDocs);

        let content = "!!! note\n    Some text\n\n    ```python\n    def hello():\n        pass\n    ```\n";
        let blocks = processor.extract_code_blocks(content);

        assert_eq!(blocks.len(), 1, "Should detect code block inside MkDocs admonition");
        assert_eq!(blocks[0].language, "python");
    }

    #[test]
    fn test_extract_mkdocs_tab_code_block() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::MkDocs);

        let content = "=== \"Python\"\n\n    ```python\n    print(\"hello\")\n    ```\n";
        let blocks = processor.extract_code_blocks(content);

        assert_eq!(blocks.len(), 1, "Should detect code block inside MkDocs tab");
        assert_eq!(blocks[0].language, "python");
    }

    #[test]
    fn test_standard_flavor_ignores_admonition_indented_content() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // With standard flavor, pulldown_cmark parses this differently;
        // our MkDocs extraction should NOT run
        let content = "!!! note\n    Some text\n\n    ```python\n    def hello():\n        pass\n    ```\n";
        let blocks = processor.extract_code_blocks(content);

        // Standard flavor relies on pulldown_cmark only, which may or may not detect
        // indented fenced blocks. The key assertion is that we don't double-detect.
        // With standard flavor, the MkDocs extraction path is skipped entirely.
        for (i, b) in blocks.iter().enumerate() {
            for (j, b2) in blocks.iter().enumerate() {
                if i != j {
                    assert_ne!(b.start_line, b2.start_line, "No duplicate blocks should exist");
                }
            }
        }
    }

    #[test]
    fn test_mkdocs_top_level_blocks_alongside_admonition() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::MkDocs);

        let content =
            "```rust\nfn main() {}\n```\n\n!!! note\n    Some text\n\n    ```python\n    print(\"hello\")\n    ```\n";
        let blocks = processor.extract_code_blocks(content);

        assert_eq!(
            blocks.len(),
            2,
            "Should detect both top-level and admonition code blocks"
        );
        assert_eq!(blocks[0].language, "rust");
        assert_eq!(blocks[1].language, "python");
    }

    #[test]
    fn test_mkdocs_nested_admonition_code_block() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::MkDocs);

        let content = "\
!!! note
    Some text

    !!! warning
        Nested content

        ```python
        x = 1
        ```
";
        let blocks = processor.extract_code_blocks(content);
        assert_eq!(blocks.len(), 1, "Should detect code block inside nested admonition");
        assert_eq!(blocks[0].language, "python");
    }

    #[test]
    fn test_mkdocs_consecutive_admonitions_no_stale_context() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::MkDocs);

        // Two consecutive admonitions at the same indent level.
        // The first has no code block, the second does.
        let content = "\
!!! note
    First admonition content

!!! warning
    Second admonition content

    ```python
    y = 2
    ```
";
        let blocks = processor.extract_code_blocks(content);
        assert_eq!(blocks.len(), 1, "Should detect code block in second admonition only");
        assert_eq!(blocks[0].language, "python");
    }

    /// An admonition inside a fenced block is code, not an admonition, and a
    /// fenced block still ends the admonition it is not indented into.
    #[test]
    fn test_mkdocs_fenced_block_lines_are_code() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::MkDocs);

        // (language, first line, last line) of an extracted block
        type Span<'a> = (&'a str, usize, usize);
        let cases: &[(&str, &[Span<'_>])] = &[
            (
                "```markdown\n!!! note\n\n    ```json\n    {}\n    ```\n```\n",
                &[("markdown", 0, 6)],
            ),
            (
                "> ```md\n> === \"Tab\"\n>\n>     ```py\n>     x\n>     ```\n> ```\n",
                &[("md", 0, 6)],
            ),
            (
                "!!! note\n\n    text\n\n```text\nx\n```\n\n    ```json\n    {}\n    ```\n",
                &[("text", 4, 6)],
            ),
            (
                "```markdown\n!!! note\n```\n\n!!! tip\n\n    ```json\n    {}\n    ```\n",
                &[("markdown", 0, 2), ("json", 6, 8)],
            ),
            (
                "> ```up\n> a\n!!! note\n\n    ```up\n    b\n    ```\n",
                &[("up", 0, 1), ("up", 4, 6)],
            ),
        ];
        for &(content, expected) in cases {
            let extracted = processor.extract_code_blocks(content);
            let blocks: Vec<Span<'_>> = extracted
                .iter()
                .map(|block| (block.language.as_str(), block.start_line, block.end_line))
                .collect();
            assert_eq!(blocks, expected, "{content:?}");
        }
    }

    #[test]
    fn test_mkdocs_crlf_line_endings() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::MkDocs);

        // Use \r\n line endings
        let content = "!!! note\r\n    Some text\r\n\r\n    ```python\r\n    x = 1\r\n    ```\r\n";
        let blocks = processor.extract_code_blocks(content);

        assert_eq!(blocks.len(), 1, "Should detect code block with CRLF line endings");
        assert_eq!(blocks[0].language, "python");

        // Verify byte offsets point to valid content
        let extracted = &content[blocks[0].content_start..blocks[0].content_end];
        assert!(
            extracted.contains("x = 1"),
            "Extracted content should contain code. Got: {extracted:?}"
        );
    }

    #[test]
    fn test_mkdocs_unclosed_fence_in_admonition() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::MkDocs);

        // Unclosed fence should not produce a block
        let content = "!!! note\n    ```python\n    x = 1\n    no closing fence\n";
        let blocks = processor.extract_code_blocks(content);
        assert_eq!(blocks.len(), 0, "Unclosed fence should not produce a block");
    }

    #[test]
    fn test_mkdocs_tilde_fence_in_admonition() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::MkDocs);

        let content = "!!! note\n    ~~~ruby\n    puts 'hi'\n    ~~~\n";
        let blocks = processor.extract_code_blocks(content);
        assert_eq!(blocks.len(), 1, "Should detect tilde-fenced code block");
        assert_eq!(blocks[0].language, "ruby");
    }

    #[test]
    fn test_mkdocs_empty_lines_in_code_block() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::MkDocs);

        // Code block with empty lines inside — verifies byte offsets are correct
        // across empty lines (the previous find("") approach would break here)
        let content = "!!! note\n    ```python\n    x = 1\n\n    y = 2\n    ```\n";
        let blocks = processor.extract_code_blocks(content);
        assert_eq!(blocks.len(), 1);

        let extracted = &content[blocks[0].content_start..blocks[0].content_end];
        assert!(
            extracted.contains("x = 1") && extracted.contains("y = 2"),
            "Extracted content should span across the empty line. Got: {extracted:?}"
        );
    }

    #[test]
    fn test_mkdocs_content_byte_offsets_lf() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::MkDocs);

        let content = "!!! note\n    ```python\n    print('hi')\n    ```\n";
        let blocks = processor.extract_code_blocks(content);
        assert_eq!(blocks.len(), 1);

        // Verify the extracted content is exactly the code body
        let extracted = &content[blocks[0].content_start..blocks[0].content_end];
        assert_eq!(extracted, "    print('hi')\n", "Content offsets should be exact for LF");
    }

    #[test]
    fn test_mkdocs_content_byte_offsets_crlf() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::MkDocs);

        let content = "!!! note\r\n    ```python\r\n    print('hi')\r\n    ```\r\n";
        let blocks = processor.extract_code_blocks(content);
        assert_eq!(blocks.len(), 1);

        let extracted = &content[blocks[0].content_start..blocks[0].content_end];
        assert_eq!(
            extracted, "    print('hi')\r\n",
            "Content offsets should be exact for CRLF"
        );
    }

    #[test]
    fn test_lint_enabled_false_skips_language_in_strict_mode() {
        // With on-missing-language-definition = "fail", a language configured
        // with enabled=false should be silently skipped (no error).
        let mut config = default_config();
        config.normalize_language = NormalizeLanguage::Exact;
        config.on_missing_language_definition = OnMissing::Fail;

        // Python has tools, plaintext is disabled
        config.languages.insert(
            "python".to_string(),
            LanguageToolConfig {
                lint: vec!["ruff:check".to_string()],
                ..Default::default()
            },
        );
        config.languages.insert(
            "plaintext".to_string(),
            LanguageToolConfig {
                enabled: false,
                ..Default::default()
            },
        );

        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```plaintext\nsome text\n```";
        let result = processor.lint(content);

        // No error for plaintext: enabled=false satisfies strict mode
        assert!(result.is_ok());
        let diagnostics = result.unwrap();
        assert!(
            diagnostics.is_empty(),
            "Expected no diagnostics for disabled language, got: {diagnostics:?}"
        );
    }

    #[test]
    fn test_format_enabled_false_skips_language_in_strict_mode() {
        // Same test but for format mode
        let mut config = default_config();
        config.normalize_language = NormalizeLanguage::Exact;
        config.on_missing_language_definition = OnMissing::Fail;

        config.languages.insert(
            "plaintext".to_string(),
            LanguageToolConfig {
                enabled: false,
                ..Default::default()
            },
        );

        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```plaintext\nsome text\n```";
        let result = processor.format(content);

        // No error for plaintext: enabled=false satisfies strict mode
        assert!(result.is_ok());
        let output = result.unwrap();
        assert!(!output.had_errors, "Expected no errors for disabled language");
        assert!(
            output.error_messages.is_empty(),
            "Expected no error messages, got: {:?}",
            output.error_messages
        );
    }

    #[test]
    fn test_enabled_false_default_true_preserved() {
        // Verify that when enabled is not set, it defaults to true (existing behavior)
        let mut config = default_config();
        config.on_missing_language_definition = OnMissing::Fail;

        // Configure python without explicitly setting enabled
        config.languages.insert(
            "python".to_string(),
            LanguageToolConfig {
                lint: vec!["ruff:check".to_string()],
                ..Default::default()
            },
        );

        let lang_config = config.languages.get("python").unwrap();
        assert!(lang_config.enabled, "enabled should default to true");
    }

    #[test]
    fn test_enabled_false_with_fail_fast_no_error() {
        // Even with fail-fast, enabled=false should skip silently
        let mut config = default_config();
        config.normalize_language = NormalizeLanguage::Exact;
        config.on_missing_language_definition = OnMissing::FailFast;

        config.languages.insert(
            "unknown".to_string(),
            LanguageToolConfig {
                enabled: false,
                ..Default::default()
            },
        );

        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```unknown\nsome content\n```";
        let result = processor.lint(content);

        // Should not return an error: enabled=false takes precedence over fail-fast
        assert!(result.is_ok(), "Expected Ok but got Err: {result:?}");
        assert!(result.unwrap().is_empty());
    }

    #[test]
    fn test_enabled_false_format_with_fail_fast_no_error() {
        // Same for format mode
        let mut config = default_config();
        config.normalize_language = NormalizeLanguage::Exact;
        config.on_missing_language_definition = OnMissing::FailFast;

        config.languages.insert(
            "unknown".to_string(),
            LanguageToolConfig {
                enabled: false,
                ..Default::default()
            },
        );

        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```unknown\nsome content\n```";
        let result = processor.format(content);

        assert!(result.is_ok(), "Expected Ok but got Err: {result:?}");
        let output = result.unwrap();
        assert!(!output.had_errors);
    }

    #[test]
    fn test_enabled_false_with_tools_still_skips() {
        // If enabled=false but tools are listed, the language should still be skipped
        let mut config = default_config();
        config.on_missing_language_definition = OnMissing::Fail;

        config.languages.insert(
            "python".to_string(),
            LanguageToolConfig {
                enabled: false,
                lint: vec!["ruff:check".to_string()],
                format: vec!["ruff:format".to_string()],
                ..Default::default()
            },
        );

        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```python\nprint('hello')\n```";

        // Lint should skip
        let lint_result = processor.lint(content);
        assert!(lint_result.is_ok());
        assert!(lint_result.unwrap().is_empty());

        // Format should skip
        let format_result = processor.format(content);
        assert!(format_result.is_ok());
        let output = format_result.unwrap();
        assert!(!output.had_errors);
        assert_eq!(output.content, content, "Content should be unchanged");
    }

    #[test]
    fn test_enabled_true_without_tools_triggers_strict_mode() {
        // A language configured with enabled=true (default) but no tools
        // should still trigger strict mode errors
        let mut config = default_config();
        config.on_missing_language_definition = OnMissing::Fail;

        config.languages.insert(
            "python".to_string(),
            LanguageToolConfig {
                // enabled defaults to true, no tools
                ..Default::default()
            },
        );

        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```python\nprint('hello')\n```";
        let result = processor.lint(content);

        // Should report an error because enabled=true but no lint tools configured
        assert!(result.is_ok());
        let diagnostics = result.unwrap();
        assert_eq!(diagnostics.len(), 1);
        assert!(diagnostics[0].message.contains("No lint tools configured"));
    }

    #[test]
    fn test_mixed_enabled_and_disabled_languages() {
        // Multiple languages: one disabled, one unconfigured
        let mut config = default_config();
        config.normalize_language = NormalizeLanguage::Exact;
        config.on_missing_language_definition = OnMissing::Fail;

        config.languages.insert(
            "plaintext".to_string(),
            LanguageToolConfig {
                enabled: false,
                ..Default::default()
            },
        );

        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "\
```plaintext
some text
```

```javascript
console.log('hi');
```
";

        let result = processor.lint(content);
        assert!(result.is_ok());
        let diagnostics = result.unwrap();

        // plaintext: skipped (enabled=false), no error
        // javascript: not configured at all, should trigger strict mode error
        assert_eq!(diagnostics.len(), 1, "Expected 1 diagnostic, got: {diagnostics:?}");
        assert!(
            diagnostics[0].message.contains("javascript"),
            "Error should be about javascript, got: {}",
            diagnostics[0].message
        );
    }

    #[test]
    fn test_generic_fallback_includes_all_stderr_lines() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // Use output that won't be parsed by any structured format parser
        let output = ToolOutput {
            stdout: String::new(),
            stderr: "Parse error at position 42\nUnexpected token '::'\n3 errors found".to_string(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tombi", 5);
        assert_eq!(diags.len(), 3, "Expected one diagnostic per non-empty stderr line");
        assert_eq!(diags[0].message, "Parse error at position 42");
        assert_eq!(diags[1].message, "Unexpected token '::'");
        assert_eq!(diags[2].message, "3 errors found");
        assert!(diags.iter().all(|d| d.tool == "tombi"));
        assert!(diags.iter().all(|d| d.file_line == 5));
    }

    #[test]
    fn test_generic_fallback_includes_all_stdout_lines_when_stderr_empty() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let output = ToolOutput {
            stdout: "Line 1 error\nLine 2 detail\nLine 3 summary".to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "some-tool", 10);
        assert_eq!(diags.len(), 3);
        assert_eq!(diags[0].message, "Line 1 error");
        assert_eq!(diags[1].message, "Line 2 detail");
        assert_eq!(diags[2].message, "Line 3 summary");
    }

    #[test]
    fn test_generic_fallback_skips_blank_lines() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let output = ToolOutput {
            stdout: String::new(),
            stderr: "error: bad input\n\n  \n\ndetail: see above\n".to_string(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tool", 1);
        assert_eq!(diags.len(), 2);
        assert_eq!(diags[0].message, "error: bad input");
        assert_eq!(diags[1].message, "detail: see above");
    }

    #[test]
    fn test_generic_fallback_exit_code_when_no_output() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let output = ToolOutput {
            stdout: String::new(),
            stderr: String::new(),
            exit_code: 42,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tool", 1);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].message, "Tool exited with code 42");
    }

    #[test]
    fn test_generic_fallback_not_triggered_on_success() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let output = ToolOutput {
            stdout: "some informational output".to_string(),
            stderr: String::new(),
            exit_code: 0,
            success: true,
        };

        let diags = processor.parse_tool_output(&output, "tool", 1);
        assert!(
            diags.is_empty(),
            "Successful tool runs should produce no fallback diagnostics"
        );
    }

    #[test]
    fn test_ansi_codes_stripped_before_parsing() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // ruff-style output with ANSI color codes wrapping the message
        let output = ToolOutput {
            stdout: "\x1b[1m_.py\x1b[0m:\x1b[33m1\x1b[0m:\x1b[33m1\x1b[0m: \x1b[31mE501\x1b[0m Line too long"
                .to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "ruff:check", 5);
        assert_eq!(diags.len(), 1, "ANSI-colored output should still be parsed");
        assert_eq!(diags[0].message, "E501 Line too long");
        assert_eq!(diags[0].file_line, 6); // 5 + 1
    }

    #[test]
    fn test_tombi_multiline_error_format() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // Simulates tombi output (ANSI codes stripped for clarity)
        let output = ToolOutput {
            stdout: "[test]\ntest: \"test\"\nError: invalid key\n    at line 2 column 1\nError: expected key\n    at line 2 column 1\nError: expected '='\n    at line 2 column 1\nError: expected value\n    at line 2 column 1".to_string(),
            stderr: "1 file failed to be formatted".to_string(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tombi", 7);
        assert_eq!(
            diags.len(),
            4,
            "Expected 4 diagnostics from tombi errors, got {diags:?}"
        );
        assert_eq!(diags[0].message, "invalid key");
        assert_eq!(diags[0].file_line, 9); // 7 + 2
        assert_eq!(diags[0].column, Some(1));
        assert_eq!(diags[1].message, "expected key");
        assert_eq!(diags[1].file_line, 9);
        assert_eq!(diags[2].message, "expected '='");
        assert_eq!(diags[3].message, "expected value");
        assert!(diags.iter().all(|d| d.tool == "tombi"));
    }

    #[test]
    fn test_tombi_with_ansi_codes() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // Real tombi output with ANSI escape codes
        let output = ToolOutput {
            stdout: "[test]\ntest: \"test\"\n\x1b[1;31m  Error\x1b[0m: \x1b[1minvalid key\x1b[0m\n    \x1b[90mat line 2 column 1\x1b[0m\n\x1b[1;31m  Error\x1b[0m: \x1b[1mexpected '='\x1b[0m\n    \x1b[90mat line 2 column 1\x1b[0m".to_string(),
            stderr: "1 file failed to be formatted".to_string(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tombi", 7);
        assert_eq!(
            diags.len(),
            2,
            "Expected 2 diagnostics from ANSI-colored tombi output, got {diags:?}"
        );
        assert_eq!(diags[0].message, "invalid key");
        assert_eq!(diags[0].file_line, 9);
        assert_eq!(diags[1].message, "expected '='");
        assert_eq!(diags[1].file_line, 9);
    }

    /// sqlfluff's `github-annotation-native` output, verbatim, for `SELECT   1  FROM   t`.
    ///
    /// The `::group::` / `::endgroup::` markers bracket every run and carry no finding, so
    /// a parser that treated any `::...::` line as a diagnostic would report five.
    const SQLFLUFF_ANNOTATIONS: &str = "\
::group::stdin
::warning title=SQLFluff,file=stdin,line=1,col=7,endLine=1,endColumn=10::LT01: Expected only single space before numeric literal. Found '   '. [layout.spacing]
::warning title=SQLFluff,file=stdin,line=1,col=11,endLine=1,endColumn=13::LT01: Expected only single space before 'FROM' keyword. Found '  '. [layout.spacing]
::warning title=SQLFluff,file=stdin,line=1,col=17,endLine=1,endColumn=20::LT01: Expected only single space before naked identifier. Found '   '. [layout.spacing]
::endgroup::";

    #[test]
    fn test_github_annotation_maps_line_and_column() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let output = ToolOutput {
            stdout: SQLFLUFF_ANNOTATIONS.to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "sqlfluff:lint", 3);
        assert_eq!(diags.len(), 3, "the group markers carry no finding: {diags:?}");
        assert_eq!(diags.iter().map(|d| d.file_line).collect::<Vec<_>>(), vec![4, 4, 4]);
        assert_eq!(
            diags.iter().map(|d| d.column).collect::<Vec<_>>(),
            vec![Some(7), Some(11), Some(17)]
        );
        assert!(diags[0].message.starts_with("LT01: Expected only single space"));
        assert!(diags.iter().all(|d| matches!(d.severity, DiagnosticSeverity::Warning)));
    }

    #[test]
    fn test_github_annotation_severity_per_level() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let output = ToolOutput {
            stdout: "::error line=1::broken\n::warning line=2::suspicious\n::notice line=3::detail".to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tool", 10);
        assert_eq!(diags.len(), 3);
        assert!(matches!(diags[0].severity, DiagnosticSeverity::Error));
        assert!(matches!(diags[1].severity, DiagnosticSeverity::Warning));
        assert!(matches!(diags[2].severity, DiagnosticSeverity::Info));
        assert_eq!(diags[2].file_line, 13);
    }

    #[test]
    fn test_github_annotation_without_line_anchors_at_fence() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let output = ToolOutput {
            stdout: "::error::something the tool could not place".to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tool", 12);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].file_line, 12);
        assert_eq!(diags[0].column, None);
        assert_eq!(diags[0].message, "something the tool could not place");
    }

    #[test]
    fn test_github_annotation_decodes_escaped_message() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // The format escapes `%`, `:`, `,` and line breaks, because each of them is a
        // separator in the line the message is embedded in.
        let output = ToolOutput {
            stdout: "::error line=1,col=2::100%25 of rows%3A a%2C b%0Aand more".to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tool", 5);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].message, "100% of rows: a, b and more");
        assert_eq!(diags[0].file_line, 6);
        assert_eq!(diags[0].column, Some(2));
    }

    #[test]
    fn test_decode_workflow_command_is_a_single_left_to_right_pass() {
        // `%250A` is an escaped `%` followed by the literal text "0A". Decoding `%25` first
        // and then looking for `%0A` in the result would turn it into a line break.
        assert_eq!(decode_workflow_command("%250A"), "%0A");
        assert_eq!(decode_workflow_command("no escapes"), "no escapes");
        assert_eq!(decode_workflow_command("50% off"), "50% off");
        assert_eq!(decode_workflow_command("trailing %"), "trailing %");
        assert_eq!(decode_workflow_command("%0d%0a"), "  ");
        assert_eq!(decode_workflow_command("%3A%2C"), ":,");
    }

    #[test]
    fn test_position_in_message_anchors_a_prose_position() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // jq names the position inside the message rather than prefixing it, and exits
        // non-zero, so this arrives through the raw-output fallback.
        let output = ToolOutput {
            stdout: String::new(),
            stderr: "jq: error (at <stdin>:0): syntax error, unexpected '}' at line 3, column 8".to_string(),
            exit_code: 2,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "jq", 20);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].file_line, 23);
        assert_eq!(diags[0].column, Some(8));
    }

    #[test]
    fn test_position_in_message_takes_the_last_one_named() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let output = ToolOutput {
            stdout: "unterminated string started at line 2, column 1 at line 6, column 4".to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tool", 0);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].file_line, 6, "the sentence ends on the position it is about");
        assert_eq!(diags[0].column, Some(4));
    }

    #[test]
    fn test_message_naming_no_position_stays_on_the_fence() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let output = ToolOutput {
            stdout: "could not parse the input".to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tool", 7);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].file_line, 7);
        assert_eq!(diags[0].column, None);
    }

    #[test]
    fn test_fallback_combines_stdout_and_stderr() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // Tool puts some errors on stdout, summary on stderr
        let output = ToolOutput {
            stdout: "problem found in input".to_string(),
            stderr: "1 file failed".to_string(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tool", 1);
        assert_eq!(diags.len(), 2, "Fallback should include both stdout and stderr");
        assert_eq!(diags[0].message, "problem found in input");
        assert_eq!(diags[1].message, "1 file failed");
    }

    #[test]
    fn test_error_line_without_position_info() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // Error: line not followed by "at line N column M"
        let output = ToolOutput {
            stdout: "Error: something went wrong\nsome unrelated line".to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tool", 5);
        // "Error: something went wrong" → parsed by error-line parser (no position)
        // "some unrelated line" → no parser matches, but diagnostics not empty → no fallback
        assert!(!diags.is_empty());
        assert_eq!(diags[0].message, "something went wrong");
        assert_eq!(diags[0].file_line, 5); // No line offset, uses code_block_start
    }

    #[test]
    fn test_warning_line_with_position() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let output = ToolOutput {
            stdout: "Warning: deprecated syntax\n    at line 3 column 5".to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tool", 10);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].message, "deprecated syntax");
        assert_eq!(diags[0].file_line, 13); // 10 + 3
        assert_eq!(diags[0].column, Some(5));
        assert!(matches!(diags[0].severity, DiagnosticSeverity::Warning));
    }

    #[test]
    fn test_strip_ansi_codes() {
        assert_eq!(strip_ansi_codes("hello"), "hello");
        assert_eq!(strip_ansi_codes("\x1b[31mred\x1b[0m"), "red");
        assert_eq!(
            strip_ansi_codes("\x1b[1;31m  Error\x1b[0m: \x1b[1mmsg\x1b[0m"),
            "  Error: msg"
        );
        assert_eq!(strip_ansi_codes("no codes here"), "no codes here");
        assert_eq!(strip_ansi_codes(""), "");
        assert_eq!(
            strip_ansi_codes("\x1b[90mat line 2 column 1\x1b[0m"),
            "at line 2 column 1"
        );
    }

    #[test]
    fn test_parse_at_line_column() {
        assert_eq!(
            CodeBlockToolProcessor::parse_at_line_column("at line 2 column 1"),
            Some((2, 1))
        );
        assert_eq!(
            CodeBlockToolProcessor::parse_at_line_column("at line 10 column 15"),
            Some((10, 15))
        );
        assert_eq!(
            CodeBlockToolProcessor::parse_at_line_column("At Line 5 Column 3"),
            Some((5, 3))
        );
        assert_eq!(
            CodeBlockToolProcessor::parse_at_line_column("not a position line"),
            None
        );
        assert_eq!(
            CodeBlockToolProcessor::parse_at_line_column("at line abc column 1"),
            None
        );
    }

    #[test]
    fn test_parse_error_line() {
        let (msg, sev) = CodeBlockToolProcessor::parse_error_line("Error: invalid key").unwrap();
        assert_eq!(msg, "invalid key");
        assert!(matches!(sev, DiagnosticSeverity::Error));

        let (msg, sev) = CodeBlockToolProcessor::parse_error_line("Warning: deprecated").unwrap();
        assert_eq!(msg, "deprecated");
        assert!(matches!(sev, DiagnosticSeverity::Warning));

        // Lowercase should NOT match (avoids conflict with unstructured tool output)
        assert!(CodeBlockToolProcessor::parse_error_line("error: bad input").is_none());
        assert!(CodeBlockToolProcessor::parse_error_line("warning: minor issue").is_none());

        // Empty message after prefix should not match
        assert!(CodeBlockToolProcessor::parse_error_line("Error:").is_none());
        assert!(CodeBlockToolProcessor::parse_error_line("Error:   ").is_none());

        // Not an error line
        assert!(CodeBlockToolProcessor::parse_error_line("some random text").is_none());
    }

    #[test]
    fn test_consecutive_error_lines_without_position() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // Two Error: lines in a row — first should flush without position,
        // second gets position from "at line"
        let output = ToolOutput {
            stdout: "Error: first problem\nError: second problem\n    at line 3 column 1".to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tool", 5);
        assert_eq!(diags.len(), 2, "Expected 2 diagnostics, got {diags:?}");
        // First error flushed without position when second Error: was encountered
        assert_eq!(diags[0].message, "first problem");
        assert_eq!(diags[0].file_line, 5); // No line mapping
        assert_eq!(diags[0].column, None);
        // Second error resolved with position
        assert_eq!(diags[1].message, "second problem");
        assert_eq!(diags[1].file_line, 8); // 5 + 3
        assert_eq!(diags[1].column, Some(1));
    }

    #[test]
    fn test_error_line_at_end_of_output() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // Error: as the very last line — flushed by post-loop code
        let output = ToolOutput {
            stdout: "Error: trailing error".to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tool", 5);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].message, "trailing error");
        assert_eq!(diags[0].file_line, 5); // No position info available
        assert_eq!(diags[0].column, None);
    }

    #[test]
    fn test_blank_lines_between_error_and_position() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // Blank lines between Error: and "at line" should be transparently skipped
        let output = ToolOutput {
            stdout: "Error: spaced out\n\n\n    at line 4 column 2".to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tool", 10);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].message, "spaced out");
        assert_eq!(diags[0].file_line, 14); // 10 + 4
        assert_eq!(diags[0].column, Some(2));
    }

    #[test]
    fn test_mixed_structured_and_error_line_parsers() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // Mix of ruff-style structured output and tombi-style Error: output
        let output = ToolOutput {
            stdout: "_.py:1:5: E501 Line too long\nError: invalid syntax\n    at line 3 column 1".to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tool", 5);
        assert_eq!(diags.len(), 2, "Expected 2 diagnostics, got {diags:?}");
        // First: standard format parser
        assert_eq!(diags[0].message, "E501 Line too long");
        assert_eq!(diags[0].file_line, 6); // 5 + 1
        // Second: Error: + at line parser
        assert_eq!(diags[1].message, "invalid syntax");
        assert_eq!(diags[1].file_line, 8); // 5 + 3
    }

    #[test]
    fn test_at_line_without_preceding_error() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // "at line N column M" without a preceding Error: should not create a diagnostic
        let output = ToolOutput {
            stdout: "at line 2 column 1\nsome other text".to_string(),
            stderr: String::new(),
            exit_code: 1,
            success: false,
        };

        let diags = processor.parse_tool_output(&output, "tool", 5);
        // No pending error, so "at line" is just an unmatched line
        // Both lines are unmatched, fallback fires with combined output
        assert_eq!(diags.len(), 2);
        assert_eq!(diags[0].message, "at line 2 column 1");
        assert_eq!(diags[1].message, "some other text");
    }

    // =========================================================================
    // Issue #527: formatter that produces empty output should not erase content
    // =========================================================================

    /// A formatter that produces no stdout (like `tombi lint -` mistakenly used
    /// as a formatter) must never replace non-empty content with an empty string.
    /// It is a formatter that failed, so `on-error` decides what is reported.
    /// This test uses `true`, which exits 0 with no output.
    #[test]
    fn test_format_empty_output_does_not_erase_content() {
        use super::super::config::LanguageToolConfig;

        let content = "```toml\nkey = \"value\"\n```\n";
        let config_for = |on_error: OnError| {
            let mut config = default_config();
            config.on_error = on_error;
            config.languages.insert(
                "toml".to_string(),
                LanguageToolConfig {
                    format: vec!["empty-formatter".to_string()],
                    ..Default::default()
                },
            );
            config.tools.insert(
                "empty-formatter".to_string(),
                super::super::config::ToolDefinition {
                    command: vec!["true".to_string()],
                    stdin: true,
                    stdout: true,
                    lint_args: vec![],
                    format_args: vec![],
                },
            );
            config
        };

        let config = config_for(OnError::Fail);
        let result = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default()).format(content);
        assert!(
            matches!(result, Err(ProcessorError::ToolErrorAt { line: 1, .. })),
            "fail must stop at the block: {result:?}"
        );

        let config = config_for(OnError::Warn);
        let output = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default())
            .format(content)
            .unwrap();
        assert_eq!(output.content, content, "warn must leave the block as it was");
        assert_eq!(output.error_messages.len(), 1, "{:?}", output.error_messages);
        assert!(output.error_messages[0].starts_with("line 1 (toml): "));

        let config = config_for(OnError::Skip);
        let output = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default())
            .format(content)
            .unwrap();
        assert_eq!(output.content, content, "skip must leave the block as it was");
        assert!(output.error_messages.is_empty(), "{:?}", output.error_messages);
    }

    /// A formatter that echoes input back (like `cat`) should preserve content.
    #[test]
    fn test_format_identity_formatter_preserves_content() {
        use super::super::config::LanguageToolConfig;

        let mut config = default_config();
        config.languages.insert(
            "toml".to_string(),
            LanguageToolConfig {
                format: vec!["cat-formatter".to_string()],
                ..Default::default()
            },
        );
        config.tools.insert(
            "cat-formatter".to_string(),
            super::super::config::ToolDefinition {
                command: vec!["cat".to_string()],
                stdin: true,
                stdout: true,
                lint_args: vec![],
                format_args: vec![],
            },
        );

        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```toml\nkey = \"value\"\n```\n";
        let result = processor.format(content);

        assert!(result.is_ok(), "Format should not error");
        let output = result.unwrap();
        assert_eq!(
            output.content, content,
            "Identity formatter should preserve content exactly"
        );
    }

    /// Verify that the context-aware tool resolution resolves bare "tombi"
    /// to "tombi:format" in format context and "tombi:lint" in lint context.
    #[test]
    fn test_resolve_tool_context_aware_tombi() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // In format context, bare "tombi" should resolve to "tombi:format"
        let format_def = processor
            .registry
            .resolve("tombi", ToolSlot::Format)
            .expect("Should resolve tombi in format context");
        assert!(
            format_def.command.iter().any(|arg| arg == "format"),
            "Bare 'tombi' in format context should resolve to 'tombi format', got: {:?}",
            format_def.command
        );

        // In lint context, bare "tombi" should resolve to "tombi:lint"
        let lint_def = processor
            .registry
            .resolve("tombi", ToolSlot::Lint)
            .expect("Should resolve tombi in lint context");
        assert!(
            lint_def.command.iter().any(|arg| arg == "lint"),
            "Bare 'tombi' in lint context should resolve to 'tombi lint', got: {:?}",
            lint_def.command
        );

        // Explicit suffix should bypass context-aware resolution
        let explicit_def = processor
            .registry
            .resolve("tombi:lint", ToolSlot::Format)
            .expect("Should resolve explicit tombi:lint even in format context");
        assert!(
            explicit_def.command.iter().any(|arg| arg == "lint"),
            "Explicit 'tombi:lint' should always use lint, got: {:?}",
            explicit_def.command
        );
    }

    /// Verify context-aware resolution for ruff (uses "check" suffix, not "lint").
    #[test]
    fn test_resolve_tool_context_aware_ruff() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // In lint context, bare "ruff" should resolve to "ruff:check"
        let lint_def = processor
            .registry
            .resolve("ruff", ToolSlot::Lint)
            .expect("Should resolve ruff in lint context");
        assert!(
            lint_def.command.iter().any(|arg| arg == "check"),
            "Bare 'ruff' in lint context should resolve to 'ruff check', got: {:?}",
            lint_def.command
        );

        // In format context, bare "ruff" should resolve to "ruff:format"
        let format_def = processor
            .registry
            .resolve("ruff", ToolSlot::Format)
            .expect("Should resolve ruff in format context");
        assert!(
            format_def.command.iter().any(|arg| arg == "format"),
            "Bare 'ruff' in format context should resolve to 'ruff format', got: {:?}",
            format_def.command
        );
    }

    /// Tools without context-specific variants should still resolve via bare name.
    #[test]
    fn test_resolve_tool_bare_name_fallback() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // "shellcheck" has no :lint or :format variant — should fall back to bare name
        let def = processor
            .registry
            .resolve("shellcheck", ToolSlot::Lint)
            .expect("Should resolve shellcheck via fallback");
        assert!(
            def.command.iter().any(|arg| arg == "shellcheck"),
            "shellcheck should resolve to itself, got: {:?}",
            def.command
        );
    }

    /// Context-aware resolution for tools with non-standard format suffixes.
    #[test]
    fn test_resolve_tool_context_aware_sqlfluff() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // sqlfluff uses ":fix" as its format variant
        let format_def = processor
            .registry
            .resolve("sqlfluff", ToolSlot::Format)
            .expect("Should resolve sqlfluff in format context");
        assert!(
            format_def.command.iter().any(|arg| arg == "fix"),
            "Bare 'sqlfluff' in format context should resolve to 'sqlfluff fix', got: {:?}",
            format_def.command
        );
    }

    /// Context-aware resolution for djlint (:reformat suffix).
    #[test]
    fn test_resolve_tool_context_aware_djlint() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // djlint uses ":reformat" as its format variant
        let format_def = processor
            .registry
            .resolve("djlint", ToolSlot::Format)
            .expect("Should resolve djlint in format context");
        assert!(
            format_def.command.iter().any(|arg| arg.contains("reformat")),
            "Bare 'djlint' in format context should resolve to djlint reformat, got: {:?}",
            format_def.command
        );
    }

    /// User-defined tools with context-specific variants resolve correctly.
    #[test]
    fn test_resolve_tool_user_defined_with_context_variant() {
        use super::super::config::ToolDefinition;

        let mut config = default_config();
        config.tools.insert(
            "mytool".to_string(),
            ToolDefinition {
                command: vec!["mytool".to_string(), "--lint".to_string()],
                ..Default::default()
            },
        );
        config.tools.insert(
            "mytool:format".to_string(),
            ToolDefinition {
                command: vec!["mytool".to_string(), "--format".to_string()],
                ..Default::default()
            },
        );

        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        // In format context, should resolve to "mytool:format"
        let def = processor
            .registry
            .resolve("mytool", ToolSlot::Format)
            .expect("Should resolve user tool in format context");
        assert!(
            def.command.iter().any(|arg| arg == "--format"),
            "User 'mytool' in format context should resolve to mytool:format, got: {:?}",
            def.command
        );

        // In lint context, should fall back to bare "mytool" (no mytool:lint exists)
        let def = processor
            .registry
            .resolve("mytool", ToolSlot::Lint)
            .expect("Should resolve user tool in lint context via fallback");
        assert!(
            def.command.iter().any(|arg| arg == "--lint"),
            "User 'mytool' in lint context should fall back to bare name, got: {:?}",
            def.command
        );
    }

    /// Nonexistent tool returns None.
    #[test]
    fn test_resolve_tool_nonexistent_returns_none() {
        let config = default_config();
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        assert!(
            processor
                .registry
                .resolve("nonexistent-tool-xyz", ToolSlot::Lint)
                .is_none(),
            "Nonexistent tool should return None in lint context"
        );
        assert!(
            processor
                .registry
                .resolve("nonexistent-tool-xyz", ToolSlot::Format)
                .is_none(),
            "Nonexistent tool should return None in format context"
        );
    }

    #[test]
    fn test_strip_ansi_codes_edge_cases() {
        // Lone ESC without CSI bracket — non-printable, safely dropped
        assert_eq!(strip_ansi_codes("before\x1bafter"), "beforeafter");
        // ESC at end of string
        assert_eq!(strip_ansi_codes("trailing\x1b"), "trailing");
        // Nested/consecutive sequences
        assert_eq!(strip_ansi_codes("\x1b[1m\x1b[31mbold red\x1b[0m"), "bold red");
        // 256-color and RGB sequences
        assert_eq!(strip_ansi_codes("\x1b[38;5;196mred\x1b[0m"), "red");
        assert_eq!(strip_ansi_codes("\x1b[38;2;255;0;0mred\x1b[0m"), "red");
    }

    #[test]
    fn merge_conflict_prevents_external_formatting() {
        use super::super::config::{LanguageToolConfig, ToolDefinition};
        let mut config = default_config();
        config.on_missing_tool_binary = OnMissing::Fail;
        config.languages.insert(
            "testlang".to_string(),
            LanguageToolConfig {
                format: vec!["unavailable".to_string()],
                ..Default::default()
            },
        );
        config.tools.insert(
            "unavailable".to_string(),
            ToolDefinition {
                command: vec!["rumdl-858-tool-that-must-not-run".to_string()],
                ..Default::default()
            },
        );
        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());
        let content = "<<<<<<< HEAD\r\n\n```testlang\ntext\n```\n";
        let output = processor.format(content).unwrap();
        assert_eq!(output.content, content);
        assert!(!output.had_errors);
        assert!(output.failures.is_empty());

        // Disabling the safety rule must let the formatter run, rather than
        // silently skipping every code block because of a documented marker.
        let mut document_config = crate::config::Config::default();
        document_config.global.disable.push("MD092".into());
        let output = processor.format_with_config(content, &document_config, None).unwrap();
        assert!(output.had_errors);
        assert!(!output.failures.is_empty());
        let suppressed = format!("<!-- rumdl-disable MD092 -->\n{content}");
        assert!(processor.format(&suppressed).unwrap().had_errors);
    }

    /// A linter that enforces a trailing newline (like ryl/yamllint
    /// `new-line-at-end-of-file`) must not fire on a fenced code block whose
    /// final newline was stripped during extraction. The content piped to the
    /// tool should be newline-terminated, matching how the tool sees a real file.
    #[cfg(unix)]
    #[test]
    fn test_lint_yaml_block_no_false_new_line_at_eof() {
        use super::super::config::{LanguageToolConfig, ToolDefinition};

        let mut config = default_config();
        config.normalize_language = NormalizeLanguage::Exact;
        config.languages.insert(
            "yaml".to_string(),
            LanguageToolConfig {
                lint: vec!["eof-newline-linter".to_string()],
                ..Default::default()
            },
        );
        // Emits a diagnostic only when stdin's last byte is not a newline.
        // `$(tail -c1)` strips trailing newlines, so it is empty iff the input
        // already ends in a newline.
        config.tools.insert(
            "eof-newline-linter".to_string(),
            ToolDefinition {
                command: vec![
                    "sh".to_string(),
                    "-c".to_string(),
                    "if [ -n \"$(tail -c1)\" ]; then echo '1:1: no newline at end of file'; fi".to_string(),
                ],
                ..Default::default()
            },
        );

        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```yaml\nfoo: bar\n```\n";
        let diagnostics = processor.lint(content).expect("lint should succeed");

        assert!(
            diagnostics.is_empty(),
            "EOF-newline linter should not fire on an extracted code block, got: {diagnostics:?}"
        );
    }

    /// Format mode should also feed tools newline-terminated input, so a
    /// formatter that enforces an end-of-file newline sees the content as a
    /// complete file rather than re-adding a newline rumdl would then have to
    /// strip. The formatter here reports whether its stdin ended in a newline.
    #[cfg(unix)]
    #[test]
    fn test_format_feeds_newline_terminated_input() {
        use super::super::config::{LanguageToolConfig, ToolDefinition};

        let mut config = default_config();
        config.normalize_language = NormalizeLanguage::Exact;
        config.languages.insert(
            "yaml".to_string(),
            LanguageToolConfig {
                format: vec!["newline-probe".to_string()],
                ..Default::default()
            },
        );
        config.tools.insert(
            "newline-probe".to_string(),
            ToolDefinition {
                command: vec![
                    "sh".to_string(),
                    "-c".to_string(),
                    "if [ -z \"$(tail -c1)\" ]; then echo HAD_NEWLINE; else echo NO_NEWLINE; fi".to_string(),
                ],
                ..Default::default()
            },
        );

        let processor = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default());

        let content = "```yaml\nfoo: bar\n```\n";
        let output = processor.format(content).expect("format should succeed");

        assert!(
            output.content.contains("HAD_NEWLINE"),
            "Formatter should receive newline-terminated stdin, got: {:?}",
            output.content
        );
    }

    /// A config whose `text` blocks are formatted by `tools` in `mode`. The
    /// stages are small shell filters, so each one leaves a visible mark and a
    /// result names exactly which stages ran and in what order.
    #[cfg(unix)]
    fn staged_config(tools: &[&str], mode: FormatMode, on_error: OnError) -> CodeBlockToolsConfig {
        use super::super::config::ToolDefinition;

        let mut config = default_config();
        config.normalize_language = NormalizeLanguage::Exact;
        config.on_error = on_error;
        config.languages.insert(
            "text".to_string(),
            LanguageToolConfig {
                format: tools.iter().map(ToString::to_string).collect(),
                format_mode: mode,
                ..Default::default()
            },
        );
        for (id, script) in [
            ("upper", "tr a-z A-Z"),
            ("exclaim", "sed 's/$/!/'"),
            ("prefix-a", "sed 's/^/a/'"),
            ("prefix-b", "sed 's/^/b/'"),
            ("identity", "cat"),
            ("fails", "cat >/dev/null; exit 3"),
            ("empty", "cat >/dev/null"),
        ] {
            config.tools.insert(
                id.to_string(),
                ToolDefinition {
                    command: vec!["sh".to_string(), "-c".to_string(), script.to_string()],
                    ..Default::default()
                },
            );
        }
        config
    }

    #[cfg(unix)]
    fn format_staged(tools: &[&str], mode: FormatMode, on_error: OnError) -> Result<FormatOutput, ProcessorError> {
        let config = staged_config(tools, mode, on_error);
        CodeBlockToolProcessor::new(&config, MarkdownFlavor::default()).format("```text\nhello\n```\n")
    }

    #[cfg(unix)]
    fn staged_block(tools: &[&str], mode: FormatMode, on_error: OnError) -> String {
        let output = format_staged(tools, mode, on_error).expect("format runs");
        output
            .content
            .strip_prefix("```text\n")
            .and_then(|rest| rest.strip_suffix("```\n"))
            .unwrap_or_else(|| panic!("fence kept: {:?}", output.content))
            .to_string()
    }

    /// The first formatter that succeeds supplies the block, even when it
    /// changes nothing, and the default is that fallback list.
    #[cfg(unix)]
    #[test]
    fn a_fallback_format_list_stops_at_the_first_success() {
        assert_eq!(LanguageToolConfig::default().format_mode, FormatMode::Fallback);
        for (tools, expected) in [
            (&["upper", "exclaim"][..], "HELLO\n"),
            (&["identity", "upper"][..], "hello\n"),
            (&["fails", "upper", "exclaim"][..], "HELLO\n"),
            (&["empty", "upper", "exclaim"][..], "HELLO\n"),
        ] {
            assert_eq!(
                staged_block(tools, FormatMode::Fallback, OnError::Skip),
                expected,
                "{tools:?}"
            );
        }
    }

    /// Every formatter runs in declaration order on the previous one's output,
    /// and a formatter that changes nothing does not end the pipeline.
    #[cfg(unix)]
    #[test]
    fn a_pipeline_runs_every_formatter_on_the_last_output() {
        for (tools, expected) in [
            (&["upper", "exclaim"][..], "HELLO!\n"),
            (&["identity", "upper"][..], "HELLO\n"),
            (&["prefix-a", "prefix-b"][..], "bahello\n"),
            (&["prefix-b", "prefix-a"][..], "abhello\n"),
            (&["identity"][..], "hello\n"),
        ] {
            assert_eq!(
                staged_block(tools, FormatMode::Pipeline, OnError::Fail),
                expected,
                "{tools:?}"
            );
        }
    }

    /// A stage that fails, or prints nothing, is dropped under `warn` and
    /// `skip`: the next stage starts from the last output that succeeded, never
    /// from the failed stage's output. Only `warn` reports it.
    #[cfg(unix)]
    #[test]
    fn a_failed_pipeline_stage_is_dropped_under_warn_and_skip() {
        for failing in ["fails", "empty"] {
            let tools = ["upper", failing, "exclaim"];
            let warned = format_staged(&tools, FormatMode::Pipeline, OnError::Warn).unwrap();
            assert_eq!(warned.content, "```text\nHELLO!\n```\n", "{tools:?}");
            assert_eq!(warned.error_messages.len(), 1, "{:?}", warned.error_messages);
            assert!(warned.error_messages[0].starts_with("line 1 (text): "));

            let skipped = format_staged(&tools, FormatMode::Pipeline, OnError::Skip).unwrap();
            assert_eq!(skipped.content, "```text\nHELLO!\n```\n", "{tools:?}");
            assert!(skipped.error_messages.is_empty(), "{:?}", skipped.error_messages);
        }
        // With no stage left that succeeds, the block stays as written.
        assert_eq!(
            staged_block(&["fails", "empty"], FormatMode::Pipeline, OnError::Skip),
            "hello\n"
        );
    }

    /// Under `fail`, a stage that fails stops the run at its block, after an
    /// earlier stage succeeded just as before any did.
    #[cfg(unix)]
    #[test]
    fn a_failed_pipeline_stage_stops_the_run_under_fail() {
        for tools in [&["upper", "fails", "exclaim"][..], &["upper", "empty"][..]] {
            let result = format_staged(tools, FormatMode::Pipeline, OnError::Fail);
            assert!(
                matches!(result, Err(ProcessorError::ToolErrorAt { line: 1, .. })),
                "{tools:?}: {result:?}"
            );
        }
    }

    /// A stage whose binary is missing follows `on-missing-tool-binary`, and
    /// the pipeline carries on past it.
    #[cfg(unix)]
    #[test]
    fn a_pipeline_stage_with_a_missing_binary_follows_on_missing_tool_binary() {
        use super::super::config::ToolDefinition;

        let mut config = staged_config(&["upper", "gone", "exclaim"], FormatMode::Pipeline, OnError::Fail);
        config.tools.insert(
            "gone".to_string(),
            ToolDefinition {
                command: vec!["rumdl-test-nonexistent-formatter".to_string()],
                ..Default::default()
            },
        );
        let content = "```text\nhello\n```\n";

        config.on_missing_tool_binary = OnMissing::Ignore;
        let output = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default())
            .format(content)
            .unwrap();
        assert_eq!(output.content, "```text\nHELLO!\n```\n");

        config.on_missing_tool_binary = OnMissing::Fail;
        let output = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default())
            .format(content)
            .unwrap();
        assert_eq!(output.content, "```text\nHELLO!\n```\n");
        assert_eq!(output.failures.len(), 1, "{:?}", output.failures);

        config.on_missing_tool_binary = OnMissing::FailFast;
        let result = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default()).format(content);
        assert!(
            matches!(result, Err(ProcessorError::ToolBinaryNotFound { .. })),
            "{result:?}"
        );
    }

    /// Each stage gets newline-terminated input, and the final block keeps the
    /// original's line ending however the stages end theirs.
    #[cfg(unix)]
    #[test]
    fn a_pipeline_keeps_the_block_line_ending_between_stages() {
        use super::super::config::ToolDefinition;

        let mut config = staged_config(&["strip-newline", "probe"], FormatMode::Pipeline, OnError::Fail);
        for (id, script) in [
            ("strip-newline", "printf '%s' \"$(cat)\""),
            (
                "probe",
                "if [ -z \"$(tail -c1)\" ]; then echo HAD_NEWLINE; else echo NO_NEWLINE; fi",
            ),
        ] {
            config.tools.insert(
                id.to_string(),
                ToolDefinition {
                    command: vec!["sh".to_string(), "-c".to_string(), script.to_string()],
                    ..Default::default()
                },
            );
        }
        let output = CodeBlockToolProcessor::new(&config, MarkdownFlavor::default())
            .format("```text\nhello\n```\n")
            .unwrap();
        assert_eq!(output.content, "```text\nHAD_NEWLINE\n```\n");
    }

    #[test]
    fn test_ensure_trailing_newline() {
        // Non-empty content without a trailing newline gets one appended.
        assert_eq!(ensure_trailing_newline("foo: bar"), "foo: bar\n");
        // Multi-line content gets a single newline appended to the last line.
        assert_eq!(ensure_trailing_newline("a: 1\nb: 2"), "a: 1\nb: 2\n");
        // Content already ending in a newline is returned unchanged.
        assert_eq!(ensure_trailing_newline("foo: bar\n"), "foo: bar\n");
        // A trailing blank line (already newline-terminated) is left alone.
        assert_eq!(ensure_trailing_newline("foo: bar\n\n"), "foo: bar\n\n");
        // Genuinely empty content is left empty — no newline synthesized.
        assert_eq!(ensure_trailing_newline(""), "");
    }
}