fresh-editor 0.3.7

A lightweight, fast terminal-based text editor with LSP support and TypeScript plugins
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
//! JSON Schema parsing for settings UI
//!
//! Parses the config JSON Schema to build the settings UI structure.
//!
//! # Extensible Enums with `x-enum-values`
//!
//! This module supports a custom JSON Schema extension called `x-enum-values` that allows
//! enum values to be defined separately from type definitions. This enables extensibility -
//! new enum values can be added without modifying the type schema.
//!
//! ## How it works
//!
//! 1. Define a type in `$defs` (without hardcoded enum values):
//!    ```json
//!    "$defs": {
//!      "ThemeOptions": {
//!        "type": "string"
//!      }
//!    }
//!    ```
//!
//! 2. Reference the type in properties:
//!    ```json
//!    "properties": {
//!      "theme": {
//!        "$ref": "#/$defs/ThemeOptions",
//!        "default": "dark"
//!      }
//!    }
//!    ```
//!
//! 3. Define enum values separately - each value declares which type it extends:
//!    ```json
//!    "x-enum-values": [
//!      { "ref": "#/$defs/ThemeOptions", "name": "Dark", "value": "dark" },
//!      { "ref": "#/$defs/ThemeOptions", "name": "Light", "value": "light" },
//!      { "ref": "#/$defs/ThemeOptions", "name": "High Contrast", "value": "high-contrast" }
//!    ]
//!    ```
//!
//! ## Entry structure
//!
//! Each entry in `x-enum-values` has:
//! - `ref` (required): JSON pointer to the type being extended (e.g., `#/$defs/ThemeOptions`)
//! - `value` (required): The actual value, must match the referenced type
//! - `name` (optional): Human-friendly display name, defaults to `value` if not provided
//!
//! ## Benefits
//!
//! - **Extensibility**: Add new values without changing the schema structure
//! - **Self-describing**: Values declare which type they belong to
//! - **Plugin-friendly**: External sources can contribute enum values
//! - **Type-safe**: Values are validated against their referenced type

use rust_i18n::t;
use serde::Deserialize;
use std::collections::HashMap;

/// A property/setting from the schema
#[derive(Debug, Clone)]
pub struct SettingSchema {
    /// JSON pointer path (e.g., "/editor/tab_size")
    pub path: String,
    /// Human-readable name derived from property name
    pub name: String,
    /// Description from schema
    pub description: Option<String>,
    /// The type of this setting
    pub setting_type: SettingType,
    /// Default value (as JSON)
    pub default: Option<serde_json::Value>,
    /// Whether this field is read-only (cannot be edited by user)
    pub read_only: bool,
    /// Section/group within the category (from x-section)
    pub section: Option<String>,
    /// Sort order override (from x-order). Lower values sort first.
    /// When set, overrides alphabetical sorting.
    pub order: Option<i32>,
    /// Whether this setting accepts null (i.e., can be "unset" to inherit).
    /// Derived from JSON Schema `"type": ["<type>", "null"]`.
    pub nullable: bool,
    /// Dynamic enum source path: derive dropdown options from the keys of
    /// another config property at runtime (e.g., "/languages").
    pub enum_from: Option<String>,
    /// Path to the sibling dual-list setting (for cross-exclusion)
    pub dual_list_sibling: Option<String>,
    /// Whether this field can be dynamically extended with runtime options (e.g., custom tokens from plugins)
    pub dynamically_extendable_status_bar_elements: bool,
}

/// Type of a setting, determines which control to render
#[derive(Debug, Clone)]
pub enum SettingType {
    /// Boolean toggle
    Boolean,
    /// Integer number with optional min/max
    Integer {
        minimum: Option<i64>,
        maximum: Option<i64>,
    },
    /// Floating point number
    Number {
        minimum: Option<f64>,
        maximum: Option<f64>,
    },
    /// Free-form string
    String,
    /// String with enumerated options (display name, value)
    Enum { options: Vec<EnumOption> },
    /// Array of strings
    StringArray,
    /// Array of integers (rendered as TextList, values parsed as numbers)
    IntegerArray,
    /// Array of objects with a schema (for keybindings, etc.)
    ObjectArray {
        item_schema: Box<SettingSchema>,
        /// JSON pointer to field within item to display as preview (e.g., "/action")
        display_field: Option<String>,
    },
    /// Nested object (category)
    Object { properties: Vec<SettingSchema> },
    /// Map with string keys (for languages, lsp configs)
    Map {
        value_schema: Box<SettingSchema>,
        /// JSON pointer to field within value to display as preview (e.g., "/command")
        display_field: Option<String>,
        /// Whether to disallow adding new entries (entries are auto-managed)
        no_add: bool,
    },
    /// Dual-list: ordered subset of a fixed set of options with sibling cross-exclusion
    DualList {
        options: Vec<EnumOption>,
        sibling_path: Option<String>,
    },
    /// Complex type we can't edit directly
    Complex,
}

