Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

editor: Treat single punctuation as a word boundary (#58882)

Maja Backman482963d5e966
diff --git a/crates/editor/src/movement.rs b/crates/editor/src/movement.rs
index 92b1a30cf22..de46fdcc870 100644
--- a/crates/editor/src/movement.rs
+++ b/crates/editor/src/movement.rs
@@ -265,24 +265,69 @@ pub fn line_end(
/// uppercase letter, lowercase letter, '_' character or language-specific word character (like '-' in CSS).
pub fn previous_word_start(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
let raw_point = point.to_point(map);
- let classifier = map.buffer_snapshot().char_classifier_at(raw_point);
+ let buffer_snapshot = map.buffer_snapshot();
+ let classifier = buffer_snapshot.char_classifier_at(raw_point);
+ let cursor_offset = raw_point.to_offset(buffer_snapshot);
+ let cursor_char = buffer_snapshot.chars_at(cursor_offset).next();
let mut is_first_iteration = true;
- find_preceding_boundary_display_point(map, point, FindRange::MultiLine, &mut |left, right| {
- // Make alt-left skip punctuation to respect VSCode behaviour. For example: hello.| goes to |hello.
- if is_first_iteration
- && classifier.is_punctuation(right)
- && !classifier.is_punctuation(left)
- && left != '\n'
- {
+ let word_start = find_preceding_boundary_display_point(
+ map,
+ point,
+ FindRange::MultiLine,
+ &mut |left, right| {
+ // Make alt-left skip punctuation to respect VSCode behaviour. For example: hello.| goes to |hello.
+ if is_first_iteration
+ && classifier.is_punctuation(right)
+ && !classifier.is_punctuation(left)
+ && left != '\n'
+ && (!classifier.is_whitespace(left)
+ || cursor_char.is_some_and(|character| {
+ !classifier.is_whitespace(character) && character != '\n'
+ }))
+ {
+ is_first_iteration = false;
+ return false;
+ }
is_first_iteration = false;
- return false;
- }
- is_first_iteration = false;
- (classifier.kind(left) != classifier.kind(right) && !classifier.is_whitespace(right))
- || left == '\n'
- })
+ (classifier.kind(left) != classifier.kind(right) && !classifier.is_whitespace(right))
+ || left == '\n'
+ },
+ );
+
+ let word_start_point = word_start.to_point(map);
+ let word_start_offset = word_start_point.to_offset(buffer_snapshot);
+ let mut chars_before_word = buffer_snapshot.reversed_chars_at(word_start_offset);
+ let Some(punctuation) = chars_before_word
+ .next()
+ .filter(|character| classifier.is_punctuation(*character))
+ else {
+ return word_start;
+ };
+
+ let punctuation_is_single = chars_before_word
+ .next()
+ .is_none_or(|character| !classifier.is_punctuation(character));
+ let punctuation_prefixes_word = buffer_snapshot
+ .chars_at(word_start_offset)
+ .next()
+ .is_some_and(|character| classifier.is_word(character));
+ let cursor_is_at_word_end = buffer_snapshot
+ .reversed_chars_at(cursor_offset)
+ .next()
+ .is_some_and(|character| classifier.is_word(character))
+ && cursor_char.is_none_or(|character| !classifier.is_word(character));
+
+ // Forward movement treats a single punctuation prefix as part of the following word.
+ // Include it when moving back from the word's end so `|@word` and `@word|` are symmetric.
+ if cursor_is_at_word_end && punctuation_is_single && punctuation_prefixes_word {
+ let mut punctuation_offset = word_start_offset;
+ punctuation_offset -= punctuation.len_utf8();
+ punctuation_offset.to_display_point(map)
+ } else {
+ word_start
+ }
}
/// Returns a position of the previous word boundary, where a word character is defined as either
@@ -446,7 +491,7 @@ pub fn next_word_end(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint
// Make alt-right skip punctuation to respect VSCode behaviour. For example: |.hello goes to .hello|
if is_first_iteration
&& classifier.is_punctuation(left)
- && !classifier.is_punctuation(right)
+ && classifier.is_word(right)
&& right != '\n'
{
is_first_iteration = false;
@@ -1073,6 +1118,13 @@ mod tests {
assert("helloˇ.---..ˇtest", cx);
assert("test ˇ.--ˇtest", cx);
assert("oneˇ,;:!?ˇtwo", cx);
+ assert("foo ˇ.ˇ bar", cx);
+ assert("ˇfoo @ˇbar", cx);
+ assert("foo ˇ@barˇ baz", cx);
+ assert("foo @ˇbˇar", cx);
+ assert("foo ..ˇbarˇ baz", cx);
+ assert("ˇ.helloˇ", cx);
+ assert("[2001:4860:4860::8888ˇ] ˇ", cx);
}
#[gpui::test]
@@ -1256,6 +1308,10 @@ mod tests {
assert("helloˇ.---..ˇtest", cx);
assert("testˇ.--ˇ test", cx);
assert("oneˇ,;:!?ˇtwo", cx);
+ assert("foo ˇ.ˇ bar", cx);
+ assert("fooˇ.ˇ bar", cx);
+ assert("foo ˇ@barˇ baz", cx);
+ assert("[2001:4860:4860::8888ˇ]ˇ ", cx);
}
#[gpui::test]