mise-interactive-config 2026.6.2

Interactive TOML config editor for mise
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
//! Key handlers for different editor modes

use console::Key;
use std::io;

use super::undo::UndoAction;
use super::{ConfigResult, InteractiveConfig};
use crate::cursor::{AddButtonKind, CursorTarget};
use crate::document::EntryValue;
use crate::inline_edit::InlineEdit;
use crate::providers::version_variants;
use crate::render::{BooleanSelectState, Mode, PickerKind, VersionSelectState};

impl InteractiveConfig {
    /// Handle a key press. Returns Some(result) if the editor should exit.
    pub(super) async fn handle_key(&mut self, key: Key) -> io::Result<Option<ConfigResult>> {
        match &mut self.mode {
            Mode::Navigate => self.handle_navigate_key(key).await,
            Mode::Edit(_) => self.handle_edit_key(key),
            Mode::NewKey(_) => self.handle_new_key_key(key),
            Mode::ConfirmQuit => self.handle_confirm_quit_key(key),
            Mode::RenameKey(_, _, _) => self.handle_rename_key(key),
            Mode::Picker(_, _) => self.handle_picker_key(key).await,
            Mode::VersionSelect(_) => self.handle_version_select_key(key),
            Mode::BackendToolName(_, _, _) => self.handle_backend_tool_name_key(key),
            Mode::BooleanSelect(_) => self.handle_boolean_select_key(key),
            Mode::Loading(_) => {
                // Loading mode doesn't handle key presses - it's a transient state
                Ok(None)
            }
        }
    }

    pub(super) async fn handle_navigate_key(
        &mut self,
        key: Key,
    ) -> io::Result<Option<ConfigResult>> {
        match key {
            // Navigation
            Key::ArrowUp | Key::Char('k') => {
                self.cursor.up(&self.doc);
            }
            Key::ArrowDown | Key::Char('j') => {
                self.cursor.down(&self.doc);
            }
            Key::ArrowLeft | Key::Char('h') => {
                self.handle_collapse();
            }
            Key::ArrowRight | Key::Char('l') => {
                self.handle_expand();
            }
            Key::Tab => {
                self.cursor.next_section(&self.doc);
            }
            Key::BackTab => {
                self.cursor.prev_section(&self.doc);
            }

            // Actions
            Key::Enter => {
                self.handle_enter().await?;
            }
            Key::Backspace => {
                self.handle_remove()?;
            }
            Key::Char('o') => {
                self.handle_add_options()?;
            }
            Key::Char('u') => {
                self.undo();
            }
            Key::Char('r') => {
                self.handle_rename()?;
            }
            Key::Char('s') if !self.dry_run => {
                self.save()?;
                let content = self.doc.to_toml();
                return Ok(Some(ConfigResult::Saved(content)));
            }
            Key::Char('q') | Key::Escape => {
                if self.dry_run {
                    // In dry-run mode, just exit immediately
                    let content = self.doc.to_toml();
                    return Ok(Some(ConfigResult::Saved(content)));
                } else if self.doc.modified {
                    self.mode = Mode::ConfirmQuit;
                } else {
                    return Ok(Some(ConfigResult::Cancelled));
                }
            }

            _ => {}
        }
        Ok(None)
    }

    pub(super) fn handle_edit_key(&mut self, key: Key) -> io::Result<Option<ConfigResult>> {
        if let Mode::Edit(ref mut edit) = self.mode {
            match key {
                Key::Enter => {
                    let value = std::mem::replace(edit, InlineEdit::new("")).confirm();
                    self.apply_edit(value);
                    self.mode = Mode::Navigate;
                }
                Key::Escape => {
                    self.mode = Mode::Navigate;
                }
                Key::ArrowLeft => {
                    edit.left();
                }
                Key::ArrowRight => {
                    edit.right();
                }
                Key::Home => {
                    edit.home();
                }
                Key::End => {
                    edit.end();
                }
                Key::Backspace => {
                    edit.backspace();
                }
                Key::Del => {
                    edit.delete();
                }
                Key::Char(c) => {
                    edit.insert(c);
                }
                _ => {}
            }
        }
        Ok(None)
    }

