visual.rs

   1use std::sync::Arc;
   2
   3use collections::HashMap;
   4use editor::{
   5    display_map::{DisplaySnapshot, ToDisplayPoint},
   6    movement,
   7    scroll::Autoscroll,
   8    Bias, DisplayPoint, Editor, ToOffset,
   9};
  10use gpui::{actions, ViewContext};
  11use language::{Point, Selection, SelectionGoal};
  12use multi_buffer::MultiBufferRow;
  13use search::BufferSearchBar;
  14use util::ResultExt;
  15use workspace::searchable::Direction;
  16
  17use crate::{
  18    motion::{first_non_whitespace, next_line_end, start_of_line, Motion},
  19    object::Object,
  20    state::{Mode, Operator},
  21    Vim,
  22};
  23
  24actions!(
  25    vim,
  26    [
  27        ToggleVisual,
  28        ToggleVisualLine,
  29        ToggleVisualBlock,
  30        VisualDelete,
  31        VisualDeleteLine,
  32        VisualYank,
  33        VisualYankLine,
  34        OtherEnd,
  35        SelectNext,
  36        SelectPrevious,
  37        SelectNextMatch,
  38        SelectPreviousMatch,
  39        RestoreVisualSelection,
  40        VisualInsertEndOfLine,
  41        VisualInsertFirstNonWhiteSpace,
  42    ]
  43);
  44
  45pub fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) {
  46    Vim::action(editor, cx, |vim, _: &ToggleVisual, cx| {
  47        vim.toggle_mode(Mode::Visual, cx)
  48    });
  49    Vim::action(editor, cx, |vim, _: &ToggleVisualLine, cx| {
  50        vim.toggle_mode(Mode::VisualLine, cx)
  51    });
  52    Vim::action(editor, cx, |vim, _: &ToggleVisualBlock, cx| {
  53        vim.toggle_mode(Mode::VisualBlock, cx)
  54    });
  55    Vim::action(editor, cx, Vim::other_end);
  56    Vim::action(editor, cx, Vim::visual_insert_end_of_line);
  57    Vim::action(editor, cx, Vim::visual_insert_first_non_white_space);
  58    Vim::action(editor, cx, |vim, _: &VisualDelete, cx| {
  59        vim.record_current_action(cx);
  60        vim.visual_delete(false, cx);
  61    });
  62    Vim::action(editor, cx, |vim, _: &VisualDeleteLine, cx| {
  63        vim.record_current_action(cx);
  64        vim.visual_delete(true, cx);
  65    });
  66    Vim::action(editor, cx, |vim, _: &VisualYank, cx| vim.visual_yank(cx));
  67
  68    Vim::action(editor, cx, Vim::select_next);
  69    Vim::action(editor, cx, Vim::select_previous);
  70    Vim::action(editor, cx, |vim, _: &SelectNextMatch, cx| {
  71        vim.select_match(Direction::Next, cx);
  72    });
  73    Vim::action(editor, cx, |vim, _: &SelectPreviousMatch, cx| {
  74        vim.select_match(Direction::Prev, cx);
  75    });
  76
  77    Vim::action(editor, cx, |vim, _: &RestoreVisualSelection, cx| {
  78        let Some((stored_mode, reversed)) = vim.stored_visual_mode.take() else {
  79            return;
  80        };
  81        let Some((start, end)) = vim.marks.get("<").zip(vim.marks.get(">")) else {
  82            return;
  83        };
  84        let ranges = start
  85            .iter()
  86            .zip(end)
  87            .zip(reversed)
  88            .map(|((start, end), reversed)| (*start, *end, reversed))
  89            .collect::<Vec<_>>();
  90
  91        if vim.mode.is_visual() {
  92            vim.create_visual_marks(vim.mode, cx);
  93        }
  94
  95        vim.update_editor(cx, |_, editor, cx| {
  96            editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
  97                let map = s.display_map();
  98                let ranges = ranges
  99                    .into_iter()
 100                    .map(|(start, end, reversed)| {
 101                        let new_end = movement::saturating_right(&map, end.to_display_point(&map));
 102                        Selection {
 103                            id: s.new_selection_id(),
 104                            start: start.to_offset(&map.buffer_snapshot),
 105                            end: new_end.to_offset(&map, Bias::Left),
 106                            reversed,
 107                            goal: SelectionGoal::None,
 108                        }
 109                    })
 110                    .collect();
 111                s.select(ranges);
 112            })
 113        });
 114        vim.switch_mode(stored_mode, true, cx)
 115    });
 116}
 117
 118impl Vim {
 119    pub fn visual_motion(
 120        &mut self,
 121        motion: Motion,
 122        times: Option<usize>,
 123        cx: &mut ViewContext<Self>,
 124    ) {
 125        self.update_editor(cx, |vim, editor, cx| {
 126            let text_layout_details = editor.text_layout_details(cx);
 127            if vim.mode == Mode::VisualBlock
 128                && !matches!(
 129                    motion,
 130                    Motion::EndOfLine {
 131                        display_lines: false
 132                    }
 133                )
 134            {
 135                let is_up_or_down = matches!(motion, Motion::Up { .. } | Motion::Down { .. });
 136                vim.visual_block_motion(is_up_or_down, editor, cx, |map, point, goal| {
 137                    motion.move_point(map, point, goal, times, &text_layout_details)
 138                })
 139            } else {
 140                editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 141                    s.move_with(|map, selection| {
 142                        let was_reversed = selection.reversed;
 143                        let mut current_head = selection.head();
 144
 145                        // our motions assume the current character is after the cursor,
 146                        // but in (forward) visual mode the current character is just
 147                        // before the end of the selection.
 148
 149                        // If the file ends with a newline (which is common) we don't do this.
 150                        // so that if you go to the end of such a file you can use "up" to go
 151                        // to the previous line and have it work somewhat as expected.
 152                        #[allow(clippy::nonminimal_bool)]
 153                        if !selection.reversed
 154                            && !selection.is_empty()
 155                            && !(selection.end.column() == 0 && selection.end == map.max_point())
 156                        {
 157                            current_head = movement::left(map, selection.end)
 158                        }
 159
 160                        let Some((new_head, goal)) = motion.move_point(
 161                            map,
 162                            current_head,
 163                            selection.goal,
 164                            times,
 165                            &text_layout_details,
 166                        ) else {
 167                            return;
 168                        };
 169
 170                        selection.set_head(new_head, goal);
 171
 172                        // ensure the current character is included in the selection.
 173                        if !selection.reversed {
 174                            let next_point = if vim.mode == Mode::VisualBlock {
 175                                movement::saturating_right(map, selection.end)
 176                            } else {
 177                                movement::right(map, selection.end)
 178                            };
 179
 180                            if !(next_point.column() == 0 && next_point == map.max_point()) {
 181                                selection.end = next_point;
 182                            }
 183                        }
 184
 185                        // vim always ensures the anchor character stays selected.
 186                        // if our selection has reversed, we need to move the opposite end
 187                        // to ensure the anchor is still selected.
 188                        if was_reversed && !selection.reversed {
 189                            selection.start = movement::left(map, selection.start);
 190                        } else if !was_reversed && selection.reversed {
 191                            selection.end = movement::right(map, selection.end);
 192                        }
 193                    })
 194                });
 195            }
 196        });
 197    }
 198
 199    pub fn visual_block_motion(
 200        &mut self,
 201        preserve_goal: bool,
 202        editor: &mut Editor,
 203        cx: &mut ViewContext<Editor>,
 204        mut move_selection: impl FnMut(
 205            &DisplaySnapshot,
 206            DisplayPoint,
 207            SelectionGoal,
 208        ) -> Option<(DisplayPoint, SelectionGoal)>,
 209    ) {
 210        let text_layout_details = editor.text_layout_details(cx);
 211        editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 212            let map = &s.display_map();
 213            let mut head = s.newest_anchor().head().to_display_point(map);
 214            let mut tail = s.oldest_anchor().tail().to_display_point(map);
 215
 216            let mut head_x = map.x_for_display_point(head, &text_layout_details);
 217            let mut tail_x = map.x_for_display_point(tail, &text_layout_details);
 218
 219            let (start, end) = match s.newest_anchor().goal {
 220                SelectionGoal::HorizontalRange { start, end } if preserve_goal => (start, end),
 221                SelectionGoal::HorizontalPosition(start) if preserve_goal => (start, start),
 222                _ => (tail_x.0, head_x.0),
 223            };
 224            let mut goal = SelectionGoal::HorizontalRange { start, end };
 225
 226            let was_reversed = tail_x > head_x;
 227            if !was_reversed && !preserve_goal {
 228                head = movement::saturating_left(map, head);
 229            }
 230
 231            let Some((new_head, _)) = move_selection(map, head, goal) else {
 232                return;
 233            };
 234            head = new_head;
 235            head_x = map.x_for_display_point(head, &text_layout_details);
 236
 237            let is_reversed = tail_x > head_x;
 238            if was_reversed && !is_reversed {
 239                tail = movement::saturating_left(map, tail);
 240                tail_x = map.x_for_display_point(tail, &text_layout_details);
 241            } else if !was_reversed && is_reversed {
 242                tail = movement::saturating_right(map, tail);
 243                tail_x = map.x_for_display_point(tail, &text_layout_details);
 244            }
 245            if !is_reversed && !preserve_goal {
 246                head = movement::saturating_right(map, head);
 247                head_x = map.x_for_display_point(head, &text_layout_details);
 248            }
 249
 250            let positions = if is_reversed {
 251                head_x..tail_x
 252            } else {
 253                tail_x..head_x
 254            };
 255
 256            if !preserve_goal {
 257                goal = SelectionGoal::HorizontalRange {
 258                    start: positions.start.0,
 259                    end: positions.end.0,
 260                };
 261            }
 262
 263            let mut selections = Vec::new();
 264            let mut row = tail.row();
 265
 266            loop {
 267                let laid_out_line = map.layout_row(row, &text_layout_details);
 268                let start = DisplayPoint::new(
 269                    row,
 270                    laid_out_line.closest_index_for_x(positions.start) as u32,
 271                );
 272                let mut end =
 273                    DisplayPoint::new(row, laid_out_line.closest_index_for_x(positions.end) as u32);
 274                if end <= start {
 275                    if start.column() == map.line_len(start.row()) {
 276                        end = start;
 277                    } else {
 278                        end = movement::saturating_right(map, start);
 279                    }
 280                }
 281
 282                if positions.start <= laid_out_line.width {
 283                    let selection = Selection {
 284                        id: s.new_selection_id(),
 285                        start: start.to_point(map),
 286                        end: end.to_point(map),
 287                        reversed: is_reversed,
 288                        goal,
 289                    };
 290
 291                    selections.push(selection);
 292                }
 293                if row == head.row() {
 294                    break;
 295                }
 296                if tail.row() > head.row() {
 297                    row.0 -= 1
 298                } else {
 299                    row.0 += 1
 300                }
 301            }
 302
 303            s.select(selections);
 304        })
 305    }
 306
 307    pub fn visual_object(&mut self, object: Object, cx: &mut ViewContext<Vim>) {
 308        if let Some(Operator::Object { around }) = self.active_operator() {
 309            self.pop_operator(cx);
 310            let current_mode = self.mode;
 311            let target_mode = object.target_visual_mode(current_mode);
 312            if target_mode != current_mode {
 313                self.switch_mode(target_mode, true, cx);
 314            }
 315
 316            self.update_editor(cx, |_, editor, cx| {
 317                editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 318                    s.move_with(|map, selection| {
 319                        let mut mut_selection = selection.clone();
 320
 321                        // all our motions assume that the current character is
 322                        // after the cursor; however in the case of a visual selection
 323                        // the current character is before the cursor.
 324                        // But this will affect the judgment of the html tag
 325                        // so the html tag needs to skip this logic.
 326                        if !selection.reversed && object != Object::Tag {
 327                            mut_selection.set_head(
 328                                movement::left(map, mut_selection.head()),
 329                                mut_selection.goal,
 330                            );
 331                        }
 332
 333                        if let Some(range) = object.range(map, mut_selection, around) {
 334                            if !range.is_empty() {
 335                                let expand_both_ways = object.always_expands_both_ways()
 336                                    || selection.is_empty()
 337                                    || movement::right(map, selection.start) == selection.end;
 338
 339                                if expand_both_ways {
 340                                    selection.start = range.start;
 341                                    selection.end = range.end;
 342                                } else if selection.reversed {
 343                                    selection.start = range.start;
 344                                } else {
 345                                    selection.end = range.end;
 346                                }
 347                            }
 348
 349                            // In the visual selection result of a paragraph object, the cursor is
 350                            // placed at the start of the last line. And in the visual mode, the
 351                            // selection end is located after the end character. So, adjustment of
 352                            // selection end is needed.
 353                            //
 354                            // We don't do this adjustment for a one-line blank paragraph since the
 355                            // trailing newline is included in its selection from the beginning.
 356                            if object == Object::Paragraph && range.start != range.end {
 357                                let row_of_selection_end_line = selection.end.to_point(map).row;
 358                                let new_selection_end = if map
 359                                    .buffer_snapshot
 360                                    .line_len(MultiBufferRow(row_of_selection_end_line))
 361                                    == 0
 362                                {
 363                                    Point::new(row_of_selection_end_line + 1, 0)
 364                                } else {
 365                                    Point::new(row_of_selection_end_line, 1)
 366                                };
 367                                selection.end = new_selection_end.to_display_point(map);
 368                            }
 369                        }
 370                    });
 371                });
 372            });
 373        }
 374    }
 375
 376    fn visual_insert_end_of_line(&mut self, _: &VisualInsertEndOfLine, cx: &mut ViewContext<Self>) {
 377        self.update_editor(cx, |_, editor, cx| {
 378            editor.split_selection_into_lines(&Default::default(), cx);
 379            editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 380                s.move_cursors_with(|map, cursor, _| {
 381                    (next_line_end(map, cursor, 1), SelectionGoal::None)
 382                });
 383            });
 384        });
 385
 386        self.switch_mode(Mode::Insert, false, cx);
 387    }
 388
 389    fn visual_insert_first_non_white_space(
 390        &mut self,
 391        _: &VisualInsertFirstNonWhiteSpace,
 392        cx: &mut ViewContext<Self>,
 393    ) {
 394        self.update_editor(cx, |_, editor, cx| {
 395            editor.split_selection_into_lines(&Default::default(), cx);
 396            editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 397                s.move_cursors_with(|map, cursor, _| {
 398                    (
 399                        first_non_whitespace(map, false, cursor),
 400                        SelectionGoal::None,
 401                    )
 402                });
 403            });
 404        });
 405
 406        self.switch_mode(Mode::Insert, false, cx);
 407    }
 408
 409    fn toggle_mode(&mut self, mode: Mode, cx: &mut ViewContext<Self>) {
 410        if self.mode == mode {
 411            self.switch_mode(Mode::Normal, false, cx);
 412        } else {
 413            self.switch_mode(mode, false, cx);
 414        }
 415    }
 416
 417    pub fn other_end(&mut self, _: &OtherEnd, cx: &mut ViewContext<Self>) {
 418        self.update_editor(cx, |_, editor, cx| {
 419            editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 420                s.move_with(|_, selection| {
 421                    selection.reversed = !selection.reversed;
 422                })
 423            })
 424        });
 425    }
 426
 427    pub fn visual_delete(&mut self, line_mode: bool, cx: &mut ViewContext<Self>) {
 428        self.store_visual_marks(cx);
 429        self.update_editor(cx, |vim, editor, cx| {
 430            let mut original_columns: HashMap<_, _> = Default::default();
 431            let line_mode = line_mode || editor.selections.line_mode;
 432
 433            editor.transact(cx, |editor, cx| {
 434                editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 435                    s.move_with(|map, selection| {
 436                        if line_mode {
 437                            let mut position = selection.head();
 438                            if !selection.reversed {
 439                                position = movement::left(map, position);
 440                            }
 441                            original_columns.insert(selection.id, position.to_point(map).column);
 442                            if vim.mode == Mode::VisualBlock {
 443                                *selection.end.column_mut() = map.line_len(selection.end.row())
 444                            } else if vim.mode != Mode::VisualLine {
 445                                selection.start = DisplayPoint::new(selection.start.row(), 0);
 446                                if selection.end.row() == map.max_point().row() {
 447                                    selection.end = map.max_point()
 448                                } else {
 449                                    *selection.end.row_mut() += 1;
 450                                    *selection.end.column_mut() = 0;
 451                                }
 452                            }
 453                        }
 454                        selection.goal = SelectionGoal::None;
 455                    });
 456                });
 457                vim.copy_selections_content(editor, line_mode, cx);
 458                editor.insert("", cx);
 459
 460                // Fixup cursor position after the deletion
 461                editor.set_clip_at_line_ends(true, cx);
 462                editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 463                    s.move_with(|map, selection| {
 464                        let mut cursor = selection.head().to_point(map);
 465
 466                        if let Some(column) = original_columns.get(&selection.id) {
 467                            cursor.column = *column
 468                        }
 469                        let cursor = map.clip_point(cursor.to_display_point(map), Bias::Left);
 470                        selection.collapse_to(cursor, selection.goal)
 471                    });
 472                    if vim.mode == Mode::VisualBlock {
 473                        s.select_anchors(vec![s.first_anchor()])
 474                    }
 475                });
 476            })
 477        });
 478        self.switch_mode(Mode::Normal, true, cx);
 479    }
 480
 481    pub fn visual_yank(&mut self, cx: &mut ViewContext<Self>) {
 482        self.store_visual_marks(cx);
 483        self.update_editor(cx, |vim, editor, cx| {
 484            let line_mode = editor.selections.line_mode;
 485            vim.yank_selections_content(editor, line_mode, cx);
 486            editor.change_selections(None, cx, |s| {
 487                s.move_with(|map, selection| {
 488                    if line_mode {
 489                        selection.start = start_of_line(map, false, selection.start);
 490                    };
 491                    selection.collapse_to(selection.start, SelectionGoal::None)
 492                });
 493                if vim.mode == Mode::VisualBlock {
 494                    s.select_anchors(vec![s.first_anchor()])
 495                }
 496            });
 497        });
 498        self.switch_mode(Mode::Normal, true, cx);
 499    }
 500
 501    pub(crate) fn visual_replace(&mut self, text: Arc<str>, cx: &mut ViewContext<Self>) {
 502        self.stop_recording(cx);
 503        self.update_editor(cx, |_, editor, cx| {
 504            editor.transact(cx, |editor, cx| {
 505                let (display_map, selections) = editor.selections.all_adjusted_display(cx);
 506
 507                // Selections are biased right at the start. So we need to store
 508                // anchors that are biased left so that we can restore the selections
 509                // after the change
 510                let stable_anchors = editor
 511                    .selections
 512                    .disjoint_anchors()
 513                    .iter()
 514                    .map(|selection| {
 515                        let start = selection.start.bias_left(&display_map.buffer_snapshot);
 516                        start..start
 517                    })
 518                    .collect::<Vec<_>>();
 519
 520                let mut edits = Vec::new();
 521                for selection in selections.iter() {
 522                    let selection = selection.clone();
 523                    for row_range in
 524                        movement::split_display_range_by_lines(&display_map, selection.range())
 525                    {
 526                        let range = row_range.start.to_offset(&display_map, Bias::Right)
 527                            ..row_range.end.to_offset(&display_map, Bias::Right);
 528                        let text = text.repeat(range.len());
 529                        edits.push((range, text));
 530                    }
 531                }
 532
 533                editor.buffer().update(cx, |buffer, cx| {
 534                    buffer.edit(edits, None, cx);
 535                });
 536                editor.change_selections(None, cx, |s| s.select_ranges(stable_anchors));
 537            });
 538        });
 539        self.switch_mode(Mode::Normal, false, cx);
 540    }
 541
 542    pub fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
 543        let count = self
 544            .take_count(cx)
 545            .unwrap_or_else(|| if self.mode.is_visual() { 1 } else { 2 });
 546        self.update_editor(cx, |_, editor, cx| {
 547            editor.set_clip_at_line_ends(false, cx);
 548            for _ in 0..count {
 549                if editor
 550                    .select_next(&Default::default(), cx)
 551                    .log_err()
 552                    .is_none()
 553                {
 554                    break;
 555                }
 556            }
 557        });
 558    }
 559
 560    pub fn select_previous(&mut self, _: &SelectPrevious, cx: &mut ViewContext<Self>) {
 561        let count = self
 562            .take_count(cx)
 563            .unwrap_or_else(|| if self.mode.is_visual() { 1 } else { 2 });
 564        self.update_editor(cx, |_, editor, cx| {
 565            for _ in 0..count {
 566                if editor
 567                    .select_previous(&Default::default(), cx)
 568                    .log_err()
 569                    .is_none()
 570                {
 571                    break;
 572                }
 573            }
 574        });
 575    }
 576
 577    pub fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
 578        let count = self.take_count(cx).unwrap_or(1);
 579        let Some(pane) = self.pane(cx) else {
 580            return;
 581        };
 582        let vim_is_normal = self.mode == Mode::Normal;
 583        let mut start_selection = 0usize;
 584        let mut end_selection = 0usize;
 585
 586        self.update_editor(cx, |_, editor, _| {
 587            editor.set_collapse_matches(false);
 588        });
 589        if vim_is_normal {
 590            pane.update(cx, |pane, cx| {
 591                if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>()
 592                {
 593                    search_bar.update(cx, |search_bar, cx| {
 594                        if !search_bar.has_active_match() || !search_bar.show(cx) {
 595                            return;
 596                        }
 597                        // without update_match_index there is a bug when the cursor is before the first match
 598                        search_bar.update_match_index(cx);
 599                        search_bar.select_match(direction.opposite(), 1, cx);
 600                    });
 601                }
 602            });
 603        }
 604        self.update_editor(cx, |_, editor, cx| {
 605            let latest = editor.selections.newest::<usize>(cx);
 606            start_selection = latest.start;
 607            end_selection = latest.end;
 608        });
 609
 610        let mut match_exists = false;
 611        pane.update(cx, |pane, cx| {
 612            if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
 613                search_bar.update(cx, |search_bar, cx| {
 614                    search_bar.update_match_index(cx);
 615                    search_bar.select_match(direction, count, cx);
 616                    match_exists = search_bar.match_exists(cx);
 617                });
 618            }
 619        });
 620        if !match_exists {
 621            self.clear_operator(cx);
 622            self.stop_replaying(cx);
 623            return;
 624        }
 625        self.update_editor(cx, |_, editor, cx| {
 626            let latest = editor.selections.newest::<usize>(cx);
 627            if vim_is_normal {
 628                start_selection = latest.start;
 629                end_selection = latest.end;
 630            } else {
 631                start_selection = start_selection.min(latest.start);
 632                end_selection = end_selection.max(latest.end);
 633            }
 634            if direction == Direction::Prev {
 635                std::mem::swap(&mut start_selection, &mut end_selection);
 636            }
 637            editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 638                s.select_ranges([start_selection..end_selection]);
 639            });
 640            editor.set_collapse_matches(true);
 641        });
 642
 643        match self.maybe_pop_operator() {
 644            Some(Operator::Change) => self.substitute(None, false, cx),
 645            Some(Operator::Delete) => {
 646                self.stop_recording(cx);
 647                self.visual_delete(false, cx)
 648            }
 649            Some(Operator::Yank) => self.visual_yank(cx),
 650            _ => {} // Ignoring other operators
 651        }
 652    }
 653}
 654#[cfg(test)]
 655mod test {
 656    use indoc::indoc;
 657    use workspace::item::Item;
 658
 659    use crate::{
 660        state::Mode,
 661        test::{NeovimBackedTestContext, VimTestContext},
 662    };
 663
 664    #[gpui::test]
 665    async fn test_enter_visual_mode(cx: &mut gpui::TestAppContext) {
 666        let mut cx = NeovimBackedTestContext::new(cx).await;
 667
 668        cx.set_shared_state(indoc! {
 669            "The ˇquick brown
 670            fox jumps over
 671            the lazy dog"
 672        })
 673        .await;
 674        let cursor = cx.update_editor(|editor, cx| editor.pixel_position_of_cursor(cx));
 675
 676        // entering visual mode should select the character
 677        // under cursor
 678        cx.simulate_shared_keystrokes("v").await;
 679        cx.shared_state()
 680            .await
 681            .assert_eq(indoc! { "The «qˇ»uick brown
 682            fox jumps over
 683            the lazy dog"});
 684        cx.update_editor(|editor, cx| assert_eq!(cursor, editor.pixel_position_of_cursor(cx)));
 685
 686        // forwards motions should extend the selection
 687        cx.simulate_shared_keystrokes("w j").await;
 688        cx.shared_state().await.assert_eq(indoc! { "The «quick brown
 689            fox jumps oˇ»ver
 690            the lazy dog"});
 691
 692        cx.simulate_shared_keystrokes("escape").await;
 693        cx.shared_state().await.assert_eq(indoc! { "The quick brown
 694            fox jumps ˇover
 695            the lazy dog"});
 696
 697        // motions work backwards
 698        cx.simulate_shared_keystrokes("v k b").await;
 699        cx.shared_state()
 700            .await
 701            .assert_eq(indoc! { "The «ˇquick brown
 702            fox jumps o»ver
 703            the lazy dog"});
 704
 705        // works on empty lines
 706        cx.set_shared_state(indoc! {"
 707            a
 708            ˇ
 709            b
 710            "})
 711            .await;
 712        let cursor = cx.update_editor(|editor, cx| editor.pixel_position_of_cursor(cx));
 713        cx.simulate_shared_keystrokes("v").await;
 714        cx.shared_state().await.assert_eq(indoc! {"
 715            a
 716            «
 717            ˇ»b
 718        "});
 719        cx.update_editor(|editor, cx| assert_eq!(cursor, editor.pixel_position_of_cursor(cx)));
 720
 721        // toggles off again
 722        cx.simulate_shared_keystrokes("v").await;
 723        cx.shared_state().await.assert_eq(indoc! {"
 724            a
 725            ˇ
 726            b
 727            "});
 728
 729        // works at the end of a document
 730        cx.set_shared_state(indoc! {"
 731            a
 732            b
 733            ˇ"})
 734            .await;
 735
 736        cx.simulate_shared_keystrokes("v").await;
 737        cx.shared_state().await.assert_eq(indoc! {"
 738            a
 739            b
 740            ˇ"});
 741    }
 742
 743    #[gpui::test]
 744    async fn test_visual_insert_first_non_whitespace(cx: &mut gpui::TestAppContext) {
 745        let mut cx = VimTestContext::new(cx, true).await;
 746
 747        cx.set_state(
 748            indoc! {
 749                "«The quick brown
 750                fox jumps over
 751                the lazy dogˇ»"
 752            },
 753            Mode::Visual,
 754        );
 755        cx.simulate_keystrokes("g shift-i");
 756        cx.assert_state(
 757            indoc! {
 758                "ˇThe quick brown
 759                ˇfox jumps over
 760                ˇthe lazy dog"
 761            },
 762            Mode::Insert,
 763        );
 764    }
 765
 766    #[gpui::test]
 767    async fn test_visual_insert_end_of_line(cx: &mut gpui::TestAppContext) {
 768        let mut cx = VimTestContext::new(cx, true).await;
 769
 770        cx.set_state(
 771            indoc! {
 772                "«The quick brown
 773                fox jumps over
 774                the lazy dogˇ»"
 775            },
 776            Mode::Visual,
 777        );
 778        cx.simulate_keystrokes("g shift-a");
 779        cx.assert_state(
 780            indoc! {
 781                "The quick brownˇ
 782                fox jumps overˇ
 783                the lazy dogˇ"
 784            },
 785            Mode::Insert,
 786        );
 787    }
 788
 789    #[gpui::test]
 790    async fn test_enter_visual_line_mode(cx: &mut gpui::TestAppContext) {
 791        let mut cx = NeovimBackedTestContext::new(cx).await;
 792
 793        cx.set_shared_state(indoc! {
 794            "The ˇquick brown
 795            fox jumps over
 796            the lazy dog"
 797        })
 798        .await;
 799        cx.simulate_shared_keystrokes("shift-v").await;
 800        cx.shared_state()
 801            .await
 802            .assert_eq(indoc! { "The «qˇ»uick brown
 803            fox jumps over
 804            the lazy dog"});
 805        cx.simulate_shared_keystrokes("x").await;
 806        cx.shared_state().await.assert_eq(indoc! { "fox ˇjumps over
 807        the lazy dog"});
 808
 809        // it should work on empty lines
 810        cx.set_shared_state(indoc! {"
 811            a
 812            ˇ
 813            b"})
 814            .await;
 815        cx.simulate_shared_keystrokes("shift-v").await;
 816        cx.shared_state().await.assert_eq(indoc! {"
 817            a
 818            «
 819            ˇ»b"});
 820        cx.simulate_shared_keystrokes("x").await;
 821        cx.shared_state().await.assert_eq(indoc! {"
 822            a
 823            ˇb"});
 824
 825        // it should work at the end of the document
 826        cx.set_shared_state(indoc! {"
 827            a
 828            b
 829            ˇ"})
 830            .await;
 831        let cursor = cx.update_editor(|editor, cx| editor.pixel_position_of_cursor(cx));
 832        cx.simulate_shared_keystrokes("shift-v").await;
 833        cx.shared_state().await.assert_eq(indoc! {"
 834            a
 835            b
 836            ˇ"});
 837        cx.update_editor(|editor, cx| assert_eq!(cursor, editor.pixel_position_of_cursor(cx)));
 838        cx.simulate_shared_keystrokes("x").await;
 839        cx.shared_state().await.assert_eq(indoc! {"
 840            a
 841            ˇb"});
 842    }
 843
 844    #[gpui::test]
 845    async fn test_visual_delete(cx: &mut gpui::TestAppContext) {
 846        let mut cx = NeovimBackedTestContext::new(cx).await;
 847
 848        cx.simulate("v w", "The quick ˇbrown")
 849            .await
 850            .assert_matches();
 851
 852        cx.simulate("v w x", "The quick ˇbrown")
 853            .await
 854            .assert_matches();
 855        cx.simulate(
 856            "v w j x",
 857            indoc! {"
 858                The ˇquick brown
 859                fox jumps over
 860                the lazy dog"},
 861        )
 862        .await
 863        .assert_matches();
 864        // Test pasting code copied on delete
 865        cx.simulate_shared_keystrokes("j p").await;
 866        cx.shared_state().await.assert_matches();
 867
 868        cx.simulate_at_each_offset(
 869            "v w j x",
 870            indoc! {"
 871                The ˇquick brown
 872                fox jumps over
 873                the ˇlazy dog"},
 874        )
 875        .await
 876        .assert_matches();
 877        cx.simulate_at_each_offset(
 878            "v b k x",
 879            indoc! {"
 880                The ˇquick brown
 881                fox jumps ˇover
 882                the ˇlazy dog"},
 883        )
 884        .await
 885        .assert_matches();
 886    }
 887
 888    #[gpui::test]
 889    async fn test_visual_line_delete(cx: &mut gpui::TestAppContext) {
 890        let mut cx = NeovimBackedTestContext::new(cx).await;
 891
 892        cx.set_shared_state(indoc! {"
 893                The quˇick brown
 894                fox jumps over
 895                the lazy dog"})
 896            .await;
 897        cx.simulate_shared_keystrokes("shift-v x").await;
 898        cx.shared_state().await.assert_matches();
 899
 900        // Test pasting code copied on delete
 901        cx.simulate_shared_keystrokes("p").await;
 902        cx.shared_state().await.assert_matches();
 903
 904        cx.set_shared_state(indoc! {"
 905                The quick brown
 906                fox jumps over
 907                the laˇzy dog"})
 908            .await;
 909        cx.simulate_shared_keystrokes("shift-v x").await;
 910        cx.shared_state().await.assert_matches();
 911        cx.shared_clipboard().await.assert_eq("the lazy dog\n");
 912
 913        cx.set_shared_state(indoc! {"
 914                                The quˇick brown
 915                                fox jumps over
 916                                the lazy dog"})
 917            .await;
 918        cx.simulate_shared_keystrokes("shift-v j x").await;
 919        cx.shared_state().await.assert_matches();
 920        // Test pasting code copied on delete
 921        cx.simulate_shared_keystrokes("p").await;
 922        cx.shared_state().await.assert_matches();
 923
 924        cx.set_shared_state(indoc! {"
 925            The ˇlong line
 926            should not
 927            crash
 928            "})
 929            .await;
 930        cx.simulate_shared_keystrokes("shift-v $ x").await;
 931        cx.shared_state().await.assert_matches();
 932    }
 933
 934    #[gpui::test]
 935    async fn test_visual_yank(cx: &mut gpui::TestAppContext) {
 936        let mut cx = NeovimBackedTestContext::new(cx).await;
 937
 938        cx.set_shared_state("The quick ˇbrown").await;
 939        cx.simulate_shared_keystrokes("v w y").await;
 940        cx.shared_state().await.assert_eq("The quick ˇbrown");
 941        cx.shared_clipboard().await.assert_eq("brown");
 942
 943        cx.set_shared_state(indoc! {"
 944                The ˇquick brown
 945                fox jumps over
 946                the lazy dog"})
 947            .await;
 948        cx.simulate_shared_keystrokes("v w j y").await;
 949        cx.shared_state().await.assert_eq(indoc! {"
 950                    The ˇquick brown
 951                    fox jumps over
 952                    the lazy dog"});
 953        cx.shared_clipboard().await.assert_eq(indoc! {"
 954                quick brown
 955                fox jumps o"});
 956
 957        cx.set_shared_state(indoc! {"
 958                    The quick brown
 959                    fox jumps over
 960                    the ˇlazy dog"})
 961            .await;
 962        cx.simulate_shared_keystrokes("v w j y").await;
 963        cx.shared_state().await.assert_eq(indoc! {"
 964                    The quick brown
 965                    fox jumps over
 966                    the ˇlazy dog"});
 967        cx.shared_clipboard().await.assert_eq("lazy d");
 968        cx.simulate_shared_keystrokes("shift-v y").await;
 969        cx.shared_clipboard().await.assert_eq("the lazy dog\n");
 970
 971        cx.set_shared_state(indoc! {"
 972                    The ˇquick brown
 973                    fox jumps over
 974                    the lazy dog"})
 975            .await;
 976        cx.simulate_shared_keystrokes("v b k y").await;
 977        cx.shared_state().await.assert_eq(indoc! {"
 978                    ˇThe quick brown
 979                    fox jumps over
 980                    the lazy dog"});
 981        assert_eq!(
 982            cx.read_from_clipboard()
 983                .map(|item| item.text().unwrap().to_string())
 984                .unwrap(),
 985            "The q"
 986        );
 987
 988        cx.set_shared_state(indoc! {"
 989                    The quick brown
 990                    fox ˇjumps over
 991                    the lazy dog"})
 992            .await;
 993        cx.simulate_shared_keystrokes("shift-v shift-g shift-y")
 994            .await;
 995        cx.shared_state().await.assert_eq(indoc! {"
 996                    The quick brown
 997                    ˇfox jumps over
 998                    the lazy dog"});
 999        cx.shared_clipboard()
1000            .await
1001            .assert_eq("fox jumps over\nthe lazy dog\n");
1002    }
1003
1004    #[gpui::test]
1005    async fn test_visual_block_mode(cx: &mut gpui::TestAppContext) {
1006        let mut cx = NeovimBackedTestContext::new(cx).await;
1007
1008        cx.set_shared_state(indoc! {
1009            "The ˇquick brown
1010             fox jumps over
1011             the lazy dog"
1012        })
1013        .await;
1014        cx.simulate_shared_keystrokes("ctrl-v").await;
1015        cx.shared_state().await.assert_eq(indoc! {
1016            "The «qˇ»uick brown
1017            fox jumps over
1018            the lazy dog"
1019        });
1020        cx.simulate_shared_keystrokes("2 down").await;
1021        cx.shared_state().await.assert_eq(indoc! {
1022            "The «qˇ»uick brown
1023            fox «jˇ»umps over
1024            the «lˇ»azy dog"
1025        });
1026        cx.simulate_shared_keystrokes("e").await;
1027        cx.shared_state().await.assert_eq(indoc! {
1028            "The «quicˇ»k brown
1029            fox «jumpˇ»s over
1030            the «lazyˇ» dog"
1031        });
1032        cx.simulate_shared_keystrokes("^").await;
1033        cx.shared_state().await.assert_eq(indoc! {
1034            "«ˇThe q»uick brown
1035            «ˇfox j»umps over
1036            «ˇthe l»azy dog"
1037        });
1038        cx.simulate_shared_keystrokes("$").await;
1039        cx.shared_state().await.assert_eq(indoc! {
1040            "The «quick brownˇ»
1041            fox «jumps overˇ»
1042            the «lazy dogˇ»"
1043        });
1044        cx.simulate_shared_keystrokes("shift-f space").await;
1045        cx.shared_state().await.assert_eq(indoc! {
1046            "The «quickˇ» brown
1047            fox «jumpsˇ» over
1048            the «lazy ˇ»dog"
1049        });
1050
1051        // toggling through visual mode works as expected
1052        cx.simulate_shared_keystrokes("v").await;
1053        cx.shared_state().await.assert_eq(indoc! {
1054            "The «quick brown
1055            fox jumps over
1056            the lazy ˇ»dog"
1057        });
1058        cx.simulate_shared_keystrokes("ctrl-v").await;
1059        cx.shared_state().await.assert_eq(indoc! {
1060            "The «quickˇ» brown
1061            fox «jumpsˇ» over
1062            the «lazy ˇ»dog"
1063        });
1064
1065        cx.set_shared_state(indoc! {
1066            "The ˇquick
1067             brown
1068             fox
1069             jumps over the
1070
1071             lazy dog
1072            "
1073        })
1074        .await;
1075        cx.simulate_shared_keystrokes("ctrl-v down down").await;
1076        cx.shared_state().await.assert_eq(indoc! {
1077            "The«ˇ q»uick
1078            bro«ˇwn»
1079            foxˇ
1080            jumps over the
1081
1082            lazy dog
1083            "
1084        });
1085        cx.simulate_shared_keystrokes("down").await;
1086        cx.shared_state().await.assert_eq(indoc! {
1087            "The «qˇ»uick
1088            brow«nˇ»
1089            fox
1090            jump«sˇ» over the
1091
1092            lazy dog
1093            "
1094        });
1095        cx.simulate_shared_keystrokes("left").await;
1096        cx.shared_state().await.assert_eq(indoc! {
1097            "The«ˇ q»uick
1098            bro«ˇwn»
1099            foxˇ
1100            jum«ˇps» over the
1101
1102            lazy dog
1103            "
1104        });
1105        cx.simulate_shared_keystrokes("s o escape").await;
1106        cx.shared_state().await.assert_eq(indoc! {
1107            "Theˇouick
1108            broo
1109            foxo
1110            jumo over the
1111
1112            lazy dog
1113            "
1114        });
1115
1116        // https://github.com/zed-industries/zed/issues/6274
1117        cx.set_shared_state(indoc! {
1118            "Theˇ quick brown
1119
1120            fox jumps over
1121            the lazy dog
1122            "
1123        })
1124        .await;
1125        cx.simulate_shared_keystrokes("l ctrl-v j j").await;
1126        cx.shared_state().await.assert_eq(indoc! {
1127            "The «qˇ»uick brown
1128
1129            fox «jˇ»umps over
1130            the lazy dog
1131            "
1132        });
1133    }
1134
1135    #[gpui::test]
1136    async fn test_visual_block_issue_2123(cx: &mut gpui::TestAppContext) {
1137        let mut cx = NeovimBackedTestContext::new(cx).await;
1138
1139        cx.set_shared_state(indoc! {
1140            "The ˇquick brown
1141            fox jumps over
1142            the lazy dog
1143            "
1144        })
1145        .await;
1146        cx.simulate_shared_keystrokes("ctrl-v right down").await;
1147        cx.shared_state().await.assert_eq(indoc! {
1148            "The «quˇ»ick brown
1149            fox «juˇ»mps over
1150            the lazy dog
1151            "
1152        });
1153    }
1154
1155    #[gpui::test]
1156    async fn test_visual_block_insert(cx: &mut gpui::TestAppContext) {
1157        let mut cx = NeovimBackedTestContext::new(cx).await;
1158
1159        cx.set_shared_state(indoc! {
1160            "ˇThe quick brown
1161            fox jumps over
1162            the lazy dog
1163            "
1164        })
1165        .await;
1166        cx.simulate_shared_keystrokes("ctrl-v 9 down").await;
1167        cx.shared_state().await.assert_eq(indoc! {
1168            "«Tˇ»he quick brown
1169            «fˇ»ox jumps over
1170            «tˇ»he lazy dog
1171            ˇ"
1172        });
1173
1174        cx.simulate_shared_keystrokes("shift-i k escape").await;
1175        cx.shared_state().await.assert_eq(indoc! {
1176            "ˇkThe quick brown
1177            kfox jumps over
1178            kthe lazy dog
1179            k"
1180        });
1181
1182        cx.set_shared_state(indoc! {
1183            "ˇThe quick brown
1184            fox jumps over
1185            the lazy dog
1186            "
1187        })
1188        .await;
1189        cx.simulate_shared_keystrokes("ctrl-v 9 down").await;
1190        cx.shared_state().await.assert_eq(indoc! {
1191            "«Tˇ»he quick brown
1192            «fˇ»ox jumps over
1193            «tˇ»he lazy dog
1194            ˇ"
1195        });
1196        cx.simulate_shared_keystrokes("c k escape").await;
1197        cx.shared_state().await.assert_eq(indoc! {
1198            "ˇkhe quick brown
1199            kox jumps over
1200            khe lazy dog
1201            k"
1202        });
1203    }
1204
1205    #[gpui::test]
1206    async fn test_visual_object(cx: &mut gpui::TestAppContext) {
1207        let mut cx = NeovimBackedTestContext::new(cx).await;
1208
1209        cx.set_shared_state("hello (in [parˇens] o)").await;
1210        cx.simulate_shared_keystrokes("ctrl-v l").await;
1211        cx.simulate_shared_keystrokes("a ]").await;
1212        cx.shared_state()
1213            .await
1214            .assert_eq("hello (in «[parens]ˇ» o)");
1215        cx.simulate_shared_keystrokes("i (").await;
1216        cx.shared_state()
1217            .await
1218            .assert_eq("hello («in [parens] oˇ»)");
1219
1220        cx.set_shared_state("hello in a wˇord again.").await;
1221        cx.simulate_shared_keystrokes("ctrl-v l i w").await;
1222        cx.shared_state()
1223            .await
1224            .assert_eq("hello in a w«ordˇ» again.");
1225        assert_eq!(cx.mode(), Mode::VisualBlock);
1226        cx.simulate_shared_keystrokes("o a s").await;
1227        cx.shared_state()
1228            .await
1229            .assert_eq("«ˇhello in a word» again.");
1230    }
1231
1232    #[gpui::test]
1233    async fn test_mode_across_command(cx: &mut gpui::TestAppContext) {
1234        let mut cx = VimTestContext::new(cx, true).await;
1235
1236        cx.set_state("aˇbc", Mode::Normal);
1237        cx.simulate_keystrokes("ctrl-v");
1238        assert_eq!(cx.mode(), Mode::VisualBlock);
1239        cx.simulate_keystrokes("cmd-shift-p escape");
1240        assert_eq!(cx.mode(), Mode::VisualBlock);
1241    }
1242
1243    #[gpui::test]
1244    async fn test_gn(cx: &mut gpui::TestAppContext) {
1245        let mut cx = NeovimBackedTestContext::new(cx).await;
1246
1247        cx.set_shared_state("aaˇ aa aa aa aa").await;
1248        cx.simulate_shared_keystrokes("/ a a enter").await;
1249        cx.shared_state().await.assert_eq("aa ˇaa aa aa aa");
1250        cx.simulate_shared_keystrokes("g n").await;
1251        cx.shared_state().await.assert_eq("aa «aaˇ» aa aa aa");
1252        cx.simulate_shared_keystrokes("g n").await;
1253        cx.shared_state().await.assert_eq("aa «aa aaˇ» aa aa");
1254        cx.simulate_shared_keystrokes("escape d g n").await;
1255        cx.shared_state().await.assert_eq("aa aa ˇ aa aa");
1256
1257        cx.set_shared_state("aaˇ aa aa aa aa").await;
1258        cx.simulate_shared_keystrokes("/ a a enter").await;
1259        cx.shared_state().await.assert_eq("aa ˇaa aa aa aa");
1260        cx.simulate_shared_keystrokes("3 g n").await;
1261        cx.shared_state().await.assert_eq("aa aa aa «aaˇ» aa");
1262
1263        cx.set_shared_state("aaˇ aa aa aa aa").await;
1264        cx.simulate_shared_keystrokes("/ a a enter").await;
1265        cx.shared_state().await.assert_eq("aa ˇaa aa aa aa");
1266        cx.simulate_shared_keystrokes("g shift-n").await;
1267        cx.shared_state().await.assert_eq("aa «ˇaa» aa aa aa");
1268        cx.simulate_shared_keystrokes("g shift-n").await;
1269        cx.shared_state().await.assert_eq("«ˇaa aa» aa aa aa");
1270    }
1271
1272    #[gpui::test]
1273    async fn test_gl(cx: &mut gpui::TestAppContext) {
1274        let mut cx = VimTestContext::new(cx, true).await;
1275
1276        cx.set_state("aaˇ aa\naa", Mode::Normal);
1277        cx.simulate_keystrokes("g l");
1278        cx.assert_state("«aaˇ» «aaˇ»\naa", Mode::Visual);
1279        cx.simulate_keystrokes("g >");
1280        cx.assert_state("«aaˇ» aa\n«aaˇ»", Mode::Visual);
1281    }
1282
1283    #[gpui::test]
1284    async fn test_dgn_repeat(cx: &mut gpui::TestAppContext) {
1285        let mut cx = NeovimBackedTestContext::new(cx).await;
1286
1287        cx.set_shared_state("aaˇ aa aa aa aa").await;
1288        cx.simulate_shared_keystrokes("/ a a enter").await;
1289        cx.shared_state().await.assert_eq("aa ˇaa aa aa aa");
1290        cx.simulate_shared_keystrokes("d g n").await;
1291
1292        cx.shared_state().await.assert_eq("aa ˇ aa aa aa");
1293        cx.simulate_shared_keystrokes(".").await;
1294        cx.shared_state().await.assert_eq("aa  ˇ aa aa");
1295        cx.simulate_shared_keystrokes(".").await;
1296        cx.shared_state().await.assert_eq("aa   ˇ aa");
1297    }
1298
1299    #[gpui::test]
1300    async fn test_cgn_repeat(cx: &mut gpui::TestAppContext) {
1301        let mut cx = NeovimBackedTestContext::new(cx).await;
1302
1303        cx.set_shared_state("aaˇ aa aa aa aa").await;
1304        cx.simulate_shared_keystrokes("/ a a enter").await;
1305        cx.shared_state().await.assert_eq("aa ˇaa aa aa aa");
1306        cx.simulate_shared_keystrokes("c g n x escape").await;
1307        cx.shared_state().await.assert_eq("aa ˇx aa aa aa");
1308        cx.simulate_shared_keystrokes(".").await;
1309        cx.shared_state().await.assert_eq("aa x ˇx aa aa");
1310    }
1311
1312    #[gpui::test]
1313    async fn test_cgn_nomatch(cx: &mut gpui::TestAppContext) {
1314        let mut cx = NeovimBackedTestContext::new(cx).await;
1315
1316        cx.set_shared_state("aaˇ aa aa aa aa").await;
1317        cx.simulate_shared_keystrokes("/ b b enter").await;
1318        cx.shared_state().await.assert_eq("aaˇ aa aa aa aa");
1319        cx.simulate_shared_keystrokes("c g n x escape").await;
1320        cx.shared_state().await.assert_eq("aaˇaa aa aa aa");
1321        cx.simulate_shared_keystrokes(".").await;
1322        cx.shared_state().await.assert_eq("aaˇa aa aa aa");
1323
1324        cx.set_shared_state("aaˇ bb aa aa aa").await;
1325        cx.simulate_shared_keystrokes("/ b b enter").await;
1326        cx.shared_state().await.assert_eq("aa ˇbb aa aa aa");
1327        cx.simulate_shared_keystrokes("c g n x escape").await;
1328        cx.shared_state().await.assert_eq("aa ˇx aa aa aa");
1329        cx.simulate_shared_keystrokes(".").await;
1330        cx.shared_state().await.assert_eq("aa ˇx aa aa aa");
1331    }
1332
1333    #[gpui::test]
1334    async fn test_visual_shift_d(cx: &mut gpui::TestAppContext) {
1335        let mut cx = NeovimBackedTestContext::new(cx).await;
1336
1337        cx.set_shared_state(indoc! {
1338            "The ˇquick brown
1339            fox jumps over
1340            the lazy dog
1341            "
1342        })
1343        .await;
1344        cx.simulate_shared_keystrokes("v down shift-d").await;
1345        cx.shared_state().await.assert_eq(indoc! {
1346            "the ˇlazy dog\n"
1347        });
1348
1349        cx.set_shared_state(indoc! {
1350            "The ˇquick brown
1351            fox jumps over
1352            the lazy dog
1353            "
1354        })
1355        .await;
1356        cx.simulate_shared_keystrokes("ctrl-v down shift-d").await;
1357        cx.shared_state().await.assert_eq(indoc! {
1358            "Theˇ•
1359            fox•
1360            the lazy dog
1361            "
1362        });
1363    }
1364
1365    #[gpui::test]
1366    async fn test_gv(cx: &mut gpui::TestAppContext) {
1367        let mut cx = NeovimBackedTestContext::new(cx).await;
1368
1369        cx.set_shared_state(indoc! {
1370            "The ˇquick brown"
1371        })
1372        .await;
1373        cx.simulate_shared_keystrokes("v i w escape g v").await;
1374        cx.shared_state().await.assert_eq(indoc! {
1375            "The «quickˇ» brown"
1376        });
1377
1378        cx.simulate_shared_keystrokes("o escape g v").await;
1379        cx.shared_state().await.assert_eq(indoc! {
1380            "The «ˇquick» brown"
1381        });
1382
1383        cx.simulate_shared_keystrokes("escape ^ ctrl-v l").await;
1384        cx.shared_state().await.assert_eq(indoc! {
1385            "«Thˇ»e quick brown"
1386        });
1387        cx.simulate_shared_keystrokes("g v").await;
1388        cx.shared_state().await.assert_eq(indoc! {
1389            "The «ˇquick» brown"
1390        });
1391        cx.simulate_shared_keystrokes("g v").await;
1392        cx.shared_state().await.assert_eq(indoc! {
1393            "«Thˇ»e quick brown"
1394        });
1395
1396        cx.set_state(
1397            indoc! {"
1398            fiˇsh one
1399            fish two
1400            fish red
1401            fish blue
1402        "},
1403            Mode::Normal,
1404        );
1405        cx.simulate_keystrokes("4 g l escape escape g v");
1406        cx.assert_state(
1407            indoc! {"
1408                «fishˇ» one
1409                «fishˇ» two
1410                «fishˇ» red
1411                «fishˇ» blue
1412            "},
1413            Mode::Visual,
1414        );
1415        cx.simulate_keystrokes("y g v");
1416        cx.assert_state(
1417            indoc! {"
1418                «fishˇ» one
1419                «fishˇ» two
1420                «fishˇ» red
1421                «fishˇ» blue
1422            "},
1423            Mode::Visual,
1424        );
1425    }
1426}