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

Fix Cursor::previous_logical_word #215

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 8 additions & 6 deletions parley/src/layout/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,15 @@ impl Cursor {
/// in logical order.
#[must_use]
pub fn next_logical_word<B: Brush>(&self, layout: &Layout<B>) -> Self {
let [left, right] = self.logical_clusters(layout);
if let Some(cluster) = right.or(left) {
let [upstream, downstream] = self.logical_clusters(layout);
if let Some(cluster) = downstream.or(upstream) {
let start = cluster.clone();
let cluster = cluster.next_logical_word().unwrap_or(cluster);
if cluster.path == start.path {
return Self::from_byte_index(layout, usize::MAX, Affinity::Downstream);
}
return Self::from_cluster(layout, cluster, true);
let moving_right = !cluster.is_rtl();
return Self::from_cluster(layout, cluster, moving_right);
}
*self
}
Expand All @@ -259,10 +260,11 @@ impl Cursor {
/// in logical order.
#[must_use]
pub fn previous_logical_word<B: Brush>(&self, layout: &Layout<B>) -> Self {
let [left, right] = self.logical_clusters(layout);
if let Some(cluster) = left.or(right) {
let [upstream, downstream] = self.logical_clusters(layout);
if let Some(cluster) = downstream.or(upstream) {
Comment on lines -262 to +264
Copy link
Member Author

@tomcur tomcur Dec 9, 2024

Choose a reason for hiding this comment

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

This is interesting as we're checking the same clusters (in logical order) as for the method in the other direction. I believe the asymmetry comes from the word boundary clusters being the logically first cluster of a word and (e.g.) the whitespace logically following the word, e.g., in "foo bar" the boundary clusters of the first word are "f" and " ".

let cluster = cluster.previous_logical_word().unwrap_or(cluster);
return Self::from_cluster(layout, cluster, true);
let moving_right = cluster.is_rtl();
return Self::from_cluster(layout, cluster, moving_right);
}
*self
}
Expand Down
Loading