    pub(super) fn handle_new_key_key(&mut self, key: Key) -> io::Result<Option<ConfigResult>> {
        if let Mode::NewKey(ref mut edit) = self.mode {
            match key {
                Key::Enter => {
                    // Check if we need KEY=value validation (for env variables)
                    let needs_key_value = matches!(
                        self.cursor.target(&self.doc),
                        Some(CursorTarget::AddButton(AddButtonKind::EnvVariable(_)))
                    );

                    let input = edit.buffer().to_string();

                    // Validate KEY=value format if needed
                    if needs_key_value && !input.contains('=') {
                        // Don't accept - needs KEY=value format
                        return Ok(None);
                    }

                    let key_name = std::mem::replace(edit, InlineEdit::new("")).confirm();
                    if !key_name.is_empty() {
                        self.apply_new_key(key_name);
                    }
                    self.mode = Mode::Navigate;
                }
                Key::Escape => {
                    self.mode = Mode::Navigate;
                }
                Key::ArrowLeft => {
                    edit.left();
                }
                Key::ArrowRight => {
                    edit.right();
                }
                Key::Home => {
                    edit.home();
                }
                Key::End => {
                    edit.end();
                }
                Key::Backspace => {
                    edit.backspace();
                }
                Key::Del => {
                    edit.delete();
                }
                Key::Char(c) => {
                    edit.insert(c);
                }
                _ => {}
            }
        }
        Ok(None)
    }

    pub(super) fn handle_confirm_quit_key(&mut self, key: Key) -> io::Result<Option<ConfigResult>> {
        match key {
            Key::Char('y') | Key::Char('Y') => {
                self.save()?;
                let content = self.doc.to_toml();
                return Ok(Some(ConfigResult::Saved(content)));
            }
            Key::Char('n') | Key::Char('N') => {
                return Ok(Some(ConfigResult::Cancelled));
            }
            Key::Escape => {
                self.mode = Mode::Navigate;
            }
            _ => {}
        }
        Ok(None)
    }

    pub(super) async fn handle_picker_key(&mut self, key: Key) -> io::Result<Option<ConfigResult>> {
        // We need to take ownership of the mode to modify the picker
        let mode = std::mem::replace(&mut self.mode, Mode::Navigate);

        if let Mode::Picker(kind, picker) = mode {
            let mut picker = *picker;
            match key {
                Key::Escape => {
                    // Cancel and return to navigate mode
                    self.mode = Mode::Navigate;
                }
                Key::Enter => {
                    // Select the current item
                    if let Some(selected) = picker.selected() {
                        let tool_name = selected.name.clone();
                        match &kind {
                            PickerKind::Tool(section_idx) => {
                                self.handle_picker_tool_select(&tool_name, *section_idx)
                                    .await;
                            }
                            PickerKind::Backend(section_idx) => {
                                // Transition to entering the tool name with the selected backend
                                let backend_name = tool_name;
                                self.mode = Mode::BackendToolName(
                                    backend_name,
                                    *section_idx,
                                    InlineEdit::new(""),
                                );
                            }
                            PickerKind::Setting(section_idx) => {
                                self.handle_picker_setting_select(&tool_name, *section_idx);
                            }
                            PickerKind::Hook(section_idx) => {
                                // Add the selected hook with empty value
                                self.doc.add_entry(*section_idx, tool_name, String::new());
                                let entry_idx = self.doc.sections[*section_idx].entries.len() - 1;
                                // Track undo for added entry
                                self.undo_stack
                                    .push(UndoAction::AddEntry(*section_idx, entry_idx));
                                let target = CursorTarget::Entry(*section_idx, entry_idx);
                                self.cursor.goto(&self.doc, &target);
                                self.mode = Mode::Edit(InlineEdit::new(""));
                            }
                            PickerKind::TaskConfig(section_idx) => {
                                self.handle_picker_task_config_select(&tool_name, *section_idx);
                            }
                            PickerKind::Monorepo(section_idx) => {
                                self.handle_picker_monorepo_select(&tool_name, *section_idx);
                            }
                            PickerKind::Section => {
                                self.handle_picker_section_select(&tool_name);
                            }
                        }
                    } else {
                        // No selection, return to navigate
                        self.mode = Mode::Navigate;
                    }
                }
                Key::ArrowUp | Key::Char('k') => {
                    picker.move_up();
                    self.mode = Mode::Picker(kind, Box::new(picker));
                }
                Key::ArrowDown | Key::Char('j') => {
                    picker.move_down();
                    self.mode = Mode::Picker(kind, Box::new(picker));
                }
                Key::Backspace => {
                    picker.backspace();
                    self.mode = Mode::Picker(kind, Box::new(picker));
                }
                Key::Char(c) => {
                    picker.type_char(c);
                    self.mode = Mode::Picker(kind, Box::new(picker));
                }
                _ => {
                    // Keep current state for unhandled keys
                    self.mode = Mode::Picker(kind, Box::new(picker));
                }
            }
        }
        Ok(None)
    }

