tui-canvas 0.8.10

Form/textarea/input for TUI
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
#[cfg(feature = "crossterm")]
use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
#[cfg(feature = "cursor-style")]
use std::io;

#[cfg(feature = "gui")]
use crate::gui_utils::{
    compute_h_scroll_with_padding, display_cols_up_to, display_width, effective_right_pad,
};
use crate::textinput::provider::{TextInputDataProvider, TextInputProvider};
#[cfg(feature = "cursor-style")]
use crate::CursorManager;
use crate::{canvas::state::EditorState, textform::TextFormState};

#[cfg(feature = "gui")]
use ratatui::{layout::Rect, widgets::Block};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextInputEventOutcome {
    Ignored,
    Handled,
    Submitted,
}

/// Single-line text input widget state.
///
/// Wraps a single-field [`TextFormState`]. Editing, cursor, and movement methods
/// from the form are available directly, and the form can be reached explicitly
/// via [`TextInputState::form`] / [`TextInputState::form_mut`].
/// With the `validation` and `computed` features enabled, the corresponding
/// helper methods are re-exposed as inherent methods on this type.
pub struct TextInputState<P: TextInputDataProvider = TextInputProvider> {
    pub(crate) form: TextFormState<P>,
    pub(crate) placeholder: Option<String>,
    pub(crate) suggestion_suffix: Option<String>,
    pub(crate) overflow_indicator: char,
    pub(crate) h_scroll: u16,
    #[cfg(feature = "gui")]
    pub(crate) edited_this_frame: bool,
}

impl<P: TextInputDataProvider + Default> Default for TextInputState<P> {
    fn default() -> Self {
        Self {
            form: TextFormState::new(P::default()),
            placeholder: None,
            suggestion_suffix: None,
            overflow_indicator: '$',
            h_scroll: 0,
            #[cfg(feature = "gui")]
            edited_this_frame: false,
        }
    }
}

impl<P: TextInputDataProvider> std::ops::Deref for TextInputState<P> {
    type Target = TextFormState<P>;

    fn deref(&self) -> &Self::Target {
        &self.form
    }
}

impl<P: TextInputDataProvider> std::ops::DerefMut for TextInputState<P> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.form
    }
}

impl<P: TextInputDataProvider> TextInputState<P> {
    pub fn with_provider(provider: P) -> Self {
        Self {
            form: TextFormState::new(provider),
            placeholder: None,
            suggestion_suffix: None,
            overflow_indicator: '$',
            h_scroll: 0,
            #[cfg(feature = "gui")]
            edited_this_frame: false,
        }
    }

    pub fn from_text<S: Into<String>>(text: S) -> Self {
        Self::with_provider(P::from_text(text.into()))
    }

    pub fn text(&self) -> String {
        self.form.data_provider().to_text()
    }

    pub fn set_text<S: Into<String>>(&mut self, text: S) {
        self.form.data_provider_mut().set_text(text.into());
        self.form.ui_state.current_field = 0;
        let cursor = self.current_text().chars().count();
        self.form.set_cursor_raw(cursor);
        self.clear_suggestion_suffix();
        self.h_scroll = 0;
    }

    pub fn set_placeholder<S: Into<String>>(&mut self, s: S) {
        self.placeholder = Some(s.into());
    }

    pub fn suggestion_suffix(&self) -> Option<&str> {
        self.suggestion_suffix.as_deref()
    }

    pub fn set_suggestion_suffix<S: Into<String>>(&mut self, suffix: S) {
        let suffix = suffix.into();
        self.suggestion_suffix = if suffix.is_empty() {
            None
        } else {
            Some(suffix)
        };
    }

    pub fn clear_suggestion_suffix(&mut self) {
        self.suggestion_suffix = None;
    }

    pub fn accept_suggestion_suffix(&mut self) -> TextInputEventOutcome {
        let Some(suffix) = self.suggestion_suffix.clone() else {
            return TextInputEventOutcome::Ignored;
        };

        if suffix.is_empty() {
            self.clear_suggestion_suffix();
            return TextInputEventOutcome::Ignored;
        }

        self.enter_edit_mode();
        #[cfg(feature = "gui")]
        {
            self.edited_this_frame = true;
        }
        self.clear_suggestion_suffix();
        let _ = self.insert_text(&suffix);
        TextInputEventOutcome::Handled
    }

