vig 0.5.0

Git TUI side-by-side diff viewer
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
use crate::core::config::constraint::parse_constraint;
use crate::core::config::keymap_builder::{build_keymap, KeymapEntry};
use crate::core::config::merge::merge_user_config;
use crate::core::keymap::Keymap;
use crate::core::layout::{LayoutNode, PageLayoutConfig, SlotRule, SplitDirection};
use anyhow::{anyhow, Context, Result};
use kdl::{KdlDocument, KdlNode};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::str::FromStr;

static DEFAULT_KDL: &str = include_str!("../../../assets/default.kdl");

// ── Public output types ───────────────────────────────────────────────────────

/// Parsed config for a single page.
pub struct LoadedPageConfig {
    /// Page name (e.g. `"git"`, `"github"`).
    pub name: String,
    pub layout: PageLayoutConfig,
    /// Pane keymaps keyed by pane name (e.g. `"file_tree"`, `"view"`, …).
    pub pane_keys: HashMap<String, Vec<KeymapEntry>>,
    /// Pane names and their auto-assigned IDs (declaration order, layout panes only).
    pub pane_ids: Vec<(String, usize)>,
    /// select→detail instance bindings declared in the KDL.
    pub bindings: Vec<(String, String)>,
    /// Path of the user config file this page came from, if any (for error messages).
    pub source: Option<PathBuf>,
}

impl LoadedPageConfig {
    fn describe_source(&self) -> String {
        describe_source(self.source.as_deref())
    }

    /// Build the typed keymap for `pane` from this page's `keys { }` block.
    pub fn keymap<A>(&self, pane: &str) -> Result<Keymap<A>>
    where
        A: Clone + FromStr<Err = String>,
    {
        let entries = self.pane_keys.get(pane).ok_or_else(|| {
            anyhow!(
                "invalid {}: page {:?} missing pane {pane:?} keys block",
                self.describe_source(),
                self.name
            )
        })?;
        build_keymap::<A>(entries).map_err(|e| {
            anyhow!(
                "invalid {}: page {:?} pane {pane:?}: {e}",
                self.describe_source(),
                self.name
            )
        })
    }

    /// Resolve a pane name to its numeric ID.
    pub fn resolve_id(&self, name: &str) -> Option<usize> {
        self.pane_ids
            .iter()
            .find(|(n, _)| n == name)
            .map(|(_, id)| *id)
    }

    /// Resolve a pane name to its numeric ID, panicking if not found.
    /// Use this for pane names known statically by the page (e.g. `"file_tree"`).
    pub fn resolve_id_expect(&self, name: &str) -> usize {
        self.resolve_id(name)
            .unwrap_or_else(|| panic!("pane {name:?} not found in {:?} page config", self.name))
    }

    /// Build a `select_id → detail_id` map from the KDL `bind` declarations.
    /// Panics if any pane name in the bindings does not exist in this config.
    pub fn resolve_select_bindings(&self) -> HashMap<usize, usize> {
        self.bindings
            .iter()
            .map(|(sel, det)| {
                let s = self
                    .resolve_id(sel)
                    .unwrap_or_else(|| panic!("bind select={sel:?}: pane not found in config"));
                let d = self
                    .resolve_id(det)
                    .unwrap_or_else(|| panic!("bind detail={det:?}: pane not found in config"));
                (s, d)
            })
            .collect()
    }
}

// ── App-block parser ──────────────────────────────────────────────────────────

fn parse_app_block(doc: &KdlDocument) -> Result<Vec<(String, String)>> {
    let mut entries = Vec::new();
    for node in doc.nodes() {
        let key_str = node.name().value().to_string();
        let action_str = node
            .get(0usize)
            .and_then(|v| v.as_string())
            .ok_or_else(|| anyhow!("app entry {key_str:?} missing action argument"))?
            .to_string();
        entries.push((key_str, action_str));
    }
    Ok(entries)
}

fn describe_source(source: Option<&Path>) -> String {
    match source {
        Some(p) => format!("config file {}", p.display()),
        None => "built-in config".to_string(),
    }
}

// ── Config ────────────────────────────────────────────────────────────────────

/// The effective configuration: the built-in defaults with an optional user
/// document merged on top (see `merge.rs` for the rules).
pub struct Config {
    /// Pristine built-in defaults. Pane IDs are always derived from this
    /// document so a user layout can never renumber panes.
    default_doc: KdlDocument,
    /// Defaults with the user document merged in.
    doc: KdlDocument,
    /// Path of the user config file, if any (for error messages).
    source: Option<PathBuf>,
}

