Skip to content

Commit 034797f

Browse files
dinocostasmitbarmase
authored andcommitted
editor: Ignore soft wrapped lines when adding selection above or below (zed-industries#40190)
- Add `skip_soft_wrap` field to both `AddSelectionAbove` and `AddSelectionBelow` actions. When set to `true`, which is now the default this will skip soft wrapped lines when extending the selections. - Move the `start_of_relative_buffer_row` function from the `vim::motion` module to the `editor::display_map::DisplaySnapshot` implementation as a method. - Update the default behavior for both `editor: add selection above` and `editor: add selection below` commands in order to skip over soft wrapped lines by default, mirroring VS Code's default behavior. - Update existing keymaps to specify this `skip_soft_wrap` value for both `AddSelectionAbove` and `AddSelectionBelow` actions. Closes zed-industries#16979 Release Notes: - Updated both the `editor: add selection above` and `editor: add selection below` commands to ignore soft wrapped lines. If you wish to restore the old behavior, add the following to your keymap file: ``` { "context": "Editor", "bindings": { "cmd-alt-up": ["editor::AddSelectionAbove", { "skip_soft_wrap": false }], "cmd-alt-down": ["editor::AddSelectionBelow", { "skip_soft_wrap": false }] } } ``` --------- Co-authored-by: Smit Barmase <[email protected]>
1 parent 5b2fc64 commit 034797f

File tree

18 files changed

+191
-66
lines changed

18 files changed

+191
-66
lines changed

assets/keymaps/default-linux.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,8 @@
491491
"bindings": {
492492
"ctrl-[": "editor::Outdent",
493493
"ctrl-]": "editor::Indent",
494-
"shift-alt-up": "editor::AddSelectionAbove", // Insert Cursor Above
495-
"shift-alt-down": "editor::AddSelectionBelow", // Insert Cursor Below
494+
"shift-alt-up": ["editor::AddSelectionAbove", { "skip_soft_wrap": true }], // Insert Cursor Above
495+
"shift-alt-down": ["editor::AddSelectionBelow", { "skip_soft_wrap": true }], // Insert Cursor Below
496496
"ctrl-shift-k": "editor::DeleteLine",
497497
"alt-up": "editor::MoveLineUp",
498498
"alt-down": "editor::MoveLineDown",

assets/keymaps/default-macos.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,10 @@
539539
"bindings": {
540540
"cmd-[": "editor::Outdent",
541541
"cmd-]": "editor::Indent",
542-
"cmd-ctrl-p": "editor::AddSelectionAbove", // Insert cursor above
543-
"cmd-alt-up": "editor::AddSelectionAbove",
544-
"cmd-ctrl-n": "editor::AddSelectionBelow", // Insert cursor below
545-
"cmd-alt-down": "editor::AddSelectionBelow",
542+
"cmd-ctrl-p": ["editor::AddSelectionAbove", { "skip_soft_wrap": false }], // Insert cursor above
543+
"cmd-alt-up": ["editor::AddSelectionAbove", { "skip_soft_wrap": true }],
544+
"cmd-ctrl-n": ["editor::AddSelectionBelow", { "skip_soft_wrap": false }], // Insert cursor below
545+
"cmd-alt-down": ["editor::AddSelectionBelow", { "skip_soft_wrap": true }],
546546
"cmd-shift-k": "editor::DeleteLine",
547547
"alt-up": "editor::MoveLineUp",
548548
"alt-down": "editor::MoveLineDown",

assets/keymaps/default-windows.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,8 @@
500500
"bindings": {
501501
"ctrl-[": "editor::Outdent",
502502
"ctrl-]": "editor::Indent",
503-
"ctrl-shift-alt-up": "editor::AddSelectionAbove", // Insert Cursor Above
504-
"ctrl-shift-alt-down": "editor::AddSelectionBelow", // Insert Cursor Below
503+
"ctrl-shift-alt-up": ["editor::AddSelectionAbove", { "skip_soft_wrap": true }], // Insert Cursor Above
504+
"ctrl-shift-alt-down": ["editor::AddSelectionBelow", { "skip_soft_wrap": true }], // Insert Cursor Below
505505
"ctrl-shift-k": "editor::DeleteLine",
506506
"alt-up": "editor::MoveLineUp",
507507
"alt-down": "editor::MoveLineDown",

assets/keymaps/linux/atom.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"ctrl-<": "editor::ScrollCursorCenter", // editor:scroll-to-cursor
2525
"f3": ["editor::SelectNext", { "replace_newest": true }], // find-and-replace:find-next
2626
"shift-f3": ["editor::SelectPrevious", { "replace_newest": true }], //find-and-replace:find-previous
27-
"alt-shift-down": "editor::AddSelectionBelow", // editor:add-selection-below
28-
"alt-shift-up": "editor::AddSelectionAbove", // editor:add-selection-above
27+
"alt-shift-down": ["editor::AddSelectionBelow", { "skip_soft_wrap": true }], // editor:add-selection-below
28+
"alt-shift-up": ["editor::AddSelectionAbove", { "skip_soft_wrap": true }], // editor:add-selection-above
2929
"ctrl-j": "editor::JoinLines", // editor:join-lines
3030
"ctrl-shift-d": "editor::DuplicateLineDown", // editor:duplicate-lines
3131
"ctrl-up": "editor::MoveLineUp", // editor:move-line-up

assets/keymaps/linux/sublime_text.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
{
2929
"context": "Editor",
3030
"bindings": {
31-
"ctrl-alt-up": "editor::AddSelectionAbove",
32-
"ctrl-alt-down": "editor::AddSelectionBelow",
31+
"ctrl-alt-up": ["editor::AddSelectionAbove", { "skip_soft_wrap": false }],
32+
"ctrl-alt-down": ["editor::AddSelectionBelow", { "skip_soft_wrap": false }],
3333
"ctrl-shift-up": "editor::MoveLineUp",
3434
"ctrl-shift-down": "editor::MoveLineDown",
3535
"ctrl-shift-m": "editor::SelectLargerSyntaxNode",

assets/keymaps/macos/atom.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
"cmd-<": "editor::ScrollCursorCenter",
2626
"cmd-g": ["editor::SelectNext", { "replace_newest": true }],
2727
"cmd-shift-g": ["editor::SelectPrevious", { "replace_newest": true }],
28-
"ctrl-shift-down": "editor::AddSelectionBelow",
29-
"ctrl-shift-up": "editor::AddSelectionAbove",
28+
"ctrl-shift-down": ["editor::AddSelectionBelow", { "skip_soft_wrap": true }],
29+
"ctrl-shift-up": ["editor::AddSelectionAbove", { "skip_soft_wrap": true }],
3030
"alt-enter": "editor::Newline",
3131
"cmd-shift-d": "editor::DuplicateLineDown",
3232
"ctrl-cmd-up": "editor::MoveLineUp",

assets/keymaps/macos/sublime_text.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
{
2929
"context": "Editor",
3030
"bindings": {
31-
"ctrl-shift-up": "editor::AddSelectionAbove",
32-
"ctrl-shift-down": "editor::AddSelectionBelow",
31+
"ctrl-shift-up": ["editor::AddSelectionAbove", { "skip_soft_wrap": false }],
32+
"ctrl-shift-down": ["editor::AddSelectionBelow", { "skip_soft_wrap": false }],
3333
"cmd-ctrl-up": "editor::MoveLineUp",
3434
"cmd-ctrl-down": "editor::MoveLineDown",
3535
"cmd-shift-space": "editor::SelectAll",

assets/keymaps/vim.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,8 @@
498498
"ctrl-c": "editor::ToggleComments",
499499
"d": "vim::HelixDelete",
500500
"c": "vim::Substitute",
501-
"shift-c": "editor::AddSelectionBelow",
502-
"alt-shift-c": "editor::AddSelectionAbove"
501+
"shift-c": ["editor::AddSelectionBelow", { "skip_soft_wrap": true }],
502+
"alt-shift-c": ["editor::AddSelectionAbove", { "skip_soft_wrap": true }]
503503
}
504504
},
505505
{

crates/editor/src/actions.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,24 @@ pub struct GoToPreviousDiagnostic {
318318
pub severity: GoToDiagnosticSeverityFilter,
319319
}
320320

321+
/// Adds a cursor above the current selection.
322+
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
323+
#[action(namespace = editor)]
324+
#[serde(deny_unknown_fields)]
325+
pub struct AddSelectionAbove {
326+
#[serde(default = "default_true")]
327+
pub skip_soft_wrap: bool,
328+
}
329+
330+
/// Adds a cursor below the current selection.
331+
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
332+
#[action(namespace = editor)]
333+
#[serde(deny_unknown_fields)]
334+
pub struct AddSelectionBelow {
335+
#[serde(default = "default_true")]
336+
pub skip_soft_wrap: bool,
337+
}
338+
321339
actions!(
322340
debugger,
323341
[
@@ -345,10 +363,6 @@ actions!(
345363
/// Accepts a partial edit prediction.
346364
#[action(deprecated_aliases = ["editor::AcceptPartialCopilotSuggestion"])]
347365
AcceptPartialEditPrediction,
348-
/// Adds a cursor above the current selection.
349-
AddSelectionAbove,
350-
/// Adds a cursor below the current selection.
351-
AddSelectionBelow,
352366
/// Applies all diff hunks in the editor.
353367
ApplyAllDiffHunks,
354368
/// Applies the diff hunk at the current position.

crates/editor/src/display_map.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,26 @@ impl DisplaySnapshot {
14011401
pub fn excerpt_header_height(&self) -> u32 {
14021402
self.block_snapshot.excerpt_header_height
14031403
}
1404+
1405+
/// Given a `DisplayPoint`, returns another `DisplayPoint` corresponding to
1406+
/// the start of the buffer row that is a given number of buffer rows away
1407+
/// from the provided point.
1408+
///
1409+
/// This moves by buffer rows instead of display rows, a distinction that is
1410+
/// important when soft wrapping is enabled.
1411+
pub fn start_of_relative_buffer_row(&self, point: DisplayPoint, times: isize) -> DisplayPoint {
1412+
let start = self.display_point_to_fold_point(point, Bias::Left);
1413+
let target = start.row() as isize + times;
1414+
let new_row = (target.max(0) as u32).min(self.fold_snapshot().max_point().row());
1415+
1416+
self.clip_point(
1417+
self.fold_point_to_display_point(
1418+
self.fold_snapshot()
1419+
.clip_point(FoldPoint::new(new_row, 0), Bias::Right),
1420+
),
1421+
Bias::Right,
1422+
)
1423+
}
14041424
}
14051425

14061426
#[derive(Copy, Clone, Default, Eq, Ord, PartialOrd, PartialEq)]

0 commit comments

Comments
 (0)