    pub fn set_overflow_indicator(&mut self, ch: char) {
        self.overflow_indicator = ch;
    }

    pub fn paste(&mut self, text: &str) -> TextInputEventOutcome {
        let filtered: String = text
            .chars()
            .filter(|&ch| ch != '\n' && ch != '\r')
            .collect();

        if filtered.is_empty() {
            return TextInputEventOutcome::Ignored;
        }

        self.enter_edit_mode();
        self.clear_suggestion_suffix();
        #[cfg(feature = "gui")]
        {
            self.edited_this_frame = true;
        }
        let _ = self.insert_text(&filtered);
        TextInputEventOutcome::Handled
    }

    /// Update terminal cursor style for this single-line input.
    ///
    /// Follows the editor's current mode so that Helix/Vim-style modal editing
    /// produces the correct cursor: a steady block in normal mode, a beam in
    /// insert mode, and a blinking block for selections.
    #[cfg(feature = "cursor-style")]
    pub fn update_cursor_style(&self) -> io::Result<()> {
        CursorManager::update_for_mode(self.form.mode())
    }

    #[cfg(not(feature = "cursor-style"))]
    pub fn update_cursor_style(&self) -> std::io::Result<()> {
        Ok(())
    }

    // TODO: Replace direct crossterm event coupling with a backend-agnostic
    // input abstraction so terminal input backends can be swapped cleanly.
    #[cfg(feature = "crossterm")]
    pub fn handle_event(&mut self, event: Event) -> TextInputEventOutcome {
        match event {
            Event::Key(key) => self.input(key),
            Event::Paste(text) => self.paste(&text),
            _ => TextInputEventOutcome::Ignored,
        }
    }

    #[cfg(feature = "crossterm")]
    pub fn input(&mut self, key: KeyEvent) -> TextInputEventOutcome {
        if key.kind != KeyEventKind::Press {
            return TextInputEventOutcome::Ignored;
        }

        match (key.code, key.modifiers) {
            (KeyCode::Enter, _) => TextInputEventOutcome::Submitted,
            (KeyCode::Tab, KeyModifiers::NONE) => self.accept_suggestion_suffix(),
            (KeyCode::Backspace, _) => {
                self.clear_suggestion_suffix();
                #[cfg(feature = "gui")]
                {
                    self.edited_this_frame = true;
                }
                let _ = self.delete_backward();
                TextInputEventOutcome::Handled
            }
            (KeyCode::Delete, _) => {
                self.clear_suggestion_suffix();
                #[cfg(feature = "gui")]
                {
                    self.edited_this_frame = true;
                }
                let _ = self.delete_forward();
                TextInputEventOutcome::Handled
            }
            (KeyCode::Left, _) => {
                let _ = self.move_left();
                TextInputEventOutcome::Handled
            }
            (KeyCode::Right, _) => {
                let _ = self.move_right();
                TextInputEventOutcome::Handled
            }
            (KeyCode::Up, _) | (KeyCode::Down, _) => TextInputEventOutcome::Ignored,
            (KeyCode::Home, _) | (KeyCode::Char('a'), KeyModifiers::CONTROL) => {
                self.move_line_start();
                TextInputEventOutcome::Handled
            }
            (KeyCode::End, _) | (KeyCode::Char('e'), KeyModifiers::CONTROL) => {
                self.move_line_end();
                TextInputEventOutcome::Handled
            }
            (KeyCode::Char('b'), KeyModifiers::ALT) => {
                self.move_word_prev();
                TextInputEventOutcome::Handled
            }
            (KeyCode::Char('f'), KeyModifiers::ALT) => {
                self.move_word_next();
                TextInputEventOutcome::Handled
            }
            (KeyCode::Char('e'), KeyModifiers::ALT) => {
                self.move_word_end();
                TextInputEventOutcome::Handled
            }
            (KeyCode::Char(c), m) if m.is_empty() => {
                self.enter_edit_mode();
                self.clear_suggestion_suffix();
                #[cfg(feature = "gui")]
                {
                    self.edited_this_frame = true;
                }
                let _ = self.insert_char(c);
                TextInputEventOutcome::Handled
            }
            _ => TextInputEventOutcome::Ignored,
        }
    }