impl Config {
    /// The raw text of the embedded default config.
    pub fn default_text() -> &'static str {
        DEFAULT_KDL
    }

    fn parse_default() -> KdlDocument {
        DEFAULT_KDL.parse().expect("default.kdl is always valid")
    }

    /// Built-in defaults only.
    pub fn builtin() -> Self {
        let default_doc = Self::parse_default();
        Self {
            doc: default_doc.clone(),
            default_doc,
            source: None,
        }
    }

    /// Defaults with `user` merged on top. Structural errors (unknown pages,
    /// panes, incomplete layouts, …) are reported immediately.
    pub fn with_user(user: &KdlDocument, source: PathBuf) -> Result<Self> {
        let default_doc = Self::parse_default();
        let mut doc = default_doc.clone();
        merge_user_config(&mut doc, user)
            .with_context(|| format!("invalid config file {}", source.display()))?;
        let cfg = Self {
            default_doc,
            doc,
            source: Some(source),
        };
        cfg.git_page()?;
        cfg.github_page()?;
        cfg.files_page()?;
        cfg.app_entries()?;
        cfg.theme()?;
        cfg.icons()?;
        cfg.image_preview()?;
        Ok(cfg)
    }

    /// How the Files view previews images (`image-preview "auto"` / `"halfblocks"` / `"none"`).
    pub fn image_preview(&self) -> Result<crate::files::domain::image::ImagePreviewMode> {
        use crate::files::domain::image::{ImagePreviewMode, IMAGE_PREVIEW_MODES};
        let mode = self
            .doc
            .nodes()
            .iter()
            .find(|n| n.name().value() == "image-preview")
            .map(|n| {
                n.get(0usize)
                    .and_then(|v| v.as_string())
                    .map(str::to_string)
                    .ok_or_else(|| anyhow!("image-preview block missing mode argument"))
            })
            .transpose()
            .with_context(|| format!("invalid {}", self.describe()))?
            .unwrap_or_else(|| IMAGE_PREVIEW_MODES[0].to_string());
        ImagePreviewMode::parse(&mode).ok_or_else(|| {
            anyhow!(
                "invalid {}: unknown image-preview mode {mode:?}; expected one of: {}",
                self.describe(),
                IMAGE_PREVIEW_MODES.join(", ")
            )
        })
    }

    /// Whether the Files view shows Nerd Font icons (`icons "nerd"` / `"none"`).
    pub fn icons(&self) -> Result<bool> {
        let modes = crate::files::domain::icons::ICON_MODES;
        let mode = self
            .doc
            .nodes()
            .iter()
            .find(|n| n.name().value() == "icons")
            .map(|n| {
                n.get(0usize)
                    .and_then(|v| v.as_string())
                    .map(str::to_string)
                    .ok_or_else(|| anyhow!("icons block missing mode argument"))
            })
            .transpose()
            .with_context(|| format!("invalid {}", self.describe()))?
            .unwrap_or_else(|| modes[0].to_string());
        if !modes.contains(&mode.as_str()) {
            return Err(anyhow!(
                "invalid {}: unknown icons mode {mode:?}; expected one of: {}",
                self.describe(),
                modes.join(", ")
            ));
        }
        Ok(mode == "nerd")
    }

    /// Syntax highlighting theme name, validated against the bundled themes.
    pub fn theme(&self) -> Result<String> {
        let name = self
            .doc
            .nodes()
            .iter()
            .find(|n| n.name().value() == "theme")
            .map(|n| {
                n.get(0usize)
                    .and_then(|v| v.as_string())
                    .map(str::to_string)
                    .ok_or_else(|| anyhow!("theme block missing name argument"))
            })
            .transpose()
            .with_context(|| format!("invalid {}", self.describe()))?
            .unwrap_or_else(|| crate::core::syntax::DEFAULT_THEME.to_string());
        let available = crate::core::syntax::theme_names();
        if !available.contains(&name) {
            return Err(anyhow!(
                "invalid {}: unknown theme {name:?}; available: {}",
                self.describe(),
                available.join(", ")
            ));
        }
        Ok(name)
    }

    /// Human-readable origin for error messages.
    pub fn describe(&self) -> String {
        describe_source(self.source.as_deref())
    }

    pub fn git_page(&self) -> Result<LoadedPageConfig> {
        self.page("git")
    }

    pub fn github_page(&self) -> Result<LoadedPageConfig> {
        self.page("github")
    }

    pub fn files_page(&self) -> Result<LoadedPageConfig> {
        self.page("files")
    }

    fn page(&self, name: &str) -> Result<LoadedPageConfig> {
        load_page_from_doc(&self.doc, &self.default_doc, name)
            .map(|mut p| {
                p.source = self.source.clone();
                p
            })
            .with_context(|| format!("invalid {}", self.describe()))
    }

    /// App-level `(key_str, action_str)` pairs.
    pub fn app_entries(&self) -> Result<Vec<(String, String)>> {
        self.doc
            .nodes()
            .iter()
            .find(|n| n.name().value() == "app")
            .and_then(|n| n.children())
            .map(parse_app_block)
            .unwrap_or_else(|| Ok(Vec::new()))
            .with_context(|| format!("invalid {}: app block", self.describe()))
    }
}