    async fn handle_picker_tool_select(&mut self, tool_name: &str, section_idx: usize) {
        // Add the selected tool with default version
        self.doc
            .add_entry(section_idx, tool_name.to_string(), "latest".to_string());
        // Move cursor to the new entry
        let entry_idx = self.doc.sections[section_idx].entries.len() - 1;
        // Track undo for added entry
        self.undo_stack
            .push(UndoAction::AddEntry(section_idx, entry_idx));
        let target = CursorTarget::Entry(section_idx, entry_idx);
        self.cursor.goto(&self.doc, &target);

        // Show loading indicator while fetching version info
        self.mode = Mode::Loading(format!("Fetching versions for {}...", tool_name));
        let _ = self.render_current_mode();

        // Try to use version selector if we have version info
        if let Some(latest) = self.version_provider.latest_version(tool_name).await {
            let variants = version_variants(&latest);
            let mut vs = VersionSelectState::new(
                tool_name.to_string(),
                variants.clone(),
                section_idx,
                entry_idx,
            );
            // Use preferred specificity, clamped to valid range
            vs.selected = self
                .preferred_specificity
                .min(variants.len().saturating_sub(2));
            self.mode = Mode::VersionSelect(vs);
        } else {
            // Fall back to inline edit
            self.mode = Mode::Edit(InlineEdit::new("latest"));
        }
    }

    fn handle_picker_setting_select(&mut self, tool_name: &str, section_idx: usize) {
        // Check if this is a boolean setting
        let schema_type = crate::schema::setting_type(tool_name);
        if schema_type == Some(crate::schema::SchemaType::Boolean) {
            // Show boolean picker
            self.mode = Mode::BooleanSelect(BooleanSelectState::new_entry(
                tool_name.to_string(),
                section_idx,
            ));
        } else {
            // Add with type-appropriate value
            let (value, needs_edit) = Self::type_appropriate_default(schema_type);
            self.doc
                .add_entry_with_value(section_idx, tool_name.to_string(), value);
            let entry_idx = self.doc.sections[section_idx].entries.len() - 1;
            // Track undo for added entry
            self.undo_stack
                .push(UndoAction::AddEntry(section_idx, entry_idx));
            let target = CursorTarget::Entry(section_idx, entry_idx);
            self.cursor.goto(&self.doc, &target);
            if needs_edit {
                self.mode = Mode::Edit(InlineEdit::new(""));
            } else {
                self.mode = Mode::Navigate;
            }
        }
    }

    fn handle_picker_task_config_select(&mut self, tool_name: &str, section_idx: usize) {
        // Check if this is a boolean
        let schema_type = crate::schema::task_config_type(tool_name);
        if schema_type == Some(crate::schema::SchemaType::Boolean) {
            // Show boolean picker
            self.mode = Mode::BooleanSelect(BooleanSelectState::new_entry(
                tool_name.to_string(),
                section_idx,
            ));
        } else {
            // Add with type-appropriate value
            let (value, needs_edit) = Self::type_appropriate_default(schema_type);
            self.doc
                .add_entry_with_value(section_idx, tool_name.to_string(), value);
            let entry_idx = self.doc.sections[section_idx].entries.len() - 1;
            // Track undo for added entry
            self.undo_stack
                .push(UndoAction::AddEntry(section_idx, entry_idx));
            let target = CursorTarget::Entry(section_idx, entry_idx);
            self.cursor.goto(&self.doc, &target);
            if needs_edit {
                self.mode = Mode::Edit(InlineEdit::new(""));
            } else {
                self.mode = Mode::Navigate;
            }
        }
    }

