Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display selected newlines as whitespace in the selection highlight #296

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions parley/src/layout/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,10 +799,7 @@ impl Selection {
/// This avoids allocation if the intent is to render the rectangles
/// immediately.
pub fn geometry_with<B: Brush>(&self, layout: &Layout<B>, mut f: impl FnMut(Rect)) {
// Ensure we add some visual indicator for selected empty
// lines.
// Make this configurable?
const MIN_RECT_WIDTH: f64 = 8.0;
const NEWLINE_WHITESPACE_WIDTH_RATIO: f64 = 0.25;
if self.is_collapsed() {
return;
}
Expand All @@ -824,6 +821,17 @@ impl Selection {
let metrics = line.metrics();
let line_min = metrics.min_coord as f64;
let line_max = metrics.max_coord as f64;
// Trailing whitespace to indicate that the newline character at the
// end of this line is selected. It's based on the ascent and
// descent so it doesn't change with the line height.
//
// TODO: the width of this whitespace should be the width of a space
// (U+0020) character.
let newline_whitespace = if line.break_reason() == BreakReason::Explicit {
(metrics.ascent as f64 + metrics.descent as f64) * NEWLINE_WHITESPACE_WIDTH_RATIO
} else {
0.0
};
if line_ix == line_start_ix || line_ix == line_end_ix {
// We only need to run the expensive logic on the first and
// last lines
Expand All @@ -838,22 +846,29 @@ impl Selection {
cur_x += advance;
} else {
if cur_x != start_x {
let width = (cur_x - start_x).max(MIN_RECT_WIDTH);
f(Rect::new(start_x, line_min, start_x + width, line_max));
f(Rect::new(start_x, line_min, cur_x, line_max));
}
cur_x += advance;
start_x = cur_x;
}
}
}
if cur_x != start_x || (cluster_count != 0 && line.metrics().advance == 0.0) {
let width = (cur_x - start_x).max(MIN_RECT_WIDTH);
f(Rect::new(start_x, line_min, start_x + width, line_max));
let mut end_x = cur_x;
if line_ix != line_end_ix || (cluster_count != 0 && metrics.advance == 0.0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, we would always draw if there were clusters but no advance (the second condition). However, now we only do so if there was an explicit break.
I can see two scenarios which we are in:

  • That second condition is to catch this case, in which case it can be removed entirely.
  • That second condition is for unrelated reasons, in which case it should instead be an or for the 860 line.

I think the former is more likely, but I haven't looked into the history.

Copy link
Contributor Author

@valadaptive valadaptive Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked over #170 but couldn't find where cluster_count was added. I suspect the only time a cluster will have zero advance is an explicit newline. (Maybe there are edge cases around zero-width spaces or something? I think a newline is the only time we want to display a selection rectangle.)

I'll take another look later but I think you're right and the cluster-counting stuff can be removed.

end_x += newline_whitespace;
}
if end_x != start_x {
f(Rect::new(start_x, line_min, end_x, line_max));
}
} else {
let x = metrics.offset as f64;
let width = (metrics.advance as f64).max(MIN_RECT_WIDTH);
f(Rect::new(x, line_min, x + width, line_max));
let width = metrics.advance as f64;
f(Rect::new(
x,
line_min,
x + width + newline_whitespace,
line_max,
));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions parley/tests/snapshots/editor_select_all-0.png
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any tests where there is a line with just a newline (i.e. a \n\n sequence)?

It's probably worth adding one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look at adding more selection-drawing tests.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.