pixelsrc 0.2.0

Pixelsrc - GenAI-native pixel art format and compiler
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
//! Language Server Protocol implementation for Pixelsrc
//!
//! Provides LSP support for .pxl files in editors like VS Code, Neovim, etc.

use crate::color::parse_color;
use crate::motion::{ease, parse_timing_function, Interpolation, StepPosition};
use crate::tokenizer::tokenize;
use crate::transforms::{explain_transform, parse_transform_str, Transform};
use crate::validate::{Severity, ValidationIssue, Validator};
use crate::variables::VariableRegistry;
use serde_json::Value;
use std::collections::HashMap;
use std::sync::RwLock;
use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer, LspService, Server};

/// Information about a token's position in a grid
#[derive(Debug, Clone)]
struct GridInfo {
    /// Column index (0-indexed)
    x: usize,
    /// Row index (0-indexed within grid array)
    y: usize,
    /// The token at this position
    token: String,
    /// Width of this row in tokens
    row_width: usize,
    /// Expected width (from first row or size field)
    expected_width: usize,
    /// Name of the sprite
    sprite_name: String,
}

/// Information about a timing function at cursor position
#[derive(Debug, Clone)]
struct TimingFunctionInfo {
    /// Raw timing function string from JSON
    function_str: String,
    /// Parsed interpolation
    interpolation: Interpolation,
}

/// Information about a color found in the document
#[derive(Debug, Clone)]
struct ColorMatch {
    /// The original color string as it appears in the document
    #[allow(dead_code)]
    original: String,
    /// The resolved RGBA values (0.0-1.0 range)
    rgba: (f32, f32, f32, f32),
    /// Start position in the line
    start: u32,
    /// End position in the line
    end: u32,
}

/// Information about a transform at a cursor position
#[derive(Debug, Clone)]
struct TransformInfo {
    /// The parsed transform
    transform: Transform,
    /// The raw transform string
    raw: String,
    /// Object type (sprite, animation, composition, etc.)
    object_type: String,
    /// Object name
    object_name: String,
    /// Index in the transform array (0-indexed)
    index: usize,
    /// Total number of transforms in the array
    total: usize,
}

/// The Pixelsrc Language Server
pub struct PixelsrcLanguageServer {
    client: Client,
    /// Document state tracking for open files
    documents: RwLock<HashMap<Url, String>>,
}

impl PixelsrcLanguageServer {
    pub fn new(client: Client) -> Self {
        Self { client, documents: RwLock::new(HashMap::new()) }
    }

    /// Validate document content and publish diagnostics
    async fn validate_and_publish(&self, uri: &Url, content: &str) {
        let mut validator = Validator::new();
        for (line_num, line) in content.lines().enumerate() {
            validator.validate_line(line_num + 1, line);
        }

        let diagnostics: Vec<Diagnostic> =
            validator.issues().iter().map(Self::issue_to_diagnostic).collect();

        self.client.publish_diagnostics(uri.clone(), diagnostics, None).await;
    }

    /// Convert a ValidationIssue to an LSP Diagnostic
    fn issue_to_diagnostic(issue: &ValidationIssue) -> Diagnostic {
        let severity = match issue.severity {
            Severity::Error => DiagnosticSeverity::ERROR,
            Severity::Warning => DiagnosticSeverity::WARNING,
        };

        // Build the message with optional suggestion
        let message = if let Some(ref suggestion) = issue.suggestion {
            format!("{} ({})", issue.message, suggestion)
        } else {
            issue.message.clone()
        };

        Diagnostic {
            range: Range {
                start: Position { line: (issue.line - 1) as u32, character: 0 },
                end: Position { line: (issue.line - 1) as u32, character: u32::MAX },
            },
            severity: Some(severity),
            code: Some(NumberOrString::String(issue.issue_type.to_string())),
            source: Some("pixelsrc".to_string()),
            message,
            ..Default::default()
        }
    }

    /// Parse grid context from a JSON line at a specific character position
    ///
    /// Returns GridInfo if the cursor is positioned within a grid token.
    fn parse_grid_context(line: &str, char_pos: u32) -> Option<GridInfo> {
        // Parse the JSON line
        let obj: Value = serde_json::from_str(line).ok()?;
        let obj = obj.as_object()?;

        // Must be a sprite type
        let obj_type = obj.get("type")?.as_str()?;
        if obj_type != "sprite" {
            return None;
        }

        let sprite_name = obj.get("name")?.as_str()?.to_string();

        // Get the grid array
        let grid = obj.get("grid")?.as_array()?;
        if grid.is_empty() {
            return None;
        }

        // Get expected width from size field or first row
        let expected_width = if let Some(size) = obj.get("size").and_then(|s| s.as_array()) {
            size.first().and_then(|v| v.as_u64()).unwrap_or(0) as usize
        } else {
            // Use first row width as expected
            let first_row = grid.first()?.as_str()?;
            let (tokens, _) = tokenize(first_row);
            tokens.len()
        };

        // Find the "grid" key position in the raw JSON
        // We need to locate where the grid array starts in the line
        let grid_key_pos = line.find("\"grid\"")?;

        // Find the opening bracket of the grid array
        let after_key = &line[grid_key_pos..];
        let bracket_offset = after_key.find('[')?;
        let grid_array_start = grid_key_pos + bracket_offset;

        // If cursor is before the grid array, no hover
        if (char_pos as usize) <= grid_array_start {
            return None;
        }

        // Now we need to find which row string contains the cursor
        // Walk through the grid array portion of the line
        let grid_portion = &line[grid_array_start..];
        let char_in_grid = (char_pos as usize) - grid_array_start;

        // Parse through the grid array manually to find string positions
        let mut pos = 0;
        let chars: Vec<char> = grid_portion.chars().collect();

        for (row_idx, grid_row) in grid.iter().enumerate() {
            let row_str = grid_row.as_str()?;

            // Find the opening quote for this row string
            while pos < chars.len() && chars[pos] != '"' {
                pos += 1;
            }
            if pos >= chars.len() {
                return None;
            }

            let string_start = pos + 1; // Position after opening quote

            // Find the closing quote
            pos += 1; // Move past opening quote
            while pos < chars.len() && chars[pos] != '"' {
                // Handle escaped quotes
                if chars[pos] == '\\' && pos + 1 < chars.len() {
                    pos += 2;
                    continue;
                }
                pos += 1;
            }

            let string_end = pos; // Position of closing quote

            // Check if cursor is within this string
            if char_in_grid >= string_start && char_in_grid < string_end {
                // Cursor is in this row string
                let char_in_string = char_in_grid - string_start;

                // Tokenize the row and find which token the cursor is in
                let (tokens, _) = tokenize(row_str);
                let row_width = tokens.len();

                // Track position within the string to map to token index
                let mut string_pos = 0;
                for (token_idx, token) in tokens.iter().enumerate() {
                    let token_start = string_pos;
                    let token_end = string_pos + token.len();

                    if char_in_string >= token_start && char_in_string < token_end {
                        return Some(GridInfo {
                            x: token_idx,
                            y: row_idx,
                            token: token.clone(),
                            row_width,
                            expected_width,
                            sprite_name,
                        });
                    }

                    string_pos = token_end;
                }
            }

            pos += 1; // Move past closing quote
        }

        None
    }

    /// Parse transform context from a JSON line at a specific character position
    ///
    /// Returns TransformInfo if the cursor is positioned within a transform string.
    fn parse_transform_context(line: &str, char_pos: u32) -> Option<TransformInfo> {
        // Parse the JSON line
        let obj: Value = serde_json::from_str(line).ok()?;
        let obj = obj.as_object()?;

        // Get the type and name
        let obj_type = obj.get("type")?.as_str()?.to_string();
        let obj_name = obj.get("name").and_then(|n| n.as_str()).unwrap_or("").to_string();

        // Get the transform array
        let transform_array = obj.get("transform")?.as_array()?;
        if transform_array.is_empty() {
            return None;
        }

        // Find the "transform" key position in the raw JSON
        let transform_key_pos = line.find("\"transform\"")?;

        // Find the opening bracket of the transform array
        let after_key = &line[transform_key_pos..];
        let bracket_offset = after_key.find('[')?;
        let array_start = transform_key_pos + bracket_offset;

        // If cursor is before the transform array, no hover
        if (char_pos as usize) <= array_start {
            return None;
        }

        // Now we need to find which array element contains the cursor
        // Walk through the array portion of the line
        let array_portion = &line[array_start..];
        let char_in_array = (char_pos as usize) - array_start;

        // Parse through the array manually to find string positions
        let mut pos = 0;
        let chars: Vec<char> = array_portion.chars().collect();
        let total = transform_array.len();

        for (idx, transform_val) in transform_array.iter().enumerate() {
            // Get the transform string value
            let transform_str = transform_val.as_str()?;

            // Find the opening quote for this string
            while pos < chars.len() && chars[pos] != '"' {
                pos += 1;
            }
            if pos >= chars.len() {
                return None;
            }

            let string_start = pos + 1; // Position after opening quote

            // Find the closing quote
            pos += 1; // Move past opening quote
            while pos < chars.len() && chars[pos] != '"' {
                // Handle escaped quotes
                if chars[pos] == '\\' && pos + 1 < chars.len() {
                    pos += 2;
                    continue;
                }
                pos += 1;
            }

            let string_end = pos; // Position of closing quote

            // Check if cursor is within this string (including quotes for better UX)
            if char_in_array >= string_start.saturating_sub(1) && char_in_array <= string_end {
                // Parse the transform string
                if let Ok(transform) = parse_transform_str(transform_str) {
                    return Some(TransformInfo {
                        transform,
                        raw: transform_str.to_string(),
                        object_type: obj_type,
                        object_name: obj_name,
                        index: idx,
                        total,
                    });
                }
            }

            pos += 1; // Move past closing quote
        }

        None
    }