    fn handle_picker_monorepo_select(&mut self, tool_name: &str, section_idx: usize) {
        // Check if this is a boolean
        let schema_type = crate::schema::monorepo_type(tool_name);
        if schema_type == Some(crate::schema::SchemaType::Boolean) {
            // Show boolean picker
            self.mode = Mode::BooleanSelect(BooleanSelectState::new_entry(
                tool_name.to_string(),
                section_idx,
            ));
        } else {
            // Add with type-appropriate value
            let (value, needs_edit) = Self::type_appropriate_default(schema_type);
            self.doc
                .add_entry_with_value(section_idx, tool_name.to_string(), value);
            let entry_idx = self.doc.sections[section_idx].entries.len() - 1;
            // Track undo for added entry
            self.undo_stack
                .push(UndoAction::AddEntry(section_idx, entry_idx));
            let target = CursorTarget::Entry(section_idx, entry_idx);
            self.cursor.goto(&self.doc, &target);
            if needs_edit {
                self.mode = Mode::Edit(InlineEdit::new(""));
            } else {
                self.mode = Mode::Navigate;
            }
        }
    }

    fn handle_picker_section_select(&mut self, tool_name: &str) {
        // Check if the selected item is a section or a top-level entry
        let is_section = crate::schema::is_valid_section(tool_name);

        if is_section {
            // Add as a new section
            let count_before = self.doc.sections.len();
            self.doc.add_section(tool_name.to_string());
            // Find and move cursor to the new section
            if let Some(idx) = self.doc.sections.iter().position(|s| s.name == tool_name) {
                // Only track undo if a section was actually added
                if self.doc.sections.len() > count_before {
                    self.undo_stack.push(UndoAction::AddSection(idx));
                }
                let target = CursorTarget::SectionHeader(idx);
                self.cursor.goto(&self.doc, &target);
            }
            self.mode = Mode::Navigate;
        } else {
            // Add as a top-level entry (in root section with empty name)
            // Find or create the root section
            let root_idx =
                if let Some(idx) = self.doc.sections.iter().position(|s| s.name.is_empty()) {
                    idx
                } else {
                    // Create root section at the beginning
                    // This shifts all section indices, so clear undo stack to avoid corruption
                    self.undo_stack.clear();
                    self.doc.sections.insert(
                        0,
                        crate::document::Section {
                            name: String::new(),
                            entries: Vec::new(),
                            expanded: true,
                            comments: Vec::new(),
                        },
                    );
                    self.doc.modified = true;
                    0
                };

            // Check if this is a boolean entry
            let schema_type = crate::schema::entry_type(tool_name);
            if schema_type == Some(crate::schema::SchemaType::Boolean) {
                // Show boolean picker
                self.mode = Mode::BooleanSelect(BooleanSelectState::new_entry(
                    tool_name.to_string(),
                    root_idx,
                ));
            } else {
                // Add the entry with type-appropriate value
                let (value, needs_edit) = Self::type_appropriate_default(schema_type);
                self.doc
                    .add_entry_with_value(root_idx, tool_name.to_string(), value);
                let entry_idx = self.doc.sections[root_idx].entries.len() - 1;
                // Track undo for added entry
                self.undo_stack
                    .push(UndoAction::AddEntry(root_idx, entry_idx));
                let target = CursorTarget::Entry(root_idx, entry_idx);
                self.cursor.goto(&self.doc, &target);
                if needs_edit {
                    self.mode = Mode::Edit(InlineEdit::new(""));
                } else {
                    self.mode = Mode::Navigate;
                }
            }
        }
    }

