Skip to content

Commit

Permalink
Fix hint Select action for hyperlink escape
Browse files Browse the repository at this point in the history
This fixes an issue where the `Select` action for hyperlink escape text
would select the entire line, instead of selecting only the hyperlink
itself.

It also changes the way hyperlinks with the same ID are highlighted,
removing the restriction of being on consecutive lines and instead
highlighting all visible cells that correspond to the matching
hyperlink.

Closes alacritty#7766.
  • Loading branch information
chrisduerr committed Mar 9, 2024
1 parent f0ebb7f commit 4954563
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 37 deletions.
48 changes: 15 additions & 33 deletions alacritty/src/display/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ pub struct HintMatch {
impl HintMatch {
#[inline]
pub fn should_highlight(&self, point: Point, pointed_hyperlink: Option<&Hyperlink>) -> bool {
self.bounds.contains(&point) && self.hyperlink.as_ref() == pointed_hyperlink
self.hyperlink.as_ref() == pointed_hyperlink
&& (self.bounds.contains(&point) || self.hyperlink.is_some())
}

#[inline]
Expand Down Expand Up @@ -400,49 +401,30 @@ pub fn highlighted_at<T>(
}

/// Retrieve the hyperlink with its range, if there is one at the specified point.
///
/// This will only return contiguous cells, even if another hyperlink with the same ID exists.
fn hyperlink_at<T>(term: &Term<T>, point: Point) -> Option<(Hyperlink, Match)> {
let hyperlink = term.grid()[point].hyperlink()?;

let viewport_start = Line(-(term.grid().display_offset() as i32));
let viewport_end = viewport_start + term.bottommost_line();

let mut match_start = Point::new(point.line, Column(0));
let mut match_end = Point::new(point.line, Column(term.columns() - 1));
let grid = term.grid();

// Find adjacent lines that have the same `hyperlink`. The end purpose to highlight hyperlinks
// that span across multiple lines or not directly attached to each other.

// Find the closest to the viewport start adjacent line.
while match_start.line > viewport_start {
let next_line = match_start.line - 1i32;
// Iterate over all the cells in the grid's line and check if any of those cells contains
// the hyperlink we've found at original `point`.
let line_contains_hyperlink = grid[next_line]
.into_iter()
.any(|cell| cell.hyperlink().map_or(false, |h| h == hyperlink));

// There's no hyperlink on the next line, break.
if !line_contains_hyperlink {
let mut match_end = point;
for cell in grid.iter_from(point) {
if cell.hyperlink().map_or(false, |link| link == hyperlink) {
match_end = cell.point;
} else {
break;
}

match_start.line = next_line;
}

// Ditto for the end.
while match_end.line < viewport_end {
let next_line = match_end.line + 1i32;

let line_contains_hyperlink = grid[next_line]
.into_iter()
.any(|cell| cell.hyperlink().map_or(false, |h| h == hyperlink));

if !line_contains_hyperlink {
let mut match_start = point;
let mut iter = grid.iter_from(point);
while let Some(cell) = iter.prev() {
if cell.hyperlink().map_or(false, |link| link == hyperlink) {
match_start = cell.point;
} else {
break;
}

match_end.line = next_line;
}

Some((hyperlink, match_start..=match_end))
Expand Down
5 changes: 1 addition & 4 deletions alacritty/src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,12 +1050,9 @@ impl<T: EventListener, A: ActionContext<T>> Processor<T, A> {
let point = self.ctx.mouse().point(&self.ctx.size_info(), display_offset);
let hyperlink = self.ctx.terminal().grid()[point].hyperlink();

// Function to check if mouse is on top of a hint.
let hint_highlighted = |hint: &HintMatch| hint.should_highlight(point, hyperlink.as_ref());

if let Some(mouse_state) = self.message_bar_cursor_state() {
mouse_state
} else if self.ctx.display().highlighted_hint.as_ref().map_or(false, hint_highlighted) {
} else if hyperlink.is_some() {
CursorIcon::Pointer
} else if !self.ctx.modifiers().state().shift_key() && self.ctx.mouse_mode() {
CursorIcon::Default
Expand Down

0 comments on commit 4954563

Please sign in to comment.