    #[cfg(feature = "gui")]
    pub fn cursor(&self, area: Rect, block: Option<&Block<'_>>) -> (u16, u16) {
        let inner = if let Some(b) = block {
            b.inner(area)
        } else {
            area
        };
        let cursor_cols = self.current_cursor_cols();
        let total_cols = display_width(&self.current_display_text_for_render());
        let left_cols = if self.h_scroll > 0 { 1 } else { 0 };

        let mut x_off_visible = cursor_cols
            .saturating_sub(self.h_scroll)
            .saturating_add(left_cols);

        let limit = inner
            .width
            .saturating_sub(1 + effective_right_pad(cursor_cols, total_cols));
        if x_off_visible > limit {
            x_off_visible = limit;
        }

        (inner.x.saturating_add(x_off_visible), inner.y)
    }

    #[cfg(feature = "gui")]
    pub(crate) fn ensure_visible(&mut self, area: Rect, block: Option<&Block<'_>>) {
        let inner = if let Some(b) = block {
            b.inner(area)
        } else {
            area
        };
        if inner.width == 0 {
            return;
        }

        let total_cols = display_width(&self.current_display_text_for_render());
        let cursor_cols = self.current_cursor_cols();
        let (target_h, _) = compute_h_scroll_with_padding(cursor_cols, total_cols, inner.width);

        if target_h > self.h_scroll {
            self.h_scroll = target_h;
        } else if cursor_cols < self.h_scroll {
            self.h_scroll = cursor_cols;
        }
    }

    #[cfg(feature = "gui")]
    pub(crate) fn take_edited_flag(&mut self) -> bool {
        let v = self.edited_this_frame;
        self.edited_this_frame = false;
        v
    }

    #[cfg(feature = "gui")]
    pub(crate) fn current_display_text_for_render(&self) -> String {
        #[cfg(feature = "validation")]
        {
            self.current_display_text()
        }

        #[cfg(not(feature = "validation"))]
        {
            self.current_text().to_string()
        }
    }

    #[cfg(feature = "gui")]
    fn current_cursor_cols(&self) -> u16 {
        let text = self.current_display_text_for_render();
        display_cols_up_to(&text, self.display_cursor_position())
    }
}

impl<P: TextInputDataProvider> TextInputState<P> {
    /// Borrow the underlying [`TextFormState`].
    pub fn form(&self) -> &TextFormState<P> {
        &self.form
    }

    /// Mutably borrow the underlying [`TextFormState`].
    pub fn form_mut(&mut self) -> &mut TextFormState<P> {
        &mut self.form
    }

    pub fn current_field(&self) -> usize {
        self.form.current_field()
    }

    pub fn cursor_position(&self) -> usize {
        self.form.cursor_position()
    }

    pub fn current_text(&self) -> &str {
        self.form.current_text()
    }

    pub fn mode(&self) -> crate::canvas::modes::AppMode {
        self.form.mode()
    }

    pub fn ui_state(&self) -> &EditorState {
        self.form.ui_state()
    }

    pub fn data_provider(&self) -> &P {
        self.form.data_provider()
    }

    pub fn data_provider_mut(&mut self) -> &mut P {
        self.form.data_provider_mut()
    }

    pub fn move_left(&mut self) -> anyhow::Result<()> {
        self.form.move_left()
    }

    pub fn move_right(&mut self) -> anyhow::Result<()> {
        self.form.move_right()
    }

    pub fn move_line_start(&mut self) {
        self.form.move_line_start();
    }

    pub fn move_line_end(&mut self) {
        self.form.move_line_end();
    }

    pub fn set_cursor_position(&mut self, position: usize) {
        self.form.set_cursor_position(position);
    }

    pub fn enter_edit_mode(&mut self) {
        self.form.enter_edit_mode();
    }

    pub fn exit_edit_mode(&mut self) -> anyhow::Result<()> {
        self.form.exit_edit_mode()
    }

