Skip to content

Commit

Permalink
feat: add stop_at_first_char option
Browse files Browse the repository at this point in the history
make move/select/delete to beginning of line actions consistent with other beginning of line actions by adding a `stop_at_first_char` option

closes zed-industries#7863
  • Loading branch information
felixpackard committed Feb 21, 2025
1 parent 21bb724 commit 40577a8
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 58 deletions.
6 changes: 3 additions & 3 deletions assets/keymaps/default-linux.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@
"ctrl-a": "editor::SelectAll",
"ctrl-l": "editor::SelectLine",
"ctrl-shift-i": "editor::Format",
// "cmd-shift-left": ["editor::SelectToBeginningOfLine", {"stop_at_soft_wraps": true }],
// "ctrl-shift-a": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": true }],
"shift-home": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": true }],
// "cmd-shift-left": ["editor::SelectToBeginningOfLine", {"stop_at_soft_wraps": true, "stop_at_first_char": false }],
// "ctrl-shift-a": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": true, "stop_at_first_char": false }],
"shift-home": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": true, "stop_at_first_char": false }],
// "cmd-shift-right": ["editor::SelectToEndOfLine", { "stop_at_soft_wraps": true }],
// "ctrl-shift-e": ["editor::SelectToEndOfLine", { "stop_at_soft_wraps": true }],
"shift-end": ["editor::SelectToEndOfLine", { "stop_at_soft_wraps": true }],
Expand Down
6 changes: 3 additions & 3 deletions assets/keymaps/default-macos.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@
"cmd-a": "editor::SelectAll",
"cmd-l": "editor::SelectLine",
"cmd-shift-i": "editor::Format",
"cmd-shift-left": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": true }],
"shift-home": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": true }],
"ctrl-shift-a": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": true }],
"cmd-shift-left": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": true, "stop_at_first_char": false }],
"shift-home": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": true, "stop_at_first_char": false }],
"ctrl-shift-a": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": true, "stop_at_first_char": false }],
"cmd-shift-right": ["editor::SelectToEndOfLine", { "stop_at_soft_wraps": true }],
"shift-end": ["editor::SelectToEndOfLine", { "stop_at_soft_wraps": true }],
"ctrl-shift-e": ["editor::SelectToEndOfLine", { "stop_at_soft_wraps": true }],
Expand Down
10 changes: 5 additions & 5 deletions assets/keymaps/linux/emacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
"ctrl-b": "editor::MoveLeft", // backward-char
"ctrl-n": "editor::MoveDown", // next-line
"ctrl-p": "editor::MoveUp", // previous-line
"home": ["editor::MoveToBeginningOfLine", { "stop_at_soft_wraps": false }], // move-beginning-of-line
"home": ["editor::MoveToBeginningOfLine", { "stop_at_soft_wraps": false, "stop_at_first_char": false }], // move-beginning-of-line
"end": ["editor::MoveToEndOfLine", { "stop_at_soft_wraps": false }], // move-end-of-line
"ctrl-a": ["editor::MoveToBeginningOfLine", { "stop_at_soft_wraps": false }], // move-beginning-of-line
"ctrl-a": ["editor::MoveToBeginningOfLine", { "stop_at_soft_wraps": false, "stop_at_first_char": false }], // move-beginning-of-line
"ctrl-e": ["editor::MoveToEndOfLine", { "stop_at_soft_wraps": false }], // move-end-of-line
"shift-home": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": false }], // move-beginning-of-line
"shift-home": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": false, "stop_at_first_char": false }], // move-beginning-of-line
"shift-end": ["editor::SelectToEndOfLine", { "stop_at_soft_wraps": false }], // move-end-of-line
"alt-f": "editor::MoveToNextSubwordEnd", // forward-word
"alt-b": "editor::MoveToPreviousSubwordStart", // backward-word
Expand Down Expand Up @@ -74,9 +74,9 @@
"ctrl-b": "editor::SelectLeft",
"ctrl-n": "editor::SelectDown",
"ctrl-p": "editor::SelectUp",
"home": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": false }],
"home": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": false, "stop_at_first_char": false }],
"end": ["editor::SelectToEndOfLine", { "stop_at_soft_wraps": false }],
"ctrl-a": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": false }],
"ctrl-a": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": false, "stop_at_first_char": false }],
"ctrl-e": ["editor::SelectToEndOfLine", { "stop_at_soft_wraps": false }],
"alt-f": "editor::SelectToNextWordEnd",
"alt-b": "editor::SelectToPreviousSubwordStart",
Expand Down
10 changes: 5 additions & 5 deletions assets/keymaps/macos/emacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
"ctrl-b": "editor::MoveLeft", // backward-char
"ctrl-n": "editor::MoveDown", // next-line
"ctrl-p": "editor::MoveUp", // previous-line
"home": ["editor::MoveToBeginningOfLine", { "stop_at_soft_wraps": false }], // move-beginning-of-line
"home": ["editor::MoveToBeginningOfLine", { "stop_at_soft_wraps": false, "stop_at_first_char": false }], // move-beginning-of-line
"end": ["editor::MoveToEndOfLine", { "stop_at_soft_wraps": false }], // move-end-of-line
"ctrl-a": ["editor::MoveToBeginningOfLine", { "stop_at_soft_wraps": false }], // move-beginning-of-line
"ctrl-a": ["editor::MoveToBeginningOfLine", { "stop_at_soft_wraps": false, "stop_at_first_char": false }], // move-beginning-of-line
"ctrl-e": ["editor::MoveToEndOfLine", { "stop_at_soft_wraps": false }], // move-end-of-line
"shift-home": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": false }], // move-beginning-of-line
"shift-home": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": false, "stop_at_first_char": false }], // move-beginning-of-line
"shift-end": ["editor::SelectToEndOfLine", { "stop_at_soft_wraps": false }], // move-end-of-line
"alt-f": "editor::MoveToNextSubwordEnd", // forward-word
"alt-b": "editor::MoveToPreviousSubwordStart", // backward-word
Expand Down Expand Up @@ -74,9 +74,9 @@
"ctrl-b": "editor::SelectLeft",
"ctrl-n": "editor::SelectDown",
"ctrl-p": "editor::SelectUp",
"home": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": false }],
"home": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": false, "stop_at_first_char": false }],
"end": ["editor::SelectToEndOfLine", { "stop_at_soft_wraps": false }],
"ctrl-a": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": false }],
"ctrl-a": ["editor::SelectToBeginningOfLine", { "stop_at_soft_wraps": false, "stop_at_first_char": false }],
"ctrl-e": ["editor::SelectToEndOfLine", { "stop_at_soft_wraps": false }],
"alt-f": "editor::SelectToNextWordEnd",
"alt-b": "editor::SelectToPreviousSubwordStart",
Expand Down
13 changes: 12 additions & 1 deletion crates/editor/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,24 @@ pub struct SelectPrevious {
pub struct MoveToBeginningOfLine {
#[serde(default = "default_true")]
pub stop_at_soft_wraps: bool,
#[serde(default)]
pub stop_at_first_char: bool,
}

#[derive(PartialEq, Clone, Deserialize, Default, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct SelectToBeginningOfLine {
#[serde(default)]
pub(super) stop_at_soft_wraps: bool,
#[serde(default)]
pub(super) stop_at_first_char: bool,
}

