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
//! Keybinding handlers for AI suggestion selection
//!
//! Handles Alt+1-5 for direct selection, Alt+Up/Down/j/k for navigation,
//! and Enter for applying navigated selection.
use ;
use SelectionState;
/// Handle direct selection keybindings (Alt+1-5)
///
/// Parses Alt+1 through Alt+5 keybindings and validates the selection
/// index against the available suggestion count.
///
/// # Arguments
/// * `key` - The key event to handle
/// * `suggestion_count` - Number of available suggestions
///
/// # Returns
/// * `Some(index)` - The 0-based index of the selected suggestion if valid
/// * `None` - If the key is not a selection key or the index is invalid
///
/// # Requirements
/// - 1.1-1.5: Alt+1-5 selects corresponding suggestion
/// - 2.1-2.4: Invalid selections are ignored
/// Handle navigation keybindings (Alt+Up/Down and Alt+j/k)
///
/// Parses Alt+Up/Down and Alt+j/k keybindings and updates the selection state.
/// Navigation stops at boundaries (no wrap-around).
///
/// # Arguments
/// * `key` - The key event to handle
/// * `selection_state` - The selection state to update
/// * `suggestion_count` - Number of available suggestions
///
/// # Returns
/// * `true` - If the key was handled (Alt+Up/Down or Alt+j/k)
/// * `false` - If the key was not a navigation key
///
/// # Requirements
/// - 8.1: Alt+Down/j moves selection to next suggestion
/// - 8.2: Alt+Up/k moves selection to previous suggestion
/// Handle Enter key for applying navigated selection
///
/// Checks if navigation mode is active (user has used Alt+Up/Down/j/k) and
/// returns the selected index if so. This allows Enter to apply the
/// currently highlighted suggestion.
///
/// # Arguments
/// * `key` - The key event to handle
/// * `selection_state` - The selection state to check
///
/// # Returns
/// * `Some(index)` - The 0-based index of the selected suggestion if navigation is active
/// * `None` - If Enter was not pressed or no suggestion is selected via navigation
///
/// # Requirements
/// - 9.1: Enter applies the highlighted suggestion when navigation is active
/// - 9.2: Does not interfere with normal Enter behavior when no navigation
/// - 9.3: Clears selection highlight after application (caller responsibility)
/// - 9.4: Does not interfere when popup has no suggestions