    pub fn insert_char(&mut self, ch: char) -> anyhow::Result<()> {
        self.form.insert_char(ch)
    }

    pub fn insert_text(&mut self, text: &str) -> anyhow::Result<()> {
        self.form.insert_text(text)
    }

    pub fn set_current_field_value(&mut self, value: String) {
        self.form.set_current_field_value(value);
    }

    pub fn delete_backward(&mut self) -> anyhow::Result<()> {
        self.form.delete_backward()
    }

    pub fn delete_forward(&mut self) -> anyhow::Result<()> {
        self.form.delete_forward()
    }

    /// Move to the start of the next word.
    pub fn move_word_next(&mut self) {
        self.form.move_word_next();
    }

    /// Move to the start of the previous word.
    pub fn move_word_prev(&mut self) {
        self.form.move_word_prev();
    }

    /// Move to the end of the current/next word.
    pub fn move_word_end(&mut self) {
        self.form.move_word_end();
    }

    /// Move to the end of the previous word.
    pub fn move_word_end_prev(&mut self) {
        self.form.move_word_end_prev();
    }

    /// Move to the start of the next WORD (whitespace-delimited, vim `W`).
    pub fn move_big_word_next(&mut self) {
        self.form.move_big_word_next();
    }

    /// Move to the start of the previous WORD (vim `B`).
    pub fn move_big_word_prev(&mut self) {
        self.form.move_big_word_prev();
    }

    /// Move to the end of the current/next WORD (vim `E`).
    pub fn move_big_word_end(&mut self) {
        self.form.move_big_word_end();
    }

    /// Move to the end of the previous WORD (vim `gE`).
    pub fn move_big_word_end_prev(&mut self) {
        self.form.move_big_word_end_prev();
    }

    /// Enter edit mode with the cursor positioned for append (vim `a`).
    pub fn enter_append_mode(&mut self) {
        self.form.enter_append_mode();
    }

    /// The current field's display text (mask/formatter-aware when the
    /// `validation` feature is enabled; otherwise the raw text).
    #[cfg(feature = "validation")]
    pub fn current_display_text(&self) -> String {
        self.form.current_display_text()
    }

    /// The current field's text (raw; no validation feature for masking).
    #[cfg(not(feature = "validation"))]
    pub fn current_display_text(&self) -> String {
        self.form.current_text().to_string()
    }

    /// Cursor position in display coordinates (accounts for a display mask).
    pub fn display_cursor_position(&self) -> usize {
        self.form.display_cursor_position()
    }
}

/// Validation helpers, re-exposed from the underlying [`TextFormState`] so they are
/// part of `TextInputState`'s own public API.
#[cfg(feature = "validation")]
impl<P: TextInputDataProvider> TextInputState<P> {
    pub fn set_validation_enabled(&mut self, enabled: bool) {
        self.form.set_validation_enabled(enabled);
    }

    pub fn is_validation_enabled(&self) -> bool {
        self.form.is_validation_enabled()
    }

    pub fn set_field_validation(
        &mut self,
        field_index: usize,
        config: crate::validation::ValidationConfig,
    ) {
        self.form.set_field_validation(field_index, config);
    }

    pub fn remove_field_validation(&mut self, field_index: usize) {
        self.form.remove_field_validation(field_index);
    }

    pub fn validate_current_field(&mut self) -> crate::validation::ValidationResult {
        self.form.validate_current_field()
    }

    pub fn validate_field(
        &mut self,
        field_index: usize,
    ) -> Option<crate::validation::ValidationResult> {
        self.form.validate_field(field_index)
    }

    pub fn clear_validation_results(&mut self) {
        self.form.clear_validation_results();
    }

    pub fn validation_summary(&self) -> crate::validation::ValidationSummary {
        self.form.validation_summary()
    }

    pub fn can_switch_fields(&self) -> bool {
        self.form.can_switch_fields()
    }

    pub fn field_switch_block_reason(&self) -> Option<String> {
        self.form.field_switch_block_reason()
    }

    pub fn last_switch_block(&self) -> Option<&str> {
        self.form.last_switch_block()
    }

