concord 2.1.6

A terminal user interface client for Discord
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
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
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
use std::ops::Range;

use crate::discord::ids::{
    Id,
    marker::{ChannelMarker, MessageMarker},
};
use crate::discord::{
    APPLICATION_COMMAND_CHANNEL_KIND, APPLICATION_COMMAND_MENTIONABLE_KIND,
    APPLICATION_COMMAND_ROLE_KIND, APPLICATION_COMMAND_USER_KIND, ApplicationCommandInfo,
    ApplicationCommandInvocation, MAX_UPLOAD_ATTACHMENT_COUNT, MessageAttachmentUpload,
    application_command_content_is_complete, application_command_option_scope,
    parsed_application_command_option_names,
};

use super::super::{
    ActiveModalPopupKind, CommandPickerEntry, DashboardState, EmojiPickerEntry, FocusPane,
    MentionPickerEntry,
};
use super::completions::{
    ComposerEmojiImageCompletion, EmojiCompletion, MentionCompletion,
    build_channel_mention_candidates, build_command_candidates, build_command_choice_candidates,
    build_command_option_candidates, build_emoji_candidates, build_mention_candidates,
    expand_composer_completions, expand_emoji_shortcodes, is_command_query_char,
    is_emoji_query_char, is_mention_query_char, move_picker_selection,
    should_start_completion_query,
};
use crate::discord::AppCommand;

#[derive(Debug, Default)]
pub(in crate::tui::state) struct ComposerUiState {
    pub(in crate::tui::state) composer_input: String,
    pub(in crate::tui::state) composer_cursor_byte_index: usize,
    pub(in crate::tui::state) pending_composer_attachments: Vec<MessageAttachmentUpload>,
    pub(in crate::tui::state) composer_active: bool,
    pub(in crate::tui::state) reply_target_message_id: Option<Id<MessageMarker>>,
    pub(in crate::tui::state) edit_target_message: Option<(Id<ChannelMarker>, Id<MessageMarker>)>,
    /// Set when the user is in the middle of an `@mention` autocomplete. The
    /// stored string is the characters typed *after* the `@` and is used to
    /// filter the candidate list. `None` means the picker is closed.
    pub(in crate::tui::state) composer_mention_query: Option<String>,
    pub(in crate::tui::state) composer_mention_start: Option<usize>,
    pub(in crate::tui::state) composer_mention_selected: usize,
    /// Set when the user is typing a Unicode emoji shortcode after `:`. The
    /// picker opens after two shortcode characters, mirroring Discord's
    /// threshold while avoiding noisy popups for ordinary punctuation.
    pub(in crate::tui::state) composer_emoji_query: Option<String>,
    pub(in crate::tui::state) composer_emoji_start: Option<usize>,
    pub(in crate::tui::state) composer_emoji_selected: usize,
    pub(in crate::tui::state) composer_emoji_candidates: Vec<EmojiPickerEntry>,
    pub(in crate::tui::state) composer_command_query: Option<String>,
    pub(in crate::tui::state) composer_command_start: Option<usize>,
    pub(in crate::tui::state) composer_command_selected: usize,
    pub(in crate::tui::state) composer_command_candidates: Vec<CommandPickerEntry>,
    /// Records `@displayname` substrings that the picker inserted, so the
    /// composer can rewrite them to Discord's `<@USER_ID>` wire format on
    /// submit even though the visible text is still the friendly form.
    pub(in crate::tui::state) composer_mention_completions: Vec<MentionCompletion>,
    /// Recorded custom emoji ranges inserted by the picker. The editor keeps
    /// the readable `:name:` text while submit rewrites these ranges to
    /// Discord's `<:name:id>` or `<a:name:id>` wire format.
    pub(in crate::tui::state) composer_emoji_completions: Vec<EmojiCompletion>,
}

impl ComposerUiState {
    fn composer_cursor_byte_index(&self) -> usize {
        let mut index = self
            .composer_cursor_byte_index
            .min(self.composer_input.len());
        while index > 0 && !self.composer_input.is_char_boundary(index) {
            index -= 1;
        }
        index
    }
}

impl DashboardState {
    pub fn is_composing(&self) -> bool {
        self.composer.composer_active
    }

    pub(in crate::tui::state) fn start_reply_composer(&mut self) {
        let Some(message_id) = self.selected_message_state().map(|message| message.id) else {
            return;
        };
        // Replies are sends, so the channel must allow SEND_MESSAGES for the
        // action to be useful.
        if !self.can_send_in_selected_channel() {
            return;
        }
        self.composer.composer_input.clear();
        self.composer.composer_cursor_byte_index = 0;
        self.composer.pending_composer_attachments.clear();
        self.runtime.clipboard_paste_pending = false;
        self.composer.reply_target_message_id = Some(message_id);
        self.composer.edit_target_message = None;
        self.reset_mention_picker_state();
        self.composer.composer_active = true;
        self.navigation.focus = FocusPane::Messages;
    }

    pub(in crate::tui::state) fn start_edit_composer(&mut self) {
        let Some(message) = self.selected_message_state() else {
            return;
        };
        if Some(message.author_id) != self.discord.current_user_id
            || !message.message_kind.is_regular_or_reply()
        {
            return;
        }
        let Some(content) = message.content.clone() else {
            return;
        };
        let channel_id = message.channel_id;
        let message_id = message.id;
        self.composer.composer_input = content;
        self.composer.composer_cursor_byte_index = self.composer.composer_input.len();
        self.composer.pending_composer_attachments.clear();
        self.runtime.clipboard_paste_pending = false;
        self.composer.reply_target_message_id = None;
        self.composer.edit_target_message = Some((channel_id, message_id));
        self.reset_mention_picker_state();
        self.composer.composer_active = true;
        self.navigation.focus = FocusPane::Messages;
    }

