A few more fixes for wrapped line motions

This commit is contained in:
Conrad Irwin 2023-08-24 22:48:58 -06:00
parent 20aa2a4c54
commit dee1a433dd
5 changed files with 123 additions and 22 deletions

View file

@ -535,7 +535,7 @@ fn down_display(
(point, goal) (point, goal)
} }
fn up( pub(crate) fn up(
map: &DisplaySnapshot, map: &DisplaySnapshot,
point: DisplayPoint, point: DisplayPoint,
mut goal: SelectionGoal, mut goal: SelectionGoal,
@ -681,7 +681,11 @@ fn first_non_whitespace(
map.clip_point(last_point, Bias::Left) map.clip_point(last_point, Bias::Left)
} }
fn start_of_line(map: &DisplaySnapshot, display_lines: bool, point: DisplayPoint) -> DisplayPoint { pub(crate) fn start_of_line(
map: &DisplaySnapshot,
display_lines: bool,
point: DisplayPoint,
) -> DisplayPoint {
if display_lines { if display_lines {
map.clip_point(DisplayPoint::new(point.row(), 0), Bias::Right) map.clip_point(DisplayPoint::new(point.row(), 0), Bias::Right)
} else { } else {
@ -689,7 +693,11 @@ fn start_of_line(map: &DisplaySnapshot, display_lines: bool, point: DisplayPoint
} }
} }
fn end_of_line(map: &DisplaySnapshot, display_lines: bool, point: DisplayPoint) -> DisplayPoint { pub(crate) fn end_of_line(
map: &DisplaySnapshot,
display_lines: bool,
point: DisplayPoint,
) -> DisplayPoint {
if display_lines { if display_lines {
map.clip_point( map.clip_point(
DisplayPoint::new(point.row(), map.line_len(point.row())), DisplayPoint::new(point.row(), map.line_len(point.row())),
@ -819,12 +827,8 @@ fn find_backward(
} }
fn next_line_start(map: &DisplaySnapshot, point: DisplayPoint, times: usize) -> DisplayPoint { fn next_line_start(map: &DisplaySnapshot, point: DisplayPoint, times: usize) -> DisplayPoint {
let new_row = (point.row() + times as u32).min(map.max_buffer_row()); let correct_line = down(map, point, SelectionGoal::None, times).0;
first_non_whitespace( first_non_whitespace(map, false, correct_line)
map,
false,
map.clip_point(DisplayPoint::new(new_row, 0), Bias::Left),
)
} }
#[cfg(test)] #[cfg(test)]

View file

@ -10,7 +10,7 @@ mod yank;
use std::sync::Arc; use std::sync::Arc;
use crate::{ use crate::{
motion::Motion, motion::{self, Motion},
object::Object, object::Object,
state::{Mode, Operator}, state::{Mode, Operator},
Vim, Vim,
@ -214,19 +214,19 @@ fn insert_line_above(_: &mut Workspace, _: &InsertLineAbove, cx: &mut ViewContex
.collect(); .collect();
let edits = selection_start_rows.into_iter().map(|row| { let edits = selection_start_rows.into_iter().map(|row| {
let (indent, _) = map.line_indent(row); let (indent, _) = map.line_indent(row);
let start_of_line = map let start_of_line =
.clip_point(DisplayPoint::new(row, 0), Bias::Left) motion::start_of_line(&map, false, DisplayPoint::new(row, 0))
.to_point(&map); .to_point(&map);
let mut new_text = " ".repeat(indent as usize); let mut new_text = " ".repeat(indent as usize);
new_text.push('\n'); new_text.push('\n');
(start_of_line..start_of_line, new_text) (start_of_line..start_of_line, new_text)
}); });
editor.edit_with_autoindent(edits, cx); editor.edit_with_autoindent(edits, cx);
editor.change_selections(Some(Autoscroll::fit()), cx, |s| { editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
s.move_cursors_with(|map, mut cursor, _| { s.move_cursors_with(|map, cursor, _| {
*cursor.row_mut() -= 1; let previous_line = motion::up(map, cursor, SelectionGoal::None, 1).0;
*cursor.column_mut() = map.line_len(cursor.row()); let insert_point = motion::end_of_line(map, false, previous_line);
(map.clip_point(cursor, Bias::Left), SelectionGoal::None) (insert_point, SelectionGoal::None)
}); });
}); });
}); });
@ -240,15 +240,16 @@ fn insert_line_below(_: &mut Workspace, _: &InsertLineBelow, cx: &mut ViewContex
vim.update_active_editor(cx, |editor, cx| { vim.update_active_editor(cx, |editor, cx| {
editor.transact(cx, |editor, cx| { editor.transact(cx, |editor, cx| {
let (map, old_selections) = editor.selections.all_display(cx); let (map, old_selections) = editor.selections.all_display(cx);
let selection_end_rows: HashSet<u32> = old_selections let selection_end_rows: HashSet<u32> = old_selections
.into_iter() .into_iter()
.map(|selection| selection.end.row()) .map(|selection| selection.end.row())
.collect(); .collect();
let edits = selection_end_rows.into_iter().map(|row| { let edits = selection_end_rows.into_iter().map(|row| {
let (indent, _) = map.line_indent(row); let (indent, _) = map.line_indent(row);
let end_of_line = map let end_of_line =
.clip_point(DisplayPoint::new(row, map.line_len(row)), Bias::Left) motion::end_of_line(&map, false, DisplayPoint::new(row, 0)).to_point(&map);
.to_point(&map);
let mut new_text = "\n".to_string(); let mut new_text = "\n".to_string();
new_text.push_str(&" ".repeat(indent as usize)); new_text.push_str(&" ".repeat(indent as usize));
(end_of_line..end_of_line, new_text) (end_of_line..end_of_line, new_text)

View file

@ -358,6 +358,79 @@ async fn test_wrapped_lines(cx: &mut gpui::TestAppContext) {
twelve char twelve char
"}) "})
.await; .await;
cx.set_shared_state(indoc! { "
tˇwelve char twelve char
twelve char
"})
.await;
cx.simulate_shared_keystrokes(["enter"]).await;
cx.assert_shared_state(indoc! { "
twelve char twelve char
ˇtwelve char
"})
.await;
cx.set_shared_state(indoc! { "
twelve char
tˇwelve char twelve char
twelve char
"})
.await;
cx.simulate_shared_keystrokes(["o", "o", "escape"]).await;
cx.assert_shared_state(indoc! { "
twelve char
twelve char twelve char
ˇo
twelve char
"})
.await;
cx.set_shared_state(indoc! { "
twelve char
tˇwelve char twelve char
twelve char
"})
.await;
cx.simulate_shared_keystrokes(["shift-a", "a", "escape"])
.await;
cx.assert_shared_state(indoc! { "
twelve char
twelve char twelve charˇa
twelve char
"})
.await;
cx.simulate_shared_keystrokes(["shift-i", "i", "escape"])
.await;
cx.assert_shared_state(indoc! { "
twelve char
ˇitwelve char twelve chara
twelve char
"})
.await;
cx.simulate_shared_keystrokes(["shift-d"]).await;
cx.assert_shared_state(indoc! { "
twelve char
ˇ
twelve char
"})
.await;
cx.set_shared_state(indoc! { "
twelve char
twelve char tˇwelve char
twelve char
"})
.await;
cx.simulate_shared_keystrokes(["shift-o", "o", "escape"])
.await;
cx.assert_shared_state(indoc! { "
twelve char
ˇo
twelve char twelve char
twelve char
"})
.await;
} }
#[gpui::test] #[gpui::test]

View file

@ -1,4 +1,3 @@
use editor::EditorSettings;
use indoc::indoc; use indoc::indoc;
use settings::SettingsStore; use settings::SettingsStore;
use std::ops::{Deref, DerefMut, Range}; use std::ops::{Deref, DerefMut, Range};
@ -6,7 +5,7 @@ use std::ops::{Deref, DerefMut, Range};
use collections::{HashMap, HashSet}; use collections::{HashMap, HashSet};
use gpui::ContextHandle; use gpui::ContextHandle;
use language::{ use language::{
language_settings::{AllLanguageSettings, LanguageSettings, SoftWrap}, language_settings::{AllLanguageSettings, SoftWrap},
OffsetRangeExt, OffsetRangeExt,
}; };
use util::test::{generate_marked_text, marked_text_offsets}; use util::test::{generate_marked_text, marked_text_offsets};

View file

@ -24,3 +24,27 @@
{"Get":{"state":"twelve charˇ twelve char\ntwelve char\n","mode":"Normal"}} {"Get":{"state":"twelve charˇ twelve char\ntwelve char\n","mode":"Normal"}}
{"Key":"$"} {"Key":"$"}
{"Get":{"state":"twelve char twelve chaˇr\ntwelve char\n","mode":"Normal"}} {"Get":{"state":"twelve char twelve chaˇr\ntwelve char\n","mode":"Normal"}}
{"Put":{"state":"tˇwelve char twelve char\ntwelve char\n"}}
{"Key":"enter"}
{"Get":{"state":"twelve char twelve char\nˇtwelve char\n","mode":"Normal"}}
{"Put":{"state":"twelve char\ntˇwelve char twelve char\ntwelve char\n"}}
{"Key":"o"}
{"Key":"o"}
{"Key":"escape"}
{"Get":{"state":"twelve char\ntwelve char twelve char\nˇo\ntwelve char\n","mode":"Normal"}}
{"Put":{"state":"twelve char\ntˇwelve char twelve char\ntwelve char\n"}}
{"Key":"shift-a"}
{"Key":"a"}
{"Key":"escape"}
{"Get":{"state":"twelve char\ntwelve char twelve charˇa\ntwelve char\n","mode":"Normal"}}
{"Key":"shift-i"}
{"Key":"i"}
{"Key":"escape"}
{"Get":{"state":"twelve char\nˇitwelve char twelve chara\ntwelve char\n","mode":"Normal"}}
{"Key":"shift-d"}
{"Get":{"state":"twelve char\nˇ\ntwelve char\n","mode":"Normal"}}
{"Put":{"state":"twelve char\ntwelve char tˇwelve char\ntwelve char\n"}}
{"Key":"shift-o"}
{"Key":"o"}
{"Key":"escape"}
{"Get":{"state":"twelve char\nˇo\ntwelve char twelve char\ntwelve char\n","mode":"Normal"}}