    pub fn current_limits_status_text(&self) -> Option<String> {
        self.form.current_limits_status_text()
    }

    pub fn current_formatter_warning(&self) -> Option<String> {
        self.form.current_formatter_warning()
    }

    pub fn external_validation_of(
        &self,
        field_index: usize,
    ) -> crate::validation::ExternalValidationState {
        self.form.external_validation_of(field_index)
    }

    pub fn clear_all_external_validation(&mut self) {
        self.form.clear_all_external_validation();
    }

    pub fn clear_external_validation(&mut self, field_index: usize) {
        self.form.clear_external_validation(field_index);
    }

    pub fn set_external_validation(
        &mut self,
        field_index: usize,
        state: crate::validation::ExternalValidationState,
    ) {
        self.form.set_external_validation(field_index, state);
    }

    pub fn set_external_validation_callback<F>(&mut self, callback: F)
    where
        F: FnMut(usize, &str) -> crate::validation::ExternalValidationState + Send + Sync + 'static,
    {
        self.form.set_external_validation_callback(callback);
    }
}

/// Computed-field helpers, re-exposed from the underlying [`TextFormState`].
#[cfg(feature = "computed")]
impl<P: TextInputDataProvider> TextInputState<P> {
    pub fn register_computed_provider<C>(&mut self, provider: &C)
    where
        C: crate::computed::ComputedProvider,
    {
        self.form.register_computed_provider(provider);
    }

    pub fn set_computed_provider<C>(&mut self, provider: C)
    where
        C: crate::computed::ComputedProvider,
    {
        self.form.set_computed_provider(provider);
    }

    pub fn recompute_fields<C>(&mut self, provider: &mut C, field_indices: &[usize])
    where
        C: crate::computed::ComputedProvider,
    {
        self.form.recompute_fields(provider, field_indices);
    }

    pub fn recompute_all_fields<C>(&mut self, provider: &mut C)
    where
        C: crate::computed::ComputedProvider,
    {
        self.form.recompute_all_fields(provider);
    }

    pub fn on_field_changed<C>(&mut self, provider: &mut C, changed_field: usize)
    where
        C: crate::computed::ComputedProvider,
    {
        self.form.on_field_changed(provider, changed_field);
    }

    pub fn effective_field_value(&self, field_index: usize) -> String {
        self.form.effective_field_value(field_index)
    }
}

/// Undo/redo, re-exposed from the underlying [`TextFormState`].
impl<P: TextInputDataProvider> TextInputState<P> {
    pub fn undo(&mut self) -> bool {
        self.form.undo()
    }

    pub fn redo(&mut self) -> bool {
        self.form.redo()
    }

    pub fn can_undo(&self) -> bool {
        self.form.can_undo()
    }

    pub fn can_redo(&self) -> bool {
        self.form.can_redo()
    }

    pub fn clear_history(&mut self) {
        self.form.clear_history();
    }

    pub fn set_history_limit(&mut self, limit: usize) {
        self.form.set_history_limit(limit);
    }
}

/// Dropdown suggestions, re-exposed from the underlying [`TextFormState`] so that
/// `TextInput`, `TextArea`, and `TextFormState` all share one suggestions
/// mechanism. Render the dropdown with
/// `canvas::suggestions::render::render_suggestions_dropdown(.., self.form())`.
///
/// This is distinct from the lightweight inline-suffix completion
/// ([`TextInputState::set_suggestion_suffix`]), which `TextInput` also offers.
#[cfg(feature = "suggestions")]
impl<P: TextInputDataProvider> TextInputState<P> {
    pub fn open_suggestions(&mut self, field_index: usize) {
        self.form.open_suggestions(field_index);
    }

    pub fn check_suggestion_trigger(&mut self) {
        self.form.check_suggestion_trigger();
    }

    pub fn trigger_suggestions(&mut self) -> Option<(usize, String)> {
        self.form.trigger_suggestions()
    }

    pub fn apply_suggestions(&mut self, items: Vec<crate::SuggestionItem>) {
        self.form.apply_suggestions(items);
    }

    pub fn update_suggestions(&mut self, items: Vec<crate::SuggestionItem>) {
        self.form.update_suggestions(items);
    }