    /// Extract document symbols from content
    ///
    /// Returns a list of (name, type, line_number) tuples for all defined objects.
    fn extract_symbols(content: &str) -> Vec<(String, String, usize)> {
        let mut symbols = Vec::new();

        for (line_num, line) in content.lines().enumerate() {
            // Try to parse as JSON
            let obj: Value = match serde_json::from_str(line) {
                Ok(v) => v,
                Err(_) => continue,
            };

            let obj = match obj.as_object() {
                Some(o) => o,
                None => continue,
            };

            // Get type and name fields
            let obj_type = match obj.get("type").and_then(|t| t.as_str()) {
                Some(t) => t,
                None => continue,
            };

            let name = match obj.get("name").and_then(|n| n.as_str()) {
                Some(n) => n,
                None => continue,
            };

            symbols.push((name.to_string(), obj_type.to_string(), line_num));
        }

        symbols
    }

    /// Map pixelsrc type to LSP SymbolKind
    fn type_to_symbol_kind(obj_type: &str) -> SymbolKind {
        match obj_type {
            "palette" => SymbolKind::CONSTANT,
            "sprite" => SymbolKind::CLASS,
            "animation" => SymbolKind::FUNCTION,
            "composition" => SymbolKind::MODULE,
            _ => SymbolKind::OBJECT,
        }
    }

    /// Collect all defined tokens from palettes in the document
    ///
    /// Returns a list of (token, color) pairs from all palette definitions.
    fn collect_defined_tokens(content: &str) -> Vec<(String, String)> {
        let mut tokens = Vec::new();

        for line in content.lines() {
            // Try to parse as JSON
            let obj: Value = match serde_json::from_str(line) {
                Ok(v) => v,
                Err(_) => continue,
            };

            let obj = match obj.as_object() {
                Some(o) => o,
                None => continue,
            };

            // Check if it's a palette
            let obj_type = match obj.get("type").and_then(|t| t.as_str()) {
                Some(t) => t,
                None => continue,
            };

            if obj_type != "palette" {
                continue;
            }

            // Get the colors object
            let colors = match obj.get("colors").and_then(|c| c.as_object()) {
                Some(c) => c,
                None => continue,
            };

            // Extract tokens (keys starting with '{' and ending with '}')
            for (key, value) in colors {
                if key.starts_with('{') && key.ends_with('}') {
                    let color_str = match value.as_str() {
                        Some(s) => s.to_string(),
                        None => continue,
                    };
                    tokens.push((key.clone(), color_str));
                }
            }
        }

        tokens
    }

    /// Collect all CSS variables from palettes in the document
    ///
    /// Returns a list of (variable_name, raw_value, line_number, palette_name) tuples.
    fn collect_css_variables(content: &str) -> Vec<(String, String, usize, String)> {
        let mut variables = Vec::new();

        for (line_num, line) in content.lines().enumerate() {
            // Try to parse as JSON
            let obj: Value = match serde_json::from_str(line) {
                Ok(v) => v,
                Err(_) => continue,
            };

            let obj = match obj.as_object() {
                Some(o) => o,
                None => continue,
            };

            // Check if it's a palette
            let obj_type = match obj.get("type").and_then(|t| t.as_str()) {
                Some(t) => t,
                None => continue,
            };

            if obj_type != "palette" {
                continue;
            }

            let palette_name =
                obj.get("name").and_then(|n| n.as_str()).unwrap_or("unknown").to_string();

            // Get the colors object
            let colors = match obj.get("colors").and_then(|c| c.as_object()) {
                Some(c) => c,
                None => continue,
            };

            // Extract CSS variables (keys starting with '--')
            for (key, value) in colors {
                if key.starts_with("--") {
                    let value_str = match value.as_str() {
                        Some(s) => s.to_string(),
                        None => continue,
                    };
                    variables.push((key.clone(), value_str, line_num, palette_name.clone()));
                }
            }
        }

        variables
    }

    /// Build a VariableRegistry from document content
    fn build_variable_registry(content: &str) -> VariableRegistry {
        let mut registry = VariableRegistry::new();

        for (name, value, _, _) in Self::collect_css_variables(content) {
            registry.define(&name, &value);
        }

        registry
    }

    /// Find the position of a CSS variable definition in content
    ///
    /// Returns (line_number, start_char, end_char) if found.
    fn find_variable_definition(content: &str, var_name: &str) -> Option<(usize, u32, u32)> {
        let normalized_name = if var_name.starts_with("--") {
            var_name.to_string()
        } else {
            format!("--{}", var_name)
        };

        // Search pattern: "var_name": or "var_name" : (with possible spaces)
        let search_key = format!("\"{}\"", normalized_name);

        for (line_num, line) in content.lines().enumerate() {
            if let Some(pos) = line.find(&search_key) {
                // Verify this is in a palette's colors object
                if let Ok(obj) = serde_json::from_str::<Value>(line) {
                    if let Some(obj) = obj.as_object() {
                        if obj.get("type").and_then(|t| t.as_str()) == Some("palette") {
                            if let Some(colors) = obj.get("colors").and_then(|c| c.as_object()) {
                                if colors.contains_key(&normalized_name) {
                                    let start = pos as u32;
                                    let end = start + search_key.len() as u32;
                                    return Some((line_num, start, end));
                                }
                            }
                        }
                    }
                }
            }
        }

        None
    }

    /// Extract CSS variable reference from text at a position
    ///
    /// Handles:
    /// - `var(--name)` - returns Some("--name")
    /// - `var(--name, fallback)` - returns Some("--name")
    /// - Direct definition `"--name": "value"` - returns Some("--name")
    fn extract_variable_at_position(line: &str, char_pos: u32) -> Option<String> {
        let pos = char_pos as usize;
        if pos >= line.len() {
            return None;
        }

        // Check if we're in a var() reference
        if let Some(var_start) = line[..=pos.min(line.len() - 1)].rfind("var(") {
            // Find the end of the var() call
            let after_var = &line[var_start..];
            let mut paren_depth = 0;
            let mut var_end = var_start;

            for (i, c) in after_var.char_indices() {
                match c {
                    '(' => paren_depth += 1,
                    ')' => {
                        paren_depth -= 1;
                        if paren_depth == 0 {
                            var_end = var_start + i;
                            break;
                        }
                    }
                    _ => {}
                }
            }

            // Check if cursor is within this var() call
            if pos >= var_start && pos <= var_end {
                // Extract the variable name from var(--name) or var(--name, fallback)
                let content = &line[var_start + 4..var_end];
                let var_name = if let Some(comma) = content.find(',') {
                    content[..comma].trim()
                } else {
                    content.trim()
                };

                if var_name.starts_with("--") || !var_name.contains('(') {
                    return Some(if var_name.starts_with("--") {
                        var_name.to_string()
                    } else {
                        format!("--{}", var_name)
                    });
                }
            }
        }

        // Check if we're on a variable definition (e.g., "--name": "value")
        // Look for "--" before the cursor position
        let before_pos = &line[..=pos.min(line.len() - 1)];
        if let Some(quote_start) = before_pos.rfind('"') {
            let after_quote = &line[quote_start + 1..];
            if let Some(quote_end) = after_quote.find('"') {
                let potential_var = &after_quote[..quote_end];
                if potential_var.starts_with("--") {
                    // Check if cursor is within or right after this string
                    let string_end = quote_start + 1 + quote_end;
                    if pos >= quote_start && pos <= string_end {
                        return Some(potential_var.to_string());
                    }
                }
            }
        }

        None
    }

    /// Check if cursor is in a context where CSS variable completions should be offered
    ///
    /// Returns true if cursor is after "var(" or "var(--"
    fn is_css_variable_completion_context(line: &str, char_pos: u32) -> bool {
        let pos = char_pos as usize;
        if pos < 4 {
            return false;
        }

        let before = &line[..pos];

        // Check for "var(" or "var(--" patterns
        if before.ends_with("var(") || before.ends_with("var(--") {
            return true;
        }

        // Check if we're inside a var() that's still being typed
        if let Some(var_start) = before.rfind("var(") {
            let in_var = &before[var_start + 4..];
            // We're in a var() context if there's no closing paren yet
            // and we haven't hit a comma (fallback)
            if !in_var.contains(')') && !in_var.contains(',') {
                return true;
            }
        }

        false
    }

    /// Render an ASCII visualization of an easing curve
    ///
    /// Creates a simple ASCII graph showing the easing function's shape.
    fn render_easing_curve(interpolation: &Interpolation, width: usize, height: usize) -> String {
        let mut grid = vec![vec![' '; width]; height];

        // Sample the easing function
        let samples: Vec<f64> = (0..=width)
            .map(|i| {
                let t = i as f64 / width as f64;
                ease(t, interpolation)
            })
            .collect();

        // Find min/max for scaling (handle overshoot)
        let min_val = samples.iter().cloned().fold(f64::INFINITY, f64::min).min(0.0);
        let max_val = samples.iter().cloned().fold(f64::NEG_INFINITY, f64::max).max(1.0);
        let range = max_val - min_val;

        // Plot the curve
        for (x, &value) in samples.iter().enumerate().take(width) {
            // Scale value to grid height
            let normalized = if range > 0.0 { (value - min_val) / range } else { 0.5 };
            let y = ((1.0 - normalized) * (height - 1) as f64).round() as usize;
            let y = y.min(height - 1);
            if x < width {
                grid[y][x] = 'â–ˆ';
            }
        }

        // Build the output with axis labels
        let mut output = String::new();

        // Top label (1.0 or max)
        let top_label = if max_val > 1.0 { format!("{:.1}", max_val) } else { "1.0".to_string() };
        output.push_str(&format!("{:>4}│", top_label));
        output.push_str(&grid[0].iter().collect::<String>());
        output.push('\n');

        // Middle rows
        for row in grid.iter().skip(1).take(height - 2) {
            output.push_str("    │");
            output.push_str(&row.iter().collect::<String>());
            output.push('\n');
        }

        // Bottom row with 0.0 label
        let bottom_label =
            if min_val < 0.0 { format!("{:.1}", min_val) } else { "0.0".to_string() };
        output.push_str(&format!("{:>4}│", bottom_label));
        output.push_str(&grid[height - 1].iter().collect::<String>());
        output.push('\n');

        // X-axis
        output.push_str("    â””");
        output.push_str(&"─".repeat(width));
        output.push('\n');
        output.push_str("     0");
        output.push_str(&" ".repeat(width - 3));
        output.push_str("→ 1");

        output
    }