    pub(super) fn handle_backend_tool_name_key(
        &mut self,
        key: Key,
    ) -> io::Result<Option<ConfigResult>> {
        // Take ownership of the mode
        let mode = std::mem::replace(&mut self.mode, Mode::Navigate);

        if let Mode::BackendToolName(backend_name, section_idx, mut edit) = mode {
            match key {
                Key::Escape => {
                    // Cancel and return to navigate mode
                    self.mode = Mode::Navigate;
                }
                Key::Enter => {
                    let tool_name = edit.confirm();
                    if !tool_name.is_empty() {
                        // Combine backend and tool name (e.g., "cargo:ripgrep")
                        let full_name = format!("{}:{}", backend_name, tool_name);
                        self.doc
                            .add_entry(section_idx, full_name, "latest".to_string());
                        // Move cursor to the new entry
                        let entry_idx = self.doc.sections[section_idx].entries.len() - 1;
                        // Track undo for added entry
                        self.undo_stack
                            .push(UndoAction::AddEntry(section_idx, entry_idx));
                        let target = CursorTarget::Entry(section_idx, entry_idx);
                        self.cursor.goto(&self.doc, &target);
                    }
                    self.mode = Mode::Navigate;
                }
                Key::ArrowLeft => {
                    edit.left();
                    self.mode = Mode::BackendToolName(backend_name, section_idx, edit);
                }
                Key::ArrowRight => {
                    edit.right();
                    self.mode = Mode::BackendToolName(backend_name, section_idx, edit);
                }
                Key::Home => {
                    edit.home();
                    self.mode = Mode::BackendToolName(backend_name, section_idx, edit);
                }
                Key::End => {
                    edit.end();
                    self.mode = Mode::BackendToolName(backend_name, section_idx, edit);
                }
                Key::Backspace => {
                    edit.backspace();
                    self.mode = Mode::BackendToolName(backend_name, section_idx, edit);
                }
                Key::Del => {
                    edit.delete();
                    self.mode = Mode::BackendToolName(backend_name, section_idx, edit);
                }
                Key::Char(c) => {
                    edit.insert(c);
                    self.mode = Mode::BackendToolName(backend_name, section_idx, edit);
                }
                _ => {
                    self.mode = Mode::BackendToolName(backend_name, section_idx, edit);
                }
            }
        }
        Ok(None)
    }

    pub(super) fn handle_version_select_key(
        &mut self,
        key: Key,
    ) -> io::Result<Option<ConfigResult>> {
        use crate::providers::VERSION_CUSTOM_MARKER;

        // Take ownership of the mode
        let mode = std::mem::replace(&mut self.mode, Mode::Navigate);

        if let Mode::VersionSelect(mut vs) = mode {
            match key {
                Key::Escape => {
                    // Cancel and return to navigate mode
                    self.mode = Mode::Navigate;
                }
                Key::Enter => {
                    let version = vs.current().to_string();
                    if version == VERSION_CUSTOM_MARKER {
                        // Switch to inline edit for custom version entry
                        // Move cursor to the entry we're editing
                        let target = CursorTarget::Entry(vs.section_idx, vs.entry_idx);
                        self.cursor.goto(&self.doc, &target);
                        // Get current value to pre-fill the edit
                        let current = if let Some(entry) = self
                            .doc
                            .sections
                            .get(vs.section_idx)
                            .and_then(|s| s.entries.get(vs.entry_idx))
                        {
                            match &entry.value {
                                EntryValue::Simple(v) => v.clone(),
                                _ => String::new(),
                            }
                        } else {
                            String::new()
                        };
                        self.mode = Mode::Edit(InlineEdit::new(&current));
                    } else {
                        // Confirm selection and update the entry
                        // Save old value for undo
                        let old_value = self.doc.sections[vs.section_idx].entries[vs.entry_idx]
                            .value
                            .clone();
                        self.undo_stack.push(UndoAction::EditEntry(
                            vs.section_idx,
                            vs.entry_idx,
                            old_value,
                        ));
                        self.doc
                            .update_entry(vs.section_idx, vs.entry_idx, version.clone());
                        // Remember this specificity for future tools
                        self.preferred_specificity = vs.selected;
                        self.mode = Mode::Navigate;
                    }
                }
                Key::ArrowLeft | Key::Char('h') => {
                    vs.prev();
                    self.mode = Mode::VersionSelect(vs);
                }
                Key::ArrowRight | Key::Char('l') => {
                    vs.next();
                    self.mode = Mode::VersionSelect(vs);
                }
                _ => {
                    // Keep current state
                    self.mode = Mode::VersionSelect(vs);
                }
            }
        }
        Ok(None)
    }