/// An option in an enum type
#[derive(Debug, Clone)]
pub struct EnumOption {
    /// Display name shown in UI
    pub name: String,
    /// Actual value stored in config
    pub value: String,
}

/// A category in the settings tree
#[derive(Debug, Clone)]
pub struct SettingCategory {
    /// Category name (e.g., "Editor", "File Explorer")
    pub name: String,
    /// JSON path prefix for this category
    pub path: String,
    /// Description of this category
    pub description: Option<String>,
    /// Whether this category is nullable (e.g., `Option<LanguageConfig>`)
    /// and can be cleared as a whole.
    pub nullable: bool,
    /// Settings in this category
    pub settings: Vec<SettingSchema>,
    /// Subcategories
    pub subcategories: Vec<SettingCategory>,
}

/// Raw JSON Schema structure for deserialization
#[derive(Debug, Deserialize)]
struct RawSchema {
    #[serde(rename = "type")]
    schema_type: Option<SchemaType>,
    description: Option<String>,
    default: Option<serde_json::Value>,
    properties: Option<HashMap<String, RawSchema>>,
    items: Option<Box<RawSchema>>,
    #[serde(rename = "enum")]
    enum_values: Option<Vec<serde_json::Value>>,
    minimum: Option<serde_json::Number>,
    maximum: Option<serde_json::Number>,
    #[serde(rename = "$ref")]
    ref_path: Option<String>,
    #[serde(rename = "$defs")]
    defs: Option<HashMap<String, RawSchema>>,
    #[serde(rename = "additionalProperties")]
    additional_properties: Option<AdditionalProperties>,
    /// Extensible enum values - see module docs for details
    #[serde(rename = "x-enum-values", default)]
    extensible_enum_values: Vec<EnumValueEntry>,
    /// Custom extension: field to display as preview in Map/ObjectArray entries
    /// e.g., "/command" for OnSaveAction, "/action" for Keybinding
    #[serde(rename = "x-display-field")]
    display_field: Option<String>,
    /// Whether this field is read-only
    #[serde(rename = "readOnly", default)]
    read_only: bool,
    /// Whether this Map-type property should be rendered as its own category
    #[serde(rename = "x-standalone-category", default)]
    standalone_category: bool,
    /// Whether this Map should disallow adding new entries (entries are auto-managed)
    #[serde(rename = "x-no-add", default)]
    no_add: bool,
    /// Section/group within the category for organizing related settings
    #[serde(rename = "x-section")]
    section: Option<String>,
    /// Sort order override for field ordering in entry dialogs
    #[serde(rename = "x-order")]
    order: Option<i32>,
    /// anyOf combinator (used by schemars for Option<T> where T is a struct)
    #[serde(rename = "anyOf")]
    any_of: Option<Vec<RawSchema>>,
    /// Dynamic enum: derive dropdown options from the keys of another config
    /// property at runtime (e.g., `"x-enum-from": "/languages"` populates
    /// the dropdown with keys from the `languages` HashMap).
    #[serde(rename = "x-enum-from")]
    enum_from: Option<String>,
    /// Dual-list options defined on the item schema (array of {value, name})
    #[serde(rename = "x-dual-list-options", default)]
    dual_list_options: Vec<DualListOptionEntry>,
    /// Path to the sibling dual-list setting (for cross-exclusion)
    #[serde(rename = "x-dual-list-sibling")]
    dual_list_sibling: Option<String>,
    /// Whether this field can be dynamically extended with runtime options (e.g., custom tokens from plugins)
    #[serde(rename = "x-dynamically-extendable-status-bar-elements", default)]
    dynamically_extendable_status_bar_elements: bool,
}