#[derive(PartialEq, Clone, Deserialize, Default, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct DeleteToBeginningOfLine {
#[serde(default)]
pub(super) stop_at_first_char: bool,
}

#[derive(PartialEq, Clone, Deserialize, Default, JsonSchema)]
Expand Down Expand Up @@ -210,6 +221,7 @@ impl_actions!(
ConfirmCompletion,
DeleteToNextWordEnd,
DeleteToPreviousWordStart,
DeleteToBeginningOfLine,
ExpandExcerpts,
ExpandExcerptsDown,
ExpandExcerptsUp,
Expand Down Expand Up @@ -272,7 +284,6 @@ gpui::actions!(
CutToEndOfLine,
Delete,
DeleteLine,
DeleteToBeginningOfLine,
DeleteToEndOfLine,
DeleteToNextSubwordEnd,
DeleteToPreviousSubwordStart,
Expand Down
25 changes: 16 additions & 9 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8803,10 +8803,13 @@ impl Editor {
) {
self.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
s.move_cursors_with(|map, head, _| {
(
movement::indented_line_beginning(map, head, action.stop_at_soft_wraps),
SelectionGoal::None,
)
let point = if action.stop_at_first_char {
movement::indented_line_beginning(map, head, true)
} else {
movement::line_beginning(map, head, action.stop_at_soft_wraps)
};

(point, SelectionGoal::None)
});
})
}
Expand All @@ -8819,17 +8822,20 @@ impl Editor {
) {
self.change_selections(Some(Autoscroll::fit()), window, cx, |s| {
s.move_heads_with(|map, head, _| {
(
movement::indented_line_beginning(map, head, action.stop_at_soft_wraps),
SelectionGoal::None,
)
let point = if action.stop_at_first_char {
movement::indented_line_beginning(map, head, true)
} else {
movement::line_beginning(map, head, action.stop_at_soft_wraps)
};

(point, SelectionGoal::None)
});
});
}

pub fn delete_to_beginning_of_line(
&mut self,
_: &DeleteToBeginningOfLine,
action: &DeleteToBeginningOfLine,
window: &mut Window,
cx: &mut Context<Self>,
) {
Expand All @@ -8843,6 +8849,7 @@ impl Editor {
this.select_to_beginning_of_line(
&SelectToBeginningOfLine {
stop_at_soft_wraps: false,
stop_at_first_char: action.stop_at_first_char,
},
window,
cx,
Expand Down
Loading

0 comments on commit 40577a8

Please sign in to comment.