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

Conversation

tomcur
Copy link
Member

@tomcur tomcur commented Dec 9, 2024

Currently, in foo b|ar where | indicates the cursor position, Cursor::previous_logical_word will return foo| bar. This doesn't match the behavior of Cursor::{next_logical_word, previous_visual_word} which place the cursor at the boundary of the current word. This happens because Cluster::previous_logical_word is called from the upstream cluster (the "b") and the cursor then lands between the "o" and the space.

This also renames "left", "right" -> "upstream", "downstream" (naming copied from Cursor::logical_clusters) to make it clear we're operating in logical order and not visual, and sets moving_right based on the text direction we're jumping to, to set the affinity of the cursor towards the word whose boundary it lands on.

(Note the behavior between {previous, next}_logical_word and {previous, next}_visual_word aren't quite the same yet: the visual methods jump over whitespace, the logical ones don't.)

Currently, in `foo b|ar` where `|` indicates the cursor position,
`Cursor::previous_logical_word` will return `foo| bar`. This doesn't
match the behavior of `Cursor::{next_logical_word,
previous_visual_word}` which place the cursor at the boundary of the
current word. This happens because `Cluster::previous_logical_word` is
called from the upstream cluster (the "b") and the cursor then lands
between the "o" and the space.

This also renames "left", "right" -> "upstream", "downstream" (naming
copied from `Cursor::logical_clusters`) to make it clear we're operating
in logical order and not visual, and sets `moving_right` based on the
text direction we're jumping to, to affine the cursor towards the word
whose boundary it lands on.

(Note the behavior between `{previous, next}_logical_word` and
{previous, next}_visual_word` aren't quite the same yet: the `visual`
methods jump over whitespace, the `logical` ones don't.)
Comment on lines -262 to +264
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) {
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 " ".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant