File size: 170,610 Bytes
4e23b01 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 | import assert from "node:assert";
import { describe, it, mock } from "node:test";
import { stripVTControlCharacters } from "node:util";
import { type AutocompleteProvider, CombinedAutocompleteProvider } from "../src/autocomplete.ts";
import { Editor, wordWrapLine } from "../src/components/editor.ts";
import { PasteBurst } from "../src/paste-burst.ts";
import type { TUI } from "../src/tui.ts";
import { TuiMainScreen } from "../src/tui-main-screen.ts";
import { visibleWidth } from "../src/utils.ts";
import { defaultEditorTheme } from "./test-themes.ts";
import { VirtualTerminal } from "./virtual-terminal.ts";
/** Create a TUI with a virtual terminal for testing */
function createTestTUI(cols = 80, rows = 24): TUI {
return new TuiMainScreen(new VirtualTerminal(cols, rows));
}
/** Standard applyCompletion that replaces prefix with item.value */
function applyCompletion(
lines: string[],
cursorLine: number,
cursorCol: number,
item: { value: string },
prefix: string,
): { lines: string[]; cursorLine: number; cursorCol: number } {
const line = lines[cursorLine] || "";
const before = line.slice(0, cursorCol - prefix.length);
const after = line.slice(cursorCol);
const newLines = [...lines];
newLines[cursorLine] = before + item.value + after;
return {
lines: newLines,
cursorLine,
cursorCol: cursorCol - prefix.length + item.value.length,
};
}
async function flushAutocomplete(): Promise<void> {
await Promise.resolve();
await new Promise((resolve) => setImmediate(resolve));
}
describe("PasteBurst", () => {
it("does not suppress Enter for slow typing", () => {
const burst = new PasteBurst();
burst.onPlainChar(0);
burst.onPlainChar(20);
assert.strictEqual(burst.shouldInsertNewlineInsteadOfSubmit(40), false);
});
it("suppresses Enter during a rapid paste-like burst", () => {
const burst = new PasteBurst();
burst.onPlainChar(0);
burst.onPlainChar(1);
burst.onPlainChar(2);
burst.onPlainChar(3);
burst.onPlainChar(4);
burst.onPlainChar(5);
burst.onPlainChar(6);
burst.onPlainChar(7);
assert.strictEqual(burst.shouldInsertNewlineInsteadOfSubmit(8), true);
});
it("keeps suppressing Enter briefly after a burst", () => {
const burst = new PasteBurst();
burst.onPlainChar(0);
burst.onPlainChar(1);
burst.onPlainChar(2);
burst.onPlainChar(3);
burst.onPlainChar(4);
burst.onPlainChar(5);
burst.onPlainChar(6);
burst.onPlainChar(7);
assert.strictEqual(burst.shouldInsertNewlineInsteadOfSubmit(100), true);
assert.strictEqual(burst.shouldInsertNewlineInsteadOfSubmit(200), false);
});
it("resets after explicit paste handling", () => {
const burst = new PasteBurst();
burst.onPlainChar(0);
burst.onPlainChar(1);
burst.onPlainChar(2);
burst.onPlainChar(3);
burst.onPlainChar(4);
burst.onPlainChar(5);
burst.onPlainChar(6);
burst.onPlainChar(7);
burst.reset();
assert.strictEqual(burst.shouldInsertNewlineInsteadOfSubmit(8), false);
});
});
describe("Editor component", () => {
describe("Prompt history navigation", () => {
it("does nothing on Up arrow when history is empty", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("\x1b[A"); // Up arrow
assert.strictEqual(editor.getText(), "");
});
it("shows most recent history entry on Up arrow when editor is empty", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("first prompt");
editor.addToHistory("second prompt");
editor.handleInput("\x1b[A"); // Up arrow
assert.strictEqual(editor.getText(), "second prompt");
});
it("cycles through history entries on repeated Up arrow", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("first");
editor.addToHistory("second");
editor.addToHistory("third");
editor.handleInput("\x1b[A"); // Up - shows "third"
assert.strictEqual(editor.getText(), "third");
editor.handleInput("\x1b[A"); // Up - shows "second"
assert.strictEqual(editor.getText(), "second");
editor.handleInput("\x1b[A"); // Up - shows "first"
assert.strictEqual(editor.getText(), "first");
editor.handleInput("\x1b[A"); // Up - stays at "first" (oldest)
assert.strictEqual(editor.getText(), "first");
});
it("jumps to start before entering history from a non-empty draft", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("prompt");
editor.setText("draft");
editor.handleInput("\x1b[D");
editor.handleInput("\x1b[D");
editor.handleInput("\x1b[A"); // Up - jumps to start before history browsing
assert.strictEqual(editor.getText(), "draft");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
editor.handleInput("\x1b[A"); // Up at start - shows "prompt"
assert.strictEqual(editor.getText(), "prompt");
editor.handleInput("\x1b[B"); // Down - restores draft
assert.strictEqual(editor.getText(), "draft");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
});
it("navigates forward through history with Down arrow", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("first");
editor.addToHistory("second");
editor.addToHistory("third");
editor.setText("draft");
// Go to oldest
editor.handleInput("\x1b[A"); // start of draft
editor.handleInput("\x1b[A"); // third
editor.handleInput("\x1b[A"); // second
editor.handleInput("\x1b[A"); // first
// Navigate back
editor.handleInput("\x1b[B"); // second
assert.strictEqual(editor.getText(), "second");
editor.handleInput("\x1b[B"); // third
assert.strictEqual(editor.getText(), "third");
editor.handleInput("\x1b[B"); // draft
assert.strictEqual(editor.getText(), "draft");
});
it("exits history mode when typing a character", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("old prompt");
editor.handleInput("\x1b[A"); // Up - shows "old prompt"
editor.handleInput("x"); // Type a character - exits history mode
assert.strictEqual(editor.getText(), "xold prompt");
});
it("exits history mode on setText", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("first");
editor.addToHistory("second");
editor.handleInput("\x1b[A"); // Up - shows "second"
editor.setText(""); // External clear
// Up should start fresh from most recent
editor.handleInput("\x1b[A");
assert.strictEqual(editor.getText(), "second");
});
it("does not add empty strings to history", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("");
editor.addToHistory(" ");
editor.addToHistory("valid");
editor.handleInput("\x1b[A");
assert.strictEqual(editor.getText(), "valid");
// Should not have more entries
editor.handleInput("\x1b[A");
assert.strictEqual(editor.getText(), "valid");
});
it("does not add consecutive duplicates to history", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("same");
editor.addToHistory("same");
editor.addToHistory("same");
editor.handleInput("\x1b[A"); // "same"
assert.strictEqual(editor.getText(), "same");
editor.handleInput("\x1b[A"); // stays at "same" (only one entry)
assert.strictEqual(editor.getText(), "same");
});
it("allows non-consecutive duplicates in history", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("first");
editor.addToHistory("second");
editor.addToHistory("first"); // Not consecutive, should be added
editor.handleInput("\x1b[A"); // "first"
assert.strictEqual(editor.getText(), "first");
editor.handleInput("\x1b[A"); // "second"
assert.strictEqual(editor.getText(), "second");
editor.handleInput("\x1b[A"); // "first" (older one)
assert.strictEqual(editor.getText(), "first");
});
it("uses cursor movement instead of history when editor has content", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("history item");
editor.setText("line1\nline2");
// Cursor is at end of line2, Up should move to line1
editor.handleInput("\x1b[A"); // Up - cursor movement
// Insert character to verify cursor position
editor.handleInput("X");
// X should be inserted in line1, not replace with history
assert.strictEqual(editor.getText(), "line1X\nline2");
});
it("limits history to 100 entries", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Add 105 entries
for (let i = 0; i < 105; i++) {
editor.addToHistory(`prompt ${i}`);
}
// Navigate to oldest
for (let i = 0; i < 100; i++) {
editor.handleInput("\x1b[A");
}
// Should be at entry 5 (oldest kept), not entry 0
assert.strictEqual(editor.getText(), "prompt 5");
// One more Up should not change anything
editor.handleInput("\x1b[A");
assert.strictEqual(editor.getText(), "prompt 5");
});
it("places cursor at start after browsing history upward", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("older entry");
editor.addToHistory("line1\nline2\nline3");
editor.handleInput("\x1b[A"); // Up - shows multi-line entry at start
assert.strictEqual(editor.getText(), "line1\nline2\nline3");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
editor.handleInput("\x1b[A"); // Up again - immediately navigates to older entry
assert.strictEqual(editor.getText(), "older entry");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
});
it("places cursor at end after browsing history downward", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("older entry");
editor.addToHistory("line1\nline2\nline3");
editor.addToHistory("newer entry");
editor.handleInput("\x1b[A"); // newer entry
editor.handleInput("\x1b[A"); // multi-line entry
editor.handleInput("\x1b[A"); // older entry
editor.handleInput("\x1b[B"); // Down - shows multi-line entry at end
assert.strictEqual(editor.getText(), "line1\nline2\nline3");
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 5 });
editor.handleInput("\x1b[B"); // Down again - immediately navigates to newer entry
assert.strictEqual(editor.getText(), "newer entry");
});
it("allows opposite-direction cursor movement within multi-line history entry", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("line1\nline2\nline3");
editor.handleInput("\x1b[A"); // Up - shows entry at start
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
editor.handleInput("\x1b[B"); // Down - cursor moves to line2
assert.strictEqual(editor.getText(), "line1\nline2\nline3");
assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 });
editor.handleInput("\x1b[A"); // Up - cursor moves back to line1
assert.strictEqual(editor.getText(), "line1\nline2\nline3");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
});
});
describe("history filter", () => {
it("visits all entries when no filter is set", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("first");
editor.addToHistory("second");
editor.handleInput("\x1b[A"); // Up - "second"
assert.strictEqual(editor.getText(), "second");
editor.handleInput("\x1b[A"); // Up - "first"
assert.strictEqual(editor.getText(), "first");
});
it("skips entries that do not pass the filter on Up", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("prompt-a");
editor.addToHistory("!cmd-b");
editor.addToHistory("prompt-c");
// history: ["prompt-c", "!cmd-b", "prompt-a"]
editor.setHistoryFilter((entry) => entry.startsWith("!"));
editor.handleInput("\x1b[A"); // Up - "!cmd-b" (skips "prompt-c")
assert.strictEqual(editor.getText(), "!cmd-b");
editor.handleInput("\x1b[A"); // Up - no more shell entries, stays
assert.strictEqual(editor.getText(), "!cmd-b");
});
it("skips entries that do not pass the filter on Down", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("!cmd-a");
editor.addToHistory("prompt-b");
editor.addToHistory("!cmd-c");
// history: ["!cmd-c", "prompt-b", "!cmd-a"]
editor.setHistoryFilter((entry) => entry.startsWith("!"));
editor.handleInput("\x1b[A"); // Up - "!cmd-c"
assert.strictEqual(editor.getText(), "!cmd-c");
editor.handleInput("\x1b[A"); // Up - "!cmd-a" (skips "prompt-b")
assert.strictEqual(editor.getText(), "!cmd-a");
editor.handleInput("\x1b[B"); // Down - "!cmd-c" (skips "prompt-b")
assert.strictEqual(editor.getText(), "!cmd-c");
});
it("does nothing on Up when no entry passes the filter", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("prompt-a");
editor.addToHistory("prompt-b");
editor.setHistoryFilter((entry) => entry.startsWith("!"));
editor.handleInput("\x1b[A");
assert.strictEqual(editor.getText(), "");
});
it("still restores the draft with a filter active", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("!cmd");
editor.setHistoryFilter((entry) => entry.startsWith("!"));
editor.setText("draft");
editor.handleInput("\x1b[D");
editor.handleInput("\x1b[D");
editor.handleInput("\x1b[A"); // to line start
editor.handleInput("\x1b[A"); // recall "!cmd"
assert.strictEqual(editor.getText(), "!cmd");
editor.handleInput("\x1b[B"); // restore draft
assert.strictEqual(editor.getText(), "draft");
});
});
describe("onRecall", () => {
it("uses the returned text instead of the stored entry", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("!cmd");
editor.onRecall = (entry) => (entry.startsWith("!") ? entry.slice(1) : undefined);
editor.handleInput("\x1b[A");
assert.strictEqual(editor.getText(), "cmd");
});
it("uses the stored entry when onRecall returns undefined", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("prompt");
editor.onRecall = () => undefined;
editor.handleInput("\x1b[A");
assert.strictEqual(editor.getText(), "prompt");
});
it("passes the navigation direction to onRecall", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("a");
editor.addToHistory("b");
const directions: Array<1 | -1> = [];
editor.onRecall = (_entry, direction) => {
directions.push(direction);
return undefined;
};
editor.handleInput("\x1b[A"); // Up -> "b" (-1)
editor.handleInput("\x1b[A"); // Up -> "a" (-1)
editor.handleInput("\x1b[B"); // Down -> "b" (1)
assert.deepStrictEqual(directions, [-1, -1, 1]);
});
});
describe("history draft host state", () => {
it("saves host state on entering browse and restores it on draft return", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("entry");
let restored: unknown;
editor.onHistoryDraftSave = () => "prompt-mode";
editor.onHistoryDraftRestore = (state) => {
restored = state;
};
editor.handleInput("\x1b[A"); // Up - recall "entry", saves host state
assert.strictEqual(editor.getText(), "entry");
assert.strictEqual(restored, undefined);
editor.handleInput("\x1b[B"); // Down - restore draft
assert.strictEqual(editor.getText(), "");
assert.strictEqual(restored, "prompt-mode");
});
it("does not restore host state when leaving browse by typing", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("entry");
let restored = false;
editor.onHistoryDraftSave = () => "state";
editor.onHistoryDraftRestore = () => {
restored = true;
};
editor.handleInput("\x1b[A"); // recall "entry"
editor.handleInput("x"); // type - exits browse without restoring draft
assert.strictEqual(restored, false);
});
it("saves and restores host state across multiple browse sessions", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("entry");
let count = 0;
editor.onHistoryDraftSave = () => "state";
editor.onHistoryDraftRestore = () => {
count++;
};
editor.handleInput("\x1b[A"); // recall
editor.handleInput("\x1b[B"); // restore draft (count=1)
editor.handleInput("\x1b[A"); // recall again
editor.handleInput("\x1b[B"); // restore draft again (count=2)
assert.strictEqual(count, 2);
});
});
describe("public state accessors", () => {
it("returns cursor position", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
editor.handleInput("a");
editor.handleInput("b");
editor.handleInput("c");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 });
editor.handleInput("\x1b[D"); // Left
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 2 });
});
it("returns lines as a defensive copy", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("a\nb");
const lines = editor.getLines();
assert.deepStrictEqual(lines, ["a", "b"]);
lines[0] = "mutated";
assert.deepStrictEqual(editor.getLines(), ["a", "b"]);
});
});
describe("Paste burst fallback", () => {
it("inserts a newline instead of submitting during a rapid burst", () => {
// Freeze the clock so the 8 synchronous keystrokes are all seen as one
// burst regardless of host speed or scheduler jitter (the production
// heuristic uses an 8ms inter-char interval, which a slow CI runner can
// exceed between synchronous calls).
mock.timers.enable({ apis: ["Date"] });
mock.timers.setTime(1000);
try {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
let submitted = false;
editor.onSubmit = () => {
submitted = true;
};
editor.handleInput("a");
editor.handleInput("b");
editor.handleInput("c");
editor.handleInput("d");
editor.handleInput("e");
editor.handleInput("f");
editor.handleInput("g");
editor.handleInput("h");
editor.handleInput("\r");
assert.strictEqual(submitted, false);
assert.strictEqual(editor.getText(), "abcdefgh\n");
} finally {
mock.timers.reset();
}
});
it("can be disabled", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme, { disablePasteBurst: true });
let submitted = "";
editor.onSubmit = (text) => {
submitted = text;
};
editor.handleInput("a");
editor.handleInput("b");
editor.handleInput("c");
editor.handleInput("\r");
assert.strictEqual(submitted, "abc");
});
it("resets when DEL backspace arrives after a burst", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
let submitted = "";
editor.onSubmit = (text) => {
submitted = text;
};
editor.handleInput("a");
editor.handleInput("b");
editor.handleInput("c");
editor.handleInput("d");
editor.handleInput("e");
editor.handleInput("f");
editor.handleInput("g");
editor.handleInput("h");
editor.handleInput("\x7f");
editor.handleInput("\r");
assert.strictEqual(submitted, "abcdefg");
});
});
describe("Backslash+Enter newline workaround", () => {
it("inserts backslash immediately (no buffering)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("\\");
// Backslash should be visible immediately, not buffered
assert.strictEqual(editor.getText(), "\\");
});
it("converts standalone backslash to newline on Enter", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("\\");
editor.handleInput("\r");
assert.strictEqual(editor.getText(), "\n");
});
it("inserts backslash normally when followed by other characters", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("\\");
editor.handleInput("x");
assert.strictEqual(editor.getText(), "\\x");
});
it("does not trigger newline when backslash is not immediately before cursor", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
let submitted = false;
editor.onSubmit = () => {
submitted = true;
};
editor.handleInput("\\");
editor.handleInput("x");
editor.handleInput("\r");
// Should submit, not insert newline (backslash not at cursor)
assert.strictEqual(submitted, true);
});
it("only removes one backslash when multiple are present", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("\\");
editor.handleInput("\\");
editor.handleInput("\\");
assert.strictEqual(editor.getText(), "\\\\\\");
editor.handleInput("\r");
// Only the last backslash is removed, newline inserted
assert.strictEqual(editor.getText(), "\\\\\n");
});
});
describe("Kitty CSI-u handling", () => {
it("ignores printable CSI-u sequences with unsupported modifiers", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("\x1b[99;9u");
assert.strictEqual(editor.getText(), "");
});
it("inserts shifted CSI-u letters as text", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("\x1b[69;2u");
assert.strictEqual(editor.getText(), "E");
});
it("inserts shifted xterm modifyOtherKeys letters as text", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("\x1b[27;2;69~");
assert.strictEqual(editor.getText(), "E");
});
});
describe("Unicode text editing behavior", () => {
it("inserts mixed ASCII, umlauts, and emojis as literal text", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("H");
editor.handleInput("e");
editor.handleInput("l");
editor.handleInput("l");
editor.handleInput("o");
editor.handleInput(" ");
editor.handleInput("ä");
editor.handleInput("ö");
editor.handleInput("ü");
editor.handleInput(" ");
editor.handleInput("😀");
const text = editor.getText();
assert.strictEqual(text, "Hello äöü 😀");
});
it("deletes single-code-unit unicode characters (umlauts) with Backspace", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("ä");
editor.handleInput("ö");
editor.handleInput("ü");
// Delete the last character (ü)
editor.handleInput("\x7f"); // Backspace
const text = editor.getText();
assert.strictEqual(text, "äö");
});
it("deletes multi-code-unit emojis with single Backspace", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("😀");
editor.handleInput("👍");
// Delete the last emoji (👍) - single backspace deletes whole grapheme cluster
editor.handleInput("\x7f"); // Backspace
const text = editor.getText();
assert.strictEqual(text, "😀");
});
it("inserts characters at the correct position after cursor movement over umlauts", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("ä");
editor.handleInput("ö");
editor.handleInput("ü");
// Move cursor left twice
editor.handleInput("\x1b[D"); // Left arrow
editor.handleInput("\x1b[D"); // Left arrow
// Insert 'x' in the middle
editor.handleInput("x");
const text = editor.getText();
assert.strictEqual(text, "äxöü");
});
it("moves cursor across multi-code-unit emojis with single arrow key", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("😀");
editor.handleInput("👍");
editor.handleInput("🎉");
// Move cursor left over last emoji (🎉) - single arrow moves over whole grapheme
editor.handleInput("\x1b[D"); // Left arrow
// Move cursor left over second emoji (👍)
editor.handleInput("\x1b[D");
// Insert 'x' between first and second emoji
editor.handleInput("x");
const text = editor.getText();
assert.strictEqual(text, "😀x👍🎉");
});
it("preserves umlauts across line breaks", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("ä");
editor.handleInput("ö");
editor.handleInput("ü");
editor.handleInput("\n"); // new line
editor.handleInput("Ä");
editor.handleInput("Ö");
editor.handleInput("Ü");
const text = editor.getText();
assert.strictEqual(text, "äöü\nÄÖÜ");
});
it("replaces the entire document with unicode text via setText (paste simulation)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Simulate bracketed paste / programmatic replacement
editor.setText("Hällö Wörld! 😀 äöüÄÖÜß");
const text = editor.getText();
assert.strictEqual(text, "Hällö Wörld! 😀 äöüÄÖÜß");
});
it("moves cursor to document start on Ctrl+A and inserts at the beginning", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("a");
editor.handleInput("b");
editor.handleInput("\x01"); // Ctrl+A (move to start)
editor.handleInput("x"); // Insert at start
const text = editor.getText();
assert.strictEqual(text, "xab");
});
it("deletes words correctly with Ctrl+W and Alt+Backspace", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Basic word deletion
editor.setText("foo bar baz");
editor.handleInput("\x17"); // Ctrl+W
assert.strictEqual(editor.getText(), "foo bar ");
// Trailing whitespace
editor.setText("foo bar ");
editor.handleInput("\x17");
assert.strictEqual(editor.getText(), "foo ");
// Punctuation run
editor.setText("foo bar...");
editor.handleInput("\x17");
assert.strictEqual(editor.getText(), "foo bar");
// ASCII punctuation inside Intl word-like segments preserves old boundaries
editor.setText("foo.bar");
editor.handleInput("\x17");
assert.strictEqual(editor.getText(), "foo.");
editor.setText("foo:bar");
editor.handleInput("\x17");
assert.strictEqual(editor.getText(), "foo:");
// Delete across multiple lines
editor.setText("line one\nline two");
editor.handleInput("\x17");
assert.strictEqual(editor.getText(), "line one\nline ");
// Delete empty line (merge)
editor.setText("line one\n");
editor.handleInput("\x17");
assert.strictEqual(editor.getText(), "line one");
// Grapheme safety (emoji as a word)
editor.setText("foo 😀😀 bar");
editor.handleInput("\x17");
assert.strictEqual(editor.getText(), "foo 😀😀 ");
editor.handleInput("\x17");
assert.strictEqual(editor.getText(), "foo ");
// Alt+Backspace
editor.setText("foo bar");
editor.handleInput("\x1b\x7f"); // Alt+Backspace (legacy)
assert.strictEqual(editor.getText(), "foo ");
});
it("navigates words correctly with Ctrl+Left/Right", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("foo bar... baz");
// Cursor at end
// Move left over baz
editor.handleInput("\x1b[1;5D"); // Ctrl+Left
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 }); // after '...'
// Move left over punctuation
editor.handleInput("\x1b[1;5D"); // Ctrl+Left
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // after 'bar'
// Move left over bar
editor.handleInput("\x1b[1;5D"); // Ctrl+Left
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 }); // after 'foo '
// Move right over bar
editor.handleInput("\x1b[1;5C"); // Ctrl+Right
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // at end of 'bar'
// Move right over punctuation run
editor.handleInput("\x1b[1;5C"); // Ctrl+Right
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 }); // after '...'
// Move right skips space and lands after baz
editor.handleInput("\x1b[1;5C"); // Ctrl+Right
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 14 }); // end of line
// Test forward from start with leading whitespace
editor.setText(" foo bar");
editor.handleInput("\x01"); // Ctrl+A to go to start
editor.handleInput("\x1b[1;5C"); // Ctrl+Right
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 }); // after 'foo'
// ASCII punctuation inside Intl word-like segments preserves old boundaries
editor.setText("foo.bar baz");
editor.handleInput("\x1b[1;5D"); // Ctrl+Left over baz
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 });
editor.handleInput("\x1b[1;5D"); // Ctrl+Left over bar
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 });
editor.handleInput("\x1b[1;5D"); // Ctrl+Left over .
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 });
editor.handleInput("\x01"); // Ctrl+A
editor.handleInput("\x1b[1;5C"); // Ctrl+Right over foo
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 });
editor.handleInput("\x1b[1;5C"); // Ctrl+Right over .
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 });
editor.handleInput("\x1b[1;5C"); // Ctrl+Right over bar
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 });
});
it("stops at fullwidth Chinese punctuation (issue #4972)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// 你好,世界 = 你好(0-2) ,(2-3) 世界(3-5)
editor.setText("你好,世界");
// Cursor at end (col 5)
// Move left over 世界
editor.handleInput("\x1b[1;5D"); // Ctrl+Left
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 }); // after ,
// Move left over ,
editor.handleInput("\x1b[1;5D"); // Ctrl+Left
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 2 }); // after 你好
// Move left over 你好
editor.handleInput("\x1b[1;5D"); // Ctrl+Left
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); // start
// Move right over 你好
editor.handleInput("\x1b[1;5C"); // Ctrl+Right
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 2 }); // after 你好
// Move right over ,
editor.handleInput("\x1b[1;5C"); // Ctrl+Right
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 }); // after ,
// Move right over 世界
editor.handleInput("\x1b[1;5C"); // Ctrl+Right
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); // end
});
it("handles mixed CJK and ASCII word movement", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// "hello你好,world世界" = hello(0-5) 你好(5-7) ,(7-8) world(8-13) 世界(13-15)
editor.setText("hello你好,world世界");
// Cursor at end (col 15)
// Move left over 世界
editor.handleInput("\x1b[1;5D"); // Ctrl+Left
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 13 }); // after 'world'
// Move left over world
editor.handleInput("\x1b[1;5D"); // Ctrl+Left
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 }); // after ,
// Move left over ,
editor.handleInput("\x1b[1;5D"); // Ctrl+Left
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // after 你好
// Move left over 你好
editor.handleInput("\x1b[1;5D"); // Ctrl+Left
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); // after 'hello'
// Move left over hello
editor.handleInput("\x1b[1;5D"); // Ctrl+Left
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); // start
// Forward from start
editor.handleInput("\x1b[1;5C"); // Ctrl+Right
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); // after 'hello'
editor.handleInput("\x1b[1;5C"); // Ctrl+Right
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // after 你好
editor.handleInput("\x1b[1;5C"); // Ctrl+Right
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 }); // after ,
editor.handleInput("\x1b[1;5C"); // Ctrl+Right
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 13 }); // after 'world'
editor.handleInput("\x1b[1;5C"); // Ctrl+Right
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 15 }); // end
});
});
describe("Scroll indicators", () => {
it("centers scroll indicators on wide borders", () => {
const width = 40;
const editor = new Editor(createTestTUI(width), defaultEditorTheme);
editor.setText(Array.from({ length: 20 }, (_, index) => `line ${index}`).join("\n"));
editor.render(width);
for (let index = 0; index < 10; index++) editor.handleInput("\x1b[A");
const lines = editor.render(width);
assert.strictEqual(stripVTControlCharacters(lines[0]!), `${"─".repeat(15)} ↑ 9 more ${"─".repeat(15)}`);
assert.strictEqual(stripVTControlCharacters(lines.at(-1)!), `${"─".repeat(15)} ↓ 4 more ${"─".repeat(15)}`);
});
it("keeps truncated scroll indicators within width and preserves their color (issue #6962)", () => {
const width = 10;
const borderColor = (text: string) => `\x1b[35m${text}\x1b[39m`;
const editor = new Editor(createTestTUI(width), { ...defaultEditorTheme, borderColor });
editor.setText(Array.from({ length: 20 }, (_, index) => `line ${index}`).join("\n"));
// Render once to initialize wrapping, then move the cursor so content remains above and below the viewport.
editor.render(width);
for (let index = 0; index < 10; index++) editor.handleInput("\x1b[A");
const lines = editor.render(width);
const topBorder = lines[0]!;
const bottomBorder = lines.at(-1)!;
assert.match(stripVTControlCharacters(topBorder), /^─── ↑/);
assert.match(stripVTControlCharacters(bottomBorder), /^─── ↓/);
assert.strictEqual(topBorder, borderColor(stripVTControlCharacters(topBorder)));
assert.strictEqual(bottomBorder, borderColor(stripVTControlCharacters(bottomBorder)));
for (const line of lines) {
assert.strictEqual(visibleWidth(line), width, `line exceeds width ${width}: ${JSON.stringify(line)}`);
}
});
});
describe("Grapheme-aware text wrapping", () => {
it("wraps lines correctly when text contains wide emojis", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const width = 20;
// ✅ is 2 columns wide, so "Hello ✅ World" is 14 columns
editor.setText("Hello ✅ World");
const lines = editor.render(width);
// All content lines (between borders) should fit within width
for (let i = 1; i < lines.length - 1; i++) {
const lineWidth = visibleWidth(lines[i]!);
assert.strictEqual(lineWidth, width, `Line ${i} has width ${lineWidth}, expected ${width}`);
}
});
it("wraps long text with emojis at correct positions", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const width = 10;
// Each ✅ is 2 columns. "✅✅✅✅✅" = 10 columns, fits exactly
// "✅✅✅✅✅✅" = 12 columns, needs wrap
editor.setText("✅✅✅✅✅✅");
const lines = editor.render(width);
// Should have 2 content lines (plus 2 border lines)
// First line: 5 emojis (10 cols), second line: 1 emoji (2 cols) + padding
for (let i = 1; i < lines.length - 1; i++) {
const lineWidth = visibleWidth(lines[i]!);
assert.strictEqual(lineWidth, width, `Line ${i} has width ${lineWidth}, expected ${width}`);
}
});
it("renders isolated Thai and Lao AM clusters without width drift", () => {
for (const text of ["ำabc", "ຳabc"]) {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const width = 8;
editor.setText(text);
for (const line of editor.render(width)) {
assert.strictEqual(visibleWidth(line), width, `line width drift for ${JSON.stringify(text)}: ${line}`);
}
}
});
it("wraps CJK characters correctly (each is 2 columns wide)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const width = 10 + 1; // +1 col reserved for cursor
// Each CJK char is 2 columns. "日本語テスト" = 6 chars = 12 columns
editor.setText("日本語テスト");
const lines = editor.render(width);
for (let i = 1; i < lines.length - 1; i++) {
const lineWidth = visibleWidth(lines[i]!);
assert.strictEqual(lineWidth, width, `Line ${i} has width ${lineWidth}, expected ${width}`);
}
// Verify content split correctly
const contentLines = lines.slice(1, -1).map((l) => stripVTControlCharacters(l).trim());
assert.strictEqual(contentLines.length, 2);
assert.strictEqual(contentLines[0], "日本語テス"); // 5 chars = 10 columns
assert.strictEqual(contentLines[1], "ト"); // 1 char = 2 columns (+ padding)
});
it("handles mixed ASCII and wide characters in wrapping", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const width = 15 + 1; // +1 col reserved for cursor
// "Test ✅ OK 日本" = 4 + 1 + 2 + 1 + 2 + 1 + 4 = 15 columns (fits in width-1=15)
editor.setText("Test ✅ OK 日本");
const lines = editor.render(width);
// Should fit in one content line
const contentLines = lines.slice(1, -1);
assert.strictEqual(contentLines.length, 1);
const lineWidth = visibleWidth(contentLines[0]!);
assert.strictEqual(lineWidth, width);
});
it("renders cursor correctly on wide characters", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const width = 20;
editor.setText("A✅B");
// Cursor should be at end (after B)
const lines = editor.render(width);
// The cursor (reverse video space) should be visible
const contentLine = lines[1]!;
assert.ok(contentLine.includes("\x1b[7m"), "Should have reverse video cursor");
// Line should still be correct width
assert.strictEqual(visibleWidth(contentLine), width);
});
it("does not exceed terminal width with emoji at wrap boundary", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const width = 11;
// "0123456789✅" = 10 ASCII + 2-wide emoji = 12 columns
// Should wrap before the emoji since it would exceed width
editor.setText("0123456789✅");
const lines = editor.render(width);
for (let i = 1; i < lines.length - 1; i++) {
const lineWidth = visibleWidth(lines[i]!);
assert.ok(lineWidth <= width, `Line ${i} has width ${lineWidth}, exceeds max ${width}`);
}
});
it("shows cursor at end of line before wrap, wraps on next char", () => {
const width = 10;
for (const paddingX of [0, 1]) {
const editor = new Editor(createTestTUI(width + paddingX), defaultEditorTheme, { paddingX });
// Type 9 chars → fills layoutWidth exactly, cursor at end on same line
for (const ch of "aaaaaaaaa") editor.handleInput(ch);
let lines = editor.render(width + paddingX);
let contentLines = lines.slice(1, -1);
assert.strictEqual(contentLines.length, 1, "Should be 1 content line before wrap");
assert.ok(contentLines[0]!.endsWith("\x1b[7m \x1b[0m"), "Cursor should be at end of line");
// Type 1 more → text wraps to second line
editor.handleInput("a");
lines = editor.render(width + paddingX);
contentLines = lines.slice(1, -1);
assert.strictEqual(contentLines.length, 2, "Should wrap to 2 content lines");
}
});
});
describe("Word wrapping", () => {
it("wraps at word boundaries instead of mid-word", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const width = 40;
editor.setText("Hello world this is a test of word wrapping functionality");
const lines = editor.render(width);
// Get content lines (between borders)
const contentLines = lines.slice(1, -1).map((l) => stripVTControlCharacters(l).trim());
// Should NOT break mid-word
// Line 1 should end with a complete word
assert.ok(!contentLines[0]!.endsWith("-"), "Line should not end with hyphen (mid-word break)");
// Each content line should be complete words
for (const line of contentLines) {
// Words at end of line should be complete (no partial words)
const lastChar = line.trimEnd().slice(-1);
assert.ok(lastChar === "" || /[\w.,!?;:]/.test(lastChar), `Line ends unexpectedly with: "${lastChar}"`);
}
});
it("does not start lines with leading whitespace after word wrap", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const width = 20;
editor.setText("Word1 Word2 Word3 Word4 Word5 Word6");
const lines = editor.render(width);
// Get content lines (between borders)
const contentLines = lines.slice(1, -1);
// No line should start with whitespace (except for padding at the end)
for (let i = 0; i < contentLines.length; i++) {
const line = stripVTControlCharacters(contentLines[i]!);
const trimmedStart = line.trimStart();
// The line should either be all padding or start with a word character
if (trimmedStart.length > 0) {
assert.ok(!/^\s+\S/.test(line.trimEnd()), `Line ${i} starts with unexpected whitespace before content`);
}
}
});
it("breaks long words (URLs) at character level", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const width = 30;
editor.setText("Check https://example.com/very/long/path/that/exceeds/width here");
const lines = editor.render(width);
// All lines should fit within width
for (let i = 1; i < lines.length - 1; i++) {
const lineWidth = visibleWidth(lines[i]!);
assert.strictEqual(lineWidth, width, `Line ${i} has width ${lineWidth}, expected ${width}`);
}
});
it("preserves multiple spaces within words on same line", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const width = 50;
editor.setText("Word1 Word2 Word3");
const lines = editor.render(width);
const contentLine = stripVTControlCharacters(lines[1]!).trim();
// Multiple spaces should be preserved
assert.ok(contentLine.includes("Word1 Word2"), "Multiple spaces should be preserved");
});
it("handles empty string", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const width = 40;
editor.setText("");
const lines = editor.render(width);
// Should have border + empty content + border
assert.strictEqual(lines.length, 3);
});
it("handles single word that fits exactly", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const width = 10 + 1; // +1 col reserved for cursor
editor.setText("1234567890");
const lines = editor.render(width);
// Should have exactly 3 lines (top border, content, bottom border)
assert.strictEqual(lines.length, 3);
const contentLine = stripVTControlCharacters(lines[1]!);
assert.ok(contentLine.includes("1234567890"), "Content should contain the word");
});
it("wraps word to next line when it ends exactly at terminal width", () => {
// "hello " (6) + "world" (5) = 11, but "world" is non-whitespace ending at width.
// Thus, wrap it to next line. The trailing space stays with "hello" on line 1
const chunks = wordWrapLine("hello world test", 11);
assert.strictEqual(chunks.length, 2);
assert.strictEqual(chunks[0]!.text, "hello ");
assert.strictEqual(chunks[1]!.text, "world test");
});
it("keeps whitespace at terminal width boundary on same line", () => {
// "hello world " is exactly 12 chars (including trailing space)
// The space at position 12 should stay on the first line
const chunks = wordWrapLine("hello world test", 12);
assert.strictEqual(chunks.length, 2);
assert.strictEqual(chunks[0]!.text, "hello world ");
assert.strictEqual(chunks[1]!.text, "test");
});
it("handles unbreakable word filling width exactly followed by space", () => {
const chunks = wordWrapLine("aaaaaaaaaaaa aaaa", 12);
assert.strictEqual(chunks.length, 2);
assert.strictEqual(chunks[0]!.text, "aaaaaaaaaaaa");
assert.strictEqual(chunks[1]!.text, " aaaa");
});
it("wraps word to next line when it fits width but not remaining space", () => {
const chunks = wordWrapLine(" aaaaaaaaaaaa", 12);
assert.strictEqual(chunks.length, 2);
assert.strictEqual(chunks[0]!.text, " ");
assert.strictEqual(chunks[1]!.text, "aaaaaaaaaaaa");
});
it("keeps word with multi-space and following word together when they fit", () => {
const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30);
assert.strictEqual(chunks.length, 2);
assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit ");
assert.strictEqual(chunks[1]!.text, "amet, consectetur");
});
it("keeps word with multi-space and following word when they fill width exactly", () => {
const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30);
assert.strictEqual(chunks.length, 2);
assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit ");
assert.strictEqual(chunks[1]!.text, "amet, consectetur");
});
it("splits when word plus multi-space plus word exceeds width", () => {
const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30);
assert.strictEqual(chunks.length, 3);
assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit ");
assert.strictEqual(chunks[1]!.text, "amet, ");
assert.strictEqual(chunks[2]!.text, "consectetur");
});
it("breaks long whitespace at line boundary", () => {
const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30);
assert.strictEqual(chunks.length, 3);
assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit ");
assert.strictEqual(chunks[1]!.text, "amet, ");
assert.strictEqual(chunks[2]!.text, "consectetur");
});
it("breaks long whitespace at line boundary 2", () => {
const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30);
assert.strictEqual(chunks.length, 3);
assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit ");
assert.strictEqual(chunks[1]!.text, "amet, ");
assert.strictEqual(chunks[2]!.text, " consectetur");
});
it("breaks whitespace spanning full lines", () => {
const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30);
assert.strictEqual(chunks.length, 3);
assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit ");
assert.strictEqual(chunks[1]!.text, "amet, ");
assert.strictEqual(chunks[2]!.text, " consectetur");
});
it("force-breaks when wide char after word boundary wrap still overflows", () => {
// " " (1) + "a"*186 (186) + "你" (2) = 189 visible width
// maxWidth = 187: backtracking to the space would leave 186 + 2 = 188 > 187,
// so the algorithm must force-break before the wide char instead.
const line = ` ${"a".repeat(186)}你`;
const chunks = wordWrapLine(line, 187);
for (const chunk of chunks) {
assert.ok(
visibleWidth(chunk.text) <= 187,
`chunk "${chunk.text.slice(0, 20)}..." has visible width ${visibleWidth(chunk.text)}, expected <= 187`,
);
}
// Verify no content is lost
const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join("");
assert.strictEqual(reconstructed, line);
});
it("splits oversized atomic segment across multiple chunks", () => {
// Simulate a paste marker wider than maxWidth by passing pre-segmented data
const marker = "[paste #1 +20 lines]"; // 21 chars
const line = `A${marker}B`;
const segments: Intl.SegmentData[] = [
{ segment: "A", index: 0, input: line },
{ segment: marker, index: 1, input: line },
{ segment: "B", index: 1 + marker.length, input: line },
];
const chunks = wordWrapLine(line, 10, segments);
// Every chunk must fit within maxWidth
for (const chunk of chunks) {
assert.ok(
visibleWidth(chunk.text) <= 10,
`chunk "${chunk.text}" has visible width ${visibleWidth(chunk.text)}, expected <= 10`,
);
}
// Verify no content is lost
const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join("");
assert.strictEqual(reconstructed, line);
});
it("splits oversized atomic segment at start of line", () => {
const marker = "[paste #1 +20 lines]"; // 21 chars
const line = `${marker}B`;
const segments: Intl.SegmentData[] = [
{ segment: marker, index: 0, input: line },
{ segment: "B", index: marker.length, input: line },
];
const chunks = wordWrapLine(line, 10, segments);
for (const chunk of chunks) {
assert.ok(visibleWidth(chunk.text) <= 10);
}
// "B" ends up on the last line (either alone or with the marker tail)
assert.strictEqual(chunks[chunks.length - 1]!.text.includes("B"), true);
const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join("");
assert.strictEqual(reconstructed, line);
});
it("splits oversized atomic segment at end of line", () => {
const marker = "[paste #1 +20 lines]"; // 21 chars
const line = `A${marker}`;
const segments: Intl.SegmentData[] = [
{ segment: "A", index: 0, input: line },
{ segment: marker, index: 1, input: line },
];
const chunks = wordWrapLine(line, 10, segments);
for (const chunk of chunks) {
assert.ok(visibleWidth(chunk.text) <= 10);
}
assert.strictEqual(chunks[0]!.text, "A");
const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join("");
assert.strictEqual(reconstructed, line);
});
it("splits consecutive oversized atomic segments", () => {
const m1 = "[paste #1 +20 lines]"; // 21 chars
const m2 = "[paste #2 +30 lines]"; // 21 chars
const line = `${m1}${m2}`;
const segments: Intl.SegmentData[] = [
{ segment: m1, index: 0, input: line },
{ segment: m2, index: m1.length, input: line },
];
const chunks = wordWrapLine(line, 10, segments);
for (const chunk of chunks) {
assert.ok(
visibleWidth(chunk.text) <= 10,
`chunk "${chunk.text}" has visible width ${visibleWidth(chunk.text)}, expected <= 10`,
);
}
const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join("");
assert.strictEqual(reconstructed, line);
});
it("wraps normally after oversized atomic segment", () => {
const marker = "[paste #1 +20 lines]"; // 21 chars
const line = `${marker} hello world`;
const segments: Intl.SegmentData[] = [
{ segment: marker, index: 0, input: line },
{ segment: " ", index: marker.length, input: line },
{ segment: "h", index: marker.length + 1, input: line },
{ segment: "e", index: marker.length + 2, input: line },
{ segment: "l", index: marker.length + 3, input: line },
{ segment: "l", index: marker.length + 4, input: line },
{ segment: "o", index: marker.length + 5, input: line },
{ segment: " ", index: marker.length + 6, input: line },
{ segment: "w", index: marker.length + 7, input: line },
{ segment: "o", index: marker.length + 8, input: line },
{ segment: "r", index: marker.length + 9, input: line },
{ segment: "l", index: marker.length + 10, input: line },
{ segment: "d", index: marker.length + 11, input: line },
];
const chunks = wordWrapLine(line, 10, segments);
// All chunks must fit
for (const chunk of chunks) {
assert.ok(
visibleWidth(chunk.text) <= 10,
`chunk "${chunk.text}" has visible width ${visibleWidth(chunk.text)}, expected <= 10`,
);
}
// Last chunk should contain "world" (normal wrapping resumes)
assert.strictEqual(chunks[chunks.length - 1]!.text, "world");
const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join("");
assert.strictEqual(reconstructed, line);
});
});
describe("Kill ring", () => {
it("Ctrl+W saves deleted text to kill ring and Ctrl+Y yanks it", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("foo bar baz");
editor.handleInput("\x17"); // Ctrl+W - deletes "baz"
assert.strictEqual(editor.getText(), "foo bar ");
// Move to beginning and yank
editor.handleInput("\x01"); // Ctrl+A
editor.handleInput("\x19"); // Ctrl+Y
assert.strictEqual(editor.getText(), "bazfoo bar ");
});
it("Ctrl+U saves deleted text to kill ring", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world");
// Move cursor to middle
editor.handleInput("\x01"); // Ctrl+A (start)
editor.handleInput("\x1b[C"); // Right 5 times
editor.handleInput("\x1b[C");
editor.handleInput("\x1b[C");
editor.handleInput("\x1b[C");
editor.handleInput("\x1b[C");
editor.handleInput("\x1b[C"); // After "hello "
editor.handleInput("\x15"); // Ctrl+U - deletes "hello "
assert.strictEqual(editor.getText(), "world");
editor.handleInput("\x19"); // Ctrl+Y
assert.strictEqual(editor.getText(), "hello world");
});
it("Ctrl+K saves deleted text to kill ring", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world");
editor.handleInput("\x01"); // Ctrl+A (start)
editor.handleInput("\x0b"); // Ctrl+K - deletes "hello world"
assert.strictEqual(editor.getText(), "");
editor.handleInput("\x19"); // Ctrl+Y
assert.strictEqual(editor.getText(), "hello world");
});
it("Ctrl+Y does nothing when kill ring is empty", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("test");
editor.handleInput("\x19"); // Ctrl+Y
assert.strictEqual(editor.getText(), "test");
});
it("Alt+Y cycles through kill ring after Ctrl+Y", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Create kill ring with multiple entries
editor.setText("first");
editor.handleInput("\x17"); // Ctrl+W - deletes "first"
editor.setText("second");
editor.handleInput("\x17"); // Ctrl+W - deletes "second"
editor.setText("third");
editor.handleInput("\x17"); // Ctrl+W - deletes "third"
// Kill ring now has: [first, second, third]
assert.strictEqual(editor.getText(), "");
editor.handleInput("\x19"); // Ctrl+Y - yanks "third" (most recent)
assert.strictEqual(editor.getText(), "third");
editor.handleInput("\x1by"); // Alt+Y - cycles to "second"
assert.strictEqual(editor.getText(), "second");
editor.handleInput("\x1by"); // Alt+Y - cycles to "first"
assert.strictEqual(editor.getText(), "first");
editor.handleInput("\x1by"); // Alt+Y - cycles back to "third"
assert.strictEqual(editor.getText(), "third");
});
it("Alt+Y does nothing if not preceded by yank", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("test");
editor.handleInput("\x17"); // Ctrl+W - deletes "test"
editor.setText("other");
// Type something to break the yank chain
editor.handleInput("x");
assert.strictEqual(editor.getText(), "otherx");
// Alt+Y should do nothing
editor.handleInput("\x1by"); // Alt+Y
assert.strictEqual(editor.getText(), "otherx");
});
it("Alt+Y does nothing if kill ring has ≤1 entry", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("only");
editor.handleInput("\x17"); // Ctrl+W - deletes "only"
editor.handleInput("\x19"); // Ctrl+Y - yanks "only"
assert.strictEqual(editor.getText(), "only");
editor.handleInput("\x1by"); // Alt+Y - should do nothing (only 1 entry)
assert.strictEqual(editor.getText(), "only");
});
it("consecutive Ctrl+W accumulates into one kill ring entry", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("one two three");
editor.handleInput("\x17"); // Ctrl+W - deletes "three"
editor.handleInput("\x17"); // Ctrl+W - deletes "two " (prepended)
editor.handleInput("\x17"); // Ctrl+W - deletes "one " (prepended)
assert.strictEqual(editor.getText(), "");
// Should be one combined entry
editor.handleInput("\x19"); // Ctrl+Y
assert.strictEqual(editor.getText(), "one two three");
});
it("Ctrl+U accumulates multiline deletes including newlines", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Start with multiline text, cursor at end
editor.setText("line1\nline2\nline3");
// Cursor is at end of line3 (line 2, col 5)
// Delete "line3"
editor.handleInput("\x15"); // Ctrl+U
assert.strictEqual(editor.getText(), "line1\nline2\n");
// Delete newline (at start of empty line 2, merges with line1)
editor.handleInput("\x15"); // Ctrl+U
assert.strictEqual(editor.getText(), "line1\nline2");
// Delete "line2"
editor.handleInput("\x15"); // Ctrl+U
assert.strictEqual(editor.getText(), "line1\n");
// Delete newline
editor.handleInput("\x15"); // Ctrl+U
assert.strictEqual(editor.getText(), "line1");
// Delete "line1"
editor.handleInput("\x15"); // Ctrl+U
assert.strictEqual(editor.getText(), "");
// All deletions accumulated into one entry: "line1\nline2\nline3"
editor.handleInput("\x19"); // Ctrl+Y
assert.strictEqual(editor.getText(), "line1\nline2\nline3");
});
it("backward deletions prepend, forward deletions append during accumulation", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("prefix|suffix");
// Position cursor at |
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); // Move right 6 times
editor.handleInput("\x0b"); // Ctrl+K - deletes "suffix" (forward)
editor.handleInput("\x0b"); // Ctrl+K - deletes "|" (forward, appended)
assert.strictEqual(editor.getText(), "prefix");
editor.handleInput("\x19"); // Ctrl+Y
assert.strictEqual(editor.getText(), "prefix|suffix");
});
it("non-delete actions break kill accumulation", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Delete "baz", then type "x" to break accumulation, then delete "x"
editor.setText("foo bar baz");
editor.handleInput("\x17"); // Ctrl+W - deletes "baz"
assert.strictEqual(editor.getText(), "foo bar ");
editor.handleInput("x"); // Typing breaks accumulation
assert.strictEqual(editor.getText(), "foo bar x");
editor.handleInput("\x17"); // Ctrl+W - deletes "x" (separate entry, not accumulated)
assert.strictEqual(editor.getText(), "foo bar ");
// Yank most recent - should be "x", not "xbaz"
editor.handleInput("\x19"); // Ctrl+Y
assert.strictEqual(editor.getText(), "foo bar x");
// Cycle to previous - should be "baz" (separate entry)
editor.handleInput("\x1by"); // Alt+Y
assert.strictEqual(editor.getText(), "foo bar baz");
});
it("non-yank actions break Alt+Y chain", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("first");
editor.handleInput("\x17"); // Ctrl+W
editor.setText("second");
editor.handleInput("\x17"); // Ctrl+W
editor.setText("");
editor.handleInput("\x19"); // Ctrl+Y - yanks "second"
assert.strictEqual(editor.getText(), "second");
editor.handleInput("x"); // Type breaks yank chain
assert.strictEqual(editor.getText(), "secondx");
editor.handleInput("\x1by"); // Alt+Y - should do nothing
assert.strictEqual(editor.getText(), "secondx");
});
it("kill ring rotation persists after cycling", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("first");
editor.handleInput("\x17"); // deletes "first"
editor.setText("second");
editor.handleInput("\x17"); // deletes "second"
editor.setText("third");
editor.handleInput("\x17"); // deletes "third"
editor.setText("");
// Ring: [first, second, third]
editor.handleInput("\x19"); // Ctrl+Y - yanks "third"
editor.handleInput("\x1by"); // Alt+Y - cycles to "second", ring rotates
// Now ring is: [third, first, second]
assert.strictEqual(editor.getText(), "second");
// Do something else
editor.handleInput("x");
editor.setText("");
// New yank should get "second" (now at end after rotation)
editor.handleInput("\x19"); // Ctrl+Y
assert.strictEqual(editor.getText(), "second");
});
it("consecutive deletions across lines coalesce into one entry", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// "1\n2\n3" with cursor at end, delete everything with Ctrl+W
editor.setText("1\n2\n3");
editor.handleInput("\x17"); // Ctrl+W - deletes "3"
assert.strictEqual(editor.getText(), "1\n2\n");
editor.handleInput("\x17"); // Ctrl+W - deletes newline (merge with prev line)
assert.strictEqual(editor.getText(), "1\n2");
editor.handleInput("\x17"); // Ctrl+W - deletes "2"
assert.strictEqual(editor.getText(), "1\n");
editor.handleInput("\x17"); // Ctrl+W - deletes newline
assert.strictEqual(editor.getText(), "1");
editor.handleInput("\x17"); // Ctrl+W - deletes "1"
assert.strictEqual(editor.getText(), "");
// All deletions should have accumulated into one entry
editor.handleInput("\x19"); // Ctrl+Y
assert.strictEqual(editor.getText(), "1\n2\n3");
});
it("Ctrl+K at line end deletes newline and coalesces", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// "ab" on line 1, "cd" on line 2, cursor at end of line 1
editor.setText("");
editor.handleInput("a");
editor.handleInput("b");
editor.handleInput("\n");
editor.handleInput("c");
editor.handleInput("d");
// Move to end of first line
editor.handleInput("\x1b[A"); // Up arrow
editor.handleInput("\x05"); // Ctrl+E - end of line
// Now at end of "ab", Ctrl+K should delete newline (merge with "cd")
editor.handleInput("\x0b"); // Ctrl+K - deletes newline
assert.strictEqual(editor.getText(), "abcd");
// Continue deleting
editor.handleInput("\x0b"); // Ctrl+K - deletes "cd"
assert.strictEqual(editor.getText(), "ab");
// Both deletions should accumulate
editor.handleInput("\x19"); // Ctrl+Y
assert.strictEqual(editor.getText(), "ab\ncd");
});
it("handles yank in middle of text", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("word");
editor.handleInput("\x17"); // Ctrl+W - deletes "word"
editor.setText("hello world");
// Move to middle (after "hello ")
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C");
editor.handleInput("\x19"); // Ctrl+Y
assert.strictEqual(editor.getText(), "hello wordworld");
});
it("handles yank-pop in middle of text", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Create two kill ring entries
editor.setText("FIRST");
editor.handleInput("\x17"); // Ctrl+W - deletes "FIRST"
editor.setText("SECOND");
editor.handleInput("\x17"); // Ctrl+W - deletes "SECOND"
// Ring: ["FIRST", "SECOND"]
// Set up "hello world" and position cursor after "hello "
editor.setText("hello world");
editor.handleInput("\x01"); // Ctrl+A - go to start of line
for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); // Move right 6
// Yank "SECOND" in the middle
editor.handleInput("\x19"); // Ctrl+Y
assert.strictEqual(editor.getText(), "hello SECONDworld");
// Yank-pop replaces "SECOND" with "FIRST"
editor.handleInput("\x1by"); // Alt+Y
assert.strictEqual(editor.getText(), "hello FIRSTworld");
});
it("multiline yank and yank-pop in middle of text", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Create single-line entry
editor.setText("SINGLE");
editor.handleInput("\x17"); // Ctrl+W - deletes "SINGLE"
// Create multiline entry via consecutive Ctrl+U
editor.setText("A\nB");
editor.handleInput("\x15"); // Ctrl+U - deletes "B"
editor.handleInput("\x15"); // Ctrl+U - deletes newline
editor.handleInput("\x15"); // Ctrl+U - deletes "A"
// Ring: ["SINGLE", "A\nB"]
// Insert in middle of "hello world"
editor.setText("hello world");
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C");
// Yank multiline "A\nB"
editor.handleInput("\x19"); // Ctrl+Y
assert.strictEqual(editor.getText(), "hello A\nBworld");
// Yank-pop replaces with "SINGLE"
editor.handleInput("\x1by"); // Alt+Y
assert.strictEqual(editor.getText(), "hello SINGLEworld");
});
it("Alt+D deletes word forward and saves to kill ring", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world test");
editor.handleInput("\x01"); // Ctrl+A - go to start
editor.handleInput("\x1bd"); // Alt+D - deletes "hello"
assert.strictEqual(editor.getText(), " world test");
editor.handleInput("\x1bd"); // Alt+D - deletes " world" (skips whitespace, then word)
assert.strictEqual(editor.getText(), " test");
// Yank should get accumulated text
editor.handleInput("\x19"); // Ctrl+Y
assert.strictEqual(editor.getText(), "hello world test");
});
it("Alt+D at end of line deletes newline", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("line1\nline2");
// Move to start of document, then to end of first line
editor.handleInput("\x1b[A"); // Up arrow - go to first line
editor.handleInput("\x05"); // Ctrl+E - end of line
editor.handleInput("\x1bd"); // Alt+D - deletes newline (merges lines)
assert.strictEqual(editor.getText(), "line1line2");
editor.handleInput("\x19"); // Ctrl+Y
assert.strictEqual(editor.getText(), "line1\nline2");
});
});
describe("Undo", () => {
it("does nothing when undo stack is empty", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "");
});
it("coalesces consecutive word characters into one undo unit", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("h");
editor.handleInput("e");
editor.handleInput("l");
editor.handleInput("l");
editor.handleInput("o");
editor.handleInput(" ");
editor.handleInput("w");
editor.handleInput("o");
editor.handleInput("r");
editor.handleInput("l");
editor.handleInput("d");
assert.strictEqual(editor.getText(), "hello world");
// Undo removes " world" (space captured state before it, so we restore to "hello")
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "hello");
// Undo removes "hello"
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "");
});
it("undoes spaces one at a time", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("h");
editor.handleInput("e");
editor.handleInput("l");
editor.handleInput("l");
editor.handleInput("o");
editor.handleInput(" ");
editor.handleInput(" ");
assert.strictEqual(editor.getText(), "hello ");
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) - removes second " "
assert.strictEqual(editor.getText(), "hello ");
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) - removes first " "
assert.strictEqual(editor.getText(), "hello");
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) - removes "hello"
assert.strictEqual(editor.getText(), "");
});
it("undoes newlines and signals next word to capture state", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("h");
editor.handleInput("e");
editor.handleInput("l");
editor.handleInput("l");
editor.handleInput("o");
editor.handleInput("\n");
editor.handleInput("w");
editor.handleInput("o");
editor.handleInput("r");
editor.handleInput("l");
editor.handleInput("d");
assert.strictEqual(editor.getText(), "hello\nworld");
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "hello\n");
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "hello");
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "");
});
it("undoes backspace", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("h");
editor.handleInput("e");
editor.handleInput("l");
editor.handleInput("l");
editor.handleInput("o");
editor.handleInput("\x7f"); // Backspace
assert.strictEqual(editor.getText(), "hell");
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "hello");
});
it("undoes forward delete", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("h");
editor.handleInput("e");
editor.handleInput("l");
editor.handleInput("l");
editor.handleInput("o");
editor.handleInput("\x01"); // Ctrl+A - go to start
editor.handleInput("\x1b[C"); // Right arrow
editor.handleInput("\x1b[3~"); // Delete key
assert.strictEqual(editor.getText(), "hllo");
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "hello");
});
it("undoes Ctrl+W (delete word backward)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("h");
editor.handleInput("e");
editor.handleInput("l");
editor.handleInput("l");
editor.handleInput("o");
editor.handleInput(" ");
editor.handleInput("w");
editor.handleInput("o");
editor.handleInput("r");
editor.handleInput("l");
editor.handleInput("d");
assert.strictEqual(editor.getText(), "hello world");
editor.handleInput("\x17"); // Ctrl+W
assert.strictEqual(editor.getText(), "hello ");
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "hello world");
});
it("undoes Ctrl+K (delete to line end)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("h");
editor.handleInput("e");
editor.handleInput("l");
editor.handleInput("l");
editor.handleInput("o");
editor.handleInput(" ");
editor.handleInput("w");
editor.handleInput("o");
editor.handleInput("r");
editor.handleInput("l");
editor.handleInput("d");
editor.handleInput("\x01"); // Ctrl+A - go to start
for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); // Move right 6 times
editor.handleInput("\x0b"); // Ctrl+K
assert.strictEqual(editor.getText(), "hello ");
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "hello world");
editor.handleInput("|");
assert.strictEqual(editor.getText(), "hello |world");
});
it("undoes Ctrl+U (delete to line start)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("h");
editor.handleInput("e");
editor.handleInput("l");
editor.handleInput("l");
editor.handleInput("o");
editor.handleInput(" ");
editor.handleInput("w");
editor.handleInput("o");
editor.handleInput("r");
editor.handleInput("l");
editor.handleInput("d");
editor.handleInput("\x01"); // Ctrl+A - go to start
for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); // Move right 6 times
editor.handleInput("\x15"); // Ctrl+U
assert.strictEqual(editor.getText(), "world");
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "hello world");
});
it("undoes yank", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("h");
editor.handleInput("e");
editor.handleInput("l");
editor.handleInput("l");
editor.handleInput("o");
editor.handleInput(" ");
editor.handleInput("\x17"); // Ctrl+W - delete "hello "
editor.handleInput("\x19"); // Ctrl+Y - yank
assert.strictEqual(editor.getText(), "hello ");
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "");
});
it("undoes single-line paste atomically", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world");
editor.handleInput("\x01"); // Ctrl+A - go to start
for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C"); // Move right 5 (after "hello", before space)
// Simulate bracketed paste of "beep boop"
editor.handleInput("\x1b[200~beep boop\x1b[201~");
assert.strictEqual(editor.getText(), "hellobeep boop world");
// Single undo should restore entire pre-paste state
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "hello world");
editor.handleInput("|");
assert.strictEqual(editor.getText(), "hello| world");
});
it("does not trigger autocomplete during single-line paste", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
let suggestionCalls = 0;
const mockProvider: AutocompleteProvider = {
getSuggestions: async () => {
suggestionCalls += 1;
return null;
},
applyCompletion,
};
editor.setAutocompleteProvider(mockProvider);
editor.handleInput("\x1b[200~look at @node_modules/react/index.js please\x1b[201~");
assert.strictEqual(editor.getText(), "look at @node_modules/react/index.js please");
assert.strictEqual(suggestionCalls, 0);
assert.strictEqual(editor.isShowingAutocomplete(), false);
});
it("decodes CSI-u Ctrl+letter sequences inside bracketed paste (tmux popup)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// tmux popups with extended-keys-format=csi-u re-encode \n in pastes as
// \x1b[106;5u (Ctrl+J). Without decoding, the per-char filter strips ESC
// and leaks "[106;5u" between lines. See issue #3599.
editor.handleInput("\x1b[200~line1\x1b[106;5uline2\x1b[106;5uline3\x1b[201~");
assert.strictEqual(editor.getText(), "line1\nline2\nline3");
});
it("undoes multi-line paste atomically", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world");
editor.handleInput("\x01"); // Ctrl+A - go to start
for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C"); // Move right 5 (after "hello", before space)
// Simulate bracketed paste of multi-line text
editor.handleInput("\x1b[200~line1\nline2\nline3\x1b[201~");
assert.strictEqual(editor.getText(), "helloline1\nline2\nline3 world");
// Single undo should restore entire pre-paste state
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "hello world");
editor.handleInput("|");
assert.strictEqual(editor.getText(), "hello| world");
});
it("undoes insertTextAtCursor atomically", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world");
editor.handleInput("\x01"); // Ctrl+A - go to start
for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C"); // Move right 5 (after "hello", before space)
// Programmatic insertion (e.g., clipboard image path)
editor.insertTextAtCursor("/tmp/image.png");
assert.strictEqual(editor.getText(), "hello/tmp/image.png world");
// Single undo should restore entire pre-insert state
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "hello world");
editor.handleInput("|");
assert.strictEqual(editor.getText(), "hello| world");
});
it("insertTextAtCursor handles multiline text", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world");
editor.handleInput("\x01"); // Ctrl+A - go to start
for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C"); // Move right 5 (after "hello", before space)
// Insert multiline text
editor.insertTextAtCursor("line1\nline2\nline3");
assert.strictEqual(editor.getText(), "helloline1\nline2\nline3 world");
// Cursor should be at end of inserted text (after "line3", before " world")
const cursor = editor.getCursor();
assert.strictEqual(cursor.line, 2);
assert.strictEqual(cursor.col, 5); // "line3".length
// Single undo should restore entire pre-insert state
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "hello world");
});
it("insertTextAtCursor normalizes CRLF and CR line endings", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("");
// Insert text with CRLF
editor.insertTextAtCursor("a\r\nb\r\nc");
assert.strictEqual(editor.getText(), "a\nb\nc");
editor.handleInput("\x1b[45;5u"); // Undo
assert.strictEqual(editor.getText(), "");
// Insert text with CR only
editor.insertTextAtCursor("x\ry\rz");
assert.strictEqual(editor.getText(), "x\ny\nz");
});
it("undoes setText to empty string", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("h");
editor.handleInput("e");
editor.handleInput("l");
editor.handleInput("l");
editor.handleInput("o");
editor.handleInput(" ");
editor.handleInput("w");
editor.handleInput("o");
editor.handleInput("r");
editor.handleInput("l");
editor.handleInput("d");
assert.strictEqual(editor.getText(), "hello world");
editor.setText("");
assert.strictEqual(editor.getText(), "");
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "hello world");
});
it("clears undo stack on submit", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
let submitted = "";
editor.onSubmit = (text) => {
submitted = text;
};
editor.handleInput("h");
editor.handleInput("e");
editor.handleInput("l");
editor.handleInput("l");
editor.handleInput("o");
editor.handleInput("\r"); // Enter - submit
assert.strictEqual(submitted, "hello");
assert.strictEqual(editor.getText(), "");
// Undo should do nothing - stack was cleared
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "");
});
it("exits history browsing mode on undo", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Add "hello" to history
editor.addToHistory("hello");
assert.strictEqual(editor.getText(), "");
// Type "world"
editor.handleInput("w");
editor.handleInput("o");
editor.handleInput("r");
editor.handleInput("l");
editor.handleInput("d");
assert.strictEqual(editor.getText(), "world");
// Ctrl+W - delete word
editor.handleInput("\x17"); // Ctrl+W
assert.strictEqual(editor.getText(), "");
// Press Up - enter history browsing, shows "hello"
editor.handleInput("\x1b[A"); // Up arrow
assert.strictEqual(editor.getText(), "hello");
// Undo should restore to "" (state before entering history browsing)
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "");
// Undo again should restore to "world" (state before Ctrl+W)
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "world");
});
it("undo restores to pre-history state even after multiple history navigations", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Add history entries
editor.addToHistory("first");
editor.addToHistory("second");
editor.addToHistory("third");
// Type something
editor.handleInput("c");
editor.handleInput("u");
editor.handleInput("r");
editor.handleInput("r");
editor.handleInput("e");
editor.handleInput("n");
editor.handleInput("t");
assert.strictEqual(editor.getText(), "current");
// Clear editor
editor.handleInput("\x17"); // Ctrl+W
assert.strictEqual(editor.getText(), "");
// Navigate through history multiple times
editor.handleInput("\x1b[A"); // Up - "third"
assert.strictEqual(editor.getText(), "third");
editor.handleInput("\x1b[A"); // Up - "second"
assert.strictEqual(editor.getText(), "second");
editor.handleInput("\x1b[A"); // Up - "first"
assert.strictEqual(editor.getText(), "first");
// Undo should go back to "" (state before we started browsing), not intermediate states
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "");
// Another undo goes back to "current"
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "current");
});
it("cursor movement starts new undo unit", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("h");
editor.handleInput("e");
editor.handleInput("l");
editor.handleInput("l");
editor.handleInput("o");
editor.handleInput(" ");
editor.handleInput("w");
editor.handleInput("o");
editor.handleInput("r");
editor.handleInput("l");
editor.handleInput("d");
assert.strictEqual(editor.getText(), "hello world");
// Move cursor left 5 (to after "hello ")
for (let i = 0; i < 5; i++) editor.handleInput("\x1b[D");
// Type "lol" in the middle
editor.handleInput("l");
editor.handleInput("o");
editor.handleInput("l");
assert.strictEqual(editor.getText(), "hello lolworld");
// Undo should restore to "hello world" (before inserting "lol")
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "hello world");
editor.handleInput("|");
assert.strictEqual(editor.getText(), "hello |world");
});
it("no-op delete operations do not push undo snapshots", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("h");
editor.handleInput("e");
editor.handleInput("l");
editor.handleInput("l");
editor.handleInput("o");
assert.strictEqual(editor.getText(), "hello");
// Delete word on empty - multiple times (should be no-ops)
editor.handleInput("\x17"); // Ctrl+W - deletes "hello"
assert.strictEqual(editor.getText(), "");
editor.handleInput("\x17"); // Ctrl+W - no-op (nothing to delete)
editor.handleInput("\x17"); // Ctrl+W - no-op
// Single undo should restore "hello"
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "hello");
});
it("undoes autocomplete", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Create a mock autocomplete provider
const mockProvider: AutocompleteProvider = {
getSuggestions: async (lines, _cursorLine, cursorCol) => {
const text = lines[0] || "";
const prefix = text.slice(0, cursorCol);
if (prefix === "di") {
return {
items: [{ value: "dist/", label: "dist/" }],
prefix: "di",
};
}
return null;
},
applyCompletion,
};
editor.setAutocompleteProvider(mockProvider);
// Type "di"
editor.handleInput("d");
editor.handleInput("i");
assert.strictEqual(editor.getText(), "di");
// Press Tab to trigger autocomplete
editor.handleInput("\t");
await flushAutocomplete();
assert.strictEqual(editor.getText(), "dist/");
assert.strictEqual(editor.isShowingAutocomplete(), false);
// Undo should restore to "di"
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "di");
});
});
describe("Autocomplete", () => {
it("auto-applies single force-file suggestion without showing menu", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const mockProvider: AutocompleteProvider = {
getSuggestions: async (lines, _cursorLine, cursorCol, options) => {
if (!options.force) {
return null;
}
const text = lines[0] || "";
const prefix = text.slice(0, cursorCol);
if (prefix === "Work") {
return {
items: [{ value: "Workspace/", label: "Workspace/" }],
prefix: "Work",
};
}
return null;
},
applyCompletion,
};
editor.setAutocompleteProvider(mockProvider);
// Type "Work"
editor.handleInput("W");
editor.handleInput("o");
editor.handleInput("r");
editor.handleInput("k");
assert.strictEqual(editor.getText(), "Work");
// Press Tab - should auto-apply without showing menu
editor.handleInput("\t");
await flushAutocomplete();
assert.strictEqual(editor.getText(), "Workspace/");
assert.strictEqual(editor.isShowingAutocomplete(), false);
// Undo should restore to "Work"
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "Work");
});
it("shows menu when force-file has multiple suggestions", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const mockProvider: AutocompleteProvider = {
getSuggestions: async (lines, _cursorLine, cursorCol, options) => {
if (!options.force) {
return null;
}
const text = lines[0] || "";
const prefix = text.slice(0, cursorCol);
if (prefix === "src") {
return {
items: [
{ value: "src/", label: "src/" },
{ value: "src.txt", label: "src.txt" },
],
prefix: "src",
};
}
return null;
},
applyCompletion,
};
editor.setAutocompleteProvider(mockProvider);
// Type "src"
editor.handleInput("s");
editor.handleInput("r");
editor.handleInput("c");
assert.strictEqual(editor.getText(), "src");
// Press Tab - should show menu because there are multiple suggestions
editor.handleInput("\t");
await flushAutocomplete();
assert.strictEqual(editor.getText(), "src");
assert.strictEqual(editor.isShowingAutocomplete(), true);
// Press Tab again to accept first suggestion
editor.handleInput("\t");
assert.strictEqual(editor.getText(), "src/");
assert.strictEqual(editor.isShowingAutocomplete(), false);
});
it("keeps suggestions open when typing in force mode (Tab-triggered)", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const allFiles = [
{ value: "readme.md", label: "readme.md" },
{ value: "package.json", label: "package.json" },
{ value: "src/", label: "src/" },
{ value: "dist/", label: "dist/" },
];
const mockProvider: AutocompleteProvider = {
getSuggestions: async (lines, _cursorLine, cursorCol, options) => {
const text = lines[0] || "";
const prefix = text.slice(0, cursorCol);
const shouldMatch = options.force || prefix.includes("/") || prefix.startsWith(".");
if (!shouldMatch) {
return null;
}
const filtered = allFiles.filter((f) => f.value.toLowerCase().startsWith(prefix.toLowerCase()));
if (filtered.length > 0) {
return { items: filtered, prefix };
}
return null;
},
applyCompletion,
};
editor.setAutocompleteProvider(mockProvider);
// Press Tab on empty prompt - should show all files (force mode)
editor.handleInput("\t");
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), true);
// Type "r" - should narrow to "readme.md" (force mode keeps suggestions open)
editor.handleInput("r");
await flushAutocomplete();
assert.strictEqual(editor.getText(), "r");
assert.strictEqual(editor.isShowingAutocomplete(), true);
// Type "e" - should still show "readme.md"
editor.handleInput("e");
await flushAutocomplete();
assert.strictEqual(editor.getText(), "re");
assert.strictEqual(editor.isShowingAutocomplete(), true);
// Accept with Tab
editor.handleInput("\t");
assert.strictEqual(editor.getText(), "readme.md");
assert.strictEqual(editor.isShowingAutocomplete(), false);
});
it("debounces @ autocomplete while typing", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
let suggestionCalls = 0;
const mockProvider: AutocompleteProvider = {
getSuggestions: async (lines, _cursorLine, cursorCol) => {
suggestionCalls += 1;
const text = (lines[0] || "").slice(0, cursorCol);
return {
items: [{ value: "@main.ts", label: "main.ts" }],
prefix: text,
};
},
applyCompletion,
};
editor.setAutocompleteProvider(mockProvider);
editor.handleInput("@");
editor.handleInput("m");
editor.handleInput("a");
editor.handleInput("i");
assert.strictEqual(suggestionCalls, 0);
assert.strictEqual(editor.isShowingAutocomplete(), false);
await new Promise((resolve) => setTimeout(resolve, 50));
await flushAutocomplete();
assert.strictEqual(suggestionCalls, 1);
assert.strictEqual(editor.isShowingAutocomplete(), true);
});
it("re-queries the autocomplete picker when the cursor moves back into the command name", async () => {
// Regression for earendil-works/pi#5496: arrowing left out of a slash
// command's argument region must re-query the picker, not leave the
// stale argument list showing. Before the fix, moveCursor() never
// called updateAutocomplete(), so `/cmd ` (argument menu) + Left kept
// displaying the arguments against a `/cmd` prefix — and a Tab there
// would concatenate the stale suggestion onto the partial command name.
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const mockProvider: AutocompleteProvider = {
getSuggestions: async (lines, _cursorLine, cursorCol) => {
const before = (lines[0] || "").slice(0, cursorCol);
if (!before.startsWith("/")) return null;
// Past the command name (a space before the cursor): offer arguments.
if (before.includes(" ")) {
return {
items: [
{ value: "repo", label: "repo" },
{ value: "message", label: "message" },
{ value: "help", label: "help" },
],
prefix: before.slice(before.indexOf(" ") + 1),
};
}
// Inside the command name: offer the command name only.
return { items: [{ value: "cmd", label: "cmd" }], prefix: before };
},
applyCompletion,
};
editor.setAutocompleteProvider(mockProvider);
// Type `/cmd ` so the picker ends up showing the argument list.
for (const ch of "/cmd ") {
editor.handleInput(ch);
await flushAutocomplete();
}
assert.strictEqual(editor.getText(), "/cmd ");
assert.strictEqual(editor.isShowingAutocomplete(), true);
const atArg = editor
.render(80)
.map((l) => stripVTControlCharacters(l))
.join("\n");
assert.ok(atArg.includes("repo"), "argument menu should be visible at `/cmd `");
// Arrow Left back into the command name (`/cmd`).
editor.handleInput("\x1b[D");
await flushAutocomplete();
// The picker must have re-queried: the stale argument items are gone
// (replaced by the command-name suggestion, or the picker closed).
const afterMove = editor
.render(80)
.map((l) => stripVTControlCharacters(l))
.join("\n");
assert.ok(!afterMove.includes("repo"), "stale argument menu must not survive the cursor move");
assert.ok(!afterMove.includes("message"), "stale argument menu must not survive the cursor move");
});
it("debounces # autocomplete while typing", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
let suggestionCalls = 0;
const mockProvider: AutocompleteProvider = {
getSuggestions: async (lines, _cursorLine, cursorCol) => {
suggestionCalls += 1;
const text = (lines[0] || "").slice(0, cursorCol);
return {
items: [{ value: "#2983", label: "#2983" }],
prefix: text,
};
},
applyCompletion,
};
editor.setAutocompleteProvider(mockProvider);
editor.handleInput("#");
editor.handleInput("2");
editor.handleInput("9");
editor.handleInput("8");
assert.strictEqual(suggestionCalls, 0);
assert.strictEqual(editor.isShowingAutocomplete(), false);
await new Promise((resolve) => setTimeout(resolve, 50));
await flushAutocomplete();
assert.strictEqual(suggestionCalls, 1);
assert.strictEqual(editor.isShowingAutocomplete(), true);
});
it("debounces custom triggerCharacters autocomplete while typing", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
let suggestionCalls = 0;
editor.setAutocompleteProvider({
triggerCharacters: ["$"],
getSuggestions: async (lines, _cursorLine, cursorCol) => {
suggestionCalls += 1;
const prefix = (lines[0] || "").slice(0, cursorCol);
return { items: [{ value: "$skill-name", label: "skill-name" }], prefix };
},
applyCompletion,
});
editor.handleInput("$");
editor.handleInput("s");
editor.handleInput("k");
assert.strictEqual(suggestionCalls, 0);
await new Promise((resolve) => setTimeout(resolve, 50));
await flushAutocomplete();
assert.strictEqual(suggestionCalls, 1);
assert.strictEqual(editor.isShowingAutocomplete(), true);
});
it("resets custom triggerCharacters when provider changes", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
let suggestionCalls = 0;
editor.setAutocompleteProvider({
triggerCharacters: ["$"],
getSuggestions: async () => ({ items: [{ value: "$skill-name", label: "skill-name" }], prefix: "$" }),
applyCompletion,
});
editor.setAutocompleteProvider({
getSuggestions: async () => {
suggestionCalls += 1;
return { items: [{ value: "$skill-name", label: "skill-name" }], prefix: "$" };
},
applyCompletion,
});
editor.handleInput("$");
editor.handleInput("s");
await new Promise((resolve) => setTimeout(resolve, 50));
await flushAutocomplete();
assert.strictEqual(suggestionCalls, 0);
assert.strictEqual(editor.isShowingAutocomplete(), false);
});
it("aborts active @ autocomplete when typing continues", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
let aborts = 0;
const mockProvider: AutocompleteProvider = {
getSuggestions: async (_lines, _cursorLine, _cursorCol, options) => {
return await new Promise((resolve) => {
const timeout = setTimeout(() => {
resolve({ items: [{ value: "@main.ts", label: "main.ts" }], prefix: "@main" });
}, 500);
options.signal.addEventListener(
"abort",
() => {
aborts += 1;
clearTimeout(timeout);
resolve(null);
},
{ once: true },
);
});
},
applyCompletion,
};
editor.setAutocompleteProvider(mockProvider);
editor.handleInput("@");
editor.handleInput("m");
editor.handleInput("a");
editor.handleInput("i");
await new Promise((resolve) => setTimeout(resolve, 250));
editor.handleInput("n");
await new Promise((resolve) => setTimeout(resolve, 50));
assert.strictEqual(aborts, 1);
});
it("hides autocomplete when backspacing slash command to empty", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Mock provider with slash commands
const mockProvider: AutocompleteProvider = {
getSuggestions: async (lines, _cursorLine, cursorCol) => {
const text = lines[0] || "";
const prefix = text.slice(0, cursorCol);
// Only return slash command suggestions when line starts with /
if (prefix.startsWith("/")) {
const commands = [
{ value: "/model", label: "model", description: "Change model" },
{ value: "/help", label: "help", description: "Show help" },
];
const query = prefix.slice(1); // Remove leading /
const filtered = commands.filter((c) => c.value.startsWith(query));
if (filtered.length > 0) {
return { items: filtered, prefix };
}
}
return null;
},
applyCompletion,
};
editor.setAutocompleteProvider(mockProvider);
// Type "/" - should show slash command suggestions
editor.handleInput("/");
await flushAutocomplete();
assert.strictEqual(editor.getText(), "/");
assert.strictEqual(editor.isShowingAutocomplete(), true);
// Backspace to delete "/" - should hide autocomplete completely
editor.handleInput("\x7f"); // Backspace
await flushAutocomplete();
assert.strictEqual(editor.getText(), "");
assert.strictEqual(editor.isShowingAutocomplete(), false);
});
it("applies exact typed slash-argument value on Enter even when first item is highlighted", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Mock provider for /argtest command with argument completions
const mockProvider: AutocompleteProvider = {
getSuggestions: async (lines, _cursorLine, cursorCol) => {
const text = lines[0] || "";
const beforeCursor = text.slice(0, cursorCol);
// Check if we're in argument completion context: "/argtest <prefix>"
const argtestMatch = beforeCursor.match(/^\/argtest\s+(\S+)$/);
if (argtestMatch) {
const argumentText = argtestMatch[1]!;
const allArguments = [
{ value: "one", label: "one" },
{ value: "two", label: "two" },
{ value: "three", label: "three" },
];
// Return all arguments that start with the typed prefix
const filtered = allArguments.filter((arg) => arg.value.startsWith(argumentText));
if (filtered.length > 0) {
return { items: filtered, prefix: argumentText };
}
}
return null;
},
applyCompletion,
};
editor.setAutocompleteProvider(mockProvider);
// Type "/argtest two"
editor.handleInput("/");
editor.handleInput("a");
editor.handleInput("r");
editor.handleInput("g");
editor.handleInput("t");
editor.handleInput("e");
editor.handleInput("s");
editor.handleInput("t");
editor.handleInput(" ");
editor.handleInput("t");
editor.handleInput("w");
editor.handleInput("o");
assert.strictEqual(editor.getText(), "/argtest two");
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), true);
// Press Enter - should apply the exact typed value "two", not the first item
editor.handleInput("\r");
// The exact typed value "two" should be retained
assert.strictEqual(editor.getText(), "/argtest two");
});
it("selects first prefix match on Enter when typed arg is not exact match", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Mock provider for /argtest command with argument completions
const mockProvider: AutocompleteProvider = {
getSuggestions: async (lines, _cursorLine, cursorCol) => {
const text = lines[0] || "";
const beforeCursor = text.slice(0, cursorCol);
// Check if we're in argument completion context
const argtestMatch = beforeCursor.match(/^\/argtest\s+(\S+)$/);
if (argtestMatch) {
const argumentText = argtestMatch[1]!;
const allArguments = [
{ value: "two", label: "two" },
{ value: "three", label: "three" },
{ value: "twelve", label: "twelve" },
];
// Return all items that start with the typed prefix
const filtered = allArguments.filter((arg) => arg.value.startsWith(argumentText));
if (filtered.length > 0) {
return { items: filtered, prefix: argumentText };
}
}
return null;
},
applyCompletion,
};
editor.setAutocompleteProvider(mockProvider);
// Type "/argtest t" - filtered to [two, three, twelve], prefix "t" matches "two" first
editor.handleInput("/");
editor.handleInput("a");
editor.handleInput("r");
editor.handleInput("g");
editor.handleInput("t");
editor.handleInput("e");
editor.handleInput("s");
editor.handleInput("t");
editor.handleInput(" ");
editor.handleInput("t");
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), true);
// Press Enter - "t" prefix matches "two" (first in list), so "two" is applied
editor.handleInput("\r");
assert.strictEqual(editor.getText(), "/argtest two");
});
it("highlights unique prefix match as user types (before full exact match)", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Mock provider that returns all items unfiltered (like real extensions do)
const mockProvider: AutocompleteProvider = {
getSuggestions: async (lines, _cursorLine, cursorCol) => {
const text = lines[0] || "";
const beforeCursor = text.slice(0, cursorCol);
const argtestMatch = beforeCursor.match(/^\/argtest\s+(\S+)$/);
if (argtestMatch) {
const argumentText = argtestMatch[1]!;
// Return all items - provider does not filter
const allArguments = [
{ value: "one", label: "one" },
{ value: "two", label: "two" },
{ value: "three", label: "three" },
];
return { items: allArguments, prefix: argumentText };
}
return null;
},
applyCompletion,
};
editor.setAutocompleteProvider(mockProvider);
// Type "/argtest tw" - "tw" is a prefix of only "two"
editor.handleInput("/");
editor.handleInput("a");
editor.handleInput("r");
editor.handleInput("g");
editor.handleInput("t");
editor.handleInput("e");
editor.handleInput("s");
editor.handleInput("t");
editor.handleInput(" ");
editor.handleInput("t");
editor.handleInput("w");
assert.strictEqual(editor.getText(), "/argtest tw");
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), true);
// Press Enter - "tw" uniquely matches "two", so "two" should be applied
editor.handleInput("\r");
assert.strictEqual(editor.getText(), "/argtest two");
});
it("selects first prefix match when multiple items match", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Mock provider that returns all items unfiltered
const mockProvider: AutocompleteProvider = {
getSuggestions: async (lines, _cursorLine, cursorCol) => {
const text = lines[0] || "";
const beforeCursor = text.slice(0, cursorCol);
const argtestMatch = beforeCursor.match(/^\/argtest\s+(\S+)$/);
if (argtestMatch) {
const argumentText = argtestMatch[1]!;
const allArguments = [
{ value: "one", label: "one" },
{ value: "two", label: "two" },
{ value: "three", label: "three" },
];
return { items: allArguments, prefix: argumentText };
}
return null;
},
applyCompletion,
};
editor.setAutocompleteProvider(mockProvider);
// Type "/argtest t" - "t" is a prefix of both "two" and "three"
editor.handleInput("/");
editor.handleInput("a");
editor.handleInput("r");
editor.handleInput("g");
editor.handleInput("t");
editor.handleInput("e");
editor.handleInput("s");
editor.handleInput("t");
editor.handleInput(" ");
editor.handleInput("t");
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), true);
// Press Enter - "t" matches "two" first, so "two" is selected
editor.handleInput("\r");
assert.strictEqual(editor.getText(), "/argtest two");
});
it("works for built-in-style command argument completion path (model-like)", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Mock provider for /model command with model completions
const mockProvider: AutocompleteProvider = {
getSuggestions: async (lines, _cursorLine, cursorCol) => {
const text = lines[0] || "";
const beforeCursor = text.slice(0, cursorCol);
// Check if we're in /model argument completion context
// Use [^ ]+ to match any non-space characters (including hyphens)
const modelMatch = beforeCursor.match(/^\/model\s+(\S+)$/);
if (modelMatch) {
const modelText = modelMatch[1]!;
const allModels = [
{ value: "gpt-4o", label: "gpt-4o" },
{ value: "gpt-4o-mini", label: "gpt-4o-mini" },
{ value: "claude-sonnet", label: "claude-sonnet" },
];
// Return all models that start with the typed prefix
const filtered = allModels.filter((m) => m.value.startsWith(modelText));
if (filtered.length > 0) {
return { items: filtered, prefix: modelText };
}
}
return null;
},
applyCompletion,
};
editor.setAutocompleteProvider(mockProvider);
// Type "/model gpt-4o-mini" - exact match for second item in list
editor.handleInput("/");
editor.handleInput("m");
editor.handleInput("o");
editor.handleInput("d");
editor.handleInput("e");
editor.handleInput("l");
editor.handleInput(" ");
editor.handleInput("g");
editor.handleInput("p");
editor.handleInput("t");
editor.handleInput("-");
editor.handleInput("4");
editor.handleInput("o");
editor.handleInput("-");
editor.handleInput("m");
editor.handleInput("i");
editor.handleInput("n");
editor.handleInput("i");
assert.strictEqual(editor.getText(), "/model gpt-4o-mini");
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), true);
// Press Enter - should retain exact typed value, not apply first highlighted item
editor.handleInput("\r");
// The exact typed value should be retained
assert.strictEqual(editor.getText(), "/model gpt-4o-mini");
});
it("awaits async slash command argument completions", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const provider = new CombinedAutocompleteProvider(
[
{
name: "load-skills",
description: "Load skills",
getArgumentCompletions: async (prefix) =>
prefix.startsWith("s") ? [{ value: "skill-a", label: "skill-a" }] : null,
},
],
process.cwd(),
);
editor.setAutocompleteProvider(provider);
editor.setText("/load-skills ");
editor.handleInput("s");
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), true);
editor.handleInput("\t");
assert.strictEqual(editor.getText(), "/load-skills skill-a");
assert.strictEqual(editor.isShowingAutocomplete(), false);
});
it("ignores invalid slash command argument completion results", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const provider = new CombinedAutocompleteProvider(
[
{
name: "load-skills",
description: "Load skills",
getArgumentCompletions: (() => "not-an-array") as unknown as (
argumentPrefix: string,
) => Promise<{ value: string; label: string }[] | null>,
},
],
process.cwd(),
);
editor.setAutocompleteProvider(provider);
editor.setText("/load-skills ");
editor.handleInput("s");
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), false);
assert.strictEqual(editor.getText(), "/load-skills s");
});
it("does not show argument completions when command has no argument completer", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const provider = new CombinedAutocompleteProvider(
[
{ name: "help", description: "Show help" },
{
name: "model",
description: "Switch model",
getArgumentCompletions: () => [{ value: "claude-opus", label: "claude-opus" }],
},
],
process.cwd(),
);
editor.setAutocompleteProvider(provider);
editor.handleInput("/");
editor.handleInput("h");
editor.handleInput("e");
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), true);
editor.handleInput("\t");
assert.strictEqual(editor.getText(), "/help ");
assert.strictEqual(editor.isShowingAutocomplete(), false);
});
});
describe("Inline slash trigger", () => {
const inlineProvider: AutocompleteProvider = {
getSuggestions: async (lines, cursorLine, cursorCol) => {
const beforeCursor = (lines[cursorLine] || "").slice(0, cursorCol);
if (!beforeCursor.endsWith("/")) return null;
return {
items: [{ value: "skill:review", label: "skill:review" }],
prefix: "/",
};
},
applyCompletion,
};
it("does not trigger for `/` after whitespace mid-input by default", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setAutocompleteProvider(inlineProvider);
for (const ch of "hello ") editor.handleInput(ch);
editor.handleInput("/");
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), false);
});
it("triggers for `/` after whitespace mid-input when inlineSlashTrigger is on", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme, { inlineSlashTrigger: true });
editor.setAutocompleteProvider(inlineProvider);
for (const ch of "hello ") editor.handleInput(ch);
editor.handleInput("/");
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), true);
});
it("retriggers inline completion as token characters arrive with the slash's request in flight", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme, { inlineSlashTrigger: true });
editor.setAutocompleteProvider({
getSuggestions: async (lines, cursorLine, cursorCol) => {
const beforeCursor = (lines[cursorLine] || "").slice(0, cursorCol);
const match = /\/skill:\w*$/.exec(beforeCursor);
if (match === null) return null;
return {
items: [{ value: "skill:review", label: "skill:review" }],
prefix: match[0],
};
},
applyCompletion,
});
// The slash and its first letters land back-to-back, the way one
// stdin chunk delivers them: the slash's request is still in flight
// while the letters arrive with no autocomplete state yet.
for (const ch of "hello /skill:r") editor.handleInput(ch);
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), true);
});
it("retriggers inline completion on later lines as the token grows", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme, { inlineSlashTrigger: true });
editor.setAutocompleteProvider({
getSuggestions: async (lines, cursorLine, cursorCol) => {
const beforeCursor = (lines[cursorLine] || "").slice(0, cursorCol);
const match = /\/skill:\w*$/.exec(beforeCursor);
if (match === null) return null;
return {
items: [{ value: "skill:review", label: "skill:review" }],
prefix: match[0],
};
},
applyCompletion,
});
for (const ch of "hello\n/skill:r") editor.handleInput(ch);
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), true);
});
it("retriggers inline completion when a colon follows the token name", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme, { inlineSlashTrigger: true });
let calls = 0;
editor.setAutocompleteProvider({
getSuggestions: async (lines, cursorLine, cursorCol) => {
calls += 1;
const beforeCursor = (lines[cursorLine] || "").slice(0, cursorCol);
const match = /\/skill:\w*$/.exec(beforeCursor);
if (match === null) return null;
return {
items: [{ value: "skill:review", label: "skill:review" }],
prefix: match[0],
};
},
applyCompletion,
});
for (const ch of "hello /skill") editor.handleInput(ch);
await flushAutocomplete();
const beforeColon = calls;
editor.handleInput(":");
await flushAutocomplete();
assert.ok(calls > beforeColon, "expected the colon to retrigger completion");
editor.handleInput("r");
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), true);
});
it("triggers for `/` at the start of a later line when inlineSlashTrigger is on", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme, { inlineSlashTrigger: true });
editor.setAutocompleteProvider(inlineProvider);
for (const ch of "first") editor.handleInput(ch);
editor.handleInput("\n");
editor.handleInput("/");
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), true);
});
it("does not trigger for `/` inside a word even when inlineSlashTrigger is on", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme, { inlineSlashTrigger: true });
editor.setAutocompleteProvider(inlineProvider);
for (const ch of "and/") editor.handleInput(ch);
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), false);
});
function inlineProviderWith(item: { value: string; label: string; data?: Record<string, unknown> }): AutocompleteProvider {
return {
getSuggestions: async (lines, cursorLine, cursorCol) => {
const beforeCursor = (lines[cursorLine] || "").slice(0, cursorCol);
if (!beforeCursor.endsWith("/")) return null;
return { items: [item], prefix: "/" };
},
applyCompletion,
};
}
it("does not submit when confirming an inline-marked completion with Enter", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme, { inlineSlashTrigger: true });
let submitted: string | undefined;
editor.onSubmit = (text) => {
submitted = text;
};
editor.setAutocompleteProvider(
inlineProviderWith({ value: "skill:review", label: "skill:review", data: { inlineSkill: true } }),
);
for (const ch of "hello ") editor.handleInput(ch);
editor.handleInput("/");
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), true);
editor.handleInput("\r");
await flushAutocomplete();
assert.strictEqual(submitted, undefined);
assert.strictEqual(editor.getText(), "hello skill:review");
});
it("still submits when confirming an unmarked slash completion with Enter", async () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme, { inlineSlashTrigger: true });
let submitted: string | undefined;
editor.onSubmit = (text) => {
submitted = text;
};
editor.setAutocompleteProvider(inlineProviderWith({ value: "help", label: "help" }));
for (const ch of "hello ") editor.handleInput(ch);
editor.handleInput("/");
await flushAutocomplete();
assert.strictEqual(editor.isShowingAutocomplete(), true);
editor.handleInput("\r");
await flushAutocomplete();
assert.strictEqual(submitted, "hello help");
});
});
describe("Character jump (Ctrl+])", () => {
it("jumps forward to first occurrence of character on same line", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world");
editor.handleInput("\x01"); // Ctrl+A - go to start
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
editor.handleInput("\x1d"); // Ctrl+] (legacy sequence for ctrl+])
editor.handleInput("o"); // Jump to first 'o'
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 }); // 'o' in "hello"
});
it("jumps forward to next occurrence after cursor", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world");
editor.handleInput("\x01"); // Ctrl+A - go to start
// Move cursor to the 'o' in "hello" (col 4)
for (let i = 0; i < 4; i++) editor.handleInput("\x1b[C");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 });
editor.handleInput("\x1d"); // Ctrl+]
editor.handleInput("o"); // Jump to next 'o' (in "world")
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // 'o' in "world"
});
it("jumps forward across multiple lines", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("abc\ndef\nghi");
// Cursor is at end (line 2, col 3). Move to line 0 via up arrows, then Ctrl+A
editor.handleInput("\x1b[A"); // Up
editor.handleInput("\x1b[A"); // Up - now on line 0
editor.handleInput("\x01"); // Ctrl+A - go to start of line
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
editor.handleInput("\x1d"); // Ctrl+]
editor.handleInput("g"); // Jump to 'g' on line 3
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 0 });
});
it("jumps backward to first occurrence before cursor on same line", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world");
// Cursor at end (col 11)
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 });
editor.handleInput("\x1b\x1d"); // Ctrl+Alt+] (ESC followed by Ctrl+])
editor.handleInput("o"); // Jump to last 'o' before cursor
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // 'o' in "world"
});
it("jumps backward across multiple lines", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("abc\ndef\nghi");
// Cursor at end of line 3
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 3 });
editor.handleInput("\x1b\x1d"); // Ctrl+Alt+]
editor.handleInput("a"); // Jump to 'a' on line 1
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
});
it("does nothing when character is not found (forward)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world");
editor.handleInput("\x01"); // Ctrl+A - go to start
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
editor.handleInput("\x1d"); // Ctrl+]
editor.handleInput("z"); // 'z' doesn't exist
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); // Cursor unchanged
});
it("does nothing when character is not found (backward)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world");
// Cursor at end
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 });
editor.handleInput("\x1b\x1d"); // Ctrl+Alt+]
editor.handleInput("z"); // 'z' doesn't exist
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 }); // Cursor unchanged
});
it("is case-sensitive", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("Hello World");
editor.handleInput("\x01"); // Ctrl+A - go to start
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
// Search for lowercase 'h' - should not find it (only 'H' exists)
editor.handleInput("\x1d"); // Ctrl+]
editor.handleInput("h");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); // Cursor unchanged
// Search for uppercase 'W' - should find it
editor.handleInput("\x1d"); // Ctrl+]
editor.handleInput("W");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 }); // 'W' in "World"
});
it("cancels jump mode when Ctrl+] is pressed again", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world");
editor.handleInput("\x01"); // Ctrl+A - go to start
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
editor.handleInput("\x1d"); // Ctrl+] - enter jump mode
editor.handleInput("\x1d"); // Ctrl+] again - cancel
// Type 'o' normally - should insert, not jump
editor.handleInput("o");
assert.strictEqual(editor.getText(), "ohello world");
});
it("cancels jump mode on Escape and processes the Escape", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world");
editor.handleInput("\x01"); // Ctrl+A - go to start
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
editor.handleInput("\x1d"); // Ctrl+] - enter jump mode
editor.handleInput("\x1b"); // Escape - cancel jump mode
// Cursor should be unchanged (Escape itself doesn't move cursor in editor)
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
// Type 'o' normally - should insert, not jump
editor.handleInput("o");
assert.strictEqual(editor.getText(), "ohello world");
});
it("cancels backward jump mode when Ctrl+Alt+] is pressed again", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world");
// Cursor at end
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 });
editor.handleInput("\x1b\x1d"); // Ctrl+Alt+] - enter backward jump mode
editor.handleInput("\x1b\x1d"); // Ctrl+Alt+] again - cancel
// Type 'o' normally - should insert, not jump
editor.handleInput("o");
assert.strictEqual(editor.getText(), "hello worldo");
});
it("searches for special characters", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("foo(bar) = baz;");
editor.handleInput("\x01"); // Ctrl+A - go to start
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
// Jump to '('
editor.handleInput("\x1d"); // Ctrl+]
editor.handleInput("(");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 });
// Jump to '='
editor.handleInput("\x1d"); // Ctrl+]
editor.handleInput("=");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 9 });
});
it("handles empty text gracefully", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
editor.handleInput("\x1d"); // Ctrl+]
editor.handleInput("x");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); // Cursor unchanged
});
it("resets lastAction when jumping", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world");
editor.handleInput("\x01"); // Ctrl+A - go to start
// Type to set lastAction to "type-word"
editor.handleInput("x");
assert.strictEqual(editor.getText(), "xhello world");
// Jump forward
editor.handleInput("\x1d"); // Ctrl+]
editor.handleInput("o");
// Type more - should start a new undo unit (lastAction was reset)
editor.handleInput("Y");
assert.strictEqual(editor.getText(), "xhellYo world");
// Undo should only undo "Y", not "x" as well
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "xhello world");
});
});
describe("Sticky column", () => {
// Helper: position cursor at a specific line and column
function positionCursor(editor: Editor, line: number, col: number): void {
// Go to line 0 first
for (let i = 0; i < 20; i++) editor.handleInput("\x1b[A");
// Go to target line
for (let i = 0; i < line; i++) editor.handleInput("\x1b[B");
// Go to target col
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < col; i++) editor.handleInput("\x1b[C");
}
it("preserves target column when moving up through a shorter line", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Line 0: "2222222222x222" (x at col 10)
// Line 1: "" (empty)
// Line 2: "1111111111_111111111111" (_ at col 10)
editor.setText("2222222222x222\n\n1111111111_111111111111");
// Position cursor on _ (line 2, col 10)
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 23 }); // At end
editor.handleInput("\x01"); // Ctrl+A - go to start of line
for (let i = 0; i < 10; i++) editor.handleInput("\x1b[C"); // Move right to col 10
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 10 });
// Press Up - should move to empty line (col clamped to 0)
editor.handleInput("\x1b[A"); // Up arrow
assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 });
// Press Up again - should move to line 0 at col 10 (on 'x')
editor.handleInput("\x1b[A"); // Up arrow
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 });
});
it("preserves target column when moving down through a shorter line", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("1111111111_111\n\n2222222222x222222222222");
// Position cursor on _ (line 0, col 10)
editor.handleInput("\x1b[A"); // Up to line 1
editor.handleInput("\x1b[A"); // Up to line 0
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 10; i++) editor.handleInput("\x1b[C");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 });
// Press Down - should move to empty line (col clamped to 0)
editor.handleInput("\x1b[B"); // Down arrow
assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 });
// Press Down again - should move to line 2 at col 10 (on 'x')
editor.handleInput("\x1b[B"); // Down arrow
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 10 });
});
it("resets sticky column on horizontal movement (left arrow)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("1234567890\n\n1234567890");
// Start at line 2, col 5
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C");
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 5 });
// Move up through empty line
editor.handleInput("\x1b[A"); // Up - line 1, col 0
editor.handleInput("\x1b[A"); // Up - line 0, col 5 (sticky)
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 });
// Move left - resets sticky column
editor.handleInput("\x1b[D"); // Left
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 });
// Move down twice
editor.handleInput("\x1b[B"); // Down - line 1, col 0
editor.handleInput("\x1b[B"); // Down - line 2, col 4 (new sticky from col 4)
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 4 });
});
it("resets sticky column on horizontal movement (right arrow)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("1234567890\n\n1234567890");
// Start at line 0, col 5
editor.handleInput("\x1b[A"); // Up to line 1
editor.handleInput("\x1b[A"); // Up to line 0
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 });
// Move down through empty line
editor.handleInput("\x1b[B"); // Down - line 1, col 0
editor.handleInput("\x1b[B"); // Down - line 2, col 5 (sticky)
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 5 });
// Move right - resets sticky column
editor.handleInput("\x1b[C"); // Right
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 6 });
// Move up twice
editor.handleInput("\x1b[A"); // Up - line 1, col 0
editor.handleInput("\x1b[A"); // Up - line 0, col 6 (new sticky from col 6)
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 });
});
it("resets sticky column on typing", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("1234567890\n\n1234567890");
// Start at line 2, col 8
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 8; i++) editor.handleInput("\x1b[C");
// Move up through empty line
editor.handleInput("\x1b[A"); // Up
editor.handleInput("\x1b[A"); // Up - line 0, col 8
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 });
// Type a character - resets sticky column
editor.handleInput("X");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 9 });
// Move down twice
editor.handleInput("\x1b[B"); // Down - line 1, col 0
editor.handleInput("\x1b[B"); // Down - line 2, col 9 (new sticky from col 9)
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 9 });
});
it("resets sticky column on backspace", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("1234567890\n\n1234567890");
// Start at line 2, col 8
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 8; i++) editor.handleInput("\x1b[C");
// Move up through empty line
editor.handleInput("\x1b[A"); // Up
editor.handleInput("\x1b[A"); // Up - line 0, col 8
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 });
// Backspace - resets sticky column
editor.handleInput("\x7f"); // Backspace
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 });
// Move down twice
editor.handleInput("\x1b[B"); // Down - line 1, col 0
editor.handleInput("\x1b[B"); // Down - line 2, col 7 (new sticky from col 7)
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 7 });
});
it("resets sticky column on Ctrl+A (move to line start)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("1234567890\n\n1234567890");
// Start at line 2, col 8
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 8; i++) editor.handleInput("\x1b[C");
// Move up - establishes sticky col 8
editor.handleInput("\x1b[A"); // Up - line 1, col 0
// Ctrl+A - resets sticky column to 0
editor.handleInput("\x01"); // Ctrl+A
assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 });
// Move up
editor.handleInput("\x1b[A"); // Up - line 0, col 0 (new sticky from col 0)
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
});
it("resets sticky column on Ctrl+E (move to line end)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("12345\n\n1234567890");
// Start at line 2, col 3
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 3; i++) editor.handleInput("\x1b[C");
// Move up through empty line - establishes sticky col 3
editor.handleInput("\x1b[A"); // Up - line 1, col 0
editor.handleInput("\x1b[A"); // Up - line 0, col 3
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 });
// Ctrl+E - resets sticky column to end
editor.handleInput("\x05"); // Ctrl+E
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 });
// Move down twice
editor.handleInput("\x1b[B"); // Down - line 1, col 0
editor.handleInput("\x1b[B"); // Down - line 2, col 5 (new sticky from col 5)
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 5 });
});
it("resets sticky column on word movement (Ctrl+Left)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world\n\nhello world");
// Start at end of line 2 (col 11)
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 11 });
// Move up through empty line - establishes sticky col 11
editor.handleInput("\x1b[A"); // Up - line 1, col 0
editor.handleInput("\x1b[A"); // Up - line 0, col 11
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 });
// Ctrl+Left - word movement resets sticky column
editor.handleInput("\x1b[1;5D"); // Ctrl+Left
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 }); // Before "world"
// Move down twice
editor.handleInput("\x1b[B"); // Down - line 1, col 0
editor.handleInput("\x1b[B"); // Down - line 2, col 6 (new sticky from col 6)
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 6 });
});
it("resets sticky column on word movement (Ctrl+Right)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("hello world\n\nhello world");
// Start at line 0, col 0
editor.handleInput("\x1b[A"); // Up
editor.handleInput("\x1b[A"); // Up
editor.handleInput("\x01"); // Ctrl+A
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
// Move down through empty line - establishes sticky col 0
editor.handleInput("\x1b[B"); // Down - line 1, col 0
editor.handleInput("\x1b[B"); // Down - line 2, col 0
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 0 });
// Ctrl+Right - word movement resets sticky column
editor.handleInput("\x1b[1;5C"); // Ctrl+Right
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 5 }); // After "hello"
// Move up twice
editor.handleInput("\x1b[A"); // Up - line 1, col 0
editor.handleInput("\x1b[A"); // Up - line 0, col 5 (new sticky from col 5)
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 });
});
it("resets sticky column on undo", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("1234567890\n\n1234567890");
// Go to line 0, col 8
editor.handleInput("\x1b[A"); // Up to line 1
editor.handleInput("\x1b[A"); // Up to line 0
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 8; i++) editor.handleInput("\x1b[C");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 });
// Move down through empty line - establishes sticky col 8
editor.handleInput("\x1b[B"); // Down - line 1, col 0
editor.handleInput("\x1b[B"); // Down - line 2, col 8 (sticky)
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 8 });
// Type something to create undo state - this clears sticky and sets col to 9
editor.handleInput("X");
assert.strictEqual(editor.getText(), "1234567890\n\n12345678X90");
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 9 });
// Move up - establishes new sticky col 9
editor.handleInput("\x1b[A"); // Up - line 1, col 0
editor.handleInput("\x1b[A"); // Up - line 0, col 9
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 9 });
// Undo - resets sticky column and restores cursor to line 2, col 8
editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
assert.strictEqual(editor.getText(), "1234567890\n\n1234567890");
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 8 });
// Move up - should capture new sticky from restored col 8, not old col 9
editor.handleInput("\x1b[A"); // Up - line 1, col 0
editor.handleInput("\x1b[A"); // Up - line 0, col 8 (new sticky from restored position)
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 });
});
it("handles multiple consecutive up/down movements", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("1234567890\nab\ncd\nef\n1234567890");
// Start at line 4, col 7
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 7; i++) editor.handleInput("\x1b[C");
assert.deepStrictEqual(editor.getCursor(), { line: 4, col: 7 });
// Move up multiple times through short lines
editor.handleInput("\x1b[A"); // Up - line 3, col 2 (clamped)
editor.handleInput("\x1b[A"); // Up - line 2, col 2 (clamped)
editor.handleInput("\x1b[A"); // Up - line 1, col 2 (clamped)
editor.handleInput("\x1b[A"); // Up - line 0, col 7 (restored)
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 });
// Move down multiple times - sticky should still be 7
editor.handleInput("\x1b[B"); // Down - line 1, col 2
editor.handleInput("\x1b[B"); // Down - line 2, col 2
editor.handleInput("\x1b[B"); // Down - line 3, col 2
editor.handleInput("\x1b[B"); // Down - line 4, col 7 (restored)
assert.deepStrictEqual(editor.getCursor(), { line: 4, col: 7 });
});
it("moves correctly through wrapped visual lines without getting stuck", () => {
const tui = createTestTUI(15, 24); // Narrow terminal
const editor = new Editor(tui, defaultEditorTheme);
// Line 0: short
// Line 1: 30 chars = wraps to 3 visual lines at width 10 (after padding)
editor.setText("short\n123456789012345678901234567890");
editor.render(15); // This gives 14 layout width
// Position at end of line 1 (col 30)
assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 30 });
// Move up repeatedly - should traverse all visual lines of the wrapped text
// and eventually reach line 0
editor.handleInput("\x1b[A"); // Up - to previous visual line within line 1
assert.strictEqual(editor.getCursor().line, 1);
editor.handleInput("\x1b[A"); // Up - another visual line
assert.strictEqual(editor.getCursor().line, 1);
editor.handleInput("\x1b[A"); // Up - should reach line 0
assert.strictEqual(editor.getCursor().line, 0);
});
it("handles setText resetting sticky column", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("1234567890\n\n1234567890");
// Establish sticky column
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 8; i++) editor.handleInput("\x1b[C");
editor.handleInput("\x1b[A"); // Up
// setText should reset sticky column
editor.setText("abcdefghij\n\nabcdefghij");
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 10 }); // At end
// Move up - should capture new sticky from current position (10)
editor.handleInput("\x1b[A"); // Up - line 1, col 0
editor.handleInput("\x1b[A"); // Up - line 0, col 10
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 });
});
it("sets preferredVisualCol when pressing right at end of prompt (last line)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Line 0: 20 chars with 'x' at col 10
// Line 1: empty
// Line 2: 10 chars ending with '_'
editor.setText("111111111x1111111111\n\n333333333_");
// Go to line 0, press Ctrl+E (end of line) - col 20
editor.handleInput("\x1b[A"); // Up to line 1
editor.handleInput("\x1b[A"); // Up to line 0
editor.handleInput("\x05"); // Ctrl+E - move to end of line
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 20 });
// Move down to line 2 - cursor clamped to col 10 (end of line)
editor.handleInput("\x1b[B"); // Down to line 1, col 0
editor.handleInput("\x1b[B"); // Down to line 2, col 10 (clamped)
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 10 });
// Press Right at end of prompt - nothing visible happens, but sets preferredVisualCol to 10
editor.handleInput("\x1b[C"); // Right - can't move, but sets preferredVisualCol
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 10 }); // Still at same position
// Move up twice to line 0 - should use preferredVisualCol (10) to land on 'x'
editor.handleInput("\x1b[A"); // Up to line 1, col 0
editor.handleInput("\x1b[A"); // Up to line 0, col 10 (on 'x')
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 });
});
it("handles editor resizes when preferredVisualCol is on the same line", () => {
// Create editor with wider terminal
const tui = createTestTUI(80, 24);
const editor = new Editor(tui, defaultEditorTheme);
editor.setText("12345678901234567890\n\n12345678901234567890");
// Start at line 2, col 15
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 15; i++) editor.handleInput("\x1b[C");
// Move up through empty line - establishes sticky col 15
editor.handleInput("\x1b[A"); // Up
editor.handleInput("\x1b[A"); // Up - line 0, col 15
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 15 });
// Render with narrower width to simulate resize
editor.render(12); // Width 12
// Move down - sticky should be clamped to new width
editor.handleInput("\x1b[B"); // Down - line 1
editor.handleInput("\x1b[B"); // Down - line 2, col should be clamped
assert.equal(editor.getCursor().col, 4);
});
it("handles editor resizes when preferredVisualCol is on a different line", () => {
const tui = createTestTUI(80, 24);
const editor = new Editor(tui, defaultEditorTheme);
// Create a line that wraps into multiple visual lines at width 10
// "12345678901234567890" = 20 chars, wraps to 2 visual lines at width 10
editor.setText("short\n12345678901234567890");
// Go to line 1, col 15
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 15; i++) editor.handleInput("\x1b[C");
assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 15 });
// Move up to establish sticky col 15
editor.handleInput("\x1b[A"); // Up to line 0
// Line 0 has only 5 chars, so cursor at col 5
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 });
// Narrow the editor
editor.render(10);
// Move down - preferredVisualCol was 15, but width is 10
// Should land on line 1, clamped to width (visual col 9, which is logical col 9)
editor.handleInput("\x1b[B"); // Down to line 1
assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 8 });
// Move up
editor.handleInput("\x1b[A"); // Up - should go to line 0
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); // Line 0 only has 5 chars
// Restore the original width
editor.render(80);
// Move down - preferredVisualCol was kept at 15
editor.handleInput("\x1b[B"); // Down to line 1
assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 15 });
});
it("rewrapped lines: target fits current visual column", () => {
const tui = createTestTUI(80, 24);
const editor = new Editor(tui, defaultEditorTheme);
editor.setText("abcdefghijklmnopqr\n123456789012345678");
positionCursor(editor, 0, 18);
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 18 });
// Narrow to width 10 (layoutWidth = 9).
// Line 0 last segment has visual col max 9, line 1 first segment max 8
editor.render(10);
// Move down: cursor clamps to 8
editor.handleInput("\x1b[B");
assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 8 });
// Widen back. Move up, the current visual col wins
editor.render(80);
editor.handleInput("\x1b[A");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 });
// Preferred was cleared by the rewrapped branch
editor.handleInput("\x1b[B");
assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 8 });
});
it("rewrapped lines: target shorter than current visual column", () => {
const tui = createTestTUI(80, 24);
const editor = new Editor(tui, defaultEditorTheme);
editor.setText("abcdefghijklmnopqr\n123456789012345678\nab");
positionCursor(editor, 0, 18);
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 18 });
// Narrow to width 10 (layoutWidth = 9). Moving down clamps to col 8
editor.render(10);
editor.handleInput("\x1b[B");
assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 8 });
// Widen the editor
editor.render(80);
// Move down to short line "ab".
// preferredVisualCol is replaced with current visual col (8), cursor clamps to 2
editor.handleInput("\x1b[B");
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 2 });
// Moving up restores to preferred col 8
editor.handleInput("\x1b[A");
assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 8 });
});
});
describe("Paste marker atomic behavior", () => {
/** Helper: simulate a large paste that creates a marker */
function pasteWithMarker(editor: Editor): string {
const bigContent = "line\n".repeat(20).trimEnd(); // 20 lines
editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`);
// The editor replaces large pastes with a marker like "[paste #1 +20 lines]"
return editor.getText();
}
/** Helper: 12-line paste content with a distinguishing tag */
function bigPaste(tag: string): string {
return Array.from({ length: 12 }, (_, i) => `${tag}${i}`).join("\n");
}
it("creates a paste marker for large pastes", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const text = pasteWithMarker(editor);
assert.match(text, /\[paste #\d+ \+\d+ lines\]/);
});
it("treats paste marker as single unit for right arrow", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("A");
pasteWithMarker(editor);
editor.handleInput("B");
// Text: "A[paste #1 +20 lines]B", cursor at end
// Go to start
editor.handleInput("\x01"); // Ctrl+A
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
// Right arrow: should move past "A"
editor.handleInput("\x1b[C");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 });
// Right arrow: should skip the entire marker
editor.handleInput("\x1b[C");
const marker = editor.getText().match(/\[paste #\d+ \+\d+ lines\]/)![0];
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 + marker.length });
// Right arrow: should move past "B"
editor.handleInput("\x1b[C");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 + marker.length + 1 });
});
it("treats paste marker as single unit for left arrow", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("A");
pasteWithMarker(editor);
editor.handleInput("B");
// Cursor at end
// Left arrow: past "B"
editor.handleInput("\x1b[D");
const text = editor.getText();
const marker = text.match(/\[paste #\d+ \+\d+ lines\]/)![0];
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 + marker.length });
// Left arrow: skip the entire marker
editor.handleInput("\x1b[D");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 });
// Left arrow: past "A"
editor.handleInput("\x1b[D");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
});
it("treats paste marker as single unit for backspace", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("A");
pasteWithMarker(editor);
editor.handleInput("B");
const text = editor.getText();
const marker = text.match(/\[paste #\d+ \+\d+ lines\]/)![0];
// Position cursor right after the marker (before "B")
editor.handleInput("\x01"); // Ctrl+A
// Move past "A" and the marker
editor.handleInput("\x1b[C"); // past "A"
editor.handleInput("\x1b[C"); // past marker
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 + marker.length });
// Backspace: should delete the entire marker at once
editor.handleInput("\x7f");
assert.strictEqual(editor.getText(), "AB");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 });
});
it("treats paste marker as single unit for forward delete", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("A");
pasteWithMarker(editor);
editor.handleInput("B");
// Position cursor on "A" (col 0) then move right once to be just before marker
editor.handleInput("\x01"); // Ctrl+A
editor.handleInput("\x1b[C"); // past "A", now at col 1 (start of marker)
// Forward delete: should delete the entire marker at once
editor.handleInput("\x1b[3~"); // Delete key
assert.strictEqual(editor.getText(), "AB");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 });
});
it("treats paste marker as single unit for word movement", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("X");
editor.handleInput(" ");
pasteWithMarker(editor);
editor.handleInput(" ");
editor.handleInput("Y");
// Text: "X [paste #1 +20 lines] Y"
const text = editor.getText();
const marker = text.match(/\[paste #\d+ \+\d+ lines\]/)![0];
// Go to start
editor.handleInput("\x01"); // Ctrl+A
// Ctrl+Right: skip "X"
editor.handleInput("\x1b[1;5C");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 });
// Ctrl+Right: skip whitespace + marker (marker treated as single non-ws, non-punct unit)
editor.handleInput("\x1b[1;5C");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 2 + marker.length });
});
it("undo restores marker after backspace deletion", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.handleInput("A");
pasteWithMarker(editor);
editor.handleInput("B");
const textBefore = editor.getText();
// Position after marker
editor.handleInput("\x01");
editor.handleInput("\x1b[C"); // past A
editor.handleInput("\x1b[C"); // past marker
// Delete marker
editor.handleInput("\x7f");
assert.strictEqual(editor.getText(), "AB");
// Undo
editor.handleInput("\x1b[45;5u");
assert.strictEqual(editor.getText(), textBefore);
});
it("undo after paste marker deletion restores the paste registry", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
let submitted = "";
editor.onSubmit = (t) => {
submitted = t;
};
const paste = bigPaste("alpha");
editor.handleInput(`\x1b[200~${paste}\x1b[201~`);
editor.handleInput("\x7f"); // delete the marker
editor.handleInput("\x1b[45;5u"); // undo: restores marker text and registry
editor.handleInput("\r");
assert.strictEqual(submitted, paste);
});
it("undo after deleting the first of two paste markers restores both registry entries", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
let submitted = "";
editor.onSubmit = (t) => {
submitted = t;
};
const pasteA = bigPaste("alpha");
const pasteB = bigPaste("beta");
editor.handleInput(`\x1b[200~${pasteA}\x1b[201~`); // #1 = A
editor.handleInput(`\x1b[200~${pasteB}\x1b[201~`); // #2 = B, cursor at end
editor.handleInput("\x01"); // Ctrl+A
editor.handleInput("\x1b[C"); // right over marker #1
editor.handleInput("\x7f"); // delete marker #1, renumbers #2 -> #1
editor.handleInput("\x1b[45;5u"); // undo
editor.handleInput("\r");
assert.strictEqual(submitted, pasteA + pasteB);
});
it("renumbers the paste registry in ascending id order when markers are out of order in text", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
let submitted = "";
editor.onSubmit = (t) => {
submitted = t;
};
const pasteA = bigPaste("alpha");
const pasteB = bigPaste("beta");
const pasteC = bigPaste("gamma");
editor.handleInput(`\x1b[200~${pasteA}\x1b[201~`); // #1 = A
editor.handleInput("\x01"); // Ctrl+A
editor.handleInput(`\x1b[200~${pasteB}\x1b[201~`); // #2 = B, text: [#2][#1]
editor.handleInput("\x01"); // Ctrl+A
editor.handleInput(`\x1b[200~${pasteC}\x1b[201~`); // #3 = C, text: [#3][#2][#1]
editor.handleInput("\x05"); // Ctrl+E
editor.handleInput("\x7f"); // delete marker #1, renumber #3 -> #2 and #2 -> #1
editor.handleInput("\r");
assert.strictEqual(submitted, pasteC + pasteB);
});
it("undo after setText restores paste markers and registry", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
let submitted = "";
editor.onSubmit = (t) => {
submitted = t;
};
const paste = bigPaste("alpha");
editor.handleInput(`\x1b[200~${paste}\x1b[201~`);
editor.setText("replacement");
editor.handleInput("\x1b[45;5u"); // undo
editor.handleInput("\r");
assert.strictEqual(submitted, paste);
});
it("setText with preservePasteRegistry keeps the registry for surviving markers", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
let submitted = "";
editor.onSubmit = (t) => {
submitted = t;
};
const paste = bigPaste("alpha");
editor.handleInput(`\x1b[200~${paste}\x1b[201~`); // #1 = alpha
// A programmatic replace that still contains the marker (e.g. a subclass
// expanding one of several paste markers) must not orphan its entry.
editor.setText(editor.getText(), { preservePasteRegistry: true });
editor.handleInput("\r");
assert.strictEqual(submitted, paste);
});
it("handles multiple paste markers in same line", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
pasteWithMarker(editor);
editor.handleInput(" ");
pasteWithMarker(editor);
const text = editor.getText();
const markers = [...text.matchAll(/\[paste #\d+ \+\d+ lines\]/g)];
assert.strictEqual(markers.length, 2);
// Go to start
editor.handleInput("\x01");
// Right arrow: should skip first marker atomically
editor.handleInput("\x1b[C");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: markers[0]![0].length });
// Right arrow: past space
editor.handleInput("\x1b[C");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: markers[0]![0].length + 1 });
// Right arrow: should skip second marker atomically
editor.handleInput("\x1b[C");
assert.deepStrictEqual(editor.getCursor(), {
line: 0,
col: markers[0]![0].length + 1 + markers[1]![0].length,
});
});
it("does not treat manually typed marker-like text as atomic (no valid paste ID)", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Type text that matches the pattern but was typed manually (no paste entry)
const fakeMarker = "[paste #99 +5 lines]";
for (const ch of fakeMarker) editor.handleInput(ch);
assert.strictEqual(editor.getText(), fakeMarker);
// No paste with ID 99 exists, so the marker is NOT treated atomically.
// Right arrow should move one grapheme at a time.
editor.handleInput("\x01"); // Ctrl+A
editor.handleInput("\x1b[C"); // Right
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 }); // Just past "["
});
it("does not crash when paste marker is wider than terminal width", () => {
// Reproduce: terminal width 8, paste marker "[paste #1 +47 lines]" (21 chars)
const tui = createTestTUI();
const editor = new Editor(tui, defaultEditorTheme);
const bigContent = "line\n".repeat(47).trimEnd();
editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`);
const text = editor.getText();
const marker = text.match(/\[paste #\d+ \+\d+ lines\]/);
assert.ok(marker, "paste marker should be created");
assert.ok(visibleWidth(marker[0]) > 8, "marker should be wider than render width");
// Render at very narrow width - should not throw
const lines = editor.render(8);
// Every rendered line must fit within the width (marker is split)
for (const line of lines) {
assert.ok(
visibleWidth(line) <= 8,
`line exceeds width 8: visible=${visibleWidth(line)} text=${JSON.stringify(line)}`,
);
}
});
it("does not crash when text + paste marker exceeds terminal width with cursor on marker", () => {
// Reproduce: terminal width 54, text "b".repeat(35) + "[paste #1 +27 lines]" + "bbbb"
// Cursor lands on the paste marker after word-wrap, causing the rendered line
// to be 55 visible chars (1 over the width).
const tui = createTestTUI();
const editor = new Editor(tui, defaultEditorTheme);
// Type 35 'b' characters
for (let i = 0; i < 35; i++) editor.handleInput("b");
// Paste 27 lines
const bigContent = "line\n".repeat(27).trimEnd();
editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`);
// Type a few more characters
for (let i = 0; i < 4; i++) editor.handleInput("b");
// Move cursor left to land on the paste marker
editor.handleInput("\x1b[D"); // past last 'b'
editor.handleInput("\x1b[D"); // past last 'b'
editor.handleInput("\x1b[D"); // past last 'b'
editor.handleInput("\x1b[D"); // past last 'b'
editor.handleInput("\x1b[D"); // now on the paste marker
// Render at width 54 - should not throw
const renderWidth = 54;
const lines = editor.render(renderWidth);
for (const line of lines) {
assert.ok(
visibleWidth(line) <= renderWidth,
`line exceeds width ${renderWidth}: visible=${visibleWidth(line)} text=${JSON.stringify(line)}`,
);
}
});
it("wordWrapLine re-checks overflow after backtracking to wrap opportunity", () => {
// Reproduce crash #2: " " + "b".repeat(35) + atomic_marker(20 chars) + "bbbb"
// layoutWidth=53. After wrapping at the space, the remaining 35 b's + marker = 55
// must trigger a second force-break instead of silently overflowing.
const tui = createTestTUI();
const editor = new Editor(tui, defaultEditorTheme);
// Type a space, then 35 b's
editor.handleInput(" ");
for (let i = 0; i < 35; i++) editor.handleInput("b");
// Paste 27 lines to create marker
const bigContent = "line\n".repeat(27).trimEnd();
editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`);
// Type trailing chars
for (let i = 0; i < 4; i++) editor.handleInput("b");
// Render at width 54 (contentWidth=54, layoutWidth=53 with paddingX=0)
const renderWidth = 54;
const lines = editor.render(renderWidth);
for (const line of lines) {
assert.ok(
visibleWidth(line) <= renderWidth,
`line exceeds width ${renderWidth}: visible=${visibleWidth(line)} text=${JSON.stringify(line)}`,
);
}
});
it("expands large pasted content literally in getExpandedText", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const pastedText = [
"line 1",
"line 2",
"line 3",
"line 4",
"line 5",
"line 6",
"line 7",
"line 8",
"line 9",
"line 10",
"tokens $1 $2 $& $$ $` $' end",
].join("\n");
editor.handleInput(`\x1b[200~${pastedText}\x1b[201~`);
assert.match(editor.getText(), /\[paste #\d+ \+\d+ lines\]/);
assert.strictEqual(editor.getExpandedText(), pastedText);
});
it("snaps to the paste marker start when navigating down into it", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
// Line 0: long enough text to establish a sticky column
editor.setText("12345678901234567890\n\nhello ");
// Create a large paste to get a marker
const bigContent = "x".repeat(2000);
editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`);
editor.render(80);
const text = editor.getText();
const _marker = text.match(/\[paste #\d+ \d+ chars\]/)![0];
// Line 0: "12345678901234567890"
// Line 1: "" (empty)
// Line 2: "hello [paste #1 2000 chars]"
// marker starts at col 6
// Navigate to line 0, col 10
editor.handleInput("\x1b[A"); // Up to line 1
editor.handleInput("\x1b[A"); // Up to line 0
editor.handleInput("\x01"); // Ctrl+A (start of line)
for (let i = 0; i < 10; i++) editor.handleInput("\x1b[C"); // Right 10
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 });
// Down to empty line
editor.handleInput("\x1b[B");
assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 });
// Down to paste marker line - sticky col 10 falls inside marker (starts at col 6).
// Cursor should snap to start of marker (col 6), not end (col 6 + marker.length).
editor.handleInput("\x1b[B");
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 6 });
});
it("preserves sticky column when navigating through paste marker line", () => {
const tui = createTestTUI(30, 24);
const editor = new Editor(tui, defaultEditorTheme);
// Build:
// Line 0: "1234567890123456" (16 chars)
// Line 1: "" (empty)
// Line 2: "[paste #1 2000 chars]" (22 chars, paste marker)
// Line 3: "" (empty)
// Line 4: "abcdefghijklmnop" (16 chars)
for (const ch of "1234567890123456") editor.handleInput(ch);
editor.handleInput("\n");
editor.handleInput("\n");
editor.handleInput(`\x1b[200~${"x".repeat(2000)}\x1b[201~`);
editor.handleInput("\n");
editor.handleInput("\n");
for (const ch of "abcdefghijklmnop") editor.handleInput(ch);
editor.render(30);
// Navigate to line 0, col 10
for (let i = 0; i < 4; i++) editor.handleInput("\x1b[A"); // Up to line 0
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 10; i++) editor.handleInput("\x1b[C");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 });
// Down to empty line - sticky col 10 established
editor.handleInput("\x1b[B");
assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 });
// Down to paste marker - cursor snapped to col 0 (start of marker)
editor.handleInput("\x1b[B");
assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 0 });
// Down to empty line
editor.handleInput("\x1b[B");
assert.deepStrictEqual(editor.getCursor(), { line: 3, col: 0 });
// Down to last line - should restore sticky col 10
editor.handleInput("\x1b[B");
assert.deepStrictEqual(editor.getCursor(), { line: 4, col: 10 });
});
it("does not get stuck moving down from a multi-visual-line paste marker", () => {
const tui = createTestTUI(20, 24);
const editor = new Editor(tui, defaultEditorTheme);
// Build:
// Logical line 0: "abcdefgh" + marker(21 chars) + "ijklmnopqr"
// Logical line 1: "123456789012345678"
//
// Marker "[paste #1 +100 lines]" (21 chars) is wider than the
// terminal (20). Word-wrap splits at the space before "lines",
// producing:
// VL1: abcdefgh (startCol 0, len 8)
// VL2: [paste #1 +100 (startCol 8, len 15) <- marker head
// VL3: lines]ijklmnopqr (startCol 23, len 16) <- marker tail + content
// VL4: 123456789012345678 (line 1)
//
// On VL3 the marker tail "lines]" occupies visual cols 0-5.
// Content ("i") starts at visual col 6 = logical col 29.
for (const ch of "abcdefgh") editor.handleInput(ch);
const bigContent = "line\n".repeat(100).trimEnd();
editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`);
for (const ch of "ijklmnopqr") editor.handleInput(ch);
editor.handleInput("\n");
for (const ch of "123456789012345678") editor.handleInput(ch);
editor.render(20);
const text = editor.getText();
const markerMatch = text.match(/\[paste #\d+ \+\d+ lines]/);
assert.ok(markerMatch, "paste marker should be created");
const markerLen = markerMatch[0].length; // 21
assert.ok(markerLen > 20, "marker should be wider than terminal");
const markerStart = 8;
const markerEnd = markerStart + markerLen; // 29
// Navigate to line 0, col 6 (on "g"). Preferred col 6 is past the
// marker tail on VL3, so the cursor should land on content ("i" at
// col 29) without snapping back.
editor.handleInput("\x1b[A"); // Up to line 0
editor.handleInput("\x01"); // Ctrl+A (start of line)
for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); // Right to col 6
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 });
// Down: cursor lands on paste marker start
editor.handleInput("\x1b[B");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: markerStart });
// Down again: preferred col 6 lands at VL3 col 29 ("i"), which is
// past the marker. Cursor stays on line 0.
editor.handleInput("\x1b[B");
assert.strictEqual(editor.getCursor().line, 0);
assert.strictEqual(editor.getCursor().col, markerEnd); // col 29 = "i"
// Up: back to paste marker
editor.handleInput("\x1b[A");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: markerStart });
// Up again: back to col 6 ("g")
editor.handleInput("\x1b[A");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 });
});
it("skips marker continuation VLs when preferred col falls in marker tail", () => {
const tui = createTestTUI(20, 24);
const editor = new Editor(tui, defaultEditorTheme);
// Same layout. Start at col 3 ("d"). Preferred col 3 maps to VL3
// visual col 3 which is inside the "lines]" marker tail.
// moveToVisualLine detects the continuation VL and skips to VL4
// (line 1).
// VL1: abcdefgh (startCol 0, len 8)
// VL2: [paste #1 +100 (startCol 8, len 15) <- marker head
// VL3: lines]ijklmnopqr (startCol 23, len 16) <- marker tail + content
// VL4: 123456789012345678 (line 1)
for (const ch of "abcdefgh") editor.handleInput(ch);
const bigContent = "line\n".repeat(100).trimEnd();
editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`);
for (const ch of "ijklmnopqr") editor.handleInput(ch);
editor.handleInput("\n");
for (const ch of "123456789012345678") editor.handleInput(ch);
editor.render(20);
// Navigate to line 0, col 3 (on "d")
editor.handleInput("\x1b[A"); // Up to line 0
editor.handleInput("\x01"); // Ctrl+A
for (let i = 0; i < 3; i++) editor.handleInput("\x1b[C");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 });
// Down: marker
editor.handleInput("\x1b[B");
assert.strictEqual(editor.getCursor().col, 8);
// Down: skips VL3 (col 3 in marker tail) and lands on line 1
editor.handleInput("\x1b[B");
assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 3 });
// Round-trip back
editor.handleInput("\x1b[A");
assert.strictEqual(editor.getCursor().col, 8); // marker
editor.handleInput("\x1b[A");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 });
});
it("submits large pasted content literally", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
const pastedText = [
"line 1",
"line 2",
"line 3",
"line 4",
"line 5",
"line 6",
"line 7",
"line 8",
"line 9",
"line 10",
"tokens $1 $2 $& $$ $` $' end",
].join("\n");
let submitted = "";
editor.onSubmit = (text) => {
submitted = text;
};
editor.handleInput(`\x1b[200~${pastedText}\x1b[201~`);
editor.handleInput("\r");
assert.strictEqual(submitted, pastedText);
});
});
});
describe("wordWrapLine narrow width", () => {
it("does not recurse infinitely on a wide grapheme at maxWidth 1", () => {
const chunks = wordWrapLine("中", 1);
assert.deepStrictEqual(
chunks.map((c) => c.text),
["中"],
);
});
it("splits CJK text into per-grapheme overflow chunks at maxWidth 1", () => {
const chunks = wordWrapLine("中文文本", 1);
assert.deepStrictEqual(
chunks.map((c) => c.text),
["中", "文", "文", "本"],
);
assert.deepStrictEqual(
chunks.map((c) => [c.startIndex, c.endIndex]),
[
[0, 1],
[1, 2],
[2, 3],
[3, 4],
],
);
});
it("handles mixed narrow and wide graphemes at maxWidth 1", () => {
const chunks = wordWrapLine("ab中cd", 1);
assert.deepStrictEqual(
chunks.map((c) => c.text),
["a", "b", "中", "c", "d"],
);
});
it("still re-wraps multi-grapheme atomic segments at narrow widths", () => {
// Paste markers arrive as one atomic pre-segmented unit; they can
// still be broken down grapheme by grapheme — recursion must keep
// that ability.
const marker = "[paste #1]";
const preSegmented: Intl.SegmentData[] = [{ segment: marker, index: 0, input: marker }];
const chunks = wordWrapLine(marker, 3, preSegmented);
assert.ok(chunks.length > 1);
assert.strictEqual(chunks.map((c) => c.text).join(""), marker);
});
it("does not recurse infinitely on a multi-code-unit grapheme at maxWidth 1", () => {
// Guards "grapheme count, not code-unit length": a ZWJ family emoji
// is 11 code units but 1 grapheme (width 2). A guard mistakenly
// written as `grapheme.length <= 1` passes the BMP CJK cases yet
// recurses forever on this input.
const chunks = wordWrapLine("👨👩👧👦", 1);
assert.deepStrictEqual(
chunks.map((c) => c.text),
["👨👩👧👦"],
);
});
});
describe("Editor narrow width rendering", () => {
it("renders CJK text without crashing at widths 1-8 (default padding)", () => {
for (let width = 1; width <= 8; width++) {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("你好世界");
assert.doesNotThrow(() => editor.render(width), `width ${width}`);
}
});
it("renders CJK text without crashing at widths 1-8 (paddingX 4, matches kimi-code)", () => {
for (let width = 1; width <= 8; width++) {
const editor = new Editor(createTestTUI(), defaultEditorTheme, { paddingX: 4 });
editor.setText("你好,世界!");
assert.doesNotThrow(() => editor.render(width), `width ${width}`);
}
});
it("recalls history without crashing after rendering at width 1", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.addToHistory("你好世界");
editor.render(1); // narrow render pins lastWidth at 1
assert.doesNotThrow(() => {
(editor as unknown as { navigateHistory(direction: 1 | -1): void }).navigateHistory(-1);
// Recalling CJK text re-wraps it at the pinned narrow width —
// without the guard this overflows the stack.
editor.render(1);
});
assert.strictEqual(editor.getText(), "你好世界");
});
it("renders inside a TUI at 5 columns without crashing or overflowing", async () => {
const terminal = new VirtualTerminal(5, 12);
const tui = new TuiMainScreen(terminal);
const editor = new Editor(tui, defaultEditorTheme, { paddingX: 4 });
tui.addChild(editor);
editor.setText("你好世界");
tui.start();
await terminal.waitForRender();
const viewport = terminal.getViewport();
// Assert the exact visible rows: without truncation, xterm
// auto-wraps the overwide rows and the structure shifts, turning
// these assertions red (a tautological every(visibleWidth <= 5)
// check cannot fail on a 5-column terminal and was removed).
// Each content row is 6 columns wide (left padding 2 + CJK char 2
// + right padding 2) and is truncated to 5, leaving the trailing
// space.
assert.strictEqual(viewport[0], "─────");
assert.strictEqual(viewport[1], " 你 ");
assert.strictEqual(viewport[2], " 好 ");
assert.strictEqual(viewport[3], " 世 ");
assert.strictEqual(viewport[4], " 界 ");
assert.strictEqual(viewport[5], "─────");
tui.stop();
});
it("does not throw at zero or negative widths", () => {
const editor = new Editor(createTestTUI(), defaultEditorTheme);
editor.setText("你好世界");
assert.doesNotThrow(() => editor.render(0));
assert.doesNotThrow(() => editor.render(-1));
});
});
|