// ── Built-in convenience loaders (tests) ──────────────────────────────────────

/// Parse the git page from the embedded default config.
#[cfg(test)]
pub fn load_git_page_config() -> Result<LoadedPageConfig> {
    Config::builtin().git_page()
}

/// Parse the github page from the embedded default config.
#[cfg(test)]
pub fn load_github_page_config() -> Result<LoadedPageConfig> {
    Config::builtin().github_page()
}

/// Parse app-level key entries from the embedded default config.
#[cfg(test)]
pub fn load_app_entries() -> Vec<(String, String)> {
    Config::builtin()
        .app_entries()
        .expect("default.kdl app block is always valid")
}

fn page_children<'a>(doc: &'a KdlDocument, page_name: &str) -> Result<&'a KdlDocument> {
    doc.nodes()
        .iter()
        .filter(|n| n.name().value() == "page")
        .find(|n| n.get(0usize).and_then(|v| v.as_string()) == Some(page_name))
        .ok_or_else(|| anyhow!("page {page_name:?} not found"))?
        .children()
        .ok_or_else(|| anyhow!("page {page_name:?} has no children block"))
}

/// The `layout { }` block's children, and its single root element.
fn layout_block<'a>(
    children: &'a KdlDocument,
    page_name: &str,
) -> Result<(&'a KdlDocument, &'a KdlNode)> {
    let layout_doc = children
        .nodes()
        .iter()
        .find(|n| n.name().value() == "layout")
        .and_then(|n| n.children())
        .ok_or_else(|| anyhow!("page {page_name:?} missing layout block"))?;
    let root = layout_doc
        .nodes()
        .first()
        .ok_or_else(|| anyhow!("page {page_name:?} layout is empty"))?;
    Ok((layout_doc, root))
}

fn load_page_from_doc(
    doc: &KdlDocument,
    default_doc: &KdlDocument,
    page_name: &str,
) -> Result<LoadedPageConfig> {
    let children = page_children(doc, page_name)?;
    let (layout_doc, layout_root) = layout_block(children, page_name)?;

    // Pane IDs come from the *default* layout's pane set, in `pane` block
    // declaration order, so a user layout can rearrange panes without
    // renumbering them.
    let default_children = page_children(default_doc, page_name)?;
    let (_, default_layout_root) = layout_block(default_children, page_name)?;
    let default_layout_names = collect_layout_pane_names(default_layout_root);
    let pane_ids = build_pane_ids(children, &default_layout_names);

    // Panes are compile-time fixed, so a layout may rearrange them but not
    // drop them: every pane of the page must be placed somewhere.
    let placed = collect_layout_pane_names(layout_root);
    let missing: Vec<&str> = default_layout_names
        .iter()
        .filter(|n| !placed.contains(n))
        .map(String::as_str)
        .collect();
    if !missing.is_empty() {
        return Err(anyhow!(
            "page {page_name:?}: layout must place every pane of the page; missing: {missing:?}"
        ));
    }

    // Build name_map from pane_ids for layout / tab / slot parsing.
    let name_map: HashMap<&str, usize> = pane_ids
        .iter()
        .map(|(name, id)| (name.as_str(), *id))
        .collect();

    // Tab panes
    let tab_panes = parse_tabs(children, &name_map, page_name)?;

    // Parse layout tree + slot rules together
    let mut slot_id_counter: usize = 0;
    let mut slot_name_to_id: HashMap<String, usize> = HashMap::new();
    let mut slot_rules: Vec<SlotRule> = Vec::new();

    let root = parse_layout_root(
        layout_doc,
        &name_map,
        &mut slot_id_counter,
        &mut slot_name_to_id,
        &mut slot_rules,
        page_name,
    )?;

    // Pane keymaps
    let pane_keys = parse_all_pane_keys(children, page_name)?;

    // select→detail bindings
    let bindings = parse_bindings(children, &name_map, page_name)?;

    Ok(LoadedPageConfig {
        name: page_name.to_string(),
        layout: PageLayoutConfig {
            tree: root,
            tab_panes,
            slot_rules,
        },
        pane_keys,
        pane_ids,
        bindings,
        source: None,
    })
}

// ── Pane ID builder ───────────────────────────────────────────────────────────

/// Collect all pane names referenced in a layout node
/// (from `place`, `slot then=`/`default=`, and `triggers` children),
/// deduplicated while preserving first-occurrence order.
fn collect_layout_pane_names(node: &KdlNode) -> Vec<String> {
    let mut names = Vec::new();
    collect_layout_pane_names_into(node, &mut names);
    let mut seen = std::collections::HashSet::new();
    names.retain(|n| seen.insert(n.clone()));
    names
}

