Commit 8d22b83d85

8d22b83d855f65a32b3fe6f97b61f99a22a30afa

parent: 529073f4d4

Unregistered key

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

Support sending and completing Emoji shortcodes in the message bar (#100)

Layout: unified · split

Cargo.lock +1
@@ -494,6 +494,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
494494checksum = "482aa5695bca086022be453c700a40c02893f1ba7098a2c88351de55341ae894"
495495dependencies = [
496496 "clap",
497 "emojis",
497498 "entities",
498499 "memchr",
499500 "once_cell",
Cargo.toml +1 −1
@@ -18,7 +18,7 @@ arboard = "3.2.0"
1818bitflags = "1.3.2"
1919chrono = "0.4"
2020clap = {version = "4.0", features = ["derive"]}
21comrak = "0.18.0"
21comrak = {version = "0.18.0", features = ["shortcodes"]}
2222css-color-parser = "0.1.2"
2323dirs = "4.0.0"
2424emojis = "~0.5.2"
src/base.rs +71 −3
@@ -788,9 +788,7 @@ impl ApplicationInfo for IambInfo {
788788 IambBufferId::Command(CommandType::Command) => complete_cmdbar(text, cursor, store),
789789 IambBufferId::Command(CommandType::Search) => vec![],
790790
791 IambBufferId::Room(_, RoomFocus::MessageBar) => {
792 complete_matrix_names(text, cursor, store)
793 },
791 IambBufferId::Room(_, RoomFocus::MessageBar) => complete_msgbar(text, cursor, store),
794792 IambBufferId::Room(_, RoomFocus::Scrollback) => vec![],
795793
796794 IambBufferId::DirectList => vec![],
@@ -822,6 +820,53 @@ fn complete_users(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) ->
822820 .collect()
823821}
824822
823fn complete_msgbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> {
824 let id = text
825 .get_prefix_word_mut(cursor, &MATRIX_ID_WORD)
826 .unwrap_or_else(EditRope::empty);
827 let id = Cow::from(&id);
828
829 match id.chars().next() {
830 // Complete room aliases.
831 Some('#') => {
832 return store.application.names.complete(id.as_ref());
833 },
834
835 // Complete room identifiers.
836 Some('!') => {
837 return store
838 .application
839 .rooms
840 .complete(id.as_ref())
841 .into_iter()
842 .map(|i| i.to_string())
843 .collect();
844 },
845
846 // Complete Emoji shortcodes.
847 Some(':') => {
848 let list = store.application.emojis.complete(&id[1..]);
849 let iter = list.into_iter().take(200).map(|s| format!(":{}:", s));
850
851 return iter.collect();
852 },
853
854 // Complete usernames for @ and empty strings.
855 Some('@') | None => {
856 return store
857 .application
858 .presences
859 .complete(id.as_ref())
860 .into_iter()
861 .map(|i| i.to_string())
862 .collect();
863 },
864
865 // Unknown sigil.
866 Some(_) => return vec![],
867 }
868}
869
825870fn complete_matrix_names(
826871 text: &EditRope,
827872 cursor: &mut Cursor,
@@ -1011,6 +1056,29 @@ pub mod tests {
10111056 );
10121057 }
10131058
1059 #[tokio::test]
1060 async fn test_complete_msgbar() {
1061 let store = mock_store().await;
1062
1063 let text = EditRope::from("going for a walk :walk ");
1064 let mut cursor = Cursor::new(0, 22);
1065 let res = complete_msgbar(&text, &mut cursor, &store);
1066 assert_eq!(res, vec![":walking:", ":walking_man:", ":walking_woman:"]);
1067 assert_eq!(cursor, Cursor::new(0, 17));
1068
1069 let text = EditRope::from("hello @user1 ");
1070 let mut cursor = Cursor::new(0, 12);
1071 let res = complete_msgbar(&text, &mut cursor, &store);
1072 assert_eq!(res, vec!["@user1:example.com"]);
1073 assert_eq!(cursor, Cursor::new(0, 6));
1074
1075 let text = EditRope::from("see #room ");
1076 let mut cursor = Cursor::new(0, 9);
1077 let res = complete_msgbar(&text, &mut cursor, &store);
1078 assert_eq!(res, vec!["#room1:example.com"]);
1079 assert_eq!(cursor, Cursor::new(0, 4));
1080 }
1081
10141082 #[tokio::test]
10151083 async fn test_complete_cmdbar() {
10161084 let store = mock_store().await;
src/message/mod.rs +6
@@ -97,6 +97,7 @@ const TIME_GUTTER_EMPTY_SPAN: Span<'static> = span_static(TIME_GUTTER_EMPTY);
9797
9898fn text_to_message_content(input: String) -> TextMessageEventContent {
9999 let mut options = ComrakOptions::default();
100 options.extension.shortcodes = true;
100101 options.render.hardbreaks = true;
101102 let html = markdown_to_html(input.as_str(), &options);
102103
@@ -1013,6 +1014,11 @@ pub mod tests {
10131014 "<pre><code class=\"language-rust\">const A: usize = 1;\n</code></pre>\n"
10141015 );
10151016
1017 let input = ":heart:\n";
1018 let content = text_to_message_content(input.into());
1019 assert_eq!(content.body, input);
1020 assert_eq!(content.formatted.unwrap().body, "<p>\u{2764}\u{FE0F}</p>\n");
1021
10161022 let input = "para 1\n\npara 2\n";
10171023 let content = text_to_message_content(input.into());
10181024 assert_eq!(content.body, input);
src/tests.rs +4 −1
@@ -42,6 +42,8 @@ use crate::{
4242 worker::Requester,
4343};
4444
45const TEST_ROOM1_ALIAS: &str = "#room1:example.com";
46
4547lazy_static! {
4648 pub static ref TEST_ROOM1_ID: OwnedRoomId = RoomId::new(server_name!("example.com")).to_owned();
4749 pub static ref TEST_USER1: OwnedUserId = user_id!("@user1:example.com").to_owned();
@@ -223,7 +225,8 @@ pub async fn mock_store() -> ProgramStore {
223225 let room_id = TEST_ROOM1_ID.clone();
224226 let info = mock_room();
225227
226 store.rooms.insert(room_id, info);
228 store.rooms.insert(room_id.clone(), info);
229 store.names.insert(TEST_ROOM1_ALIAS.to_string(), room_id);
227230
228231 ProgramStore::new(store)
229232}