    pub fn composer_input(&self) -> &str {
        &self.composer.composer_input
    }

    pub fn composer_cursor_byte_index(&self) -> usize {
        clamp_cursor_index(
            &self.composer.composer_input,
            self.composer.composer_cursor_byte_index,
        )
    }

    pub fn pending_composer_attachments(&self) -> &[MessageAttachmentUpload] {
        &self.composer.pending_composer_attachments
    }

    pub fn composer_title(&self) -> &'static str {
        if self.composer.edit_target_message.is_some() {
            " Edit Message "
        } else if self.composer.reply_target_message_id.is_some() {
            " Reply "
        } else {
            " Message Input "
        }
    }

    pub fn add_pending_composer_attachments(&mut self, attachments: Vec<MessageAttachmentUpload>) {
        if attachments.is_empty() || !self.composer_accepts_attachments() {
            return;
        }
        let available = MAX_UPLOAD_ATTACHMENT_COUNT
            .saturating_sub(self.composer.pending_composer_attachments.len());
        self.composer
            .pending_composer_attachments
            .extend(attachments.into_iter().take(available));
    }

    pub fn pop_pending_composer_attachment(&mut self) {
        self.composer.pending_composer_attachments.pop();
    }

    pub fn composer_accepts_attachments(&self) -> bool {
        self.composer.edit_target_message.is_none() && self.can_attach_in_selected_channel()
    }

    /// Whether the user can post messages in the currently selected channel.
    /// Returns `true` when no channel is selected so callers don't have to
    /// special-case the empty state.
    pub fn can_send_in_selected_channel(&self) -> bool {
        match self.selected_channel_state() {
            Some(channel) if channel.is_forum() => false,
            Some(channel) => self.discord.cache.can_send_in_channel(channel),
            None => true,
        }
    }

    /// Whether the user can attach files in the currently selected channel.
    /// Paste-based attachment input uses this to decide whether file paths
    /// become pending uploads or plain composer text.
    pub fn can_attach_in_selected_channel(&self) -> bool {
        match self.selected_channel_state() {
            Some(channel) if channel.is_forum() => false,
            Some(channel) => self.discord.cache.can_attach_in_channel(channel),
            None => true,
        }
    }

    pub fn start_composer(&mut self) {
        if self.selected_channel_id().is_none() {
            return;
        }
        // Refusing here keeps the shortcut simple: the same key that opens the
        // composer in writable channels just no-ops in read-only ones, so the
        // user never lands in a typing state for a channel that would 403 on
        // submit.
        if !self.can_send_in_selected_channel() {
            return;
        }
        self.composer.reply_target_message_id = None;
        self.composer.edit_target_message = None;
        self.composer.composer_active = true;
        self.queue_application_commands_for_selected_channel();
        self.move_composer_cursor_end();
        self.navigation.focus = FocusPane::Messages;
    }

    pub fn replace_composer_input_from_editor(&mut self, value: String) {
        self.composer.composer_input = value;
        self.composer.composer_cursor_byte_index = self.composer.composer_input.len();
        self.reset_mention_picker_state();
        self.refresh_active_mention_query();
    }

    pub fn cancel_composer(&mut self) {
        self.composer.composer_active = false;
        self.composer.composer_input.clear();
        self.composer.composer_cursor_byte_index = 0;
        self.composer.pending_composer_attachments.clear();
        self.runtime.clipboard_paste_pending = false;
        self.composer.reply_target_message_id = None;
        self.composer.edit_target_message = None;
        self.reset_mention_picker_state();
    }

    pub fn close_composer(&mut self) {
        if self.composer.reply_target_message_id.is_some()
            || self.composer.edit_target_message.is_some()
        {
            self.cancel_composer();
            return;
        }
        self.composer.composer_active = false;
        self.runtime.clipboard_paste_pending = false;
        self.reset_mention_picker_state();
    }

    pub fn clear_composer_input(&mut self) {
        self.composer.composer_input.clear();
        self.composer.composer_cursor_byte_index = 0;
        self.composer.pending_composer_attachments.clear();
        self.runtime.clipboard_paste_pending = false;
        self.reset_mention_picker_state();
    }

    pub fn push_composer_char(&mut self, value: char) {
        let mut text = String::new();
        text.push(value);
        self.insert_composer_text_at_cursor(&text);
    }

    pub fn insert_composer_text_at_cursor(&mut self, value: &str) {
        if value.is_empty() {
            return;
        }
        let cursor = self.composer.composer_cursor_byte_index();
        self.replace_composer_range(cursor..cursor, value);
    }

    pub fn open_composer_reaction_picker_from_plus_colon(&mut self) -> bool {
        let cursor = self.composer.composer_cursor_byte_index();
        if !composer_plus_colon_trigger_before_cursor(&self.composer.composer_input, cursor) {
            return false;
        }

        self.open_emoji_reaction_picker();
        if !self.is_active_modal_popup(ActiveModalPopupKind::EmojiReactionPicker) {
            return false;
        }

        let plus_start = cursor - '+'.len_utf8();
        self.replace_composer_range(plus_start..cursor, "");
        true
    }

    pub fn pop_composer_char(&mut self) {
        let end = self.composer.composer_cursor_byte_index();
        if end == 0 {
            return;
        }
        let start = previous_char_boundary(&self.composer.composer_input, end);
        self.replace_composer_range(start..end, "");
    }

    pub fn delete_previous_composer_word(&mut self) {
        let end = self.composer.composer_cursor_byte_index();
        if end == 0 {
            return;
        }
        let start = previous_word_boundary(&self.composer.composer_input, end);
        self.replace_composer_range(start..end, "");
    }

    pub fn move_composer_cursor_left(&mut self) {
        let cursor = self.composer.composer_cursor_byte_index();
        self.composer.composer_cursor_byte_index =
            previous_char_boundary(&self.composer.composer_input, cursor);
        self.refresh_active_mention_query();
    }

    pub fn move_composer_cursor_right(&mut self) {
        let cursor = self.composer.composer_cursor_byte_index();
        self.composer.composer_cursor_byte_index =
            next_char_boundary(&self.composer.composer_input, cursor);
        self.refresh_active_mention_query();
    }

    pub fn move_composer_cursor_up(&mut self) {
        let cursor = self.composer.composer_cursor_byte_index();
        if let Some(target) = vertical_cursor_target(&self.composer.composer_input, cursor, -1) {
            self.composer.composer_cursor_byte_index = target;
            self.refresh_active_mention_query();
        }
    }

    pub fn move_composer_cursor_down(&mut self) {
        let cursor = self.composer.composer_cursor_byte_index();
        if let Some(target) = vertical_cursor_target(&self.composer.composer_input, cursor, 1) {
            self.composer.composer_cursor_byte_index = target;
            self.refresh_active_mention_query();
        }
    }

    pub fn move_composer_cursor_word_left(&mut self) {
        let cursor = self.composer.composer_cursor_byte_index();
        self.composer.composer_cursor_byte_index =
            previous_word_boundary(&self.composer.composer_input, cursor);
        self.refresh_active_mention_query();
    }

    pub fn move_composer_cursor_word_right(&mut self) {
        let cursor = self.composer.composer_cursor_byte_index();
        self.composer.composer_cursor_byte_index =
            next_word_boundary(&self.composer.composer_input, cursor);
        self.refresh_active_mention_query();
    }

    pub fn move_composer_cursor_home(&mut self) {
        self.composer.composer_cursor_byte_index = 0;
        self.refresh_active_mention_query();
    }

    pub fn move_composer_cursor_end(&mut self) {
        self.composer.composer_cursor_byte_index = self.composer.composer_input.len();
        self.refresh_active_mention_query();
    }

    pub fn submit_composer(&mut self) -> Option<AppCommand> {
        let expanded = expand_composer_completions(
            &self.composer.composer_input,
            &self.composer.composer_mention_completions,
            &self.composer.composer_emoji_completions,
        );
        let expanded = expand_emoji_shortcodes(&expanded);
        let content = expanded.trim().to_owned();
        let has_attachments = !self.composer.pending_composer_attachments.is_empty();
        if content.is_empty() && !has_attachments {
            return None;
        }
        if let Some((channel_id, message_id)) = self.composer.edit_target_message.take() {
            if content.is_empty() {
                self.composer.edit_target_message = Some((channel_id, message_id));
                return None;
            }
            self.cancel_composer();
            return Some(AppCommand::EditMessage {
                channel_id,
                message_id,
                content,
            });
        }
        let channel_id = self.selected_channel_id()?;
        // Defense in depth: the channel could have lost SEND_MESSAGES while
        // the composer was open (role change, channel overwrite update). Drop
        // the message rather than fire a request that would 403.
        if !self.can_send_in_selected_channel() {
            self.cancel_composer();
            return None;
        }
        if has_attachments && !self.can_attach_in_selected_channel() {
            self.cancel_composer();
            return None;
        }

        if !has_attachments && self.composer.reply_target_message_id.is_none() {
            match self.application_command_submit_for_content(&content) {
                ApplicationCommandSubmit::Ready(interaction) => {
                    self.clear_submitted_composer_text();
                    self.composer.reply_target_message_id = None;
                    self.composer.pending_composer_attachments.clear();
                    return Some(AppCommand::RunApplicationCommand {
                        invocation: interaction,
                    });
                }
                ApplicationCommandSubmit::Incomplete => return None,
                ApplicationCommandSubmit::NotCommand => {}
            }
        }

        self.clear_submitted_composer_text();
        let reply_to = self.composer.reply_target_message_id.take();
        let attachments = std::mem::take(&mut self.composer.pending_composer_attachments);
        // Stay in insert mode so the user can send several messages in a
        // row without re-pressing `i`. The composer closes only when the
        // user explicitly bails with Esc or the channel revokes
        // SEND_MESSAGES (handled above).
        Some(AppCommand::SendMessage {
            channel_id,
            content,
            reply_to,
            attachments,
        })
    }

    fn clear_submitted_composer_text(&mut self) {
        self.composer.composer_input.clear();
        self.composer.composer_cursor_byte_index = 0;
        self.reset_mention_picker_state();
    }

    /// Returns the characters typed after the `@` if the picker is open.
    pub fn composer_mention_query(&self) -> Option<&str> {
        self.composer.composer_mention_query.as_deref()
    }

    pub fn composer_mention_selected(&self) -> usize {
        self.composer.composer_mention_selected
    }

    /// Builds the full suggestion list for the picker, ordered by best match
    /// across the member's display name AND username: prefix matches beat
    /// substring matches, alias matches beat username matches at the same rank,
    /// and ties are broken alphabetically by display name.
    pub fn composer_mention_candidates(&self) -> Vec<MentionPickerEntry> {
        let Some(query) = self.composer.composer_mention_query.as_deref() else {
            return Vec::new();
        };
        if self.active_composer_mention_trigger() == Some('#') {
            build_channel_mention_candidates(query, self.composer_channel_candidates())
        } else {
            build_mention_candidates(
                query,
                self.flattened_members(),
                self.composer_role_candidates(),
            )
        }
    }

    fn active_composer_mention_trigger(&self) -> Option<char> {
        let start = self.composer.composer_mention_start?;
        self.composer.composer_input[start..]
            .chars()
            .next()
            .filter(|value| matches!(value, '@' | '#'))
    }

    fn selected_composer_guild_id(&self) -> Option<Id<crate::discord::ids::marker::GuildMarker>> {
        self.selected_channel_state()
            .and_then(|channel| channel.guild_id)
            .or_else(|| self.selected_guild_id())
    }

    fn composer_role_candidates(&self) -> Vec<&crate::discord::RoleState> {
        self.selected_composer_guild_id()
            .map(|guild_id| self.discord.cache.roles_for_guild(guild_id))
            .unwrap_or_default()
    }

    fn composer_channel_candidates(&self) -> Vec<&crate::discord::ChannelState> {
        self.discord
            .cache
            .viewable_channels_for_guild(self.selected_composer_guild_id())
    }

    pub fn move_composer_mention_selection(&mut self, delta: isize) {
        if self.composer.composer_mention_query.is_none() {
            return;
        }
        let len = self.composer_mention_candidates().len();
        self.composer.composer_mention_selected =
            move_picker_selection(self.composer.composer_mention_selected, len, delta);
    }

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

    pub fn composer_emoji_selected(&self) -> usize {
        self.composer.composer_emoji_selected
    }

    pub fn composer_emoji_candidates(&self) -> Vec<EmojiPickerEntry> {
        self.composer.composer_emoji_candidates.clone()
    }

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

    pub fn composer_command_selected(&self) -> usize {
        self.composer.composer_command_selected
    }

    pub fn composer_command_candidates(&self) -> Vec<CommandPickerEntry> {
        self.composer.composer_command_candidates.clone()
    }

    pub(in crate::tui) fn composer_command_can_submit(&self) -> bool {
        let expanded = expand_composer_completions(
            &self.composer.composer_input,
            &self.composer.composer_mention_completions,
            &self.composer.composer_emoji_completions,
        );
        let expanded = expand_emoji_shortcodes(&expanded);
        matches!(
            self.application_command_submit_for_content(expanded.trim()),
            ApplicationCommandSubmit::Ready(_)
        )
    }

    pub fn move_composer_command_selection(&mut self, delta: isize) {
        if self.composer.composer_command_query.is_none() {
            return;
        }
        let len = self.composer.composer_command_candidates.len();
        self.composer.composer_command_selected =
            move_picker_selection(self.composer.composer_command_selected, len, delta);
    }

    pub fn move_composer_emoji_selection(&mut self, delta: isize) {
        if self.composer.composer_emoji_query.is_none() {
            return;
        }
        let len = self.composer.composer_emoji_candidates.len();
        self.composer.composer_emoji_selected =
            move_picker_selection(self.composer.composer_emoji_selected, len, delta);
    }

    /// Confirms the currently highlighted mention. Replaces the trailing
    /// `@query` with `@displayname ` (so the user sees what they wrote) and
    /// records the byte range so `submit_composer` can rewrite it to
    /// `<@USER_ID>` later. Returns `false` when the picker has no candidate
    /// to apply.
    pub fn confirm_composer_mention(&mut self) -> bool {
        let Some(_query) = self.composer.composer_mention_query.clone() else {
            return false;
        };
        let Some(mention_start) = self.composer.composer_mention_start else {
            return false;
        };
        let candidates = self.composer_mention_candidates();
        let Some(entry) = candidates.get(self.composer.composer_mention_selected) else {
            return false;
        };
        let entry = entry.clone();

        let cursor = self.composer.composer_cursor_byte_index();
        if mention_start > cursor {
            return false;
        }

        let replacement = format!("{} ", entry.visible_text());
        self.replace_composer_range(mention_start..cursor, &replacement);
        let end = mention_start + entry.visible_text().len();

        self.composer
            .composer_mention_completions
            .push(MentionCompletion {
                byte_start: mention_start,
                byte_end: end,
                target: entry.target,
            });
        self.close_composer_mention_query();
        true
    }

    /// Confirms the highlighted emoji. Unicode emoji are inserted directly.
    /// available custom emoji keep their readable `:name:` form and record a
    /// byte range so submit can send Discord's wire markup. Unavailable custom
    /// emoji stay visible in the picker as a hint, but cannot be confirmed.
    pub fn confirm_composer_emoji(&mut self) -> bool {
        let Some(_query) = self.composer.composer_emoji_query.clone() else {
            return false;
        };
        let Some(emoji_start) = self.composer.composer_emoji_start else {
            return false;
        };
        let Some(entry) = self
            .composer
            .composer_emoji_candidates
            .get(self.composer.composer_emoji_selected)
        else {
            return false;
        };
        let entry = entry.clone();
        if !entry.available {
            return false;
        }

        let cursor = self.composer.composer_cursor_byte_index();
        if emoji_start > cursor {
            return false;
        }

        let replacement = if entry.wire_format.is_some() {
            format!(":{}: ", entry.shortcode)
        } else {
            format!("{} ", entry.emoji)
        };
        self.replace_composer_range(emoji_start..cursor, &replacement);
        if let Some(wire_format) = entry.wire_format {
            let end = emoji_start + ':'.len_utf8() + entry.shortcode.len() + ':'.len_utf8();
            self.composer
                .composer_emoji_completions
                .push(EmojiCompletion {
                    byte_start: emoji_start,
                    byte_end: end,
                    replacement: wire_format,
                    custom_image_url: entry.custom_image_url,
                });
        }
        self.close_composer_emoji_query();
        true
    }

    pub fn confirm_composer_command(&mut self) -> bool {
        let Some(command_start) = self.composer.composer_command_start else {
            return false;
        };
        let Some(entry) = self
            .composer
            .composer_command_candidates
            .get(self.composer.composer_command_selected)
            .cloned()
        else {
            return false;
        };
        let cursor = self.composer_cursor_byte_index();
        if command_start > cursor {
            return false;
        }

        self.replace_composer_range(command_start..cursor, &entry.replacement);
        self.close_composer_command_query();
        self.refresh_active_mention_query();
        true
    }

    pub(in crate::tui) fn composer_emoji_image_completions(
        &self,
    ) -> Vec<ComposerEmojiImageCompletion> {
        self.composer
            .composer_emoji_completions
            .iter()
            .filter(|completion| completion.byte_end <= self.composer.composer_input.len())
            .filter_map(|completion| {
                completion
                    .custom_image_url
                    .as_ref()
                    .map(|url| ComposerEmojiImageCompletion {
                        byte_start: completion.byte_start,
                        byte_end: completion.byte_end,
                        url: url.clone(),
                    })
            })
            .collect()
    }

    /// Closes the picker without inserting anything. The literal `@query`
    /// stays in the composer.
    pub fn cancel_composer_mention(&mut self) {
        self.close_composer_mention_query();
    }

    pub fn cancel_composer_emoji(&mut self) {
        self.close_composer_emoji_query();
    }

    pub fn cancel_composer_command(&mut self) {
        self.close_composer_command_query();
    }

    fn reset_mention_picker_state(&mut self) {
        self.close_composer_mention_query();
        self.close_composer_emoji_query();
        self.close_composer_command_query();
        self.composer.composer_mention_completions.clear();
        self.composer.composer_emoji_completions.clear();
    }

    fn close_composer_mention_query(&mut self) {
        self.composer.composer_mention_query = None;
        self.composer.composer_mention_start = None;
        self.composer.composer_mention_selected = 0;
    }

    fn close_composer_emoji_query(&mut self) {
        self.composer.composer_emoji_query = None;
        self.composer.composer_emoji_start = None;
        self.composer.composer_emoji_selected = 0;
        self.composer.composer_emoji_candidates.clear();
    }

    fn close_composer_command_query(&mut self) {
        self.composer.composer_command_query = None;
        self.composer.composer_command_start = None;
        self.composer.composer_command_selected = 0;
        self.composer.composer_command_candidates.clear();
    }

    pub(in crate::tui::state) fn refresh_composer_emoji_candidates_for_current_query(&mut self) {
        let Some(query) = self.composer.composer_emoji_query.clone() else {
            self.composer.composer_emoji_candidates.clear();
            return;
        };

        let candidates = self.emoji_candidates_for_query(&query);
        if candidates.is_empty() {
            self.close_composer_emoji_query();
            return;
        }

        self.composer.composer_emoji_selected = self
            .composer
            .composer_emoji_selected
            .min(candidates.len() - 1);
        self.composer.composer_emoji_candidates = candidates;
    }

    fn replace_composer_range(&mut self, range: Range<usize>, replacement: &str) {
        if range.start > range.end
            || range.end > self.composer.composer_input.len()
            || !self.composer.composer_input.is_char_boundary(range.start)
            || !self.composer.composer_input.is_char_boundary(range.end)
        {
            return;
        }
        self.adjust_mention_completions_for_replace(range.clone(), replacement.len());
        self.adjust_emoji_completions_for_replace(range.clone(), replacement.len());
        self.composer
            .composer_input
            .replace_range(range.clone(), replacement);
        self.composer.composer_cursor_byte_index = range.start + replacement.len();
        self.refresh_active_mention_query();
    }

    pub(in crate::tui::state) fn refresh_active_mention_query(&mut self) {
        let cursor = self.composer.composer_cursor_byte_index();
        let mut query_start = cursor;

        while query_start > 0 {
            let previous = previous_char_boundary(&self.composer.composer_input, query_start);
            let value = self.composer.composer_input[previous..query_start]
                .chars()
                .next()
                .expect("character boundary slice contains one character");
            if !is_mention_query_char(value) {
                break;
            }
            query_start = previous;
        }

        if query_start > 0 {
            let mention_start = previous_char_boundary(&self.composer.composer_input, query_start);
            let trigger = &self.composer.composer_input[mention_start..query_start];
            if matches!(trigger, "@" | "#")
                && self.should_start_composer_mention_query(mention_start, trigger)
            {
                self.composer.composer_mention_query =
                    Some(self.composer.composer_input[query_start..cursor].to_owned());
                self.composer.composer_mention_start = Some(mention_start);
                self.composer.composer_mention_selected = 0;
                self.close_composer_emoji_query();
                self.close_composer_command_query();
                return;
            }
        }

        let mut query_start = cursor;

        while query_start > 0 {
            let previous = previous_char_boundary(&self.composer.composer_input, query_start);
            let value = self.composer.composer_input[previous..query_start]
                .chars()
                .next()
                .expect("character boundary slice contains one character");
            if !is_emoji_query_char(value) {
                break;
            }
            query_start = previous;
        }

        if query_start > 0 {
            let emoji_start = previous_char_boundary(&self.composer.composer_input, query_start);
            let query = &self.composer.composer_input[query_start..cursor];
            if &self.composer.composer_input[emoji_start..query_start] == ":"
                && query.chars().count() >= 2
                && should_start_completion_query(&self.composer.composer_input[..emoji_start])
            {
                let candidates = self.emoji_candidates_for_query(query);
                if candidates.is_empty() {
                    self.close_composer_mention_query();
                    self.close_composer_emoji_query();
                    return;
                }
                self.composer.composer_emoji_query = Some(query.to_owned());
                self.composer.composer_emoji_start = Some(emoji_start);
                self.composer.composer_emoji_selected = 0;
                self.composer.composer_emoji_candidates = candidates;
                self.close_composer_mention_query();
                self.close_composer_command_query();
                return;
            }
        }

        if let Some((query_start, candidates)) = self.command_completion_at_cursor() {
            if candidates.is_empty() {
                self.close_composer_command_query();
            } else {
                self.composer.composer_command_query =
                    Some(self.composer.composer_input[query_start..cursor].to_owned());
                self.composer.composer_command_start = Some(query_start);
                self.composer.composer_command_selected = self
                    .composer
                    .composer_command_selected
                    .min(candidates.len() - 1);
                self.composer.composer_command_candidates = candidates;
                self.close_composer_mention_query();
                self.close_composer_emoji_query();
                return;
            }
        }

        self.close_composer_mention_query();
        self.close_composer_emoji_query();
        self.close_composer_command_query();
    }

    fn should_start_composer_mention_query(&self, mention_start: usize, trigger: &str) -> bool {
        should_start_completion_query(&self.composer.composer_input[..mention_start])
            || self.command_option_accepts_mention_trigger(mention_start, trigger)
    }

    fn command_option_accepts_mention_trigger(&self, mention_start: usize, trigger: &str) -> bool {
        let before_marker = &self.composer.composer_input[..mention_start];
        let token_start = before_marker
            .rfind(char::is_whitespace)
            .map(|index| index + before_marker[index..].chars().next().unwrap().len_utf8())
            .unwrap_or(0);
        let token = &before_marker[token_start..];
        let Some((option_name, _)) = token.split_once(':') else {
            return false;
        };
        let Some(command) = self.application_command_for_input() else {
            return false;
        };
        let Some(option_scope) = application_command_option_scope(command, before_marker) else {
            return false;
        };
        let Some(option) = option_scope
            .iter()
            .find(|option| option.name == option_name)
        else {
            return false;
        };
        match trigger {
            "@" => matches!(
                option.kind,
                APPLICATION_COMMAND_USER_KIND
                    | APPLICATION_COMMAND_ROLE_KIND
                    | APPLICATION_COMMAND_MENTIONABLE_KIND
            ),
            "#" => option.kind == APPLICATION_COMMAND_CHANNEL_KIND,
            _ => false,
        }
    }

    fn queue_application_commands_for_selected_channel(&mut self) {
        let guild_id = self
            .selected_channel_state()
            .and_then(|channel| channel.guild_id);
        if self.discord.application_commands.contains_key(&guild_id) {
            return;
        }
        self.queue_application_command_load(guild_id);
    }

    fn command_completion_at_cursor(&mut self) -> Option<(usize, Vec<CommandPickerEntry>)> {
        if self.composer.composer_input.is_empty() || !self.composer.composer_input.starts_with('/')
        {
            return None;
        }
        self.queue_application_commands_for_selected_channel();

        let cursor = self.composer.composer_cursor_byte_index();
        if cursor == 0 || cursor > self.composer.composer_input.len() {
            return None;
        }
        let before_cursor = &self.composer.composer_input[..cursor];
        let token_start = before_cursor
            .rfind(char::is_whitespace)
            .map(|index| index + before_cursor[index..].chars().next().unwrap().len_utf8())
            .unwrap_or(0);
        let token = &self.composer.composer_input[token_start..cursor];
        let commands = self.application_commands_for_selected_channel();

        if token_start == 0 {
            let query = token.strip_prefix('/')?;
            if query.chars().all(is_command_query_char) {
                return Some((0, build_command_candidates(query, commands)));
            }
            return None;
        }

        let command = self.application_command_for_input()?;
        let option_scope = application_command_option_scope(command, before_cursor)?;
        if let Some((option_name, value_query)) = token.split_once(':') {
            let option = option_scope
                .iter()
                .find(|option| option.name == option_name)?;
            if !option.choices.is_empty() {
                return Some((
                    token_start + option_name.len() + ':'.len_utf8(),
                    build_command_choice_candidates(value_query, option),
                ));
            }
            let candidates = self.command_option_value_candidates(value_query, option);
            if !candidates.is_empty() {
                return Some((token_start + option_name.len() + ':'.len_utf8(), candidates));
            }
            return None;
        }

        if token.chars().all(is_command_query_char) {
            let used = parsed_application_command_option_names(
                &self.composer.composer_input,
                command,
                option_scope,
            );
            let options = option_scope
                .iter()
                .filter(|option| !used.contains(&option.name))
                .cloned()
                .collect::<Vec<_>>();
            return Some((
                token_start,
                build_command_option_candidates(token, &options),
            ));
        }

        None
    }

    fn command_option_value_candidates(
        &self,
        value_query: &str,
        option: &crate::discord::ApplicationCommandOptionInfo,
    ) -> Vec<CommandPickerEntry> {
        let query = value_query.trim_start_matches(['@', '#']);
        let mention_candidates = match option.kind {
            APPLICATION_COMMAND_USER_KIND => {
                build_mention_candidates(query, self.flattened_members(), Vec::new())
            }
            APPLICATION_COMMAND_ROLE_KIND => {
                build_mention_candidates(query, Vec::new(), self.composer_role_candidates())
            }
            APPLICATION_COMMAND_CHANNEL_KIND => {
                build_channel_mention_candidates(query, self.composer_channel_candidates())
            }
            APPLICATION_COMMAND_MENTIONABLE_KIND => build_mention_candidates(
                query,
                self.flattened_members(),
                self.composer_role_candidates(),
            ),
            _ => return Vec::new(),
        };

        mention_candidates
            .into_iter()
            .map(|entry| CommandPickerEntry {
                label: entry.visible_text(),
                detail: match entry.target {
                    super::completions::MentionPickerTarget::User(_) => entry
                        .username
                        .map(|username| format!("user @{username}"))
                        .unwrap_or_else(|| "user".to_owned()),
                    super::completions::MentionPickerTarget::Role(_) => "role".to_owned(),
                    super::completions::MentionPickerTarget::Channel(_) => "channel".to_owned(),
                },
                replacement: format!("{} ", entry.target.wire_format()),
            })
            .collect()
    }

    fn application_commands_for_selected_channel(&self) -> &[ApplicationCommandInfo] {
        let guild_id = self
            .selected_channel_state()
            .and_then(|channel| channel.guild_id);
        self.discord
            .application_commands
            .get(&guild_id)
            .map(Vec::as_slice)
            .unwrap_or(&[])
    }

    fn application_command_for_input(&self) -> Option<&ApplicationCommandInfo> {
        let name = self
            .composer
            .composer_input
            .strip_prefix('/')?
            .split_whitespace()
            .next()?;
        self.application_commands_for_selected_channel()
            .iter()
            .find(|command| command.name == name)
    }

    fn application_command_submit_for_content(&self, content: &str) -> ApplicationCommandSubmit {
        let Some(channel_id) = self.selected_channel_id() else {
            return ApplicationCommandSubmit::Incomplete;
        };
        let guild_id = self
            .selected_channel_state()
            .and_then(|channel| channel.guild_id);
        let Some(command_name) = content
            .strip_prefix('/')
            .and_then(|rest| rest.split_whitespace().next())
        else {
            return ApplicationCommandSubmit::NotCommand;
        };
        let Some(command) = self
            .discord
            .application_commands
            .get(&guild_id)
            .and_then(|commands| commands.iter().find(|command| command.name == command_name))
            .cloned()
        else {
            return ApplicationCommandSubmit::NotCommand;
        };
        if !application_command_content_is_complete(content, &command) {
            return ApplicationCommandSubmit::Incomplete;
        }
        ApplicationCommandSubmit::Ready(ApplicationCommandInvocation {
            guild_id,
            channel_id,
            command_name: command.name,
            content: content.to_owned(),
        })
    }

    fn adjust_mention_completions_for_replace(
        &mut self,
        replaced: Range<usize>,
        replacement_len: usize,
    ) {
        let replaced_len = replaced.end - replaced.start;
        let delta = replacement_len as isize - replaced_len as isize;
        let mut completions = Vec::with_capacity(self.composer.composer_mention_completions.len());

        for mut completion in self.composer.composer_mention_completions.drain(..) {
            if completion.byte_end <= replaced.start {
                completions.push(completion);
            } else if completion.byte_start >= replaced.end {
                completion.byte_start = shift_byte_index(completion.byte_start, delta);
                completion.byte_end = shift_byte_index(completion.byte_end, delta);
                completions.push(completion);
            }
        }

        self.composer.composer_mention_completions = completions;
    }

    fn adjust_emoji_completions_for_replace(
        &mut self,
        replaced: Range<usize>,
        replacement_len: usize,
    ) {
        let replaced_len = replaced.end - replaced.start;
        let delta = replacement_len as isize - replaced_len as isize;
        let mut completions = Vec::with_capacity(self.composer.composer_emoji_completions.len());

        for mut completion in self.composer.composer_emoji_completions.drain(..) {
            if completion.byte_end <= replaced.start {
                completions.push(completion);
            } else if completion.byte_start >= replaced.end {
                completion.byte_start = shift_byte_index(completion.byte_start, delta);
                completion.byte_end = shift_byte_index(completion.byte_end, delta);
                completions.push(completion);
            }
        }

        self.composer.composer_emoji_completions = completions;
    }

    fn emoji_candidates_for_query(&self, query: &str) -> Vec<EmojiPickerEntry> {
        let custom_emojis = self
            .selected_channel_guild_id()
            .map(|guild_id| self.discord.cache.custom_emojis_for_guild(guild_id))
            .unwrap_or_default();
        build_emoji_candidates(
            query,
            custom_emojis,
            self.discord
                .current_user_can_use_animated_custom_emojis
                .unwrap_or(false),
        )
    }
}