    pub(super) fn handle_boolean_select_key(
        &mut self,
        key: Key,
    ) -> io::Result<Option<ConfigResult>> {
        // Take ownership of the mode
        let mode = std::mem::replace(&mut self.mode, Mode::Navigate);

        if let Mode::BooleanSelect(mut bs) = mode {
            match key {
                Key::Escape => {
                    // Cancel and return to navigate mode
                    self.mode = Mode::Navigate;
                }
                Key::Enter => {
                    // Confirm selection
                    let value = bs.value_str().to_string();
                    if let (Some(entry_idx), Some(field_idx)) = (bs.entry_idx, bs.field_idx) {
                        // Editing inline table field
                        if let Some(section) = self.doc.sections.get_mut(bs.section_idx)
                            && let Some(entry) = section.entries.get_mut(entry_idx)
                            && let EntryValue::InlineTable(ref mut pairs) = entry.value
                            && let Some((_, v)) = pairs.get_mut(field_idx)
                        {
                            *v = value;
                            self.doc.modified = true;
                        }
                    } else if let Some(entry_idx) = bs.entry_idx {
                        // Editing existing entry
                        self.doc.update_entry(bs.section_idx, entry_idx, value);
                    } else {
                        // Adding new entry
                        self.doc.add_entry(bs.section_idx, bs.key.clone(), value);
                        let entry_idx = self.doc.sections[bs.section_idx].entries.len() - 1;
                        // Track undo for added entry
                        self.undo_stack
                            .push(UndoAction::AddEntry(bs.section_idx, entry_idx));
                        let target = CursorTarget::Entry(bs.section_idx, entry_idx);
                        self.cursor.goto(&self.doc, &target);
                    }
                    self.mode = Mode::Navigate;
                }
                Key::ArrowLeft | Key::ArrowRight | Key::Char('h') | Key::Char('l') | Key::Tab => {
                    // Toggle between true and false
                    bs.toggle();
                    self.mode = Mode::BooleanSelect(bs);
                }
                Key::Char('t') => {
                    // Quick select true
                    bs.selected = true;
                    self.mode = Mode::BooleanSelect(bs);
                }
                Key::Char('f') => {
                    // Quick select false
                    bs.selected = false;
                    self.mode = Mode::BooleanSelect(bs);
                }
                _ => {
                    // Keep current state
                    self.mode = Mode::BooleanSelect(bs);
                }
            }
        }
        Ok(None)
    }

    pub(super) fn handle_rename_key(&mut self, key: Key) -> io::Result<Option<ConfigResult>> {
        // Take ownership of the mode
        let mode = std::mem::replace(&mut self.mode, Mode::Navigate);

        if let Mode::RenameKey(section_idx, entry_idx, mut edit) = mode {
            match key {
                Key::Escape => {
                    self.mode = Mode::Navigate;
                }
                Key::Enter => {
                    let new_key = edit.confirm();
                    if !new_key.is_empty() {
                        // Apply the rename
                        if let Some(section) = self.doc.sections.get_mut(section_idx)
                            && let Some(entry) = section.entries.get_mut(entry_idx)
                            && entry.key != new_key
                        {
                            // Save old key for undo
                            let old_key = entry.key.clone();
                            self.undo_stack.push(UndoAction::RenameEntry(
                                section_idx,
                                entry_idx,
                                old_key,
                            ));
                            entry.key = new_key;
                            self.doc.modified = true;
                        }
                    }
                    self.mode = Mode::Navigate;
                }
                Key::ArrowLeft => {
                    edit.left();
                    self.mode = Mode::RenameKey(section_idx, entry_idx, edit);
                }
                Key::ArrowRight => {
                    edit.right();
                    self.mode = Mode::RenameKey(section_idx, entry_idx, edit);
                }
                Key::Home => {
                    edit.home();
                    self.mode = Mode::RenameKey(section_idx, entry_idx, edit);
                }
                Key::End => {
                    edit.end();
                    self.mode = Mode::RenameKey(section_idx, entry_idx, edit);
                }
                Key::Backspace => {
                    edit.backspace();
                    self.mode = Mode::RenameKey(section_idx, entry_idx, edit);
                }
                Key::Del => {
                    edit.delete();
                    self.mode = Mode::RenameKey(section_idx, entry_idx, edit);
                }
                Key::Char(c) => {
                    edit.insert(c);
                    self.mode = Mode::RenameKey(section_idx, entry_idx, edit);
                }
                _ => {
                    self.mode = Mode::RenameKey(section_idx, entry_idx, edit);
                }
            }
        }
        Ok(None)
    }
}