Commit 5df9fe7960

5df9fe7960f803d2d5bb962632acaf6b06a96ec8

parent: a5c25f2487

Unregistered key

Ulyssa <git@ulyssa.dev> · 2023-05-02 04:14 UTC

Tab completion panics for unrecognized commands (#81)

Layout: unified · split

src/base.rs +56 −25
@@ -847,6 +847,17 @@ fn complete_emoji(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) ->
847847 store.application.emojis.complete(sc.as_ref())
848848}
849849
850fn complete_cmdname(
851 desc: CommandDescription,
852 text: &EditRope,
853 cursor: &mut Cursor,
854 store: &ProgramStore,
855) -> Vec<String> {
856 // Complete command name and set cursor position.
857 let _ = text.get_prefix_word_mut(cursor, &WordStyle::Little);
858 store.application.cmds.complete_name(desc.command.as_str())
859}
860
850861fn complete_cmdarg(
851862 desc: CommandDescription,
852863 text: &EditRope,
@@ -865,24 +876,26 @@ fn complete_cmdarg(
865876 "react" | "unreact" => complete_emoji(text, cursor, store),
866877
867878 "invite" => complete_users(text, cursor, store),
868 "join" => complete_matrix_names(text, cursor, store),
879 "join" | "split" | "vsplit" | "tabedit" => complete_matrix_names(text, cursor, store),
869880 "room" => vec![],
870881 "verify" => vec![],
871 _ => panic!("unknown command {}", cmd.name.as_str()),
882 "vertical" | "horizontal" | "aboveleft" | "belowright" | "tab" => {
883 complete_cmd(desc.arg.text.as_str(), text, cursor, store)
884 },
885 _ => vec![],
872886 }
873887}
874888
875fn complete_cmdbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> {
876 let eo = text.cursor_to_offset(cursor);
877 let slice = text.slice(0.into(), eo, false);
878 let cow = Cow::from(&slice);
879
880 match CommandDescription::from_str(cow.as_ref()) {
889fn complete_cmd(
890 cmd: &str,
891 text: &EditRope,
892 cursor: &mut Cursor,
893 store: &ProgramStore,
894) -> Vec<String> {
895 match CommandDescription::from_str(cmd) {
881896 Ok(desc) => {
882897 if desc.arg.untrimmed.is_empty() {
883 // Complete command name and set cursor position.
884 let _ = text.get_prefix_word_mut(cursor, &WordStyle::Little);
885 store.application.cmds.complete_name(desc.command.as_str())
898 complete_cmdname(desc, text, cursor, store)
886899 } else {
887900 // Complete command argument.
888901 complete_cmdarg(desc, text, cursor, store)
@@ -894,6 +907,14 @@ fn complete_cmdbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -
894907 }
895908}
896909
910fn complete_cmdbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> {
911 let eo = text.cursor_to_offset(cursor);
912 let slice = text.slice(0.into(), eo, false);
913 let cow = Cow::from(&slice);
914
915 complete_cmd(cow.as_ref(), text, cursor, store)
916}
917
897918#[cfg(test)]
898919pub mod tests {
899920 use super::*;
@@ -981,6 +1002,13 @@ pub mod tests {
9811002 #[tokio::test]
9821003 async fn test_complete_cmdbar() {
9831004 let store = mock_store().await;
1005 let users = vec![
1006 "@user1:example.com",
1007 "@user2:example.com",
1008 "@user3:example.com",
1009 "@user4:example.com",
1010 "@user5:example.com",
1011 ];
9841012
9851013 let text = EditRope::from("invite ");
9861014 let mut cursor = Cursor::new(0, 7);
@@ -993,28 +1021,31 @@ pub mod tests {
9931021 let text = EditRope::from("invite ");
9941022 let mut cursor = Cursor::new(0, 7);
9951023 let res = complete_cmdbar(&text, &mut cursor, &store);
996 assert_eq!(res, vec![
997 "@user1:example.com",
998 "@user2:example.com",
999 "@user3:example.com",
1000 "@user4:example.com",
1001 "@user5:example.com"
1002 ]);
1024 assert_eq!(res, users);
10031025
10041026 let text = EditRope::from("invite ignored");
10051027 let mut cursor = Cursor::new(0, 7);
10061028 let res = complete_cmdbar(&text, &mut cursor, &store);
1007 assert_eq!(res, vec![
1008 "@user1:example.com",
1009 "@user2:example.com",
1010 "@user3:example.com",
1011 "@user4:example.com",
1012 "@user5:example.com"
1013 ]);
1029 assert_eq!(res, users);
10141030
10151031 let text = EditRope::from("invite @user1ignored");
10161032 let mut cursor = Cursor::new(0, 13);
10171033 let res = complete_cmdbar(&text, &mut cursor, &store);
10181034 assert_eq!(res, vec!["@user1:example.com"]);
1035
1036 let text = EditRope::from("abo hor");
1037 let mut cursor = Cursor::new(0, 7);
1038 let res = complete_cmdbar(&text, &mut cursor, &store);
1039 assert_eq!(res, vec!["horizontal"]);
1040
1041 let text = EditRope::from("abo hor inv");
1042 let mut cursor = Cursor::new(0, 11);
1043 let res = complete_cmdbar(&text, &mut cursor, &store);
1044 assert_eq!(res, vec!["invite"]);
1045
1046 let text = EditRope::from("abo hor invite \n");
1047 let mut cursor = Cursor::new(0, 15);
1048 let res = complete_cmdbar(&text, &mut cursor, &store);
1049 assert_eq!(res, users);
10191050 }
10201051}