fn clamp_cursor_index(input: &str, index: usize) -> usize {
    let mut index = index.min(input.len());
    while index > 0 && !input.is_char_boundary(index) {
        index -= 1;
    }
    index
}

enum ApplicationCommandSubmit {
    Ready(ApplicationCommandInvocation),
    Incomplete,
    NotCommand,
}

fn previous_char_boundary(input: &str, index: usize) -> usize {
    let index = clamp_cursor_index(input, index);
    if index == 0 {
        return 0;
    }
    let mut previous = index - 1;
    while previous > 0 && !input.is_char_boundary(previous) {
        previous -= 1;
    }
    previous
}

fn next_char_boundary(input: &str, index: usize) -> usize {
    let mut next = clamp_cursor_index(input, index).saturating_add(1);
    while next < input.len() && !input.is_char_boundary(next) {
        next += 1;
    }
    next.min(input.len())
}

fn vertical_cursor_target(input: &str, cursor: usize, direction: isize) -> Option<usize> {
    let cursor = clamp_cursor_index(input, cursor);
    let line_start = line_start_before(input, cursor);
    let line_end = line_end_after(input, cursor);
    let column = input[line_start..cursor].chars().count();

    match direction {
        -1 => {
            if line_start == 0 {
                return None;
            }
            let target_end = line_start - 1;
            let target_start = line_start_before(input, target_end);
            Some(byte_index_for_line_column(
                input,
                target_start,
                target_end,
                column,
            ))
        }
        1 => {
            let next_start = line_end.checked_add(1)?;
            if next_start > input.len() {
                return None;
            }
            let target_end = line_end_after(input, next_start);
            Some(byte_index_for_line_column(
                input, next_start, target_end, column,
            ))
        }
        _ => None,
    }
}

