1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
//! Runtime configuration I/O operations.
//!
//! This module contains system directory detection and config loading utilities
//! that require runtime dependencies (dirs, tracing).
//! These are separated from config.rs to allow schema-only builds.
use crate::config::{Config, ConfigError};
use crate::partial_config::{Merge, PartialConfig, SessionConfig};
use serde_json::Value;
use std::path::{Path, PathBuf};
// ============================================================================
// JSON Utilities
// ============================================================================
/// Recursively strip null values and empty objects from a JSON value.
/// This ensures that config layer files only contain the actual overridden values,
/// not null placeholders for inherited fields.
fn strip_nulls(value: Value) -> Option<Value> {
match value {
Value::Null => None,
Value::Object(map) => {
let filtered: serde_json::Map<String, Value> = map
.into_iter()
.filter_map(|(k, v)| strip_nulls(v).map(|v| (k, v)))
.collect();
if filtered.is_empty() {
None
} else {
Some(Value::Object(filtered))
}
}
Value::Array(arr) => {
let filtered: Vec<Value> = arr.into_iter().filter_map(strip_nulls).collect();
Some(Value::Array(filtered))
}
other => Some(other),
}
}
/// Recursively strip default values (empty strings, empty arrays) from a JSON value.
/// This ensures that fields with default serde values don't get saved to config files.
fn strip_empty_defaults(value: Value) -> Option<Value> {
match value {
Value::Null => None,
Value::String(s) if s.is_empty() => None,
Value::Array(arr) if arr.is_empty() => None,
Value::Object(map) => {
let filtered: serde_json::Map<String, Value> = map
.into_iter()
.filter_map(|(k, v)| strip_empty_defaults(v).map(|v| (k, v)))
.collect();
if filtered.is_empty() {
None
} else {
Some(Value::Object(filtered))
}
}
Value::Array(arr) => {
let filtered: Vec<Value> = arr.into_iter().filter_map(strip_empty_defaults).collect();
if filtered.is_empty() {
None
} else {
Some(Value::Array(filtered))
}
}
other => Some(other),
}
}
/// Set a value at a JSON pointer path, creating intermediate objects as needed.
/// The pointer should be in JSON Pointer format (e.g., "/editor/tab_size").
fn set_json_pointer(root: &mut Value, pointer: &str, value: Value) {
if pointer.is_empty() || pointer == "/" {
*root = value;
return;
}
let parts: Vec<&str> = pointer.trim_start_matches('/').split('/').collect();
let mut current = root;
for (i, part) in parts.iter().enumerate() {
if i == parts.len() - 1 {
// Last part - set the value
if let Value::Object(map) = current {
map.insert(part.to_string(), value);
}
return;
}
// Intermediate part - ensure it exists as an object
if let Value::Object(map) = current {
if !map.contains_key(*part) {
map.insert(part.to_string(), Value::Object(Default::default()));
}
current = map.get_mut(*part).unwrap();
} else {
return; // Can't traverse non-object
}
}
}
/// Remove a value at a JSON pointer path.
pub(crate) fn remove_json_pointer(root: &mut Value, pointer: &str) {
if pointer.is_empty() || pointer == "/" {
return;
}
let parts: Vec<&str> = pointer.trim_start_matches('/').split('/').collect();
let mut current = root;
for (i, part) in parts.iter().enumerate() {
if i == parts.len() - 1 {
// Last part - remove the key
if let Value::Object(map) = current {
map.remove(*part);
}
return;
}
// Intermediate part - traverse
if let Value::Object(map) = current {
if let Some(next) = map.get_mut(*part) {
current = next;
} else {
return; // Path doesn't exist
}
} else {
return; // Can't traverse non-object
}
}
}
/// Find all JSON pointer paths where two values differ.
/// Returns leaf paths that have different values between old and new.
fn find_changed_paths(old: &Value, new: &Value) -> std::collections::HashSet<String> {
let mut changed = std::collections::HashSet::new();
find_changed_paths_recursive(old, new, String::new(), &mut changed);
changed
}
fn find_changed_paths_recursive(
old: &Value,
new: &Value,
prefix: String,
changed: &mut std::collections::HashSet<String>,
) {
match (old, new) {
(Value::Object(old_map), Value::Object(new_map)) => {
// Check all keys in both objects
let all_keys: std::collections::HashSet<_> =
old_map.keys().chain(new_map.keys()).collect();
for key in all_keys {
let path = if prefix.is_empty() {
format!("/{}", key)
} else {
format!("{}/{}", prefix, key)
};
let old_val = old_map.get(key).unwrap_or(&Value::Null);
let new_val = new_map.get(key).unwrap_or(&Value::Null);
find_changed_paths_recursive(old_val, new_val, path, changed);
}
}
(old_val, new_val) if old_val != new_val => {
// Leaf values differ - mark as changed
if !prefix.is_empty() {
changed.insert(prefix);
}
}
_ => {} // Values are equal, no change
}
}
/// Strip defaults/nulls from `value`, serialize to pretty JSON, ensure the parent
/// directory exists, and write to `path`.
fn write_clean_value_to_path(path: &Path, value: Value) -> Result<(), ConfigError> {
if let Some(parent_dir) = path.parent() {
std::fs::create_dir_all(parent_dir)
.map_err(|e| ConfigError::IoError(format!("{}: {}", parent_dir.display(), e)))?;
}
let stripped = strip_nulls(value).unwrap_or(Value::Object(Default::default()));
let clean = strip_empty_defaults(stripped).unwrap_or(Value::Object(Default::default()));
let json = serde_json::to_string_pretty(&clean)
.map_err(|e| ConfigError::SerializeError(e.to_string()))?;
std::fs::write(path, json)
.map_err(|e| ConfigError::IoError(format!("{}: {}", path.display(), e)))?;
Ok(())
}
/// Read an existing config file as raw JSON, returning an empty object when the file
/// is absent or unparseable.
fn read_existing_json(path: &Path) -> Result<Value, ConfigError> {
if !path.exists() {
return Ok(Value::Object(Default::default()));
}
let content = std::fs::read_to_string(path)
.map_err(|e| ConfigError::IoError(format!("{}: {}", path.display(), e)))?;
Ok(serde_json::from_str(&content).unwrap_or(Value::Object(Default::default())))
}
// ============================================================================
// Configuration Migration System
// ============================================================================
/// Current config schema version.
/// Increment this when making breaking changes to config structure.
pub const CURRENT_CONFIG_VERSION: u32 = 2;
/// Apply all necessary migrations to bring a config JSON to the current version.
pub fn migrate_config(mut value: Value) -> Result<Value, ConfigError> {
let version = value.get("version").and_then(|v| v.as_u64()).unwrap_or(0) as u32;
// Apply migrations sequentially
if version < 1 {
value = migrate_v0_to_v1(value)?;
}
if version < 2 {
value = migrate_v1_to_v2(value)?;
}
Ok(value)
}
/// Migration from v0 (implicit/missing version) to v1.
/// This is the initial migration that establishes the version field.
fn migrate_v0_to_v1(mut value: Value) -> Result<Value, ConfigError> {
if let Value::Object(ref mut map) = value {
// Set version to 1
map.insert("version".to_string(), Value::Number(1.into()));
// Example: rename camelCase keys to snake_case if they exist
if let Some(Value::Object(ref mut editor_map)) = map.get_mut("editor") {
// tabSize -> tab_size (hypothetical legacy format)
if let Some(val) = editor_map.remove("tabSize") {
editor_map.entry("tab_size").or_insert(val);
}
// lineNumbers -> line_numbers
if let Some(val) = editor_map.remove("lineNumbers") {
editor_map.entry("line_numbers").or_insert(val);
}
}
}
Ok(value)
}
/// Migration from v1 to v2.
///
/// Injects `"{remote}"` at the front of `editor.status_bar.left` when
/// the user has customized the list and the element is not already
/// present. Users who never overrode the default get the element via
/// `default_status_bar_left` at resolve time — we intentionally skip
/// inserting a `status_bar` object here so those users stay on the
/// rolling default if future versions reorder or rename elements.
fn migrate_v1_to_v2(mut value: Value) -> Result<Value, ConfigError> {
if let Value::Object(ref mut map) = value {
map.insert("version".to_string(), Value::Number(2.into()));
let left = map
.get_mut("editor")
.and_then(|editor| editor.as_object_mut())
.and_then(|editor| editor.get_mut("status_bar"))
.and_then(|status_bar| status_bar.as_object_mut())
.and_then(|status_bar| status_bar.get_mut("left"))
.and_then(|left| left.as_array_mut());
if let Some(left) = left {
let already_present = left.iter().any(|v| v.as_str() == Some("{remote}"));
if !already_present {
left.insert(0, Value::String("{remote}".to_string()));
}
}
}
Ok(value)
}
/// Represents a configuration layer in the 4-level hierarchy.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfigLayer {
/// Hardcoded defaults embedded in binary (lowest precedence)
System,
/// User-global settings (~/.config/fresh/config.json)
User,
/// Project-local settings ($PROJECT_ROOT/.fresh/config.json)
Project,
/// Runtime/volatile session state (highest precedence)
Session,
}
impl ConfigLayer {
/// Get the precedence level (higher = takes priority)
pub fn precedence(self) -> u8 {
match self {
Self::System => 0,
Self::User => 1,
Self::Project => 2,
Self::Session => 3,
}
}
}
/// Manages loading and merging of all configuration layers.
///
/// Resolution order: System → User → Project → Session
/// Higher precedence layers override lower precedence layers.
pub struct ConfigResolver {
dir_context: DirectoryContext,
working_dir: PathBuf,
}
impl ConfigResolver {
/// Create a new ConfigResolver for a working directory.
pub fn new(dir_context: DirectoryContext, working_dir: PathBuf) -> Self {
Self {
dir_context,
working_dir,
}
}
/// Load all layers and merge them into a resolved Config.
///
/// Layers are merged from highest to lowest precedence:
/// Session > Project > UserPlatform > User > System
///
/// Each layer fills in values missing from higher precedence layers.
pub fn resolve(&self) -> Result<Config, ConfigError> {
// Start with highest precedence layer (Session)
let mut merged = self.load_session_layer()?.unwrap_or_default();
// Merge in Project layer (fills missing values)
if let Some(project_partial) = self.load_project_layer()? {
tracing::debug!("Loaded project config layer");
merged.merge_from(&project_partial);
}
// Merge in User Platform layer (e.g., config_linux.json)
if let Some(platform_partial) = self.load_user_platform_layer()? {
tracing::debug!("Loaded user platform config layer");
merged.merge_from(&platform_partial);
}
// Merge in User layer (fills remaining missing values)
if let Some(user_partial) = self.load_user_layer()? {
tracing::debug!("Loaded user config layer");
merged.merge_from(&user_partial);
}
// Resolve to concrete Config (applies system defaults for any remaining None values)
Ok(merged.resolve())
}
/// Get the path to user config file.
pub fn user_config_path(&self) -> PathBuf {
self.dir_context.config_path()
}
/// Get the path to project config file.
/// Checks new location first (.fresh/config.json), falls back to legacy (config.json).
pub fn project_config_path(&self) -> PathBuf {
let new_path = self.working_dir.join(".fresh").join("config.json");
if new_path.exists() {
return new_path;
}
// Fall back to legacy location for backward compatibility
let legacy_path = self.working_dir.join("config.json");
if legacy_path.exists() {
return legacy_path;
}
// Return new path as default for new projects
new_path
}
/// Get the preferred path for writing project config (new location).
pub fn project_config_write_path(&self) -> PathBuf {
self.working_dir.join(".fresh").join("config.json")
}
/// Get the path to session config file.
pub fn session_config_path(&self) -> PathBuf {
self.working_dir.join(".fresh").join("session.json")
}
/// Get the platform-specific config filename.
fn platform_config_filename() -> Option<&'static str> {
if cfg!(target_os = "linux") {
Some("config_linux.json")
} else if cfg!(target_os = "macos") {
Some("config_macos.json")
} else if cfg!(target_os = "windows") {
Some("config_windows.json")
} else {
None
}
}
/// Get the path to platform-specific user config file.
pub fn user_platform_config_path(&self) -> Option<PathBuf> {
Self::platform_config_filename().map(|filename| self.dir_context.config_dir.join(filename))
}
/// Load the user layer from disk.
pub fn load_user_layer(&self) -> Result<Option<PartialConfig>, ConfigError> {
self.load_layer_from_path(&self.user_config_path())
}
/// Load the platform-specific user layer from disk.
pub fn load_user_platform_layer(&self) -> Result<Option<PartialConfig>, ConfigError> {
if let Some(path) = self.user_platform_config_path() {
self.load_layer_from_path(&path)
} else {
Ok(None)
}
}
/// Load the project layer from disk.
pub fn load_project_layer(&self) -> Result<Option<PartialConfig>, ConfigError> {
self.load_layer_from_path(&self.project_config_path())
}
/// Load the session layer from disk.
pub fn load_session_layer(&self) -> Result<Option<PartialConfig>, ConfigError> {
self.load_layer_from_path(&self.session_config_path())
}
/// Load a layer from a specific path, applying migrations if needed.
fn load_layer_from_path(&self, path: &Path) -> Result<Option<PartialConfig>, ConfigError> {
if !path.exists() {
return Ok(None);
}
let content = std::fs::read_to_string(path)
.map_err(|e| ConfigError::IoError(format!("{}: {}", path.display(), e)))?;
// Parse as raw JSON first
let value: Value = serde_json::from_str(&content)
.map_err(|e| ConfigError::ParseError(format!("{}: {}", path.display(), e)))?;
// Apply migrations
let migrated = migrate_config(value)?;
// Now deserialize to PartialConfig
let partial: PartialConfig = serde_json::from_value(migrated)
.map_err(|e| ConfigError::ParseError(format!("{}: {}", path.display(), e)))?;
Ok(Some(partial))
}
/// Resolve the writable path for `layer`, returning an error for the read-only
/// System layer.
fn layer_write_path(&self, layer: ConfigLayer) -> Result<PathBuf, ConfigError> {
match layer {
ConfigLayer::User => Ok(self.user_config_path()),
ConfigLayer::Project => Ok(self.project_config_write_path()),
ConfigLayer::Session => Ok(self.session_config_path()),
ConfigLayer::System => Err(ConfigError::ValidationError(
"Cannot write to System layer".to_string(),
)),
}
}
/// Save a config to a specific layer, writing only the delta from parent layers.
pub fn save_to_layer(&self, config: &Config, layer: ConfigLayer) -> Result<(), ConfigError> {
let path = self.layer_write_path(layer)?;
let parent_partial = self.resolve_up_to_layer(layer)?;
let parent = PartialConfig::from(&parent_partial.resolve());
let current = PartialConfig::from(config);
let delta = diff_partial_config(¤t, &parent);
// Preserve any manual edits made externally; delta takes precedence.
let existing: PartialConfig = if path.exists() {
let content = std::fs::read_to_string(&path)
.map_err(|e| ConfigError::IoError(format!("{}: {}", path.display(), e)))?;
serde_json::from_str(&content).unwrap_or_default()
} else {
PartialConfig::default()
};
let mut merged = delta;
merged.merge_from(&existing);
let merged_value = serde_json::to_value(&merged)
.map_err(|e| ConfigError::SerializeError(e.to_string()))?;
write_clean_value_to_path(&path, merged_value)
}
/// Save a config to a specific layer, using a baseline to track changes.
///
/// This solves the problem where `save_to_layer` can't distinguish between:
/// - "User didn't change this field" (should preserve external edits)
/// - "User changed this field to the default" (should update the file)
///
/// By comparing `current` against `baseline` (what was loaded), we know exactly
/// which fields the user modified. Those fields are updated even if they match
/// defaults; untouched fields preserve any external edits to the file.
pub fn save_to_layer_with_baseline(
&self,
current: &Config,
baseline: &Config,
layer: ConfigLayer,
) -> Result<(), ConfigError> {
let path = self.layer_write_path(layer)?;
let parent_partial = self.resolve_up_to_layer(layer)?;
let parent = PartialConfig::from(&parent_partial.resolve());
let current_json = serde_json::to_value(current)
.map_err(|e| ConfigError::SerializeError(e.to_string()))?;
let baseline_json = serde_json::to_value(baseline)
.map_err(|e| ConfigError::SerializeError(e.to_string()))?;
let parent_json = serde_json::to_value(&parent)
.map_err(|e| ConfigError::SerializeError(e.to_string()))?;
let changed_paths = find_changed_paths(&baseline_json, ¤t_json);
let mut result = read_existing_json(&path)?;
// For each changed path: remove if value reverted to default, otherwise set it.
for pointer in &changed_paths {
let current_val = current_json.pointer(pointer);
let parent_val = parent_json.pointer(pointer);
if current_val == parent_val {
remove_json_pointer(&mut result, pointer);
} else if let Some(val) = current_val {
set_json_pointer(&mut result, pointer, val.clone());
}
}
write_clean_value_to_path(&path, result)
}
/// Save specific changes to a layer file using JSON pointer paths.
///
/// This reads the existing file, applies only the specified changes,
/// and writes back. This preserves any manual edits not touched by the changes.
pub fn save_changes_to_layer(
&self,
changes: &std::collections::HashMap<String, serde_json::Value>,
deletions: &std::collections::HashSet<String>,
layer: ConfigLayer,
) -> Result<(), ConfigError> {
let path = self.layer_write_path(layer)?;
let mut config_value = read_existing_json(&path)?;
for pointer in deletions {
remove_json_pointer(&mut config_value, pointer);
}
for (pointer, value) in changes {
set_json_pointer(&mut config_value, pointer, value.clone());
}
// Validate before writing.
let _: PartialConfig = serde_json::from_value(config_value.clone()).map_err(|e| {
ConfigError::ValidationError(format!("Result config would be invalid: {}", e))
})?;
write_clean_value_to_path(&path, config_value)
}
/// Save a SessionConfig to the session layer file.
pub fn save_session(&self, session: &SessionConfig) -> Result<(), ConfigError> {
let path = self.session_config_path();
// Ensure .fresh directory exists
if let Some(parent_dir) = path.parent() {
std::fs::create_dir_all(parent_dir)
.map_err(|e| ConfigError::IoError(format!("{}: {}", parent_dir.display(), e)))?;
}
let json = serde_json::to_string_pretty(session)
.map_err(|e| ConfigError::SerializeError(e.to_string()))?;
std::fs::write(&path, json)
.map_err(|e| ConfigError::IoError(format!("{}: {}", path.display(), e)))?;
tracing::debug!("Saved session config to {}", path.display());
Ok(())
}
/// Load the session config from disk, or return an empty one if it doesn't exist.
pub fn load_session(&self) -> Result<SessionConfig, ConfigError> {
match self.load_session_layer()? {
Some(partial) => Ok(SessionConfig::from(partial)),
None => Ok(SessionConfig::new()),
}
}
/// Clear the session config file on editor exit.
pub fn clear_session(&self) -> Result<(), ConfigError> {
let path = self.session_config_path();
if path.exists() {
std::fs::remove_file(&path)
.map_err(|e| ConfigError::IoError(format!("{}: {}", path.display(), e)))?;
tracing::debug!("Cleared session config at {}", path.display());
}
Ok(())
}
/// Resolve config by merging layers below the target layer.
/// Used to calculate the "parent" config for delta serialization.
fn resolve_up_to_layer(&self, layer: ConfigLayer) -> Result<PartialConfig, ConfigError> {
let mut merged = PartialConfig::default();
// Merge from highest precedence (just below target) to lowest
// Session layer: parent includes Project + UserPlatform + User
// Project layer: parent includes UserPlatform + User
// User layer: parent is empty (system defaults applied during resolve)
if layer == ConfigLayer::Session {
// Session's parent is Project + UserPlatform + User
if let Some(project) = self.load_project_layer()? {
merged = project;
}
if let Some(platform) = self.load_user_platform_layer()? {
merged.merge_from(&platform);
}
if let Some(user) = self.load_user_layer()? {
merged.merge_from(&user);
}
} else if layer == ConfigLayer::Project {
// Project's parent is UserPlatform + User
if let Some(platform) = self.load_user_platform_layer()? {
merged = platform;
}
if let Some(user) = self.load_user_layer()? {
merged.merge_from(&user);
}
}
// User layer's parent is empty (defaults handled during resolve)
Ok(merged)
}
/// Determine which layer each setting value comes from.
/// Returns a map of JSON pointer paths to their source layer.
pub fn get_layer_sources(
&self,
) -> Result<std::collections::HashMap<String, ConfigLayer>, ConfigError> {
use std::collections::HashMap;
let mut sources: HashMap<String, ConfigLayer> = HashMap::new();
// Load each layer and mark which paths come from it
// Check layers in precedence order (highest first)
// Session layer takes priority, then Project, then User, then System defaults
if let Some(session) = self.load_session_layer()? {
let json = serde_json::to_value(&session).unwrap_or_default();
collect_paths(&json, "", &mut |path| {
sources.insert(path, ConfigLayer::Session);
});
}
if let Some(project) = self.load_project_layer()? {
let json = serde_json::to_value(&project).unwrap_or_default();
collect_paths(&json, "", &mut |path| {
sources.entry(path).or_insert(ConfigLayer::Project);
});
}
if let Some(user) = self.load_user_layer()? {
let json = serde_json::to_value(&user).unwrap_or_default();
collect_paths(&json, "", &mut |path| {
sources.entry(path).or_insert(ConfigLayer::User);
});
}
// Any path not in the map comes from System defaults (implicitly)
Ok(sources)
}
}
/// Recursively collect all non-null leaf paths in a JSON value.
fn collect_paths<F>(value: &Value, prefix: &str, collector: &mut F)
where
F: FnMut(String),
{
match value {
Value::Object(map) => {
for (key, val) in map {
let path = if prefix.is_empty() {
format!("/{}", key)
} else {
format!("{}/{}", prefix, key)
};
collect_paths(val, &path, collector);
}
}
Value::Null => {} // Skip nulls (unset in partial config)
_ => {
// Leaf value - collect this path
collector(prefix.to_string());
}
}
}
/// Calculate the delta between a partial config and its parent.
/// Returns a PartialConfig containing only values that differ from parent.
fn diff_partial_config(current: &PartialConfig, parent: &PartialConfig) -> PartialConfig {
// Convert both to JSON values and diff them
let current_json = serde_json::to_value(current).unwrap_or_default();
let parent_json = serde_json::to_value(parent).unwrap_or_default();
let diff = json_diff(&parent_json, ¤t_json);
// Convert diff back to PartialConfig
serde_json::from_value(diff).unwrap_or_default()
}
impl Config {
/// Get the system config file paths (without local/working directory).
///
/// On macOS, prioritizes `~/.config/fresh/config.json` if it exists.
/// Then checks the standard system config directory.
fn system_config_paths() -> Vec<PathBuf> {
let mut paths = Vec::with_capacity(2);
// macOS: Prioritize ~/.config/fresh/config.json
#[cfg(target_os = "macos")]
if let Some(home) = dirs::home_dir() {
let path = home.join(".config").join("fresh").join(Config::FILENAME);
if path.exists() {
paths.push(path);
}
}
// Standard system paths (XDG on Linux, AppSupport on macOS, Roaming on Windows)
if let Some(config_dir) = dirs::config_dir() {
let path = config_dir.join("fresh").join(Config::FILENAME);
if !paths.contains(&path) && path.exists() {
paths.push(path);
}
}
paths
}
/// Get all config search paths, checking local (working directory) first.
///
/// Search order:
/// 1. `{working_dir}/config.json` (project-local config)
/// 2. System config paths (see `system_config_paths()`)
///
/// Only returns paths that exist on disk.
fn config_search_paths(working_dir: &Path) -> Vec<PathBuf> {
let local = Self::local_config_path(working_dir);
let mut paths = Vec::with_capacity(3);
if local.exists() {
paths.push(local);
}
paths.extend(Self::system_config_paths());
paths
}
/// Find the first existing config file, checking local directory first.
///
/// Returns `None` if no config file exists anywhere.
pub fn find_config_path(working_dir: &Path) -> Option<PathBuf> {
Self::config_search_paths(working_dir).into_iter().next()
}
/// Load configuration using the 4-level layer system.
///
/// Merges layers in precedence order: Session > Project > User > System
/// Falls back to defaults for any unspecified values.
pub fn load_with_layers(dir_context: &DirectoryContext, working_dir: &Path) -> Self {
let resolver = ConfigResolver::new(dir_context.clone(), working_dir.to_path_buf());
match resolver.resolve() {
Ok(config) => {
tracing::info!("Loaded layered config for {}", working_dir.display());
config
}
Err(e) => {
tracing::warn!("Failed to load layered config: {}, using defaults", e);
Self::default()
}
}
}
/// Read the raw user config file content as JSON.
///
/// This returns the sparse user config (only what's in the file, not merged
/// with defaults). Useful for plugins that need to distinguish between
/// user-set values and defaults.
///
/// Checks working directory first, then system paths.
pub fn read_user_config_raw(working_dir: &Path) -> serde_json::Value {
for path in Self::config_search_paths(working_dir) {
if let Ok(contents) = std::fs::read_to_string(&path) {
match serde_json::from_str(&contents) {
Ok(value) => return value,
Err(e) => {
tracing::warn!("Failed to parse config from {}: {}", path.display(), e);
}
}
}
}
serde_json::Value::Object(serde_json::Map::new())
}
}
/// Compute the difference between two JSON values.
/// Returns only the parts of `current` that differ from `defaults`.
fn json_diff(defaults: &serde_json::Value, current: &serde_json::Value) -> serde_json::Value {
use serde_json::Value;
match (defaults, current) {
// Both are objects - recursively diff
(Value::Object(def_map), Value::Object(cur_map)) => {
let mut result = serde_json::Map::new();
for (key, cur_val) in cur_map {
if let Some(def_val) = def_map.get(key) {
// Key exists in both - recurse
let diff = json_diff(def_val, cur_val);
// Only include if there's an actual difference
if !is_empty_diff(&diff) {
result.insert(key.clone(), diff);
}
} else {
// Key only in current - include it, but strip empty defaults
if let Some(stripped) = strip_empty_defaults(cur_val.clone()) {
result.insert(key.clone(), stripped);
}
}
}
Value::Object(result)
}
// For arrays and primitives, include if different
_ => {
// Treat empty string as "not set" - don't include in diff
if let Value::String(s) = current {
if s.is_empty() {
return Value::Object(serde_json::Map::new()); // No diff
}
}
if defaults == current {
Value::Object(serde_json::Map::new()) // Empty object signals "no diff"
} else {
current.clone()
}
}
}
}
/// Check if a diff result represents "no changes"
fn is_empty_diff(value: &serde_json::Value) -> bool {
match value {
serde_json::Value::Object(map) => map.is_empty(),
_ => false,
}
}
/// Directory paths for editor state and configuration
///
/// This struct holds all directory paths that the editor needs.
/// Only the top-level `main` function should use `dirs::*` to construct this;
/// all other code should receive it by construction/parameter passing.
///
/// This design ensures:
/// - Tests can use isolated temp directories
/// - Parallel tests don't interfere with each other
/// - No hidden global state dependencies
#[derive(Debug, Clone)]
pub struct DirectoryContext {
/// Data directory for persistent state (recovery, workspaces, history)
/// e.g., ~/.local/share/fresh on Linux, ~/Library/Application Support/fresh on macOS
pub data_dir: std::path::PathBuf,
/// Config directory for user configuration
/// e.g., ~/.config/fresh on Linux, ~/Library/Application Support/fresh on macOS
pub config_dir: std::path::PathBuf,
/// User's home directory (for file open dialog shortcuts)
pub home_dir: Option<std::path::PathBuf>,
/// User's documents directory (for file open dialog shortcuts)
pub documents_dir: Option<std::path::PathBuf>,
/// User's downloads directory (for file open dialog shortcuts)
pub downloads_dir: Option<std::path::PathBuf>,
}
impl DirectoryContext {
/// Create a DirectoryContext from the system directories
/// This should ONLY be called from main()
pub fn from_system() -> std::io::Result<Self> {
let data_dir = dirs::data_dir()
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::NotFound,
"Could not determine data directory",
)
})?
.join("fresh");
let config_dir = Self::default_config_dir().ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::NotFound,
"Could not determine config directory",
)
})?;
Ok(Self {
data_dir,
config_dir,
home_dir: dirs::home_dir(),
documents_dir: dirs::document_dir(),
downloads_dir: dirs::download_dir(),
})
}
/// Create a DirectoryContext for testing with a temp directory
/// All paths point to subdirectories within the provided temp_dir
pub fn for_testing(temp_dir: &std::path::Path) -> Self {
Self {
data_dir: temp_dir.join("data"),
config_dir: temp_dir.join("config"),
home_dir: Some(temp_dir.join("home")),
documents_dir: Some(temp_dir.join("documents")),
downloads_dir: Some(temp_dir.join("downloads")),
}
}
/// Get the recovery directory path
pub fn recovery_dir(&self) -> std::path::PathBuf {
self.data_dir.join("recovery")
}
/// Get the workspaces directory path
pub fn workspaces_dir(&self) -> std::path::PathBuf {
self.data_dir.join("workspaces")
}
/// Per-project state directory: all persistent state for one workspace
/// lives under here (workspace trust today; room for more). Keyed by the
/// canonicalized working directory (so symlinked spellings of the same
/// project share state) under the shared `workspaces/` location. Using a
/// directory per project — rather than one shared file — keeps concurrent
/// `fresh` processes on different projects from contending over a single
/// file.
///
// TODO(workspace-state): consolidate the rest of a project's persistent
// state into this directory. Today the workspace snapshot still lives
// beside it as `workspaces/<encoded>.json`, and recovery/history live
// under their own `data_dir` subtrees. Migrate them to
// `project_state_dir(working_dir)/{workspace,recovery,history}.json` (with
// a one-time move of the legacy paths, falling back to read-old/write-new)
// so a project's full state is self-contained in one directory.
pub fn project_state_dir(&self, working_dir: &std::path::Path) -> std::path::PathBuf {
let canonical = working_dir
.canonicalize()
.unwrap_or_else(|_| working_dir.to_path_buf());
self.workspaces_dir()
.join(crate::workspace::encode_path_for_filename(&canonical))
}
/// Get the history file path for a specific prompt type
/// This is the generic method used by prompt_histories HashMap.
/// history_name can be: "search", "replace", "goto_line", "plugin:custom_name", etc.
pub fn prompt_history_path(&self, history_name: &str) -> std::path::PathBuf {
// Sanitize the name for filesystem safety (replace : with _)
let safe_name = history_name.replace(':', "_");
self.data_dir.join(format!("{}_history.json", safe_name))
}
/// Get the search history file path (legacy, calls generic method)
pub fn search_history_path(&self) -> std::path::PathBuf {
self.prompt_history_path("search")
}
/// Get the replace history file path (legacy, calls generic method)
pub fn replace_history_path(&self) -> std::path::PathBuf {
self.prompt_history_path("replace")
}
/// Get the goto line history file path (legacy, calls generic method)
pub fn goto_line_history_path(&self) -> std::path::PathBuf {
self.prompt_history_path("goto_line")
}
/// Get the terminals root directory
pub fn terminals_dir(&self) -> std::path::PathBuf {
self.data_dir.join("terminals")
}
/// Get the terminal directory for a specific working directory
pub fn terminal_dir_for(&self, working_dir: &std::path::Path) -> std::path::PathBuf {
let encoded = crate::workspace::encode_path_for_filename(working_dir);
self.terminals_dir().join(encoded)
}
/// Per-working-directory data root (`<data_dir>/workdirs/<encoded-cwd>/`).
/// The canonical home for plugin state that should be scoped to a single
/// project root / worktree rather than shared across all of them — each
/// worktree gets its own subtree. Plugins reach this via
/// `editor.getWorkingDataDir()`.
pub fn working_data_dir_for(&self, working_dir: &std::path::Path) -> std::path::PathBuf {
let encoded = crate::workspace::encode_path_for_filename(working_dir);
self.data_dir.join("workdirs").join(encoded)
}
/// Get the config file path
pub fn config_path(&self) -> std::path::PathBuf {
self.config_dir.join(Config::FILENAME)
}
/// Get the themes directory path
pub fn themes_dir(&self) -> std::path::PathBuf {
self.config_dir.join("themes")
}
/// Get the grammars directory path
pub fn grammars_dir(&self) -> std::path::PathBuf {
self.config_dir.join("grammars")
}
/// Get the plugins directory path
pub fn plugins_dir(&self) -> std::path::PathBuf {
self.config_dir.join("plugins")
}
/// Get the default config directory path (static/internal version).
///
/// This is used internally by `from_system()` to determine the config directory.
///
/// On macOS, this prioritizes `~/.config/fresh` over `~/Library/Application Support/fresh`
/// to match the documented configuration location.
fn default_config_dir() -> Option<std::path::PathBuf> {
#[cfg(target_os = "macos")]
{
dirs::home_dir().map(|p| p.join(".config").join("fresh"))
}
#[cfg(not(target_os = "macos"))]
{
dirs::config_dir().map(|p| p.join("fresh"))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
fn create_test_resolver() -> (TempDir, ConfigResolver) {
let temp_dir = TempDir::new().unwrap();
let dir_context = DirectoryContext::for_testing(temp_dir.path());
let working_dir = temp_dir.path().join("project");
std::fs::create_dir_all(&working_dir).unwrap();
let resolver = ConfigResolver::new(dir_context, working_dir);
(temp_dir, resolver)
}
#[test]
fn resolver_returns_defaults_when_no_config_files() {
let (_temp, resolver) = create_test_resolver();
let config = resolver.resolve().unwrap();
// Should have system defaults
assert_eq!(config.editor.tab_size, 4);
assert!(config.editor.line_numbers);
}
#[test]
fn resolver_loads_user_layer() {
let (temp, resolver) = create_test_resolver();
// Create user config
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
std::fs::write(&user_config_path, r#"{"editor": {"tab_size": 2}}"#).unwrap();
let config = resolver.resolve().unwrap();
assert_eq!(config.editor.tab_size, 2);
assert!(config.editor.line_numbers); // Still default
drop(temp);
}
#[test]
fn resolver_project_overrides_user() {
let (temp, resolver) = create_test_resolver();
// Create user config with tab_size=2
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
std::fs::write(
&user_config_path,
r#"{"editor": {"tab_size": 2, "line_numbers": false}}"#,
)
.unwrap();
// Create project config with tab_size=8
let project_config_path = resolver.project_config_path();
std::fs::create_dir_all(project_config_path.parent().unwrap()).unwrap();
std::fs::write(&project_config_path, r#"{"editor": {"tab_size": 8}}"#).unwrap();
let config = resolver.resolve().unwrap();
assert_eq!(config.editor.tab_size, 8); // Project wins
assert!(!config.editor.line_numbers); // User value preserved
drop(temp);
}
#[test]
fn resolver_session_overrides_all() {
let (temp, resolver) = create_test_resolver();
// Create user config
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
std::fs::write(&user_config_path, r#"{"editor": {"tab_size": 2}}"#).unwrap();
// Create project config
let project_config_path = resolver.project_config_path();
std::fs::create_dir_all(project_config_path.parent().unwrap()).unwrap();
std::fs::write(&project_config_path, r#"{"editor": {"tab_size": 4}}"#).unwrap();
// Create session config
let session_config_path = resolver.session_config_path();
std::fs::write(&session_config_path, r#"{"editor": {"tab_size": 16}}"#).unwrap();
let config = resolver.resolve().unwrap();
assert_eq!(config.editor.tab_size, 16); // Session wins
drop(temp);
}
#[test]
fn layer_precedence_ordering() {
assert!(ConfigLayer::Session.precedence() > ConfigLayer::Project.precedence());
assert!(ConfigLayer::Project.precedence() > ConfigLayer::User.precedence());
assert!(ConfigLayer::User.precedence() > ConfigLayer::System.precedence());
}
#[test]
fn save_to_system_layer_fails() {
let (_temp, resolver) = create_test_resolver();
let config = Config::default();
let result = resolver.save_to_layer(&config, ConfigLayer::System);
assert!(result.is_err());
}
#[test]
fn resolver_loads_legacy_project_config() {
let (temp, resolver) = create_test_resolver();
// Create legacy project config at {working_dir}/config.json
let working_dir = temp.path().join("project");
let legacy_path = working_dir.join("config.json");
std::fs::write(&legacy_path, r#"{"editor": {"tab_size": 3}}"#).unwrap();
let config = resolver.resolve().unwrap();
assert_eq!(config.editor.tab_size, 3);
drop(temp);
}
#[test]
fn resolver_prefers_new_config_over_legacy() {
let (temp, resolver) = create_test_resolver();
// Create both legacy and new project configs
let working_dir = temp.path().join("project");
// Legacy: tab_size=3
let legacy_path = working_dir.join("config.json");
std::fs::write(&legacy_path, r#"{"editor": {"tab_size": 3}}"#).unwrap();
// New: tab_size=5
let new_path = working_dir.join(".fresh").join("config.json");
std::fs::create_dir_all(new_path.parent().unwrap()).unwrap();
std::fs::write(&new_path, r#"{"editor": {"tab_size": 5}}"#).unwrap();
let config = resolver.resolve().unwrap();
assert_eq!(config.editor.tab_size, 5); // New path wins
drop(temp);
}
#[test]
fn load_with_layers_works() {
let temp = TempDir::new().unwrap();
let dir_context = DirectoryContext::for_testing(temp.path());
let working_dir = temp.path().join("project");
std::fs::create_dir_all(&working_dir).unwrap();
// Create user config
std::fs::create_dir_all(&dir_context.config_dir).unwrap();
std::fs::write(dir_context.config_path(), r#"{"editor": {"tab_size": 2}}"#).unwrap();
let config = Config::load_with_layers(&dir_context, &working_dir);
assert_eq!(config.editor.tab_size, 2);
}
#[test]
fn platform_config_overrides_user() {
let (temp, resolver) = create_test_resolver();
// Create user config with tab_size=2
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
std::fs::write(&user_config_path, r#"{"editor": {"tab_size": 2}}"#).unwrap();
// Create platform config with tab_size=6
if let Some(platform_path) = resolver.user_platform_config_path() {
std::fs::write(&platform_path, r#"{"editor": {"tab_size": 6}}"#).unwrap();
let config = resolver.resolve().unwrap();
assert_eq!(config.editor.tab_size, 6); // Platform overrides user
}
drop(temp);
}
#[test]
fn project_overrides_platform() {
let (temp, resolver) = create_test_resolver();
// Create user config
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
std::fs::write(&user_config_path, r#"{"editor": {"tab_size": 2}}"#).unwrap();
// Create platform config
if let Some(platform_path) = resolver.user_platform_config_path() {
std::fs::write(&platform_path, r#"{"editor": {"tab_size": 6}}"#).unwrap();
}
// Create project config with tab_size=10
let project_config_path = resolver.project_config_path();
std::fs::create_dir_all(project_config_path.parent().unwrap()).unwrap();
std::fs::write(&project_config_path, r#"{"editor": {"tab_size": 10}}"#).unwrap();
let config = resolver.resolve().unwrap();
assert_eq!(config.editor.tab_size, 10); // Project overrides platform
drop(temp);
}
#[test]
fn migration_adds_version() {
let input = serde_json::json!({
"editor": {"tab_size": 2}
});
let migrated = migrate_config(input).unwrap();
assert_eq!(
migrated.get("version"),
Some(&serde_json::json!(CURRENT_CONFIG_VERSION))
);
}
#[test]
fn migration_v1_to_v2_injects_remote_element() {
// User who customized status_bar.left on v1 without the
// indicator: v2 migration prepends `"{remote}"`.
let input = serde_json::json!({
"version": 1,
"editor": {
"status_bar": {
"left": ["{filename}", "{cursor}"]
}
}
});
let migrated = migrate_config(input).unwrap();
assert_eq!(migrated.get("version"), Some(&serde_json::json!(2)));
let left = migrated
.pointer("/editor/status_bar/left")
.and_then(|v| v.as_array())
.expect("status_bar.left should remain an array");
assert_eq!(left[0], serde_json::json!("{remote}"));
assert_eq!(left[1], serde_json::json!("{filename}"));
assert_eq!(left[2], serde_json::json!("{cursor}"));
}
#[test]
fn migration_v1_to_v2_is_idempotent() {
// User already has `"{remote}"` somewhere in left (e.g. they
// opted in manually before v2). Don't double-insert.
let input = serde_json::json!({
"version": 1,
"editor": {
"status_bar": {
"left": ["{filename}", "{remote}", "{cursor}"]
}
}
});
let migrated = migrate_config(input).unwrap();
let left = migrated
.pointer("/editor/status_bar/left")
.and_then(|v| v.as_array())
.unwrap();
let remote_count = left
.iter()
.filter(|v| v.as_str() == Some("{remote}"))
.count();
assert_eq!(
remote_count, 1,
"migration should never duplicate an existing {{remote}} entry; left = {:?}",
left
);
}
#[test]
fn migration_v1_to_v2_leaves_default_users_alone() {
// User with no `status_bar` override stays on the rolling
// default — migration bumps the version but doesn't
// materialize a status_bar object.
let input = serde_json::json!({
"version": 1,
"editor": {"tab_size": 4}
});
let migrated = migrate_config(input).unwrap();
assert_eq!(migrated.get("version"), Some(&serde_json::json!(2)));
assert!(
migrated.pointer("/editor/status_bar").is_none(),
"migration must not fabricate a status_bar object for users \
who never overrode the default; migrated = {:?}",
migrated
);
}
#[test]
fn migration_renames_camelcase_keys() {
let input = serde_json::json!({
"editor": {
"tabSize": 8,
"lineNumbers": false
}
});
let migrated = migrate_config(input).unwrap();
let editor = migrated.get("editor").unwrap();
assert_eq!(editor.get("tab_size"), Some(&serde_json::json!(8)));
assert_eq!(editor.get("line_numbers"), Some(&serde_json::json!(false)));
assert!(editor.get("tabSize").is_none());
assert!(editor.get("lineNumbers").is_none());
}
#[test]
fn migration_preserves_existing_snake_case() {
let input = serde_json::json!({
"version": 1,
"editor": {"tab_size": 4}
});
let migrated = migrate_config(input).unwrap();
let editor = migrated.get("editor").unwrap();
assert_eq!(editor.get("tab_size"), Some(&serde_json::json!(4)));
}
#[test]
fn resolver_loads_legacy_camelcase_config() {
let (temp, resolver) = create_test_resolver();
// Create config with legacy camelCase keys
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
std::fs::write(
&user_config_path,
r#"{"editor": {"tabSize": 3, "lineNumbers": false}}"#,
)
.unwrap();
let config = resolver.resolve().unwrap();
assert_eq!(config.editor.tab_size, 3);
assert!(!config.editor.line_numbers);
drop(temp);
}
#[test]
fn resolver_migrates_v1_status_bar_left_on_load() {
// A user with a v1 config that customized status_bar.left
// without {remote} loads the editor for the first time on
// v2. The resolver runs the migration in-memory; the
// resolved Config has {remote} at index 0.
let (temp, resolver) = create_test_resolver();
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
std::fs::write(
&user_config_path,
r#"{
"version": 1,
"editor": {
"status_bar": {
"left": ["{filename}", "{cursor}"],
"right": []
}
}
}"#,
)
.unwrap();
let config = resolver.resolve().unwrap();
let left = &config.editor.status_bar.left;
assert_eq!(
left.first().cloned(),
Some(crate::config::StatusBarElement::RemoteIndicator),
"resolver should inject RemoteIndicator at index 0 during v1→v2 \
migration; left = {:?}",
left
);
drop(temp);
}
#[test]
fn save_and_load_session() {
let (_temp, resolver) = create_test_resolver();
let mut session = SessionConfig::new();
session.set_theme(crate::config::ThemeName::from("dark"));
session.set_editor_option(|e| e.tab_size = Some(2));
// Save session
resolver.save_session(&session).unwrap();
// Load session
let loaded = resolver.load_session().unwrap();
assert_eq!(loaded.theme, Some(crate::config::ThemeName::from("dark")));
assert_eq!(loaded.editor.as_ref().unwrap().tab_size, Some(2));
}
#[test]
fn clear_session_removes_file() {
let (_temp, resolver) = create_test_resolver();
let mut session = SessionConfig::new();
session.set_theme(crate::config::ThemeName::from("dark"));
// Save then clear
resolver.save_session(&session).unwrap();
assert!(resolver.session_config_path().exists());
resolver.clear_session().unwrap();
assert!(!resolver.session_config_path().exists());
}
#[test]
fn load_session_returns_empty_when_no_file() {
let (_temp, resolver) = create_test_resolver();
let session = resolver.load_session().unwrap();
assert!(session.is_empty());
}
#[test]
fn session_affects_resolved_config() {
let (_temp, resolver) = create_test_resolver();
// Save a session with tab_size=16
let mut session = SessionConfig::new();
session.set_editor_option(|e| e.tab_size = Some(16));
resolver.save_session(&session).unwrap();
// Resolve should pick up session value
let config = resolver.resolve().unwrap();
assert_eq!(config.editor.tab_size, 16);
}
#[test]
fn save_to_layer_writes_minimal_delta() {
let (temp, resolver) = create_test_resolver();
// Create user config with tab_size=2
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
std::fs::write(
&user_config_path,
r#"{"editor": {"tab_size": 2, "line_numbers": false}}"#,
)
.unwrap();
// Resolve the full config (inherits user values)
let mut config = resolver.resolve().unwrap();
assert_eq!(config.editor.tab_size, 2);
assert!(!config.editor.line_numbers);
// Change only tab_size in the project layer
config.editor.tab_size = 8;
// Save to project layer
resolver
.save_to_layer(&config, ConfigLayer::Project)
.unwrap();
// Read the project config file and verify it contains ONLY the delta
let project_config_path = resolver.project_config_write_path();
let content = std::fs::read_to_string(&project_config_path).unwrap();
let json: serde_json::Value = serde_json::from_str(&content).unwrap();
// Should only have editor.tab_size = 8, nothing else
assert_eq!(
json.get("editor").and_then(|e| e.get("tab_size")),
Some(&serde_json::json!(8)),
"Project config should contain tab_size override"
);
// Should NOT have line_numbers (inherited from user, not changed)
assert!(
json.get("editor")
.and_then(|e| e.get("line_numbers"))
.is_none(),
"Project config should NOT contain line_numbers (it's inherited from user layer)"
);
// Should NOT have other editor fields like scroll_offset (system default)
assert!(
json.get("editor")
.and_then(|e| e.get("scroll_offset"))
.is_none(),
"Project config should NOT contain scroll_offset (it's a system default)"
);
drop(temp);
}
/// Known limitation of save_to_layer: when a value is set to match the parent layer,
/// save_to_layer cannot distinguish this from "value unchanged" and may preserve
/// the old file value due to the merge behavior.
///
/// Use save_changes_to_layer with explicit deletions for workflows that need this.
#[test]
#[ignore = "Known limitation: save_to_layer cannot remove values that match parent layer"]
fn save_to_layer_removes_inherited_values() {
let (temp, resolver) = create_test_resolver();
// Create user config with tab_size=2
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
std::fs::write(&user_config_path, r#"{"editor": {"tab_size": 2}}"#).unwrap();
// Create project config with tab_size=8
let project_config_path = resolver.project_config_write_path();
std::fs::create_dir_all(project_config_path.parent().unwrap()).unwrap();
std::fs::write(&project_config_path, r#"{"editor": {"tab_size": 8}}"#).unwrap();
// Resolve config
let mut config = resolver.resolve().unwrap();
assert_eq!(config.editor.tab_size, 8);
// Set tab_size back to the user value (2)
config.editor.tab_size = 2;
// Save to project layer
resolver
.save_to_layer(&config, ConfigLayer::Project)
.unwrap();
// Read the project config - tab_size should be removed (same as parent)
let content = std::fs::read_to_string(&project_config_path).unwrap();
let json: serde_json::Value = serde_json::from_str(&content).unwrap();
// Should not have editor.tab_size since it matches the user value
assert!(
json.get("editor").and_then(|e| e.get("tab_size")).is_none(),
"Project config should NOT contain tab_size when it matches user layer"
);
drop(temp);
}
/// Issue #630 FIX: save_to_layer saves only the delta, defaults are inherited.
///
/// The save_to_layer method correctly:
/// 1. Saves only settings that differ from defaults
/// 2. Loads correctly because defaults are applied during resolve()
///
/// This test verifies that modifying a config and saving works correctly.
#[test]
fn issue_630_save_to_file_strips_settings_matching_defaults() {
let (_temp, resolver) = create_test_resolver();
// Create a config with some non-default settings
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
std::fs::write(
&user_config_path,
r#"{
"theme": "dracula",
"editor": {
"tab_size": 2
}
}"#,
)
.unwrap();
// Load the config
let mut config = resolver.resolve().unwrap();
assert_eq!(config.theme.0, "dracula");
assert_eq!(config.editor.tab_size, 2);
// User disables LSP via UI
if let Some(lsp_configs) = config.lsp.get_mut("python") {
for c in lsp_configs.as_mut_slice().iter_mut() {
c.enabled = false;
}
}
// Save using save_to_layer
resolver.save_to_layer(&config, ConfigLayer::User).unwrap();
// Read back the saved config file
let content = std::fs::read_to_string(&user_config_path).unwrap();
let json: serde_json::Value = serde_json::from_str(&content).unwrap();
eprintln!(
"Saved config:\n{}",
serde_json::to_string_pretty(&json).unwrap()
);
// Verify the delta contains what we changed
assert_eq!(
json.get("theme").and_then(|v| v.as_str()),
Some("dracula"),
"Theme should be saved (differs from default)"
);
assert_eq!(
json.get("editor")
.and_then(|e| e.get("tab_size"))
.and_then(|v| v.as_u64()),
Some(2),
"tab_size should be saved (differs from default)"
);
assert_eq!(
json.get("lsp")
.and_then(|l| l.get("python"))
.and_then(|p| p.get("enabled"))
.and_then(|v| v.as_bool()),
Some(false),
"lsp.python.enabled should be saved (differs from default)"
);
// Reload and verify the full config is correct
let reloaded = resolver.resolve().unwrap();
assert_eq!(reloaded.theme.0, "dracula");
assert_eq!(reloaded.editor.tab_size, 2);
assert!(!reloaded.lsp["python"].as_slice()[0].enabled);
// Command should come from defaults
assert_eq!(reloaded.lsp["python"].as_slice()[0].command, "pylsp");
}
/// Test that toggling LSP enabled/disabled preserves the command field.
///
/// 1. Start with empty config (defaults apply, python has command "pylsp")
/// 2. Disable python LSP, save
/// 3. Load, enable python LSP, save
/// 4. Load and verify command is still the default
#[test]
fn toggle_lsp_preserves_command() {
let (_temp, resolver) = create_test_resolver();
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
// Step 1: Empty config - defaults apply (python has command "pylsp")
std::fs::write(&user_config_path, r#"{}"#).unwrap();
// Load and verify default command
let config = resolver.resolve().unwrap();
let original_command = config.lsp["python"].as_slice()[0].command.clone();
assert!(
!original_command.is_empty(),
"Default python LSP should have a command"
);
// Step 2: Disable python LSP, save
let mut config = resolver.resolve().unwrap();
config.lsp.get_mut("python").unwrap().as_mut_slice()[0].enabled = false;
resolver.save_to_layer(&config, ConfigLayer::User).unwrap();
// Verify saved file only has enabled:false, not empty command/args
let saved_content = std::fs::read_to_string(&user_config_path).unwrap();
assert!(
!saved_content.contains(r#""command""#),
"Saved config should not contain 'command' field. File content: {}",
saved_content
);
assert!(
!saved_content.contains(r#""args""#),
"Saved config should not contain 'args' field. File content: {}",
saved_content
);
// Step 3: Load again, enable python LSP, save
let mut config = resolver.resolve().unwrap();
assert!(!config.lsp["python"].as_slice()[0].enabled);
config.lsp.get_mut("python").unwrap().as_mut_slice()[0].enabled = true;
resolver.save_to_layer(&config, ConfigLayer::User).unwrap();
// Step 4: Load and verify command is still the same
let config = resolver.resolve().unwrap();
assert_eq!(
config.lsp["python"].as_slice()[0].command,
original_command,
"Command should be preserved after toggling enabled. Got: '{}'",
config.lsp["python"].as_slice()[0].command
);
}
/// Issue #631 REPRODUCTION: Config with disabled LSP (no command) should be valid.
///
/// Users write configs like:
/// ```json
/// { "lsp": { "python": { "enabled": false } } }
/// ```
/// This SHOULD be valid - a disabled LSP doesn't need a command.
/// But currently it FAILS because `command` is required.
///
/// THIS TEST WILL FAIL until the bug is fixed.
#[test]
fn issue_631_disabled_lsp_without_command_should_be_valid() {
let (_temp, resolver) = create_test_resolver();
// Create the exact config from issue #631 - disabled LSP without command field
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
std::fs::write(
&user_config_path,
r#"{
"lsp": {
"json": { "enabled": false },
"python": { "enabled": false },
"toml": { "enabled": false }
},
"theme": "dracula"
}"#,
)
.unwrap();
// Try to load this config - it SHOULD succeed
let result = resolver.resolve();
// THIS ASSERTION FAILS - demonstrating bug #631
// A disabled LSP config should NOT require a command field
assert!(
result.is_ok(),
"BUG #631: Config with disabled LSP should be valid even without 'command' field. \
Got parse error: {:?}",
result.err()
);
// Verify the theme was loaded (config parsed correctly)
let config = result.unwrap();
assert_eq!(
config.theme.0, "dracula",
"Theme should be 'dracula' from config file"
);
}
/// Test that loading a config without command field uses the default command.
#[test]
fn loading_lsp_without_command_uses_default() {
let (_temp, resolver) = create_test_resolver();
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
// Write config with rust LSP but no command field
std::fs::write(
&user_config_path,
r#"{ "lsp": { "rust": { "enabled": false } } }"#,
)
.unwrap();
// Load and check that command comes from defaults
let config = resolver.resolve().unwrap();
assert_eq!(
config.lsp["rust"].as_slice()[0].command,
"rust-analyzer",
"Command should come from defaults when not in file. Got: '{}'",
config.lsp["rust"].as_slice()[0].command
);
assert!(
!config.lsp["rust"].as_slice()[0].enabled,
"enabled should be false from file"
);
}
/// Test simulating the Settings UI flow using save_changes_to_layer:
/// 1. Load config with defaults
/// 2. Apply change (toggle enabled) via JSON pointer (like Settings UI does)
/// 3. Save via save_changes_to_layer with explicit changes
/// 4. Reload and verify command is preserved
#[test]
fn settings_ui_toggle_lsp_preserves_command() {
let (_temp, resolver) = create_test_resolver();
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
// Step 1: Start with empty config
std::fs::write(&user_config_path, r#"{}"#).unwrap();
// Load resolved config - should have rust with command="rust-analyzer"
let config = resolver.resolve().unwrap();
assert_eq!(
config.lsp["rust"].as_slice()[0].command,
"rust-analyzer",
"Default rust command should be rust-analyzer"
);
assert!(
config.lsp["rust"].as_slice()[0].enabled,
"Default rust enabled should be true"
);
// Step 2: Simulate Settings UI applying a change to disable rust LSP
// Using save_changes_to_layer with explicit change tracking
let mut changes = std::collections::HashMap::new();
changes.insert("/lsp/rust/enabled".to_string(), serde_json::json!(false));
let deletions = std::collections::HashSet::new();
// Step 3: Save via save_changes_to_layer
resolver
.save_changes_to_layer(&changes, &deletions, ConfigLayer::User)
.unwrap();
// Check what was saved to file
let saved_content = std::fs::read_to_string(&user_config_path).unwrap();
eprintln!("After disable, file contains:\n{}", saved_content);
// Step 4: Reload and verify command is preserved
let reloaded = resolver.resolve().unwrap();
assert_eq!(
reloaded.lsp["rust"].as_slice()[0].command,
"rust-analyzer",
"Command should be preserved after save/reload (disabled). Got: '{}'",
reloaded.lsp["rust"].as_slice()[0].command
);
assert!(
!reloaded.lsp["rust"].as_slice()[0].enabled,
"rust should be disabled"
);
// Step 5: Re-enable rust LSP (simulating Settings UI)
let mut changes = std::collections::HashMap::new();
changes.insert("/lsp/rust/enabled".to_string(), serde_json::json!(true));
let deletions = std::collections::HashSet::new();
// Step 6: Save via save_changes_to_layer
resolver
.save_changes_to_layer(&changes, &deletions, ConfigLayer::User)
.unwrap();
// Check what was saved to file
let saved_content = std::fs::read_to_string(&user_config_path).unwrap();
eprintln!("After re-enable, file contains:\n{}", saved_content);
// Step 7: Reload and verify command is STILL preserved
let final_config = resolver.resolve().unwrap();
assert_eq!(
final_config.lsp["rust"].as_slice()[0].command,
"rust-analyzer",
"Command should be preserved after toggle cycle. Got: '{}'",
final_config.lsp["rust"].as_slice()[0].command
);
assert!(
final_config.lsp["rust"].as_slice()[0].enabled,
"rust should be enabled"
);
}
/// Issue #806 REPRODUCTION: Manual config.json edits are lost when saving from Settings UI.
///
/// Scenario:
/// 1. User manually edits config.json to add custom LSP settings (e.g., rust-analyzer with custom args)
/// 2. User opens Settings UI and changes a simple setting (e.g., tab_size)
/// 3. User saves the settings
/// 4. Result: The manually-added LSP settings are GONE
///
/// Expected behavior: Only the changed setting (tab_size) should be modified;
/// the manually-added LSP settings should be preserved.
#[test]
fn issue_806_manual_config_edits_lost_when_saving_from_ui() {
let (_temp, resolver) = create_test_resolver();
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
// Step 1: User manually creates config.json with custom LSP settings
// This is the EXACT example from issue #806
std::fs::write(
&user_config_path,
r#"{
"lsp": {
"rust-analyzer": {
"enabled": true,
"command": "rust-analyzer",
"args": ["--log-file", "/tmp/rust-analyzer-{pid}.log"],
"languages": ["rust"]
}
}
}"#,
)
.unwrap();
// Step 2: Load the config (simulating Fresh startup)
let config = resolver.resolve().unwrap();
// Verify the custom LSP settings were loaded
assert!(
config.lsp.contains_key("rust-analyzer"),
"Config should contain manually-added 'rust-analyzer' LSP entry"
);
let rust_analyzer = &config.lsp["rust-analyzer"].as_slice()[0];
assert!(rust_analyzer.enabled, "rust-analyzer should be enabled");
assert_eq!(
rust_analyzer.command, "rust-analyzer",
"rust-analyzer command should be preserved"
);
assert_eq!(
rust_analyzer.args,
vec!["--log-file", "/tmp/rust-analyzer-{pid}.log"],
"rust-analyzer args should be preserved"
);
// Step 3: User opens Settings UI and changes tab_size
// This simulates what SettingsState::apply_changes does
let mut config_json = serde_json::to_value(&config).unwrap();
*config_json
.pointer_mut("/editor/tab_size")
.expect("path should exist") = serde_json::json!(2);
let modified_config: crate::config::Config =
serde_json::from_value(config_json).expect("should deserialize");
// Step 4: Save via save_to_layer (what save_settings() does)
resolver
.save_to_layer(&modified_config, ConfigLayer::User)
.unwrap();
// Step 5: Check what was saved to file
let saved_content = std::fs::read_to_string(&user_config_path).unwrap();
let saved_json: serde_json::Value = serde_json::from_str(&saved_content).unwrap();
eprintln!(
"Issue #806 - Saved config after changing tab_size:\n{}",
serde_json::to_string_pretty(&saved_json).unwrap()
);
// CRITICAL ASSERTION: The "lsp" section with "rust-analyzer" MUST still be present
assert!(
saved_json.get("lsp").is_some(),
"BUG #806: 'lsp' section should NOT be deleted when saving unrelated changes. \
File content: {}",
saved_content
);
assert!(
saved_json
.get("lsp")
.and_then(|l| l.get("rust-analyzer"))
.is_some(),
"BUG #806: 'lsp.rust-analyzer' should NOT be deleted when saving unrelated changes. \
File content: {}",
saved_content
);
// Verify the custom args are preserved
let saved_args = saved_json
.get("lsp")
.and_then(|l| l.get("rust-analyzer"))
.and_then(|r| r.get("args"));
assert!(
saved_args.is_some(),
"BUG #806: 'lsp.rust-analyzer.args' should be preserved. File content: {}",
saved_content
);
assert_eq!(
saved_args.unwrap(),
&serde_json::json!(["--log-file", "/tmp/rust-analyzer-{pid}.log"]),
"BUG #806: Custom args should be preserved exactly"
);
// Verify the tab_size change was saved
assert_eq!(
saved_json
.get("editor")
.and_then(|e| e.get("tab_size"))
.and_then(|v| v.as_u64()),
Some(2),
"tab_size should be saved"
);
// Step 6: Reload and verify everything is intact
let reloaded = resolver.resolve().unwrap();
assert_eq!(
reloaded.editor.tab_size, 2,
"tab_size change should be persisted"
);
assert!(
reloaded.lsp.contains_key("rust-analyzer"),
"BUG #806: rust-analyzer should still exist after reload"
);
let reloaded_ra = &reloaded.lsp["rust-analyzer"].as_slice()[0];
assert_eq!(
reloaded_ra.args,
vec!["--log-file", "/tmp/rust-analyzer-{pid}.log"],
"BUG #806: Custom args should survive save/reload cycle"
);
}
/// Issue #806 - Variant: Test with multiple custom settings that don't exist in defaults.
///
/// This tests a broader scenario where the user has added multiple custom
/// configurations that are not part of the default config structure.
#[test]
fn issue_806_custom_lsp_entries_preserved_across_unrelated_changes() {
let (_temp, resolver) = create_test_resolver();
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
// User creates config with a completely custom LSP server not in defaults
std::fs::write(
&user_config_path,
r#"{
"theme": "dracula",
"lsp": {
"my-custom-lsp": {
"enabled": true,
"command": "/usr/local/bin/my-custom-lsp",
"args": ["--verbose", "--config", "/etc/my-lsp.json"],
"languages": ["mycustomlang"]
}
},
"languages": {
"mycustomlang": {
"extensions": [".mcl"],
"grammar": "mycustomlang"
}
}
}"#,
)
.unwrap();
// Load and verify custom settings exist
let config = resolver.resolve().unwrap();
assert!(
config.lsp.contains_key("my-custom-lsp"),
"Custom LSP entry should be loaded"
);
assert!(
config.languages.contains_key("mycustomlang"),
"Custom language should be loaded"
);
// User changes only line_numbers in Settings UI
let mut config_json = serde_json::to_value(&config).unwrap();
*config_json
.pointer_mut("/editor/line_numbers")
.expect("path should exist") = serde_json::json!(false);
let modified_config: crate::config::Config =
serde_json::from_value(config_json).expect("should deserialize");
// Save
resolver
.save_to_layer(&modified_config, ConfigLayer::User)
.unwrap();
// Verify file still contains custom LSP
let saved_content = std::fs::read_to_string(&user_config_path).unwrap();
let saved_json: serde_json::Value = serde_json::from_str(&saved_content).unwrap();
eprintln!(
"Saved config:\n{}",
serde_json::to_string_pretty(&saved_json).unwrap()
);
// Custom LSP must be preserved
assert!(
saved_json
.get("lsp")
.and_then(|l| l.get("my-custom-lsp"))
.is_some(),
"BUG #806: Custom LSP 'my-custom-lsp' should be preserved. Got: {}",
saved_content
);
// Custom language must be preserved
assert!(
saved_json
.get("languages")
.and_then(|l| l.get("mycustomlang"))
.is_some(),
"BUG #806: Custom language 'mycustomlang' should be preserved. Got: {}",
saved_content
);
// Reload and verify
let reloaded = resolver.resolve().unwrap();
assert!(
reloaded.lsp.contains_key("my-custom-lsp"),
"Custom LSP should survive save/reload"
);
assert!(
reloaded.languages.contains_key("mycustomlang"),
"Custom language should survive save/reload"
);
assert!(
!reloaded.editor.line_numbers,
"line_numbers change should be applied"
);
}
/// Issue #806 - Scenario 2: External file modification after Fresh is running.
///
/// This is the most likely real-world scenario:
/// 1. User starts Fresh with default/existing config (loaded into memory)
/// 2. User manually edits config.json WHILE Fresh is running (external edit)
/// 3. User opens Settings UI in Fresh and changes a simple setting
/// 4. User saves from Settings UI
/// 5. BUG: The external edits are LOST because Fresh's in-memory config
/// doesn't have them
///
/// This test verifies that even if the file was modified externally,
/// the save operation should preserve those external changes.
#[test]
fn issue_806_external_file_modification_lost_on_ui_save() {
let (_temp, resolver) = create_test_resolver();
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
// Step 1: User starts Fresh with a simple config
std::fs::write(&user_config_path, r#"{"theme": "monokai"}"#).unwrap();
// Step 2: Fresh loads the config (simulating startup)
let config_at_startup = resolver.resolve().unwrap();
assert_eq!(config_at_startup.theme.0, "monokai");
assert!(
!config_at_startup.lsp.contains_key("rust-analyzer"),
"No custom LSP at startup"
);
// Step 3: User externally edits config.json (e.g., with another editor)
// to add custom LSP settings. Fresh doesn't see this change yet.
std::fs::write(
&user_config_path,
r#"{
"theme": "monokai",
"lsp": {
"rust-analyzer": {
"enabled": true,
"command": "rust-analyzer",
"args": ["--log-file", "/tmp/ra.log"]
}
}
}"#,
)
.unwrap();
// Step 4: User opens Settings UI and changes tab_size
// The Settings UI works with the IN-MEMORY config (config_at_startup)
// which does NOT have the external LSP changes
let mut config_json = serde_json::to_value(&config_at_startup).unwrap();
*config_json
.pointer_mut("/editor/tab_size")
.expect("path should exist") = serde_json::json!(2);
let modified_config: crate::config::Config =
serde_json::from_value(config_json).expect("should deserialize");
// Step 5: User saves from Settings UI
// This is where the bug occurs - the in-memory config (without LSP)
// is saved, overwriting the external changes
resolver
.save_to_layer(&modified_config, ConfigLayer::User)
.unwrap();
// Step 6: Check what was saved
let saved_content = std::fs::read_to_string(&user_config_path).unwrap();
let saved_json: serde_json::Value = serde_json::from_str(&saved_content).unwrap();
eprintln!(
"Issue #806 scenario 2 - After UI save (external edits should be preserved):\n{}",
serde_json::to_string_pretty(&saved_json).unwrap()
);
// This assertion will FAIL if the bug exists
// The LSP section added externally should be preserved
// BUT with current implementation, it will be LOST because
// save_to_layer computes delta from in-memory config (which has no LSP)
// vs system defaults, NOT from the current file contents
assert!(
saved_json.get("lsp").is_some(),
"BUG #806: External edits to config.json were lost! \
The 'lsp' section added while Fresh was running should be preserved. \
Saved content: {}",
saved_content
);
assert!(
saved_json
.get("lsp")
.and_then(|l| l.get("rust-analyzer"))
.is_some(),
"BUG #806: rust-analyzer config should be preserved"
);
}
/// Issue #806 - Scenario 3: Multiple users/processes editing config
///
/// Even more edge case: Config is modified by another process right before save.
/// This demonstrates that save_to_layer() should ideally do a read-modify-write
/// operation, not just a write.
#[test]
fn issue_806_concurrent_modification_scenario() {
let (_temp, resolver) = create_test_resolver();
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
// Start with empty config
std::fs::write(&user_config_path, r#"{}"#).unwrap();
// Load config
let mut config = resolver.resolve().unwrap();
// Modify in memory: change tab_size
config.editor.tab_size = 8;
// Meanwhile, another process adds LSP config to the file
std::fs::write(
&user_config_path,
r#"{
"lsp": {
"custom-lsp": {
"enabled": true,
"command": "/usr/bin/custom-lsp"
}
}
}"#,
)
.unwrap();
// Now save our in-memory config
// With current implementation, this will OVERWRITE the concurrent changes
resolver.save_to_layer(&config, ConfigLayer::User).unwrap();
// Check result
let saved_content = std::fs::read_to_string(&user_config_path).unwrap();
let saved_json: serde_json::Value = serde_json::from_str(&saved_content).unwrap();
eprintln!(
"Concurrent modification scenario result:\n{}",
serde_json::to_string_pretty(&saved_json).unwrap()
);
// Verify our change was saved
assert_eq!(
saved_json
.get("editor")
.and_then(|e| e.get("tab_size"))
.and_then(|v| v.as_u64()),
Some(8),
"Our tab_size change should be saved"
);
// The concurrent LSP change will be lost with current implementation
// This is a known limitation - documenting it here
// A proper fix would involve read-modify-write with conflict detection
//
// For now, we just document that this scenario loses concurrent changes:
let lsp_preserved = saved_json.get("lsp").is_some();
if !lsp_preserved {
eprintln!(
"NOTE: Concurrent file modifications are lost with current implementation. \
This is expected behavior but could be improved with read-modify-write pattern."
);
}
}
/// Bug reproduction: changing a config value to match the default should persist.
///
/// When a user changes a setting FROM a non-default value TO the default value,
/// the change should be saved (either by writing the default value explicitly,
/// or by removing the field so the default propagates).
///
/// The bug: save_to_layer computes delta vs defaults, so changing TO default
/// results in no delta for that field. The merge with existing file then
/// preserves the OLD value from the file instead of the new default.
#[test]
fn save_to_layer_changing_to_default_value_should_persist() {
let (_temp, resolver) = create_test_resolver();
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
// Step 1: Create user config with non-default theme
std::fs::write(&user_config_path, r#"{"theme": "dracula"}"#).unwrap();
// Step 2: Load config - theme should be "dracula" from file
let baseline = resolver.resolve().unwrap();
assert_eq!(
baseline.theme.0, "dracula",
"Theme should be 'dracula' from file"
);
// Step 3: User changes theme to the DEFAULT value ("high-contrast")
let mut config = baseline.clone();
config.theme = crate::config::ThemeName::from("high-contrast");
// Step 4: Save the change using baseline tracking
resolver
.save_to_layer_with_baseline(&config, &baseline, ConfigLayer::User)
.unwrap();
// Step 5: Check what was saved to file
let saved_content = std::fs::read_to_string(&user_config_path).unwrap();
eprintln!(
"Saved config after changing to default theme:\n{}",
saved_content
);
// Step 6: Reload config
let reloaded = resolver.resolve().unwrap();
// The theme should be "high-contrast" (either explicitly in file, or absent so default applies)
assert_eq!(
reloaded.theme.0, "high-contrast",
"Theme should be 'high-contrast' after changing to default and saving. \
With save_to_layer_with_baseline, the theme field should be removed from file \
so the default applies. File content: {}",
saved_content
);
}
/// Test that universal_lsp config round-trips through save/load correctly.
/// This exercises the PartialConfig From/resolve_with_defaults paths.
#[test]
fn universal_lsp_round_trip_via_config_resolver() {
let (_temp, resolver) = create_test_resolver();
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
// Write a config that enables quicklsp
std::fs::write(
&user_config_path,
r#"{
"universal_lsp": {
"quicklsp": { "enabled": true, "auto_start": true }
}
}"#,
)
.unwrap();
let config = resolver.resolve().unwrap();
// quicklsp should be enabled (user override merged with defaults)
assert!(config.universal_lsp.contains_key("quicklsp"));
let server = &config.universal_lsp["quicklsp"].as_slice()[0];
assert!(server.enabled, "User override should enable quicklsp");
assert!(server.auto_start, "User override should enable auto_start");
assert_eq!(
server.command, "quicklsp",
"Command should come from defaults"
);
}
/// Test that universal_lsp supports adding custom servers alongside defaults.
#[test]
fn universal_lsp_custom_server_merges_with_defaults() {
let (_temp, resolver) = create_test_resolver();
let user_config_path = resolver.user_config_path();
std::fs::create_dir_all(user_config_path.parent().unwrap()).unwrap();
std::fs::write(
&user_config_path,
r#"{
"universal_lsp": {
"my-universal-server": {
"command": "my-server-bin",
"enabled": true
}
}
}"#,
)
.unwrap();
let config = resolver.resolve().unwrap();
// Custom server should be present
assert!(
config.universal_lsp.contains_key("my-universal-server"),
"Custom universal server should be loaded"
);
assert_eq!(
config.universal_lsp["my-universal-server"].as_slice()[0].command,
"my-server-bin"
);
// Default quicklsp should still be present (merged from defaults)
assert!(
config.universal_lsp.contains_key("quicklsp"),
"Default quicklsp should be preserved when adding custom servers"
);
}
/// Test that the PartialConfig conversion (Config -> PartialConfig -> Config)
/// preserves universal_lsp entries. This catches bugs where universal_lsp
/// is missing from the PartialConfig struct or its From/resolve impls.
#[test]
fn universal_lsp_partial_config_round_trip() {
use crate::partial_config::PartialConfig;
let mut config = Config::default();
// Enable quicklsp in the original config
if let Some(quicklsp) = config.universal_lsp.get_mut("quicklsp") {
quicklsp.as_mut_slice()[0].enabled = true;
}
// Convert to partial and back
let partial = PartialConfig::from(&config);
let resolved = partial.resolve();
// Verify universal_lsp survived the round trip
assert!(
resolved.universal_lsp.contains_key("quicklsp"),
"quicklsp should survive Config -> PartialConfig -> Config round trip"
);
assert!(
resolved.universal_lsp["quicklsp"].as_slice()[0].enabled,
"quicklsp enabled state should be preserved through round trip"
);
}
}