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) ->
847 store.application.emojis.complete(sc.as_ref()) 847 store.application.emojis.complete(sc.as_ref())
848} 848}
849 849
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
850fn complete_cmdarg( 861fn complete_cmdarg(
851 desc: CommandDescription, 862 desc: CommandDescription,
852 text: &EditRope, 863 text: &EditRope,
@@ -865,24 +876,26 @@ fn complete_cmdarg(
865 "react" | "unreact" => complete_emoji(text, cursor, store), 876 "react" | "unreact" => complete_emoji(text, cursor, store),
866 877
867 "invite" => complete_users(text, cursor, store), 878 "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),
869 "room" => vec![], 880 "room" => vec![],
870 "verify" => vec![], 881 "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![],
872 } 886 }
873} 887}
874 888
875fn complete_cmdbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> { 889fn complete_cmd(
876 let eo = text.cursor_to_offset(cursor); 890 cmd: &str,
877 let slice = text.slice(0.into(), eo, false); 891 text: &EditRope,
878 let cow = Cow::from(&slice); 892 cursor: &mut Cursor,
879 893 store: &ProgramStore,
880 match CommandDescription::from_str(cow.as_ref()) { 894) -> Vec<String> {
895 match CommandDescription::from_str(cmd) {
881 Ok(desc) => { 896 Ok(desc) => {
882 if desc.arg.untrimmed.is_empty() { 897 if desc.arg.untrimmed.is_empty() {
883 // Complete command name and set cursor position. 898 complete_cmdname(desc, text, cursor, store)
884 let _ = text.get_prefix_word_mut(cursor, &WordStyle::Little);
885 store.application.cmds.complete_name(desc.command.as_str())
886 } else { 899 } else {
887 // Complete command argument. 900 // Complete command argument.
888 complete_cmdarg(desc, text, cursor, store) 901 complete_cmdarg(desc, text, cursor, store)
@@ -894,6 +907,14 @@ fn complete_cmdbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -
894 } 907 }
895} 908}
896 909
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
897#[cfg(test)] 918#[cfg(test)]
898pub mod tests { 919pub mod tests {
899 use super::*; 920 use super::*;
@@ -981,6 +1002,13 @@ pub mod tests {
981 #[tokio::test] 1002 #[tokio::test]
982 async fn test_complete_cmdbar() { 1003 async fn test_complete_cmdbar() {
983 let store = mock_store().await; 1004 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 ];
984 1012
985 let text = EditRope::from("invite "); 1013 let text = EditRope::from("invite ");
986 let mut cursor = Cursor::new(0, 7); 1014 let mut cursor = Cursor::new(0, 7);
@@ -993,28 +1021,31 @@ pub mod tests {
993 let text = EditRope::from("invite "); 1021 let text = EditRope::from("invite ");
994 let mut cursor = Cursor::new(0, 7); 1022 let mut cursor = Cursor::new(0, 7);
995 let res = complete_cmdbar(&text, &mut cursor, &store); 1023 let res = complete_cmdbar(&text, &mut cursor, &store);
996 assert_eq!(res, vec![ 1024 assert_eq!(res, users);
997 "@user1:example.com",
998 "@user2:example.com",
999 "@user3:example.com",
1000 "@user4:example.com",
1001 "@user5:example.com"
1002 ]);
1003 1025
1004 let text = EditRope::from("invite ignored"); 1026 let text = EditRope::from("invite ignored");
1005 let mut cursor = Cursor::new(0, 7); 1027 let mut cursor = Cursor::new(0, 7);
1006 let res = complete_cmdbar(&text, &mut cursor, &store); 1028 let res = complete_cmdbar(&text, &mut cursor, &store);
1007 assert_eq!(res, vec![ 1029 assert_eq!(res, users);
1008 "@user1:example.com",
1009 "@user2:example.com",
1010 "@user3:example.com",
1011 "@user4:example.com",
1012 "@user5:example.com"
1013 ]);
1014 1030
1015 let text = EditRope::from("invite @user1ignored"); 1031 let text = EditRope::from("invite @user1ignored");
1016 let mut cursor = Cursor::new(0, 13); 1032 let mut cursor = Cursor::new(0, 13);
1017 let res = complete_cmdbar(&text, &mut cursor, &store); 1033 let res = complete_cmdbar(&text, &mut cursor, &store);
1018 assert_eq!(res, vec!["@user1:example.com"]); 1034 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);
1019 } 1050 }
1020} 1051}