fn line_start_before(input: &str, index: usize) -> usize {
    input[..index]
        .rfind('\n')
        .map(|offset| offset + '\n'.len_utf8())
        .unwrap_or(0)
}

fn line_end_after(input: &str, index: usize) -> usize {
    input[index..]
        .find('\n')
        .map(|offset| index + offset)
        .unwrap_or(input.len())
}

fn byte_index_for_line_column(input: &str, start: usize, end: usize, column: usize) -> usize {
    input[start..end]
        .char_indices()
        .nth(column)
        .map(|(offset, _)| start + offset)
        .unwrap_or(end)
}

fn previous_word_boundary(input: &str, index: usize) -> usize {
    let index = clamp_cursor_index(input, index);
    let mut prefix = input[..index].char_indices().rev().peekable();
    while matches!(prefix.peek(), Some((_, c)) if c.is_whitespace()) {
        prefix.next();
    }
    let mut word_start = None;
    while let Some(&(byte_idx, c)) = prefix.peek() {
        if c.is_whitespace() {
            break;
        }
        word_start = Some(byte_idx);
        prefix.next();
    }
    word_start.unwrap_or(0)
}

fn next_word_boundary(input: &str, index: usize) -> usize {
    let index = clamp_cursor_index(input, index);
    let mut suffix = input[index..].char_indices().peekable();
    while matches!(suffix.peek(), Some((_, c)) if !c.is_whitespace()) {
        suffix.next();
    }
    while matches!(suffix.peek(), Some((_, c)) if c.is_whitespace()) {
        suffix.next();
    }
    match suffix.peek() {
        Some(&(rel, _)) => index + rel,
        None => input.len(),
    }
}