fn collect_layout_pane_names_into(node: &KdlNode, names: &mut Vec<String>) {
    match node.name().value() {
        "place" => {
            if let Some(name) = node.get(0usize).and_then(|v| v.as_string()) {
                names.push(name.to_string());
            }
        }
        "slot" => {
            if let Some(name) = node.get("then").and_then(|v| v.as_string()) {
                names.push(name.to_string());
            }
            if let Some(name) = node.get("default").and_then(|v| v.as_string()) {
                names.push(name.to_string());
            }
            if let Some(children) = node.children() {
                for child in children.nodes() {
                    if child.name().value() == "triggers" {
                        for entry in child.entries().iter().filter(|e| e.name().is_none()) {
                            if let Some(name) = entry.value().as_string() {
                                names.push(name.to_string());
                            }
                        }
                    }
                }
            }
        }
        "split" => {
            if let Some(children) = node.children() {
                for child in children.nodes() {
                    collect_layout_pane_names_into(child, names);
                }
            }
        }
        _ => {}
    }
}

/// Assign sequential IDs to panes that appear in the layout,
/// in the order they appear as `pane "<name>"` blocks in the page.
fn build_pane_ids(page_children: &KdlDocument, layout_names: &[String]) -> Vec<(String, usize)> {
    let mut result = Vec::new();
    let mut id = 0usize;
    for node in page_children.nodes() {
        if node.name().value() == "pane" {
            if let Some(name) = node.get(0usize).and_then(|v| v.as_string()) {
                if layout_names.iter().any(|n| n == name) {
                    result.push((name.to_string(), id));
                    id += 1;
                }
            }
        }
    }
    result
}

// ── Bindings parser ───────────────────────────────────────────────────────────

fn parse_bindings(
    page_children: &KdlDocument,
    name_map: &HashMap<&str, usize>,
    page: &str,
) -> Result<Vec<(String, String)>> {
    page_children
        .nodes()
        .iter()
        .filter(|n| n.name().value() == "bind")
        .map(|n| {
            let select = n
                .get("select")
                .and_then(|v| v.as_string())
                .ok_or_else(|| anyhow!("bind in page {page:?} missing select= attribute"))?
                .to_string();
            let detail = n
                .get("detail")
                .and_then(|v| v.as_string())
                .ok_or_else(|| anyhow!("bind in page {page:?} missing detail= attribute"))?
                .to_string();
            if !name_map.contains_key(select.as_str()) {
                return Err(anyhow!(
                    "bind select={select:?} in page {page:?}: pane not found"
                ));
            }
            if !name_map.contains_key(detail.as_str()) {
                return Err(anyhow!(
                    "bind detail={detail:?} in page {page:?}: pane not found"
                ));
            }
            Ok((select, detail))
        })
        .collect()
}

// ── Tab parser ─────────────────────────────────────────────────────────────────

fn parse_tabs(
    doc: &KdlDocument,
    name_map: &HashMap<&str, usize>,
    page: &str,
) -> Result<Vec<usize>> {
    let tabs_node = doc
        .nodes()
        .iter()
        .find(|n| n.name().value() == "tabs")
        .ok_or_else(|| anyhow!("page {page:?} missing tabs node"))?;

    tabs_node
        .entries()
        .iter()
        .filter(|e| e.name().is_none()) // positional args only
        .map(|e| {
            let pane_name = e
                .value()
                .as_string()
                .ok_or_else(|| anyhow!("tabs entry is not a string"))?;
            name_map
                .get(pane_name)
                .copied()
                .ok_or_else(|| anyhow!("unknown pane in tabs: {pane_name:?}"))
        })
        .collect()
}

// ── Layout parser ─────────────────────────────────────────────────────────────

/// Parse the root layout node (the outermost element inside `layout { }`).
fn parse_layout_root(
    doc: &KdlDocument,
    name_map: &HashMap<&str, usize>,
    slot_id_counter: &mut usize,
    slot_name_to_id: &mut HashMap<String, usize>,
    slot_rules: &mut Vec<SlotRule>,
    page: &str,
) -> Result<LayoutNode> {
    let root_node = doc
        .nodes()
        .first()
        .ok_or_else(|| anyhow!("page {page:?} layout is empty"))?;

    parse_layout_element(
        root_node,
        name_map,
        slot_id_counter,
        slot_name_to_id,
        slot_rules,
    )
}

fn parse_layout_element(
    node: &KdlNode,
    name_map: &HashMap<&str, usize>,
    slot_id_counter: &mut usize,
    slot_name_to_id: &mut HashMap<String, usize>,
    slot_rules: &mut Vec<SlotRule>,
) -> Result<LayoutNode> {
    match node.name().value() {
        "split" => parse_split(node, name_map, slot_id_counter, slot_name_to_id, slot_rules),
        "place" => parse_place(node, name_map),
        "slot" => parse_slot(node, name_map, slot_id_counter, slot_name_to_id, slot_rules),
        other => Err(anyhow!("unknown layout element: {other:?}")),
    }
}