/// An entry in the x-enum-values array
#[derive(Debug, Deserialize)]
struct EnumValueEntry {
    /// JSON pointer to the type being extended (e.g., "#/$defs/ThemeOptions")
    #[serde(rename = "ref")]
    ref_path: String,
    /// Human-friendly display name (optional, defaults to value)
    name: Option<String>,
    /// The actual value (must match the referenced type)
    value: serde_json::Value,
}

/// An option entry in x-dual-list-options
#[derive(Debug, Deserialize)]
struct DualListOptionEntry {
    /// The actual value (e.g., "{filename}")
    value: String,
    /// Human-friendly display name
    name: Option<String>,
}

/// additionalProperties can be a boolean or a schema object
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum AdditionalProperties {
    Bool(bool),
    Schema(Box<RawSchema>),
}

/// JSON Schema type can be a single string or an array of strings
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum SchemaType {
    Single(String),
    Multiple(Vec<String>),
}

impl SchemaType {
    /// Get the primary type (first type if array, or the single type)
    fn primary(&self) -> Option<&str> {
        match self {
            Self::Single(s) => Some(s.as_str()),
            Self::Multiple(v) => v.first().map(|s| s.as_str()),
        }
    }

    /// Check if this type includes "null" (i.e., the field is nullable/optional)
    fn contains_null(&self) -> bool {
        match self {
            Self::Single(s) => s == "null",
            Self::Multiple(v) => v.iter().any(|s| s == "null"),
        }
    }
}

/// Map from $ref paths to their enum options
type EnumValuesMap = HashMap<String, Vec<EnumOption>>;

/// Parse the JSON Schema and build the category tree
pub fn parse_schema(schema_json: &str) -> Result<Vec<SettingCategory>, serde_json::Error> {
    let raw: RawSchema = serde_json::from_str(schema_json)?;

    let defs = raw.defs.unwrap_or_default();
    let properties = raw.properties.unwrap_or_default();

    // Build enum values map from x-enum-values entries
    let enum_values_map = build_enum_values_map(&raw.extensible_enum_values);

    let mut categories = Vec::new();
    let mut top_level_settings = Vec::new();

    // Process each top-level property (sorted for deterministic output)
    let mut sorted_props: Vec<_> = properties.into_iter().collect();
    sorted_props.sort_by(|a, b| a.0.cmp(&b.0));
    for (name, prop) in sorted_props {
        let path = format!("/{}", name);
        let display_name = humanize_name(&name);

        // Resolve references
        let resolved = resolve_ref(&prop, &defs);

        // Detect if this property is nullable (Option<T> generates anyOf with null variant)
        let is_nullable = prop.any_of.as_ref().is_some_and(|variants| {
            variants.iter().any(|v| {
                v.schema_type
                    .as_ref()
                    .map(|t| t.primary() == Some("null"))
                    .unwrap_or(false)
            })
        });

        // Check if this property should be a standalone category (for Map types)
        if prop.standalone_category {
            // Create a category with the Map setting as its only content
            let setting = parse_setting(&name, &path, &prop, &defs, &enum_values_map);
            categories.push(SettingCategory {
                name: display_name,
                path: path.clone(),
                description: prop.description.clone().or(resolved.description.clone()),
                nullable: is_nullable,
                settings: vec![setting],
                subcategories: Vec::new(),
            });
        } else if let Some(ref inner_props) = resolved.properties {
            // This is a category with nested settings.
            let settings = parse_properties(inner_props, &path, &defs, &enum_values_map);
            // Prefer the field-level doc comment (more specific to how the
            // category is used) over the struct-level one (often generic
            // boilerplate like "Editor configuration"). When both exist they
            // tend to read as near-duplicates side by side, so we don't
            // concatenate them.
            let description = prop
                .description
                .clone()
                .or_else(|| resolved.description.clone());
            categories.push(SettingCategory {
                name: display_name,
                path: path.clone(),
                description,
                nullable: is_nullable,
                settings,
                subcategories: Vec::new(),
            });
        } else {
            // This is a top-level setting
            let setting = parse_setting(&name, &path, &prop, &defs, &enum_values_map);
            top_level_settings.push(setting);
        }
    }

    // If there are top-level settings, create a "General" category for them
    if !top_level_settings.is_empty() {
        // Sort top-level settings alphabetically
        top_level_settings.sort_by(|a, b| a.name.cmp(&b.name));
        categories.insert(
            0,
            SettingCategory {
                name: "General".to_string(),
                path: String::new(),
                description: Some("General settings".to_string()),
                nullable: false,
                settings: top_level_settings,
                subcategories: Vec::new(),
            },
        );
    }

    // Sort categories alphabetically, but keep General first
    categories.sort_by(|a, b| match (a.name.as_str(), b.name.as_str()) {
        ("General", _) => std::cmp::Ordering::Less,
        (_, "General") => std::cmp::Ordering::Greater,
        (a, b) => a.cmp(b),
    });

    Ok(categories)
}

