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
use crate::{KeyBinding, KeyBindingContextPredicate, Keystroke, NoAction};
use collections::HashSet;
use smallvec::SmallVec;
use std::{
    any::{Any, TypeId},
    collections::HashMap,
};

#[derive(Copy, Clone, Eq, PartialEq, Default)]
pub struct KeymapVersion(usize);

#[derive(Default)]
pub struct Keymap {
    bindings: Vec<KeyBinding>,
    binding_indices_by_action_id: HashMap<TypeId, SmallVec<[usize; 3]>>,
    disabled_keystrokes:
        HashMap<SmallVec<[Keystroke; 2]>, HashSet<Option<KeyBindingContextPredicate>>>,
    version: KeymapVersion,
}

impl Keymap {
    pub fn new(bindings: Vec<KeyBinding>) -> Self {
        let mut this = Self::default();
        this.add_bindings(bindings);
        this
    }

    pub fn version(&self) -> KeymapVersion {
        self.version
    }

    pub fn bindings_for_action(&self, action_id: TypeId) -> impl Iterator<Item = &'_ KeyBinding> {
        self.binding_indices_by_action_id
            .get(&action_id)
            .map(SmallVec::as_slice)
            .unwrap_or(&[])
            .iter()
            .map(|ix| &self.bindings[*ix])
            .filter(|binding| !self.binding_disabled(binding))
    }

    pub fn add_bindings<T: IntoIterator<Item = KeyBinding>>(&mut self, bindings: T) {
        let no_action_id = &(NoAction {}).type_id();
        let mut new_bindings = Vec::new();
        let mut has_new_disabled_keystrokes = false;
        for binding in bindings {
            if binding.action.type_id() == *no_action_id {
                has_new_disabled_keystrokes |= self
                    .disabled_keystrokes
                    .entry(binding.keystrokes)
                    .or_default()
                    .insert(binding.context_predicate);
            } else {
                new_bindings.push(binding);
            }
        }

        if has_new_disabled_keystrokes {
            self.binding_indices_by_action_id.retain(|_, indices| {
                indices.retain(|ix| {
                    let binding = &self.bindings[*ix];
                    match self.disabled_keystrokes.get(&binding.keystrokes) {
                        Some(disabled_predicates) => {
                            !disabled_predicates.contains(&binding.context_predicate)
                        }
                        None => true,
                    }
                });
                !indices.is_empty()
            });
        }

        for new_binding in new_bindings {
            if !self.binding_disabled(&new_binding) {
                self.binding_indices_by_action_id
                    .entry(new_binding.action().as_any().type_id())
                    .or_default()
                    .push(self.bindings.len());
                self.bindings.push(new_binding);
            }
        }

        self.version.0 += 1;
    }

    pub fn clear(&mut self) {
        self.bindings.clear();
        self.binding_indices_by_action_id.clear();
        self.disabled_keystrokes.clear();
        self.version.0 += 1;
    }

    pub fn bindings(&self) -> Vec<&KeyBinding> {
        self.bindings
            .iter()
            .filter(|binding| !self.binding_disabled(binding))
            .collect()
    }

    fn binding_disabled(&self, binding: &KeyBinding) -> bool {
        match self.disabled_keystrokes.get(&binding.keystrokes) {
            Some(disabled_predicates) => disabled_predicates.contains(&binding.context_predicate),
            None => false,
        }
    }
}

// #[cfg(test)]
// mod tests {
//     use crate::actions;

//     use super::*;

//     actions!(
//         keymap_test,
//         [Present1, Present2, Present3, Duplicate, Missing]
//     );

//     #[test]
//     fn regular_keymap() {
//         let present_1 = Binding::new("ctrl-q", Present1 {}, None);
//         let present_2 = Binding::new("ctrl-w", Present2 {}, Some("pane"));
//         let present_3 = Binding::new("ctrl-e", Present3 {}, Some("editor"));
//         let keystroke_duplicate_to_1 = Binding::new("ctrl-q", Duplicate {}, None);
//         let full_duplicate_to_2 = Binding::new("ctrl-w", Present2 {}, Some("pane"));
//         let missing = Binding::new("ctrl-r", Missing {}, None);
//         let all_bindings = [
//             &present_1,
//             &present_2,
//             &present_3,
//             &keystroke_duplicate_to_1,
//             &full_duplicate_to_2,
//             &missing,
//         ];

//         let mut keymap = Keymap::default();
//         assert_absent(&keymap, &all_bindings);
//         assert!(keymap.bindings().is_empty());