fn parse_split(
    node: &KdlNode,
    name_map: &HashMap<&str, usize>,
    slot_id_counter: &mut usize,
    slot_name_to_id: &mut HashMap<String, usize>,
    slot_rules: &mut Vec<SlotRule>,
) -> Result<LayoutNode> {
    let dir_str = node
        .get("direction")
        .and_then(|v| v.as_string())
        .ok_or_else(|| anyhow!("split missing direction="))?;
    let direction = match dir_str {
        "horizontal" => SplitDirection::Horizontal,
        "vertical" => SplitDirection::Vertical,
        d => return Err(anyhow!("unknown split direction: {d:?}")),
    };

    let child_doc = node
        .children()
        .ok_or_else(|| anyhow!("split has no children block"))?;

    let mut children = Vec::new();
    for child_node in child_doc.nodes() {
        let size_str = child_node
            .get("size")
            .and_then(|v| v.as_string())
            .unwrap_or("min:0");
        let constraint =
            parse_constraint(size_str).map_err(|e| anyhow!("split child size error: {e}"))?;
        let child_layout = parse_layout_element(
            child_node,
            name_map,
            slot_id_counter,
            slot_name_to_id,
            slot_rules,
        )?;
        children.push((constraint, child_layout));
    }

    Ok(LayoutNode::Split {
        direction,
        children,
    })
}

fn parse_place(node: &KdlNode, name_map: &HashMap<&str, usize>) -> Result<LayoutNode> {
    let pane_name = node
        .get(0usize)
        .and_then(|v| v.as_string())
        .ok_or_else(|| anyhow!("place missing pane name argument"))?;
    let pane_id = *name_map
        .get(pane_name)
        .ok_or_else(|| anyhow!("unknown pane in place: {pane_name:?}"))?;
    Ok(LayoutNode::Pane(pane_id))
}

fn parse_slot(
    node: &KdlNode,
    name_map: &HashMap<&str, usize>,
    slot_id_counter: &mut usize,
    slot_name_to_id: &mut HashMap<String, usize>,
    slot_rules: &mut Vec<SlotRule>,
) -> Result<LayoutNode> {
    let slot_name = node
        .get(0usize)
        .and_then(|v| v.as_string())
        .ok_or_else(|| anyhow!("slot missing name argument"))?
        .to_string();

    let slot_id = *slot_name_to_id.entry(slot_name.clone()).or_insert_with(|| {
        let id = *slot_id_counter;
        *slot_id_counter += 1;
        id
    });

    let then_name = node
        .get("then")
        .and_then(|v| v.as_string())
        .ok_or_else(|| anyhow!("slot {slot_name:?} missing then="))?;
    let default_name = node
        .get("default")
        .and_then(|v| v.as_string())
        .ok_or_else(|| anyhow!("slot {slot_name:?} missing default="))?;

    let then_pane = *name_map
        .get(then_name)
        .ok_or_else(|| anyhow!("unknown pane in slot then=: {then_name:?}"))?;
    let default_pane = *name_map
        .get(default_name)
        .ok_or_else(|| anyhow!("unknown pane in slot default=: {default_name:?}"))?;

    // Parse triggers from child block
    let trigger_panes =
        node.children()
            .map(|child_doc| {
                child_doc
                    .nodes()
                    .iter()
                    .find(|n| n.name().value() == "triggers")
                    .map(|triggers_node| {
                        triggers_node
                            .entries()
                            .iter()
                            .filter(|e| e.name().is_none())
                            .map(|e| {
                                let pane_name = e
                                    .value()
                                    .as_string()
                                    .ok_or_else(|| anyhow!("trigger entry is not a string"))?;
                                name_map.get(pane_name).copied().ok_or_else(|| {
                                    anyhow!("unknown pane in triggers: {pane_name:?}")
                                })
                            })
                            .collect::<Result<Vec<_>>>()
                    })
                    .transpose()
            })
            .transpose()?
            .flatten()
            .unwrap_or_default();

    slot_rules.push(SlotRule {
        slot_id,
        trigger_panes,
        then_pane,
        default_pane,
    });

    Ok(LayoutNode::Slot(slot_id))
}

// ── Pane keys parser ──────────────────────────────────────────────────────────

fn parse_all_pane_keys(
    page_children: &KdlDocument,
    page: &str,
) -> Result<HashMap<String, Vec<KeymapEntry>>> {
    let mut result = HashMap::new();
    for node in page_children.nodes() {
        if node.name().value() == "pane" {
            let pane_name = node
                .get(0usize)
                .and_then(|v| v.as_string())
                .ok_or_else(|| anyhow!("pane node in page {page:?} missing name argument"))?
                .to_string();
            let keys = parse_pane_keys(node, &pane_name)?;
            result.insert(pane_name, keys);
        }
    }
    Ok(result)
}

