|
3 | 3 | //! |
4 | 4 | //! Core owns the whole decision. It checks declarations, finds matches in |
5 | 5 | //! console lines, and turns a match into a workspace location, or into the |
6 | | -//! groups a plugin backend resolves. A host only shows links and opens them. |
| 6 | +//! groups a plugin backend resolves. It also links the web addresses a line |
| 7 | +//! prints, which no plugin declares, so a server's URL opens in the browser. |
| 8 | +//! A host only shows links and opens them. |
7 | 9 |
|
8 | 10 | use lithe_plugin_model::{invalid, Validation}; |
9 | 11 | use lithe_protocol::{CoreError, ErrorCode}; |
@@ -208,12 +210,15 @@ fn request_error(message: &str) -> CoreError { |
208 | 210 | } |
209 | 211 |
|
210 | 212 | /// `outputLinks.match`: finds links in console lines with the matchers of |
211 | | -/// every installed plugin, in source order and then declaration order. |
| 213 | +/// every installed plugin, in source order and then declaration order, and |
| 214 | +/// then the web addresses the lines print. |
212 | 215 | /// |
213 | | -/// Each link reports its line index, its UTF-16 `start` and `length`, the |
214 | | -/// plugin and matcher, and either `location` with `paths` the host opens the |
215 | | -/// first existing one of, or `resolve` with the groups the backend function |
216 | | -/// receives. Matches overlapping an earlier link on the line are skipped. |
| 216 | +/// Each link reports its line index and its UTF-16 `start` and `length`. A |
| 217 | +/// plugin's link names the plugin and matcher and has either `location` with |
| 218 | +/// `paths` the host opens the first existing one of, or `resolve` with the |
| 219 | +/// groups the backend function receives; a web address has `url` alone, which |
| 220 | +/// the host opens in the browser. Matches overlapping an earlier link on the |
| 221 | +/// line are skipped, so a plugin's link wins over an address inside it. |
217 | 222 | pub fn match_lines(payload: Value) -> Result<Value, CoreError> { |
218 | 223 | let request: MatchRequest = serde_json::from_value(payload).map_err(|error| { |
219 | 224 | request_error("Invalid output link match request.").with_details(error.to_string()) |
@@ -299,10 +304,71 @@ pub fn match_lines(payload: Value) -> Result<Value, CoreError> { |
299 | 304 | links.push(link); |
300 | 305 | } |
301 | 306 | } |
| 307 | + for (start, end) in web_addresses(scanned) { |
| 308 | + if taken.len() >= MAX_LINKS_PER_LINE { |
| 309 | + break; |
| 310 | + } |
| 311 | + if taken |
| 312 | + .iter() |
| 313 | + .any(|(taken_start, taken_end)| start < *taken_end && *taken_start < end) |
| 314 | + { |
| 315 | + continue; |
| 316 | + } |
| 317 | + taken.push((start, end)); |
| 318 | + links.push(json!({ |
| 319 | + "line": line_index, |
| 320 | + "start": utf16_len(&scanned[..start]), |
| 321 | + "length": utf16_len(&scanned[start..end]), |
| 322 | + "url": &scanned[start..end], |
| 323 | + })); |
| 324 | + } |
302 | 325 | } |
| 326 | + // A line's plugin links come first and its addresses after them, so the |
| 327 | + // links of a line are ordered by where they start. |
| 328 | + links.sort_by_key(|link| (link["line"].as_u64(), link["start"].as_u64())); |
303 | 329 | Ok(json!({ "links": links })) |
304 | 330 | } |
305 | 331 |
|
| 332 | +/// The byte ranges of the `http` and `https` addresses in `line`. An address |
| 333 | +/// runs until whitespace or a character URLs do not contain unencoded; the |
| 334 | +/// punctuation that ends a sentence after it, and a closing bracket it did |
| 335 | +/// not open, are not part of it. |
| 336 | +fn web_addresses(line: &str) -> Vec<(usize, usize)> { |
| 337 | + static ADDRESS: OnceLock<Regex> = OnceLock::new(); |
| 338 | + let address = ADDRESS.get_or_init(|| { |
| 339 | + Regex::new(r#"(?i)\bhttps?://[^\s<>"'`\x00-\x1f\x7f]+"#) |
| 340 | + .expect("the address pattern is valid") |
| 341 | + }); |
| 342 | + let mut ranges = Vec::new(); |
| 343 | + for found in address.find_iter(line) { |
| 344 | + let text = found.as_str(); |
| 345 | + let mut end = text.len(); |
| 346 | + while let Some(last) = text[..end].chars().next_back() { |
| 347 | + let unbalanced = |open: char, close: char| { |
| 348 | + last == close |
| 349 | + && text[..end].matches(close).count() > text[..end].matches(open).count() |
| 350 | + }; |
| 351 | + if ".,;:!?".contains(last) |
| 352 | + || unbalanced('(', ')') |
| 353 | + || unbalanced('[', ']') |
| 354 | + || unbalanced('{', '}') |
| 355 | + { |
| 356 | + end -= last.len_utf8(); |
| 357 | + } else { |
| 358 | + break; |
| 359 | + } |
| 360 | + } |
| 361 | + // An address needs a host after the scheme. |
| 362 | + if text[..end] |
| 363 | + .split_once("://") |
| 364 | + .is_some_and(|(_, rest)| !rest.is_empty()) |
| 365 | + { |
| 366 | + ranges.push((found.start(), found.start() + end)); |
| 367 | + } |
| 368 | + } |
| 369 | + ranges |
| 370 | +} |
| 371 | + |
306 | 372 | /// The longest prefix of `line` within `limit` bytes that ends on a |
307 | 373 | /// character boundary. |
308 | 374 | fn prefix(line: &str, limit: usize) -> &str { |
@@ -444,6 +510,49 @@ pub fn decode_resolution(payload: Value) -> Result<Value, CoreError> { |
444 | 510 | mod tests { |
445 | 511 | use super::*; |
446 | 512 |
|
| 513 | + fn addresses(line: &str) -> Vec<&str> { |
| 514 | + web_addresses(line) |
| 515 | + .into_iter() |
| 516 | + .map(|(start, end)| &line[start..end]) |
| 517 | + .collect() |
| 518 | + } |
| 519 | + |
| 520 | + #[test] |
| 521 | + fn web_addresses_end_before_trailing_punctuation_and_unopened_brackets() { |
| 522 | + assert_eq!( |
| 523 | + addresses("Local: http://localhost:8080/, see (https://example.org/a_(b)) now."), |
| 524 | + ["http://localhost:8080/", "https://example.org/a_(b)"] |
| 525 | + ); |
| 526 | + assert_eq!( |
| 527 | + addresses("Swagger UI: HTTP://127.0.0.1:8080/swagger-ui/index.html?x=1&y=[2]"), |
| 528 | + ["HTTP://127.0.0.1:8080/swagger-ui/index.html?x=1&y=[2]"] |
| 529 | + ); |
| 530 | + // Quotes, angle brackets and the scheme alone end or are no address. |
| 531 | + assert_eq!( |
| 532 | + addresses("<http://a.test/x> \"https://b.test\" http:// ftp://c.test mailto:x"), |
| 533 | + ["http://a.test/x", "https://b.test"] |
| 534 | + ); |
| 535 | + // A scheme inside a word is not the start of an address. |
| 536 | + assert!(addresses("nothttp://a.test").is_empty()); |
| 537 | + } |
| 538 | + |
| 539 | + #[test] |
| 540 | + fn a_plugin_link_wins_over_an_address_it_contains() { |
| 541 | + let reply = match_lines(json!({ |
| 542 | + "scope": "run", "platform": "linux", "workspaceRoots": ["/w"], |
| 543 | + "sources": [{"pluginID": "p", "matchers": [{ |
| 544 | + "id": "m", "pattern": r"see (?P<path>\S+) \S+", "resolve": {"function": "f"} |
| 545 | + }]}], |
| 546 | + "lines": ["see a.txt https://x.test/ then https://y.test/"] |
| 547 | + })) |
| 548 | + .unwrap(); |
| 549 | + let links = reply["links"].as_array().unwrap(); |
| 550 | + assert_eq!(links.len(), 2, "{links:?}"); |
| 551 | + assert_eq!(links[0]["matcherID"], "m"); |
| 552 | + assert_eq!(links[1]["url"], "https://y.test/"); |
| 553 | + assert_eq!(links[1]["start"], 31); |
| 554 | + } |
| 555 | + |
447 | 556 | fn matcher(value: Value) -> OutputLinkMatcher { |
448 | 557 | serde_json::from_value(value).unwrap() |
449 | 558 | } |
|
0 commit comments