editor: Fix DeleteToPreviousWordStart and DeleteToNextWordEnd interaction with newlines (#16848)

Closes #5285, #14389

Changes:
- `DeleteToPreviousWordStart` now deletes '\n' separately from preceding
words and whitespace.
- `DeleteToNextWordEnd` now deletes '\n' and any following whitespace
separately from subsequent words.
- Added an `ignore_newlines` flag to both actions to optionally retain
the old behavior.

These modifications align the behavior more closely with other popular
editors like VSCode and Sublime:
- `DeleteToPreviousWordStart` now matches the default <Ctrl+Backspace>
action in those editors.
- `DeleteToNextWordEnd` becomes more intuitive and closely resembles the
default <Ctrl+Delete> behavior in those editors.

Release Notes:

- Improved `DeleteToPreviousWordStart` and `DeleteToNextWordEnd`
interactions around newlines. You can opt-in into the previous behavior
by adding {"ignore_newlines": true} to either action's binds in your
keymap. ([#5285](https://github.com/zed-industries/zed/issues/5285),
[#14389](https://github.com/zed-industries/zed/issues/14389))
This commit is contained in:
Kajus
2024-09-05 13:43:07 -04:00
committed by GitHub
parent a3d8dcda36
commit 96b592f00d
4 changed files with 151 additions and 8 deletions
+16 -4
View File
@@ -7327,7 +7327,7 @@ impl Editor {
pub fn delete_to_previous_word_start(
&mut self,
_: &DeleteToPreviousWordStart,
action: &DeleteToPreviousWordStart,
cx: &mut ViewContext<Self>,
) {
self.transact(cx, |this, cx| {
@@ -7336,7 +7336,11 @@ impl Editor {
let line_mode = s.line_mode;
s.move_with(|map, selection| {
if selection.is_empty() && !line_mode {
let cursor = movement::previous_word_start(map, selection.head());
let cursor = if action.ignore_newlines {
movement::previous_word_start(map, selection.head())
} else {
movement::previous_word_start_or_newline(map, selection.head())
};
selection.set_head(cursor, SelectionGoal::None);
}
});
@@ -7405,13 +7409,21 @@ impl Editor {
})
}
pub fn delete_to_next_word_end(&mut self, _: &DeleteToNextWordEnd, cx: &mut ViewContext<Self>) {
pub fn delete_to_next_word_end(
&mut self,
action: &DeleteToNextWordEnd,
cx: &mut ViewContext<Self>,
) {
self.transact(cx, |this, cx| {
this.change_selections(Some(Autoscroll::fit()), cx, |s| {
let line_mode = s.line_mode;
s.move_with(|map, selection| {
if selection.is_empty() && !line_mode {
let cursor = movement::next_word_end(map, selection.head());
let cursor = if action.ignore_newlines {
movement::next_word_end(map, selection.head())
} else {
movement::next_word_end_or_newline(map, selection.head())
};
selection.set_head(cursor, SelectionGoal::None);
}
});