prettier_ada_25.0.0_a67a3337/src/prettier_ada-documents-implementation.adb

   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
--
--  Copyright (C) 2023-2024, AdaCore
--  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--

with Ada.Assertions;
with Ada.Containers.Hashed_Maps;
with Ada.Containers.Hashed_Sets;
with Ada.Containers.Vectors;

with Prettier_Ada.Documents.Builders;
with Prettier_Ada.Optionals;

with VSS.Characters.Latin;
with VSS.Strings.Character_Iterators;
with VSS.Strings.Conversions;
with VSS.Strings.Markers;
with VSS.Strings.Utilities;

package body Prettier_Ada.Documents.Implementation is

   use type VSS.Strings.Display_Cell_Count;

   Current_Symbol : Symbol_Type := No_Symbol + 1;

   Document_Id : Natural := Natural'First;

   package Optional_Booleans is new
     Prettier_Ada.Optionals.Generic_Optional_Types (Boolean);

   subtype Optional_Boolean is Optional_Booleans.Generic_Optional_Type;

   package Document_Hashed_Sets is new
     Ada.Containers.Hashed_Sets
       (Element_Type        => Document_Type,
        Hash                => Hash,
        Equivalent_Elements => "=");

   subtype Document_Hashed_Set is Document_Hashed_Sets.Set;

   type Mode_Kind is (Mode_Break, Mode_Flat, None);

   type Indentation_Data_Kind is
     (Indent, String_Align, Number_Align, Dedent, Inner_Root);

   type Indentation_Data_Type (Kind : Indentation_Data_Kind := Indent) is
      record
         case Kind is
            when Indent | Dedent =>
               null;
            when Inner_Root =>
               Margin : Natural;
            when String_Align =>
               Text   : Prettier_String;
            when Number_Align =>
               Width  : Natural;
         end case;
      end record;

   package Indentation_Data_Vectors is new
     Ada.Containers.Vectors (Natural, Indentation_Data_Type);

   subtype Indentation_Data_Vector is Indentation_Data_Vectors.Vector;

   type Indentation_Queue_Type;

   type Indentation_Head_Type_Access is access Indentation_Queue_Type;

   type Indentation_Queue_Type is record
      Value : Prettier_String;
      Queue : Indentation_Data_Vector;
      Root  : Indentation_Head_Type_Access;
   end record;

   type Print_Command_Type is record
      Indentation : Indentation_Queue_Type;
      Mode        : Mode_Kind;
      Document    : Document_Type;
   end record;

   package Print_Command_Type_Vectors is new
     Ada.Containers.Vectors (Positive, Print_Command_Type);

   subtype Print_Command_Type_Vector is Print_Command_Type_Vectors.Vector;

   package Symbol_To_Mode_Maps is new
     Ada.Containers.Hashed_Maps (Symbol_Type, Mode_Kind, Hash, "=");

   subtype Symbol_To_Mode_Map is Symbol_To_Mode_Maps.Map;

   type Format_State_Record is record
      Current_Line_Length  : Natural;
      Group_Mode_Map       : Symbol_To_Mode_Map;
      Line_Suffix          : Print_Command_Type_Vector;
      Print_Commands       : Print_Command_Type_Vector;
      Printed_Cursor_Count : Natural;
      Result               : Prettier_String;
      Should_Remeasure     : Boolean;
      Last_Was_Hardline    : Boolean;
      --  Flag indicating if the last text added to Result was a hardline
   end record;

   Default_Format_State : constant Format_State_Record :=
     (Current_Line_Length  => 0,
      Group_Mode_Map       => Symbol_To_Mode_Maps.Empty_Map,
      Line_Suffix          => [],
      Print_Commands       => [],
      Printed_Cursor_Count => 0,
      Result               => Empty_Prettier_String,
      Should_Remeasure     => False,
      Last_Was_Hardline    => False);

   procedure Append (To : in out Prettier_String; Source : Prettier_String);
   --  Append Source to the end of To

   procedure Break_Parent_Group
     (Group_Stack : in out Document_Vector);
   --  TODO: Description

   procedure Break_Parent_Table
     (Table_Stack : in out Document_Vector);
   --  Sets Alignment_Table_Must_Break and Break_Parents to True for the last
   --  element of Table_Stack.

   function Fits
     (Next            : Print_Command_Type;
      Rest_Commands   : Print_Command_Type_Vector;
      Width           : Integer;
      Has_Line_Suffix : Boolean;
      Group_Mode_Map  : Symbol_To_Mode_Map;
      Must_Be_Flat    : Boolean := False)
      return Boolean;
   --  TODO: Description

   procedure Format
     (Format_State   : in out Format_State_Record;
      Format_Options : Format_Options_Type);
   --  Resume the formatting operations on Format_State.
   --  Formats according to Format_Options.

   function Generate_Indentation
     (From    : Indentation_Queue_Type;
      Data    : Indentation_Data_Type;
      Options : Indentation_Options_Type)
      return Indentation_Queue_Type;
   --  TODO: Description

   function Get_Document_Parts
     (Document : Document_Type)
      return Document_Vector;
   --  TODO: Add the following contract:
   --  with Pre => Document.Kind = Document_List
   --              or else (Document.Kind = Document_Command
   --                       and then Document.Bare_Document.Command.Kind
   --                                = Command_Fill);
   --  TODO: Description

   function Make_Align
     (From                : Indentation_Queue_Type;
      Align_Data          : Alignment_Data_Type;
      Options             : Format_Options_Type;
      Current_Line_Length : Natural)
      return Indentation_Queue_Type;
   --  TODO: Description

   function Make_Indentation
     (From    : Indentation_Queue_Type;
      Options : Indentation_Options_Type)
      return Indentation_Queue_Type;
   --  TODO: Description

   procedure Propagate_Breaks (Document : Document_Type);
   --  TODO: Description

   function Root_Indent
     (Options : Indentation_Options_Type)
      return Indentation_Queue_Type;
   --  Creates an initial indentation queue with Options.Offset as indentation
   --  offset.

   function Slice
     (Documents : Document_Vector;
      First     : Positive;
      Last      : Positive)
      return Document_Vector;
   --  TODO: Description

   procedure Traverse_Document
     (Document                           : Document_Type;
      On_Enter                           :
        access function
          (Document : Document_Type)
           return Optional_Boolean := null;
      On_Exit                            :
        access function
          (Document : Document_Type)
           return Optional_Boolean := null;
      Should_Traverse_Conditional_Groups : Boolean := False);
   --  TODO: Description
   --  TODO: Refactor so that Optional_Boolean is not needed

   type Trim_End is (Left, Right, Both);

   procedure Trim
     (Item : in out Prettier_String;
      Side : Trim_End);
   --  Remove whitespaces and horizontal tab characters from Item.
   --  Side controls from which end(s) characters are removed.

   function Trim
     (Text : in out Prettier_String)
      return Natural;
   --  Removes leading whitespaces and horizontal tab characters from Text.
   --  Returns how many characters were removed.

   function "-"
     (Left  : Integer;
      Right : VSS.Strings.Display_Cell_Count) return Integer is
      (Left - Integer (Right));

   function "+"
     (Left  : Integer;
      Right : VSS.Strings.Display_Cell_Count) return Integer is
      (Left + Integer (Right));

   ---------
   -- "=" --
   ---------

   function "=" (Left, Right : Document_Type) return Boolean is
     ((Left.Bare_Document = null and Right.Bare_Document = null)
      or (Left.Bare_Document /= null
          and then Right.Bare_Document /= null
          and then Left.Bare_Document.Id = Right.Bare_Document.Id));

   ------------
   -- Append --
   ------------

   procedure Append (To : in out Prettier_String; Source : Prettier_String)
   is
   begin
      VSS.Strings.Append (To.Text, Source.Text);
      To.Display_Width := @ + Source.Display_Width;
   end Append;

   ------------------------
   -- Break_Parent_Group --
   ------------------------

   procedure Break_Parent_Group
     (Group_Stack : in out Document_Vector)
   is
      use type Ada.Containers.Count_Type;

   begin
      if Group_Stack.Length > 0 then
         declare
            Parent_Group  : constant Document_Vectors.Reference_Type :=
              Group_Stack.Reference (Group_Stack.Last);
            Group_Command : constant Command_Access :=
              Parent_Group.Bare_Document.Command;

         begin
            if (Group_Command.Expanded_States = No_Document
                or else Group_Command.Expanded_States.Bare_Document.List.Length
                        = 0)
              and not Group_Command.Break
            then
               --  TODO: Why should this be "propagated"?
               Group_Command.all.Break := True;
            end if;
         end;
      end if;
   end Break_Parent_Group;

   ------------------------
   -- Break_Parent_Table --
   ------------------------

   procedure Break_Parent_Table
     (Table_Stack : in out Document_Vector) is
   begin
      if not Table_Stack.Is_Empty then
         Table_Stack
           .Last_Element
           .Bare_Document
           .Command
           .Alignment_Table_Must_Break := True;
         Table_Stack
           .Last_Element
           .Bare_Document
           .Command
           .Break_Parents := True;
      end if;
   end Break_Parent_Table;

   ----------
   -- Fits --
   ----------

   function Fits
     (Next            : Print_Command_Type;
      Rest_Commands   : Print_Command_Type_Vector;
      Width           : Integer;
      Has_Line_Suffix : Boolean;
      Group_Mode_Map  : Symbol_To_Mode_Map;
      Must_Be_Flat    : Boolean := False)
      return Boolean
   is
      type Fit_Command_Type is record
         Mode     : Mode_Kind;
         Document : Document_Type;
      end record;

      package Fit_Command_Type_Vectors is new
        Ada.Containers.Vectors (Positive, Fit_Command_Type);

      subtype Fit_Command_Type_Vector is Fit_Command_Type_Vectors.Vector;

      function To_Fit_Command_Type
        (Print_Command : Print_Command_Type)
         return Fit_Command_Type;
      --  TODO: Description

      -------------------------
      -- To_Fit_Command_Type --
      -------------------------

      function To_Fit_Command_Type
        (Print_Command : Print_Command_Type)
         return Fit_Command_Type
      is (Fit_Command_Type'(Print_Command.Mode, Print_Command.Document));

      function "+" (Print_Comment : Print_Command_Type) return Fit_Command_Type
        renames To_Fit_Command_Type;

      Remaining_Width : Integer := Width;
      Rest_Idx        : Print_Command_Type_Vectors.Extended_Index :=
        Rest_Commands.Last_Index;
      Fit_Commands    : Fit_Command_Type_Vector := [+Next];

      Current_Line : Prettier_String;

      Current_Has_Line_Suffix : Boolean := Has_Line_Suffix;

   begin
      if Remaining_Width = Integer'Last then
         return True;
      end if;

      while Remaining_Width >= 0 loop
         if Fit_Commands.Is_Empty then
            if Rest_Idx < Rest_Commands.First_Index then
               return True;
            end if;

            Fit_Commands.Append (+Rest_Commands.Element (Rest_Idx));
            Rest_Idx := @ - Natural (1);

         else
            declare
               Current_Fit_Command : constant Fit_Command_Type :=
                  Fit_Commands.Last_Element;
               Mode                : Mode_Kind
                 renames Current_Fit_Command.Mode;
               Document            : Document_Type
                 renames Current_Fit_Command.Document;

               use type Ada.Containers.Count_Type;

            begin
               Fit_Commands.Delete_Last;

               case Document.Bare_Document.Kind is
                  when Document_Text =>
                     Append
                       (Current_Line, Document.Bare_Document.Text);
                     Remaining_Width :=
                       @ - Document.Bare_Document.Text.Display_Width;

                  when Document_List =>
                     for Child_Document of
                        reverse Get_Document_Parts (Document)
                     loop
                        Fit_Commands.Append
                          (Fit_Command_Type'(Mode, Child_Document));
                     end loop;

                  when Document_Command =>
                     case Document.Bare_Document.Command.Kind is
                        when Command_Fill =>
                           for Child_Document of
                              reverse Get_Document_Parts (Document)
                           loop
                              Fit_Commands.Append
                                (Fit_Command_Type'(Mode, Child_Document));
                           end loop;

                        when Command_Indent =>
                           Ada.Assertions.Assert
                             (Document
                                .Bare_Document
                                .Command
                                .Indent_Contents
                              /= No_Document);
                           Fit_Commands.Append
                             (Fit_Command_Type'
                                (Mode,
                                 Document
                                   .Bare_Document
                                   .Command
                                   .Indent_Contents));

                        when Command_Align =>
                           Ada.Assertions.Assert
                             (Document
                                .Bare_Document
                                .Command
                                .Align_Contents
                              /= No_Document);
                           Fit_Commands.Append
                             (Fit_Command_Type'
                                (Mode,
                                 Document
                                   .Bare_Document
                                   .Command
                                   .Align_Contents));

                        when Command_Indent_If_Break =>
                           Ada.Assertions.Assert
                             (Document
                                .Bare_Document
                                .Command
                                .Indent_If_Break_Contents
                              /= No_Document);
                           Fit_Commands.Append
                             (Fit_Command_Type'
                                (Mode,
                                 Document
                                   .Bare_Document
                                   .Command
                                   .Indent_If_Break_Contents));

                        when Command_Label =>
                           Ada.Assertions.Assert
                             (Document
                                .Bare_Document
                                .Command
                                .Label_Contents
                              /= No_Document);
                           Fit_Commands.Append
                             (Fit_Command_Type'
                                (Mode,
                                 Document
                                   .Bare_Document
                                   .Command
                                   .Label_Contents));

                        when Command_Trim =>
                           Remaining_Width := @ + Trim (Current_Line);

                        when Command_Group =>
                           if Must_Be_Flat
                             and Document.Bare_Document.Command.Break
                           then
                              return False;
                           end if;

                           declare
                              Group_Mode : constant Mode_Kind :=
                                (if Document.Bare_Document.Command.Break then
                                   Mode_Break
                                 else
                                   Mode);
                              Contents : constant Document_Type :=
                                (if (Document
                                      .Bare_Document
                                      .Command
                                      .Expanded_States
                                     /= No_Document
                                     and then Document
                                      .Bare_Document
                                      .Command
                                      .Expanded_States
                                      .Bare_Document.List.Length > 0)
                                   and Group_Mode = Mode_Break
                                 then
                                   Document
                                     .Bare_Document
                                     .Command
                                     .Expanded_States
                                     .Bare_Document
                                     .List
                                     .Last_Element
                                 else
                                   Document
                                     .Bare_Document
                                     .Command
                                     .Group_Contents);

                           begin
                              Fit_Commands.Append
                                (Fit_Command_Type'(Group_Mode, Contents));
                           end;

                        when Command_If_Break =>
                           declare
                              Group_Mode : constant Mode_Kind :=
                                (if Document
                                      .Bare_Document
                                      .Command
                                      .If_Break_Group_Id
                                    /= No_Symbol
                                 then
                                   (if Group_Mode_Map.Contains
                                         (Document
                                            .Bare_Document
                                            .Command
                                            .If_Break_Group_Id)
                                    then
                                      Group_Mode_Map.Element
                                        (Document
                                           .Bare_Document
                                           .Command
                                           .If_Break_Group_Id)
                                    else
                                      Mode_Flat)
                                 else
                                   Mode);
                              Contents : constant Document_Type :=
                                (if Group_Mode = Mode_Break then
                                   Document
                                     .Bare_Document
                                     .Command
                                     .Break_Contents
                                 else
                                   Document
                                     .Bare_Document
                                     .Command
                                 .Flat_Contents);

                           begin
                              if Contents /= No_Document
                                and then (if Contents.Bare_Document.Kind in
                                               Document_Text
                                          then
                                            not Contents
                                                  .Bare_Document
                                                  .Text
                                                  .Text
                                                  .Is_Empty)
                              then
                                 Fit_Commands.Append
                                   (Fit_Command_Type'(Mode, Contents));
                              end if;
                           end;

                        when Command_Line =>
                           if Mode = Mode_Break
                             or Document.Bare_Document.Command.Hard
                           then
                              return True;
                           end if;

                           if not Document.Bare_Document.Command.Soft then
                              Append (Current_Line, (" ", 1));
                              Remaining_Width := @ - Natural (1);
                           end if;

                        when Command_Line_Suffix =>
                           Current_Has_Line_Suffix := True;

                        when Command_Line_Suffix_Boundary =>
                           if Current_Has_Line_Suffix then
                              return False;
                           end if;

                        when Command_Break_Parent
                             | Command_Cursor
                             | Command_Alignment_Table_End
                        =>
                           null;

                        when Command_Alignment_Table =>
                           --  First check if the table has more than 1 row and
                           --  must break. In such case, no calculations are
                           --  needed.

                           if Document
                                .Bare_Document
                                .Command
                                .Alignment_Table_Elements
                                .Length > 1
                             and then Document
                                        .Bare_Document
                                        .Command
                                        .Alignment_Table_Must_Break
                           then
                              return False;
                           end if;

                           declare
                              Alignment_Table_Elements   : Document_Table
                                renames Document
                                          .Bare_Document
                                          .Command
                                          .Alignment_Table_Elements;
                              Alignment_Table_Separators : Document_Table
                                renames Document
                                          .Bare_Document
                                          .Command
                                          .Alignment_Table_Separators;

                              --  The algorithm relies on not only checking the
                              --  previous elements/separators of the row but
                              --  also of other rows. The easist way to do this
                              --  is with indices. Start by finding the longest
                              --  column and row.

                              First_Row_Index    : constant Positive :=
                                Alignment_Table_Elements.First_Index;
                              Last_Row_Index     : constant Positive :=
                                Alignment_Table_Elements.Last_Index;
                              First_Column_Index : constant Positive :=
                                Alignment_Table_Elements
                                  .Constant_Reference (First_Row_Index)
                                  .First_Index;
                              Last_Column_Index  : constant Positive :=
                                [for Row_Elements of Alignment_Table_Elements
                                 => Row_Elements.Last_Index]'Reduce
                                                               (Positive'Max,
                                                                1);

                           begin
                              --  Append to Fit_Comands every element/separator
                              --  in reverse order (starting from the last
                              --  column of the last row).

                              for Column_Index
                                in reverse First_Column_Index
                                           .. Last_Column_Index
                              loop
                                 if Alignment_Table_Separators
                                      .Constant_Reference (Last_Row_Index)
                                      .Last_Index
                                    >= Column_Index
                                 then
                                    Fit_Commands.Append
                                      (Fit_Command_Type'
                                         (Mode_Flat,
                                          Alignment_Table_Separators
                                            .Constant_Reference
                                               (Last_Row_Index)
                                            .Constant_Reference
                                               (Column_Index)));
                                 end if;

                                 if Alignment_Table_Elements
                                      .Constant_Reference (Last_Row_Index)
                                      .Last_Index
                                    >= Column_Index
                                 then
                                    Fit_Commands.Append
                                      (Fit_Command_Type'
                                         (Mode_Flat,
                                          Alignment_Table_Elements
                                            .Constant_Reference
                                               (Last_Row_Index)
                                            .Constant_Reference
                                               (Column_Index)));
                                 end if;
                              end loop;

                              for Row_Index
                                in reverse First_Row_Index
                                           .. Standard."-" (Last_Row_Index, 1)
                              loop
                                 --   At a Line between rows

                                 Fit_Commands.Append
                                   (Fit_Command_Type'
                                      (Mode_Flat,
                                       Prettier_Ada.Documents.Builders.Line));

                                 for Column_Index
                                   in reverse First_Column_Index
                                              .. Last_Column_Index
                                 loop
                                    if Alignment_Table_Separators
                                         .Constant_Reference (Row_Index)
                                         .Last_Index
                                       >= Column_Index
                                    then
                                       Fit_Commands.Append
                                         (Fit_Command_Type'
                                            (Mode_Flat,
                                             Alignment_Table_Separators
                                               .Constant_Reference
                                                  (Row_Index)
                                               .Constant_Reference
                                                  (Column_Index)));
                                    end if;

                                    if Alignment_Table_Elements
                                         .Constant_Reference (Row_Index)
                                         .Last_Index
                                       >= Column_Index
                                    then
                                       Fit_Commands.Append
                                         (Fit_Command_Type'
                                            (Mode_Flat,
                                             Alignment_Table_Elements
                                               .Constant_Reference
                                                  (Row_Index)
                                               .Constant_Reference
                                                  (Column_Index)));
                                    end if;
                                 end loop;
                              end loop;
                           end;

                        when Command_Alignment_Table_Separator =>
                           Append
                             (Current_Line,
                              Document
                                .Bare_Document
                                .Command
                                .Alignment_Table_Separator_Text);
                           Remaining_Width :=
                             @
                             - Document
                                 .Bare_Document
                                 .Command
                                 .Alignment_Table_Separator_Text
                                 .Display_Width;

                     end case;
               end case;
            end;
         end if;
      end loop;

      return False;
   end Fits;

   ------------
   -- Format --
   ------------

   function Format
     (Document : Document_Type;
      Options  : Format_Options_Type := Default_Format_Options)
      return Ada.Strings.Unbounded.Unbounded_String
   is
      State : Format_State_Record := Default_Format_State;

   begin
      --  Adapt State, which at this point is a Default_Format_state, based
      --  on Document (to be formatted) and Options.

      State.Current_Line_Length :=
        Options.Indentation.Offset.Spaces
        + Options.Indentation.Offset.Tabs * Options.Indentation.Width;

      State.Result :=
        (VSS.Strings."&"
           (VSS.Strings."*"
              (VSS.Strings.Character_Count (Options.Indentation.Offset.Tabs),
               VSS.Characters.Latin.Character_Tabulation),
            VSS.Strings."*"
              (VSS.Strings.Character_Count (Options.Indentation.Offset.Spaces),
               VSS.Characters.Latin.Space)),
         VSS.Strings.Display_Cell_Count
           (Options.Indentation.Offset.Spaces
            + Options.Indentation.Offset.Tabs * Options.Indentation.Width));

      State.Print_Commands :=
        [Print_Command_Type'
           (Root_Indent (Options.Indentation),
            Mode_Break,
            Document)];

      Propagate_Breaks (Document);

      Format (State, Options);

      return
        VSS.Strings.Conversions.To_Unbounded_UTF_8_String (State.Result.Text);
   end Format;

   ------------
   -- Format --
   ------------

   procedure Format
     (Format_State   : in out Format_State_Record;
      Format_Options : Format_Options_Type)
   is
      use type Ada.Containers.Count_Type;
      use type VSS.Strings.Virtual_String;

      End_Of_Line : constant Prettier_String :=
        (case Format_Options.End_Of_Line is
            when LF =>
              (VSS.Strings.Empty_Virtual_String
               & VSS.Characters.Latin.Line_Feed,
               0),
            when CR =>
              (VSS.Strings.Empty_Virtual_String
               & VSS.Characters.Latin.Carriage_Return,
               0),
            when CRLF =>
              (VSS.Strings.Empty_Virtual_String
               & VSS.Characters.Latin.Carriage_Return
               & VSS.Characters.Latin.Line_Feed,
               0));

   begin
      while Format_State.Print_Commands.Length > 0 loop
         declare
            Print_Command : constant Print_Command_Type :=
              Format_State.Print_Commands.Last_Element;
            Indentation   : Indentation_Queue_Type
              renames Print_Command.Indentation;
            Mode          : Mode_Kind renames Print_Command.Mode;
            Document      : Document_Type renames Print_Command.Document;

            procedure Process_Document_Command_Group;
            --  TODO: Description

            procedure Process_Document_Command_Fill;
            --  TODO: Description

            procedure Process_Document_Command_Line;
            --  TODO: Description

            procedure Process_Document_List;
            --  TODO: Description

            procedure Process_Document_Text;
            --  TODO: Description

            procedure Process_Document_Command_Alignment_Table;
            --  Processes Print_Command as an Alignment_Table.
            --  An Alignment_Table can be processed in two modes:
            --  - Break
            --  - Flat
            --
            --  To process an Alignment Table in Break mode, each column is
            --  handled sequentially. For each column, the required alignment
            --  is calculated and applied. Once a column is processed, its
            --  separators are appended. This process is repeated for each
            --  column until the entire table is processed.
            --
            --  To process an Alignment_Table in Flat mode, each row is handled
            --  sequentually as list.

            procedure Process_Document_Command_Alignment_Table_Separator;
            --  Processes Print_Command as an Alignment_Table_Separator
            --  command. An Alignment_Table_Separator command is only processed
            --  this way if it's not a child document of an Alignment_Table or
            --  if the Alignment_Table was handled in Flat mode. In this case,
            --  the separators' text is simply appended to
            --  Format_State.Result.

            ------------------------------------
            -- Process_Document_Command_Group --
            ------------------------------------

            procedure Process_Document_Command_Group is
               procedure Process_Mode_Break;
               --  TODO

               procedure Process_Mode_Flat;
               --  TODO

               ------------------------
               -- Process_Mode_Break --
               ------------------------

               procedure Process_Mode_Break is
                  Next : constant Print_Command_Type :=
                    (Indentation => Indentation,
                     Mode        => Mode_Flat,
                     Document    =>
                       Document
                         .Bare_Document
                         .Command
                         .Group_Contents);
                  Remaining_Line_Length : constant Integer :=
                    Format_Options.Width - Format_State.Current_Line_Length;
                  Has_Line_Suffix       : constant Boolean :=
                    Format_State.Line_Suffix.Length > 0;

               begin
                  Format_State.Should_Remeasure := False;
                  if not Document.Bare_Document.Command.Break
                     and then
                       Fits
                         (Next,
                          Format_State.Print_Commands,
                          Remaining_Line_Length,
                          Has_Line_Suffix,
                          Format_State.Group_Mode_Map)
                  then
                     Format_State.Print_Commands.Append (Next);

                  else
                     if Document
                          .Bare_Document
                          .Command
                          .Expanded_States
                        /= No_Document
                       and then Document
                                  .Bare_Document
                                  .Command
                                  .Expanded_States
                                  .Bare_Document
                                  .List.Length /= 0
                     then
                        declare
                           Expanded_States :
                             constant Document_Vector :=
                               Document
                                 .Bare_Document
                                 .Command
                                 .Expanded_States
                                 .Bare_Document
                                 .List;
                           Most_Expanded :
                             constant Document_Type :=
                               Expanded_States.Last_Element;

                        begin
                           if Document
                                .Bare_Document
                                .Command
                                .Break
                           then
                              Format_State.Print_Commands.Append
                                (Print_Command_Type'
                                   (Indentation,
                                    Mode_Break,
                                    Most_Expanded));

                           else
                              for J in
                                 Expanded_States.First_Index + Natural (1)
                                 .. Expanded_States.Last_Index + Natural (1)
                              loop
                                 if J
                                    >= Expanded_States.Last_Index + Natural (1)
                                 then
                                    Format_State.Print_Commands.Append
                                      (Print_Command_Type'
                                         (Indentation,
                                          Mode_Break,
                                          Most_Expanded));
                                 else
                                    declare
                                       State         :
                                         constant Document_Type :=
                                           Expanded_States (J);
                                       Print_Command :
                                         constant
                                           Print_Command_Type :=
                                             (Indentation,
                                              Mode_Flat,
                                              State);
                                    begin
                                       if Fits
                                            (Print_Command,
                                             Format_State.Print_Commands,
                                             Remaining_Line_Length,
                                             Has_Line_Suffix,
                                             Format_State.Group_Mode_Map)
                                       then
                                          Format_State.Print_Commands.Append
                                            (Print_Command);
                                          exit;
                                       end if;
                                    end;
                                 end if;
                              end loop;
                           end if;
                        end;

                     else
                        Format_State.Print_Commands.Append
                          (Print_Command_Type'
                             (Indentation,
                              Mode_Break,
                              Document
                                .Bare_Document
                                .Command
                                .Group_Contents));
                     end if;
                  end if;
               end Process_Mode_Break;

               -----------------------
               -- Process_Mode_Flat --
               -----------------------

               procedure Process_Mode_Flat is
                  Print_Command : constant Print_Command_Type :=
                    Print_Command_Type'
                      (Indentation,
                       (if Document
                             .Bare_Document
                             .Command
                             .Break
                        then Mode_Break
                        else Mode_Flat),
                       Document
                         .Bare_Document
                         .Command
                         .Group_Contents);
               begin
                  if not Format_State.Should_Remeasure then
                     Format_State.Print_Commands.Append (Print_Command);
                  else
                     Process_Mode_Break;
                  end if;
               end Process_Mode_Flat;

            begin
               case Mode is
                  when Mode_Flat =>
                     Process_Mode_Flat;

                  when Mode_Break =>
                     Process_Mode_Break;

                  when None =>
                     null;
               end case;

               if Document.Bare_Document.Command.Id
                  /= No_Symbol
               then
                  Format_State.Group_Mode_Map.Include
                    (Document.Bare_Document.Command.Id,
                     Format_State.Print_Commands.Last_Element.Mode);
               end if;
            end Process_Document_Command_Group;

            -----------------------------------
            -- Process_Document_Command_Fill --
            -----------------------------------

            procedure Process_Document_Command_Fill is
               Remaining_Line_Length : constant Integer :=
                 Format_Options.Width - Format_State.Current_Line_Length;
               Parts     : constant Document_Vector :=
                 Document
                   .Bare_Document
                   .Command
                   .Parts
                   .Bare_Document
                   .List;

            begin
               if Parts.Length = 1 then
                  declare
                     Content_Flat_Command  : constant Print_Command_Type :=
                         (Indentation, Mode_Flat, Parts.First_Element);
                     Content_Break_Command : constant Print_Command_Type :=
                         (Indentation, Mode_Break, Parts.First_Element);
                     Content_Fits          : constant Boolean :=
                       Fits
                         (Content_Flat_Command,
                          [],
                          Remaining_Line_Length,
                          Format_State.Line_Suffix.Length > 0,
                          Format_State.Group_Mode_Map,
                          True);

                  begin
                     if Content_Fits then
                        Format_State
                          .Print_Commands
                          .Append (Content_Flat_Command);

                     else
                        Format_State
                          .Print_Commands
                          .Append (Content_Break_Command);
                     end if;
                  end;

               elsif Parts.Length = 2 then
                  declare
                     Content_Flat_Command  : constant Print_Command_Type :=
                        (Indentation, Mode_Flat, Parts.First_Element);
                     Content_Break_Command : constant Print_Command_Type :=
                        (Indentation, Mode_Break, Parts.First_Element);
                     White_Flat_Command    : constant Print_Command_Type :=
                        (Indentation,
                         Mode_Flat,
                         Parts.Element (Parts.First_Index + Natural (1)));
                     White_Break_Command   : constant Print_Command_Type :=
                        (Indentation,
                         Mode_Break,
                         Parts (Parts.First_Index + Natural (1)));
                     Content_Fits : constant Boolean :=
                       Fits
                         (Content_Flat_Command,
                          [],
                          Remaining_Line_Length,
                          Format_State.Line_Suffix.Length > 0,
                          Format_State.Group_Mode_Map,
                          True);

                  begin
                     if Content_Fits then
                        Format_State
                          .Print_Commands
                          .Append (White_Flat_Command);
                        Format_State
                          .Print_Commands
                          .Append (Content_Flat_Command);

                     else
                        Format_State
                          .Print_Commands
                          .Append (White_Break_Command);
                        Format_State
                          .Print_Commands
                          .Append (Content_Break_Command);
                     end if;
                  end;

               elsif Parts.Length /= 0 then
                  declare
                     Content_Flat_Command  :
                       constant Print_Command_Type :=
                         (Indentation, Mode_Flat, Parts.First_Element);
                     Content_Break_Command :
                       constant Print_Command_Type :=
                         (Indentation, Mode_Break, Parts.First_Element);
                     White_Flat_Command    :
                       constant Print_Command_Type :=
                         (Indentation,
                          Mode_Flat,
                          Parts.Element (Parts.First_Index + Natural (1)));
                     White_Break_Command   :
                       constant Print_Command_Type :=
                         (Indentation,
                          Mode_Break,
                          Parts.Element (Parts.First_Index + Natural (1)));

                     Content_Fits : constant Boolean :=
                       Fits
                         (Content_Flat_Command,
                          [],
                          Remaining_Line_Length,
                          Format_State.Line_Suffix.Length > 0,
                          Format_State.Group_Mode_Map,
                          True);

                     Remaining_Parts         : constant Document_Vector :=
                       Slice
                         (Parts,
                          Positive (Parts.First_Index + Natural (2)),
                          Positive (Parts.Last_Index));
                     Remaining_Print_Command : constant Print_Command_Type :=
                       (Indentation,
                        Mode,
                        Prettier_Ada.Documents.Builders.Fill
                          (Remaining_Parts));

                     First_And_Second_Content_Flat_Command :
                       constant Print_Command_Type :=
                         (Indentation,
                          Mode_Flat,
                          Prettier_Ada.Documents.Builders.List
                            (Slice
                               (Parts,
                                Positive (Parts.First_Index),
                                Positive (Parts.First_Index + Natural (2)))));
                     First_And_Second_Content_Fits         :
                       constant Boolean :=
                         Fits
                           (First_And_Second_Content_Flat_Command,
                            [],
                            Remaining_Line_Length,
                            Format_State.Line_Suffix.Length > 0,
                            Format_State.Group_Mode_Map,
                            True);

                  begin
                     if First_And_Second_Content_Fits then
                        Format_State
                          .Print_Commands
                          .Append (Remaining_Print_Command);
                        Format_State
                          .Print_Commands
                          .Append (White_Flat_Command);
                        Format_State
                          .Print_Commands
                          .Append (Content_Flat_Command);

                     elsif Content_Fits then
                        Format_State
                          .Print_Commands
                          .Append (Remaining_Print_Command);
                        Format_State
                          .Print_Commands
                          .Append (White_Break_Command);
                        Format_State
                          .Print_Commands
                          .Append (Content_Flat_Command);

                     else
                        Format_State
                          .Print_Commands
                          .Append (Remaining_Print_Command);
                        Format_State
                          .Print_Commands
                          .Append (White_Break_Command);
                        Format_State
                          .Print_Commands
                          .Append (Content_Break_Command);
                     end if;
                  end;
               end if;
            end Process_Document_Command_Fill;

            -----------------------------------
            -- Process_Document_Command_Line --
            -----------------------------------

            procedure Process_Document_Command_Line is
               procedure Process_Mode_Break;
               --  TODO

               procedure Process_Mode_Flat;
               --  TODO

               ------------------------
               -- Process_Mode_Break --
               ------------------------

               procedure Process_Mode_Break is
               begin
                  if not Format_State.Line_Suffix.Is_Empty then
                     Format_State.Print_Commands.Append
                       (Print_Command_Type'(Indentation, Mode, Document));
                     for Suffix of reverse Format_State.Line_Suffix loop
                        Format_State.Print_Commands.Append (Suffix);
                     end loop;
                     Format_State.Line_Suffix.Clear;
                     Format_State.Last_Was_Hardline := False;

                  else
                     if Document.Bare_Document.Command.Literal then
                        if Indentation.Root /= null then
                           Append (Format_State.Result, End_Of_Line);
                           Append
                             (Format_State.Result, Indentation.Root.Value);
                           Format_State.Current_Line_Length :=
                             Natural (Indentation.Root.Value.Display_Width);

                        else
                           Append (Format_State.Result, End_Of_Line);
                           Format_State.Current_Line_Length :=
                             Format_Options.Indentation.Offset.Spaces
                             + Format_Options.Indentation.Offset.Tabs
                               * Format_Options.Indentation.Width;
                        end if;
                        Format_State.Last_Was_Hardline := False;

                     else
                        Format_State.Current_Line_Length :=
                          @ - Trim (Format_State.Result);
                        Append (Format_State.Result, End_Of_Line);
                        Append (Format_State.Result, Indentation.Value);
                        Format_State.Current_Line_Length :=
                          Natural (Indentation.Value.Display_Width);
                        Format_State.Last_Was_Hardline := True;
                     end if;
                  end if;
               end Process_Mode_Break;

               -----------------------
               -- Process_Mode_Flat --
               -----------------------

               procedure Process_Mode_Flat is
               begin
                  if not Document.Bare_Document.Command.Hard then
                     if not Document
                              .Bare_Document
                              .Command
                              .Soft
                     then
                        Append (Format_State.Result, (" ", 1));
                        Format_State.Current_Line_Length := @ + Natural (1);
                     end if;
                     Format_State.Last_Was_Hardline := False;

                  else
                     Format_State.Should_Remeasure := True;
                     Process_Mode_Break;
                  end if;
               end Process_Mode_Flat;

            begin
               case Mode is
                  when Mode_Flat =>
                     Process_Mode_Flat;

                  when Mode_Break =>
                     Process_Mode_Break;

                  when None =>
                     null;
               end case;
            end Process_Document_Command_Line;

            ---------------------------
            -- Process_Document_Text --
            ---------------------------

            procedure Process_Document_Text is
            begin
               Append (Format_State.Result, Document.Bare_Document.Text);
               Format_State.Current_Line_Length :=
                 @ + Document.Bare_Document.Text.Display_Width;
            end Process_Document_Text;

            ---------------------------
            -- Process_Document_List --
            ---------------------------

            procedure Process_Document_List is
               Documents : constant Document_Vector :=
                 Document.Bare_Document.List;

            begin
               for Child_Document of reverse Documents loop
                  Ada.Assertions.Assert (Child_Document /= No_Document);
                  Format_State.Print_Commands.Append
                    (Print_Command_Type'(Indentation, Mode, Child_Document));
               end loop;
            end Process_Document_List;

            ----------------------------------------------
            -- Process_Document_Command_Alignment_Table --
            ----------------------------------------------

            procedure Process_Document_Command_Alignment_Table
            is
               procedure Process_Mode_Break;
               --  Processes the Alignment_Table comamnd in Break mode

               procedure Process_Mode_Flat;
               --  Processes the Alignment_Table comamnd in Flat mode

               ------------------------
               -- Process_Mode_Break --
               ------------------------

               procedure Process_Mode_Break is
                  use VSS.Strings;

                  type Format_State_Array is
                    array (Positive range <>) of Format_State_Record;

                  Alignment_Table_Elements   : Document_Table
                    renames Document
                              .Bare_Document
                              .Command
                              .Alignment_Table_Elements;
                  Alignment_Table_Separators : Document_Table
                    renames Document
                              .Bare_Document
                              .Command
                              .Alignment_Table_Separators;

                  States :
                    Format_State_Array
                      (Alignment_Table_Elements.First_Index
                       .. Alignment_Table_Elements.Last_Index) :=
                      [others => Format_State];
                  --  The state for each row

                  First_Row_Index    : constant Positive :=
                    Alignment_Table_Elements.First_Index;
                  Last_Row_Index     : constant Positive :=
                    Alignment_Table_Elements.Last_Index;
                  First_Column_Index : constant Positive := 1;
                  Last_Column_Index  : constant Positive :=
                    [for Row_Elements of Alignment_Table_Elements
                     => Row_Elements.Last_Index]'Reduce (Positive'Max, 1);

                  Max_Line_Length : Natural := 0;

               begin
                  Prettier_Ada_Trace.Trace
                    ("On Process_Document_Command_Alignment_Table."
                     & "Process_Mode_Break");

                  for State of States loop
                     State.Result := Empty_Prettier_String;
                  end loop;

                  if Format_State.Last_Was_Hardline then
                     --  If the last document appended to Format_State.Result
                     --  was a hardline, then we need to move any indentation
                     --  from Format_State.Result into
                     --  States (First_Row_Index).Result. Otherwise, if the
                     --  first document of the first row is a line break,
                     --  previously added identation is not removed, resulting
                     --  in trailing whitespaces in the final
                     --  Format_State.Result.

                     Trim (Format_State.Result, Right);
                     States (First_Row_Index)
                       .Result
                       .Text
                       .Append (Indentation.Value.Text);
                  end if;

                  Prettier_Ada_Trace.Trace
                    ("On Column_Index " & First_Column_Index'Image);

                  for Row_Index in First_Row_Index .. Last_Row_Index loop
                     Prettier_Ada_Trace.Trace
                       ("On Row_Index " & Row_Index'Image);
                     declare
                        Separator_Display_Width :
                          constant Display_Cell_Count :=
                            (if not Alignment_Table_Separators
                                      .Reference (Row_Index)
                                      .Is_Empty
                             then
                               Alignment_Table_Separators
                                 .Reference (Row_Index)
                                 .Reference (First_Column_Index)
                                 .Bare_Document
                                 .Command
                                 .Alignment_Table_Separator_Text
                                 .Display_Width
                            else 0);
                        Next_Options            :
                          constant Format_Options_Type :=
                            Format_Options_Type'
                               (Width       =>
                                  (if Natural (Separator_Display_Width)
                                      > Format_Options.Width
                                   then 0
                                   else
                                     Format_Options.Width
                                     - Natural (Separator_Display_Width)),
                                Indentation => Format_Options.Indentation,
                                End_Of_Line => Format_Options.End_Of_Line);
                        --  The formatting options for each element need
                        --  to have an offset on the maximum width to account
                        --  the separator's width.

                     begin
                        States (Row_Index).Should_Remeasure := False;

                        --  Check if this is the last table element. If so,
                        --  include the remaining print documents preceded
                        --  by a Command_Alignment_Table_End. The Fits
                        --  function ignores this command, but the
                        --  Format function does not. This ensures that the
                        --  remaining Print_Documents are taken into account
                        --  when checking if the table element should break
                        --  or not.

                        if Row_Index = Last_Row_Index
                          and then First_Column_Index = Last_Column_Index
                        then
                           States (Row_Index).Print_Commands :=
                             Format_State.Print_Commands;
                           States (Row_Index).Print_Commands.Append
                             (Print_Command_Type'
                                (Indentation,
                                 Mode_Break,
                                 Wrap_Command
                                   (new Command_Type'
                                      (Kind =>
                                         Command_Alignment_Table_End))));
                           States (Row_Index).Print_Commands.Append
                             (Print_Command_Type'
                                (Indentation,
                                 Mode_Break,
                                 Alignment_Table_Elements
                                   .Reference (Row_Index)
                                   .Reference (First_Column_Index)));
                        else
                           States (Row_Index).Print_Commands :=
                             [Print_Command_Type'
                                (Indentation,
                                 Mode_Break,
                                 Alignment_Table_Elements
                                   .Reference (Row_Index)
                                   .Reference (First_Column_Index))];
                        end if;

                        Format (States (Row_Index), Next_Options);

                        Prettier_Ada_Trace.Trace
                          ("States.State.Current_Line_Length"
                           & States (Row_Index).Current_Line_Length'Image);

                        --  Compute the Max_Line_Length seen so far. If there's
                        --  not separators on this row, then this row line
                        --  length should not affect the alignment of others.

                        if not Alignment_Table_Separators (Row_Index).Is_Empty
                        then
                           Max_Line_Length :=
                             Natural'Max
                               (@, States (Row_Index).Current_Line_Length);
                        end if;
                     end;
                  end loop;

                  Prettier_Ada_Trace.Trace
                    ("Max_Line_Length" & Max_Line_Length'Image);

                  for Column_Index
                     in Standard."+" (First_Column_Index, 1)
                        .. Last_Column_Index
                  loop
                     Prettier_Ada_Trace.Trace
                       ("On Column_Index " & Column_Index'Image);

                     --  Add padding to the buffers

                     Prettier_Ada_Trace.Trace
                       ("Adding padding to the buffers");

                     for Row_Index in First_Row_Index .. Last_Row_Index loop
                        Prettier_Ada_Trace.Trace
                          ("On Row_Index " & Row_Index'Image);

                        if Column_Index
                           <= Alignment_Table_Elements
                                .Element (Row_Index)
                                .Last_Index
                        then
                           declare
                              Padding : constant Character_Count :=
                                Character_Count
                                  (Max_Line_Length
                                   - States (Row_Index).Current_Line_Length);

                           begin
                              Prettier_Ada_Trace.Trace
                                ("Padding " & Padding'Image);

                              if Padding /= 0 then
                                 States (Row_Index)
                                   .Result
                                   .Text
                                   .Append (Padding * ' ');
                                 States (Row_Index).Current_Line_Length :=
                                   Max_Line_Length;
                              end if;
                           end;
                        end if;

                     end loop;

                     Prettier_Ada_Trace.Trace
                       ("Adding separators to the buffers");

                     --  Add separators to the buffers

                     for Row_Index in First_Row_Index .. Last_Row_Index loop
                        Prettier_Ada_Trace.Trace
                          ("On Row_Index " & Row_Index'Image);

                        if Standard."-" (Column_Index, 1)
                           <= Alignment_Table_Separators
                                .Element (Row_Index)
                                .Last_Index
                        then
                           declare
                              Separator : constant Document_Type :=
                                Alignment_Table_Separators
                                  .Constant_Reference (Row_Index)
                                  .Constant_Reference
                                     (Standard."-" (Column_Index, 1));

                              Separator_Display_Width : constant Natural :=
                                Natural
                                  (Separator
                                     .Bare_Document
                                     .Command
                                     .Alignment_Table_Separator_Text
                                     .Display_Width);

                              Row_Line_Length_After_Separator :
                                constant Natural :=
                                  Max_Line_Length + Separator_Display_Width;

                           begin
                              Prettier_Ada_Trace.Trace
                                ("Row_Line_Length_After_Separator "
                                 & Row_Line_Length_After_Separator'Image);

                              States (Row_Index)
                                .Result
                                .Text
                                .Append
                                   (Separator
                                      .Bare_Document
                                      .Command
                                      .Alignment_Table_Separator_Text
                                      .Text);
                              States (Row_Index).Current_Line_Length :=
                                Row_Line_Length_After_Separator;
                           end;
                        end if;
                     end loop;

                     --  Format current element and add it to the buffers

                     Prettier_Ada_Trace.Trace
                       ("Format current element and add it to the buffers");

                     Max_Line_Length := 0;

                     for Row_Index in First_Row_Index .. Last_Row_Index loop
                        Prettier_Ada_Trace.Trace
                          ("On Row_Index " & Row_Index'Image);

                        if Column_Index
                           <= Alignment_Table_Elements
                                .Constant_Reference (Row_Index)
                                .Last_Index
                        then
                           declare
                              Separator_Display_Width :
                                constant Display_Cell_Count :=
                                  (if Alignment_Table_Separators
                                        .Constant_Reference (Row_Index)
                                        .Last_Index
                                      >= Column_Index
                                   then
                                     Alignment_Table_Separators
                                       .Constant_Reference (Row_Index)
                                       .Constant_Reference (Column_Index)
                                       .Bare_Document
                                       .Command
                                       .Alignment_Table_Separator_Text
                                       .Display_Width
                                  else 0);
                              Next_Options :
                                constant Format_Options_Type :=
                                   Format_Options_Type'
                                      (Width       =>
                                         (if Natural (Separator_Display_Width)
                                             > Format_Options.Width
                                          then
                                            0
                                          else
                                            Format_Options.Width
                                            - Natural
                                                (Separator_Display_Width)),
                                       Indentation =>
                                         Format_Options.Indentation,
                                       End_Of_Line =>
                                         Format_Options.End_Of_Line);

                           begin
                              States (Row_Index).Should_Remeasure := False;

                              --  Check if this is the last table element. If
                              --  so, include the remaining print documents
                              --  preceded by a Command_Alignment_Table_End.
                              --  The Fits function ignores this command, but
                              --  the Format function does not. This ensures
                              --  that the remaining Print_Documents are taken
                              --  into account when checking if the table
                              --  element should break or not.

                              if Row_Index = Last_Row_Index
                                and then Column_Index
                                         = Alignment_Table_Elements
                                             .Constant_Reference (Row_Index)
                                             .Last_Index
                              then
                                 States (Row_Index).Print_Commands :=
                                   Format_State.Print_Commands;
                                 States (Row_Index).Print_Commands.Append
                                   (Print_Command_Type'
                                      (Indentation,
                                       Mode_Break,
                                       Wrap_Command
                                         (new Command_Type'
                                            (Kind =>
                                               Command_Alignment_Table_End))));
                                 States (Row_Index).Print_Commands.Append
                                   (Print_Command_Type'
                                      (Indentation,
                                       Mode_Break,
                                       Alignment_Table_Elements
                                         .Reference (Row_Index)
                                         .Reference (Column_Index)));
                              else
                                 States (Row_Index).Print_Commands :=
                                   [Print_Command_Type'
                                      (Indentation,
                                       Mode_Break,
                                       Alignment_Table_Elements
                                         .Reference (Row_Index)
                                         .Reference (Column_Index))];
                              end if;

                              Format (States (Row_Index), Next_Options);

                              Prettier_Ada_Trace.Trace
                                ("States.State.Current_Line_Length"
                                 & States (Row_Index)
                                     .Current_Line_Length'Image);

                              if not (Alignment_Table_Separators (Row_Index)
                                        .Last_Index
                                      < Column_Index)
                              then
                                 Max_Line_Length :=
                                   Natural'Max
                                     (@,
                                      States (Row_Index).Current_Line_Length);
                              end if;
                           end;
                        end if;
                     end loop;
                  end loop;

                  --  Finally, concatenate all buffers and add them to Result

                  Format_State.Result
                    .Text
                    .Append (States (First_Row_Index).Result.Text);
                  for Row_Index
                     in Standard."+" (First_Row_Index, 1) .. Last_Row_Index
                  loop
                     Format_State.Result.Text.Append (End_Of_Line.Text);
                     if not States (Row_Index).Result.Text.Starts_With
                              (End_Of_Line.Text)
                     then
                        Format_State.Result.Text.Append
                          (Indentation.Value.Text);
                     end if;
                     Format_State.Result.Text.Append
                       (States (Row_Index).Result.Text);
                  end loop;

                  --  Also update the Format_State.Current_Line_Length based on
                  --  the last table row.

                  Format_State.Current_Line_Length :=
                    States (Last_Row_Index).Current_Line_Length;

               end Process_Mode_Break;

               -----------------------
               -- Process_Mode_Flat --
               -----------------------

               procedure Process_Mode_Flat is
                  Remaining_Line_Length : constant Integer :=
                    Format_Options.Width - Format_State.Current_Line_Length;
                  Has_Line_Suffix       : constant Boolean :=
                    Format_State.Line_Suffix.Length > 0;

               begin
                  Prettier_Ada_Trace.Trace
                    ("On Process_Document_Command_Alignment_Table."
                     & "Process_Mode_Flat");

                  Prettier_Ada_Trace.Trace ("Checking if table fits");
                  Prettier_Ada_Trace.Trace
                    ("Remaining_Line_Length: " & Remaining_Line_Length'Image);

                  if not Fits
                           (Print_Command,
                            [],
                            Remaining_Line_Length,
                            Has_Line_Suffix,
                            Format_State.Group_Mode_Map)
                  then
                     Prettier_Ada_Trace.Trace ("Table does not fit");
                     Process_Mode_Break;

                     return;
                  end if;

                  Prettier_Ada_Trace.Trace ("Table fits");

                  declare
                     Alignment_Table_Elements   : Document_Table
                       renames Document
                                 .Bare_Document
                                 .Command
                                 .Alignment_Table_Elements;
                     Alignment_Table_Separators : Document_Table
                       renames Document
                                 .Bare_Document
                                 .Command
                                 .Alignment_Table_Separators;

                     First_Row_Index    : constant Positive :=
                       Alignment_Table_Elements.First_Index;
                     Last_Row_Index     : constant Positive :=
                       Alignment_Table_Elements.Last_Index;
                     First_Column_Index : constant Positive := 1;
                     Last_Column_Index  : constant Positive :=
                       [for Row_Elements of Alignment_Table_Elements
                        => Row_Elements.Last_Index]'Reduce (Positive'Max, 1);

                  begin
                     --  Add the last row

                     for Column_Index
                       in reverse First_Column_Index .. Last_Column_Index
                     loop
                        if Alignment_Table_Separators
                             .Constant_Reference (Last_Row_Index)
                             .Last_Index
                           >= Column_Index
                        then
                           Format_State.Print_Commands.Append
                             (Print_Command_Type'
                                (Indentation,
                                 Mode_Flat,
                                 Alignment_Table_Separators
                                   .Constant_Reference (Last_Row_Index)
                                   .Constant_Reference (Column_Index)));
                        end if;

                        if Alignment_Table_Elements
                             .Constant_Reference (Last_Row_Index)
                             .Last_Index
                           >= Column_Index
                        then
                           Format_State.Print_Commands.Append
                             (Print_Command_Type'
                                (Indentation,
                                 Mode_Flat,
                                 Alignment_Table_Elements
                                   .Constant_Reference (Last_Row_Index)
                                   .Constant_Reference (Column_Index)));
                        end if;
                     end loop;

                     --  Add the remaining rows (in reverse order) with a Line
                     --  in between.

                     for Row_Index
                       in reverse First_Row_Index
                                  .. Standard."-" (Last_Row_Index, 1)
                     loop
                        Format_State.Print_Commands.Append
                          (Print_Command_Type'
                             (Indentation,
                              Mode_Flat,
                                Prettier_Ada
                                  .Documents
                                  .Builders
                                  .Line));

                        for Column_Index
                          in reverse First_Column_Index .. Last_Column_Index
                        loop
                           if Alignment_Table_Separators
                                .Constant_Reference (Row_Index)
                                .Last_Index
                              >= Column_Index
                           then
                              Format_State.Print_Commands.Append
                                (Print_Command_Type'
                                   (Indentation,
                                    Mode_Flat,
                                    Alignment_Table_Separators
                                      .Constant_Reference (Row_Index)
                                      .Constant_Reference (Column_Index)));
                           end if;

                           if Alignment_Table_Elements
                                .Constant_Reference (Row_Index)
                                .Last_Index
                              >= Column_Index
                           then
                              Format_State.Print_Commands.Append
                                (Print_Command_Type'
                                   (Indentation,
                                    Mode_Flat,
                                    Alignment_Table_Elements
                                      .Constant_Reference (Row_Index)
                                      .Constant_Reference (Column_Index)));
                           end if;
                        end loop;
                     end loop;
                  end;
               end Process_Mode_Flat;

            begin
               Prettier_Ada_Trace.Trace
                 ("On Process_Document_Command_Alignment_Table");

               if Document.Bare_Document.Command.Alignment_Table_Must_Break
                 or Mode in Mode_Break
               then
                  Prettier_Ada_Trace.Trace ("Table must break");
                  Process_Mode_Break;

               else
                  Process_Mode_Flat;
               end if;
            end Process_Document_Command_Alignment_Table;

            --------------------------------------------------
            -- Process_Document_Command_Alignment_Separator --
            --------------------------------------------------

            procedure Process_Document_Command_Alignment_Table_Separator
            is
            begin
               Append
                 (Format_State.Result,
                  Document
                    .Bare_Document
                    .Command
                    .Alignment_Table_Separator_Text);
               Format_State.Current_Line_Length :=
                 @
                 + Document
                     .Bare_Document
                     .Command
                     .Alignment_Table_Separator_Text
                     .Display_Width;
            end Process_Document_Command_Alignment_Table_Separator;

         begin
            Format_State.Print_Commands.Delete_Last;

            case Document.Bare_Document.Kind is
               when Document_Text =>
                  Process_Document_Text;
                  Format_State.Last_Was_Hardline := False;

               when Document_List =>
                  Process_Document_List;

               when Document_Command =>
                  case Document.Bare_Document.Command.Kind is
                     when Command_Cursor =>
                        if Format_State.Printed_Cursor_Count >= 2 then
                           --  TODO: Replace this by a GNATfmt defined
                           --  exception
                           raise Program_Error;
                        end if;
                        --  TODO: How to add this command to Result?
                        --  Should Result be instead a Document_Type_Vector
                        --  with only Document_Text and Command_Cursor?
                        Format_State.Printed_Cursor_Count := @ + Natural (1);

                     when Command_Indent =>
                        Ada.Assertions.Assert
                          (Document.Bare_Document.Command.Indent_Contents
                           /= No_Document);
                        Format_State.Print_Commands.Append
                          (Print_Command_Type'
                             (Make_Indentation
                                (Indentation, Format_Options.Indentation),
                              Mode,
                              Document.Bare_Document.Command.Indent_Contents));

                     when Command_Align =>
                        Ada.Assertions.Assert
                          (Document.Bare_Document.Command.Align_Contents
                           /= No_Document);
                        Format_State.Print_Commands.Append
                          (Print_Command_Type'
                             (Make_Align
                                (Indentation,
                                 Document.Bare_Document.Command.Align_Data,
                                 Format_Options,
                                 Format_State.Current_Line_Length),
                              Mode,
                              Document.Bare_Document.Command.Align_Contents));

                     when Command_Trim =>
                        Format_State.Current_Line_Length :=
                          @ - Trim (Format_State.Result);
                        Format_State.Last_Was_Hardline := False;

                     when Command_Group =>
                        Process_Document_Command_Group;

                     when Command_Fill =>
                        Process_Document_Command_Fill;

                     when Command_If_Break =>
                        declare
                           Group_Mode : constant Mode_Kind :=
                             (if Document
                                   .Bare_Document
                                   .Command
                                   .If_Break_Group_Id
                                 /= No_Symbol
                              then
                                 (if Format_State.Group_Mode_Map.Contains
                                      (Document
                                         .Bare_Document
                                         .Command
                                         .If_Break_Group_Id)
                                  then
                                    Format_State.Group_Mode_Map.Element
                                    (Document
                                       .Bare_Document
                                       .Command
                                       .If_Break_Group_Id)
                                  else
                                    None)
                              else
                                 Mode);

                        begin
                           case Group_Mode is
                              when Mode_Break =>
                                 declare
                                    Break_Contents : Document_Type
                                      renames Document
                                                .Bare_Document
                                                .Command
                                                .Break_Contents;

                                 begin
                                    if Break_Contents /= No_Document
                                      and then
                                        not (Break_Contents.Bare_Document.Kind
                                             in Document_Text
                                             and then Break_Contents
                                                        .Bare_Document
                                                        .Text
                                                        .Text
                                                        .Is_Empty)
                                    then
                                       Format_State.Print_Commands.Append
                                         (Print_Command_Type'
                                            (Indentation,
                                             Mode,
                                             Break_Contents));
                                    end if;
                                 end;

                              when Mode_Flat =>
                                 declare
                                    Flat_Contents : Document_Type
                                      renames Document
                                                .Bare_Document
                                                .Command
                                          .Flat_Contents;

                                 begin
                                    if Flat_Contents /= No_Document
                                      and then
                                        not (Flat_Contents.Bare_Document.Kind
                                             in Document_Text
                                             and then Flat_Contents
                                                        .Bare_Document
                                                        .Text
                                                        .Text
                                                        .Is_Empty)

                                    then
                                       Format_State.Print_Commands.Append
                                         (Print_Command_Type'
                                            (Indentation,
                                             Mode,
                                             Flat_Contents));
                                    end if;
                                 end;

                              when None =>
                                 null;
                           end case;
                        end;

                     when Command_Indent_If_Break  =>
                        declare
                           Group_Mode : constant Mode_Kind :=
                             (if Document
                                   .Bare_Document
                                   .Command
                                   .Indent_If_Break_Group_Id
                                 /= No_Symbol
                                and then Format_State.Group_Mode_Map.Contains
                                           (Document
                                              .Bare_Document
                                              .Command
                                              .Indent_If_Break_Group_Id)
                              then
                                 Format_State.Group_Mode_Map.Element
                                   (Document
                                      .Bare_Document
                                      .Command
                                      .Indent_If_Break_Group_Id)
                              else
                                 Mode);

                        begin
                           case Group_Mode is
                              when Mode_Break =>
                                 declare
                                    Break_Contents : constant Document_Type :=
                                      (if Document.Bare_Document.Command.Negate
                                       then Document
                                              .Bare_Document
                                              .Command
                                              .Indent_If_Break_Contents
                                       else
                                         Prettier_Ada
                                           .Documents
                                           .Builders
                                           .Indent
                                              (Document
                                               .Bare_Document
                                               .Command
                                               .Indent_If_Break_Contents));

                                 begin
                                    Format_State.Print_Commands.Append
                                      (Print_Command_Type'
                                         (Indentation,
                                          Mode,
                                          Break_Contents));
                                 end;

                              when Mode_Flat =>
                                 declare
                                    Flat_Contents : constant Document_Type :=
                                      (if Document.Bare_Document.Command.Negate
                                       then
                                         Prettier_Ada.Documents.Builders.Indent
                                           (Document
                                              .Bare_Document
                                              .Command
                                              .Indent_If_Break_Contents)
                                       else Document
                                              .Bare_Document
                                              .Command
                                              .Indent_If_Break_Contents);

                                 begin
                                    Format_State.Print_Commands.Append
                                      (Print_Command_Type'
                                         (Indentation,
                                          Mode,
                                          Flat_Contents));
                                 end;

                              when None =>
                                 null;
                           end case;
                        end;

                     when Command_Line_Suffix =>
                        Format_State.Line_Suffix.Append
                          (Print_Command_Type'
                             (Indentation,
                              Mode,
                              Document
                                .Bare_Document
                                .Command
                                .Line_Suffix_Contents));

                     when Command_Line_Suffix_Boundary =>
                        if not Format_State.Line_Suffix.Is_Empty then
                           Format_State.Print_Commands.Append
                             (Print_Command_Type'
                                (Indentation,
                                 Mode,
                                 Prettier_Ada
                                   .Documents
                                   .Builders
                                   .Hard_Line_Without_Break_Parent));
                        end if;

                     when Command_Line =>
                        Process_Document_Command_Line;

                     when Command_Label =>
                        Format_State.Print_Commands.Append
                          (Print_Command_Type'
                             (Indentation,
                              Mode,
                              Document.Bare_Document.Command.Label_Contents));

                     when Command_Break_Parent =>
                        null;

                     when Command_Alignment_Table =>
                        Process_Document_Command_Alignment_Table;

                     when Command_Alignment_Table_Separator =>
                        Process_Document_Command_Alignment_Table_Separator;

                     when Command_Alignment_Table_End =>
                        exit;
                  end case;
            end case;

            if Format_State.Print_Commands.Length = 0
               and Format_State.Line_Suffix.Length > 0
            then
               for Suffix of reverse Format_State.Line_Suffix loop
                  Format_State.Print_Commands.Append (Suffix);
               end loop;
               Format_State.Line_Suffix.Clear;
            end if;
         end;
      end loop;

      Prettier_Ada.Documents.Builders.Reset_Document_Id;

   exception
      when others =>
         Prettier_Ada.Documents.Builders.Reset_Document_Id;
         raise;
   end Format;

   ----------
   -- Hash --
   ----------

   function Hash (Document : Document_Type) return Ada.Containers.Hash_Type is
     (Ada.Containers.Hash_Type (Document.Bare_Document.Id));

   ----------
   -- Hash --
   ----------

   function Hash (Symbol : Symbol_Type) return Ada.Containers.Hash_Type is
     (Ada.Containers.Hash_Type (Symbol));

   --------------------------
   -- Generate_Indentation --
   --------------------------

   function Generate_Indentation
     (From    : Indentation_Queue_Type;
      Data    : Indentation_Data_Type;
      Options : Indentation_Options_Type)
      return Indentation_Queue_Type
   is
      Value : Prettier_String;
      Queue : Indentation_Data_Vector := From.Queue;

      Flushed_Tabs : Natural := 0;

      Last_Tabs   : Natural := 0;
      Last_Spaces : Natural := 0;

      procedure Add_Tabs (Count : Natural);
      --  TODO: Description

      procedure Add_Spaces (Count : Natural);
      --  TODO: Description

      procedure Flush;
      --  TODO: Description

      procedure Flush_Tabs;
      --  TODO: Description

      procedure Flush_Spaces;
      --  TODO: Description

      procedure Reset_Last;
      --  TODO: Description

      --------------
      -- Add_Tabs --
      --------------

      procedure Add_Tabs (Count : Natural) is
      begin
         VSS.Strings.Append
           (Value.Text,
            VSS.Strings."*"
              (VSS.Strings.Character_Count (Count),
               VSS.Characters.Latin.Character_Tabulation));
         Value.Display_Width :=
           @ + VSS.Strings.Display_Cell_Count (Options.Width * Count);
      end Add_Tabs;

      ----------------
      -- Add_Spaces --
      ----------------

      procedure Add_Spaces (Count : Natural)
      is
      begin
         VSS.Strings.Append
           (Value.Text,
            VSS.Strings."*"
              (VSS.Strings.Character_Count (Count),
               VSS.Characters.Latin.Space));
         Value.Display_Width := @ + VSS.Strings.Display_Cell_Count (Count);
      end Add_Spaces;

      -----------
      -- Flush --
      -----------

      procedure Flush
      is
      begin
         case Options.Kind is
            when Tabs => Flush_Tabs;
            when Spaces => Flush_Spaces;
         end case;
      end Flush;

      ----------------
      -- Flush_Tabs --
      ----------------

      procedure Flush_Tabs
      is
      begin
         if Last_Tabs > 0 then
            Flushed_Tabs := @ + Last_Tabs;
            Add_Tabs (Last_Tabs);
         end if;
         Reset_Last;
      end Flush_Tabs;

      ------------------
      -- Flush_Spaces --
      ------------------

      procedure Flush_Spaces
      is
      begin
         if Last_Spaces > 0 then
            Add_Spaces (Last_Spaces);
         end if;
         Reset_Last;
      end Flush_Spaces;

      ----------------
      -- Reset_Last --
      ----------------

      procedure Reset_Last
      is
      begin
         Last_Tabs := 0;
         Last_Spaces := 0;
      end Reset_Last;

   begin
      if Data.Kind = Dedent then
         Queue.Delete_Last;

      else
         Queue.Append (Data);
      end if;

      for Part of Queue loop
         case Part.Kind is
            when Indent =>
               Flush;
               case Options.Kind is
                  when Tabs => Add_Tabs (1);
                  when Spaces => Add_Spaces (Options.Width);
               end case;

            when String_Align =>
               Flush;
               Append (Value, Part.Text);

            when Number_Align =>
               Last_Tabs := @ + Natural (1);
               Last_Spaces := @ + Part.Width;

            when Inner_Root =>
               if Part.Margin > Natural (Value.Display_Width) then
                  --  Last_Tabs and Last_Spaces are not flushed only when
                  --  Part.Kind = Number_Align. By simply adjusting Last_Spaces
                  --  based on the current line length, any previously
                  --  unflushed Last_Tabs and Last_Spaces become irrelevant,
                  --  and the number of tabs added when Part.Kind = Indent is
                  --  kept.

                  Last_Spaces := Part.Margin - Value.Display_Width;

               elsif Part.Margin < Natural (Value.Display_Width) then
                  --  This is only possible when there are String_Align before
                  --  Inner_Roots. Keep the flushed tabs and fill with spaces.
                  --
                  --  Note: If the String_Align's Texts were not whitespaces,
                  --  then it becomes unclear what the user wanted to do.
                  --  Inner root assumes that previous indentation was tabs
                  --  followed by spaces only.

                  Value.Text.Clear;
                  Value.Display_Width := 0;
                  Add_Tabs (Flushed_Tabs);
                  Last_Spaces := Part.Margin - Value.Display_Width;
               end if;

            when Dedent =>
               raise Program_Error; -- TODO: Make this a logic error
         end case;
      end loop;

      Flush_Spaces;

      return Indentation_Queue_Type'(Value, Queue, From.Root);
   end Generate_Indentation;

   ------------------------
   -- Get_Document_Parts --
   ------------------------

   function Get_Document_Parts
     (Document : Document_Type)
      return Document_Vector
   is ((case Document.Bare_Document.Kind is
           when Document_List => Document.Bare_Document.List,
           when Document_Command =>
              (case Document.Bare_Document.Command.Kind is
                  when Command_Fill =>
                     Document.Bare_Document.Command.Parts.Bare_Document.List,
                  when others => raise Program_Error),
           when others => raise Program_Error));
   --  TODO: Replace Program_Error by a Gnatfmt defined exception

   ----------------
   -- Make_Align --
   ----------------

   function Make_Align
     (From                : Indentation_Queue_Type;
      Align_Data          : Alignment_Data_Type;
      Options             : Format_Options_Type;
      Current_Line_Length : Natural)
      return Indentation_Queue_Type
   is
      use VSS.Strings;
      use VSS.Characters.Latin;

   begin
      case Align_Data.Kind is
         when None =>
            return From;

         when Width =>
            return
              Generate_Indentation
                (From,
                 Indentation_Data_Type'(Number_Align, Align_Data.N),
                 Options.Indentation);

         when Text =>
            return
              Generate_Indentation
                (From,
                 Indentation_Data_Type'
                   (String_Align,
                    To_Prettier_String (Align_Data.T)),
                 Options.Indentation);

         when Continuation_Line_Indent =>
            return
              Generate_Indentation
                (From,
                 Indentation_Data_Type'
                   (String_Align,
                    (Character_Count (Options.Indentation.Continuation)
                     * Space,
                     Display_Cell_Count (Options.Indentation.Continuation))),
                 Options.Indentation);

         when Dedent_To_Root =>
            if From.Root = null then
               return Root_Indent (Options.Indentation);
            end if;

            return From.Root.all;

         when Dedent =>
            return
              Generate_Indentation
                (From,
                 Indentation_Data_Type'(Kind => Dedent),
                 Options.Indentation);

         when Inner_Root =>
            return
              Generate_Indentation
                (From,
                 Indentation_Data_Type'
                   (Kind => Inner_Root, Margin => Current_Line_Length),
                 Options.Indentation);

         when Root =>
            declare
               Result : Indentation_Queue_Type := From;

            begin
               Result.Root := new Indentation_Queue_Type'(From);
               return Result;
            end;
      end case;
   end Make_Align;

   ----------------------
   -- Make_Indentation --
   ----------------------

   function Make_Indentation
     (From    : Indentation_Queue_Type;
      Options : Indentation_Options_Type)
      return Indentation_Queue_Type
   is (Generate_Indentation
         (From, Indentation_Data_Type'(Kind => Indent), Options));

   ---------------------
   -- New_Document_Id --
   ---------------------

   function New_Document_Id return Natural is
   begin
      return Result : constant Natural := Document_Id do
         Document_Id := @ + Natural (1);
      end return;
   end New_Document_Id;

   ----------------
   -- New_Symbol --
   ----------------

   function New_Symbol return Symbol_Type is
   begin
      return S : constant Symbol_Type := Current_Symbol do
         Current_Symbol := Current_Symbol + 1;
      end return;
   end New_Symbol;

   ----------------------
   -- Propagate_Breaks --
   ----------------------

   procedure Propagate_Breaks (Document : Document_Type) is
      Already_Visited : Document_Hashed_Set;
      Group_Stack     : Document_Vector;
      Table_Stack     : Document_Vector;

      function Propagate_Breaks_On_Enter
        (Document : Document_Type)
         return Optional_Boolean;
      --  TODO: Description

      function Propagate_Breaks_On_Exit
        (Document : Document_Type)
         return Optional_Boolean;
      --  TODO: Description

      -------------------------------
      -- Propagate_Breaks_On_Enter --
      -------------------------------

      function Propagate_Breaks_On_Enter
        (Document : Document_Type)
         return Optional_Boolean
      is

      begin
         if Document = No_Document then
            return Optional_Boolean'(Is_Set => False);
         end if;

         case Document.Bare_Document.Kind is
            when Document_Command =>
               case Document.Bare_Document.Command.Kind is
                  when Command_Break_Parent =>
                     Break_Parent_Group (Group_Stack);
                     Break_Parent_Table (Table_Stack);

                  when Command_Group =>
                     Group_Stack.Append (Document);
                     if Already_Visited.Contains (Document) then
                        return
                          Optional_Boolean'
                            (Is_Set => True, Value => False);
                     end if;
                     Already_Visited.Insert (Document);

                  when Command_Alignment_Table =>
                     Table_Stack.Append (Document);
                     if Already_Visited.Contains (Document) then
                        return
                          Optional_Boolean'
                            (Is_Set => True, Value => False);
                     end if;
                     Already_Visited.Insert (Document);

                  when others =>
                     null;
               end case;

            when others =>
               null;
         end case;

         return Optional_Boolean'(Is_Set => False);
      end Propagate_Breaks_On_Enter;

      ------------------------------
      -- Propagate_Breaks_On_Exit --
      ------------------------------

      function Propagate_Breaks_On_Exit
        (Document : Document_Type)
         return Optional_Boolean
      is
      begin
         if Document = No_Document then
            return Optional_Boolean'(Is_Set => False);
         end if;

         case Document.Bare_Document.Kind is
            when Document_Command =>
               if Document.Bare_Document.Command.Kind in Command_Group then
                  declare
                     Group : constant Document_Type :=
                       Group_Stack.Last_Element;

                  begin
                     Group_Stack.Delete_Last;
                     if Group.Bare_Document.Command.Break then
                        Break_Parent_Group (Group_Stack);
                        Break_Parent_Table (Table_Stack);
                     end if;
                  end;

               elsif Document.Bare_Document.Command.Kind
                 in Command_Alignment_Table
               then
                  declare
                     Table : constant Document_Type :=
                       Table_Stack.Last_Element;

                  begin
                     Table_Stack.Delete_Last;
                     if Table.Bare_Document.Command.Break_Parents then
                        Break_Parent_Group (Group_Stack);
                        Break_Parent_Table (Table_Stack);
                     end if;
                  end;

               end if;

            when others =>
               null;
         end case;

         return Optional_Boolean'(Is_Set => False);
      end Propagate_Breaks_On_Exit;

   begin
      Traverse_Document
        (Document                           => Document,
         On_Enter                           =>
           Propagate_Breaks_On_Enter'Access,
         On_Exit                            => Propagate_Breaks_On_Exit'Access,
         Should_Traverse_Conditional_Groups => True);
   end Propagate_Breaks;

   -----------------------
   -- Reset_Document_Id --
   -----------------------

   procedure Reset_Document_Id is
   begin
      Document_Id := Natural'First;
   end Reset_Document_Id;

   -----------------
   -- Root_Indent --
   -----------------

   function Root_Indent
     (Options : Indentation_Options_Type)
      return Indentation_Queue_Type
   is
   begin
      if Options.Offset = (Tabs => 0, Spaces => 0) then
         return
           Indentation_Queue_Type'
             (Value  => Empty_Prettier_String,
              Queue  => [],
              Root   => null);

      else
         declare
            Queue : Indentation_Data_Vector;

         begin
            Queue.Append
              ((Kind => Indent),
               Ada.Containers.Count_Type (Options.Offset.Tabs));
            Queue.Append
              (Indentation_Data_Type'
                 (Kind => String_Align,
                  Text =>
                    (VSS.Strings."*"
                       (VSS.Strings.Character_Count (Options.Offset.Spaces),
                        VSS.Characters.Latin.Space),
                     VSS.Strings.Display_Cell_Count (Options.Offset.Spaces))));

            return
              Indentation_Queue_Type'
                (Value  =>
                  (VSS.Strings."&"
                     (VSS.Strings."*"
                        (VSS.Strings.Character_Count (Options.Offset.Tabs),
                         VSS.Characters.Latin.Character_Tabulation),
                      VSS.Strings."*"
                        (VSS.Strings.Character_Count (Options.Offset.Spaces),
                         VSS.Characters.Latin.Space)),
                   VSS.Strings.Display_Cell_Count
                     (Options.Offset.Spaces
                      + Options.Offset.Tabs * Options.Width)),
                 Queue  => Queue,
                 Root   => null);
         end;
      end if;
   end Root_Indent;

   -----------
   -- Slice --
   -----------

   function Slice
     (Documents : Document_Vector;
      First     : Positive;
      Last      : Positive)
      return Document_Vector is
   begin
      return Result : Document_Vector do
         Result.Reserve_Capacity
           (Ada.Containers.Count_Type (Last - First + Natural (1)));
         for J in First .. Last loop
            Result.Append (Documents.Element (J));
         end loop;
      end return;
   end Slice;

   ----------------------
   -- To_Document_Type --
   ----------------------

   function To_Document_Type (Text : Wide_Wide_String) return Document_Type
   is
      Bare_Document : constant Bare_Document_Access :=
        new Bare_Document_Record'
          (Kind      => Document_Text,
           Ref_Count => 1,
           Text      =>
             (declare
                Text_VSS : constant VSS.Strings.Virtual_String :=
                  VSS.Strings.To_Virtual_String (Text);
              begin
                (Text_VSS, VSS.Strings.Utilities.Display_Width (Text_VSS))),
           Id        => New_Document_Id);

   begin
      return (Ada.Finalization.Controlled with Bare_Document => Bare_Document);
   end To_Document_Type;

   ------------------------
   -- To_Prettier_String --
   ------------------------

   function To_Prettier_String
     (Text : Ada.Strings.Unbounded.Unbounded_String) return Prettier_String
   is
      Text_VSS : constant VSS.Strings.Virtual_String :=
        VSS.Strings.Conversions.To_Virtual_String (Text);
   begin
      return (Text_VSS, VSS.Strings.Utilities.Display_Width (Text_VSS));
   end To_Prettier_String;

   -----------------------
   -- Traverse_Document --
   -----------------------

   procedure Traverse_Document
     (Document                           : Document_Type;
      On_Enter                           :
        access function
          (Document : Document_Type)
           return Optional_Boolean := null;
      On_Exit                            :
        access function
          (Document : Document_Type)
           return Optional_Boolean := null;
      Should_Traverse_Conditional_Groups : Boolean := False)
   is
      Doc_Stack : Document_Vector := [Document];

      Traverse_Doc_On_Exit_Stack_Marker : constant Document_Type :=
        Prettier_Ada.Documents.Builders.Text
          (Ada.Strings.Unbounded.Null_Unbounded_String);

      use type Ada.Containers.Count_Type;

   begin
      while Doc_Stack.Length > 0 loop
         declare
            Doc : constant Document_Type := Doc_Stack.Last_Element;

         begin
            Doc_Stack.Delete_Last;

            if Doc = Traverse_Doc_On_Exit_Stack_Marker then
               declare
                  Doc : constant Document_Type := Doc_Stack.Last_Element;
                  Ignore : constant Optional_Boolean := On_Exit (Doc);

               begin
                  Doc_Stack.Delete_Last;

                  goto Continue;
               end;
            end if;

            if On_Exit /= null then
               Doc_Stack.Append (Doc);
               Doc_Stack.Append (Traverse_Doc_On_Exit_Stack_Marker);
            end if;

            if On_Enter /= null then
               declare
                  On_Enter_Result : constant Optional_Boolean :=
                    On_Enter (Doc);

               begin
                  if On_Enter_Result.Is_Set
                    and then not On_Enter_Result.Value
                  then
                     goto Continue;
                  end if;
               end;
            end if;

            case Doc.Bare_Document.Kind is
               when Document_Text =>
                  null;

               when Document_List =>
                  declare
                     Documents : constant Document_Vector :=
                       Doc.Bare_Document.List;

                  begin
                     for Child_Doc of reverse Documents loop
                        Ada.Assertions.Assert (Child_Doc /= No_Document);
                        Doc_Stack.Append (Child_Doc);
                     end loop;
                  end;

               when Document_Command =>
                  case Doc.Bare_Document.Command.Kind is
                     when Command_Align =>
                        Ada.Assertions.Assert
                          (Doc.Bare_Document.Command.Align_Contents
                           /= No_Document);
                        Doc_Stack.Append
                          (Doc.Bare_Document.Command.Align_Contents);

                     when Command_Indent =>
                        Ada.Assertions.Assert
                          (Doc.Bare_Document.Command.Indent_Contents
                           /= No_Document);
                        Doc_Stack.Append
                          (Doc.Bare_Document.Command.Indent_Contents);

                     when Command_Indent_If_Break =>
                        Ada.Assertions.Assert
                          (Doc.Bare_Document.Command.Indent_If_Break_Contents
                           /= No_Document);
                        Doc_Stack.Append
                          (Doc
                             .Bare_Document
                             .Command
                             .Indent_If_Break_Contents);

                     when Command_Label =>
                        Ada.Assertions.Assert
                          (Doc.Bare_Document.Command.Label_Contents
                           /= No_Document);
                        Doc_Stack.Append
                          (Doc.Bare_Document.Command.Label_Contents);

                     when Command_Line_Suffix =>
                        Ada.Assertions.Assert
                          (Doc.Bare_Document.Command.Line_Suffix_Contents
                           /= No_Document);
                        Doc_Stack.Append
                          (Doc.Bare_Document.Command.Line_Suffix_Contents);

                     when Command_Fill =>
                        Ada.Assertions.Assert
                          (Doc.Bare_Document.Command.Parts.Bare_Document
                             /= null
                           and then Doc
                                      .Bare_Document
                                      .Command.Parts
                                      .Bare_Document
                                      .Kind = Document_List);
                        declare
                           Parts : constant Document_Vector :=
                             Doc
                               .Bare_Document
                               .Command
                               .Parts
                               .Bare_Document
                               .List;
                        begin
                           for Part of reverse Parts loop
                              Ada.Assertions.Assert (Part /= No_Document);
                              Doc_Stack.Append (Part);
                           end loop;
                        end;

                     when Command_Group =>
                        if Should_Traverse_Conditional_Groups
                          and Doc
                                .Bare_Document
                                .Command
                                .Expanded_States
                              /= No_Document
                        then
                           Ada.Assertions.Assert
                             (Doc
                                .Bare_Document
                                .Command
                                .Expanded_States
                                .Bare_Document
                                .Kind
                              = Document_List);
                           declare
                              Expanded_States : constant Document_Vector :=
                                Doc
                                  .Bare_Document
                                  .Command
                                  .Expanded_States
                                  .Bare_Document
                                  .List;

                           begin
                              for State of reverse Expanded_States loop
                                 Ada.Assertions.Assert (State /= No_Document);
                                 Doc_Stack.Append (State);
                              end loop;
                           end;

                        else
                           Ada.Assertions.Assert
                             (Doc.Bare_Document.Command.Group_Contents
                              /= No_Document);
                           Doc_Stack.Append
                             (Doc.Bare_Document.Command.Group_Contents);
                        end if;

                     when Command_If_Break =>
                        Ada.Assertions.Assert
                          (Doc.Bare_Document.Command.Flat_Contents
                           /= No_Document);
                        Ada.Assertions.Assert
                          (Doc.Bare_Document.Command.Break_Contents
                           /= No_Document);
                        Doc_Stack.Append
                          (Doc.Bare_Document.Command.Flat_Contents);
                        Doc_Stack.Append
                          (Doc.Bare_Document.Command.Break_Contents);

                     when Command_Break_Parent
                          | Command_Cursor
                          | Command_Line
                          | Command_Line_Suffix_Boundary
                          | Command_Trim
                          | Command_Alignment_Table_End
                     =>
                        null;

                     when Command_Alignment_Table =>
                        for Row_Index
                          in Doc
                               .Bare_Document
                               .Command
                               .Alignment_Table_Elements
                               .First_Index
                             .. Doc
                                  .Bare_Document
                                  .Command
                                  .Alignment_Table_Elements
                                  .Last_Index
                        loop
                           declare
                              First_Column_Index : constant Positive :=
                                Doc
                                  .Bare_Document
                                  .Command
                                  .Alignment_Table_Elements
                                  .Constant_Reference (Row_Index)
                                  .First_Index;
                              Last_Column_Index : constant Positive :=
                                Doc
                                  .Bare_Document
                                  .Command
                                  .Alignment_Table_Elements
                                  .Constant_Reference (Row_Index)
                                  .Last_Index;

                           begin
                              Doc_Stack.Append
                                (Doc
                                   .Bare_Document
                                   .Command
                                   .Alignment_Table_Elements
                                   .Constant_Reference
                                      (Row_Index)
                                   .Constant_Reference
                                      (Last_Column_Index));

                              for Column_Index
                                in reverse First_Column_Index
                                           .. Standard."-"
                                                (Last_Column_Index, 1)
                              loop
                                 Doc_Stack.Append
                                   (Doc
                                      .Bare_Document
                                      .Command
                                      .Alignment_Table_Separators
                                      .Constant_Reference
                                         (Row_Index)
                                      .Constant_Reference
                                         (Column_Index));
                                 Doc_Stack.Append
                                   (Doc
                                      .Bare_Document
                                      .Command
                                      .Alignment_Table_Elements
                                      .Constant_Reference
                                         (Row_Index)
                                      .Constant_Reference
                                         (Column_Index));
                              end loop;
                           end;
                        end loop;

                     when Command_Alignment_Table_Separator =>
                        null;
                  end case;

            end case;
         end;
         <<Continue>>
      end loop;
   end Traverse_Document;

   ----------
   -- Trim --
   ----------

   procedure Trim
     (Item : in out Prettier_String;
      Side : Trim_End)
   is
      MF : VSS.Strings.Markers.Character_Marker;
      ML : VSS.Strings.Markers.Character_Marker;

      Trim_Count : VSS.Strings.Display_Cell_Count := 0;

   begin
      if Side in Left | Both then
         declare
            J : VSS.Strings.Character_Iterators.Character_Iterator :=
              Item.Text.Before_First_Character;
            C : VSS.Characters.Virtual_Character'Base;

         begin
            while J.Forward (C) loop
               exit when C not in ' '
                                  | VSS.Characters.Latin.Character_Tabulation;

               Trim_Count := @ + 1;
            end loop;

            if J.Has_Element then
               MF := J.Marker;

            else
               Item.Text.Clear;
               Item.Display_Width := 0;

               return;
            end if;
         end;

      else
         MF := Item.Text.At_First_Character.Marker;
      end if;

      if Side in Right | Both then
         declare
            J : VSS.Strings.Character_Iterators.Character_Iterator :=
              Item.Text.After_Last_Character;
            C : VSS.Characters.Virtual_Character'Base;

         begin
            while J.Backward loop
               C := J.Element;

               exit when C not in ' '
                                  | VSS.Characters.Latin.Character_Tabulation;

               Trim_Count := @ + 1;
            end loop;

            if J.Has_Element then
               ML := J.Marker;

            else
               Item.Text.Clear;
               Item.Display_Width := 0;

               return;
            end if;
         end;

      else
         ML := Item.Text.At_Last_Character.Marker;
      end if;

      Item.Text := Item.Text.Slice (MF, ML);
      Item.Display_Width :=
        (if Trim_Count > Item.Display_Width
         then 0
         else Item.Display_Width - Trim_Count);
   end Trim;

   ----------
   -- Trim --
   ----------

   function Trim
     (Text : in out Prettier_String)
      return Natural
   is
      Initial_Display_Width : constant VSS.Strings.Display_Cell_Count :=
        Text.Display_Width;

   begin
      Trim (Text, Right);

      return Natural (Initial_Display_Width - Text.Display_Width);
   end Trim;

end Prettier_Ada.Documents.Implementation;