fn parse_pane_keys(pane_node: &KdlNode, pane_name: &str) -> Result<Vec<KeymapEntry>> {
    let Some(pane_children) = pane_node.children() else {
        return Ok(Vec::new());
    };

    // A pane may have no bindings at all (display-only panes such as the
    // Files page's parent directory column).
    let Some(keys_children) = pane_children
        .nodes()
        .iter()
        .find(|n| n.name().value() == "keys")
        .and_then(|n| n.children())
    else {
        return Ok(Vec::new());
    };

    let mut entries = Vec::new();
    for node in keys_children.nodes() {
        match node.name().value() {
            "preset" => {
                let preset_name = node
                    .get(0usize)
                    .and_then(|v| v.as_string())
                    .ok_or_else(|| anyhow!("preset in pane {pane_name:?} missing name argument"))?
                    .to_string();
                entries.push(KeymapEntry::Preset(preset_name));
            }
            key_str => {
                let action_str = node
                    .get(0usize)
                    .and_then(|v| v.as_string())
                    .ok_or_else(|| {
                        anyhow!("key {key_str:?} in pane {pane_name:?} missing action argument")
                    })?
                    .to_string();
                entries.push(KeymapEntry::Binding {
                    key: key_str.to_string(),
                    action: action_str,
                });
            }
        }
    }
    Ok(entries)
}