//         keymap.add_bindings([present_1.clone(), present_2.clone(), present_3.clone()]);
//         assert_absent(&keymap, &[&keystroke_duplicate_to_1, &missing]);
//         assert_present(
//             &keymap,
//             &[(&present_1, "q"), (&present_2, "w"), (&present_3, "e")],
//         );

//         keymap.add_bindings([
//             keystroke_duplicate_to_1.clone(),
//             full_duplicate_to_2.clone(),
//         ]);
//         assert_absent(&keymap, &[&missing]);
//         assert!(
//             !keymap.binding_disabled(&keystroke_duplicate_to_1),
//             "Duplicate binding 1 was added and should not be disabled"
//         );
//         assert!(
//             !keymap.binding_disabled(&full_duplicate_to_2),
//             "Duplicate binding 2 was added and should not be disabled"
//         );

//         assert_eq!(
//             keymap
//                 .bindings_for_action(keystroke_duplicate_to_1.action().id())
//                 .map(|binding| &binding.keystrokes)
//                 .flatten()
//                 .collect::<Vec<_>>(),
//             vec![&Keystroke {
//                 ctrl: true,
//                 alt: false,
//                 shift: false,
//                 cmd: false,
//                 function: false,
//                 key: "q".to_string(),
//                 ime_key: None,
//             }],
//             "{keystroke_duplicate_to_1:?} should have the expected keystroke in the keymap"
//         );
//         assert_eq!(
//             keymap
//                 .bindings_for_action(full_duplicate_to_2.action().id())
//                 .map(|binding| &binding.keystrokes)
//                 .flatten()
//                 .collect::<Vec<_>>(),
//             vec![
//                 &Keystroke {
//                     ctrl: true,
//                     alt: false,
//                     shift: false,
//                     cmd: false,
//                     function: false,
//                     key: "w".to_string(),
//                     ime_key: None,
//                 },
//                 &Keystroke {
//                     ctrl: true,
//                     alt: false,
//                     shift: false,
//                     cmd: false,
//                     function: false,
//                     key: "w".to_string(),
//                     ime_key: None,
//                 }
//             ],
//             "{full_duplicate_to_2:?} should have a duplicated keystroke in the keymap"
//         );

//         let updated_bindings = keymap.bindings();
//         let expected_updated_bindings = vec![
//             &present_1,
//             &present_2,
//             &present_3,
//             &keystroke_duplicate_to_1,
//             &full_duplicate_to_2,
//         ];
//         assert_eq!(
//             updated_bindings.len(),
//             expected_updated_bindings.len(),
//             "Unexpected updated keymap bindings {updated_bindings:?}"
//         );
//         for (i, expected) in expected_updated_bindings.iter().enumerate() {
//             let keymap_binding = &updated_bindings[i];
//             assert_eq!(
//                 keymap_binding.context_predicate, expected.context_predicate,
//                 "Unexpected context predicate for keymap {i} element: {keymap_binding:?}"
//             );
//             assert_eq!(
//                 keymap_binding.keystrokes, expected.keystrokes,
//                 "Unexpected keystrokes for keymap {i} element: {keymap_binding:?}"
//             );
//         }

//         keymap.clear();
//         assert_absent(&keymap, &all_bindings);
//         assert!(keymap.bindings().is_empty());
//     }

//     #[test]
//     fn keymap_with_ignored() {
//         let present_1 = Binding::new("ctrl-q", Present1 {}, None);
//         let present_2 = Binding::new("ctrl-w", Present2 {}, Some("pane"));
//         let present_3 = Binding::new("ctrl-e", Present3 {}, Some("editor"));
//         let keystroke_duplicate_to_1 = Binding::new("ctrl-q", Duplicate {}, None);
//         let full_duplicate_to_2 = Binding::new("ctrl-w", Present2 {}, Some("pane"));
//         let ignored_1 = Binding::new("ctrl-q", NoAction {}, None);
//         let ignored_2 = Binding::new("ctrl-w", NoAction {}, Some("pane"));
//         let ignored_3_with_other_context =
//             Binding::new("ctrl-e", NoAction {}, Some("other_context"));

//         let mut keymap = Keymap::default();

//         keymap.add_bindings([
//             ignored_1.clone(),
//             ignored_2.clone(),
//             ignored_3_with_other_context.clone(),
//         ]);
//         assert_absent(&keymap, &[&present_3]);
//         assert_disabled(
//             &keymap,
//             &[
//                 &present_1,
//                 &present_2,
//                 &ignored_1,
//                 &ignored_2,
//                 &ignored_3_with_other_context,
//             ],
//         );
//         assert!(keymap.bindings().is_empty());
//         keymap.clear();