/// Append a top-level "Plugin Settings" category whose subcategories are
/// built from per-plugin schema sidecars (`<plugin_name>.schema.json`).
///
/// Each sub-category lives at JSON pointer `/plugins/<name>/settings`.
/// Only the names passed in `enabled_plugins_with_schema` are rendered —
/// disabled plugins are hidden from the Settings UI (per the design
/// decision).
pub fn append_plugin_settings_category(
    categories: &mut Vec<SettingCategory>,
    plugin_schemas: &HashMap<String, serde_json::Value>,
    enabled_plugins_with_schema: &[String],
) {
    if enabled_plugins_with_schema.is_empty() {
        return;
    }

    // Push each plugin as its own top-level category, prefixed so they
    // cluster together in the left-panel alphabetical sort and don't
    // collide with built-in names like "Editor" / "Plugins".
    let mut added = 0;
    for name in enabled_plugins_with_schema {
        let Some(schema_value) = plugin_schemas.get(name) else {
            continue;
        };
        let Some(mut category) = plugin_schema_to_category(name, schema_value) else {
            continue;
        };
        category.name = format!("Plugin: {}", name);
        categories.push(category);
        added += 1;
    }

    if added == 0 {
        return;
    }

    // Re-sort categories. "General" stays first; "Plugin: <name>"
    // entries are pushed to the bottom of the list so plugin
    // configuration doesn't interleave with built-in editor settings.
    // Within each band the order is alphabetical.
    categories.sort_by(|a, b| match (a.name.as_str(), b.name.as_str()) {
        ("General", _) => std::cmp::Ordering::Less,
        (_, "General") => std::cmp::Ordering::Greater,
        (a, b) => match (a.starts_with("Plugin: "), b.starts_with("Plugin: ")) {
            (true, false) => std::cmp::Ordering::Greater,
            (false, true) => std::cmp::Ordering::Less,
            _ => a.cmp(b),
        },
    });
}

fn plugin_schema_to_category(
    plugin_name: &str,
    schema_value: &serde_json::Value,
) -> Option<SettingCategory> {
    // Parse the plugin's schema as if it were a regular JSON Schema
    // fragment, then walk its `properties` to build SettingSchema entries
    // rooted at `/plugins/<name>/settings/...`.
    let raw: RawSchema = serde_json::from_value(schema_value.clone()).ok()?;
    let defs = HashMap::new();
    let enum_values_map = HashMap::new();
    let base_path = format!("/plugins/{}/settings", plugin_name);

    let properties = raw.properties.as_ref()?;
    let settings = parse_properties(properties, &base_path, &defs, &enum_values_map);
    if settings.is_empty() {
        return None;
    }

    Some(SettingCategory {
        name: plugin_name.to_string(),
        path: base_path,
        description: raw.description.clone(),
        nullable: false,
        settings,
        subcategories: Vec::new(),
    })
}

/// Build a map from $ref paths to their enum options
fn build_enum_values_map(entries: &[EnumValueEntry]) -> EnumValuesMap {
    let mut map: EnumValuesMap = HashMap::new();

    for entry in entries {
        let value_str = match &entry.value {
            serde_json::Value::String(s) => s.clone(),
            other => other.to_string(),
        };

        let option = EnumOption {
            name: entry.name.clone().unwrap_or_else(|| value_str.clone()),
            value: value_str,
        };

        map.entry(entry.ref_path.clone()).or_default().push(option);
    }

    map
}