fn shift_byte_index(index: usize, delta: isize) -> usize {
    if delta < 0 {
        index.saturating_sub(delta.unsigned_abs())
    } else {
        index.saturating_add(delta as usize)
    }
}

fn composer_plus_colon_trigger_before_cursor(input: &str, cursor: usize) -> bool {
    if cursor == 0 || cursor > input.len() || !input.is_char_boundary(cursor) {
        return false;
    }
    let plus_start = previous_char_boundary(input, cursor);
    if &input[plus_start..cursor] != "+" {
        return false;
    }
    input[..plus_start]
        .chars()
        .last()
        .is_none_or(char::is_whitespace)
}

#[cfg(test)]
mod tests {
    use super::{
        composer_plus_colon_trigger_before_cursor, next_word_boundary, previous_word_boundary,
    };

    #[derive(Clone, Copy)]
    enum Dir {
        Left,
        Right,
    }

    fn step(dir: Dir, before: &str) -> String {
        let idx = before
            .find('|')
            .expect("fixture must mark the cursor with `|`");
        let mut input = String::with_capacity(before.len() - 1);
        input.push_str(&before[..idx]);
        input.push_str(&before[idx + 1..]);
        let next = match dir {
            Dir::Left => previous_word_boundary(&input, idx),
            Dir::Right => next_word_boundary(&input, idx),
        };
        let mut out = input.clone();
        out.insert(next, '|');
        out
    }