    pub fn dismiss_suggestions(&mut self) {
        self.form.dismiss_suggestions();
    }

    pub fn cancel_suggestions(&mut self) {
        self.form.cancel_suggestions();
    }

    pub fn suggestions_next(&mut self) {
        self.form.suggestions_next();
    }

    pub fn suggestions_prev(&mut self) {
        self.form.suggestions_prev();
    }

    pub fn apply_suggestion(&mut self) -> Option<String> {
        self.form.apply_suggestion()
    }

    pub fn is_suggestions_active(&self) -> bool {
        self.form.is_suggestions_active()
    }

    pub fn is_suggestions_loading(&self) -> bool {
        self.form.ui_state().is_suggestions_loading()
    }

    pub fn dropdown_suggestions(&self) -> &[crate::SuggestionItem] {
        self.form.suggestions()
    }
}

#[cfg(test)]
mod tests {
    #[cfg(feature = "crossterm")]
    use crossterm::event::Event;
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    #[cfg(feature = "gui")]
    use ratatui::layout::Rect;

    use super::{TextInputEventOutcome, TextInputState};
    use crate::textinput::provider::TextInputProvider;

    #[test]
    fn enter_submits_without_mutating_text() {
        let mut input = TextInputState::<TextInputProvider>::from_text("hello");
        let outcome = input.input(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));