/// Parse properties into settings
fn parse_properties(
    properties: &HashMap<String, RawSchema>,
    parent_path: &str,
    defs: &HashMap<String, RawSchema>,
    enum_values_map: &EnumValuesMap,
) -> Vec<SettingSchema> {
    let mut settings = Vec::new();

    for (name, prop) in properties {
        let path = format!("{}/{}", parent_path, name);
        let setting = parse_setting(name, &path, prop, defs, enum_values_map);

        settings.push(setting);
    }

    // Sort settings: by x-order (if set) first, then alphabetically by name.
    // Settings with x-order come before those without.
    settings.sort_by(|a, b| match (a.order, b.order) {
        (Some(a_ord), Some(b_ord)) => a_ord.cmp(&b_ord).then_with(|| a.name.cmp(&b.name)),
        (Some(_), None) => std::cmp::Ordering::Less,
        (None, Some(_)) => std::cmp::Ordering::Greater,
        (None, None) => a.name.cmp(&b.name),
    });

    settings
}

/// Parse a single setting from its schema
fn parse_setting(
    name: &str,
    path: &str,
    schema: &RawSchema,
    defs: &HashMap<String, RawSchema>,
    enum_values_map: &EnumValuesMap,
) -> SettingSchema {
    let setting_type = determine_type(schema, defs, enum_values_map);

    // Get description from resolved ref if not present on schema
    let resolved = resolve_ref(schema, defs);
    let description = schema
        .description
        .clone()
        .or_else(|| resolved.description.clone());

    // Check for readOnly flag on schema or resolved ref
    let read_only = schema.read_only || resolved.read_only;

    // Get section from schema or resolved ref
    let section = schema.section.clone().or_else(|| resolved.section.clone());

    // Get order from schema or resolved ref
    let order = schema.order.or(resolved.order);

    // Detect nullability from type array containing "null" or anyOf containing a null variant
    let nullable = resolved
        .schema_type
        .as_ref()
        .map(|t| t.contains_null())
        .unwrap_or(false)
        || schema.any_of.as_ref().is_some_and(|variants| {
            variants.iter().any(|v| {
                v.schema_type
                    .as_ref()
                    .map(|t| t.primary() == Some("null"))
                    .unwrap_or(false)
            })
        });

    SettingSchema {
        path: path.to_string(),
        name: i18n_name(path, name),
        description,
        setting_type,
        default: schema.default.clone(),
        read_only,
        section,
        order,
        nullable,
        enum_from: schema
            .enum_from
            .clone()
            .or_else(|| resolved.enum_from.clone()),
        dual_list_sibling: schema
            .dual_list_sibling
            .clone()
            .or_else(|| resolved.dual_list_sibling.clone()),
        dynamically_extendable_status_bar_elements: schema
            .dynamically_extendable_status_bar_elements
            || resolved.dynamically_extendable_status_bar_elements,
    }
}

