terminal_hyperlinks.rs

   1use alacritty_terminal::{
   2    Term,
   3    event::EventListener,
   4    grid::Dimensions,
   5    index::{Boundary, Column, Direction as AlacDirection, Line, Point as AlacPoint},
   6    term::search::{Match, RegexIter, RegexSearch},
   7};
   8use regex::Regex;
   9use std::{ops::Index, sync::LazyLock};
  10
  11const URL_REGEX: &str = r#"(ipfs:|ipns:|magnet:|mailto:|gemini://|gopher://|https://|http://|news:|file://|git://|ssh:|ftp://)[^\u{0000}-\u{001F}\u{007F}-\u{009F}<>"\s{-}\^⟨⟩`]+"#;
  12// Optional suffix matches MSBuild diagnostic suffixes for path parsing in PathLikeWithPosition
  13// https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-diagnostic-format-for-tasks
  14const WORD_REGEX: &str =
  15    r#"[\$\+\w.\[\]:/\\@\-~()]+(?:\((?:\d+|\d+,\d+)\))|[\$\+\w.\[\]:/\\@\-~()]+"#;
  16
  17const PYTHON_FILE_LINE_REGEX: &str = r#"File "(?P<file>[^"]+)", line (?P<line>\d+)"#;
  18
  19static PYTHON_FILE_LINE_MATCHER: LazyLock<Regex> =
  20    LazyLock::new(|| Regex::new(PYTHON_FILE_LINE_REGEX).unwrap());
  21
  22fn python_extract_path_and_line(input: &str) -> Option<(&str, u32)> {
  23    if let Some(captures) = PYTHON_FILE_LINE_MATCHER.captures(input) {
  24        let path_part = captures.name("file")?.as_str();
  25
  26        let line_number: u32 = captures.name("line")?.as_str().parse().ok()?;
  27        return Some((path_part, line_number));
  28    }
  29    None
  30}
  31
  32pub(super) struct RegexSearches {
  33    url_regex: RegexSearch,
  34    word_regex: RegexSearch,
  35    python_file_line_regex: RegexSearch,
  36}
  37
  38impl RegexSearches {
  39    pub(super) fn new() -> Self {
  40        Self {
  41            url_regex: RegexSearch::new(URL_REGEX).unwrap(),
  42            word_regex: RegexSearch::new(WORD_REGEX).unwrap(),
  43            python_file_line_regex: RegexSearch::new(PYTHON_FILE_LINE_REGEX).unwrap(),
  44        }
  45    }
  46}
  47
  48pub(super) fn find_from_grid_point<T: EventListener>(
  49    term: &Term<T>,
  50    point: AlacPoint,
  51    regex_searches: &mut RegexSearches,
  52) -> Option<(String, bool, Match)> {
  53    let grid = term.grid();
  54    let link = grid.index(point).hyperlink();
  55    let found_word = if link.is_some() {
  56        let mut min_index = point;
  57        loop {
  58            let new_min_index = min_index.sub(term, Boundary::Cursor, 1);
  59            if new_min_index == min_index || grid.index(new_min_index).hyperlink() != link {
  60                break;
  61            } else {
  62                min_index = new_min_index
  63            }
  64        }
  65
  66        let mut max_index = point;
  67        loop {
  68            let new_max_index = max_index.add(term, Boundary::Cursor, 1);
  69            if new_max_index == max_index || grid.index(new_max_index).hyperlink() != link {
  70                break;
  71            } else {
  72                max_index = new_max_index
  73            }
  74        }
  75
  76        let url = link.unwrap().uri().to_owned();
  77        let url_match = min_index..=max_index;
  78
  79        Some((url, true, url_match))
  80    } else if let Some(url_match) = regex_match_at(term, point, &mut regex_searches.url_regex) {
  81        let url = term.bounds_to_string(*url_match.start(), *url_match.end());
  82        Some((url, true, url_match))
  83    } else if let Some(python_match) =
  84        regex_match_at(term, point, &mut regex_searches.python_file_line_regex)
  85    {
  86        let matching_line = term.bounds_to_string(*python_match.start(), *python_match.end());
  87        python_extract_path_and_line(&matching_line).map(|(file_path, line_number)| {
  88            (format!("{file_path}:{line_number}"), false, python_match)
  89        })
  90    } else if let Some(word_match) = regex_match_at(term, point, &mut regex_searches.word_regex) {
  91        let file_path = term.bounds_to_string(*word_match.start(), *word_match.end());
  92
  93        let (sanitized_match, sanitized_word) = 'sanitize: {
  94            let mut word_match = word_match;
  95            let mut file_path = file_path;
  96
  97            if is_path_surrounded_by_common_symbols(&file_path) {
  98                word_match = Match::new(
  99                    word_match.start().add(term, Boundary::Grid, 1),
 100                    word_match.end().sub(term, Boundary::Grid, 1),
 101                );
 102                file_path = file_path[1..file_path.len() - 1].to_owned();
 103            }
 104
 105            while file_path.ends_with(':') {
 106                file_path.pop();
 107                word_match = Match::new(
 108                    *word_match.start(),
 109                    word_match.end().sub(term, Boundary::Grid, 1),
 110                );
 111            }
 112            let mut colon_count = 0;
 113            for c in file_path.chars() {
 114                if c == ':' {
 115                    colon_count += 1;
 116                }
 117            }
 118            // strip trailing comment after colon in case of
 119            // file/at/path.rs:row:column:description or error message
 120            // so that the file path is `file/at/path.rs:row:column`
 121            if colon_count > 2 {
 122                let last_index = file_path.rfind(':').unwrap();
 123                let prev_is_digit = last_index > 0
 124                    && file_path
 125                        .chars()
 126                        .nth(last_index - 1)
 127                        .map_or(false, |c| c.is_ascii_digit());
 128                let next_is_digit = last_index < file_path.len() - 1
 129                    && file_path
 130                        .chars()
 131                        .nth(last_index + 1)
 132                        .map_or(true, |c| c.is_ascii_digit());
 133                if prev_is_digit && !next_is_digit {
 134                    let stripped_len = file_path.len() - last_index;
 135                    word_match = Match::new(
 136                        *word_match.start(),
 137                        word_match.end().sub(term, Boundary::Grid, stripped_len),
 138                    );
 139                    file_path = file_path[0..last_index].to_owned();
 140                }
 141            }
 142
 143            break 'sanitize (word_match, file_path);
 144        };
 145
 146        Some((sanitized_word, false, sanitized_match))
 147    } else {
 148        None
 149    };
 150
 151    found_word.map(|(maybe_url_or_path, is_url, word_match)| {
 152        if is_url {
 153            // Treat "file://" IRIs like file paths to ensure
 154            // that line numbers at the end of the path are
 155            // handled correctly
 156            if let Some(path) = maybe_url_or_path.strip_prefix("file://") {
 157                (path.to_string(), false, word_match)
 158            } else {
 159                (maybe_url_or_path, true, word_match)
 160            }
 161        } else {
 162            (maybe_url_or_path, false, word_match)
 163        }
 164    })
 165}
 166
 167fn is_path_surrounded_by_common_symbols(path: &str) -> bool {
 168    // Avoid detecting `[]` or `()` strings as paths, surrounded by common symbols
 169    path.len() > 2
 170        // The rest of the brackets and various quotes cannot be matched by the [`WORD_REGEX`] hence not checked for.
 171        && (path.starts_with('[') && path.ends_with(']')
 172            || path.starts_with('(') && path.ends_with(')'))
 173}
 174
 175/// Based on alacritty/src/display/hint.rs > regex_match_at
 176/// Retrieve the match, if the specified point is inside the content matching the regex.
 177fn regex_match_at<T>(term: &Term<T>, point: AlacPoint, regex: &mut RegexSearch) -> Option<Match> {
 178    visible_regex_match_iter(term, regex).find(|rm| rm.contains(&point))
 179}
 180
 181/// Copied from alacritty/src/display/hint.rs:
 182/// Iterate over all visible regex matches.
 183fn visible_regex_match_iter<'a, T>(
 184    term: &'a Term<T>,
 185    regex: &'a mut RegexSearch,
 186) -> impl Iterator<Item = Match> + 'a {
 187    const MAX_SEARCH_LINES: usize = 100;
 188
 189    let viewport_start = Line(-(term.grid().display_offset() as i32));
 190    let viewport_end = viewport_start + term.bottommost_line();
 191    let mut start = term.line_search_left(AlacPoint::new(viewport_start, Column(0)));
 192    let mut end = term.line_search_right(AlacPoint::new(viewport_end, Column(0)));
 193    start.line = start.line.max(viewport_start - MAX_SEARCH_LINES);
 194    end.line = end.line.min(viewport_end + MAX_SEARCH_LINES);
 195
 196    RegexIter::new(start, end, AlacDirection::Right, term, regex)
 197        .skip_while(move |rm| rm.end().line < viewport_start)
 198        .take_while(move |rm| rm.start().line <= viewport_end)
 199}
 200
 201#[cfg(test)]
 202mod tests {
 203    use super::*;
 204    use alacritty_terminal::{
 205        event::VoidListener,
 206        index::{Boundary, Point as AlacPoint},
 207        term::{Config, cell::Flags, test::TermSize},
 208        vte::ansi::Handler,
 209    };
 210    use std::{cell::RefCell, ops::RangeInclusive, path::PathBuf};
 211    use url::Url;
 212    use util::paths::PathWithPosition;
 213
 214    fn re_test(re: &str, hay: &str, expected: Vec<&str>) {
 215        let results: Vec<_> = regex::Regex::new(re)
 216            .unwrap()
 217            .find_iter(hay)
 218            .map(|m| m.as_str())
 219            .collect();
 220        assert_eq!(results, expected);
 221    }
 222
 223    #[test]
 224    fn test_url_regex() {
 225        re_test(
 226            URL_REGEX,
 227            "test http://example.com test mailto:bob@example.com train",
 228            vec!["http://example.com", "mailto:bob@example.com"],
 229        );
 230    }
 231
 232    #[test]
 233    fn test_word_regex() {
 234        re_test(
 235            WORD_REGEX,
 236            "hello, world! \"What\" is this?",
 237            vec!["hello", "world", "What", "is", "this"],
 238        );
 239    }
 240
 241    #[test]
 242    fn test_word_regex_with_linenum() {
 243        // filename(line) and filename(line,col) as used in MSBuild output
 244        // should be considered a single "word", even though comma is
 245        // usually a word separator
 246        re_test(WORD_REGEX, "a Main.cs(20) b", vec!["a", "Main.cs(20)", "b"]);
 247        re_test(
 248            WORD_REGEX,
 249            "Main.cs(20,5) Error desc",
 250            vec!["Main.cs(20,5)", "Error", "desc"],
 251        );
 252        // filename:line:col is a popular format for unix tools
 253        re_test(
 254            WORD_REGEX,
 255            "a Main.cs:20:5 b",
 256            vec!["a", "Main.cs:20:5", "b"],
 257        );
 258        // Some tools output "filename:line:col:message", which currently isn't
 259        // handled correctly, but might be in the future
 260        re_test(
 261            WORD_REGEX,
 262            "Main.cs:20:5:Error desc",
 263            vec!["Main.cs:20:5:Error", "desc"],
 264        );
 265    }
 266
 267    #[test]
 268    fn test_python_file_line_regex() {
 269        re_test(
 270            PYTHON_FILE_LINE_REGEX,
 271            "hay File \"/zed/bad_py.py\", line 8 stack",
 272            vec!["File \"/zed/bad_py.py\", line 8"],
 273        );
 274        re_test(PYTHON_FILE_LINE_REGEX, "unrelated", vec![]);
 275    }
 276
 277    #[test]
 278    fn test_python_file_line() {
 279        let inputs: Vec<(&str, Option<(&str, u32)>)> = vec![
 280            (
 281                "File \"/zed/bad_py.py\", line 8",
 282                Some(("/zed/bad_py.py", 8u32)),
 283            ),
 284            ("File \"path/to/zed/bad_py.py\"", None),
 285            ("unrelated", None),
 286            ("", None),
 287        ];
 288        let actual = inputs
 289            .iter()
 290            .map(|input| python_extract_path_and_line(input.0))
 291            .collect::<Vec<_>>();
 292        let expected = inputs.iter().map(|(_, output)| *output).collect::<Vec<_>>();
 293        assert_eq!(actual, expected);
 294    }
 295
 296    // We use custom columns in many tests to workaround this issue by ensuring a wrapped
 297    // line never ends on a wide char:
 298    //
 299    // <https://github.com/alacritty/alacritty/issues/8586>
 300    //
 301    // This issue was recently fixed, as soon as we update to a version containing the fix we
 302    // can remove all the custom columns from these tests.
 303    //
 304    macro_rules! test_hyperlink {
 305        ($($lines:expr),+; $hyperlink_kind:ident) => { {
 306            use crate::terminal_hyperlinks::tests::line_cells_count;
 307            use std::cmp;
 308
 309            let test_lines = vec![$($lines),+];
 310            let (total_cells, longest_line_cells) =
 311                test_lines.iter().copied()
 312                    .map(line_cells_count)
 313                    .fold((0, 0), |state, cells| (state.0 + cells, cmp::max(state.1, cells)));
 314
 315            test_hyperlink!(
 316                // Alacritty has issues with 2 columns, use 3 as the minimum for now.
 317                [3, longest_line_cells / 2, longest_line_cells + 1];
 318                total_cells;
 319                test_lines.iter().copied();
 320                $hyperlink_kind
 321            )
 322        } };
 323
 324        ($($columns:literal),+; $($lines:expr),+; $hyperlink_kind:ident) => { {
 325            use crate::terminal_hyperlinks::tests::line_cells_count;
 326
 327            let test_lines = vec![$($lines),+];
 328            let total_cells = test_lines.iter().copied().map(line_cells_count).sum();
 329
 330            test_hyperlink!(
 331                [ $($columns),+ ]; total_cells; test_lines.iter().copied(); $hyperlink_kind
 332            )
 333        } };
 334
 335        ([ $($columns:expr),+ ]; $total_cells:expr; $lines:expr; $hyperlink_kind:ident) => { {
 336            use crate::terminal_hyperlinks::tests::{ test_hyperlink, HyperlinkKind };
 337
 338            let source_location = format!("{}:{}", std::file!(), std::line!());
 339            for columns in vec![ $($columns),+] {
 340                test_hyperlink(columns, $total_cells, $lines, HyperlinkKind::$hyperlink_kind,
 341                    &source_location);
 342            }
 343        } };
 344    }
 345
 346    mod path {
 347        /// 👉 := **hovered** on following char
 348        ///
 349        /// 👈 := **hovered** on wide char spacer of previous full width char
 350        ///
 351        /// **`‹›`** := expected **hyperlink** match
 352        ///
 353        /// **`«»`** := expected **path**, **row**, and **column** capture groups
 354        ///
 355        /// [**`c₀, c₁, …, cₙ;`**]ₒₚₜ := use specified terminal widths of `c₀, c₁, …, cₙ` **columns**
 356        /// (defaults to `3, longest_line_cells / 2, longest_line_cells + 1;`)
 357        ///
 358        macro_rules! test_path {
 359            ($($lines:literal),+) => { test_hyperlink!($($lines),+; Path) };
 360            ($($columns:literal),+; $($lines:literal),+) => {
 361                test_hyperlink!($($columns),+; $($lines),+; Path)
 362            };
 363        }
 364
 365        #[test]
 366        fn simple() {
 367            // Rust paths
 368            // Just the path
 369            test_path!("‹«/👉test/cool.rs»›");
 370            test_path!("‹«/test/cool👉.rs»›");
 371
 372            // path and line
 373            test_path!("‹«/👉test/cool.rs»:«4»›");
 374            test_path!("‹«/test/cool.rs»👉:«4»›");
 375            test_path!("‹«/test/cool.rs»:«👉4»›");
 376            test_path!("‹«/👉test/cool.rs»(«4»)›");
 377            test_path!("‹«/test/cool.rs»👉(«4»)›");
 378            test_path!("‹«/test/cool.rs»(«👉4»)›");
 379            test_path!("‹«/test/cool.rs»(«4»👉)›");
 380
 381            // path, line, and column
 382            test_path!("‹«/👉test/cool.rs»:«4»:«2»›");
 383            test_path!("‹«/test/cool.rs»:«4»:«👉2»›");
 384            test_path!("‹«/👉test/cool.rs»(«4»,«2»)›");
 385            test_path!("‹«/test/cool.rs»(«4»👉,«2»)›");
 386
 387            // path, line, column, and ':' suffix
 388            test_path!("‹«/👉test/cool.rs»:«4»:«2»›:");
 389            test_path!("‹«/test/cool.rs»:«4»:«👉2»›:");
 390            test_path!("‹«/👉test/cool.rs»(«4»,«2»)›:");
 391            test_path!("‹«/test/cool.rs»(«4»,«2»👉)›:");
 392
 393            // path, line, column, and description
 394            test_path!("‹«/test/cool.rs»:«4»:«2»›👉:Error!");
 395            test_path!("‹«/test/cool.rs»:«4»:«2»›:👉Error!");
 396            test_path!("‹«/test/co👉ol.rs»(«4»,«2»)›:Error!");
 397
 398            // Cargo output
 399            test_path!("    Compiling Cool 👉(‹«/test/Cool»›)");
 400            test_path!("    Compiling Cool (‹«/👉test/Cool»›)");
 401            test_path!("    Compiling Cool (‹«/test/Cool»›👉)");
 402
 403            // Python
 404            test_path!("‹«awe👉some.py»›");
 405
 406            test_path!("    ‹F👉ile \"«/awesome.py»\", line «42»›: Wat?");
 407            test_path!("    ‹File \"«/awe👉some.py»\", line «42»›: Wat?");
 408            test_path!("    ‹File \"«/awesome.py»👉\", line «42»›: Wat?");
 409            test_path!("    ‹File \"«/awesome.py»\", line «4👉2»›: Wat?");
 410        }
 411
 412        #[test]
 413        fn colons_galore() {
 414            test_path!("‹«/test/co👉ol.rs»:«4»›");
 415            test_path!("‹«/test/co👉ol.rs»:«4»›:");
 416            test_path!("‹«/test/co👉ol.rs»:«4»:«2»›");
 417            test_path!("‹«/test/co👉ol.rs»:«4»:«2»›:");
 418            test_path!("‹«/test/co👉ol.rs»(«1»)›");
 419            test_path!("‹«/test/co👉ol.rs»(«1»)›:");
 420            test_path!("‹«/test/co👉ol.rs»(«1»,«618»)›");
 421            test_path!("‹«/test/co👉ol.rs»(«1»,«618»)›:");
 422            test_path!("‹«/test/co👉ol.rs»::«42»›");
 423            test_path!("‹«/test/co👉ol.rs»::«42»›:");
 424            test_path!("‹«/test/co👉ol.rs:4:2»(«1»,«618»)›");
 425            test_path!("‹«/test/co👉ol.rs»(«1»,«618»)›::");
 426        }
 427
 428        #[test]
 429        fn word_wide_chars() {
 430            // Rust paths
 431            test_path!(4, 6, 12; "‹«/👉例/cool.rs»›");
 432            test_path!(4, 6, 12; "‹«/例👈/cool.rs»›");
 433            test_path!(4, 8, 16; "‹«/例/cool.rs»:«👉4»›");
 434            test_path!(4, 8, 16; "‹«/例/cool.rs»:«4»:«👉2»›");
 435
 436            // Cargo output
 437            test_path!(4, 27, 30; "    Compiling Cool (‹«/👉例/Cool»›)");
 438            test_path!(4, 27, 30; "    Compiling Cool (‹«/例👈/Cool»›)");
 439
 440            // Python
 441            test_path!(4, 11; "‹«👉例wesome.py»›");
 442            test_path!(4, 11; "‹«例👈wesome.py»›");
 443            test_path!(6, 17, 40; "    ‹File \"«/👉例wesome.py»\", line «42»›: Wat?");
 444            test_path!(6, 17, 40; "    ‹File \"«/例👈wesome.py»\", line «42»›: Wat?");
 445        }
 446
 447        #[test]
 448        fn non_word_wide_chars() {
 449            // Mojo diagnostic message
 450            test_path!(4, 18, 38; "    ‹File \"«/awe👉some.🔥»\", line «42»›: Wat?");
 451            test_path!(4, 18, 38; "    ‹File \"«/awesome👉.🔥»\", line «42»›: Wat?");
 452            test_path!(4, 18, 38; "    ‹File \"«/awesome.👉🔥»\", line «42»›: Wat?");
 453            test_path!(4, 18, 38; "    ‹File \"«/awesome.🔥👈»\", line «42»›: Wat?");
 454        }
 455
 456        /// These likely rise to the level of being worth fixing.
 457        mod issues {
 458            #[test]
 459            #[cfg_attr(not(target_os = "windows"), should_panic(expected = "Path = «例»"))]
 460            #[cfg_attr(target_os = "windows", should_panic(expected = r#"Path = «C:\\例»"#))]
 461            // <https://github.com/alacritty/alacritty/issues/8586>
 462            fn issue_alacritty_8586() {
 463                // Rust paths
 464                test_path!("‹«/👉例/cool.rs»›");
 465                test_path!("‹«/例👈/cool.rs»›");
 466                test_path!("‹«/例/cool.rs»:«👉4»›");
 467                test_path!("‹«/例/cool.rs»:«4»:«👉2»›");
 468
 469                // Cargo output
 470                test_path!("    Compiling Cool (‹«/👉例/Cool»›)");
 471                test_path!("    Compiling Cool (‹«/例👈/Cool»›)");
 472
 473                // Python
 474                test_path!("‹«👉例wesome.py»›");
 475                test_path!("‹«例👈wesome.py»›");
 476                test_path!("    ‹File \"«/👉例wesome.py»\", line «42»›: Wat?");
 477                test_path!("    ‹File \"«/例👈wesome.py»\", line «42»›: Wat?");
 478            }
 479
 480            #[test]
 481            #[should_panic(expected = "No hyperlink found")]
 482            // <https://github.com/zed-industries/zed/issues/12338>
 483            fn issue_12338() {
 484                // Issue #12338
 485                test_path!(".rw-r--r--     0     staff 05-27 14:03 ‹«test👉、2.txt»›");
 486                test_path!(".rw-r--r--     0     staff 05-27 14:03 ‹«test、👈2.txt»›");
 487                test_path!(".rw-r--r--     0     staff 05-27 14:03 ‹«test👉。3.txt»›");
 488                test_path!(".rw-r--r--     0     staff 05-27 14:03 ‹«test。👈3.txt»›");
 489
 490                // Rust paths
 491                test_path!("‹«/👉🏃/🦀.rs»›");
 492                test_path!("‹«/🏃👈/🦀.rs»›");
 493                test_path!("‹«/🏃/👉🦀.rs»:«4»›");
 494                test_path!("‹«/🏃/🦀👈.rs»:«4»:«2»›");
 495
 496                // Cargo output
 497                test_path!("    Compiling Cool (‹«/👉🏃/Cool»›)");
 498                test_path!("    Compiling Cool (‹«/🏃👈/Cool»›)");
 499
 500                // Python
 501                test_path!("‹«👉🏃wesome.py»›");
 502                test_path!("‹«🏃👈wesome.py»›");
 503                test_path!("    ‹File \"«/👉🏃wesome.py»\", line «42»›: Wat?");
 504                test_path!("    ‹File \"«/🏃👈wesome.py»\", line «42»›: Wat?");
 505
 506                // Mojo
 507                test_path!("‹«/awe👉some.🔥»› is some good Mojo!");
 508                test_path!("‹«/awesome👉.🔥»› is some good Mojo!");
 509                test_path!("‹«/awesome.👉🔥»› is some good Mojo!");
 510                test_path!("‹«/awesome.🔥👈»› is some good Mojo!");
 511                test_path!("    ‹File \"«/👉🏃wesome.🔥»\", line «42»›: Wat?");
 512                test_path!("    ‹File \"«/🏃👈wesome.🔥»\", line «42»›: Wat?");
 513            }
 514
 515            #[test]
 516            #[cfg_attr(
 517                not(target_os = "windows"),
 518                should_panic(
 519                    expected = "Path = «test/controllers/template_items_controller_test.rb», line = 20, at grid cells (0, 0)..=(17, 1)"
 520                )
 521            )]
 522            #[cfg_attr(
 523                target_os = "windows",
 524                should_panic(
 525                    expected = r#"Path = «test\\controllers\\template_items_controller_test.rb», line = 20, at grid cells (0, 0)..=(17, 1)"#
 526                )
 527            )]
 528            // <https://github.com/zed-industries/zed/issues/28194>
 529            //
 530            // #28194 was closed, but the link includes the description part (":in" here), which
 531            // seems wrong...
 532            fn issue_28194() {
 533                test_path!(
 534                    "‹«test/c👉ontrollers/template_items_controller_test.rb»:«20»›:in 'block (2 levels) in <class:TemplateItemsControllerTest>'"
 535                );
 536                test_path!(
 537                    "‹«test/controllers/template_items_controller_test.rb»:«19»›:i👉n 'block in <class:TemplateItemsControllerTest>'"
 538                );
 539            }
 540        }
 541
 542        /// Minor issues arguably not important enough to fix/workaround...
 543        mod nits {
 544            #[test]
 545            #[cfg_attr(
 546                not(target_os = "windows"),
 547                should_panic(expected = "Path = «/test/cool.rs(4»")
 548            )]
 549            #[cfg_attr(
 550                target_os = "windows",
 551                should_panic(expected = r#"Path = «C:\\test\\cool.rs(4»"#)
 552            )]
 553            fn alacritty_bugs_with_two_columns() {
 554                test_path!(2; "‹«/👉test/cool.rs»(«4»)›");
 555                test_path!(2; "‹«/test/cool.rs»(«👉4»)›");
 556                test_path!(2; "‹«/test/cool.rs»(«4»,«👉2»)›");
 557
 558                // Python
 559                test_path!(2; "‹«awe👉some.py»›");
 560            }
 561
 562            #[test]
 563            #[cfg_attr(
 564                not(target_os = "windows"),
 565                should_panic(
 566                    expected = "Path = «/test/cool.rs», line = 1, at grid cells (0, 0)..=(9, 0)"
 567                )
 568            )]
 569            #[cfg_attr(
 570                target_os = "windows",
 571                should_panic(
 572                    expected = r#"Path = «C:\\test\\cool.rs», line = 1, at grid cells (0, 0)..=(9, 2)"#
 573                )
 574            )]
 575            fn invalid_row_column_should_be_part_of_path() {
 576                test_path!("‹«/👉test/cool.rs:1:618033988749»›");
 577                test_path!("‹«/👉test/cool.rs(1,618033988749)»›");
 578            }
 579
 580            #[test]
 581            #[should_panic(expected = "Path = «»")]
 582            fn colon_suffix_succeeds_in_finding_an_empty_maybe_path() {
 583                test_path!("‹«/test/cool.rs»:«4»:«2»›👉:", "What is this?");
 584                test_path!("‹«/test/cool.rs»(«4»,«2»)›👉:", "What is this?");
 585            }
 586
 587            #[test]
 588            #[cfg_attr(
 589                not(target_os = "windows"),
 590                should_panic(expected = "Path = «/test/cool.rs»")
 591            )]
 592            #[cfg_attr(
 593                target_os = "windows",
 594                should_panic(expected = r#"Path = «C:\\test\\cool.rs»"#)
 595            )]
 596            fn many_trailing_colons_should_be_parsed_as_part_of_the_path() {
 597                test_path!("‹«/test/cool.rs:::👉:»›");
 598                test_path!("‹«/te:st/👉co:ol.r:s:4:2::::::»›");
 599            }
 600        }
 601
 602        #[cfg(target_os = "windows")]
 603        mod windows {
 604            // Lots of fun to be had with long file paths (verbatim) and UNC paths on Windows.
 605            // See <https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation>
 606            // See <https://users.rust-lang.org/t/understanding-windows-paths/58583>
 607            // See <https://github.com/rust-lang/cargo/issues/13919>
 608
 609            #[test]
 610            fn unc() {
 611                test_path!(r#"‹«\\server\share\👉test\cool.rs»›"#);
 612                test_path!(r#"‹«\\server\share\test\cool👉.rs»›"#);
 613            }
 614
 615            mod issues {
 616                #[test]
 617                #[should_panic(
 618                    expected = r#"Path = «C:\\test\\cool.rs», at grid cells (0, 0)..=(6, 0)"#
 619                )]
 620                fn issue_verbatim() {
 621                    test_path!(r#"‹«\\?\C:\👉test\cool.rs»›"#);
 622                    test_path!(r#"‹«\\?\C:\test\cool👉.rs»›"#);
 623                }
 624
 625                #[test]
 626                #[should_panic(
 627                    expected = r#"Path = «\\\\server\\share\\test\\cool.rs», at grid cells (0, 0)..=(10, 2)"#
 628                )]
 629                fn issue_verbatim_unc() {
 630                    test_path!(r#"‹«\\?\UNC\server\share\👉test\cool.rs»›"#);
 631                    test_path!(r#"‹«\\?\UNC\server\share\test\cool👉.rs»›"#);
 632                }
 633            }
 634        }
 635    }
 636
 637    mod file_iri {
 638        // File IRIs have a ton of use cases, most of which we currently do not support. A few of
 639        // those cases are documented here as tests which are expected to fail.
 640        // See https://en.wikipedia.org/wiki/File_URI_scheme
 641
 642        /// [**`c₀, c₁, …, cₙ;`**]ₒₚₜ := use specified terminal widths of `c₀, c₁, …, cₙ` **columns**
 643        /// (defaults to `3, longest_line_cells / 2, longest_line_cells + 1;`)
 644        ///
 645        macro_rules! test_file_iri {
 646            ($file_iri:literal) => { { test_hyperlink!(concat!("‹«👉", $file_iri, "»›"); FileIri) } };
 647            ($($columns:literal),+; $file_iri:literal) => { {
 648                test_hyperlink!($($columns),+; concat!("‹«👉", $file_iri, "»›"); FileIri)
 649            } };
 650        }
 651
 652        #[cfg(not(target_os = "windows"))]
 653        #[test]
 654        fn absolute_file_iri() {
 655            test_file_iri!("file:///test/cool/index.rs");
 656            test_file_iri!("file:///test/cool/");
 657        }
 658
 659        mod issues {
 660            #[cfg(not(target_os = "windows"))]
 661            #[test]
 662            #[should_panic(expected = "Path = «/test/Ῥόδος/», at grid cells (0, 0)..=(15, 1)")]
 663            fn issue_file_iri_with_percent_encoded_characters() {
 664                // Non-space characters
 665                // file:///test/Ῥόδος/
 666                test_file_iri!("file:///test/%E1%BF%AC%CF%8C%CE%B4%CE%BF%CF%82/"); // URI
 667
 668                // Spaces
 669                test_file_iri!("file:///te%20st/co%20ol/index.rs");
 670                test_file_iri!("file:///te%20st/co%20ol/");
 671            }
 672        }
 673
 674        #[cfg(target_os = "windows")]
 675        mod windows {
 676            mod issues {
 677                // The test uses Url::to_file_path(), but it seems that the Url crate doesn't
 678                // support relative file IRIs.
 679                #[test]
 680                #[should_panic(
 681                    expected = r#"Failed to interpret file IRI `file:/test/cool/index.rs` as a path"#
 682                )]
 683                fn issue_relative_file_iri() {
 684                    test_file_iri!("file:/test/cool/index.rs");
 685                    test_file_iri!("file:/test/cool/");
 686                }
 687
 688                // See https://en.wikipedia.org/wiki/File_URI_scheme
 689                #[test]
 690                #[should_panic(
 691                    expected = r#"Path = «C:\\test\\cool\\index.rs», at grid cells (0, 0)..=(9, 1)"#
 692                )]
 693                fn issue_absolute_file_iri() {
 694                    test_file_iri!("file:///C:/test/cool/index.rs");
 695                    test_file_iri!("file:///C:/test/cool/");
 696                }
 697
 698                #[test]
 699                #[should_panic(
 700                    expected = r#"Path = «C:\\test\\Ῥόδος\\», at grid cells (0, 0)..=(16, 1)"#
 701                )]
 702                fn issue_file_iri_with_percent_encoded_characters() {
 703                    // Non-space characters
 704                    // file:///test/Ῥόδος/
 705                    test_file_iri!("file:///C:/test/%E1%BF%AC%CF%8C%CE%B4%CE%BF%CF%82/"); // URI
 706
 707                    // Spaces
 708                    test_file_iri!("file:///C:/te%20st/co%20ol/index.rs");
 709                    test_file_iri!("file:///C:/te%20st/co%20ol/");
 710                }
 711            }
 712        }
 713    }
 714
 715    mod iri {
 716        /// [**`c₀, c₁, …, cₙ;`**]ₒₚₜ := use specified terminal widths of `c₀, c₁, …, cₙ` **columns**
 717        /// (defaults to `3, longest_line_cells / 2, longest_line_cells + 1;`)
 718        ///
 719        macro_rules! test_iri {
 720            ($iri:literal) => { { test_hyperlink!(concat!("‹«👉", $iri, "»›"); Iri) } };
 721            ($($columns:literal),+; $iri:literal) => { {
 722                test_hyperlink!($($columns),+; concat!("‹«👉", $iri, "»›"); Iri)
 723            } };
 724        }
 725
 726        #[test]
 727        fn simple() {
 728            // In the order they appear in URL_REGEX, except 'file://' which is treated as a path
 729            test_iri!("ipfs://test/cool.ipfs");
 730            test_iri!("ipns://test/cool.ipns");
 731            test_iri!("magnet://test/cool.git");
 732            test_iri!("mailto:someone@somewhere.here");
 733            test_iri!("gemini://somewhere.here");
 734            test_iri!("gopher://somewhere.here");
 735            test_iri!("http://test/cool/index.html");
 736            test_iri!("http://10.10.10.10:1111/cool.html");
 737            test_iri!("http://test/cool/index.html?amazing=1");
 738            test_iri!("http://test/cool/index.html#right%20here");
 739            test_iri!("http://test/cool/index.html?amazing=1#right%20here");
 740            test_iri!("https://test/cool/index.html");
 741            test_iri!("https://10.10.10.10:1111/cool.html");
 742            test_iri!("https://test/cool/index.html?amazing=1");
 743            test_iri!("https://test/cool/index.html#right%20here");
 744            test_iri!("https://test/cool/index.html?amazing=1#right%20here");
 745            test_iri!("news://test/cool.news");
 746            test_iri!("git://test/cool.git");
 747            test_iri!("ssh://user@somewhere.over.here:12345/test/cool.git");
 748            test_iri!("ftp://test/cool.ftp");
 749        }
 750
 751        #[test]
 752        fn wide_chars() {
 753            // In the order they appear in URL_REGEX, except 'file://' which is treated as a path
 754            test_iri!(4, 20; "ipfs://例🏃🦀/cool.ipfs");
 755            test_iri!(4, 20; "ipns://例🏃🦀/cool.ipns");
 756            test_iri!(6, 20; "magnet://例🏃🦀/cool.git");
 757            test_iri!(4, 20; "mailto:someone@somewhere.here");
 758            test_iri!(4, 20; "gemini://somewhere.here");
 759            test_iri!(4, 20; "gopher://somewhere.here");
 760            test_iri!(4, 20; "http://例🏃🦀/cool/index.html");
 761            test_iri!(4, 20; "http://10.10.10.10:1111/cool.html");
 762            test_iri!(4, 20; "http://例🏃🦀/cool/index.html?amazing=1");
 763            test_iri!(4, 20; "http://例🏃🦀/cool/index.html#right%20here");
 764            test_iri!(4, 20; "http://例🏃🦀/cool/index.html?amazing=1#right%20here");
 765            test_iri!(4, 20; "https://例🏃🦀/cool/index.html");
 766            test_iri!(4, 20; "https://10.10.10.10:1111/cool.html");
 767            test_iri!(4, 20; "https://例🏃🦀/cool/index.html?amazing=1");
 768            test_iri!(4, 20; "https://例🏃🦀/cool/index.html#right%20here");
 769            test_iri!(4, 20; "https://例🏃🦀/cool/index.html?amazing=1#right%20here");
 770            test_iri!(4, 20; "news://例🏃🦀/cool.news");
 771            test_iri!(5, 20; "git://例/cool.git");
 772            test_iri!(5, 20; "ssh://user@somewhere.over.here:12345/例🏃🦀/cool.git");
 773            test_iri!(7, 20; "ftp://例🏃🦀/cool.ftp");
 774        }
 775
 776        // There are likely more tests needed for IRI vs URI
 777        #[test]
 778        fn iris() {
 779            // These refer to the same location, see example here:
 780            // <https://en.wikipedia.org/wiki/Internationalized_Resource_Identifier#Compatibility>
 781            test_iri!("https://en.wiktionary.org/wiki/Ῥόδος"); // IRI
 782            test_iri!("https://en.wiktionary.org/wiki/%E1%BF%AC%CF%8C%CE%B4%CE%BF%CF%82"); // URI
 783        }
 784
 785        #[test]
 786        #[should_panic(expected = "Expected a path, but was a iri")]
 787        fn file_is_a_path() {
 788            test_iri!("file://test/cool/index.rs");
 789        }
 790    }
 791
 792    #[derive(Debug, PartialEq)]
 793    enum HyperlinkKind {
 794        FileIri,
 795        Iri,
 796        Path,
 797    }
 798
 799    struct ExpectedHyperlink {
 800        hovered_grid_point: AlacPoint,
 801        hovered_char: char,
 802        hyperlink_kind: HyperlinkKind,
 803        iri_or_path: String,
 804        row: Option<u32>,
 805        column: Option<u32>,
 806        hyperlink_match: RangeInclusive<AlacPoint>,
 807    }
 808
 809    /// Converts to Windows style paths on Windows, like path!(), but at runtime for improved test
 810    /// readability.
 811    fn build_term_from_test_lines<'a>(
 812        hyperlink_kind: HyperlinkKind,
 813        term_size: TermSize,
 814        test_lines: impl Iterator<Item = &'a str>,
 815    ) -> (Term<VoidListener>, ExpectedHyperlink) {
 816        #[derive(Default, Eq, PartialEq)]
 817        enum HoveredState {
 818            #[default]
 819            HoveredScan,
 820            HoveredNextChar,
 821            Done,
 822        }
 823
 824        #[derive(Default, Eq, PartialEq)]
 825        enum MatchState {
 826            #[default]
 827            MatchScan,
 828            MatchNextChar,
 829            Match(AlacPoint),
 830            Done,
 831        }
 832
 833        #[derive(Default, Eq, PartialEq)]
 834        enum CapturesState {
 835            #[default]
 836            PathScan,
 837            PathNextChar,
 838            Path(AlacPoint),
 839            RowScan,
 840            Row(String),
 841            ColumnScan,
 842            Column(String),
 843            Done,
 844        }
 845
 846        fn prev_input_point_from_term(term: &Term<VoidListener>) -> AlacPoint {
 847            let grid = term.grid();
 848            let cursor = &grid.cursor;
 849            let mut point = cursor.point;
 850
 851            if !cursor.input_needs_wrap {
 852                point.column -= 1;
 853            }
 854
 855            if grid.index(point).flags.contains(Flags::WIDE_CHAR_SPACER) {
 856                point.column -= 1;
 857            }
 858
 859            point
 860        }
 861
 862        let mut hovered_grid_point: Option<AlacPoint> = None;
 863        let mut hyperlink_match = AlacPoint::default()..=AlacPoint::default();
 864        let mut iri_or_path = String::default();
 865        let mut row = None;
 866        let mut column = None;
 867        let mut prev_input_point = AlacPoint::default();
 868        let mut hovered_state = HoveredState::default();
 869        let mut match_state = MatchState::default();
 870        let mut captures_state = CapturesState::default();
 871        let mut term = Term::new(Config::default(), &term_size, VoidListener);
 872
 873        for text in test_lines {
 874            let chars: Box<dyn Iterator<Item = char>> =
 875                if cfg!(windows) && hyperlink_kind == HyperlinkKind::Path {
 876                    Box::new(text.chars().map(|c| if c == '/' { '\\' } else { c })) as _
 877                } else {
 878                    Box::new(text.chars()) as _
 879                };
 880            let mut chars = chars.peekable();
 881            while let Some(c) = chars.next() {
 882                match c {
 883                    '👉' => {
 884                        hovered_state = HoveredState::HoveredNextChar;
 885                    }
 886                    '👈' => {
 887                        hovered_grid_point = Some(prev_input_point.add(&term, Boundary::Grid, 1));
 888                    }
 889                    '«' | '»' => {
 890                        captures_state = match captures_state {
 891                            CapturesState::PathScan => CapturesState::PathNextChar,
 892                            CapturesState::PathNextChar => {
 893                                panic!("Should have been handled by char input")
 894                            }
 895                            CapturesState::Path(start_point) => {
 896                                iri_or_path = term.bounds_to_string(start_point, prev_input_point);
 897                                CapturesState::RowScan
 898                            }
 899                            CapturesState::RowScan => CapturesState::Row(String::new()),
 900                            CapturesState::Row(number) => {
 901                                row = Some(number.parse::<u32>().unwrap());
 902                                CapturesState::ColumnScan
 903                            }
 904                            CapturesState::ColumnScan => CapturesState::Column(String::new()),
 905                            CapturesState::Column(number) => {
 906                                column = Some(number.parse::<u32>().unwrap());
 907                                CapturesState::Done
 908                            }
 909                            CapturesState::Done => {
 910                                panic!("Extra '«', '»'")
 911                            }
 912                        }
 913                    }
 914                    '‹' | '›' => {
 915                        match_state = match match_state {
 916                            MatchState::MatchScan => MatchState::MatchNextChar,
 917                            MatchState::MatchNextChar => {
 918                                panic!("Should have been handled by char input")
 919                            }
 920                            MatchState::Match(start_point) => {
 921                                hyperlink_match = start_point..=prev_input_point;
 922                                MatchState::Done
 923                            }
 924                            MatchState::Done => {
 925                                panic!("Extra '‹', '›'")
 926                            }
 927                        }
 928                    }
 929                    _ => {
 930                        if let CapturesState::Row(number) | CapturesState::Column(number) =
 931                            &mut captures_state
 932                        {
 933                            number.push(c)
 934                        }
 935
 936                        let is_windows_abs_path_start = captures_state
 937                            == CapturesState::PathNextChar
 938                            && cfg!(windows)
 939                            && hyperlink_kind == HyperlinkKind::Path
 940                            && c == '\\'
 941                            && chars.peek().is_some_and(|c| *c != '\\');
 942
 943                        if is_windows_abs_path_start {
 944                            // Convert Unix abs path start into Windows abs path start so that the
 945                            // same test can be used for both OSes.
 946                            term.input('C');
 947                            prev_input_point = prev_input_point_from_term(&term);
 948                            term.input(':');
 949                            term.input(c);
 950                        } else {
 951                            term.input(c);
 952                            prev_input_point = prev_input_point_from_term(&term);
 953                        }
 954
 955                        if hovered_state == HoveredState::HoveredNextChar {
 956                            hovered_grid_point = Some(prev_input_point);
 957                            hovered_state = HoveredState::Done;
 958                        }
 959                        if captures_state == CapturesState::PathNextChar {
 960                            captures_state = CapturesState::Path(prev_input_point);
 961                        }
 962                        if match_state == MatchState::MatchNextChar {
 963                            match_state = MatchState::Match(prev_input_point);
 964                        }
 965                    }
 966                }
 967            }
 968            term.move_down_and_cr(1);
 969        }
 970
 971        if hyperlink_kind == HyperlinkKind::FileIri {
 972            let Ok(url) = Url::parse(&iri_or_path) else {
 973                panic!("Failed to parse file IRI `{iri_or_path}`");
 974            };
 975            let Ok(path) = url.to_file_path() else {
 976                panic!("Failed to interpret file IRI `{iri_or_path}` as a path");
 977            };
 978            iri_or_path = path.to_string_lossy().to_string();
 979        }
 980
 981        if cfg!(windows) {
 982            // Handle verbatim and UNC paths for Windows
 983            if let Some(stripped) = iri_or_path.strip_prefix(r#"\\?\UNC\"#) {
 984                iri_or_path = format!(r#"\\{stripped}"#);
 985            } else if let Some(stripped) = iri_or_path.strip_prefix(r#"\\?\"#) {
 986                iri_or_path = stripped.to_string();
 987            }
 988        }
 989
 990        let hovered_grid_point = hovered_grid_point.expect("Missing hovered point (👉 or 👈)");
 991        let hovered_char = term.grid().index(hovered_grid_point).c;
 992        (
 993            term,
 994            ExpectedHyperlink {
 995                hovered_grid_point,
 996                hovered_char,
 997                hyperlink_kind,
 998                iri_or_path,
 999                row,
1000                column,
1001                hyperlink_match,
1002            },
1003        )
1004    }
1005
1006    fn line_cells_count(line: &str) -> usize {
1007        // This avoids taking a dependency on the unicode-width crate
1008        fn width(c: char) -> usize {
1009            match c {
1010                // Fullwidth unicode characters used in tests
1011                '例' | '🏃' | '🦀' | '🔥' => 2,
1012                _ => 1,
1013            }
1014        }
1015        const CONTROL_CHARS: &str = "‹«👉👈»›";
1016        line.chars()
1017            .filter(|c| !CONTROL_CHARS.contains(*c))
1018            .map(width)
1019            .sum::<usize>()
1020    }
1021
1022    struct CheckHyperlinkMatch<'a> {
1023        term: &'a Term<VoidListener>,
1024        expected_hyperlink: &'a ExpectedHyperlink,
1025        source_location: &'a str,
1026    }
1027
1028    impl<'a> CheckHyperlinkMatch<'a> {
1029        fn new(
1030            term: &'a Term<VoidListener>,
1031            expected_hyperlink: &'a ExpectedHyperlink,
1032            source_location: &'a str,
1033        ) -> Self {
1034            Self {
1035                term,
1036                expected_hyperlink,
1037                source_location,
1038            }
1039        }
1040
1041        fn check_path_with_position_and_match(
1042            &self,
1043            path_with_position: PathWithPosition,
1044            hyperlink_match: &Match,
1045        ) {
1046            let format_path_with_position_and_match =
1047                |path_with_position: &PathWithPosition, hyperlink_match: &Match| {
1048                    let mut result =
1049                        format!("Path = «{}»", &path_with_position.path.to_string_lossy());
1050                    if let Some(row) = path_with_position.row {
1051                        result += &format!(", line = {row}");
1052                        if let Some(column) = path_with_position.column {
1053                            result += &format!(", column = {column}");
1054                        }
1055                    }
1056
1057                    result += &format!(
1058                        ", at grid cells {}",
1059                        Self::format_hyperlink_match(hyperlink_match)
1060                    );
1061                    result
1062                };
1063
1064            assert_ne!(
1065                self.expected_hyperlink.hyperlink_kind,
1066                HyperlinkKind::Iri,
1067                "\n    at {}\nExpected a path, but was a iri:\n{}",
1068                self.source_location,
1069                self.format_renderable_content()
1070            );
1071
1072            assert_eq!(
1073                format_path_with_position_and_match(
1074                    &PathWithPosition {
1075                        path: PathBuf::from(self.expected_hyperlink.iri_or_path.clone()),
1076                        row: self.expected_hyperlink.row,
1077                        column: self.expected_hyperlink.column
1078                    },
1079                    &self.expected_hyperlink.hyperlink_match
1080                ),
1081                format_path_with_position_and_match(&path_with_position, hyperlink_match),
1082                "\n    at {}:\n{}",
1083                self.source_location,
1084                self.format_renderable_content()
1085            );
1086        }
1087
1088        fn check_iri_and_match(&self, iri: String, hyperlink_match: &Match) {
1089            let format_iri_and_match = |iri: &String, hyperlink_match: &Match| {
1090                format!(
1091                    "Url = «{iri}», at grid cells {}",
1092                    Self::format_hyperlink_match(hyperlink_match)
1093                )
1094            };
1095
1096            assert_eq!(
1097                self.expected_hyperlink.hyperlink_kind,
1098                HyperlinkKind::Iri,
1099                "\n    at {}\nExpected a iri, but was a path:\n{}",
1100                self.source_location,
1101                self.format_renderable_content()
1102            );
1103
1104            assert_eq!(
1105                format_iri_and_match(
1106                    &self.expected_hyperlink.iri_or_path,
1107                    &self.expected_hyperlink.hyperlink_match
1108                ),
1109                format_iri_and_match(&iri, hyperlink_match),
1110                "\n    at {}:\n{}",
1111                self.source_location,
1112                self.format_renderable_content()
1113            );
1114        }
1115
1116        fn format_hyperlink_match(hyperlink_match: &Match) -> String {
1117            format!(
1118                "({}, {})..=({}, {})",
1119                hyperlink_match.start().line.0,
1120                hyperlink_match.start().column.0,
1121                hyperlink_match.end().line.0,
1122                hyperlink_match.end().column.0
1123            )
1124        }
1125
1126        fn format_renderable_content(&self) -> String {
1127            let mut result = format!("\nHovered on '{}'\n", self.expected_hyperlink.hovered_char);
1128
1129            let mut first_header_row = String::new();
1130            let mut second_header_row = String::new();
1131            let mut marker_header_row = String::new();
1132            for index in 0..self.term.columns() {
1133                let remainder = index % 10;
1134                first_header_row.push_str(
1135                    &(index > 0 && remainder == 0)
1136                        .then_some((index / 10).to_string())
1137                        .unwrap_or(" ".into()),
1138                );
1139                second_header_row += &remainder.to_string();
1140                if index == self.expected_hyperlink.hovered_grid_point.column.0 {
1141                    marker_header_row.push('↓');
1142                } else {
1143                    marker_header_row.push(' ');
1144                }
1145            }
1146
1147            result += &format!("\n      [{}]\n", first_header_row);
1148            result += &format!("      [{}]\n", second_header_row);
1149            result += &format!("       {}", marker_header_row);
1150
1151            let spacers: Flags = Flags::LEADING_WIDE_CHAR_SPACER | Flags::WIDE_CHAR_SPACER;
1152            for cell in self
1153                .term
1154                .renderable_content()
1155                .display_iter
1156                .filter(|cell| !cell.flags.intersects(spacers))
1157            {
1158                if cell.point.column.0 == 0 {
1159                    let prefix =
1160                        if cell.point.line == self.expected_hyperlink.hovered_grid_point.line {
1161                            '→'
1162                        } else {
1163                            ' '
1164                        };
1165                    result += &format!("\n{prefix}[{:>3}] ", cell.point.line.to_string());
1166                }
1167
1168                result.push(cell.c);
1169            }
1170
1171            result
1172        }
1173    }
1174
1175    fn test_hyperlink<'a>(
1176        columns: usize,
1177        total_cells: usize,
1178        test_lines: impl Iterator<Item = &'a str>,
1179        hyperlink_kind: HyperlinkKind,
1180        source_location: &str,
1181    ) {
1182        thread_local! {
1183            static TEST_REGEX_SEARCHES: RefCell<RegexSearches> = RefCell::new(RegexSearches::new());
1184        }
1185
1186        let term_size = TermSize::new(columns, total_cells / columns + 2);
1187        let (term, expected_hyperlink) =
1188            build_term_from_test_lines(hyperlink_kind, term_size, test_lines);
1189        let hyperlink_found = TEST_REGEX_SEARCHES.with(|regex_searches| {
1190            find_from_grid_point(
1191                &term,
1192                expected_hyperlink.hovered_grid_point,
1193                &mut regex_searches.borrow_mut(),
1194            )
1195        });
1196        let check_hyperlink_match =
1197            CheckHyperlinkMatch::new(&term, &expected_hyperlink, source_location);
1198        match hyperlink_found {
1199            Some((hyperlink_word, false, hyperlink_match)) => {
1200                check_hyperlink_match.check_path_with_position_and_match(
1201                    PathWithPosition::parse_str(&hyperlink_word),
1202                    &hyperlink_match,
1203                );
1204            }
1205            Some((hyperlink_word, true, hyperlink_match)) => {
1206                check_hyperlink_match.check_iri_and_match(hyperlink_word, &hyperlink_match);
1207            }
1208            _ => {
1209                assert!(
1210                    false,
1211                    "No hyperlink found\n     at {source_location}:\n{}",
1212                    check_hyperlink_match.format_renderable_content()
1213                )
1214            }
1215        }
1216    }
1217}