    /// Get a human-readable description of an interpolation type
    fn describe_interpolation(interpolation: &Interpolation) -> &'static str {
        match interpolation {
            Interpolation::Linear => "Constant speed (no easing)",
            Interpolation::EaseIn => "Slow start, fast end (acceleration)",
            Interpolation::EaseOut => "Fast start, slow end (deceleration)",
            Interpolation::EaseInOut => "Smooth S-curve (slow start and end)",
            Interpolation::Bounce => "Overshoot and settle back",
            Interpolation::Elastic => "Spring-like oscillation",
            Interpolation::Bezier { .. } => "Custom cubic bezier curve",
            Interpolation::Steps { .. } => "Discrete step function",
        }
    }

    /// Get the CSS-canonical form of an interpolation
    fn interpolation_to_css(interpolation: &Interpolation) -> String {
        match interpolation {
            Interpolation::Linear => "linear".to_string(),
            Interpolation::EaseIn => "ease-in".to_string(),
            Interpolation::EaseOut => "ease-out".to_string(),
            Interpolation::EaseInOut => "ease-in-out".to_string(),
            Interpolation::Bounce => "bounce".to_string(),
            Interpolation::Elastic => "elastic".to_string(),
            Interpolation::Bezier { p1, p2 } => {
                format!("cubic-bezier({}, {}, {}, {})", p1.0, p1.1, p2.0, p2.1)
            }
            Interpolation::Steps { count, position } => match position {
                StepPosition::JumpEnd => {
                    if *count == 1 {
                        "step-end".to_string()
                    } else {
                        format!("steps({})", count)
                    }
                }
                StepPosition::JumpStart => {
                    if *count == 1 {
                        "step-start".to_string()
                    } else {
                        format!("steps({}, jump-start)", count)
                    }
                }
                _ => format!("steps({}, {})", count, position),
            },
        }
    }

    /// Parse timing function context from a JSON line at cursor position
    ///
    /// Returns TimingFunctionInfo if the cursor is within a timing_function value.
    fn parse_timing_function_context(line: &str, char_pos: u32) -> Option<TimingFunctionInfo> {
        // Parse the JSON line
        let obj: Value = serde_json::from_str(line).ok()?;
        let obj = obj.as_object()?;

        // Check if this is an animation type
        let obj_type = obj.get("type")?.as_str()?;
        if obj_type != "animation" {
            return None;
        }

        // Look for timing_function field
        let timing_str = obj.get("timing_function")?.as_str()?;

        // Find the timing_function key position in the raw JSON
        let key_pos = line.find("\"timing_function\"")?;

        // Find the colon after the key
        let after_key = &line[key_pos..];
        let colon_offset = after_key.find(':')?;

        // Find the opening quote of the value
        let after_colon = &after_key[colon_offset..];
        let quote_offset = after_colon.find('"')?;
        let value_start = key_pos + colon_offset + quote_offset + 1;

        // Find the closing quote
        let value_end = value_start + timing_str.len();

        // Check if cursor is within the value
        let char_pos = char_pos as usize;
        if char_pos < value_start || char_pos > value_end {
            return None;
        }

        // Parse the timing function
        let interpolation = parse_timing_function(timing_str).ok()?;

        Some(TimingFunctionInfo { function_str: timing_str.to_string(), interpolation })
    }

    /// Extract all colors from a palette line
    ///
    /// Finds color values in palette definitions and resolves var() references.
    fn extract_colors_from_line(
        line: &str,
        line_num: u32,
        var_registry: &VariableRegistry,
    ) -> Vec<(ColorMatch, u32)> {
        let mut matches = Vec::new();

        // Try to parse as JSON
        let obj: Value = match serde_json::from_str(line) {
            Ok(v) => v,
            Err(_) => return matches,
        };

        let obj = match obj.as_object() {
            Some(o) => o,
            None => return matches,
        };

        // Check if it's a palette
        let obj_type = match obj.get("type").and_then(|t| t.as_str()) {
            Some(t) => t,
            None => return matches,
        };

        if obj_type != "palette" {
            return matches;
        }

        // Get the colors object
        let colors = match obj.get("colors").and_then(|c| c.as_object()) {
            Some(c) => c,
            None => return matches,
        };

        // Find the "colors" key position in the line
        let colors_key_pos = match line.find("\"colors\"") {
            Some(pos) => pos,
            None => return matches,
        };

        // Find each color value in the colors object
        for (key, value) in colors {
            let color_str = match value.as_str() {
                Some(s) => s,
                None => continue,
            };

            // Skip CSS variable definitions (they're not colors themselves)
            if key.starts_with("--") {
                continue;
            }

            // Find the position of this color value in the line
            // We search for the pattern "key": "value"
            let search_pattern = format!("\"{}\": \"{}\"", key, color_str);
            let alt_pattern = format!("\"{}\":\"{}\"", key, color_str);

            let value_start = if let Some(pos) = line[colors_key_pos..].find(&search_pattern) {
                let key_start = colors_key_pos + pos;
                // Find the start of the value string (after ": ")
                key_start + key.len() + 5 // ": " + opening quote
            } else if let Some(pos) = line[colors_key_pos..].find(&alt_pattern) {
                let key_start = colors_key_pos + pos;
                key_start + key.len() + 4 // ":" + opening quote
            } else {
                continue;
            };

            let value_end = value_start + color_str.len();

            // Resolve var() references if present
            let resolved_value = if color_str.contains("var(") {
                match var_registry.resolve(color_str) {
                    Ok(resolved) => resolved,
                    Err(_) => color_str.to_string(),
                }
            } else {
                color_str.to_string()
            };

            // Try to parse the resolved color
            if let Ok(rgba) = parse_color(&resolved_value) {
                matches.push((
                    ColorMatch {
                        original: color_str.to_string(),
                        rgba: (
                            rgba.0[0] as f32 / 255.0,
                            rgba.0[1] as f32 / 255.0,
                            rgba.0[2] as f32 / 255.0,
                            rgba.0[3] as f32 / 255.0,
                        ),
                        start: value_start as u32,
                        end: value_end as u32,
                    },
                    line_num,
                ));
            }
        }

        matches
    }

    /// Convert RGBA values (0.0-1.0) to hex string
    fn rgba_to_hex(r: f32, g: f32, b: f32, a: f32) -> String {
        let r = (r * 255.0).round() as u8;
        let g = (g * 255.0).round() as u8;
        let b = (b * 255.0).round() as u8;
        let a = (a * 255.0).round() as u8;

        if a == 255 {
            format!("#{:02X}{:02X}{:02X}", r, g, b)
        } else {
            format!("#{:02X}{:02X}{:02X}{:02X}", r, g, b, a)
        }
    }

    /// Convert RGBA values (0.0-1.0) to rgb() or rgba() string
    fn rgba_to_rgb_functional(r: f32, g: f32, b: f32, a: f32) -> String {
        let r = (r * 255.0).round() as u8;
        let g = (g * 255.0).round() as u8;
        let b = (b * 255.0).round() as u8;

        if a >= 0.999 {
            format!("rgb({}, {}, {})", r, g, b)
        } else {
            format!("rgba({}, {}, {}, {:.2})", r, g, b, a)
        }
    }

    /// Convert RGBA values (0.0-1.0) to hsl() or hsla() string
    fn rgba_to_hsl(r: f32, g: f32, b: f32, a: f32) -> String {
        // Convert RGB to HSL
        let max = r.max(g).max(b);
        let min = r.min(g).min(b);
        let l = (max + min) / 2.0;

        if (max - min).abs() < 0.0001 {
            // Achromatic
            if a >= 0.999 {
                format!("hsl(0, 0%, {}%)", (l * 100.0).round() as u32)
            } else {
                format!("hsla(0, 0%, {}%, {:.2})", (l * 100.0).round() as u32, a)
            }
        } else {
            let d = max - min;
            let s = if l > 0.5 { d / (2.0 - max - min) } else { d / (max + min) };

            let h = if (max - r).abs() < 0.0001 {
                ((g - b) / d + if g < b { 6.0 } else { 0.0 }) / 6.0
            } else if (max - g).abs() < 0.0001 {
                ((b - r) / d + 2.0) / 6.0
            } else {
                ((r - g) / d + 4.0) / 6.0
            };

            let h_deg = (h * 360.0).round() as u32;
            let s_pct = (s * 100.0).round() as u32;
            let l_pct = (l * 100.0).round() as u32;

            if a >= 0.999 {
                format!("hsl({}, {}%, {}%)", h_deg, s_pct, l_pct)
            } else {
                format!("hsla({}, {}%, {}%, {:.2})", h_deg, s_pct, l_pct, a)
            }
        }
    }
}

#[tower_lsp::async_trait]
impl LanguageServer for PixelsrcLanguageServer {
    async fn initialize(&self, _params: InitializeParams) -> Result<InitializeResult> {
        Ok(InitializeResult {
            server_info: Some(ServerInfo {
                name: "pixelsrc-lsp".to_string(),
                version: Some(env!("CARGO_PKG_VERSION").to_string()),
            }),
            capabilities: ServerCapabilities {
                text_document_sync: Some(TextDocumentSyncCapability::Kind(
                    TextDocumentSyncKind::FULL,
                )),
                hover_provider: Some(HoverProviderCapability::Simple(true)),
                completion_provider: Some(CompletionOptions {
                    trigger_characters: Some(vec![
                        "{".to_string(),
                        "-".to_string(),
                        "(".to_string(),
                    ]),
                    ..Default::default()
                }),
                document_symbol_provider: Some(OneOf::Left(true)),
                definition_provider: Some(OneOf::Left(true)),
                color_provider: Some(ColorProviderCapability::Simple(true)),
                ..Default::default()
            },
        })
    }