// ── Tests ─────────────────────────────────────────────────────────────────────

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

    #[test]
    fn load_git_page() {
        let cfg = load_git_page_config().unwrap();
        assert_eq!(cfg.name, "git");
    }

    #[test]
    fn load_github_page() {
        let cfg = load_github_page_config().unwrap();
        assert_eq!(cfg.name, "github");
    }

    #[test]
    fn app_entries_populated() {
        let entries = load_app_entries();
        assert!(!entries.is_empty());
        let has_quit = entries.iter().any(|(_, a)| a == "Quit");
        assert!(has_quit, "app entries should contain Quit");
    }

    #[test]
    fn git_pane_keys_present() {
        let cfg = load_git_page_config().unwrap();
        for name in &[
            "view",
            "file_tree",
            "branch_list",
            "git_log",
            "reflog",
            "diff_view",
        ] {
            assert!(
                cfg.pane_keys.contains_key(*name),
                "missing pane keys for {name}"
            );
        }
    }

    #[test]
    fn github_pane_keys_present() {
        let cfg = load_github_page_config().unwrap();
        for name in &["view", "issue_list", "pr_list", "issue_detail", "pr_detail"] {
            assert!(
                cfg.pane_keys.contains_key(*name),
                "missing pane keys for {name}"
            );
        }
    }

    #[test]
    fn git_pane_ids_correct() {
        let cfg = load_git_page_config().unwrap();
        // IDs assigned in pane block declaration order (excluding "view")
        let expected = [
            ("file_tree", 0usize),
            ("branch_list", 1),
            ("git_log", 2),
            ("reflog", 3),
            ("diff_view", 4),
        ];
        for (name, id) in &expected {
            assert_eq!(
                cfg.resolve_id(name),
                Some(*id),
                "pane {name:?} should have id {id}"
            );
        }
    }

    #[test]
    fn files_page_ids_keys_and_bindings() {
        let cfg = Config::builtin().files_page().unwrap();
        assert_eq!(cfg.name, "files");
        assert_eq!(cfg.resolve_id("parent_dir"), Some(0));
        assert_eq!(cfg.resolve_id("dir_list"), Some(1));
        assert_eq!(cfg.resolve_id("preview"), Some(2));
        assert_eq!(cfg.layout.tab_panes, vec![1, 2]);
        assert_eq!(
            cfg.bindings,
            vec![("dir_list".to_string(), "preview".to_string())]
        );
        for name in ["view", "parent_dir", "dir_list", "preview"] {
            assert!(
                cfg.pane_keys.contains_key(name),
                "missing pane keys for {name}"
            );
        }
        // Display-only pane: a `pane` block without keys is allowed and empty.
        assert!(cfg.pane_keys["parent_dir"].is_empty());
        // App block switches to it with "3".
        let entries = Config::builtin().app_entries().unwrap();
        assert!(entries.contains(&("3".to_string(), "page:files".to_string())));
    }

    #[test]
    fn github_pane_ids_correct() {
        let cfg = load_github_page_config().unwrap();
        let expected = [
            ("issue_list", 0usize),
            ("pr_list", 1),
            ("issue_detail", 2),
            ("pr_detail", 3),
        ];
        for (name, id) in &expected {
            assert_eq!(
                cfg.resolve_id(name),
                Some(*id),
                "pane {name:?} should have id {id}"
            );
        }
    }

    #[test]
    fn git_bindings_correct() {
        let cfg = load_git_page_config().unwrap();
        assert!(
            cfg.bindings
                .contains(&("file_tree".to_string(), "diff_view".to_string())),
            "missing bind file_tree→diff_view"
        );
        assert!(
            cfg.bindings
                .contains(&("branch_list".to_string(), "git_log".to_string())),
            "missing bind branch_list→git_log"
        );
    }

    #[test]
    fn github_bindings_correct() {
        let cfg = load_github_page_config().unwrap();
        assert!(
            cfg.bindings
                .contains(&("issue_list".to_string(), "issue_detail".to_string())),
            "missing bind issue_list→issue_detail"
        );
        assert!(
            cfg.bindings
                .contains(&("pr_list".to_string(), "pr_detail".to_string())),
            "missing bind pr_list→pr_detail"
        );
    }

    // ── User config overlay ───────────────────────────────────────────────

    fn user(kdl: &str) -> Result<Config> {
        let doc: KdlDocument = kdl.parse().unwrap();
        Config::with_user(&doc, PathBuf::from("/u/config.kdl"))
    }

    fn key(s: &str) -> crossterm::event::KeyEvent {
        let ki: crate::core::keymap::KeyInput = s.parse().unwrap();
        crossterm::event::KeyEvent::new(ki.code, ki.modifiers)
    }

    #[test]
    fn user_overrides_and_unbinds_pane_keys() {
        use crate::git::panes::file_tree::FileTreeAction;
        let cfg = user(
            r#"page "git" { pane "file_tree" { keys { "Space" "ExpandOrOpen"; "i" "None"; "x" "ToggleDir" } } }"#,
        )
        .unwrap();
        let km = cfg
            .git_page()
            .unwrap()
            .keymap::<FileTreeAction>("file_tree")
            .unwrap();
        assert!(matches!(
            km.lookup(key("Space")),
            Some(FileTreeAction::ExpandOrOpen)
        ));
        assert!(km.lookup(key("i")).is_none(), "i should be unbound");
        assert!(matches!(
            km.lookup(key("x")),
            Some(FileTreeAction::ToggleDir)
        ));
        // Preset keys and untouched bindings survive.
        assert!(km.lookup(key("j")).is_some());
        assert!(matches!(
            km.lookup(key("Enter")),
            Some(FileTreeAction::ExpandOrOpen)
        ));
        // Other panes are untouched.
        let default = Config::builtin().git_page().unwrap();
        let merged = cfg.git_page().unwrap();
        assert_eq!(
            format!("{:?}", default.pane_keys["branch_list"]),
            format!("{:?}", merged.pane_keys["branch_list"])
        );
    }

    #[test]
    fn user_app_keys_merge() {
        let cfg = user(r#"app { "3" "page:github"; "1" "None" }"#).unwrap();
        let entries = cfg.app_entries().unwrap();
        let km = crate::core::keymap::build_app_keymap(&entries, &["git", "github"]).unwrap();
        assert!(km.lookup(key("1")).is_none());
        assert!(matches!(
            km.lookup(key("2")),
            Some(crate::core::keymap::AppAction::SwitchPage(1))
        ));
        assert!(matches!(
            km.lookup(key("3")),
            Some(crate::core::keymap::AppAction::SwitchPage(1))
        ));
        assert!(matches!(
            km.lookup(key("Ctrl+c")),
            Some(crate::core::keymap::AppAction::Quit)
        ));
    }

    #[test]
    fn user_layout_replaces_without_renumbering_panes() {
        let cfg = user(
            r#"page "git" {
                layout {
                    split direction="horizontal" {
                        split direction="vertical" size="30%" {
                            place "file_tree"
                            place "branch_list"
                            place "reflog"
                        }
                        slot "main" then="git_log" default="diff_view" {
                            triggers "branch_list" "reflog" "git_log"
                        }
                    }
                }
                tabs "branch_list" "file_tree" "reflog"
            }"#,
        )
        .unwrap();
        let page = cfg.git_page().unwrap();
        let default = Config::builtin().git_page().unwrap();
        assert_eq!(page.pane_ids, default.pane_ids, "IDs must not change");
        assert_eq!(page.bindings, default.bindings, "bind left untouched");
        assert!(matches!(
            page.layout.tree,
            LayoutNode::Split {
                direction: SplitDirection::Horizontal,
                ..
            }
        ));
        assert_eq!(page.layout.tab_panes, vec![1, 0, 3]);
    }

    #[test]
    fn user_layout_must_place_every_pane() {
        let err = user(
            r#"page "git" {
                layout { split direction="vertical" { place "file_tree"; place "diff_view" } }
            }"#,
        )
        .err()
        .expect("expected an error");
        let msg = format!("{err:#}");
        assert!(msg.contains("/u/config.kdl"), "{msg}");
        assert!(msg.contains("missing"), "{msg}");
        for pane in ["branch_list", "git_log", "reflog"] {
            assert!(msg.contains(pane), "{msg} should mention {pane}");
        }
    }

    #[test]
    fn user_structural_errors_mention_file() {
        for bad in [
            r#"page "nope" { }"#,
            r#"page "git" { pane "nope" { keys { } } }"#,
            r#"page "git" { layout { place "nope" } }"#,
            r#"page "git" { tabs "nope" }"#,
            r#"page "git" { bind select="file_tree" detail="nope" }"#,
            r#"colors { }"#,
        ] {
            let msg = format!("{:#}", user(bad).err().expect("expected an error"));
            assert!(msg.contains("/u/config.kdl"), "{bad}: {msg}");
        }
    }

    #[test]
    fn user_invalid_action_mentions_file_page_and_pane() {
        use crate::git::panes::file_tree::FileTreeAction;
        let cfg =
            user(r#"page "git" { pane "file_tree" { keys { "x" "NoSuchAction" } } }"#).unwrap();
        let msg = format!(
            "{:#}",
            cfg.git_page()
                .unwrap()
                .keymap::<FileTreeAction>("file_tree")
                .err()
                .expect("expected an error")
        );
        assert!(msg.contains("/u/config.kdl"), "{msg}");
        assert!(msg.contains("\"git\""), "{msg}");
        assert!(msg.contains("\"file_tree\""), "{msg}");
        assert!(msg.contains("NoSuchAction"), "{msg}");
    }

    #[test]
    fn theme_default_override_and_validation() {
        assert_eq!(
            Config::builtin().theme().unwrap(),
            crate::core::syntax::DEFAULT_THEME
        );
        let cfg = user(r#"theme "Solarized (dark)""#).unwrap();
        assert_eq!(cfg.theme().unwrap(), "Solarized (dark)");

        let msg = format!(
            "{:#}",
            user(r#"theme "nope""#).err().expect("expected an error")
        );
        assert!(msg.contains("/u/config.kdl"), "{msg}");
        assert!(msg.contains("unknown theme \"nope\""), "{msg}");
        assert!(msg.contains("base16-eighties.dark"), "{msg}");

        let msg = format!("{:#}", user(r#"theme"#).err().expect("expected an error"));
        assert!(msg.contains("missing name"), "{msg}");
    }

    #[test]
    fn icons_default_override_and_validation() {
        assert!(Config::builtin().icons().unwrap());
        assert!(!user(r#"icons "none""#).unwrap().icons().unwrap());
        let msg = format!(
            "{:#}",
            user(r#"icons "emoji""#).err().expect("expected an error")
        );
        assert!(msg.contains("/u/config.kdl"), "{msg}");
        assert!(msg.contains("unknown icons mode \"emoji\""), "{msg}");
        assert!(msg.contains("nerd, none"), "{msg}");
    }

    #[test]
    fn image_preview_default_override_and_validation() {
        use crate::files::domain::image::ImagePreviewMode;
        assert_eq!(
            Config::builtin().image_preview().unwrap(),
            ImagePreviewMode::Auto
        );
        assert_eq!(
            user(r#"image-preview "none""#)
                .unwrap()
                .image_preview()
                .unwrap(),
            ImagePreviewMode::None
        );
        let msg = format!(
            "{:#}",
            user(r#"image-preview "sixel""#)
                .err()
                .expect("expected an error")
        );
        assert!(
            msg.contains("unknown image-preview mode \"sixel\""),
            "{msg}"
        );
        assert!(msg.contains("auto, halfblocks, none"), "{msg}");
    }

    #[test]
    fn empty_user_config_equals_builtin() {
        let cfg = user("").unwrap();
        let a = Config::builtin().git_page().unwrap();
        let b = cfg.git_page().unwrap();
        assert_eq!(a.pane_ids, b.pane_ids);
        assert_eq!(a.bindings, b.bindings);
        assert_eq!(a.layout.tab_panes, b.layout.tab_panes);
        assert_eq!(cfg.theme().unwrap(), Config::builtin().theme().unwrap());
        assert_eq!(a.pane_keys.len(), b.pane_keys.len());
        for (pane, keys) in &a.pane_keys {
            assert_eq!(
                format!("{keys:?}"),
                format!("{:?}", b.pane_keys[pane]),
                "{pane}"
            );
        }
    }

    #[test]
    fn github_bindings_distinct_instances() {
        // Verify that issue_detail and pr_detail are different instances (different IDs)
        let cfg = load_github_page_config().unwrap();
        let issue_detail_id = cfg.resolve_id("issue_detail").unwrap();
        let pr_detail_id = cfg.resolve_id("pr_detail").unwrap();
        assert_ne!(
            issue_detail_id, pr_detail_id,
            "issue_detail and pr_detail must be distinct instances"
        );
        // Verify they are bound to different select panes
        let issue_binding = cfg
            .bindings
            .iter()
            .find(|(_, d)| d == "issue_detail")
            .expect("issue_detail binding missing");
        let pr_binding = cfg
            .bindings
            .iter()
            .find(|(_, d)| d == "pr_detail")
            .expect("pr_detail binding missing");
        assert_ne!(
            issue_binding.0, pr_binding.0,
            "issue_detail and pr_detail must be bound to different select panes"
        );
    }
}