/// Determine the SettingType from a schema
fn determine_type(
    schema: &RawSchema,
    defs: &HashMap<String, RawSchema>,
    enum_values_map: &EnumValuesMap,
) -> SettingType {
    // Check for extensible enum values via $ref
    if let Some(ref ref_path) = schema.ref_path {
        if let Some(options) = enum_values_map.get(ref_path) {
            if !options.is_empty() {
                return SettingType::Enum {
                    options: options.clone(),
                };
            }
        }
    }

    // Resolve ref for type checking
    let resolved = resolve_ref(schema, defs);

    // Check for inline enum values (on original schema or resolved ref)
    let enum_values = schema
        .enum_values
        .as_ref()
        .or(resolved.enum_values.as_ref());
    if let Some(values) = enum_values {
        let options: Vec<EnumOption> = values
            .iter()
            .filter_map(|v| {
                if v.is_null() {
                    // null in enum represents "auto-detect" or "default"
                    Some(EnumOption {
                        name: "Auto-detect".to_string(),
                        value: String::new(), // Empty string represents null
                    })
                } else {
                    v.as_str().map(|s| EnumOption {
                        name: s.to_string(),
                        value: s.to_string(),
                    })
                }
            })
            .collect();
        if !options.is_empty() {
            return SettingType::Enum { options };
        }
    }

    // Check type field
    match resolved.schema_type.as_ref().and_then(|t| t.primary()) {
        Some("boolean") => SettingType::Boolean,
        Some("integer") => {
            let minimum = resolved.minimum.as_ref().and_then(|n| n.as_i64());
            let maximum = resolved.maximum.as_ref().and_then(|n| n.as_i64());
            SettingType::Integer { minimum, maximum }
        }
        Some("number") => {
            let minimum = resolved.minimum.as_ref().and_then(|n| n.as_f64());
            let maximum = resolved.maximum.as_ref().and_then(|n| n.as_f64());
            SettingType::Number { minimum, maximum }
        }
        Some("string") => SettingType::String,
        Some("array") => {
            // Check if it's an array of strings, integers, or objects
            if let Some(ref items) = resolved.items {
                let item_resolved = resolve_ref(items, defs);
                // Check for dual-list options on the item schema
                if !item_resolved.dual_list_options.is_empty() {
                    let options = item_resolved
                        .dual_list_options
                        .iter()
                        .map(|entry| EnumOption {
                            name: entry.name.clone().unwrap_or_else(|| entry.value.clone()),
                            value: entry.value.clone(),
                        })
                        .collect();
                    return SettingType::DualList {
                        options,
                        sibling_path: schema
                            .dual_list_sibling
                            .clone()
                            .or_else(|| resolved.dual_list_sibling.clone()),
                    };
                }
                let item_type = item_resolved.schema_type.as_ref().and_then(|t| t.primary());
                if item_type == Some("string") {
                    return SettingType::StringArray;
                }
                if item_type == Some("integer") || item_type == Some("number") {
                    return SettingType::IntegerArray;
                }
                // Check if items reference an object type
                if items.ref_path.is_some() {
                    // Parse the item schema from the referenced definition
                    let item_schema =
                        parse_setting("item", "", item_resolved, defs, enum_values_map);

                    // Only create ObjectArray if the item is an object with properties
                    if matches!(item_schema.setting_type, SettingType::Object { .. }) {
                        // Get display_field from x-display-field in the referenced schema
                        let display_field = item_resolved.display_field.clone();
                        return SettingType::ObjectArray {
                            item_schema: Box::new(item_schema),
                            display_field,
                        };
                    }
                }
            }
            SettingType::Complex
        }
        Some("object") => {
            // Check for additionalProperties (map type)
            if let Some(ref add_props) = resolved.additional_properties {
                match add_props {
                    AdditionalProperties::Schema(schema_box) => {
                        let inner_resolved = resolve_ref(schema_box, defs);
                        let value_schema =
                            parse_setting("value", "", inner_resolved, defs, enum_values_map);

                        // Get display_field from x-display-field in the referenced schema.
                        // If the value schema is an array, also check the array items for display_field.
                        let display_field = inner_resolved.display_field.clone().or_else(|| {
                            inner_resolved.items.as_ref().and_then(|items| {
                                let items_resolved = resolve_ref(items, defs);
                                items_resolved.display_field.clone()
                            })
                        });

                        // Get no_add from the parent schema (resolved)
                        let no_add = resolved.no_add;

                        return SettingType::Map {
                            value_schema: Box::new(value_schema),
                            display_field,
                            no_add,
                        };
                    }
                    AdditionalProperties::Bool(true) => {
                        // additionalProperties: true means any value is allowed
                        return SettingType::Complex;
                    }
                    AdditionalProperties::Bool(false) => {
                        // additionalProperties: false means no additional properties
                        // Fall through to check for fixed properties
                    }
                }
            }
            // Regular object with fixed properties
            if let Some(ref props) = resolved.properties {
                let properties = parse_properties(props, "", defs, enum_values_map);
                return SettingType::Object { properties };
            }
            SettingType::Complex
        }
        _ => SettingType::Complex,
    }
}