        assert_eq!(outcome, TextInputEventOutcome::Submitted);
        assert_eq!(input.text(), "hello");
    }

    #[test]
    fn vertical_arrows_are_ignored() {
        let mut input = TextInputState::<TextInputProvider>::from_text("hello");
        let outcome = input.input(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE));

        assert_eq!(outcome, TextInputEventOutcome::Ignored);
        assert_eq!(input.current_field(), 0);
    }

    #[test]
    fn paste_filters_line_breaks_for_single_line_input() {
        let mut input = TextInputState::<TextInputProvider>::from_text("ab");
        input.enter_edit_mode();
        input.set_cursor_position(2);

        let outcome = input.paste("c\r\nd\nef");

        assert_eq!(outcome, TextInputEventOutcome::Handled);
        assert_eq!(input.text(), "abcdef");
    }

    #[test]
    fn tab_accepts_inline_suggestion_suffix() {
        let mut input = TextInputState::<TextInputProvider>::from_text("ad");
        input.enter_edit_mode();
        input.set_cursor_position(2);
        input.set_suggestion_suffix("min");

        let outcome = input.input(KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE));

        assert_eq!(outcome, TextInputEventOutcome::Handled);
        assert_eq!(input.text(), "admin");
        assert_eq!(input.suggestion_suffix(), None);
    }

    #[cfg(feature = "crossterm")]
    #[test]
    fn handle_event_routes_paste_events() {
        let mut input = TextInputState::<TextInputProvider>::from_text("hi");
        input.enter_edit_mode();
        input.set_cursor_position(2);

        let outcome = input.handle_event(Event::Paste(" there".to_string()));

        assert_eq!(outcome, TextInputEventOutcome::Handled);
        assert_eq!(input.text(), "hi there");
    }

    #[test]
    fn undo_redo_round_trip() {
        let mut input = TextInputState::<TextInputProvider>::from_text("");
        input.enter_edit_mode();
        let _ = input.insert_text("abc"); // one coalesced Insert run

        assert_eq!(input.text(), "abc");
        assert!(input.can_undo());
        assert!(input.undo());
        assert_eq!(input.text(), "");
        assert!(input.can_redo());
        assert!(input.redo());
        assert_eq!(input.text(), "abc");
        assert!(!input.redo());
    }

    #[test]
    fn undo_separates_insert_and_delete_runs() {
        let mut input = TextInputState::<TextInputProvider>::from_text("");
        input.enter_edit_mode();
        let _ = input.insert_text("ab"); // Insert run
        let _ = input.delete_backward(); // Delete run -> separate step
        assert_eq!(input.text(), "a");

        assert!(input.undo()); // undo the delete
        assert_eq!(input.text(), "ab");
        assert!(input.undo()); // undo the insert run
        assert_eq!(input.text(), "");
        assert!(!input.undo());
    }

    #[test]
    fn history_limit_drops_oldest() {
        let mut input = TextInputState::<TextInputProvider>::from_text("");
        input.set_history_limit(2);

        // Three non-coalescing edits; only the last two checkpoints are kept.
        input.set_current_field_value("one".to_string());
        input.set_current_field_value("two".to_string());
        input.set_current_field_value("three".to_string());

        assert!(input.undo());
        assert_eq!(input.text(), "two");
        assert!(input.undo());
        assert_eq!(input.text(), "one");
        // The checkpoint for the empty initial state was dropped by the limit.
        assert!(!input.undo());
    }

    #[test]
    fn cursor_move_breaks_coalescing() {
        let mut input = TextInputState::<TextInputProvider>::from_text("");
        input.enter_edit_mode();
        let _ = input.insert_text("ab");
        let _ = input.move_left(); // ends the typing run
        let _ = input.insert_text("X"); // new run, inserted before 'b'
        assert_eq!(input.text(), "aXb");

        assert!(input.undo()); // undo "X"
        assert_eq!(input.text(), "ab");
        assert!(input.undo()); // undo "ab"
        assert_eq!(input.text(), "");
    }

    #[cfg(all(feature = "keybindings", feature = "crossterm"))]
    #[test]
    fn handle_key_event_returns_key_event_outcome_not_textinput_outcome() {
        use crate::keybindings::{BuiltinCanvasKeybindingPreset, KeyEventOutcome};

        let mut input = TextInputState::<TextInputProvider>::from_text("hello");
        input.use_keybinding_preset(BuiltinCanvasKeybindingPreset::Helix);
        // This won't compile if handle_key_event resolves to a method returning
        // TextInputEventOutcome — it must be KeyEventOutcome from TextFormState.
        let outcome: KeyEventOutcome =
            input.handle_key_event(KeyEvent::new(KeyCode::Char('h'), KeyModifiers::NONE));
        // 'h' in normal mode with Helix keybindings should be Consumed (move left).
        assert!(!matches!(outcome, KeyEventOutcome::NotMatched));
    }

    #[cfg(all(feature = "keybindings", feature = "crossterm"))]
    #[test]
    fn helix_i_enters_insert_mode_from_normal() {
        use crate::canvas::modes::AppMode;
        use crate::keybindings::{BuiltinCanvasKeybindingPreset, KeyEventOutcome};

        let mut input = TextInputState::<TextInputProvider>::from_text("hello");
        input.use_keybinding_preset(BuiltinCanvasKeybindingPreset::Helix);

        // Start in normal mode (default for non-textmode-normal)
        assert_eq!(input.mode(), AppMode::Nor);

        // Press 'i' — should enter insert mode
        let outcome = input.handle_key_event(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE));
        assert!(
            matches!(outcome, KeyEventOutcome::Consumed(None)),
            "expected Consumed, got {:?}",
            outcome
        );
        assert_eq!(input.mode(), AppMode::Ins);
    }

    #[cfg(all(feature = "keybindings", feature = "crossterm"))]
    #[test]
    fn helix_esc_exits_insert_mode_to_normal() {
        use crate::canvas::modes::AppMode;
        use crate::keybindings::{BuiltinCanvasKeybindingPreset, KeyEventOutcome};

        let mut input = TextInputState::<TextInputProvider>::from_text("hello");
        input.use_keybinding_preset(BuiltinCanvasKeybindingPreset::Helix);

        // Enter insert mode first
        let _ = input.handle_key_event(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE));
        assert_eq!(input.mode(), AppMode::Ins);

        // Press Esc — should exit to normal mode
        let outcome =
            input.handle_key_event(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
        assert!(
            matches!(outcome, KeyEventOutcome::Consumed(None)),
            "expected Consumed, got {:?}",
            outcome
        );
        assert_eq!(input.mode(), AppMode::Nor);
    }

    #[cfg(all(feature = "keybindings", feature = "crossterm"))]
    #[test]
    fn helix_normal_movement_works() {
        use crate::canvas::modes::AppMode;
        use crate::keybindings::{BuiltinCanvasKeybindingPreset, KeyEventOutcome};

        let mut input = TextInputState::<TextInputProvider>::from_text("hello");
        input.use_keybinding_preset(BuiltinCanvasKeybindingPreset::Helix);

        // Enter insert, type a char, exit to normal
        let _ = input.handle_key_event(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE));
        let _ = input.handle_key_event(KeyEvent::new(KeyCode::Char('X'), KeyModifiers::NONE));
        let _ = input.handle_key_event(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
        assert_eq!(input.mode(), AppMode::Nor);
        // confirm text was inserted
        assert!(input.text().contains("X"));

        // h should move left in normal mode
        let outcome =
            input.handle_key_event(KeyEvent::new(KeyCode::Char('h'), KeyModifiers::NONE));
        assert!(
            matches!(outcome, KeyEventOutcome::Consumed(None)),
            "expected Consumed, got {:?}",
            outcome
        );
        // still in normal mode
        assert_eq!(input.mode(), AppMode::Nor);
    }

    #[cfg(all(feature = "keybindings", feature = "cursor-style"))]
    #[test]
    fn update_cursor_style_follows_actual_mode() {
        use crate::canvas::modes::AppMode;

        // This test verifies that update_cursor_style() tracks the real editor
        // mode rather than always forcing insert-mode cursor.
        let input = TextInputState::<TextInputProvider>::from_text("hello");
        // Default mode is normal (Nor) for modal editing.
        assert_eq!(input.mode(), AppMode::Nor);

        // update_cursor_style should respect the normal-mode cursor.
        let result: std::io::Result<()> = input.update_cursor_style();
        assert!(result.is_ok());
    }

    #[cfg(feature = "gui")]
    #[test]
    fn ensure_visible_scrolls_before_text_overflows_to_keep_end_margin() {
        let mut input = TextInputState::<TextInputProvider>::from_text("123456789");
        input.enter_edit_mode();
        input.set_cursor_position(9);

        input.ensure_visible(Rect::new(0, 0, 10, 1), None);

        assert_eq!(input.h_scroll, 3);
    }

    #[cfg(all(feature = "keybindings", feature = "crossterm", feature = "cursor-style"))]
    #[test]
    fn helix_full_event_loop_mirrors_example_usage() {
        use crate::canvas::modes::AppMode;
        use crate::keybindings::{BuiltinCanvasKeybindingPreset, KeyEventOutcome};

        // Simulate the example's event loop: update_cursor_style → handle_key_event
        let mut input = TextInputState::<TextInputProvider>::from_text("hello");
        input.use_keybinding_preset(BuiltinCanvasKeybindingPreset::Helix);

        // Frame 1: normal mode
        assert_eq!(input.mode(), AppMode::Nor);
        assert!(input.update_cursor_style().is_ok());

        // Frame 2: press 'i' to enter insert mode
        let outcome = input.handle_key_event(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE));
        assert!(matches!(outcome, KeyEventOutcome::Consumed(None)));
        assert_eq!(input.mode(), AppMode::Ins);
        assert!(input.update_cursor_style().is_ok());

        // Frame 3: type 'x' in insert mode (should insert char)
        let outcome = input.handle_key_event(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE));
        assert!(matches!(outcome, KeyEventOutcome::Consumed(None)));
        assert!(input.text().contains('x'));
        assert!(input.update_cursor_style().is_ok());

        // Frame 4: exit to normal mode with Esc
        let outcome = input.handle_key_event(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
        assert!(matches!(outcome, KeyEventOutcome::Consumed(None)));
        assert_eq!(input.mode(), AppMode::Nor);
        assert!(input.update_cursor_style().is_ok());

        // Frame 5: 'h' should move left in normal mode
        let old_pos = input.cursor_position();
        let outcome = input.handle_key_event(KeyEvent::new(KeyCode::Char('h'), KeyModifiers::NONE));
        assert!(matches!(outcome, KeyEventOutcome::Consumed(None)));
        assert_eq!(input.mode(), AppMode::Nor);
        // Cursor should have moved left
        assert!(input.cursor_position() < old_pos || old_pos == 0);
        assert!(input.update_cursor_style().is_ok());
    }
}