    async fn initialized(&self, _params: InitializedParams) {
        self.client.log_message(MessageType::INFO, "Pixelsrc LSP initialized").await;
    }

    async fn shutdown(&self) -> Result<()> {
        Ok(())
    }

    async fn did_open(&self, params: DidOpenTextDocumentParams) {
        let uri = params.text_document.uri;
        let text = params.text_document.text;

        // Store document content
        self.documents.write().unwrap().insert(uri.clone(), text.clone());

        // Validate and publish diagnostics
        self.validate_and_publish(&uri, &text).await;
    }

    async fn did_change(&self, params: DidChangeTextDocumentParams) {
        let uri = params.text_document.uri;

        // Get the full text from the first change (we use FULL sync)
        if let Some(change) = params.content_changes.into_iter().next() {
            // Store updated content
            self.documents.write().unwrap().insert(uri.clone(), change.text.clone());

            // Validate and publish diagnostics
            self.validate_and_publish(&uri, &change.text).await;
        }
    }

    async fn did_close(&self, params: DidCloseTextDocumentParams) {
        let uri = params.text_document.uri;

        // Remove document from tracking
        self.documents.write().unwrap().remove(&uri);

        // Clear diagnostics for closed document
        self.client.publish_diagnostics(uri, Vec::new(), None).await;
    }

    async fn hover(&self, params: HoverParams) -> Result<Option<Hover>> {
        let uri = &params.text_document_position_params.text_document.uri;
        let pos = params.text_document_position_params.position;

        // Get the document content
        let documents = self.documents.read().unwrap();
        let content = match documents.get(uri) {
            Some(c) => c.clone(),
            None => return Ok(None),
        };
        drop(documents); // Release lock before async work

        // Get the line at the cursor position
        let line = match content.lines().nth(pos.line as usize) {
            Some(l) => l,
            None => return Ok(None),
        };

        // Try to parse grid context at the cursor position
        if let Some(grid_info) = Self::parse_grid_context(line, pos.character) {
            // Format alignment status
            let alignment_status = if grid_info.row_width == grid_info.expected_width {
                "✓ Aligned".to_string()
            } else if grid_info.row_width < grid_info.expected_width {
                format!("âš  Short by {} token(s)", grid_info.expected_width - grid_info.row_width)
            } else {
                format!("âš  Long by {} token(s)", grid_info.row_width - grid_info.expected_width)
            };

            let hover_text = format!(
                "**Grid Position**: ({}, {})\n\n\
                 **Token**: `{}`\n\n\
                 **Row Width**: {} tokens\n\n\
                 **Expected Width**: {} tokens\n\n\
                 **Status**: {}\n\n\
                 **Sprite**: `{}`",
                grid_info.x,
                grid_info.y,
                grid_info.token,
                grid_info.row_width,
                grid_info.expected_width,
                alignment_status,
                grid_info.sprite_name,
            );

            return Ok(Some(Hover {
                contents: HoverContents::Markup(MarkupContent {
                    kind: MarkupKind::Markdown,
                    value: hover_text,
                }),
                range: None,
            }));
        }

        // Check for CSS variable reference
        if let Some(var_name) = Self::extract_variable_at_position(line, pos.character) {
            let css_variables = Self::collect_css_variables(&content);
            let registry = Self::build_variable_registry(&content);

            // Find the variable's info
            let var_info = css_variables.iter().find(|(name, _, _, _)| name == &var_name);

            if let Some((_, raw_value, _, palette_name)) = var_info {
                // Try to resolve the variable
                let resolved = registry.resolve_var(&var_name);

                let hover_text = match resolved {
                    Ok(resolved_value) => {
                        if &resolved_value == raw_value {
                            format!(
                                "**CSS Variable**: `{}`\n\n\
                                 **Value**: `{}`\n\n\
                                 **Palette**: `{}`",
                                var_name, raw_value, palette_name
                            )
                        } else {
                            format!(
                                "**CSS Variable**: `{}`\n\n\
                                 **Raw Value**: `{}`\n\n\
                                 **Resolved**: `{}`\n\n\
                                 **Palette**: `{}`",
                                var_name, raw_value, resolved_value, palette_name
                            )
                        }
                    }
                    Err(e) => {
                        format!(
                            "**CSS Variable**: `{}`\n\n\
                             **Raw Value**: `{}`\n\n\
                             **Error**: {}\n\n\
                             **Palette**: `{}`",
                            var_name, raw_value, e, palette_name
                        )
                    }
                };

                return Ok(Some(Hover {
                    contents: HoverContents::Markup(MarkupContent {
                        kind: MarkupKind::Markdown,
                        value: hover_text,
                    }),
                    range: None,
                }));
            } else {
                // Variable referenced but not defined
                return Ok(Some(Hover {
                    contents: HoverContents::Markup(MarkupContent {
                        kind: MarkupKind::Markdown,
                        value: format!(
                            "**CSS Variable**: `{}`\n\n\
                             âš  **Undefined** - This variable is not defined in any palette",
                            var_name
                        ),
                    }),
                    range: None,
                }));
            }
        }

        // Try to parse timing function context at the cursor position
        if let Some(timing_info) = Self::parse_timing_function_context(line, pos.character) {
            // Render the ASCII easing curve (25 chars wide, 8 rows tall)
            let curve = Self::render_easing_curve(&timing_info.interpolation, 25, 8);
            let description = Self::describe_interpolation(&timing_info.interpolation);
            let css_form = Self::interpolation_to_css(&timing_info.interpolation);

            let hover_text = format!(
                "**Timing Function**: `{}`\n\n\
                 **Type**: {}\n\n\
                 **CSS**: `{}`\n\n\
                 ```\n{}\n```",
                timing_info.function_str, description, css_form, curve,
            );

            return Ok(Some(Hover {
                contents: HoverContents::Markup(MarkupContent {
                    kind: MarkupKind::Markdown,
                    value: hover_text,
                }),
                range: None,
            }));
        }

        // Try to parse transform context at the cursor position
        if let Some(transform_info) = Self::parse_transform_context(line, pos.character) {
            let explanation = explain_transform(&transform_info.transform);

            // Build the hover text with context
            let position_text = if transform_info.total == 1 {
                String::new()
            } else {
                format!(
                    "\n\n**Position**: {} of {} transforms",
                    transform_info.index + 1,
                    transform_info.total
                )
            };

            let hover_text = format!(
                "**Transform**: `{}`\n\n\
                 **Effect**: {}\n\n\
                 **Applied to**: {} `{}`{}",
                transform_info.raw,
                explanation,
                transform_info.object_type,
                transform_info.object_name,
                position_text,
            );

            return Ok(Some(Hover {
                contents: HoverContents::Markup(MarkupContent {
                    kind: MarkupKind::Markdown,
                    value: hover_text,
                }),
                range: None,
            }));
        }

        Ok(None)
    }

    async fn completion(&self, params: CompletionParams) -> Result<Option<CompletionResponse>> {
        let uri = &params.text_document_position.text_document.uri;
        let pos = params.text_document_position.position;

        // Get the document content
        let documents = self.documents.read().unwrap();
        let content = match documents.get(uri) {
            Some(c) => c.clone(),
            None => return Ok(None),
        };
        drop(documents);

        // Get the current line
        let current_line = content.lines().nth(pos.line as usize).unwrap_or("");

        // Check if we're in a CSS variable completion context (inside var())
        if Self::is_css_variable_completion_context(current_line, pos.character) {
            let css_variables = Self::collect_css_variables(&content);
            let registry = Self::build_variable_registry(&content);

            let mut completions: Vec<CompletionItem> = Vec::new();

            for (var_name, raw_value, _, palette_name) in css_variables {
                // Try to resolve the variable for the detail
                let resolved = registry.resolve_var(&var_name);
                let detail = match resolved {
                    Ok(resolved_value) => {
                        if resolved_value == raw_value {
                            format!("{} ({})", raw_value, palette_name)
                        } else {
                            format!("{} → {} ({})", raw_value, resolved_value, palette_name)
                        }
                    }
                    Err(_) => format!("{} ({})", raw_value, palette_name),
                };

                completions.push(CompletionItem {
                    label: var_name.clone(),
                    detail: Some(detail),
                    kind: Some(CompletionItemKind::VARIABLE),
                    insert_text: Some(var_name),
                    ..Default::default()
                });
            }

            return Ok(Some(CompletionResponse::Array(completions)));
        }

        // Standard token completions (for grid context)
        let defined_tokens = Self::collect_defined_tokens(&content);

        // Build completion items
        let mut completions: Vec<CompletionItem> = Vec::new();

        // Add built-in transparent token
        completions.push(CompletionItem {
            label: "{_}".to_string(),
            detail: Some("Transparent (built-in)".to_string()),
            kind: Some(CompletionItemKind::COLOR),
            insert_text: Some("{_}".to_string()),
            ..Default::default()
        });

        // Add the standard dot token for transparent
        completions.push(CompletionItem {
            label: ".".to_string(),
            detail: Some("Transparent (shorthand)".to_string()),
            kind: Some(CompletionItemKind::COLOR),
            insert_text: Some(".".to_string()),
            ..Default::default()
        });

        // Add defined tokens from palettes
        for (token, color) in defined_tokens {
            completions.push(CompletionItem {
                label: token.clone(),
                detail: Some(color),
                kind: Some(CompletionItemKind::COLOR),
                insert_text: Some(token),
                ..Default::default()
            });
        }

        Ok(Some(CompletionResponse::Array(completions)))
    }