/// Resolve a $ref to its definition.
///
/// Also resolves through `anyOf` patterns generated by schemars for `Option<T>`:
///   `anyOf: [{ "$ref": "#/$defs/Foo" }, { "type": "null" }]`
/// In this case, the non-null `$ref` variant is resolved.
fn resolve_ref<'a>(schema: &'a RawSchema, defs: &'a HashMap<String, RawSchema>) -> &'a RawSchema {
    // Direct $ref
    if let Some(ref ref_path) = schema.ref_path {
        if let Some(def_name) = ref_path.strip_prefix("#/$defs/") {
            if let Some(def) = defs.get(def_name) {
                return def;
            }
        }
    }
    // anyOf: find the non-null variant and resolve it
    if let Some(ref variants) = schema.any_of {
        for variant in variants {
            let is_null = variant
                .schema_type
                .as_ref()
                .map(|t| t.primary() == Some("null"))
                .unwrap_or(false);
            if !is_null {
                return resolve_ref(variant, defs);
            }
        }
    }
    schema
}

/// Look up an i18n translation for a settings field, falling back to humanized name.
///
/// Derives a translation key from the schema path, e.g. `/editor/whitespace_show`
/// becomes `settings.field.editor.whitespace_show`. If no translation is found,
/// falls back to `humanize_name()`.
fn i18n_name(path: &str, fallback_name: &str) -> String {
    let key = format!("settings.field{}", path.replace('/', "."));
    let translated = t!(&key);
    if *translated == key {
        humanize_name(fallback_name)
    } else {
        translated.to_string()
    }
}

