Commit 85a3af4f23
Unsigned
Layout: unified · split
src/completions.rs +70 −12
| @@ -197,24 +197,44 @@ impl CompletionCallback for NicksCompletion { | ||
| 197 | 197 | _: Cow<str>, |
| 198 | 198 | completion: &Completion, |
| 199 | 199 | ) -> Result<(), ()> { |
| 200 | if buffer.get_localvar("thread_root").is_none() { | |
| 201 | return Ok(()); | |
| 202 | } | |
| 203 | ||
| 204 | if let Some(room) = self.servers.find_room(buffer) { | |
| 205 | for nick in room.names() { | |
| 206 | completion.add_with_options( | |
| 207 | &nick, | |
| 208 | true, | |
| 209 | CompletionPosition::Sorted, | |
| 210 | ) | |
| 211 | } | |
| 200 | let room_names = self | |
| 201 | .servers | |
| 202 | .find_room(buffer) | |
| 203 | .map(|room| room.names()) | |
| 204 | .unwrap_or_default(); | |
| 205 | ||
| 206 | for nick in nick_completion_candidates( | |
| 207 | buffer.get_localvar("thread_root").is_some(), | |
| 208 | room_names, | |
| 209 | ) { | |
| 210 | completion.add_with_options( | |
| 211 | &nick, | |
| 212 | true, | |
| 213 | CompletionPosition::Sorted, | |
| 214 | ) | |
| 212 | 215 | } |
| 213 | 216 | |
| 214 | 217 | Ok(()) |
| 215 | 218 | } |
| 216 | 219 | } |
| 217 | 220 | |
| 221 | /// Nick completion candidates for the given matrix buffer context. | |
| 222 | /// | |
| 223 | /// Only thread buffers contribute candidates (the parent room's members); | |
| 224 | /// every other buffer contributes nothing so WeeChat keeps using its | |
| 225 | /// built-in nicklist-based completion there. | |
| 226 | fn nick_completion_candidates( | |
| 227 | is_thread_buffer: bool, | |
| 228 | room_names: Vec<String>, | |
| 229 | ) -> Vec<String> { | |
| 230 | if !is_thread_buffer { | |
| 231 | return Vec::new(); | |
| 232 | } | |
| 233 | let mut names = room_names; | |
| 234 | names.sort_unstable(); | |
| 235 | names | |
| 236 | } | |
| 237 | ||
| 218 | 238 | struct MediaCompletion { |
| 219 | 239 | servers: Servers, |
| 220 | 240 | } |
| @@ -330,4 +350,42 @@ mod tests { | ||
| 330 | 350 | BTreeSet::from(["#matrix:example.org".to_owned()]), |
| 331 | 351 | ); |
| 332 | 352 | } |
| 353 | ||
| 354 | #[test] | |
| 355 | fn thread_buffers_contribute_sorted_room_member_nicks() { | |
| 356 | assert_eq!( | |
| 357 | nick_completion_candidates( | |
| 358 | true, | |
| 359 | vec![ | |
| 360 | "Bob".to_owned(), | |
| 361 | "Alice".to_owned(), | |
| 362 | "Ada (ada@example.org)".to_owned(), | |
| 363 | ], | |
| 364 | ), | |
| 365 | vec![ | |
| 366 | "Ada (ada@example.org)".to_owned(), | |
| 367 | "Alice".to_owned(), | |
| 368 | "Bob".to_owned(), | |
| 369 | ], | |
| 370 | ); | |
| 371 | } | |
| 372 | ||
| 373 | #[test] | |
| 374 | fn thread_buffers_without_a_room_contribute_nothing() { | |
| 375 | assert_eq!(nick_completion_candidates(true, vec![]), Vec::<String>::new()); | |
| 376 | } | |
| 377 | ||
| 378 | #[test] | |
| 379 | fn non_thread_buffers_keep_the_builtin_nicklist_completion() { | |
| 380 | assert_eq!( | |
| 381 | nick_completion_candidates( | |
| 382 | false, | |
| 383 | vec![ | |
| 384 | "Alice".to_owned(), | |
| 385 | "Bob".to_owned(), | |
| 386 | ], | |
| 387 | ), | |
| 388 | Vec::<String>::new(), | |
| 389 | ); | |
| 390 | } | |
| 333 | 391 | } |