    #[test]
    fn word_skip_lands_on_word_starts() {
        let cases: &[(Dir, &str, &str)] = &[
            (Dir::Left, "hello world|", "hello |world"),
            (Dir::Left, "hello |world", "|hello world"),
            (Dir::Right, "|hello world", "hello |world"),
            (Dir::Right, "hello |world", "hello world|"),
            (Dir::Left, "   foo|", "   |foo"),
            (Dir::Left, "|hello", "|hello"),
            (Dir::Left, "   |", "|   "),
            (Dir::Right, "hello|", "hello|"),
            (Dir::Right, "|   ", "   |"),
            (Dir::Right, "hello|   world", "hello   |world"),
            (Dir::Right, "hello   |world", "hello   world|"),
            (Dir::Left, "안녕 하세요|", "안녕 |하세요"),
            (Dir::Left, "안녕 |하세요", "|안녕 하세요"),
            (Dir::Right, "|안녕 하세요", "안녕 |하세요"),
            (Dir::Right, "안녕 |하세요", "안녕 하세요|"),
            (Dir::Right, "|a 🦀 b", "a |🦀 b"),
            (Dir::Right, "a |🦀 b", "a 🦀 |b"),
            (Dir::Left, "a 🦀 b|", "a 🦀 |b"),
            (Dir::Left, "a 🦀 |b", "a |🦀 b"),
            (Dir::Left, "|", "|"),
            (Dir::Right, "|", "|"),
        ];

        for (dir, before, expected) in cases {
            let arrow = match dir {
                Dir::Left => "Ctrl+Left",
                Dir::Right => "Ctrl+Right",
            };
            assert_eq!(
                step(*dir, before),
                *expected,
                "{arrow} on {before:?} should land at {expected:?}",
            );
        }
    }

    #[test]
    fn plus_colon_trigger_requires_boundary_plus_before_cursor() {
        let cases = [
            ("+", true),
            ("draft +", true),
            ("draft+", false),
            ("", false),
            ("draft", false),
        ];

        for (input, expected) in cases {
            assert_eq!(
                composer_plus_colon_trigger_before_cursor(input, input.len()),
                expected,
                "{input:?} should return {expected}",
            );
        }
    }
}