/// Convert snake_case to Title Case
fn humanize_name(name: &str) -> String {
    name.split('_')
        .map(|word| {
            let mut chars = word.chars();
            match chars.next() {
                None => String::new(),
                Some(first) => first.to_uppercase().chain(chars).collect(),
            }
        })
        .collect::<Vec<_>>()
        .join(" ")
}

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

    const SAMPLE_SCHEMA: &str = r##"
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Config",
  "type": "object",
  "properties": {
    "theme": {
      "description": "Color theme name",
      "type": "string",
      "default": "high-contrast"
    },
    "check_for_updates": {
      "description": "Check for new versions on quit",
      "type": "boolean",
      "default": true
    },
    "editor": {
      "description": "Editor settings",
      "$ref": "#/$defs/EditorConfig"
    }
  },
  "$defs": {
    "EditorConfig": {
      "description": "Editor behavior configuration",
      "type": "object",
      "properties": {
        "tab_size": {
          "description": "Number of spaces per tab",
          "type": "integer",
          "minimum": 1,
          "maximum": 16,
          "default": 4
        },
        "line_numbers": {
          "description": "Show line numbers",
          "type": "boolean",
          "default": true
        }
      }
    }
  }
}
"##;

    #[test]
    fn test_parse_schema() {
        let categories = parse_schema(SAMPLE_SCHEMA).unwrap();

        // Should have General and Editor categories
        assert_eq!(categories.len(), 2);
        assert_eq!(categories[0].name, "General");
        assert_eq!(categories[1].name, "Editor");
    }

    #[test]
    fn test_general_category() {
        let categories = parse_schema(SAMPLE_SCHEMA).unwrap();
        let general = &categories[0];

        // General should have theme and check_for_updates
        assert_eq!(general.settings.len(), 2);

        let theme = general
            .settings
            .iter()
            .find(|s| s.path == "/theme")
            .unwrap();
        assert!(matches!(theme.setting_type, SettingType::String));

        let updates = general
            .settings
            .iter()
            .find(|s| s.path == "/check_for_updates")
            .unwrap();
        assert!(matches!(updates.setting_type, SettingType::Boolean));
    }

    #[test]
    fn test_editor_category() {
        let categories = parse_schema(SAMPLE_SCHEMA).unwrap();
        let editor = &categories[1];

        assert_eq!(editor.path, "/editor");
        assert_eq!(editor.settings.len(), 2);

        let tab_size = editor
            .settings
            .iter()
            .find(|s| s.name == "Tab Size")
            .unwrap();
        if let SettingType::Integer { minimum, maximum } = &tab_size.setting_type {
            assert_eq!(*minimum, Some(1));
            assert_eq!(*maximum, Some(16));
        } else {
            panic!("Expected integer type");
        }
    }

    #[test]
    fn test_any_of_nullable_object() {
        // Tests that anyOf: [{$ref: "..."}, {type: "null"}] resolves to an Object type
        // and is marked as nullable. This is the pattern schemars generates for Option<T>.
        let schema_json = r##"
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Config",
  "type": "object",
  "properties": {
    "fallback": {
      "description": "Fallback language config",
      "anyOf": [
        { "$ref": "#/$defs/LanguageConfig" },
        { "type": "null" }
      ],
      "default": null
    }
  },
  "$defs": {
    "LanguageConfig": {
      "description": "Language-specific configuration",
      "type": "object",
      "properties": {
        "grammar": {
          "description": "Grammar name",
          "type": "string",
          "default": ""
        },
        "comment_prefix": {
          "description": "Comment prefix",
          "type": ["string", "null"],
          "default": null
        },
        "auto_indent": {
          "description": "Enable auto-indent",
          "type": "boolean",
          "default": false
        }
      }
    }
  }
}
"##;
        let categories = parse_schema(schema_json).unwrap();

        // anyOf with $ref should resolve through to LanguageConfig's properties,
        // creating a category (like "Editor") with individual sub-field controls
        let fallback_cat = categories
            .iter()
            .find(|c| c.path == "/fallback")
            .expect("fallback should be a category");
        assert_eq!(fallback_cat.settings.len(), 3);

        // Verify sub-fields are properly typed
        let grammar = fallback_cat
            .settings
            .iter()
            .find(|s| s.name == "Grammar")
            .unwrap();
        assert!(matches!(grammar.setting_type, SettingType::String));

        let auto_indent = fallback_cat
            .settings
            .iter()
            .find(|s| s.name == "Auto Indent")
            .unwrap();
        assert!(matches!(auto_indent.setting_type, SettingType::Boolean));
    }

    #[test]
    fn test_humanize_name() {
        assert_eq!(humanize_name("tab_size"), "Tab Size");
        assert_eq!(humanize_name("line_numbers"), "Line Numbers");
        assert_eq!(humanize_name("check_for_updates"), "Check For Updates");
        assert_eq!(humanize_name("lsp"), "Lsp");
    }

    #[test]
    fn test_enum_from_parsed_from_schema() {
        let schema_json = r##"{
            "type": "object",
            "properties": {
                "default_language": {
                    "type": ["string", "null"],
                    "x-enum-from": "/languages"
                },
                "theme": {
                    "type": "string"
                }
            }
        }"##;

        let categories = parse_schema(schema_json).unwrap();
        let general = &categories[0];
        let default_lang = general
            .settings
            .iter()
            .find(|s| s.name == "Default Language")
            .expect("should have Default Language setting");

        assert_eq!(
            default_lang.enum_from.as_deref(),
            Some("/languages"),
            "enum_from should be parsed from x-enum-from"
        );
        assert!(default_lang.nullable, "should be nullable");

        // theme should not have enum_from
        let theme = general
            .settings
            .iter()
            .find(|s| s.name == "Theme")
            .expect("should have Theme setting");
        assert!(theme.enum_from.is_none());
    }

    #[test]
    fn test_dual_list_parsed_from_schema() {
        let schema_json = r##"{
            "type": "object",
            "properties": {
                "tags": {
                    "type": "array",
                    "items": {
                        "type": "string",
                        "x-dual-list-options": [
                            {"value": "red", "name": "Red"},
                            {"value": "green", "name": "Green"},
                            {"value": "blue", "name": "Blue"}
                        ]
                    },
                    "x-dual-list-sibling": "/other_tags"
                }
            }
        }"##;
        let categories = parse_schema(schema_json).unwrap();
        let general = &categories[0];
        let tags = general
            .settings
            .iter()
            .find(|s| s.path == "/tags")
            .expect("tags setting");

        match &tags.setting_type {
            SettingType::DualList {
                options,
                sibling_path,
            } => {
                assert_eq!(options.len(), 3);
                assert_eq!(options[0].value, "red");
                assert_eq!(options[0].name, "Red");
                assert_eq!(sibling_path.as_deref(), Some("/other_tags"));
            }
            other => panic!("expected DualList, got {:?}", other),
        }
        assert_eq!(tags.dual_list_sibling.as_deref(), Some("/other_tags"));
    }
}