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.edit(edits, cx);
 534                editor.change_selections(None, cx, |s| s.select_ranges(stable_anchors));
 535            });
 536        });
 537        self.switch_mode(Mode::Normal, false, cx);
 538    }
 539
 540    pub fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
 541        let count = self
 542            .take_count(cx)
 543            .unwrap_or_else(|| if self.mode.is_visual() { 1 } else { 2 });
 544        self.update_editor(cx, |_, editor, cx| {
 545            editor.set_clip_at_line_ends(false, cx);
 546            for _ in 0..count {
 547                if editor
 548                    .select_next(&Default::default(), cx)
 549                    .log_err()
 550                    .is_none()
 551                {
 552                    break;
 553                }
 554            }
 555        });
 556    }
 557
 558    pub fn select_previous(&mut self, _: &SelectPrevious, cx: &mut ViewContext<Self>) {
 559        let count = self
 560            .take_count(cx)
 561            .unwrap_or_else(|| if self.mode.is_visual() { 1 } else { 2 });
 562        self.update_editor(cx, |_, editor, cx| {
 563            for _ in 0..count {
 564                if editor
 565                    .select_previous(&Default::default(), cx)
 566                    .log_err()
 567                    .is_none()
 568                {
 569                    break;
 570                }
 571            }
 572        });
 573    }
 574
 575    pub fn select_match(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
 576        let count = self.take_count(cx).unwrap_or(1);
 577        let Some(pane) = self.pane(cx) else {
 578            return;
 579        };
 580        let vim_is_normal = self.mode == Mode::Normal;
 581        let mut start_selection = 0usize;
 582        let mut end_selection = 0usize;
 583
 584        self.update_editor(cx, |_, editor, _| {
 585            editor.set_collapse_matches(false);
 586        });
 587        if vim_is_normal {
 588            pane.update(cx, |pane, cx| {
 589                if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>()
 590                {
 591                    search_bar.update(cx, |search_bar, cx| {
 592                        if !search_bar.has_active_match() || !search_bar.show(cx) {
 593                            return;
 594                        }
 595                        // without update_match_index there is a bug when the cursor is before the first match
 596                        search_bar.update_match_index(cx);
 597                        search_bar.select_match(direction.opposite(), 1, cx);
 598                    });
 599                }
 600            });
 601        }
 602        self.update_editor(cx, |_, editor, cx| {
 603            let latest = editor.selections.newest::<usize>(cx);
 604            start_selection = latest.start;
 605            end_selection = latest.end;
 606        });
 607
 608        let mut match_exists = false;
 609        pane.update(cx, |pane, cx| {
 610            if let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() {
 611                search_bar.update(cx, |search_bar, cx| {
 612                    search_bar.update_match_index(cx);
 613                    search_bar.select_match(direction, count, cx);
 614                    match_exists = search_bar.match_exists(cx);
 615                });
 616            }
 617        });
 618        if !match_exists {
 619            self.clear_operator(cx);
 620            self.stop_replaying(cx);
 621            return;
 622        }
 623        self.update_editor(cx, |_, editor, cx| {
 624            let latest = editor.selections.newest::<usize>(cx);
 625            if vim_is_normal {
 626                start_selection = latest.start;
 627                end_selection = latest.end;
 628            } else {
 629                start_selection = start_selection.min(latest.start);
 630                end_selection = end_selection.max(latest.end);
 631            }
 632            if direction == Direction::Prev {
 633                std::mem::swap(&mut start_selection, &mut end_selection);
 634            }
 635            editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 636                s.select_ranges([start_selection..end_selection]);
 637            });
 638            editor.set_collapse_matches(true);
 639        });
 640
 641        match self.maybe_pop_operator() {
 642            Some(Operator::Change) => self.substitute(None, false, cx),
 643            Some(Operator::Delete) => {
 644                self.stop_recording(cx);
 645                self.visual_delete(false, cx)
 646            }
 647            Some(Operator::Yank) => self.visual_yank(cx),
 648            _ => {} // Ignoring other operators
 649        }
 650    }
 651}
 652#[cfg(test)]
 653mod test {
 654    use indoc::indoc;
 655    use workspace::item::Item;
 656
 657    use crate::{
 658        state::Mode,
 659        test::{NeovimBackedTestContext, VimTestContext},
 660    };
 661
 662    #[gpui::test]
 663    async fn test_enter_visual_mode(cx: &mut gpui::TestAppContext) {
 664        let mut cx = NeovimBackedTestContext::new(cx).await;
 665
 666        cx.set_shared_state(indoc! {
 667            "The ˇquick brown
 668            fox jumps over
 669            the lazy dog"
 670        })
 671        .await;
 672        let cursor = cx.update_editor(|editor, cx| editor.pixel_position_of_cursor(cx));
 673
 674        // entering visual mode should select the character
 675        // under cursor
 676        cx.simulate_shared_keystrokes("v").await;
 677        cx.shared_state()
 678            .await
 679            .assert_eq(indoc! { "The «qˇ»uick brown
 680            fox jumps over
 681            the lazy dog"});
 682        cx.update_editor(|editor, cx| assert_eq!(cursor, editor.pixel_position_of_cursor(cx)));
 683
 684        // forwards motions should extend the selection
 685        cx.simulate_shared_keystrokes("w j").await;
 686        cx.shared_state().await.assert_eq(indoc! { "The «quick brown
 687            fox jumps oˇ»ver
 688            the lazy dog"});
 689
 690        cx.simulate_shared_keystrokes("escape").await;
 691        cx.shared_state().await.assert_eq(indoc! { "The quick brown
 692            fox jumps ˇover
 693            the lazy dog"});
 694
 695        // motions work backwards
 696        cx.simulate_shared_keystrokes("v k b").await;
 697        cx.shared_state()
 698            .await
 699            .assert_eq(indoc! { "The «ˇquick brown
 700            fox jumps o»ver
 701            the lazy dog"});
 702
 703        // works on empty lines
 704        cx.set_shared_state(indoc! {"
 705            a
 706            ˇ
 707            b
 708            "})
 709            .await;
 710        let cursor = cx.update_editor(|editor, cx| editor.pixel_position_of_cursor(cx));
 711        cx.simulate_shared_keystrokes("v").await;
 712        cx.shared_state().await.assert_eq(indoc! {"
 713            a
 714            «
 715            ˇ»b
 716        "});
 717        cx.update_editor(|editor, cx| assert_eq!(cursor, editor.pixel_position_of_cursor(cx)));
 718
 719        // toggles off again
 720        cx.simulate_shared_keystrokes("v").await;
 721        cx.shared_state().await.assert_eq(indoc! {"
 722            a
 723            ˇ
 724            b
 725            "});
 726
 727        // works at the end of a document
 728        cx.set_shared_state(indoc! {"
 729            a
 730            b
 731            ˇ"})
 732            .await;
 733
 734        cx.simulate_shared_keystrokes("v").await;
 735        cx.shared_state().await.assert_eq(indoc! {"
 736            a
 737            b
 738            ˇ"});
 739    }
 740
 741    #[gpui::test]
 742    async fn test_visual_insert_first_non_whitespace(cx: &mut gpui::TestAppContext) {
 743        let mut cx = VimTestContext::new(cx, true).await;
 744
 745        cx.set_state(
 746            indoc! {
 747                "«The quick brown
 748                fox jumps over
 749                the lazy dogˇ»"
 750            },
 751            Mode::Visual,
 752        );
 753        cx.simulate_keystrokes("g shift-i");
 754        cx.assert_state(
 755            indoc! {
 756                "ˇThe quick brown
 757                ˇfox jumps over
 758                ˇthe lazy dog"
 759            },
 760            Mode::Insert,
 761        );
 762    }
 763
 764    #[gpui::test]
 765    async fn test_visual_insert_end_of_line(cx: &mut gpui::TestAppContext) {
 766        let mut cx = VimTestContext::new(cx, true).await;
 767
 768        cx.set_state(
 769            indoc! {
 770                "«The quick brown
 771                fox jumps over
 772                the lazy dogˇ»"
 773            },
 774            Mode::Visual,
 775        );
 776        cx.simulate_keystrokes("g shift-a");
 777        cx.assert_state(
 778            indoc! {
 779                "The quick brownˇ
 780                fox jumps overˇ
 781                the lazy dogˇ"
 782            },
 783            Mode::Insert,
 784        );
 785    }
 786
 787    #[gpui::test]
 788    async fn test_enter_visual_line_mode(cx: &mut gpui::TestAppContext) {
 789        let mut cx = NeovimBackedTestContext::new(cx).await;
 790
 791        cx.set_shared_state(indoc! {
 792            "The ˇquick brown
 793            fox jumps over
 794            the lazy dog"
 795        })
 796        .await;
 797        cx.simulate_shared_keystrokes("shift-v").await;
 798        cx.shared_state()
 799            .await
 800            .assert_eq(indoc! { "The «qˇ»uick brown
 801            fox jumps over
 802            the lazy dog"});
 803        cx.simulate_shared_keystrokes("x").await;
 804        cx.shared_state().await.assert_eq(indoc! { "fox ˇjumps over
 805        the lazy dog"});
 806
 807        // it should work on empty lines
 808        cx.set_shared_state(indoc! {"
 809            a
 810            ˇ
 811            b"})
 812            .await;
 813        cx.simulate_shared_keystrokes("shift-v").await;
 814        cx.shared_state().await.assert_eq(indoc! {"
 815            a
 816            «
 817            ˇ»b"});
 818        cx.simulate_shared_keystrokes("x").await;
 819        cx.shared_state().await.assert_eq(indoc! {"
 820            a
 821            ˇb"});
 822
 823        // it should work at the end of the document
 824        cx.set_shared_state(indoc! {"
 825            a
 826            b
 827            ˇ"})
 828            .await;
 829        let cursor = cx.update_editor(|editor, cx| editor.pixel_position_of_cursor(cx));
 830        cx.simulate_shared_keystrokes("shift-v").await;
 831        cx.shared_state().await.assert_eq(indoc! {"
 832            a
 833            b
 834            ˇ"});
 835        cx.update_editor(|editor, cx| assert_eq!(cursor, editor.pixel_position_of_cursor(cx)));
 836        cx.simulate_shared_keystrokes("x").await;
 837        cx.shared_state().await.assert_eq(indoc! {"
 838            a
 839            ˇb"});
 840    }
 841
 842    #[gpui::test]
 843    async fn test_visual_delete(cx: &mut gpui::TestAppContext) {
 844        let mut cx = NeovimBackedTestContext::new(cx).await;
 845
 846        cx.simulate("v w", "The quick ˇbrown")
 847            .await
 848            .assert_matches();
 849
 850        cx.simulate("v w x", "The quick ˇbrown")
 851            .await
 852            .assert_matches();
 853        cx.simulate(
 854            "v w j x",
 855            indoc! {"
 856                The ˇquick brown
 857                fox jumps over
 858                the lazy dog"},
 859        )
 860        .await
 861        .assert_matches();
 862        // Test pasting code copied on delete
 863        cx.simulate_shared_keystrokes("j p").await;
 864        cx.shared_state().await.assert_matches();
 865
 866        cx.simulate_at_each_offset(
 867            "v w j x",
 868            indoc! {"
 869                The ˇquick brown
 870                fox jumps over
 871                the ˇlazy dog"},
 872        )
 873        .await
 874        .assert_matches();
 875        cx.simulate_at_each_offset(
 876            "v b k x",
 877            indoc! {"
 878                The ˇquick brown
 879                fox jumps ˇover
 880                the ˇlazy dog"},
 881        )
 882        .await
 883        .assert_matches();
 884    }
 885
 886    #[gpui::test]
 887    async fn test_visual_line_delete(cx: &mut gpui::TestAppContext) {
 888        let mut cx = NeovimBackedTestContext::new(cx).await;
 889
 890        cx.set_shared_state(indoc! {"
 891                The quˇick brown
 892                fox jumps over
 893                the lazy dog"})
 894            .await;
 895        cx.simulate_shared_keystrokes("shift-v x").await;
 896        cx.shared_state().await.assert_matches();
 897
 898        // Test pasting code copied on delete
 899        cx.simulate_shared_keystrokes("p").await;
 900        cx.shared_state().await.assert_matches();
 901
 902        cx.set_shared_state(indoc! {"
 903                The quick brown
 904                fox jumps over
 905                the laˇzy dog"})
 906            .await;
 907        cx.simulate_shared_keystrokes("shift-v x").await;
 908        cx.shared_state().await.assert_matches();
 909        cx.shared_clipboard().await.assert_eq("the lazy dog\n");
 910
 911        cx.set_shared_state(indoc! {"
 912                                The quˇick brown
 913                                fox jumps over
 914                                the lazy dog"})
 915            .await;
 916        cx.simulate_shared_keystrokes("shift-v j x").await;
 917        cx.shared_state().await.assert_matches();
 918        // Test pasting code copied on delete
 919        cx.simulate_shared_keystrokes("p").await;
 920        cx.shared_state().await.assert_matches();
 921
 922        cx.set_shared_state(indoc! {"
 923            The ˇlong line
 924            should not
 925            crash
 926            "})
 927            .await;
 928        cx.simulate_shared_keystrokes("shift-v $ x").await;
 929        cx.shared_state().await.assert_matches();
 930    }
 931
 932    #[gpui::test]
 933    async fn test_visual_yank(cx: &mut gpui::TestAppContext) {
 934        let mut cx = NeovimBackedTestContext::new(cx).await;
 935
 936        cx.set_shared_state("The quick ˇbrown").await;
 937        cx.simulate_shared_keystrokes("v w y").await;
 938        cx.shared_state().await.assert_eq("The quick ˇbrown");
 939        cx.shared_clipboard().await.assert_eq("brown");
 940
 941        cx.set_shared_state(indoc! {"
 942                The ˇquick brown
 943                fox jumps over
 944                the lazy dog"})
 945            .await;
 946        cx.simulate_shared_keystrokes("v w j y").await;
 947        cx.shared_state().await.assert_eq(indoc! {"
 948                    The ˇquick brown
 949                    fox jumps over
 950                    the lazy dog"});
 951        cx.shared_clipboard().await.assert_eq(indoc! {"
 952                quick brown
 953                fox jumps o"});
 954
 955        cx.set_shared_state(indoc! {"
 956                    The quick brown
 957                    fox jumps over
 958                    the ˇlazy dog"})
 959            .await;
 960        cx.simulate_shared_keystrokes("v w j y").await;
 961        cx.shared_state().await.assert_eq(indoc! {"
 962                    The quick brown
 963                    fox jumps over
 964                    the ˇlazy dog"});
 965        cx.shared_clipboard().await.assert_eq("lazy d");
 966        cx.simulate_shared_keystrokes("shift-v y").await;
 967        cx.shared_clipboard().await.assert_eq("the lazy dog\n");
 968
 969        cx.set_shared_state(indoc! {"
 970                    The ˇquick brown
 971                    fox jumps over
 972                    the lazy dog"})
 973            .await;
 974        cx.simulate_shared_keystrokes("v b k y").await;
 975        cx.shared_state().await.assert_eq(indoc! {"
 976                    ˇThe quick brown
 977                    fox jumps over
 978                    the lazy dog"});
 979        assert_eq!(
 980            cx.read_from_clipboard()
 981                .map(|item| item.text().unwrap().to_string())
 982                .unwrap(),
 983            "The q"
 984        );
 985
 986        cx.set_shared_state(indoc! {"
 987                    The quick brown
 988                    fox ˇjumps over
 989                    the lazy dog"})
 990            .await;
 991        cx.simulate_shared_keystrokes("shift-v shift-g shift-y")
 992            .await;
 993        cx.shared_state().await.assert_eq(indoc! {"
 994                    The quick brown
 995                    ˇfox jumps over
 996                    the lazy dog"});
 997        cx.shared_clipboard()
 998            .await
 999            .assert_eq("fox jumps over\nthe lazy dog\n");
1000    }
1001
1002    #[gpui::test]
1003    async fn test_visual_block_mode(cx: &mut gpui::TestAppContext) {
1004        let mut cx = NeovimBackedTestContext::new(cx).await;
1005
1006        cx.set_shared_state(indoc! {
1007            "The ˇquick brown
1008             fox jumps over
1009             the lazy dog"
1010        })
1011        .await;
1012        cx.simulate_shared_keystrokes("ctrl-v").await;
1013        cx.shared_state().await.assert_eq(indoc! {
1014            "The «qˇ»uick brown
1015            fox jumps over
1016            the lazy dog"
1017        });
1018        cx.simulate_shared_keystrokes("2 down").await;
1019        cx.shared_state().await.assert_eq(indoc! {
1020            "The «qˇ»uick brown
1021            fox «jˇ»umps over
1022            the «lˇ»azy dog"
1023        });
1024        cx.simulate_shared_keystrokes("e").await;
1025        cx.shared_state().await.assert_eq(indoc! {
1026            "The «quicˇ»k brown
1027            fox «jumpˇ»s over
1028            the «lazyˇ» dog"
1029        });
1030        cx.simulate_shared_keystrokes("^").await;
1031        cx.shared_state().await.assert_eq(indoc! {
1032            "«ˇThe q»uick brown
1033            «ˇfox j»umps over
1034            «ˇthe l»azy dog"
1035        });
1036        cx.simulate_shared_keystrokes("$").await;
1037        cx.shared_state().await.assert_eq(indoc! {
1038            "The «quick brownˇ»
1039            fox «jumps overˇ»
1040            the «lazy dogˇ»"
1041        });
1042        cx.simulate_shared_keystrokes("shift-f space").await;
1043        cx.shared_state().await.assert_eq(indoc! {
1044            "The «quickˇ» brown
1045            fox «jumpsˇ» over
1046            the «lazy ˇ»dog"
1047        });
1048
1049        // toggling through visual mode works as expected
1050        cx.simulate_shared_keystrokes("v").await;
1051        cx.shared_state().await.assert_eq(indoc! {
1052            "The «quick brown
1053            fox jumps over
1054            the lazy ˇ»dog"
1055        });
1056        cx.simulate_shared_keystrokes("ctrl-v").await;
1057        cx.shared_state().await.assert_eq(indoc! {
1058            "The «quickˇ» brown
1059            fox «jumpsˇ» over
1060            the «lazy ˇ»dog"
1061        });
1062
1063        cx.set_shared_state(indoc! {
1064            "The ˇquick
1065             brown
1066             fox
1067             jumps over the
1068
1069             lazy dog
1070            "
1071        })
1072        .await;
1073        cx.simulate_shared_keystrokes("ctrl-v down down").await;
1074        cx.shared_state().await.assert_eq(indoc! {
1075            "The«ˇ q»uick
1076            bro«ˇwn»
1077            foxˇ
1078            jumps over the
1079
1080            lazy dog
1081            "
1082        });
1083        cx.simulate_shared_keystrokes("down").await;
1084        cx.shared_state().await.assert_eq(indoc! {
1085            "The «qˇ»uick
1086            brow«nˇ»
1087            fox
1088            jump«sˇ» over the
1089
1090            lazy dog
1091            "
1092        });
1093        cx.simulate_shared_keystrokes("left").await;
1094        cx.shared_state().await.assert_eq(indoc! {
1095            "The«ˇ q»uick
1096            bro«ˇwn»
1097            foxˇ
1098            jum«ˇps» over the
1099
1100            lazy dog
1101            "
1102        });
1103        cx.simulate_shared_keystrokes("s o escape").await;
1104        cx.shared_state().await.assert_eq(indoc! {
1105            "Theˇouick
1106            broo
1107            foxo
1108            jumo over the
1109
1110            lazy dog
1111            "
1112        });
1113
1114        // https://github.com/zed-industries/zed/issues/6274
1115        cx.set_shared_state(indoc! {
1116            "Theˇ quick brown
1117
1118            fox jumps over
1119            the lazy dog
1120            "
1121        })
1122        .await;
1123        cx.simulate_shared_keystrokes("l ctrl-v j j").await;
1124        cx.shared_state().await.assert_eq(indoc! {
1125            "The «qˇ»uick brown
1126
1127            fox «jˇ»umps over
1128            the lazy dog
1129            "
1130        });
1131    }
1132
1133    #[gpui::test]
1134    async fn test_visual_block_issue_2123(cx: &mut gpui::TestAppContext) {
1135        let mut cx = NeovimBackedTestContext::new(cx).await;
1136
1137        cx.set_shared_state(indoc! {
1138            "The ˇquick brown
1139            fox jumps over
1140            the lazy dog
1141            "
1142        })
1143        .await;
1144        cx.simulate_shared_keystrokes("ctrl-v right down").await;
1145        cx.shared_state().await.assert_eq(indoc! {
1146            "The «quˇ»ick brown
1147            fox «juˇ»mps over
1148            the lazy dog
1149            "
1150        });
1151    }
1152
1153    #[gpui::test]
1154    async fn test_visual_block_insert(cx: &mut gpui::TestAppContext) {
1155        let mut cx = NeovimBackedTestContext::new(cx).await;
1156
1157        cx.set_shared_state(indoc! {
1158            "ˇThe quick brown
1159            fox jumps over
1160            the lazy dog
1161            "
1162        })
1163        .await;
1164        cx.simulate_shared_keystrokes("ctrl-v 9 down").await;
1165        cx.shared_state().await.assert_eq(indoc! {
1166            "«Tˇ»he quick brown
1167            «fˇ»ox jumps over
1168            «tˇ»he lazy dog
1169            ˇ"
1170        });
1171
1172        cx.simulate_shared_keystrokes("shift-i k escape").await;
1173        cx.shared_state().await.assert_eq(indoc! {
1174            "ˇkThe quick brown
1175            kfox jumps over
1176            kthe lazy dog
1177            k"
1178        });
1179
1180        cx.set_shared_state(indoc! {
1181            "ˇThe quick brown
1182            fox jumps over
1183            the lazy dog
1184            "
1185        })
1186        .await;
1187        cx.simulate_shared_keystrokes("ctrl-v 9 down").await;
1188        cx.shared_state().await.assert_eq(indoc! {
1189            "«Tˇ»he quick brown
1190            «fˇ»ox jumps over
1191            «tˇ»he lazy dog
1192            ˇ"
1193        });
1194        cx.simulate_shared_keystrokes("c k escape").await;
1195        cx.shared_state().await.assert_eq(indoc! {
1196            "ˇkhe quick brown
1197            kox jumps over
1198            khe lazy dog
1199            k"
1200        });
1201    }
1202
1203    #[gpui::test]
1204    async fn test_visual_object(cx: &mut gpui::TestAppContext) {
1205        let mut cx = NeovimBackedTestContext::new(cx).await;
1206
1207        cx.set_shared_state("hello (in [parˇens] o)").await;
1208        cx.simulate_shared_keystrokes("ctrl-v l").await;
1209        cx.simulate_shared_keystrokes("a ]").await;
1210        cx.shared_state()
1211            .await
1212            .assert_eq("hello (in «[parens]ˇ» o)");
1213        cx.simulate_shared_keystrokes("i (").await;
1214        cx.shared_state()
1215            .await
1216            .assert_eq("hello («in [parens] oˇ»)");
1217
1218        cx.set_shared_state("hello in a wˇord again.").await;
1219        cx.simulate_shared_keystrokes("ctrl-v l i w").await;
1220        cx.shared_state()
1221            .await
1222            .assert_eq("hello in a w«ordˇ» again.");
1223        assert_eq!(cx.mode(), Mode::VisualBlock);
1224        cx.simulate_shared_keystrokes("o a s").await;
1225        cx.shared_state()
1226            .await
1227            .assert_eq("«ˇhello in a word» again.");
1228    }
1229
1230    #[gpui::test]
1231    async fn test_mode_across_command(cx: &mut gpui::TestAppContext) {
1232        let mut cx = VimTestContext::new(cx, true).await;
1233
1234        cx.set_state("aˇbc", Mode::Normal);
1235        cx.simulate_keystrokes("ctrl-v");
1236        assert_eq!(cx.mode(), Mode::VisualBlock);
1237        cx.simulate_keystrokes("cmd-shift-p escape");
1238        assert_eq!(cx.mode(), Mode::VisualBlock);
1239    }
1240
1241    #[gpui::test]
1242    async fn test_gn(cx: &mut gpui::TestAppContext) {
1243        let mut cx = NeovimBackedTestContext::new(cx).await;
1244
1245        cx.set_shared_state("aaˇ aa aa aa aa").await;
1246        cx.simulate_shared_keystrokes("/ a a enter").await;
1247        cx.shared_state().await.assert_eq("aa ˇaa aa aa aa");
1248        cx.simulate_shared_keystrokes("g n").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("escape d g n").await;
1253        cx.shared_state().await.assert_eq("aa aa ˇ aa aa");
1254
1255        cx.set_shared_state("aaˇ aa aa aa aa").await;
1256        cx.simulate_shared_keystrokes("/ a a enter").await;
1257        cx.shared_state().await.assert_eq("aa ˇaa aa aa aa");
1258        cx.simulate_shared_keystrokes("3 g n").await;
1259        cx.shared_state().await.assert_eq("aa aa aa «aaˇ» aa");
1260
1261        cx.set_shared_state("aaˇ aa aa aa aa").await;
1262        cx.simulate_shared_keystrokes("/ a a enter").await;
1263        cx.shared_state().await.assert_eq("aa ˇaa aa aa aa");
1264        cx.simulate_shared_keystrokes("g shift-n").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    }
1269
1270    #[gpui::test]
1271    async fn test_gl(cx: &mut gpui::TestAppContext) {
1272        let mut cx = VimTestContext::new(cx, true).await;
1273
1274        cx.set_state("aaˇ aa\naa", Mode::Normal);
1275        cx.simulate_keystrokes("g l");
1276        cx.assert_state("«aaˇ» «aaˇ»\naa", Mode::Visual);
1277        cx.simulate_keystrokes("g >");
1278        cx.assert_state("«aaˇ» aa\n«aaˇ»", Mode::Visual);
1279    }
1280
1281    #[gpui::test]
1282    async fn test_dgn_repeat(cx: &mut gpui::TestAppContext) {
1283        let mut cx = NeovimBackedTestContext::new(cx).await;
1284
1285        cx.set_shared_state("aaˇ aa aa aa aa").await;
1286        cx.simulate_shared_keystrokes("/ a a enter").await;
1287        cx.shared_state().await.assert_eq("aa ˇaa aa aa aa");
1288        cx.simulate_shared_keystrokes("d g n").await;
1289
1290        cx.shared_state().await.assert_eq("aa ˇ aa aa aa");
1291        cx.simulate_shared_keystrokes(".").await;
1292        cx.shared_state().await.assert_eq("aa  ˇ aa aa");
1293        cx.simulate_shared_keystrokes(".").await;
1294        cx.shared_state().await.assert_eq("aa   ˇ aa");
1295    }
1296
1297    #[gpui::test]
1298    async fn test_cgn_repeat(cx: &mut gpui::TestAppContext) {
1299        let mut cx = NeovimBackedTestContext::new(cx).await;
1300
1301        cx.set_shared_state("aaˇ aa aa aa aa").await;
1302        cx.simulate_shared_keystrokes("/ a a enter").await;
1303        cx.shared_state().await.assert_eq("aa ˇaa aa aa aa");
1304        cx.simulate_shared_keystrokes("c g n x escape").await;
1305        cx.shared_state().await.assert_eq("aa ˇx aa aa aa");
1306        cx.simulate_shared_keystrokes(".").await;
1307        cx.shared_state().await.assert_eq("aa x ˇx aa aa");
1308    }
1309
1310    #[gpui::test]
1311    async fn test_cgn_nomatch(cx: &mut gpui::TestAppContext) {
1312        let mut cx = NeovimBackedTestContext::new(cx).await;
1313
1314        cx.set_shared_state("aaˇ aa aa aa aa").await;
1315        cx.simulate_shared_keystrokes("/ b b enter").await;
1316        cx.shared_state().await.assert_eq("aaˇ aa aa aa aa");
1317        cx.simulate_shared_keystrokes("c g n x escape").await;
1318        cx.shared_state().await.assert_eq("aaˇaa aa aa aa");
1319        cx.simulate_shared_keystrokes(".").await;
1320        cx.shared_state().await.assert_eq("aaˇa aa aa aa");
1321
1322        cx.set_shared_state("aaˇ bb aa aa aa").await;
1323        cx.simulate_shared_keystrokes("/ b b enter").await;
1324        cx.shared_state().await.assert_eq("aa ˇbb aa aa aa");
1325        cx.simulate_shared_keystrokes("c g n x escape").await;
1326        cx.shared_state().await.assert_eq("aa ˇx aa aa aa");
1327        cx.simulate_shared_keystrokes(".").await;
1328        cx.shared_state().await.assert_eq("aa ˇx aa aa aa");
1329    }
1330
1331    #[gpui::test]
1332    async fn test_visual_shift_d(cx: &mut gpui::TestAppContext) {
1333        let mut cx = NeovimBackedTestContext::new(cx).await;
1334
1335        cx.set_shared_state(indoc! {
1336            "The ˇquick brown
1337            fox jumps over
1338            the lazy dog
1339            "
1340        })
1341        .await;
1342        cx.simulate_shared_keystrokes("v down shift-d").await;
1343        cx.shared_state().await.assert_eq(indoc! {
1344            "the ˇlazy dog\n"
1345        });
1346
1347        cx.set_shared_state(indoc! {
1348            "The ˇquick brown
1349            fox jumps over
1350            the lazy dog
1351            "
1352        })
1353        .await;
1354        cx.simulate_shared_keystrokes("ctrl-v down shift-d").await;
1355        cx.shared_state().await.assert_eq(indoc! {
1356            "Theˇ•
1357            fox•
1358            the lazy dog
1359            "
1360        });
1361    }
1362
1363    #[gpui::test]
1364    async fn test_gv(cx: &mut gpui::TestAppContext) {
1365        let mut cx = NeovimBackedTestContext::new(cx).await;
1366
1367        cx.set_shared_state(indoc! {
1368            "The ˇquick brown"
1369        })
1370        .await;
1371        cx.simulate_shared_keystrokes("v i w escape g v").await;
1372        cx.shared_state().await.assert_eq(indoc! {
1373            "The «quickˇ» brown"
1374        });
1375
1376        cx.simulate_shared_keystrokes("o escape g v").await;
1377        cx.shared_state().await.assert_eq(indoc! {
1378            "The «ˇquick» brown"
1379        });
1380
1381        cx.simulate_shared_keystrokes("escape ^ ctrl-v l").await;
1382        cx.shared_state().await.assert_eq(indoc! {
1383            "«Thˇ»e quick brown"
1384        });
1385        cx.simulate_shared_keystrokes("g v").await;
1386        cx.shared_state().await.assert_eq(indoc! {
1387            "The «ˇquick» brown"
1388        });
1389        cx.simulate_shared_keystrokes("g v").await;
1390        cx.shared_state().await.assert_eq(indoc! {
1391            "«Thˇ»e quick brown"
1392        });
1393
1394        cx.set_state(
1395            indoc! {"
1396            fiˇsh one
1397            fish two
1398            fish red
1399            fish blue
1400        "},
1401            Mode::Normal,
1402        );
1403        cx.simulate_keystrokes("4 g l escape escape g v");
1404        cx.assert_state(
1405            indoc! {"
1406                «fishˇ» one
1407                «fishˇ» two
1408                «fishˇ» red
1409                «fishˇ» blue
1410            "},
1411            Mode::Visual,
1412        );
1413        cx.simulate_keystrokes("y g v");
1414        cx.assert_state(
1415            indoc! {"
1416                «fishˇ» one
1417                «fishˇ» two
1418                «fishˇ» red
1419                «fishˇ» blue
1420            "},
1421            Mode::Visual,
1422        );
1423    }
1424}