| @@ -10,10 +10,12 @@ use weechat::{ |
| 10 | 10 | Weechat, |
| 11 | 11 | }; |
| 12 | 12 | |
| 13 | use crate::commands::is_buffer_target; |
| 13 | 14 | use crate::Servers; |
| 14 | 15 | |
| 15 | 16 | #[allow(dead_code)] |
| 16 | 17 | pub struct Completions { |
| 18 | buffer_short_names: CompletionHook, |
| 17 | 19 | servers: CompletionHook, |
| 18 | 20 | users: CompletionHook, |
| 19 | 21 | media: CompletionHook, |
| @@ -22,6 +24,9 @@ pub struct Completions { |
| 22 | 24 | impl Completions { |
| 23 | 25 | pub fn hook_all(servers: Servers) -> Result<Self, ()> { |
| 24 | 26 | Ok(Self { |
| 27 | buffer_short_names: BufferShortNamesCompletion::create( |
| 28 | servers.clone(), |
| 29 | )?, |
| 25 | 30 | servers: ServersCompletion::create(servers.clone())?, |
| 26 | 31 | users: UsersCompletion::create(servers.clone())?, |
| 27 | 32 | media: MediaCompletion::create(servers)?, |
| @@ -29,6 +34,51 @@ impl Completions { |
| 29 | 34 | } |
| 30 | 35 | } |
| 31 | 36 | |
| 37 | struct BufferShortNamesCompletion { |
| 38 | servers: Servers, |
| 39 | } |
| 40 | |
| 41 | impl BufferShortNamesCompletion { |
| 42 | fn create(servers: Servers) -> Result<CompletionHook, ()> { |
| 43 | let comp = BufferShortNamesCompletion { servers }; |
| 44 | |
| 45 | // WeeChat's /buffer completion uses this item after adding its regular |
| 46 | // buffer names. Adding Matrix short names here keeps those core |
| 47 | // candidates intact while making the command-run hook reachable. |
| 48 | CompletionHook::new( |
| 49 | "buffers_names", |
| 50 | "Completion for Matrix buffer short names", |
| 51 | comp, |
| 52 | ) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | impl CompletionCallback for BufferShortNamesCompletion { |
| 57 | fn callback( |
| 58 | &mut self, |
| 59 | _: &Weechat, |
| 60 | _: &Buffer, |
| 61 | _: Cow<str>, |
| 62 | completion: &Completion, |
| 63 | ) -> Result<(), ()> { |
| 64 | for short_name in buffer_completion_candidates( |
| 65 | self.servers |
| 66 | .borrow() |
| 67 | .values() |
| 68 | .flat_map(|server| server.rooms()) |
| 69 | .flat_map(|room| room.buffer_short_names()), |
| 70 | ) { |
| 71 | completion.add_with_options( |
| 72 | &short_name, |
| 73 | false, |
| 74 | CompletionPosition::Sorted, |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | Ok(()) |
| 79 | } |
| 80 | } |
| 81 | |
| 32 | 82 | struct ServersCompletion { |
| 33 | 83 | servers: Servers, |
| 34 | 84 | } |
| @@ -185,6 +235,16 @@ fn extract_mxc_uris(message: &str) -> Vec<String> { |
| 185 | 235 | .collect() |
| 186 | 236 | } |
| 187 | 237 | |
| 238 | fn buffer_completion_candidates<I>(short_names: I) -> BTreeSet<String> |
| 239 | where |
| 240 | I: IntoIterator<Item = String>, |
| 241 | { |
| 242 | short_names |
| 243 | .into_iter() |
| 244 | .filter(|short_name| is_buffer_target(short_name)) |
| 245 | .collect() |
| 246 | } |
| 247 | |
| 188 | 248 | #[cfg(test)] |
| 189 | 249 | mod tests { |
| 190 | 250 | use super::*; |
| @@ -204,4 +264,18 @@ mod tests { |
| 204 | 264 | fn ignores_invalid_mxc_uris() { |
| 205 | 265 | assert!(extract_mxc_uris("download mxc://").is_empty()); |
| 206 | 266 | } |
| 267 | |
| 268 | #[test] |
| 269 | fn completes_only_resolvable_matrix_short_names() { |
| 270 | assert_eq!( |
| 271 | buffer_completion_candidates([ |
| 272 | "#matrix:example.org".to_owned(), |
| 273 | "#matrix:example.org".to_owned(), |
| 274 | "".to_owned(), |
| 275 | "matrix room".to_owned(), |
| 276 | "list".to_owned(), |
| 277 | ]), |
| 278 | BTreeSet::from(["#matrix:example.org".to_owned()]), |
| 279 | ); |
| 280 | } |
| 207 | 281 | } |