editor: Ignore soft wrapped lines when adding selection above or below (#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 #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 <heysmitbarmase@gmail.com>
This commit is contained in:
@@ -1525,29 +1525,6 @@ fn wrapping_right_single(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayP
|
||||
}
|
||||
}
|
||||
|
||||
/// Given a point, returns the start of the buffer row that is a given number of
|
||||
/// buffer rows away from the current position.
|
||||
///
|
||||
/// This moves by buffer rows instead of display rows, a distinction that is
|
||||
/// important when soft wrapping is enabled.
|
||||
pub(crate) fn start_of_relative_buffer_row(
|
||||
map: &DisplaySnapshot,
|
||||
point: DisplayPoint,
|
||||
times: isize,
|
||||
) -> DisplayPoint {
|
||||
let start = map.display_point_to_fold_point(point, Bias::Left);
|
||||
let target = start.row() as isize + times;
|
||||
let new_row = (target.max(0) as u32).min(map.fold_snapshot().max_point().row());
|
||||
|
||||
map.clip_point(
|
||||
map.fold_point_to_display_point(
|
||||
map.fold_snapshot()
|
||||
.clip_point(FoldPoint::new(new_row, 0), Bias::Right),
|
||||
),
|
||||
Bias::Right,
|
||||
)
|
||||
}
|
||||
|
||||
fn up_down_buffer_rows(
|
||||
map: &DisplaySnapshot,
|
||||
mut point: DisplayPoint,
|
||||
@@ -2127,7 +2104,7 @@ pub(crate) fn end_of_line(
|
||||
times: usize,
|
||||
) -> DisplayPoint {
|
||||
if times > 1 {
|
||||
point = start_of_relative_buffer_row(map, point, times as isize - 1);
|
||||
point = map.start_of_relative_buffer_row(point, times as isize - 1);
|
||||
}
|
||||
if display_lines {
|
||||
map.clip_point(
|
||||
@@ -2732,17 +2709,17 @@ fn sneak_backward(
|
||||
}
|
||||
|
||||
fn next_line_start(map: &DisplaySnapshot, point: DisplayPoint, times: usize) -> DisplayPoint {
|
||||
let correct_line = start_of_relative_buffer_row(map, point, times as isize);
|
||||
let correct_line = map.start_of_relative_buffer_row(point, times as isize);
|
||||
first_non_whitespace(map, false, correct_line)
|
||||
}
|
||||
|
||||
fn previous_line_start(map: &DisplaySnapshot, point: DisplayPoint, times: usize) -> DisplayPoint {
|
||||
let correct_line = start_of_relative_buffer_row(map, point, -(times as isize));
|
||||
let correct_line = map.start_of_relative_buffer_row(point, -(times as isize));
|
||||
first_non_whitespace(map, false, correct_line)
|
||||
}
|
||||
|
||||
fn go_to_column(map: &DisplaySnapshot, point: DisplayPoint, times: usize) -> DisplayPoint {
|
||||
let correct_line = start_of_relative_buffer_row(map, point, 0);
|
||||
let correct_line = map.start_of_relative_buffer_row(point, 0);
|
||||
right(map, correct_line, times.saturating_sub(1))
|
||||
}
|
||||
|
||||
@@ -2752,7 +2729,7 @@ pub(crate) fn next_line_end(
|
||||
times: usize,
|
||||
) -> DisplayPoint {
|
||||
if times > 1 {
|
||||
point = start_of_relative_buffer_row(map, point, times as isize - 1);
|
||||
point = map.start_of_relative_buffer_row(point, times as isize - 1);
|
||||
}
|
||||
end_of_line(map, false, point, 1)
|
||||
}
|
||||
|
||||
@@ -679,7 +679,7 @@ impl Vim {
|
||||
editor.edit_with_autoindent(edits, cx);
|
||||
editor.change_selections(Default::default(), window, cx, |s| {
|
||||
s.move_cursors_with(|map, cursor, _| {
|
||||
let previous_line = motion::start_of_relative_buffer_row(map, cursor, -1);
|
||||
let previous_line = map.start_of_relative_buffer_row(cursor, -1);
|
||||
let insert_point = motion::end_of_line(map, false, previous_line, 1);
|
||||
(insert_point, SelectionGoal::None)
|
||||
});
|
||||
|
||||
@@ -15,10 +15,7 @@ use workspace::searchable::Direction;
|
||||
|
||||
use crate::{
|
||||
Vim,
|
||||
motion::{
|
||||
Motion, MotionKind, first_non_whitespace, next_line_end, start_of_line,
|
||||
start_of_relative_buffer_row,
|
||||
},
|
||||
motion::{Motion, MotionKind, first_non_whitespace, next_line_end, start_of_line},
|
||||
object::Object,
|
||||
state::{Mark, Mode, Operator},
|
||||
};
|
||||
@@ -406,7 +403,9 @@ impl Vim {
|
||||
// Move to the next or previous buffer row, ensuring that
|
||||
// wrapped lines are handled correctly.
|
||||
let direction = if tail.row() > head.row() { -1 } else { 1 };
|
||||
row = start_of_relative_buffer_row(map, DisplayPoint::new(row, 0), direction).row();
|
||||
row = map
|
||||
.start_of_relative_buffer_row(DisplayPoint::new(row, 0), direction)
|
||||
.row();
|
||||
}
|
||||
|
||||
s.select(selections);
|
||||
|
||||
Reference in New Issue
Block a user