motion.rs

  1use std::sync::Arc;
  2
  3use editor::{
  4    char_kind,
  5    display_map::{Clip, DisplaySnapshot, ToDisplayPoint},
  6    movement, Bias, CharKind, DisplayPoint, ToOffset,
  7};
  8use gpui::{actions, impl_actions, AppContext, WindowContext};
  9use language::{Point, Selection, SelectionGoal};
 10use serde::Deserialize;
 11use workspace::Workspace;
 12
 13use crate::{
 14    normal::normal_motion,
 15    state::{Mode, Operator},
 16    visual::visual_motion,
 17    Vim,
 18};
 19
 20#[derive(Clone, Debug, PartialEq, Eq)]
 21pub enum Motion {
 22    Left,
 23    Backspace,
 24    Down,
 25    Up,
 26    Right,
 27    NextWordStart { ignore_punctuation: bool },
 28    NextWordEnd { ignore_punctuation: bool },
 29    PreviousWordStart { ignore_punctuation: bool },
 30    FirstNonWhitespace,
 31    CurrentLine,
 32    StartOfLine,
 33    EndOfLine,
 34    StartOfParagraph,
 35    EndOfParagraph,
 36    StartOfDocument,
 37    EndOfDocument,
 38    Matching,
 39    FindForward { before: bool, text: Arc<str> },
 40    FindBackward { after: bool, text: Arc<str> },
 41    NextLineStart,
 42}
 43
 44#[derive(Clone, Deserialize, PartialEq)]
 45#[serde(rename_all = "camelCase")]
 46struct NextWordStart {
 47    #[serde(default)]
 48    ignore_punctuation: bool,
 49}
 50
 51#[derive(Clone, Deserialize, PartialEq)]
 52#[serde(rename_all = "camelCase")]
 53struct NextWordEnd {
 54    #[serde(default)]
 55    ignore_punctuation: bool,
 56}
 57
 58#[derive(Clone, Deserialize, PartialEq)]
 59#[serde(rename_all = "camelCase")]
 60struct PreviousWordStart {
 61    #[serde(default)]
 62    ignore_punctuation: bool,
 63}
 64
 65#[derive(Clone, Deserialize, PartialEq)]
 66struct RepeatFind {
 67    #[serde(default)]
 68    backwards: bool,
 69}
 70
 71actions!(
 72    vim,
 73    [
 74        Left,
 75        Backspace,
 76        Down,
 77        Up,
 78        Right,
 79        FirstNonWhitespace,
 80        StartOfLine,
 81        EndOfLine,
 82        CurrentLine,
 83        StartOfParagraph,
 84        EndOfParagraph,
 85        StartOfDocument,
 86        EndOfDocument,
 87        Matching,
 88        NextLineStart,
 89    ]
 90);
 91impl_actions!(
 92    vim,
 93    [NextWordStart, NextWordEnd, PreviousWordStart, RepeatFind]
 94);
 95
 96pub fn init(cx: &mut AppContext) {
 97    cx.add_action(|_: &mut Workspace, _: &Left, cx: _| motion(Motion::Left, cx));
 98    cx.add_action(|_: &mut Workspace, _: &Backspace, cx: _| motion(Motion::Backspace, cx));
 99    cx.add_action(|_: &mut Workspace, _: &Down, cx: _| motion(Motion::Down, cx));
100    cx.add_action(|_: &mut Workspace, _: &Up, cx: _| motion(Motion::Up, cx));
101    cx.add_action(|_: &mut Workspace, _: &Right, cx: _| motion(Motion::Right, cx));
102    cx.add_action(|_: &mut Workspace, _: &FirstNonWhitespace, cx: _| {
103        motion(Motion::FirstNonWhitespace, cx)
104    });
105    cx.add_action(|_: &mut Workspace, _: &StartOfLine, cx: _| motion(Motion::StartOfLine, cx));
106    cx.add_action(|_: &mut Workspace, _: &EndOfLine, cx: _| motion(Motion::EndOfLine, cx));
107    cx.add_action(|_: &mut Workspace, _: &CurrentLine, cx: _| motion(Motion::CurrentLine, cx));
108    cx.add_action(|_: &mut Workspace, _: &StartOfParagraph, cx: _| {
109        motion(Motion::StartOfParagraph, cx)
110    });
111    cx.add_action(|_: &mut Workspace, _: &EndOfParagraph, cx: _| {
112        motion(Motion::EndOfParagraph, cx)
113    });
114    cx.add_action(|_: &mut Workspace, _: &StartOfDocument, cx: _| {
115        motion(Motion::StartOfDocument, cx)
116    });
117    cx.add_action(|_: &mut Workspace, _: &EndOfDocument, cx: _| motion(Motion::EndOfDocument, cx));
118    cx.add_action(|_: &mut Workspace, _: &Matching, cx: _| motion(Motion::Matching, cx));
119
120    cx.add_action(
121        |_: &mut Workspace, &NextWordStart { ignore_punctuation }: &NextWordStart, cx: _| {
122            motion(Motion::NextWordStart { ignore_punctuation }, cx)
123        },
124    );
125    cx.add_action(
126        |_: &mut Workspace, &NextWordEnd { ignore_punctuation }: &NextWordEnd, cx: _| {
127            motion(Motion::NextWordEnd { ignore_punctuation }, cx)
128        },
129    );
130    cx.add_action(
131        |_: &mut Workspace,
132         &PreviousWordStart { ignore_punctuation }: &PreviousWordStart,
133         cx: _| { motion(Motion::PreviousWordStart { ignore_punctuation }, cx) },
134    );
135    cx.add_action(|_: &mut Workspace, &NextLineStart, cx: _| motion(Motion::NextLineStart, cx));
136    cx.add_action(|_: &mut Workspace, action: &RepeatFind, cx: _| {
137        repeat_motion(action.backwards, cx)
138    })
139}
140
141pub(crate) fn motion(motion: Motion, cx: &mut WindowContext) {
142    if let Some(Operator::FindForward { .. }) | Some(Operator::FindBackward { .. }) =
143        Vim::read(cx).active_operator()
144    {
145        Vim::update(cx, |vim, cx| vim.pop_operator(cx));
146    }
147
148    let times = Vim::update(cx, |vim, cx| vim.pop_number_operator(cx));
149    let operator = Vim::read(cx).active_operator();
150    match Vim::read(cx).state.mode {
151        Mode::Normal => normal_motion(motion, operator, times, cx),
152        Mode::Visual { .. } => visual_motion(motion, times, cx),
153        Mode::Insert => {
154            // Shouldn't execute a motion in insert mode. Ignoring
155        }
156    }
157    Vim::update(cx, |vim, cx| vim.clear_operator(cx));
158}
159
160fn repeat_motion(backwards: bool, cx: &mut WindowContext) {
161    let find = match Vim::read(cx).state.last_find.clone() {
162        Some(Motion::FindForward { before, text }) => {
163            if backwards {
164                Motion::FindBackward {
165                    after: before,
166                    text,
167                }
168            } else {
169                Motion::FindForward { before, text }
170            }
171        }
172
173        Some(Motion::FindBackward { after, text }) => {
174            if backwards {
175                Motion::FindForward {
176                    before: after,
177                    text,
178                }
179            } else {
180                Motion::FindBackward { after, text }
181            }
182        }
183        _ => return,
184    };
185
186    motion(find, cx)
187}
188
189// Motion handling is specified here:
190// https://github.com/vim/vim/blob/master/runtime/doc/motion.txt
191impl Motion {
192    pub fn linewise(&self) -> bool {
193        use Motion::*;
194        match self {
195            Down | Up | StartOfDocument | EndOfDocument | CurrentLine | NextLineStart
196            | StartOfParagraph | EndOfParagraph => true,
197            EndOfLine
198            | NextWordEnd { .. }
199            | Matching
200            | FindForward { .. }
201            | Left
202            | Backspace
203            | Right
204            | StartOfLine
205            | NextWordStart { .. }
206            | PreviousWordStart { .. }
207            | FirstNonWhitespace
208            | FindBackward { .. } => false,
209        }
210    }
211
212    pub fn infallible(&self) -> bool {
213        use Motion::*;
214        match self {
215            StartOfDocument | EndOfDocument | CurrentLine => true,
216            Down
217            | Up
218            | EndOfLine
219            | NextWordEnd { .. }
220            | Matching
221            | FindForward { .. }
222            | Left
223            | Backspace
224            | Right
225            | StartOfLine
226            | StartOfParagraph
227            | EndOfParagraph
228            | NextWordStart { .. }
229            | PreviousWordStart { .. }
230            | FirstNonWhitespace
231            | FindBackward { .. }
232            | NextLineStart => false,
233        }
234    }
235
236    pub fn inclusive(&self) -> bool {
237        use Motion::*;
238        match self {
239            Down
240            | Up
241            | StartOfDocument
242            | EndOfDocument
243            | CurrentLine
244            | EndOfLine
245            | NextWordEnd { .. }
246            | Matching
247            | FindForward { .. }
248            | NextLineStart => true,
249            Left
250            | Backspace
251            | Right
252            | StartOfLine
253            | StartOfParagraph
254            | EndOfParagraph
255            | NextWordStart { .. }
256            | PreviousWordStart { .. }
257            | FirstNonWhitespace
258            | FindBackward { .. } => false,
259        }
260    }
261
262    pub fn move_point(
263        &self,
264        map: &DisplaySnapshot,
265        point: DisplayPoint,
266        goal: SelectionGoal,
267        maybe_times: Option<usize>,
268    ) -> Option<(DisplayPoint, SelectionGoal)> {
269        let times = maybe_times.unwrap_or(1);
270        use Motion::*;
271        let infallible = self.infallible();
272        let (new_point, goal) = match self {
273            Left => (left(map, point, times), SelectionGoal::None),
274            Backspace => (backspace(map, point, times), SelectionGoal::None),
275            Down => down(map, point, goal, times),
276            Up => up(map, point, goal, times),
277            Right => (right(map, point, times), SelectionGoal::None),
278            NextWordStart { ignore_punctuation } => (
279                next_word_start(map, point, *ignore_punctuation, times),
280                SelectionGoal::None,
281            ),
282            NextWordEnd { ignore_punctuation } => (
283                next_word_end(map, point, *ignore_punctuation, times),
284                SelectionGoal::None,
285            ),
286            PreviousWordStart { ignore_punctuation } => (
287                previous_word_start(map, point, *ignore_punctuation, times),
288                SelectionGoal::None,
289            ),
290            FirstNonWhitespace => (first_non_whitespace(map, point), SelectionGoal::None),
291            StartOfLine => (start_of_line(map, point), SelectionGoal::None),
292            EndOfLine => (end_of_line(map, point), SelectionGoal::None),
293            StartOfParagraph => (
294                movement::start_of_paragraph(map, point, times),
295                SelectionGoal::None,
296            ),
297            EndOfParagraph => (
298                map.clip_point_with(
299                    movement::end_of_paragraph(map, point, times),
300                    Bias::Left,
301                    Clip::EndOfLine,
302                ),
303                SelectionGoal::None,
304            ),
305            CurrentLine => (end_of_line(map, point), SelectionGoal::None),
306            StartOfDocument => (start_of_document(map, point, times), SelectionGoal::None),
307            EndOfDocument => (
308                end_of_document(map, point, maybe_times),
309                SelectionGoal::None,
310            ),
311            Matching => (matching(map, point), SelectionGoal::None),
312            FindForward { before, text } => (
313                find_forward(map, point, *before, text.clone(), times),
314                SelectionGoal::None,
315            ),
316            FindBackward { after, text } => (
317                find_backward(map, point, *after, text.clone(), times),
318                SelectionGoal::None,
319            ),
320            NextLineStart => (next_line_start(map, point, times), SelectionGoal::None),
321        };
322
323        (new_point != point || infallible).then_some((new_point, goal))
324    }
325
326    // Expands a selection using self motion for an operator
327    pub fn expand_selection(
328        &self,
329        map: &DisplaySnapshot,
330        selection: &mut Selection<DisplayPoint>,
331        times: Option<usize>,
332        expand_to_surrounding_newline: bool,
333    ) -> bool {
334        if let Some((new_head, goal)) =
335            self.move_point(map, selection.head(), selection.goal, times)
336        {
337            selection.set_head(new_head, goal);
338
339            if self.linewise() {
340                selection.start = map.prev_line_boundary(selection.start.to_point(map)).1;
341
342                if expand_to_surrounding_newline {
343                    if selection.end.row() < map.max_point().row() {
344                        *selection.end.row_mut() += 1;
345                        *selection.end.column_mut() = 0;
346                        selection.end = map.clip_point(selection.end, Bias::Right);
347                        // Don't reset the end here
348                        return true;
349                    } else if selection.start.row() > 0 {
350                        *selection.start.row_mut() -= 1;
351                        *selection.start.column_mut() = map.line_len(selection.start.row());
352                        selection.start = map.clip_point(selection.start, Bias::Left);
353                    }
354                }
355
356                (_, selection.end) = map.next_line_boundary(selection.end.to_point(map));
357            } else {
358                // If the motion is exclusive and the end of the motion is in column 1, the
359                // end of the motion is moved to the end of the previous line and the motion
360                // becomes inclusive. Example: "}" moves to the first line after a paragraph,
361                // but "d}" will not include that line.
362                let mut inclusive = self.inclusive();
363                if !inclusive
364                    && self != &Motion::Backspace
365                    && selection.end.row() > selection.start.row()
366                    && selection.end.column() == 0
367                {
368                    inclusive = true;
369                    *selection.end.row_mut() -= 1;
370                    *selection.end.column_mut() = 0;
371                    selection.end = map.clip_point(
372                        map.next_line_boundary(selection.end.to_point(map)).1,
373                        Bias::Left,
374                    );
375                }
376
377                if inclusive && selection.end.column() < map.line_len(selection.end.row()) {
378                    *selection.end.column_mut() += 1;
379                }
380            }
381            true
382        } else {
383            false
384        }
385    }
386}
387
388fn left(map: &DisplaySnapshot, mut point: DisplayPoint, times: usize) -> DisplayPoint {
389    for _ in 0..times {
390        point = map.move_left(point, Clip::None);
391        if point.column() == 0 {
392            break;
393        }
394    }
395    point
396}
397
398fn backspace(map: &DisplaySnapshot, mut point: DisplayPoint, times: usize) -> DisplayPoint {
399    for _ in 0..times {
400        point = movement::left(map, point);
401    }
402    point
403}
404
405fn down(
406    map: &DisplaySnapshot,
407    mut point: DisplayPoint,
408    mut goal: SelectionGoal,
409    times: usize,
410) -> (DisplayPoint, SelectionGoal) {
411    for _ in 0..times {
412        (point, goal) = movement::down(map, point, goal, true);
413    }
414    (point, goal)
415}
416
417fn up(
418    map: &DisplaySnapshot,
419    mut point: DisplayPoint,
420    mut goal: SelectionGoal,
421    times: usize,
422) -> (DisplayPoint, SelectionGoal) {
423    for _ in 0..times {
424        (point, goal) = movement::up(map, point, goal, true);
425    }
426    (point, goal)
427}
428
429pub(crate) fn right(map: &DisplaySnapshot, mut point: DisplayPoint, times: usize) -> DisplayPoint {
430    for _ in 0..times {
431        let new_point = map.clip_point(map.move_right(point, Clip::None), Bias::Right);
432        if point == new_point {
433            break;
434        }
435        point = new_point;
436    }
437    point
438}
439
440pub(crate) fn next_word_start(
441    map: &DisplaySnapshot,
442    mut point: DisplayPoint,
443    ignore_punctuation: bool,
444    times: usize,
445) -> DisplayPoint {
446    for _ in 0..times {
447        let mut crossed_newline = false;
448        point = movement::find_boundary(map, point, |left, right| {
449            let left_kind = char_kind(left).coerce_punctuation(ignore_punctuation);
450            let right_kind = char_kind(right).coerce_punctuation(ignore_punctuation);
451            let at_newline = right == '\n';
452
453            let found = (left_kind != right_kind && right_kind != CharKind::Whitespace)
454                || at_newline && crossed_newline
455                || at_newline && left == '\n'; // Prevents skipping repeated empty lines
456
457            crossed_newline |= at_newline;
458            found
459        })
460    }
461    point
462}
463
464fn next_word_end(
465    map: &DisplaySnapshot,
466    mut point: DisplayPoint,
467    ignore_punctuation: bool,
468    times: usize,
469) -> DisplayPoint {
470    for _ in 0..times {
471        *point.column_mut() += 1;
472        point = movement::find_boundary(map, point, |left, right| {
473            let left_kind = char_kind(left).coerce_punctuation(ignore_punctuation);
474            let right_kind = char_kind(right).coerce_punctuation(ignore_punctuation);
475
476            left_kind != right_kind && left_kind != CharKind::Whitespace
477        });
478
479        // find_boundary clips, so if the character after the next character is a newline or at the end of the document, we know
480        // we have backtracked already
481        if !map
482            .chars_at(point)
483            .nth(1)
484            .map(|(c, _)| c == '\n')
485            .unwrap_or(true)
486        {
487            *point.column_mut() = point.column().saturating_sub(1);
488        }
489        point = map.clip_point(point, Bias::Left);
490    }
491    point
492}
493
494fn previous_word_start(
495    map: &DisplaySnapshot,
496    mut point: DisplayPoint,
497    ignore_punctuation: bool,
498    times: usize,
499) -> DisplayPoint {
500    for _ in 0..times {
501        // This works even though find_preceding_boundary is called for every character in the line containing
502        // cursor because the newline is checked only once.
503        point = movement::find_preceding_boundary(map, point, |left, right| {
504            let left_kind = char_kind(left).coerce_punctuation(ignore_punctuation);
505            let right_kind = char_kind(right).coerce_punctuation(ignore_punctuation);
506
507            (left_kind != right_kind && !right.is_whitespace()) || left == '\n'
508        });
509    }
510    point
511}
512
513fn first_non_whitespace(map: &DisplaySnapshot, from: DisplayPoint) -> DisplayPoint {
514    let mut last_point = DisplayPoint::new(from.row(), 0);
515    for (ch, point) in map.chars_at(last_point) {
516        if ch == '\n' {
517            return from;
518        }
519
520        last_point = point;
521
522        if char_kind(ch) != CharKind::Whitespace {
523            break;
524        }
525    }
526
527    map.clip_point(last_point, Bias::Left)
528}
529
530fn start_of_line(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
531    map.prev_line_boundary(point.to_point(map)).1
532}
533
534fn end_of_line(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
535    map.clip_point(map.next_line_boundary(point.to_point(map)).1, Bias::Left)
536}
537
538fn start_of_document(map: &DisplaySnapshot, point: DisplayPoint, line: usize) -> DisplayPoint {
539    let mut new_point = Point::new((line - 1) as u32, 0).to_display_point(map);
540    *new_point.column_mut() = point.column();
541    map.clip_point(new_point, Bias::Left)
542}
543
544fn end_of_document(
545    map: &DisplaySnapshot,
546    point: DisplayPoint,
547    line: Option<usize>,
548) -> DisplayPoint {
549    let new_row = if let Some(line) = line {
550        (line - 1) as u32
551    } else {
552        map.max_buffer_row()
553    };
554
555    let new_point = Point::new(new_row, point.column());
556    map.clip_point(new_point.to_display_point(map), Bias::Left)
557}
558
559fn matching(map: &DisplaySnapshot, display_point: DisplayPoint) -> DisplayPoint {
560    // https://github.com/vim/vim/blob/1d87e11a1ef201b26ed87585fba70182ad0c468a/runtime/doc/motion.txt#L1200
561    let point = display_point.to_point(map);
562    let offset = point.to_offset(&map.buffer_snapshot);
563
564    // Ensure the range is contained by the current line.
565    let mut line_end = map.next_line_boundary(point).0;
566    if line_end == point {
567        line_end = map.max_point().to_point(map);
568    }
569
570    let line_range = map.prev_line_boundary(point).0..line_end;
571    let visible_line_range =
572        line_range.start..Point::new(line_range.end.row, line_range.end.column.saturating_sub(1));
573    let ranges = map
574        .buffer_snapshot
575        .bracket_ranges(visible_line_range.clone());
576    if let Some(ranges) = ranges {
577        let line_range = line_range.start.to_offset(&map.buffer_snapshot)
578            ..line_range.end.to_offset(&map.buffer_snapshot);
579        let mut closest_pair_destination = None;
580        let mut closest_distance = usize::MAX;
581
582        for (open_range, close_range) in ranges {
583            if open_range.start >= offset && line_range.contains(&open_range.start) {
584                let distance = open_range.start - offset;
585                if distance < closest_distance {
586                    closest_pair_destination = Some(close_range.start);
587                    closest_distance = distance;
588                    continue;
589                }
590            }
591
592            if close_range.start >= offset && line_range.contains(&close_range.start) {
593                let distance = close_range.start - offset;
594                if distance < closest_distance {
595                    closest_pair_destination = Some(open_range.start);
596                    closest_distance = distance;
597                    continue;
598                }
599            }
600
601            continue;
602        }
603
604        closest_pair_destination
605            .map(|destination| destination.to_display_point(map))
606            .unwrap_or(display_point)
607    } else {
608        display_point
609    }
610}
611
612fn find_forward(
613    map: &DisplaySnapshot,
614    from: DisplayPoint,
615    before: bool,
616    target: Arc<str>,
617    times: usize,
618) -> DisplayPoint {
619    map.find_while(from, target.as_ref(), |ch, _| ch != '\n')
620        .skip_while(|found_at| found_at == &from)
621        .nth(times - 1)
622        .map(|mut found| {
623            if before {
624                *found.column_mut() -= 1;
625                found = map.clip_point(found, Bias::Right);
626                found
627            } else {
628                found
629            }
630        })
631        .unwrap_or(from)
632}
633
634fn find_backward(
635    map: &DisplaySnapshot,
636    from: DisplayPoint,
637    after: bool,
638    target: Arc<str>,
639    times: usize,
640) -> DisplayPoint {
641    map.reverse_find_while(from, target.as_ref(), |ch, _| ch != '\n')
642        .skip_while(|found_at| found_at == &from)
643        .nth(times - 1)
644        .map(|mut found| {
645            if after {
646                *found.column_mut() += 1;
647                found = map.clip_point(found, Bias::Left);
648                found
649            } else {
650                found
651            }
652        })
653        .unwrap_or(from)
654}
655
656fn next_line_start(map: &DisplaySnapshot, point: DisplayPoint, times: usize) -> DisplayPoint {
657    let new_row = (point.row() + times as u32).min(map.max_buffer_row());
658    map.clip_point(DisplayPoint::new(new_row, 0), Bias::Left)
659}
660
661#[cfg(test)]
662
663mod test {
664
665    use crate::test::NeovimBackedTestContext;
666    use indoc::indoc;
667
668    #[gpui::test]
669    async fn test_start_end_of_paragraph(cx: &mut gpui::TestAppContext) {
670        let mut cx = NeovimBackedTestContext::new(cx).await;
671
672        let initial_state = indoc! {r"ˇabc
673            def
674
675            paragraph
676            the second
677
678
679
680            third and
681            final"};
682
683        // goes down once
684        cx.set_shared_state(initial_state).await;
685        cx.simulate_shared_keystrokes(["}"]).await;
686        cx.assert_shared_state(indoc! {r"abc
687            def
688            ˇ
689            paragraph
690            the second
691
692
693
694            third and
695            final"})
696            .await;
697
698        // goes up once
699        cx.simulate_shared_keystrokes(["{"]).await;
700        cx.assert_shared_state(initial_state).await;
701
702        // goes down twice
703        cx.simulate_shared_keystrokes(["2", "}"]).await;
704        cx.assert_shared_state(indoc! {r"abc
705            def
706
707            paragraph
708            the second
709            ˇ
710
711
712            third and
713            final"})
714            .await;
715
716        // goes down over multiple blanks
717        cx.simulate_shared_keystrokes(["}"]).await;
718        cx.assert_shared_state(indoc! {r"abc
719                def
720
721                paragraph
722                the second
723
724
725
726                third and
727                finaˇl"})
728            .await;
729
730        // goes up twice
731        cx.simulate_shared_keystrokes(["2", "{"]).await;
732        cx.assert_shared_state(indoc! {r"abc
733                def
734                ˇ
735                paragraph
736                the second
737
738
739
740                third and
741                final"})
742            .await
743    }
744
745    #[gpui::test]
746    async fn test_matching(cx: &mut gpui::TestAppContext) {
747        let mut cx = NeovimBackedTestContext::new(cx).await;
748
749        cx.set_shared_state(indoc! {r"func ˇ(a string) {
750                do(something(with<Types>.and_arrays[0, 2]))
751            }"})
752            .await;
753        cx.simulate_shared_keystrokes(["%"]).await;
754        cx.assert_shared_state(indoc! {r"func (a stringˇ) {
755                do(something(with<Types>.and_arrays[0, 2]))
756            }"})
757            .await;
758
759        // test it works on the last character of the line
760        cx.set_shared_state(indoc! {r"func (a string) ˇ{
761            do(something(with<Types>.and_arrays[0, 2]))
762            }"})
763            .await;
764        cx.simulate_shared_keystrokes(["%"]).await;
765        cx.assert_shared_state(indoc! {r"func (a string) {
766            do(something(with<Types>.and_arrays[0, 2]))
767            ˇ}"})
768            .await;
769
770        // test it works on immediate nesting
771        cx.set_shared_state("ˇ{()}").await;
772        cx.simulate_shared_keystrokes(["%"]).await;
773        cx.assert_shared_state("{()ˇ}").await;
774        cx.simulate_shared_keystrokes(["%"]).await;
775        cx.assert_shared_state("ˇ{()}").await;
776
777        // test it works on immediate nesting inside braces
778        cx.set_shared_state("{\n    ˇ{()}\n}").await;
779        cx.simulate_shared_keystrokes(["%"]).await;
780        cx.assert_shared_state("{\n    {()ˇ}\n}").await;
781
782        // test it jumps to the next paren on a line
783        cx.set_shared_state("func ˇboop() {\n}").await;
784        cx.simulate_shared_keystrokes(["%"]).await;
785        cx.assert_shared_state("func boop(ˇ) {\n}").await;
786    }
787
788    #[gpui::test]
789    async fn test_comma_semicolon(cx: &mut gpui::TestAppContext) {
790        let mut cx = NeovimBackedTestContext::new(cx).await;
791
792        cx.set_shared_state("ˇone two three four").await;
793        cx.simulate_shared_keystrokes(["f", "o"]).await;
794        cx.assert_shared_state("one twˇo three four").await;
795        cx.simulate_shared_keystrokes([","]).await;
796        cx.assert_shared_state("ˇone two three four").await;
797        cx.simulate_shared_keystrokes(["2", ";"]).await;
798        cx.assert_shared_state("one two three fˇour").await;
799        cx.simulate_shared_keystrokes(["shift-t", "e"]).await;
800        cx.assert_shared_state("one two threeˇ four").await;
801        cx.simulate_shared_keystrokes(["3", ";"]).await;
802        cx.assert_shared_state("oneˇ two three four").await;
803        cx.simulate_shared_keystrokes([","]).await;
804        cx.assert_shared_state("one two thˇree four").await;
805    }
806}