//         keymap.add_bindings([
//             present_1.clone(),
//             present_2.clone(),
//             present_3.clone(),
//             ignored_1.clone(),
//             ignored_2.clone(),
//             ignored_3_with_other_context.clone(),
//         ]);
//         assert_present(&keymap, &[(&present_3, "e")]);
//         assert_disabled(
//             &keymap,
//             &[
//                 &present_1,
//                 &present_2,
//                 &ignored_1,
//                 &ignored_2,
//                 &ignored_3_with_other_context,
//             ],
//         );
//         keymap.clear();

//         keymap.add_bindings([
//             present_1.clone(),
//             present_2.clone(),
//             present_3.clone(),
//             ignored_1.clone(),
//         ]);
//         assert_present(&keymap, &[(&present_2, "w"), (&present_3, "e")]);
//         assert_disabled(&keymap, &[&present_1, &ignored_1]);
//         assert_absent(&keymap, &[&ignored_2, &ignored_3_with_other_context]);
//         keymap.clear();

//         keymap.add_bindings([
//             present_1.clone(),
//             present_2.clone(),
//             present_3.clone(),
//             keystroke_duplicate_to_1.clone(),
//             full_duplicate_to_2.clone(),
//             ignored_1.clone(),
//             ignored_2.clone(),
//             ignored_3_with_other_context.clone(),
//         ]);
//         assert_present(&keymap, &[(&present_3, "e")]);
//         assert_disabled(
//             &keymap,
//             &[
//                 &present_1,
//                 &present_2,
//                 &keystroke_duplicate_to_1,
//                 &full_duplicate_to_2,
//                 &ignored_1,
//                 &ignored_2,
//                 &ignored_3_with_other_context,
//             ],
//         );
//         keymap.clear();
//     }

//     #[track_caller]
//     fn assert_present(keymap: &Keymap, expected_bindings: &[(&Binding, &str)]) {
//         let keymap_bindings = keymap.bindings();
//         assert_eq!(
//             expected_bindings.len(),
//             keymap_bindings.len(),
//             "Unexpected keymap bindings {keymap_bindings:?}"
//         );
//         for (i, (expected, expected_key)) in expected_bindings.iter().enumerate() {
//             assert!(
//                 !keymap.binding_disabled(expected),
//                 "{expected:?} should not be disabled as it was added into keymap for element {i}"
//             );
//             assert_eq!(
//                 keymap
//                     .bindings_for_action(expected.action().id())
//                     .map(|binding| &binding.keystrokes)
//                     .flatten()
//                     .collect::<Vec<_>>(),
//                 vec![&Keystroke {
//                     ctrl: true,
//                     alt: false,
//                     shift: false,
//                     cmd: false,
//                     function: false,
//                     key: expected_key.to_string(),
//                     ime_key: None,
//                 }],
//                 "{expected:?} should have the expected keystroke with key '{expected_key}' in the keymap for element {i}"
//             );

//             let keymap_binding = &keymap_bindings[i];
//             assert_eq!(
//                 keymap_binding.context_predicate, expected.context_predicate,
//                 "Unexpected context predicate for keymap {i} element: {keymap_binding:?}"
//             );
//             assert_eq!(
//                 keymap_binding.keystrokes, expected.keystrokes,
//                 "Unexpected keystrokes for keymap {i} element: {keymap_binding:?}"
//             );
//         }
//     }

//     #[track_caller]
//     fn assert_absent(keymap: &Keymap, bindings: &[&Binding]) {
//         for binding in bindings.iter() {
//             assert!(
//                 !keymap.binding_disabled(binding),
//                 "{binding:?} should not be disabled in the keymap where was not added"
//             );
//             assert_eq!(
//                 keymap.bindings_for_action(binding.action().id()).count(),
//                 0,
//                 "{binding:?} should have no actions in the keymap where was not added"
//             );
//         }
//     }

//     #[track_caller]
//     fn assert_disabled(keymap: &Keymap, bindings: &[&Binding]) {
//         for binding in bindings.iter() {
//             assert!(
//                 keymap.binding_disabled(binding),
//                 "{binding:?} should be disabled in the keymap"
//             );
//             assert_eq!(
//                 keymap.bindings_for_action(binding.action().id()).count(),
//                 0,
//                 "{binding:?} should have no actions in the keymap where it was disabled"
//             );
//         }
//     }
// }