    async fn document_symbol(
        &self,
        params: DocumentSymbolParams,
    ) -> Result<Option<DocumentSymbolResponse>> {
        let uri = &params.text_document.uri;

        // Get the document content
        let documents = self.documents.read().unwrap();
        let content = match documents.get(uri) {
            Some(c) => c.clone(),
            None => return Ok(None),
        };
        drop(documents);

        // Extract symbols using helper method
        let extracted = Self::extract_symbols(&content);

        // Convert to SymbolInformation
        let symbols: Vec<SymbolInformation> = extracted
            .into_iter()
            .map(|(name, obj_type, line_num)| {
                let line = content.lines().nth(line_num).unwrap_or("");
                #[allow(deprecated)]
                SymbolInformation {
                    name,
                    kind: Self::type_to_symbol_kind(&obj_type),
                    tags: None,
                    deprecated: None,
                    location: Location {
                        uri: uri.clone(),
                        range: Range {
                            start: Position { line: line_num as u32, character: 0 },
                            end: Position { line: line_num as u32, character: line.len() as u32 },
                        },
                    },
                    container_name: None,
                }
            })
            .collect();

        Ok(Some(DocumentSymbolResponse::Flat(symbols)))
    }

    async fn document_color(&self, params: DocumentColorParams) -> Result<Vec<ColorInformation>> {
        let uri = &params.text_document.uri;

        // Get the document content
        let documents = self.documents.read().unwrap();
        let content = match documents.get(uri) {
            Some(c) => c.clone(),
            None => return Ok(Vec::new()),
        };
        drop(documents);

        // Build variable registry from all palettes for var() resolution
        let var_registry = Self::build_variable_registry(&content);

        // Extract all colors from palette definitions
        let mut colors = Vec::new();

        for (line_num, line) in content.lines().enumerate() {
            let line_colors = Self::extract_colors_from_line(line, line_num as u32, &var_registry);
            for (color_match, line_idx) in line_colors {
                colors.push(ColorInformation {
                    range: Range {
                        start: Position { line: line_idx, character: color_match.start },
                        end: Position { line: line_idx, character: color_match.end },
                    },
                    color: Color {
                        red: color_match.rgba.0,
                        green: color_match.rgba.1,
                        blue: color_match.rgba.2,
                        alpha: color_match.rgba.3,
                    },
                });
            }
        }

        Ok(colors)
    }

    async fn color_presentation(
        &self,
        params: ColorPresentationParams,
    ) -> Result<Vec<ColorPresentation>> {
        let color = params.color;
        let r = color.red;
        let g = color.green;
        let b = color.blue;
        let a = color.alpha;

        // Provide multiple format options when user picks a color
        let mut presentations = Vec::new();

        // Hex format (most common for pixel art)
        let hex = Self::rgba_to_hex(r, g, b, a);
        presentations.push(ColorPresentation {
            label: hex.clone(),
            text_edit: Some(TextEdit { range: params.range, new_text: hex }),
            additional_text_edits: None,
        });

        // RGB functional format
        let rgb = Self::rgba_to_rgb_functional(r, g, b, a);
        presentations.push(ColorPresentation {
            label: rgb.clone(),
            text_edit: Some(TextEdit { range: params.range, new_text: rgb }),
            additional_text_edits: None,
        });

        // HSL format
        let hsl = Self::rgba_to_hsl(r, g, b, a);
        presentations.push(ColorPresentation {
            label: hsl.clone(),
            text_edit: Some(TextEdit { range: params.range, new_text: hsl }),
            additional_text_edits: None,
        });

        Ok(presentations)
    }

    async fn goto_definition(
        &self,
        params: GotoDefinitionParams,
    ) -> Result<Option<GotoDefinitionResponse>> {
        let uri = &params.text_document_position_params.text_document.uri;
        let pos = params.text_document_position_params.position;

        // Get the document content
        let documents = self.documents.read().unwrap();
        let content = match documents.get(uri) {
            Some(c) => c.clone(),
            None => return Ok(None),
        };
        drop(documents);

        // Get the line at the cursor position
        let line = match content.lines().nth(pos.line as usize) {
            Some(l) => l,
            None => return Ok(None),
        };

        // Check if cursor is on a CSS variable reference
        if let Some(var_name) = Self::extract_variable_at_position(line, pos.character) {
            // Find where this variable is defined
            if let Some((def_line, start_char, end_char)) =
                Self::find_variable_definition(&content, &var_name)
            {
                return Ok(Some(GotoDefinitionResponse::Scalar(Location {
                    uri: uri.clone(),
                    range: Range {
                        start: Position { line: def_line as u32, character: start_char },
                        end: Position { line: def_line as u32, character: end_char },
                    },
                })));
            }
        }

        Ok(None)
    }
}

