Make vim visual block work better

This commit is contained in:
Conrad Irwin 2023-10-10 08:53:30 -06:00
parent ab050d1890
commit cb76b2a6ad
10 changed files with 85 additions and 32 deletions

View File

@ -509,11 +509,12 @@ impl DisplaySnapshot {
pub fn highlighted_chunks<'a>( pub fn highlighted_chunks<'a>(
&'a self, &'a self,
display_rows: Range<u32>, display_rows: Range<u32>,
language_aware: bool,
style: &'a EditorStyle, style: &'a EditorStyle,
) -> impl Iterator<Item = HighlightedChunk<'a>> { ) -> impl Iterator<Item = HighlightedChunk<'a>> {
self.chunks( self.chunks(
display_rows, display_rows,
true, language_aware,
Some(style.theme.hint), Some(style.theme.hint),
Some(style.theme.suggestion), Some(style.theme.suggestion),
) )
@ -562,7 +563,7 @@ impl DisplaySnapshot {
}) })
} }
fn layout_line_for_row( pub fn lay_out_line_for_row(
&self, &self,
display_row: u32, display_row: u32,
TextLayoutDetails { TextLayoutDetails {
@ -575,7 +576,7 @@ impl DisplaySnapshot {
let mut line = String::new(); let mut line = String::new();
let range = display_row..display_row + 1; let range = display_row..display_row + 1;
for chunk in self.highlighted_chunks(range, editor_style) { for chunk in self.highlighted_chunks(range, false, editor_style) {
line.push_str(chunk.chunk); line.push_str(chunk.chunk);
let text_style = if let Some(style) = chunk.style { let text_style = if let Some(style) = chunk.style {
@ -607,7 +608,7 @@ impl DisplaySnapshot {
display_point: DisplayPoint, display_point: DisplayPoint,
text_layout_details: &TextLayoutDetails, text_layout_details: &TextLayoutDetails,
) -> f32 { ) -> f32 {
let layout_line = self.layout_line_for_row(display_point.row(), text_layout_details); let layout_line = self.lay_out_line_for_row(display_point.row(), text_layout_details);
layout_line.x_for_index(display_point.column() as usize) layout_line.x_for_index(display_point.column() as usize)
} }
@ -617,7 +618,7 @@ impl DisplaySnapshot {
x_coordinate: f32, x_coordinate: f32,
text_layout_details: &TextLayoutDetails, text_layout_details: &TextLayoutDetails,
) -> u32 { ) -> u32 {
let layout_line = self.layout_line_for_row(display_row, text_layout_details); let layout_line = self.lay_out_line_for_row(display_row, text_layout_details);
layout_line.closest_index_for_x(x_coordinate) as u32 layout_line.closest_index_for_x(x_coordinate) as u32
} }

View File

@ -1583,7 +1583,7 @@ impl EditorElement {
.collect() .collect()
} else { } else {
let style = &self.style; let style = &self.style;
let chunks = snapshot.highlighted_chunks(rows.clone(), style); let chunks = snapshot.highlighted_chunks(rows.clone(), true, style);
LineWithInvisibles::from_chunks( LineWithInvisibles::from_chunks(
chunks, chunks,

View File

@ -107,7 +107,6 @@ pub fn up_by_rows(
SelectionGoal::HorizontalPosition(x) => x, SelectionGoal::HorizontalPosition(x) => x,
SelectionGoal::WrappedHorizontalPosition((_, x)) => x, SelectionGoal::WrappedHorizontalPosition((_, x)) => x,
SelectionGoal::HorizontalRange { end, .. } => end, SelectionGoal::HorizontalRange { end, .. } => end,
SelectionGoal::WrappedHorizontalRange { end: (_, end), .. } => end,
_ => map.x_for_point(start, text_layout_details), _ => map.x_for_point(start, text_layout_details),
}; };
@ -144,7 +143,6 @@ pub fn down_by_rows(
SelectionGoal::HorizontalPosition(x) => x, SelectionGoal::HorizontalPosition(x) => x,
SelectionGoal::WrappedHorizontalPosition((_, x)) => x, SelectionGoal::WrappedHorizontalPosition((_, x)) => x,
SelectionGoal::HorizontalRange { end, .. } => end, SelectionGoal::HorizontalRange { end, .. } => end,
SelectionGoal::WrappedHorizontalRange { end: (_, end), .. } => end,
_ => map.x_for_point(start, text_layout_details), _ => map.x_for_point(start, text_layout_details),
}; };

View File

@ -313,10 +313,12 @@ impl SelectionsCollection {
let is_empty = positions.start == positions.end; let is_empty = positions.start == positions.end;
let line_len = display_map.line_len(row); let line_len = display_map.line_len(row);
let start_col = display_map.column_for_x(row, positions.start, text_layout_details); let layed_out_line = display_map.lay_out_line_for_row(row, &text_layout_details);
let start_col = layed_out_line.closest_index_for_x(positions.start) as u32;
if start_col < line_len || (is_empty && start_col == line_len) { if start_col < line_len || (is_empty && start_col == line_len) {
let start = DisplayPoint::new(row, start_col); let start = DisplayPoint::new(row, start_col);
let end_col = display_map.column_for_x(row, positions.end, text_layout_details); let end_col = layed_out_line.closest_index_for_x(positions.end) as u32;
let end = DisplayPoint::new(row, end_col); let end = DisplayPoint::new(row, end_col);
Some(Selection { Some(Selection {

View File

@ -8,7 +8,6 @@ pub enum SelectionGoal {
HorizontalPosition(f32), HorizontalPosition(f32),
HorizontalRange { start: f32, end: f32 }, HorizontalRange { start: f32, end: f32 },
WrappedHorizontalPosition((u32, f32)), WrappedHorizontalPosition((u32, f32)),
WrappedHorizontalRange { start: (u32, f32), end: (u32, f32) },
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]

View File

@ -568,7 +568,6 @@ fn up_down_buffer_rows(
let (goal_wrap, goal_x) = match goal { let (goal_wrap, goal_x) = match goal {
SelectionGoal::WrappedHorizontalPosition((row, x)) => (row, x), SelectionGoal::WrappedHorizontalPosition((row, x)) => (row, x),
SelectionGoal::WrappedHorizontalRange { end: (row, x), .. } => (row, x),
SelectionGoal::HorizontalRange { end, .. } => (select_nth_wrapped_row, end), SelectionGoal::HorizontalRange { end, .. } => (select_nth_wrapped_row, end),
SelectionGoal::HorizontalPosition(x) => (select_nth_wrapped_row, x), SelectionGoal::HorizontalPosition(x) => (select_nth_wrapped_row, x),
_ => { _ => {

View File

@ -653,6 +653,7 @@ async fn test_selection_goal(cx: &mut gpui::TestAppContext) {
.await; .await;
} }
#[gpui::test]
async fn test_wrapped_motions(cx: &mut gpui::TestAppContext) { async fn test_wrapped_motions(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await; let mut cx = NeovimBackedTestContext::new(cx).await;

View File

@ -145,16 +145,18 @@ pub fn visual_block_motion(
let map = &s.display_map(); let map = &s.display_map();
let mut head = s.newest_anchor().head().to_display_point(map); let mut head = s.newest_anchor().head().to_display_point(map);
let mut tail = s.oldest_anchor().tail().to_display_point(map); let mut tail = s.oldest_anchor().tail().to_display_point(map);
dbg!(head, tail);
dbg!(s.newest_anchor().goal);
let (start, end) = match s.newest_anchor().goal { let (start, end) = match s.newest_anchor().goal {
SelectionGoal::HorizontalRange { start, end } if preserve_goal => (start, end), SelectionGoal::HorizontalRange { start, end } if preserve_goal => (start, end),
SelectionGoal::HorizontalPosition(start) if preserve_goal => (start, start + 10.0), SelectionGoal::HorizontalPosition(start) if preserve_goal => (start, start),
_ => ( _ => (
map.x_for_point(tail, &text_layout_details), map.x_for_point(tail, &text_layout_details),
map.x_for_point(head, &text_layout_details), map.x_for_point(head, &text_layout_details),
), ),
}; };
let goal = SelectionGoal::HorizontalRange { start, end }; let mut goal = SelectionGoal::HorizontalRange { start, end };
let was_reversed = tail.column() > head.column(); let was_reversed = tail.column() > head.column();
if !was_reversed && !preserve_goal { if !was_reversed && !preserve_goal {
@ -179,35 +181,44 @@ pub fn visual_block_motion(
let positions = if is_reversed { let positions = if is_reversed {
map.x_for_point(head, &text_layout_details)..map.x_for_point(tail, &text_layout_details) map.x_for_point(head, &text_layout_details)..map.x_for_point(tail, &text_layout_details)
} else if head.column() == tail.column() { } else if head.column() == tail.column() {
map.x_for_point(head, &text_layout_details) let head_forward = movement::saturating_right(map, head);
..map.x_for_point(head, &text_layout_details) + 10.0 map.x_for_point(head, &text_layout_details)..map.x_for_point(head, &text_layout_details)
} else { } else {
map.x_for_point(tail, &text_layout_details)..map.x_for_point(head, &text_layout_details) map.x_for_point(tail, &text_layout_details)..map.x_for_point(head, &text_layout_details)
}; };
if !preserve_goal {
goal = SelectionGoal::HorizontalRange {
start: positions.start,
end: positions.end,
};
}
let mut selections = Vec::new(); let mut selections = Vec::new();
let mut row = tail.row(); let mut row = tail.row();
loop { loop {
let start = map.clip_point( let layed_out_line = map.lay_out_line_for_row(row, &text_layout_details);
DisplayPoint::new( let start = DisplayPoint::new(
row, row,
map.column_for_x(row, positions.start, &text_layout_details), layed_out_line.closest_index_for_x(positions.start) as u32,
),
Bias::Left,
); );
let end = map.clip_point( let mut end = DisplayPoint::new(
DisplayPoint::new( row,
row, layed_out_line.closest_index_for_x(positions.end) as u32,
map.column_for_x(row, positions.end, &text_layout_details),
),
Bias::Left,
); );
if end <= start {
if start.column() == map.line_len(start.row()) {
end = start;
} else {
end = movement::saturating_right(map, start);
}
}
if positions.start if positions.start
<= map.x_for_point( <=
DisplayPoint::new(row, map.line_len(row)), //map.x_for_point(DisplayPoint::new(row, map.line_len(row)), &text_layout_details)
&text_layout_details, layed_out_line.width()
)
{ {
let selection = Selection { let selection = Selection {
id: s.new_selection_id(), id: s.new_selection_id(),
@ -915,6 +926,28 @@ mod test {
.await; .await;
} }
#[gpui::test]
async fn test_visual_block_issue_2123(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
cx.set_shared_state(indoc! {
"The ˇquick brown
fox jumps over
the lazy dog
"
})
.await;
cx.simulate_shared_keystrokes(["ctrl-v", "right", "down"])
.await;
cx.assert_shared_state(indoc! {
"The «quˇ»ick brown
fox «juˇ»mps over
the lazy dog
"
})
.await;
}
#[gpui::test] #[gpui::test]
async fn test_visual_block_insert(cx: &mut gpui::TestAppContext) { async fn test_visual_block_insert(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await; let mut cx = NeovimBackedTestContext::new(cx).await;

View File

@ -0,0 +1,5 @@
{"Put":{"state":"The ˇquick brown\nfox jumps over\nthe lazy dog\n"}}
{"Key":"ctrl-v"}
{"Key":"right"}
{"Key":"down"}
{"Get":{"state":"The «quˇ»ick brown\nfox «juˇ»mps over\nthe lazy dog\n","mode":"VisualBlock"}}

View File

@ -0,0 +1,15 @@
{"SetOption":{"value":"wrap"}}
{"SetOption":{"value":"columns=12"}}
{"Put":{"state":"aaˇaa\n😃😃"}}
{"Key":"j"}
{"Get":{"state":"aaaa\n😃ˇ😃","mode":"Normal"}}
{"Put":{"state":"123456789012aaˇaa\n123456789012😃😃"}}
{"Key":"j"}
{"Get":{"state":"123456789012aaaa\n123456789012😃ˇ😃","mode":"Normal"}}
{"Put":{"state":"123456789012aaˇaa\n123456789012😃😃"}}
{"Key":"j"}
{"Get":{"state":"123456789012aaaa\n123456789012😃ˇ😃","mode":"Normal"}}
{"Put":{"state":"123456789012aaaaˇaaaaaaaa123456789012\nwow\n123456789012😃😃😃😃😃😃123456789012"}}
{"Key":"j"}
{"Key":"j"}
{"Get":{"state":"123456789012aaaaaaaaaaaa123456789012\nwow\n123456789012😃😃ˇ😃😃😃😃123456789012","mode":"Normal"}}