visual.rs

   1use std::{borrow::Cow, sync::Arc};
   2
   3use collections::HashMap;
   4use editor::{
   5    display_map::{DisplaySnapshot, ToDisplayPoint},
   6    movement,
   7    scroll::autoscroll::Autoscroll,
   8    Bias, ClipboardSelection, DisplayPoint, Editor,
   9};
  10use gpui::{actions, AppContext, ViewContext, WindowContext};
  11use language::{AutoindentMode, Selection, SelectionGoal};
  12use workspace::Workspace;
  13
  14use crate::{
  15    motion::Motion,
  16    object::Object,
  17    state::{Mode, Operator},
  18    utils::copy_selections_content,
  19    Vim,
  20};
  21
  22actions!(
  23    vim,
  24    [
  25        ToggleVisual,
  26        ToggleVisualLine,
  27        ToggleVisualBlock,
  28        VisualDelete,
  29        VisualYank,
  30        VisualPaste,
  31        OtherEnd,
  32    ]
  33);
  34
  35pub fn init(cx: &mut AppContext) {
  36    cx.add_action(|_, _: &ToggleVisual, cx: &mut ViewContext<Workspace>| {
  37        toggle_mode(Mode::Visual, cx)
  38    });
  39    cx.add_action(|_, _: &ToggleVisualLine, cx: &mut ViewContext<Workspace>| {
  40        toggle_mode(Mode::VisualLine, cx)
  41    });
  42    cx.add_action(
  43        |_, _: &ToggleVisualBlock, cx: &mut ViewContext<Workspace>| {
  44            toggle_mode(Mode::VisualBlock, cx)
  45        },
  46    );
  47    cx.add_action(other_end);
  48    cx.add_action(delete);
  49    cx.add_action(yank);
  50    cx.add_action(paste);
  51}
  52
  53pub fn visual_motion(motion: Motion, times: Option<usize>, cx: &mut WindowContext) {
  54    Vim::update(cx, |vim, cx| {
  55        vim.update_active_editor(cx, |editor, cx| {
  56            if vim.state().mode == Mode::VisualBlock && !matches!(motion, Motion::EndOfLine) {
  57                let is_up_or_down = matches!(motion, Motion::Up | Motion::Down);
  58                visual_block_motion(is_up_or_down, editor, cx, |map, point, goal| {
  59                    motion.move_point(map, point, goal, times)
  60                })
  61            } else {
  62                editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
  63                    s.move_with(|map, selection| {
  64                        let was_reversed = selection.reversed;
  65                        let mut current_head = selection.head();
  66
  67                        // our motions assume the current character is after the cursor,
  68                        // but in (forward) visual mode the current character is just
  69                        // before the end of the selection.
  70
  71                        // If the file ends with a newline (which is common) we don't do this.
  72                        // so that if you go to the end of such a file you can use "up" to go
  73                        // to the previous line and have it work somewhat as expected.
  74                        if !selection.reversed
  75                            && !selection.is_empty()
  76                            && !(selection.end.column() == 0 && selection.end == map.max_point())
  77                        {
  78                            current_head = movement::left(map, selection.end)
  79                        }
  80
  81                        let Some((new_head, goal)) =
  82                        motion.move_point(map, current_head, selection.goal, times) else { return };
  83
  84                        selection.set_head(new_head, goal);
  85
  86                        // ensure the current character is included in the selection.
  87                        if !selection.reversed {
  88                            let next_point = if vim.state().mode == Mode::VisualBlock {
  89                                movement::saturating_right(map, selection.end)
  90                            } else {
  91                                movement::right(map, selection.end)
  92                            };
  93
  94                            if !(next_point.column() == 0 && next_point == map.max_point()) {
  95                                selection.end = next_point;
  96                            }
  97                        }
  98
  99                        // vim always ensures the anchor character stays selected.
 100                        // if our selection has reversed, we need to move the opposite end
 101                        // to ensure the anchor is still selected.
 102                        if was_reversed && !selection.reversed {
 103                            selection.start = movement::left(map, selection.start);
 104                        } else if !was_reversed && selection.reversed {
 105                            selection.end = movement::right(map, selection.end);
 106                        }
 107                    })
 108                });
 109            }
 110        });
 111    });
 112}
 113
 114pub fn visual_block_motion(
 115    preserve_goal: bool,
 116    editor: &mut Editor,
 117    cx: &mut ViewContext<Editor>,
 118    mut move_selection: impl FnMut(
 119        &DisplaySnapshot,
 120        DisplayPoint,
 121        SelectionGoal,
 122    ) -> Option<(DisplayPoint, SelectionGoal)>,
 123) {
 124    editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 125        let map = &s.display_map();
 126        let mut head = s.newest_anchor().head().to_display_point(map);
 127        let mut tail = s.oldest_anchor().tail().to_display_point(map);
 128        let mut goal = s.newest_anchor().goal;
 129
 130        let was_reversed = tail.column() > head.column();
 131
 132        if !was_reversed && !preserve_goal {
 133            head = movement::saturating_left(map, head);
 134        }
 135
 136        let Some((new_head, _)) = move_selection(&map, head, goal) else {
 137            return
 138        };
 139        head = new_head;
 140
 141        let is_reversed = tail.column() > head.column();
 142        if was_reversed && !is_reversed {
 143            tail = movement::left(map, tail)
 144        } else if !was_reversed && is_reversed {
 145            tail = movement::right(map, tail)
 146        }
 147        if !is_reversed && !preserve_goal {
 148            head = movement::saturating_right(map, head)
 149        }
 150
 151        let (start, end) = match goal {
 152            SelectionGoal::ColumnRange { start, end } if preserve_goal => (start, end),
 153            SelectionGoal::Column(start) if preserve_goal => (start, start + 1),
 154            _ => (tail.column(), head.column()),
 155        };
 156        goal = SelectionGoal::ColumnRange { start, end };
 157
 158        let columns = if is_reversed {
 159            head.column()..tail.column()
 160        } else if head.column() == tail.column() {
 161            head.column()..(head.column() + 1)
 162        } else {
 163            tail.column()..head.column()
 164        };
 165
 166        let mut selections = Vec::new();
 167        let mut row = tail.row();
 168
 169        loop {
 170            let start = map.clip_point(DisplayPoint::new(row, columns.start), Bias::Left);
 171            let end = map.clip_point(DisplayPoint::new(row, columns.end), Bias::Left);
 172            if columns.start <= map.line_len(row) {
 173                let selection = Selection {
 174                    id: s.new_selection_id(),
 175                    start: start.to_point(map),
 176                    end: end.to_point(map),
 177                    reversed: is_reversed,
 178                    goal: goal.clone(),
 179                };
 180
 181                selections.push(selection);
 182            }
 183            if row == head.row() {
 184                break;
 185            }
 186            if tail.row() > head.row() {
 187                row -= 1
 188            } else {
 189                row += 1
 190            }
 191        }
 192
 193        s.select(selections);
 194    })
 195}
 196
 197pub fn visual_object(object: Object, cx: &mut WindowContext) {
 198    Vim::update(cx, |vim, cx| {
 199        if let Some(Operator::Object { around }) = vim.active_operator() {
 200            vim.pop_operator(cx);
 201
 202            vim.update_active_editor(cx, |editor, cx| {
 203                editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 204                    s.move_with(|map, selection| {
 205                        let mut head = selection.head();
 206
 207                        // all our motions assume that the current character is
 208                        // after the cursor; however in the case of a visual selection
 209                        // the current character is before the cursor.
 210                        if !selection.reversed {
 211                            head = movement::left(map, head);
 212                        }
 213
 214                        if let Some(range) = object.range(map, head, around) {
 215                            if !range.is_empty() {
 216                                let expand_both_ways = if selection.is_empty() {
 217                                    true
 218                                // contains only one character
 219                                } else if let Some((_, start)) =
 220                                    map.reverse_chars_at(selection.end).next()
 221                                {
 222                                    selection.start == start
 223                                } else {
 224                                    false
 225                                };
 226
 227                                if expand_both_ways {
 228                                    selection.start = range.start;
 229                                    selection.end = range.end;
 230                                } else if selection.reversed {
 231                                    selection.start = range.start;
 232                                } else {
 233                                    selection.end = range.end;
 234                                }
 235                            }
 236                        }
 237                    });
 238                });
 239            });
 240        }
 241    });
 242}
 243
 244fn toggle_mode(mode: Mode, cx: &mut ViewContext<Workspace>) {
 245    Vim::update(cx, |vim, cx| {
 246        if vim.state().mode == mode {
 247            vim.switch_mode(Mode::Normal, false, cx);
 248        } else {
 249            vim.switch_mode(mode, false, cx);
 250        }
 251    })
 252}
 253
 254pub fn other_end(_: &mut Workspace, _: &OtherEnd, cx: &mut ViewContext<Workspace>) {
 255    Vim::update(cx, |vim, cx| {
 256        vim.update_active_editor(cx, |editor, cx| {
 257            editor.change_selections(None, cx, |s| {
 258                s.move_with(|_, selection| {
 259                    selection.reversed = !selection.reversed;
 260                })
 261            })
 262        })
 263    });
 264}
 265
 266pub fn delete(_: &mut Workspace, _: &VisualDelete, cx: &mut ViewContext<Workspace>) {
 267    Vim::update(cx, |vim, cx| {
 268        vim.update_active_editor(cx, |editor, cx| {
 269            let mut original_columns: HashMap<_, _> = Default::default();
 270            let line_mode = editor.selections.line_mode;
 271
 272            editor.transact(cx, |editor, cx| {
 273                editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 274                    s.move_with(|map, selection| {
 275                        if line_mode {
 276                            let mut position = selection.head();
 277                            if !selection.reversed {
 278                                position = movement::left(map, position);
 279                            }
 280                            original_columns.insert(selection.id, position.to_point(map).column);
 281                        }
 282                        selection.goal = SelectionGoal::None;
 283                    });
 284                });
 285                copy_selections_content(editor, line_mode, cx);
 286                editor.insert("", cx);
 287
 288                // Fixup cursor position after the deletion
 289                editor.set_clip_at_line_ends(true, cx);
 290                editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 291                    s.move_with(|map, selection| {
 292                        let mut cursor = selection.head().to_point(map);
 293
 294                        if let Some(column) = original_columns.get(&selection.id) {
 295                            cursor.column = *column
 296                        }
 297                        let cursor = map.clip_point(cursor.to_display_point(map), Bias::Left);
 298                        selection.collapse_to(cursor, selection.goal)
 299                    });
 300                    if vim.state().mode == Mode::VisualBlock {
 301                        s.select_anchors(vec![s.first_anchor()])
 302                    }
 303                });
 304            })
 305        });
 306        vim.switch_mode(Mode::Normal, true, cx);
 307    });
 308}
 309
 310pub fn yank(_: &mut Workspace, _: &VisualYank, cx: &mut ViewContext<Workspace>) {
 311    Vim::update(cx, |vim, cx| {
 312        vim.update_active_editor(cx, |editor, cx| {
 313            let line_mode = editor.selections.line_mode;
 314            copy_selections_content(editor, line_mode, cx);
 315            editor.change_selections(None, cx, |s| {
 316                s.move_with(|_, selection| {
 317                    selection.collapse_to(selection.start, SelectionGoal::None)
 318                });
 319                if vim.state().mode == Mode::VisualBlock {
 320                    s.select_anchors(vec![s.first_anchor()])
 321                }
 322            });
 323        });
 324        vim.switch_mode(Mode::Normal, true, cx);
 325    });
 326}
 327
 328pub fn paste(_: &mut Workspace, _: &VisualPaste, cx: &mut ViewContext<Workspace>) {
 329    Vim::update(cx, |vim, cx| {
 330        vim.update_active_editor(cx, |editor, cx| {
 331            editor.transact(cx, |editor, cx| {
 332                if let Some(item) = cx.read_from_clipboard() {
 333                    copy_selections_content(editor, editor.selections.line_mode, cx);
 334                    let mut clipboard_text = Cow::Borrowed(item.text());
 335                    if let Some(mut clipboard_selections) =
 336                        item.metadata::<Vec<ClipboardSelection>>()
 337                    {
 338                        let (display_map, selections) = editor.selections.all_adjusted_display(cx);
 339                        let all_selections_were_entire_line =
 340                            clipboard_selections.iter().all(|s| s.is_entire_line);
 341                        if clipboard_selections.len() != selections.len() {
 342                            let mut newline_separated_text = String::new();
 343                            let mut clipboard_selections =
 344                                clipboard_selections.drain(..).peekable();
 345                            let mut ix = 0;
 346                            while let Some(clipboard_selection) = clipboard_selections.next() {
 347                                newline_separated_text
 348                                    .push_str(&clipboard_text[ix..ix + clipboard_selection.len]);
 349                                ix += clipboard_selection.len;
 350                                if clipboard_selections.peek().is_some() {
 351                                    newline_separated_text.push('\n');
 352                                }
 353                            }
 354                            clipboard_text = Cow::Owned(newline_separated_text);
 355                        }
 356
 357                        let mut new_selections = Vec::new();
 358                        editor.buffer().update(cx, |buffer, cx| {
 359                            let snapshot = buffer.snapshot(cx);
 360                            let mut start_offset = 0;
 361                            let mut edits = Vec::new();
 362                            for (ix, selection) in selections.iter().enumerate() {
 363                                let to_insert;
 364                                let linewise;
 365                                if let Some(clipboard_selection) = clipboard_selections.get(ix) {
 366                                    let end_offset = start_offset + clipboard_selection.len;
 367                                    to_insert = &clipboard_text[start_offset..end_offset];
 368                                    linewise = clipboard_selection.is_entire_line;
 369                                    start_offset = end_offset;
 370                                } else {
 371                                    to_insert = clipboard_text.as_str();
 372                                    linewise = all_selections_were_entire_line;
 373                                }
 374
 375                                let mut selection = selection.clone();
 376                                if !selection.reversed {
 377                                    let adjusted = selection.end;
 378                                    // If the selection is empty, move both the start and end forward one
 379                                    // character
 380                                    if selection.is_empty() {
 381                                        selection.start = adjusted;
 382                                        selection.end = adjusted;
 383                                    } else {
 384                                        selection.end = adjusted;
 385                                    }
 386                                }
 387
 388                                let range = selection.map(|p| p.to_point(&display_map)).range();
 389
 390                                let new_position = if linewise {
 391                                    edits.push((range.start..range.start, "\n"));
 392                                    let mut new_position = range.start;
 393                                    new_position.column = 0;
 394                                    new_position.row += 1;
 395                                    new_position
 396                                } else {
 397                                    range.start
 398                                };
 399
 400                                new_selections.push(selection.map(|_| new_position));
 401
 402                                if linewise && to_insert.ends_with('\n') {
 403                                    edits.push((
 404                                        range.clone(),
 405                                        &to_insert[0..to_insert.len().saturating_sub(1)],
 406                                    ))
 407                                } else {
 408                                    edits.push((range.clone(), to_insert));
 409                                }
 410
 411                                if linewise {
 412                                    edits.push((range.end..range.end, "\n"));
 413                                }
 414                            }
 415                            drop(snapshot);
 416                            buffer.edit(edits, Some(AutoindentMode::EachLine), cx);
 417                        });
 418
 419                        editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 420                            s.select(new_selections)
 421                        });
 422                    } else {
 423                        editor.insert(&clipboard_text, cx);
 424                    }
 425                }
 426            });
 427        });
 428        vim.switch_mode(Mode::Normal, true, cx);
 429    });
 430}
 431
 432pub(crate) fn visual_replace(text: Arc<str>, cx: &mut WindowContext) {
 433    Vim::update(cx, |vim, cx| {
 434        vim.update_active_editor(cx, |editor, cx| {
 435            editor.transact(cx, |editor, cx| {
 436                let (display_map, selections) = editor.selections.all_adjusted_display(cx);
 437
 438                // Selections are biased right at the start. So we need to store
 439                // anchors that are biased left so that we can restore the selections
 440                // after the change
 441                let stable_anchors = editor
 442                    .selections
 443                    .disjoint_anchors()
 444                    .into_iter()
 445                    .map(|selection| {
 446                        let start = selection.start.bias_left(&display_map.buffer_snapshot);
 447                        start..start
 448                    })
 449                    .collect::<Vec<_>>();
 450
 451                let mut edits = Vec::new();
 452                for selection in selections.iter() {
 453                    let selection = selection.clone();
 454                    for row_range in
 455                        movement::split_display_range_by_lines(&display_map, selection.range())
 456                    {
 457                        let range = row_range.start.to_offset(&display_map, Bias::Right)
 458                            ..row_range.end.to_offset(&display_map, Bias::Right);
 459                        let text = text.repeat(range.len());
 460                        edits.push((range, text));
 461                    }
 462                }
 463
 464                editor.buffer().update(cx, |buffer, cx| {
 465                    buffer.edit(edits, None, cx);
 466                });
 467                editor.change_selections(None, cx, |s| s.select_ranges(stable_anchors));
 468            });
 469        });
 470        vim.switch_mode(Mode::Normal, false, cx);
 471    });
 472}
 473
 474#[cfg(test)]
 475mod test {
 476    use indoc::indoc;
 477    use workspace::item::Item;
 478
 479    use crate::{
 480        state::Mode,
 481        test::{NeovimBackedTestContext, VimTestContext},
 482    };
 483
 484    #[gpui::test]
 485    async fn test_enter_visual_mode(cx: &mut gpui::TestAppContext) {
 486        let mut cx = NeovimBackedTestContext::new(cx).await;
 487
 488        cx.set_shared_state(indoc! {
 489            "The ˇquick brown
 490            fox jumps over
 491            the lazy dog"
 492        })
 493        .await;
 494        let cursor = cx.update_editor(|editor, _| editor.pixel_position_of_cursor());
 495
 496        // entering visual mode should select the character
 497        // under cursor
 498        cx.simulate_shared_keystrokes(["v"]).await;
 499        cx.assert_shared_state(indoc! { "The «qˇ»uick brown
 500            fox jumps over
 501            the lazy dog"})
 502            .await;
 503        cx.update_editor(|editor, _| assert_eq!(cursor, editor.pixel_position_of_cursor()));
 504
 505        // forwards motions should extend the selection
 506        cx.simulate_shared_keystrokes(["w", "j"]).await;
 507        cx.assert_shared_state(indoc! { "The «quick brown
 508            fox jumps oˇ»ver
 509            the lazy dog"})
 510            .await;
 511
 512        cx.simulate_shared_keystrokes(["escape"]).await;
 513        assert_eq!(Mode::Normal, cx.neovim_mode().await);
 514        cx.assert_shared_state(indoc! { "The quick brown
 515            fox jumps ˇover
 516            the lazy dog"})
 517            .await;
 518
 519        // motions work backwards
 520        cx.simulate_shared_keystrokes(["v", "k", "b"]).await;
 521        cx.assert_shared_state(indoc! { "The «ˇquick brown
 522            fox jumps o»ver
 523            the lazy dog"})
 524            .await;
 525
 526        // works on empty lines
 527        cx.set_shared_state(indoc! {"
 528            a
 529            ˇ
 530            b
 531            "})
 532            .await;
 533        let cursor = cx.update_editor(|editor, _| editor.pixel_position_of_cursor());
 534        cx.simulate_shared_keystrokes(["v"]).await;
 535        cx.assert_shared_state(indoc! {"
 536            a
 537            «
 538            ˇ»b
 539        "})
 540            .await;
 541        cx.update_editor(|editor, _| assert_eq!(cursor, editor.pixel_position_of_cursor()));
 542
 543        // toggles off again
 544        cx.simulate_shared_keystrokes(["v"]).await;
 545        cx.assert_shared_state(indoc! {"
 546            a
 547            ˇ
 548            b
 549            "})
 550            .await;
 551
 552        // works at the end of a document
 553        cx.set_shared_state(indoc! {"
 554            a
 555            b
 556            ˇ"})
 557            .await;
 558
 559        cx.simulate_shared_keystrokes(["v"]).await;
 560        cx.assert_shared_state(indoc! {"
 561            a
 562            b
 563            ˇ"})
 564            .await;
 565        assert_eq!(cx.mode(), cx.neovim_mode().await);
 566    }
 567
 568    #[gpui::test]
 569    async fn test_enter_visual_line_mode(cx: &mut gpui::TestAppContext) {
 570        let mut cx = NeovimBackedTestContext::new(cx).await;
 571
 572        cx.set_shared_state(indoc! {
 573            "The ˇquick brown
 574            fox jumps over
 575            the lazy dog"
 576        })
 577        .await;
 578        cx.simulate_shared_keystrokes(["shift-v"]).await;
 579        cx.assert_shared_state(indoc! { "The «qˇ»uick brown
 580            fox jumps over
 581            the lazy dog"})
 582            .await;
 583        assert_eq!(cx.mode(), cx.neovim_mode().await);
 584        cx.simulate_shared_keystrokes(["x"]).await;
 585        cx.assert_shared_state(indoc! { "fox ˇjumps over
 586        the lazy dog"})
 587            .await;
 588
 589        // it should work on empty lines
 590        cx.set_shared_state(indoc! {"
 591            a
 592            ˇ
 593            b"})
 594            .await;
 595        cx.simulate_shared_keystrokes(["shift-v"]).await;
 596        cx.assert_shared_state(indoc! { "
 597            a
 598            «
 599            ˇ»b"})
 600            .await;
 601        cx.simulate_shared_keystrokes(["x"]).await;
 602        cx.assert_shared_state(indoc! { "
 603            a
 604            ˇb"})
 605            .await;
 606
 607        // it should work at the end of the document
 608        cx.set_shared_state(indoc! {"
 609            a
 610            b
 611            ˇ"})
 612            .await;
 613        let cursor = cx.update_editor(|editor, _| editor.pixel_position_of_cursor());
 614        cx.simulate_shared_keystrokes(["shift-v"]).await;
 615        cx.assert_shared_state(indoc! {"
 616            a
 617            b
 618            ˇ"})
 619            .await;
 620        assert_eq!(cx.mode(), cx.neovim_mode().await);
 621        cx.update_editor(|editor, _| assert_eq!(cursor, editor.pixel_position_of_cursor()));
 622        cx.simulate_shared_keystrokes(["x"]).await;
 623        cx.assert_shared_state(indoc! {"
 624            a
 625            ˇb"})
 626            .await;
 627    }
 628
 629    #[gpui::test]
 630    async fn test_visual_delete(cx: &mut gpui::TestAppContext) {
 631        let mut cx = NeovimBackedTestContext::new(cx).await;
 632
 633        cx.assert_binding_matches(["v", "w"], "The quick ˇbrown")
 634            .await;
 635
 636        cx.assert_binding_matches(["v", "w", "x"], "The quick ˇbrown")
 637            .await;
 638        cx.assert_binding_matches(
 639            ["v", "w", "j", "x"],
 640            indoc! {"
 641                The ˇquick brown
 642                fox jumps over
 643                the lazy dog"},
 644        )
 645        .await;
 646        // Test pasting code copied on delete
 647        cx.simulate_shared_keystrokes(["j", "p"]).await;
 648        cx.assert_state_matches().await;
 649
 650        let mut cx = cx.binding(["v", "w", "j", "x"]);
 651        cx.assert_all(indoc! {"
 652                The ˇquick brown
 653                fox jumps over
 654                the ˇlazy dog"})
 655            .await;
 656        let mut cx = cx.binding(["v", "b", "k", "x"]);
 657        cx.assert_all(indoc! {"
 658                The ˇquick brown
 659                fox jumps ˇover
 660                the ˇlazy dog"})
 661            .await;
 662    }
 663
 664    #[gpui::test]
 665    async fn test_visual_line_delete(cx: &mut gpui::TestAppContext) {
 666        let mut cx = NeovimBackedTestContext::new(cx)
 667            .await
 668            .binding(["shift-v", "x"]);
 669        cx.assert(indoc! {"
 670                The quˇick brown
 671                fox jumps over
 672                the lazy dog"})
 673            .await;
 674        // Test pasting code copied on delete
 675        cx.simulate_shared_keystroke("p").await;
 676        cx.assert_state_matches().await;
 677
 678        cx.assert_all(indoc! {"
 679                The quick brown
 680                fox juˇmps over
 681                the laˇzy dog"})
 682            .await;
 683        let mut cx = cx.binding(["shift-v", "j", "x"]);
 684        cx.assert(indoc! {"
 685                The quˇick brown
 686                fox jumps over
 687                the lazy dog"})
 688            .await;
 689        // Test pasting code copied on delete
 690        cx.simulate_shared_keystroke("p").await;
 691        cx.assert_state_matches().await;
 692
 693        cx.assert_all(indoc! {"
 694                The quick brown
 695                fox juˇmps over
 696                the laˇzy dog"})
 697            .await;
 698
 699        cx.set_shared_state(indoc! {"
 700            The ˇlong line
 701            should not
 702            crash
 703            "})
 704            .await;
 705        cx.simulate_shared_keystrokes(["shift-v", "$", "x"]).await;
 706        cx.assert_state_matches().await;
 707    }
 708
 709    #[gpui::test]
 710    async fn test_visual_yank(cx: &mut gpui::TestAppContext) {
 711        let cx = VimTestContext::new(cx, true).await;
 712        let mut cx = cx.binding(["v", "w", "y"]);
 713        cx.assert("The quick ˇbrown", "The quick ˇbrown");
 714        cx.assert_clipboard_content(Some("brown"));
 715        let mut cx = cx.binding(["v", "w", "j", "y"]);
 716        cx.assert(
 717            indoc! {"
 718                The ˇquick brown
 719                fox jumps over
 720                the lazy dog"},
 721            indoc! {"
 722                The ˇquick brown
 723                fox jumps over
 724                the lazy dog"},
 725        );
 726        cx.assert_clipboard_content(Some(indoc! {"
 727            quick brown
 728            fox jumps o"}));
 729        cx.assert(
 730            indoc! {"
 731                The quick brown
 732                fox jumps over
 733                the ˇlazy dog"},
 734            indoc! {"
 735                The quick brown
 736                fox jumps over
 737                the ˇlazy dog"},
 738        );
 739        cx.assert_clipboard_content(Some("lazy d"));
 740        cx.assert(
 741            indoc! {"
 742                The quick brown
 743                fox jumps ˇover
 744                the lazy dog"},
 745            indoc! {"
 746                The quick brown
 747                fox jumps ˇover
 748                the lazy dog"},
 749        );
 750        cx.assert_clipboard_content(Some(indoc! {"
 751                over
 752                t"}));
 753        let mut cx = cx.binding(["v", "b", "k", "y"]);
 754        cx.assert(
 755            indoc! {"
 756                The ˇquick brown
 757                fox jumps over
 758                the lazy dog"},
 759            indoc! {"
 760                ˇThe quick brown
 761                fox jumps over
 762                the lazy dog"},
 763        );
 764        cx.assert_clipboard_content(Some("The q"));
 765        cx.assert(
 766            indoc! {"
 767                The quick brown
 768                fox jumps over
 769                the ˇlazy dog"},
 770            indoc! {"
 771                The quick brown
 772                ˇfox jumps over
 773                the lazy dog"},
 774        );
 775        cx.assert_clipboard_content(Some(indoc! {"
 776            fox jumps over
 777            the l"}));
 778        cx.assert(
 779            indoc! {"
 780                The quick brown
 781                fox jumps ˇover
 782                the lazy dog"},
 783            indoc! {"
 784                The ˇquick brown
 785                fox jumps over
 786                the lazy dog"},
 787        );
 788        cx.assert_clipboard_content(Some(indoc! {"
 789            quick brown
 790            fox jumps o"}));
 791    }
 792
 793    #[gpui::test]
 794    async fn test_visual_paste(cx: &mut gpui::TestAppContext) {
 795        let mut cx = VimTestContext::new(cx, true).await;
 796        cx.set_state(
 797            indoc! {"
 798                The quick brown
 799                fox «jumpsˇ» over
 800                the lazy dog"},
 801            Mode::Visual,
 802        );
 803        cx.simulate_keystroke("y");
 804        cx.set_state(
 805            indoc! {"
 806                The quick brown
 807                fox jumpˇs over
 808                the lazy dog"},
 809            Mode::Normal,
 810        );
 811        cx.simulate_keystroke("p");
 812        cx.assert_state(
 813            indoc! {"
 814                The quick brown
 815                fox jumpsjumpˇs over
 816                the lazy dog"},
 817            Mode::Normal,
 818        );
 819
 820        cx.set_state(
 821            indoc! {"
 822                The quick brown
 823                fox ju«mˇ»ps over
 824                the lazy dog"},
 825            Mode::VisualLine,
 826        );
 827        cx.simulate_keystroke("d");
 828        cx.assert_state(
 829            indoc! {"
 830                The quick brown
 831                the laˇzy dog"},
 832            Mode::Normal,
 833        );
 834        cx.set_state(
 835            indoc! {"
 836                The quick brown
 837                the «lazyˇ» dog"},
 838            Mode::Visual,
 839        );
 840        cx.simulate_keystroke("p");
 841        cx.assert_state(
 842            &indoc! {"
 843                The quick brown
 844                the_
 845                ˇfox jumps over
 846                dog"}
 847            .replace("_", " "), // Hack for trailing whitespace
 848            Mode::Normal,
 849        );
 850    }
 851
 852    #[gpui::test]
 853    async fn test_visual_block_mode(cx: &mut gpui::TestAppContext) {
 854        let mut cx = NeovimBackedTestContext::new(cx).await;
 855
 856        cx.set_shared_state(indoc! {
 857            "The ˇquick brown
 858             fox jumps over
 859             the lazy dog"
 860        })
 861        .await;
 862        cx.simulate_shared_keystrokes(["ctrl-v"]).await;
 863        cx.assert_shared_state(indoc! {
 864            "The «qˇ»uick brown
 865            fox jumps over
 866            the lazy dog"
 867        })
 868        .await;
 869        cx.simulate_shared_keystrokes(["2", "down"]).await;
 870        cx.assert_shared_state(indoc! {
 871            "The «qˇ»uick brown
 872            fox «jˇ»umps over
 873            the «lˇ»azy dog"
 874        })
 875        .await;
 876        cx.simulate_shared_keystrokes(["e"]).await;
 877        cx.assert_shared_state(indoc! {
 878            "The «quicˇ»k brown
 879            fox «jumpˇ»s over
 880            the «lazyˇ» dog"
 881        })
 882        .await;
 883        cx.simulate_shared_keystrokes(["^"]).await;
 884        cx.assert_shared_state(indoc! {
 885            "«ˇThe q»uick brown
 886            «ˇfox j»umps over
 887            «ˇthe l»azy dog"
 888        })
 889        .await;
 890        cx.simulate_shared_keystrokes(["$"]).await;
 891        cx.assert_shared_state(indoc! {
 892            "The «quick brownˇ»
 893            fox «jumps overˇ»
 894            the «lazy dogˇ»"
 895        })
 896        .await;
 897        cx.simulate_shared_keystrokes(["shift-f", " "]).await;
 898        cx.assert_shared_state(indoc! {
 899            "The «quickˇ» brown
 900            fox «jumpsˇ» over
 901            the «lazy ˇ»dog"
 902        })
 903        .await;
 904
 905        // toggling through visual mode works as expected
 906        cx.simulate_shared_keystrokes(["v"]).await;
 907        cx.assert_shared_state(indoc! {
 908            "The «quick brown
 909            fox jumps over
 910            the lazy ˇ»dog"
 911        })
 912        .await;
 913        cx.simulate_shared_keystrokes(["ctrl-v"]).await;
 914        cx.assert_shared_state(indoc! {
 915            "The «quickˇ» brown
 916            fox «jumpsˇ» over
 917            the «lazy ˇ»dog"
 918        })
 919        .await;
 920
 921        cx.set_shared_state(indoc! {
 922            "The ˇquick
 923             brown
 924             fox
 925             jumps over the
 926
 927             lazy dog
 928            "
 929        })
 930        .await;
 931        cx.simulate_shared_keystrokes(["ctrl-v", "down", "down"])
 932            .await;
 933        cx.assert_shared_state(indoc! {
 934            "The«ˇ q»uick
 935            bro«ˇwn»
 936            foxˇ
 937            jumps over the
 938
 939            lazy dog
 940            "
 941        })
 942        .await;
 943        cx.simulate_shared_keystrokes(["down"]).await;
 944        cx.assert_shared_state(indoc! {
 945            "The «qˇ»uick
 946            brow«nˇ»
 947            fox
 948            jump«sˇ» over the
 949
 950            lazy dog
 951            "
 952        })
 953        .await;
 954        cx.simulate_shared_keystroke("left").await;
 955        cx.assert_shared_state(indoc! {
 956            "The«ˇ q»uick
 957            bro«ˇwn»
 958            foxˇ
 959            jum«ˇps» over the
 960
 961            lazy dog
 962            "
 963        })
 964        .await;
 965        cx.simulate_shared_keystrokes(["s", "o", "escape"]).await;
 966        cx.assert_shared_state(indoc! {
 967            "Theˇouick
 968            broo
 969            foxo
 970            jumo over the
 971
 972            lazy dog
 973            "
 974        })
 975        .await;
 976    }
 977
 978    #[gpui::test]
 979    async fn test_visual_block_insert(cx: &mut gpui::TestAppContext) {
 980        let mut cx = NeovimBackedTestContext::new(cx).await;
 981
 982        cx.set_shared_state(indoc! {
 983            "ˇThe quick brown
 984            fox jumps over
 985            the lazy dog
 986            "
 987        })
 988        .await;
 989        cx.simulate_shared_keystrokes(["ctrl-v", "9", "down"]).await;
 990        cx.assert_shared_state(indoc! {
 991            "«Tˇ»he quick brown
 992            «fˇ»ox jumps over
 993            «tˇ»he lazy dog
 994            ˇ"
 995        })
 996        .await;
 997
 998        cx.simulate_shared_keystrokes(["shift-i", "k", "escape"])
 999            .await;
1000        cx.assert_shared_state(indoc! {
1001            "ˇkThe quick brown
1002            kfox jumps over
1003            kthe lazy dog
1004            k"
1005        })
1006        .await;
1007
1008        cx.set_shared_state(indoc! {
1009            "ˇThe quick brown
1010            fox jumps over
1011            the lazy dog
1012            "
1013        })
1014        .await;
1015        cx.simulate_shared_keystrokes(["ctrl-v", "9", "down"]).await;
1016        cx.assert_shared_state(indoc! {
1017            "«Tˇ»he quick brown
1018            «fˇ»ox jumps over
1019            «tˇ»he lazy dog
1020            ˇ"
1021        })
1022        .await;
1023        cx.simulate_shared_keystrokes(["c", "k", "escape"]).await;
1024        cx.assert_shared_state(indoc! {
1025            "ˇkhe quick brown
1026            kox jumps over
1027            khe lazy dog
1028            k"
1029        })
1030        .await;
1031    }
1032
1033    #[gpui::test]
1034    async fn test_mode_across_command(cx: &mut gpui::TestAppContext) {
1035        let mut cx = VimTestContext::new(cx, true).await;
1036
1037        cx.set_state("aˇbc", Mode::Normal);
1038        cx.simulate_keystrokes(["ctrl-v"]);
1039        assert_eq!(cx.mode(), Mode::VisualBlock);
1040        cx.simulate_keystrokes(["cmd-shift-p", "escape"]);
1041        assert_eq!(cx.mode(), Mode::VisualBlock);
1042    }
1043}