Commit 5df9fe7960
Unregistered key
Layout: unified · split
src/base.rs +56 −25
| @@ -847,6 +847,17 @@ fn complete_emoji(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> | ||
| 847 | 847 | store.application.emojis.complete(sc.as_ref()) |
| 848 | 848 | } |
| 849 | 849 | |
| 850 | fn 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 | ||
| 850 | 861 | fn complete_cmdarg( |
| 851 | 862 | desc: CommandDescription, |
| 852 | 863 | text: &EditRope, |
| @@ -865,24 +876,26 @@ fn complete_cmdarg( | ||
| 865 | 876 | "react" | "unreact" => complete_emoji(text, cursor, store), |
| 866 | 877 | |
| 867 | 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 | 880 | "room" => vec![], |
| 870 | 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 | |
| 875 | fn 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()) { | |
| 889 | fn complete_cmd( | |
| 890 | cmd: &str, | |
| 891 | text: &EditRope, | |
| 892 | cursor: &mut Cursor, | |
| 893 | store: &ProgramStore, | |
| 894 | ) -> Vec<String> { | |
| 895 | match CommandDescription::from_str(cmd) { | |
| 881 | 896 | Ok(desc) => { |
| 882 | 897 | 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) | |
| 886 | 899 | } else { |
| 887 | 900 | // Complete command argument. |
| 888 | 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 | |
| 910 | fn 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 | 918 | #[cfg(test)] |
| 898 | 919 | pub mod tests { |
| 899 | 920 | use super::*; |
| @@ -981,6 +1002,13 @@ pub mod tests { | ||
| 981 | 1002 | #[tokio::test] |
| 982 | 1003 | async fn test_complete_cmdbar() { |
| 983 | 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 | 1013 | let text = EditRope::from("invite "); |
| 986 | 1014 | let mut cursor = Cursor::new(0, 7); |
| @@ -993,28 +1021,31 @@ pub mod tests { | ||
| 993 | 1021 | let text = EditRope::from("invite "); |
| 994 | 1022 | let mut cursor = Cursor::new(0, 7); |
| 995 | 1023 | 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); | |
| 1003 | 1025 | |
| 1004 | 1026 | let text = EditRope::from("invite ignored"); |
| 1005 | 1027 | let mut cursor = Cursor::new(0, 7); |
| 1006 | 1028 | 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); | |
| 1014 | 1030 | |
| 1015 | 1031 | let text = EditRope::from("invite @user1ignored"); |
| 1016 | 1032 | let mut cursor = Cursor::new(0, 13); |
| 1017 | 1033 | let res = complete_cmdbar(&text, &mut cursor, &store); |
| 1018 | 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 | } |