/// Run the LSP server on stdin/stdout
pub async fn run_server() {
    let stdin = tokio::io::stdin();
    let stdout = tokio::io::stdout();

    let (service, socket) = LspService::new(PixelsrcLanguageServer::new);
    Server::new(stdin, stdout, socket).serve(service).await;
}

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

    #[test]
    fn test_issue_to_diagnostic_error() {
        let issue = ValidationIssue::error(5, IssueType::JsonSyntax, "Invalid JSON");
        let diagnostic = PixelsrcLanguageServer::issue_to_diagnostic(&issue);

        assert_eq!(diagnostic.range.start.line, 4); // 0-indexed
        assert_eq!(diagnostic.range.end.line, 4);
        assert_eq!(diagnostic.severity, Some(DiagnosticSeverity::ERROR));
        assert_eq!(diagnostic.message, "Invalid JSON");
        assert_eq!(diagnostic.source, Some("pixelsrc".to_string()));
        assert_eq!(diagnostic.code, Some(NumberOrString::String("json_syntax".to_string())));
    }

    #[test]
    fn test_issue_to_diagnostic_warning() {
        let issue = ValidationIssue::warning(10, IssueType::UndefinedToken, "Undefined token {x}");
        let diagnostic = PixelsrcLanguageServer::issue_to_diagnostic(&issue);

        assert_eq!(diagnostic.range.start.line, 9); // 0-indexed
        assert_eq!(diagnostic.severity, Some(DiagnosticSeverity::WARNING));
        assert_eq!(diagnostic.message, "Undefined token {x}");
    }

    #[test]
    fn test_issue_to_diagnostic_with_suggestion() {
        let issue =
            ValidationIssue::warning(3, IssueType::UndefinedToken, "Undefined token {skni}")
                .with_suggestion("did you mean {skin}?");
        let diagnostic = PixelsrcLanguageServer::issue_to_diagnostic(&issue);

        assert_eq!(diagnostic.range.start.line, 2); // 0-indexed
        assert_eq!(diagnostic.message, "Undefined token {skni} (did you mean {skin}?)");
    }

    #[test]
    fn test_parse_grid_context_first_row_first_token() {
        let line = r#"{"type": "sprite", "name": "test", "grid": ["{a}{b}{c}"]}"#;
        // Find position of first {a} - after the opening quote of the first grid row
        let grid_start = line.find("[\"").unwrap() + 2; // Position after ["
        let info = PixelsrcLanguageServer::parse_grid_context(line, grid_start as u32);

        assert!(info.is_some());
        let info = info.unwrap();
        assert_eq!(info.x, 0);
        assert_eq!(info.y, 0);
        assert_eq!(info.token, "{a}");
        assert_eq!(info.row_width, 3);
        assert_eq!(info.expected_width, 3);
        assert_eq!(info.sprite_name, "test");
    }

    #[test]
    fn test_parse_grid_context_second_token() {
        let line = r#"{"type": "sprite", "name": "test", "grid": ["{a}{b}{c}"]}"#;
        // Position within {b}
        let grid_start = line.find("[\"").unwrap() + 2 + 3; // After [" and {a}
        let info = PixelsrcLanguageServer::parse_grid_context(line, grid_start as u32);

        assert!(info.is_some());
        let info = info.unwrap();
        assert_eq!(info.x, 1);
        assert_eq!(info.y, 0);
        assert_eq!(info.token, "{b}");
    }

    #[test]
    fn test_parse_grid_context_second_row() {
        let line = r#"{"type": "sprite", "name": "test", "grid": ["{a}{a}", "{b}{b}"]}"#;
        // Find position within second row
        let second_row_start = line.rfind("\"{b}").unwrap() + 1; // Position after the quote
        let info = PixelsrcLanguageServer::parse_grid_context(line, second_row_start as u32);

        assert!(info.is_some());
        let info = info.unwrap();
        assert_eq!(info.x, 0);
        assert_eq!(info.y, 1);
        assert_eq!(info.token, "{b}");
    }

    #[test]
    fn test_parse_grid_context_with_size() {
        let line = r#"{"type": "sprite", "name": "sized", "size": [4, 2], "grid": ["{a}{a}"]}"#;
        let grid_start = line.find("[\"").unwrap() + 2;
        let info = PixelsrcLanguageServer::parse_grid_context(line, grid_start as u32);

        assert!(info.is_some());
        let info = info.unwrap();
        assert_eq!(info.row_width, 2);
        assert_eq!(info.expected_width, 4); // From size field
    }

    #[test]
    fn test_parse_grid_context_not_sprite() {
        let line = r##"{"type": "palette", "name": "test", "colors": {"{a}": "#FF0000"}}"##;
        let info = PixelsrcLanguageServer::parse_grid_context(line, 50);
        assert!(info.is_none());
    }

    #[test]
    fn test_parse_grid_context_outside_grid() {
        let line = r#"{"type": "sprite", "name": "test", "grid": ["{a}{b}"]}"#;
        // Position before the grid array
        let info = PixelsrcLanguageServer::parse_grid_context(line, 10);
        assert!(info.is_none());
    }

    #[test]
    fn test_collect_defined_tokens_single_palette() {
        let content = r##"{"type": "palette", "name": "test", "colors": {"{a}": "#FF0000", "{b}": "#00FF00", "--var": "100"}}"##;
        let tokens = PixelsrcLanguageServer::collect_defined_tokens(content);

        assert_eq!(tokens.len(), 2); // Only {a} and {b}, not --var
        assert!(tokens.iter().any(|(t, c)| t == "{a}" && c == "#FF0000"));
        assert!(tokens.iter().any(|(t, c)| t == "{b}" && c == "#00FF00"));
    }

    #[test]
    fn test_collect_defined_tokens_multiple_palettes() {
        let content = r##"{"type": "palette", "name": "p1", "colors": {"{red}": "#FF0000"}}
{"type": "palette", "name": "p2", "colors": {"{blue}": "#0000FF"}}
{"type": "sprite", "name": "s", "grid": ["{red}{blue}"]}"##;
        let tokens = PixelsrcLanguageServer::collect_defined_tokens(content);

        assert_eq!(tokens.len(), 2);
        assert!(tokens.iter().any(|(t, _)| t == "{red}"));
        assert!(tokens.iter().any(|(t, _)| t == "{blue}"));
    }

    #[test]
    fn test_collect_defined_tokens_no_palettes() {
        let content = r#"{"type": "sprite", "name": "s", "grid": ["{a}{b}"]}"#;
        let tokens = PixelsrcLanguageServer::collect_defined_tokens(content);
        assert!(tokens.is_empty());
    }

    #[test]
    fn test_collect_defined_tokens_empty_content() {
        let tokens = PixelsrcLanguageServer::collect_defined_tokens("");
        assert!(tokens.is_empty());
    }

    // === Document Symbol Tests ===

    #[test]
    fn test_extract_symbols_single_palette() {
        let content = r##"{"type": "palette", "name": "hero", "colors": {"{a}": "#FF0000"}}"##;
        let symbols = PixelsrcLanguageServer::extract_symbols(content);

        assert_eq!(symbols.len(), 1);
        assert_eq!(symbols[0].0, "hero");
        assert_eq!(symbols[0].1, "palette");
        assert_eq!(symbols[0].2, 0);
    }

    #[test]
    fn test_extract_symbols_single_sprite() {
        let content = r#"{"type": "sprite", "name": "player", "grid": ["{a}"]}"#;
        let symbols = PixelsrcLanguageServer::extract_symbols(content);

        assert_eq!(symbols.len(), 1);
        assert_eq!(symbols[0].0, "player");
        assert_eq!(symbols[0].1, "sprite");
        assert_eq!(symbols[0].2, 0);
    }

    #[test]
    fn test_extract_symbols_animation() {
        let content = r#"{"type": "animation", "name": "walk_cycle", "frames": ["frame1"]}"#;
        let symbols = PixelsrcLanguageServer::extract_symbols(content);

        assert_eq!(symbols.len(), 1);
        assert_eq!(symbols[0].0, "walk_cycle");
        assert_eq!(symbols[0].1, "animation");
    }

    #[test]
    fn test_extract_symbols_composition() {
        let content = r#"{"type": "composition", "name": "scene1", "layers": []}"#;
        let symbols = PixelsrcLanguageServer::extract_symbols(content);

        assert_eq!(symbols.len(), 1);
        assert_eq!(symbols[0].0, "scene1");
        assert_eq!(symbols[0].1, "composition");
    }

    #[test]
    fn test_extract_symbols_multiple_objects() {
        let content = r##"{"type": "palette", "name": "colors", "colors": {"{a}": "#FF0000"}}
{"type": "sprite", "name": "hero", "grid": ["{a}"]}
{"type": "sprite", "name": "enemy", "grid": ["{a}"]}
{"type": "animation", "name": "idle", "frames": ["hero"]}"##;
        let symbols = PixelsrcLanguageServer::extract_symbols(content);

        assert_eq!(symbols.len(), 4);

        // Check names in order
        assert_eq!(symbols[0].0, "colors");
        assert_eq!(symbols[0].1, "palette");
        assert_eq!(symbols[0].2, 0);

        assert_eq!(symbols[1].0, "hero");
        assert_eq!(symbols[1].1, "sprite");
        assert_eq!(symbols[1].2, 1);

        assert_eq!(symbols[2].0, "enemy");
        assert_eq!(symbols[2].1, "sprite");
        assert_eq!(symbols[2].2, 2);

        assert_eq!(symbols[3].0, "idle");
        assert_eq!(symbols[3].1, "animation");
        assert_eq!(symbols[3].2, 3);
    }

    #[test]
    fn test_extract_symbols_skips_invalid_json() {
        let content = r##"this is not json
{"type": "palette", "name": "valid", "colors": {}}
also not json"##;
        let symbols = PixelsrcLanguageServer::extract_symbols(content);

        assert_eq!(symbols.len(), 1);
        assert_eq!(symbols[0].0, "valid");
        assert_eq!(symbols[0].2, 1); // Line 1 (0-indexed)
    }

    #[test]
    fn test_extract_symbols_skips_missing_type() {
        let content = r#"{"name": "no_type", "colors": {}}"#;
        let symbols = PixelsrcLanguageServer::extract_symbols(content);
        assert!(symbols.is_empty());
    }

    #[test]
    fn test_extract_symbols_skips_missing_name() {
        let content = r#"{"type": "palette", "colors": {}}"#;
        let symbols = PixelsrcLanguageServer::extract_symbols(content);
        assert!(symbols.is_empty());
    }

    #[test]
    fn test_extract_symbols_empty_content() {
        let symbols = PixelsrcLanguageServer::extract_symbols("");
        assert!(symbols.is_empty());
    }

    #[test]
    fn test_type_to_symbol_kind_palette() {
        assert_eq!(PixelsrcLanguageServer::type_to_symbol_kind("palette"), SymbolKind::CONSTANT);
    }

    #[test]
    fn test_type_to_symbol_kind_sprite() {
        assert_eq!(PixelsrcLanguageServer::type_to_symbol_kind("sprite"), SymbolKind::CLASS);
    }

    #[test]
    fn test_type_to_symbol_kind_animation() {
        assert_eq!(PixelsrcLanguageServer::type_to_symbol_kind("animation"), SymbolKind::FUNCTION);
    }

    #[test]
    fn test_type_to_symbol_kind_composition() {
        assert_eq!(PixelsrcLanguageServer::type_to_symbol_kind("composition"), SymbolKind::MODULE);
    }

    #[test]
    fn test_type_to_symbol_kind_unknown() {
        assert_eq!(PixelsrcLanguageServer::type_to_symbol_kind("unknown"), SymbolKind::OBJECT);
    }

    // === CSS Variable Tests ===

    #[test]
    fn test_collect_css_variables_single_palette() {
        let content = r##"{"type": "palette", "name": "hero", "colors": {"--primary": "#FF0000", "{body}": "var(--primary)"}}"##;
        let variables = PixelsrcLanguageServer::collect_css_variables(content);

        assert_eq!(variables.len(), 1);
        assert_eq!(variables[0].0, "--primary");
        assert_eq!(variables[0].1, "#FF0000");
        assert_eq!(variables[0].2, 0); // Line 0
        assert_eq!(variables[0].3, "hero"); // Palette name
    }

    #[test]
    fn test_collect_css_variables_multiple_palettes() {
        let content = r##"{"type": "palette", "name": "p1", "colors": {"--color1": "#FF0000"}}
{"type": "palette", "name": "p2", "colors": {"--color2": "#00FF00", "--color3": "var(--color1)"}}"##;
        let variables = PixelsrcLanguageServer::collect_css_variables(content);

        assert_eq!(variables.len(), 3);

        // Check that we have all variables
        assert!(variables.iter().any(|(n, _, _, _)| n == "--color1"));
        assert!(variables.iter().any(|(n, _, _, _)| n == "--color2"));
        assert!(variables.iter().any(|(n, _, _, _)| n == "--color3"));
    }

    #[test]
    fn test_collect_css_variables_no_variables() {
        let content = r##"{"type": "palette", "name": "simple", "colors": {"{red}": "#FF0000"}}"##;
        let variables = PixelsrcLanguageServer::collect_css_variables(content);
        assert!(variables.is_empty());
    }

    #[test]
    fn test_build_variable_registry() {
        let content = r##"{"type": "palette", "name": "test", "colors": {"--primary": "#FF0000", "--secondary": "var(--primary)"}}"##;
        let registry = PixelsrcLanguageServer::build_variable_registry(content);

        assert!(registry.contains("--primary"));
        assert!(registry.contains("--secondary"));
        assert_eq!(registry.resolve_var("--primary").unwrap(), "#FF0000");
        assert_eq!(registry.resolve_var("--secondary").unwrap(), "#FF0000");
    }

    #[test]
    fn test_find_variable_definition_exists() {
        let content =
            r##"{"type": "palette", "name": "test", "colors": {"--primary": "#FF0000"}}"##;
        let result = PixelsrcLanguageServer::find_variable_definition(content, "--primary");

        assert!(result.is_some());
        let (line, start, end) = result.unwrap();
        assert_eq!(line, 0);
        assert!(start > 0);
        assert!(end > start);
    }

    #[test]
    fn test_find_variable_definition_without_dashes() {
        let content =
            r##"{"type": "palette", "name": "test", "colors": {"--primary": "#FF0000"}}"##;
        // Should work even without the -- prefix
        let result = PixelsrcLanguageServer::find_variable_definition(content, "primary");

        assert!(result.is_some());
    }

    #[test]
    fn test_find_variable_definition_not_found() {
        let content = r##"{"type": "palette", "name": "test", "colors": {"{red}": "#FF0000"}}"##;
        let result = PixelsrcLanguageServer::find_variable_definition(content, "--missing");
        assert!(result.is_none());
    }

    #[test]
    fn test_extract_variable_at_position_var_reference() {
        let line = r#""{body}": "var(--primary)""#;
        // Position inside var(--primary)
        let pos = line.find("--primary").unwrap() as u32;
        let result = PixelsrcLanguageServer::extract_variable_at_position(line, pos);

        assert!(result.is_some());
        assert_eq!(result.unwrap(), "--primary");
    }

    #[test]
    fn test_extract_variable_at_position_var_with_fallback() {
        let line = r#""{body}": "var(--primary, #FF0000)""#;
        let pos = line.find("--primary").unwrap() as u32;
        let result = PixelsrcLanguageServer::extract_variable_at_position(line, pos);

        assert!(result.is_some());
        assert_eq!(result.unwrap(), "--primary");
    }

    #[test]
    fn test_extract_variable_at_position_definition() {
        let line = r##""--primary": "#FF0000""##;
        // Position on the variable name in the definition
        let pos = line.find("--primary").unwrap() as u32;
        let result = PixelsrcLanguageServer::extract_variable_at_position(line, pos);

        assert!(result.is_some());
        assert_eq!(result.unwrap(), "--primary");
    }

    #[test]
    fn test_extract_variable_at_position_not_on_variable() {
        let line = r##""{body}": "#FF0000""##;
        let result = PixelsrcLanguageServer::extract_variable_at_position(line, 5);
        assert!(result.is_none());
    }

    #[test]
    fn test_is_css_variable_completion_context_after_var_open() {
        let line = r#""{body}": "var("#;
        assert!(PixelsrcLanguageServer::is_css_variable_completion_context(
            line,
            line.len() as u32
        ));
    }

    // === Timing Function Visualization Tests ===

    #[test]
    fn test_parse_timing_function_context_ease_in() {
        let line = r#"{"type": "animation", "name": "bounce", "timing_function": "ease-in", "frames": []}"#;
        // Find position within "ease-in" value
        let value_start = line.find("\"ease-in\"").unwrap() + 1;
        let info = PixelsrcLanguageServer::parse_timing_function_context(line, value_start as u32);

        assert!(info.is_some());
        let info = info.unwrap();
        assert_eq!(info.function_str, "ease-in");
        assert!(matches!(info.interpolation, Interpolation::EaseIn));
    }

    #[test]
    fn test_parse_timing_function_context_cubic_bezier() {
        let line = r#"{"type": "animation", "name": "custom", "timing_function": "cubic-bezier(0.25, 0.1, 0.25, 1.0)", "frames": []}"#;
        let value_start = line.find("\"cubic-bezier").unwrap() + 1;
        let info = PixelsrcLanguageServer::parse_timing_function_context(line, value_start as u32);

        assert!(info.is_some());
        let info = info.unwrap();
        assert_eq!(info.function_str, "cubic-bezier(0.25, 0.1, 0.25, 1.0)");
        assert!(matches!(info.interpolation, Interpolation::Bezier { .. }));
    }

    #[test]
    fn test_parse_timing_function_context_steps() {
        let line = r#"{"type": "animation", "name": "step", "timing_function": "steps(4, jump-end)", "frames": []}"#;
        let value_start = line.find("\"steps").unwrap() + 1;
        let info = PixelsrcLanguageServer::parse_timing_function_context(line, value_start as u32);

        assert!(info.is_some());
        let info = info.unwrap();
        assert_eq!(info.function_str, "steps(4, jump-end)");
        assert!(matches!(info.interpolation, Interpolation::Steps { count: 4, .. }));
    }

    #[test]
    fn test_is_css_variable_completion_context_after_var_dashes() {
        let line = r#""{body}": "var(--"#;
        assert!(PixelsrcLanguageServer::is_css_variable_completion_context(
            line,
            line.len() as u32
        ));
    }

    #[test]
    fn test_is_css_variable_completion_context_typing_name() {
        let line = r#""{body}": "var(--pri"#;
        assert!(PixelsrcLanguageServer::is_css_variable_completion_context(
            line,
            line.len() as u32
        ));
    }

    #[test]
    fn test_is_css_variable_completion_context_not_in_var() {
        let line = r##""{body}": "#FF0000""##;
        assert!(!PixelsrcLanguageServer::is_css_variable_completion_context(
            line,
            line.len() as u32
        ));
    }

    #[test]
    fn test_is_css_variable_completion_context_after_close() {
        let line = r#""{body}": "var(--primary)"#;
        // After the closing paren, should not be in context
        assert!(!PixelsrcLanguageServer::is_css_variable_completion_context(
            line,
            line.len() as u32
        ));
    }

    #[test]
    fn test_is_css_variable_completion_context_in_fallback() {
        let line = r#""{body}": "var(--primary, "#;
        // After the comma (in fallback), should not be in var completion context
        assert!(!PixelsrcLanguageServer::is_css_variable_completion_context(
            line,
            line.len() as u32
        ));
    }

    #[test]
    fn test_parse_timing_function_context_not_animation() {
        let line = r#"{"type": "sprite", "name": "test", "timing_function": "ease"}"#;
        let info = PixelsrcLanguageServer::parse_timing_function_context(line, 50);
        assert!(info.is_none());
    }

    // === Transform Context Tests (LSP-12) ===

    #[test]
    fn test_parse_transform_context_single_transform() {
        let line = r#"{"type": "sprite", "name": "flipped", "source": "original", "transform": ["mirror-h"]}"#;
        // Find position within the "mirror-h" string
        let transform_start = line.find("[\"mirror-h\"]").unwrap() + 2;
        let info = PixelsrcLanguageServer::parse_transform_context(line, transform_start as u32);

        assert!(info.is_some());
        let info = info.unwrap();
        assert_eq!(info.raw, "mirror-h");
        assert_eq!(info.object_type, "sprite");
        assert_eq!(info.object_name, "flipped");
        assert_eq!(info.index, 0);
        assert_eq!(info.total, 1);
    }

    #[test]
    fn test_parse_transform_context_multiple_transforms_first() {
        let line = r#"{"type": "animation", "name": "walk_left", "source": "walk", "transform": ["mirror-h", "rotate:90"]}"#;
        // Position within first transform
        let first_transform_pos = line.find("[\"mirror-h\"").unwrap() + 2;
        let info =
            PixelsrcLanguageServer::parse_transform_context(line, first_transform_pos as u32);

        assert!(info.is_some());
        let info = info.unwrap();
        assert_eq!(info.raw, "mirror-h");
        assert_eq!(info.index, 0);
        assert_eq!(info.total, 2);
    }

    #[test]
    fn test_parse_transform_context_multiple_transforms_second() {
        let line = r#"{"type": "animation", "name": "walk_left", "source": "walk", "transform": ["mirror-h", "rotate:90"]}"#;
        // Position within second transform
        let second_transform_pos = line.find("\"rotate:90\"").unwrap() + 1;
        let info =
            PixelsrcLanguageServer::parse_transform_context(line, second_transform_pos as u32);

        assert!(info.is_some());
        let info = info.unwrap();
        assert_eq!(info.raw, "rotate:90");
        assert_eq!(info.index, 1);
        assert_eq!(info.total, 2);
    }

    #[test]
    fn test_parse_transform_context_not_a_transform() {
        let line = r#"{"type": "sprite", "name": "test", "grid": ["{a}{b}"]}"#;
        let info = PixelsrcLanguageServer::parse_transform_context(line, 30);
        assert!(info.is_none());
    }

    #[test]
    fn test_parse_transform_context_before_array() {
        let line = r#"{"type": "sprite", "name": "test", "transform": ["mirror-h"]}"#;
        // Position before the transform array
        let info = PixelsrcLanguageServer::parse_transform_context(line, 10);
        assert!(info.is_none());
    }

    #[test]
    fn test_parse_timing_function_context_cursor_outside_value() {
        let line =
            r#"{"type": "animation", "name": "test", "timing_function": "ease", "frames": []}"#;
        // Position in "name" field, not timing_function
        let info = PixelsrcLanguageServer::parse_timing_function_context(line, 20);
        assert!(info.is_none());
    }

    #[test]
    fn test_parse_transform_context_composition() {
        let line = r#"{"type": "composition", "name": "scene", "layers": [], "transform": ["scale:2.0,2.0"]}"#;
        let transform_pos = line.find("\"scale:2.0,2.0\"").unwrap() + 1;
        let info = PixelsrcLanguageServer::parse_transform_context(line, transform_pos as u32);

        assert!(info.is_some());
        let info = info.unwrap();
        assert_eq!(info.raw, "scale:2.0,2.0");
        assert_eq!(info.object_type, "composition");
        assert_eq!(info.object_name, "scene");
    }

    #[test]
    fn test_render_easing_curve_linear() {
        let curve = PixelsrcLanguageServer::render_easing_curve(&Interpolation::Linear, 10, 5);
        // Linear should produce a diagonal line
        assert!(curve.contains("â–ˆ"));
        assert!(curve.contains("1.0"));
        assert!(curve.contains("0.0"));
        assert!(curve.contains("→ 1"));
    }

    #[test]
    fn test_render_easing_curve_ease_in() {
        let curve = PixelsrcLanguageServer::render_easing_curve(&Interpolation::EaseIn, 10, 5);
        // Ease-in starts slow, should have more blocks in lower rows initially
        assert!(curve.contains("â–ˆ"));
    }

    #[test]
    fn test_render_easing_curve_steps() {
        let curve = PixelsrcLanguageServer::render_easing_curve(
            &Interpolation::Steps { count: 4, position: StepPosition::JumpEnd },
            20,
            6,
        );
        // Steps should produce a staircase pattern
        assert!(curve.contains("â–ˆ"));
    }

    #[test]
    fn test_describe_interpolation_all_types() {
        assert_eq!(
            PixelsrcLanguageServer::describe_interpolation(&Interpolation::Linear),
            "Constant speed (no easing)"
        );
        assert_eq!(
            PixelsrcLanguageServer::describe_interpolation(&Interpolation::EaseIn),
            "Slow start, fast end (acceleration)"
        );
        assert_eq!(
            PixelsrcLanguageServer::describe_interpolation(&Interpolation::EaseOut),
            "Fast start, slow end (deceleration)"
        );
        assert_eq!(
            PixelsrcLanguageServer::describe_interpolation(&Interpolation::EaseInOut),
            "Smooth S-curve (slow start and end)"
        );
        assert_eq!(
            PixelsrcLanguageServer::describe_interpolation(&Interpolation::Bounce),
            "Overshoot and settle back"
        );
        assert_eq!(
            PixelsrcLanguageServer::describe_interpolation(&Interpolation::Elastic),
            "Spring-like oscillation"
        );
        assert_eq!(
            PixelsrcLanguageServer::describe_interpolation(&Interpolation::Bezier {
                p1: (0.0, 0.0),
                p2: (1.0, 1.0)
            }),
            "Custom cubic bezier curve"
        );
        assert_eq!(
            PixelsrcLanguageServer::describe_interpolation(&Interpolation::Steps {
                count: 4,
                position: StepPosition::JumpEnd
            }),
            "Discrete step function"
        );
    }

    #[test]
    fn test_interpolation_to_css_named() {
        assert_eq!(PixelsrcLanguageServer::interpolation_to_css(&Interpolation::Linear), "linear");
        assert_eq!(PixelsrcLanguageServer::interpolation_to_css(&Interpolation::EaseIn), "ease-in");
        assert_eq!(
            PixelsrcLanguageServer::interpolation_to_css(&Interpolation::EaseOut),
            "ease-out"
        );
        assert_eq!(
            PixelsrcLanguageServer::interpolation_to_css(&Interpolation::EaseInOut),
            "ease-in-out"
        );
    }

    #[test]
    fn test_interpolation_to_css_bezier() {
        assert_eq!(
            PixelsrcLanguageServer::interpolation_to_css(&Interpolation::Bezier {
                p1: (0.25, 0.1),
                p2: (0.25, 1.0)
            }),
            "cubic-bezier(0.25, 0.1, 0.25, 1)"
        );
    }

    #[test]
    fn test_interpolation_to_css_steps() {
        assert_eq!(
            PixelsrcLanguageServer::interpolation_to_css(&Interpolation::Steps {
                count: 1,
                position: StepPosition::JumpEnd
            }),
            "step-end"
        );
        assert_eq!(
            PixelsrcLanguageServer::interpolation_to_css(&Interpolation::Steps {
                count: 1,
                position: StepPosition::JumpStart
            }),
            "step-start"
        );
        assert_eq!(
            PixelsrcLanguageServer::interpolation_to_css(&Interpolation::Steps {
                count: 4,
                position: StepPosition::JumpEnd
            }),
            "steps(4)"
        );
        assert_eq!(
            PixelsrcLanguageServer::interpolation_to_css(&Interpolation::Steps {
                count: 4,
                position: StepPosition::JumpStart
            }),
            "steps(4, jump-start)"
        );
        assert_eq!(
            PixelsrcLanguageServer::interpolation_to_css(&Interpolation::Steps {
                count: 4,
                position: StepPosition::JumpBoth
            }),
            "steps(4, jump-both)"
        );
    }

    // === Color Provider Tests ===

    #[test]
    fn test_extract_colors_from_line_hex() {
        let line = r##"{"type": "palette", "name": "test", "colors": {"{red}": "#FF0000", "{blue}": "#0000FF"}}"##;
        let registry = crate::variables::VariableRegistry::new();
        let colors = PixelsrcLanguageServer::extract_colors_from_line(line, 0, &registry);

        assert_eq!(colors.len(), 2);
        // Check that we found the colors
        let has_red = colors.iter().any(|(c, _)| {
            (c.rgba.0 - 1.0).abs() < 0.01 && c.rgba.1.abs() < 0.01 && c.rgba.2.abs() < 0.01
        });
        let has_blue = colors.iter().any(|(c, _)| {
            c.rgba.0.abs() < 0.01 && c.rgba.1.abs() < 0.01 && (c.rgba.2 - 1.0).abs() < 0.01
        });
        assert!(has_red, "Should find red color");
        assert!(has_blue, "Should find blue color");
    }

    #[test]
    fn test_extract_colors_from_line_css_functions() {
        let line = r##"{"type": "palette", "name": "test", "colors": {"{red}": "rgb(255, 0, 0)", "{green}": "hsl(120, 100%, 50%)"}}"##;
        let registry = crate::variables::VariableRegistry::new();
        let colors = PixelsrcLanguageServer::extract_colors_from_line(line, 0, &registry);

        assert_eq!(colors.len(), 2);
    }

    #[test]
    fn test_extract_colors_from_line_with_vars() {
        let content = r##"{"type": "palette", "name": "test", "colors": {"--primary": "#FF0000", "{red}": "var(--primary)"}}"##;
        let registry = PixelsrcLanguageServer::build_variable_registry(content);
        let colors = PixelsrcLanguageServer::extract_colors_from_line(content, 0, &registry);

        // Should have 1 color (the {red} token, --primary is skipped as a variable definition)
        assert_eq!(colors.len(), 1);
        // The resolved color should be red
        let (color, _) = &colors[0];
        assert!((color.rgba.0 - 1.0).abs() < 0.01, "Red component should be 1.0");
        assert!(color.rgba.1.abs() < 0.01, "Green component should be 0.0");
        assert!(color.rgba.2.abs() < 0.01, "Blue component should be 0.0");
    }

    #[test]
    fn test_extract_colors_from_line_color_mix() {
        let line = r##"{"type": "palette", "name": "test", "colors": {"{purple}": "color-mix(in srgb, red 50%, blue)"}}"##;
        let registry = crate::variables::VariableRegistry::new();
        let colors = PixelsrcLanguageServer::extract_colors_from_line(line, 0, &registry);

        assert_eq!(colors.len(), 1);
        // Color should be a purple-ish mix
        let (color, _) = &colors[0];
        assert!(color.rgba.0 > 0.4, "Should have red component");
        assert!(color.rgba.2 > 0.4, "Should have blue component");
    }

    #[test]
    fn test_extract_colors_from_line_not_palette() {
        let line = r#"{"type": "sprite", "name": "test", "grid": ["{a}"]}"#;
        let registry = crate::variables::VariableRegistry::new();
        let colors = PixelsrcLanguageServer::extract_colors_from_line(line, 0, &registry);

        assert!(colors.is_empty(), "Should not extract colors from sprites");
    }

    #[test]
    fn test_extract_colors_skips_css_vars() {
        let line = r##"{"type": "palette", "name": "test", "colors": {"--primary": "#FF0000"}}"##;
        let registry = crate::variables::VariableRegistry::new();
        let colors = PixelsrcLanguageServer::extract_colors_from_line(line, 0, &registry);

        // CSS variable definitions should be skipped
        assert!(colors.is_empty(), "Should skip CSS variable definitions");
    }

    #[test]
    fn test_rgba_to_hex_no_alpha() {
        let hex = PixelsrcLanguageServer::rgba_to_hex(1.0, 0.0, 0.0, 1.0);
        assert_eq!(hex, "#FF0000");

        let hex = PixelsrcLanguageServer::rgba_to_hex(0.0, 1.0, 0.0, 1.0);
        assert_eq!(hex, "#00FF00");

        let hex = PixelsrcLanguageServer::rgba_to_hex(0.0, 0.0, 1.0, 1.0);
        assert_eq!(hex, "#0000FF");
    }

    #[test]
    fn test_rgba_to_hex_with_alpha() {
        let hex = PixelsrcLanguageServer::rgba_to_hex(1.0, 0.0, 0.0, 0.5);
        assert_eq!(hex, "#FF000080");

        let hex = PixelsrcLanguageServer::rgba_to_hex(1.0, 1.0, 1.0, 0.0);
        assert_eq!(hex, "#FFFFFF00");
    }

    #[test]
    fn test_rgba_to_rgb_functional() {
        let rgb = PixelsrcLanguageServer::rgba_to_rgb_functional(1.0, 0.0, 0.0, 1.0);
        assert_eq!(rgb, "rgb(255, 0, 0)");

        let rgba = PixelsrcLanguageServer::rgba_to_rgb_functional(1.0, 0.0, 0.0, 0.5);
        assert_eq!(rgba, "rgba(255, 0, 0, 0.50)");
    }

    #[test]
    fn test_rgba_to_hsl() {
        // Pure red
        let hsl = PixelsrcLanguageServer::rgba_to_hsl(1.0, 0.0, 0.0, 1.0);
        assert_eq!(hsl, "hsl(0, 100%, 50%)");

        // Pure green
        let hsl = PixelsrcLanguageServer::rgba_to_hsl(0.0, 1.0, 0.0, 1.0);
        assert_eq!(hsl, "hsl(120, 100%, 50%)");

        // Pure blue
        let hsl = PixelsrcLanguageServer::rgba_to_hsl(0.0, 0.0, 1.0, 1.0);
        assert_eq!(hsl, "hsl(240, 100%, 50%)");

        // White
        let hsl = PixelsrcLanguageServer::rgba_to_hsl(1.0, 1.0, 1.0, 1.0);
        assert_eq!(hsl, "hsl(0, 0%, 100%)");

        // Black
        let hsl = PixelsrcLanguageServer::rgba_to_hsl(0.0, 0.0, 0.0, 1.0);
        assert_eq!(hsl, "hsl(0, 0%, 0%)");
    }

    #[test]
    fn test_rgba_to_hsl_with_alpha() {
        let hsla = PixelsrcLanguageServer::rgba_to_hsl(1.0, 0.0, 0.0, 0.5);
        assert_eq!(hsla, "hsla(0, 100%, 50%, 0.50)");
    }
}