Commit d2e0c81982
Unsigned
Layout: unified · split
src/completions.rs +58 −1
| @@ -331,9 +331,12 @@ fn extract_mxc_uris(message: &str) -> Vec<String> { | ||
| 331 | 331 | }) |
| 332 | 332 | .next() |
| 333 | 333 | .unwrap_or_default(); |
| 334 | let uri = uri.trim_end_matches(|c: char| { | |
| 335 | !(c.is_ascii_alphanumeric() || c == '-' || c == '_') | |
| 336 | }); | |
| 334 | 337 | let uri = <&MxcUri>::from(uri); |
| 335 | 338 | |
| 336 | if uri.is_valid() { | |
| 339 | if uri.is_valid() && uri.media_id().is_ok_and(|id| !id.is_empty()) { | |
| 337 | 340 | Some(uri.as_str().to_owned()) |
| 338 | 341 | } else { |
| 339 | 342 | None |
| @@ -372,6 +375,60 @@ mod tests { | ||
| 372 | 375 | assert!(extract_mxc_uris("download mxc://").is_empty()); |
| 373 | 376 | } |
| 374 | 377 | |
| 378 | #[test] | |
| 379 | fn extracts_uris_followed_by_sentence_punctuation() { | |
| 380 | assert_eq!( | |
| 381 | vec!["mxc://matrix.org/some-media-id".to_owned()], | |
| 382 | extract_mxc_uris("look at mxc://matrix.org/some-media-id."), | |
| 383 | ); | |
| 384 | assert_eq!( | |
| 385 | vec!["mxc://matrix.org/some-media-id".to_owned()], | |
| 386 | extract_mxc_uris("see mxc://matrix.org/some-media-id, then reply"), | |
| 387 | ); | |
| 388 | assert_eq!( | |
| 389 | vec!["mxc://matrix.org/some-media-id".to_owned()], | |
| 390 | extract_mxc_uris("is it mxc://matrix.org/some-media-id?"), | |
| 391 | ); | |
| 392 | } | |
| 393 | ||
| 394 | #[test] | |
| 395 | fn extracts_uris_wrapped_in_delimiters() { | |
| 396 | assert_eq!( | |
| 397 | vec!["mxc://matrix.org/some-media-id".to_owned()], | |
| 398 | extract_mxc_uris("(mxc://matrix.org/some-media-id)"), | |
| 399 | ); | |
| 400 | assert_eq!( | |
| 401 | vec!["mxc://matrix.org/some-media-id".to_owned()], | |
| 402 | extract_mxc_uris("[mxc://matrix.org/some-media-id]"), | |
| 403 | ); | |
| 404 | assert_eq!( | |
| 405 | vec!["mxc://matrix.org/some-media-id".to_owned()], | |
| 406 | extract_mxc_uris("\"mxc://matrix.org/some-media-id\""), | |
| 407 | ); | |
| 408 | assert_eq!( | |
| 409 | vec!["mxc://matrix.org/some-media-id".to_owned()], | |
| 410 | extract_mxc_uris("<mxc://matrix.org/some-media-id>"), | |
| 411 | ); | |
| 412 | } | |
| 413 | ||
| 414 | #[test] | |
| 415 | fn extracts_multiple_uris_from_a_single_line() { | |
| 416 | assert_eq!( | |
| 417 | vec![ | |
| 418 | "mxc://a.example.org/one".to_owned(), | |
| 419 | "mxc://b.example.org/two".to_owned(), | |
| 420 | ], | |
| 421 | extract_mxc_uris( | |
| 422 | "first mxc://a.example.org/one, then mxc://b.example.org/two." | |
| 423 | ), | |
| 424 | ); | |
| 425 | } | |
| 426 | ||
| 427 | #[test] | |
| 428 | fn drops_uris_without_a_media_id() { | |
| 429 | assert!(extract_mxc_uris("download mxc://matrix.org/").is_empty()); | |
| 430 | } | |
| 431 | ||
| 375 | 432 | #[test] |
| 376 | 433 | fn completes_only_resolvable_matrix_short_names() { |
| 377 | 434 | assert_eq!( |