object.rs

  1use std::ops::Range;
  2
  3use editor::{char_kind, display_map::DisplaySnapshot, movement, Bias, CharKind, DisplayPoint};
  4use gpui::{actions, impl_actions, AppContext, WindowContext};
  5use language::Selection;
  6use serde::Deserialize;
  7use workspace::Workspace;
  8
  9use crate::{motion::right, normal::normal_object, state::Mode, visual::visual_object, Vim};
 10
 11#[derive(Copy, Clone, Debug, PartialEq)]
 12pub enum Object {
 13    Word { ignore_punctuation: bool },
 14    Sentence,
 15    Quotes,
 16    BackQuotes,
 17    DoubleQuotes,
 18    Parentheses,
 19    SquareBrackets,
 20    CurlyBrackets,
 21    AngleBrackets,
 22}
 23
 24#[derive(Clone, Deserialize, PartialEq)]
 25#[serde(rename_all = "camelCase")]
 26struct Word {
 27    #[serde(default)]
 28    ignore_punctuation: bool,
 29}
 30
 31actions!(
 32    vim,
 33    [
 34        Sentence,
 35        Quotes,
 36        BackQuotes,
 37        DoubleQuotes,
 38        Parentheses,
 39        SquareBrackets,
 40        CurlyBrackets,
 41        AngleBrackets
 42    ]
 43);
 44impl_actions!(vim, [Word]);
 45
 46pub fn init(cx: &mut AppContext) {
 47    cx.add_action(
 48        |_: &mut Workspace, &Word { ignore_punctuation }: &Word, cx: _| {
 49            object(Object::Word { ignore_punctuation }, cx)
 50        },
 51    );
 52    cx.add_action(|_: &mut Workspace, _: &Sentence, cx: _| object(Object::Sentence, cx));
 53    cx.add_action(|_: &mut Workspace, _: &Quotes, cx: _| object(Object::Quotes, cx));
 54    cx.add_action(|_: &mut Workspace, _: &BackQuotes, cx: _| object(Object::BackQuotes, cx));
 55    cx.add_action(|_: &mut Workspace, _: &DoubleQuotes, cx: _| object(Object::DoubleQuotes, cx));
 56    cx.add_action(|_: &mut Workspace, _: &Parentheses, cx: _| object(Object::Parentheses, cx));
 57    cx.add_action(|_: &mut Workspace, _: &SquareBrackets, cx: _| {
 58        object(Object::SquareBrackets, cx)
 59    });
 60    cx.add_action(|_: &mut Workspace, _: &CurlyBrackets, cx: _| object(Object::CurlyBrackets, cx));
 61    cx.add_action(|_: &mut Workspace, _: &AngleBrackets, cx: _| object(Object::AngleBrackets, cx));
 62}
 63
 64fn object(object: Object, cx: &mut WindowContext) {
 65    match Vim::read(cx).state().mode {
 66        Mode::Normal => normal_object(object, cx),
 67        Mode::Visual | Mode::VisualLine | Mode::VisualBlock => visual_object(object, cx),
 68        Mode::Insert => {
 69            // Shouldn't execute a text object in insert mode. Ignoring
 70        }
 71    }
 72}
 73
 74impl Object {
 75    pub fn is_multiline(self) -> bool {
 76        match self {
 77            Object::Word { .. } | Object::Quotes | Object::BackQuotes | Object::DoubleQuotes => {
 78                false
 79            }
 80            Object::Sentence
 81            | Object::Parentheses
 82            | Object::AngleBrackets
 83            | Object::CurlyBrackets
 84            | Object::SquareBrackets => true,
 85        }
 86    }
 87
 88    pub fn always_expands_both_ways(self) -> bool {
 89        match self {
 90            Object::Word { .. } | Object::Sentence => false,
 91            Object::Quotes
 92            | Object::BackQuotes
 93            | Object::DoubleQuotes
 94            | Object::Parentheses
 95            | Object::SquareBrackets
 96            | Object::CurlyBrackets
 97            | Object::AngleBrackets => true,
 98        }
 99    }
100
101    pub fn target_visual_mode(self, current_mode: Mode) -> Mode {
102        match self {
103            Object::Word { .. } if current_mode == Mode::VisualLine => Mode::Visual,
104            Object::Word { .. } => current_mode,
105            Object::Sentence
106            | Object::Quotes
107            | Object::BackQuotes
108            | Object::DoubleQuotes
109            | Object::Parentheses
110            | Object::SquareBrackets
111            | Object::CurlyBrackets
112            | Object::AngleBrackets => Mode::Visual,
113        }
114    }
115
116    pub fn range(
117        self,
118        map: &DisplaySnapshot,
119        relative_to: DisplayPoint,
120        around: bool,
121    ) -> Option<Range<DisplayPoint>> {
122        match self {
123            Object::Word { ignore_punctuation } => {
124                if around {
125                    around_word(map, relative_to, ignore_punctuation)
126                } else {
127                    in_word(map, relative_to, ignore_punctuation)
128                }
129            }
130            Object::Sentence => sentence(map, relative_to, around),
131            Object::Quotes => {
132                surrounding_markers(map, relative_to, around, self.is_multiline(), '\'', '\'')
133            }
134            Object::BackQuotes => {
135                surrounding_markers(map, relative_to, around, self.is_multiline(), '`', '`')
136            }
137            Object::DoubleQuotes => {
138                surrounding_markers(map, relative_to, around, self.is_multiline(), '"', '"')
139            }
140            Object::Parentheses => {
141                surrounding_markers(map, relative_to, around, self.is_multiline(), '(', ')')
142            }
143            Object::SquareBrackets => {
144                surrounding_markers(map, relative_to, around, self.is_multiline(), '[', ']')
145            }
146            Object::CurlyBrackets => {
147                surrounding_markers(map, relative_to, around, self.is_multiline(), '{', '}')
148            }
149            Object::AngleBrackets => {
150                surrounding_markers(map, relative_to, around, self.is_multiline(), '<', '>')
151            }
152        }
153    }
154
155    pub fn expand_selection(
156        self,
157        map: &DisplaySnapshot,
158        selection: &mut Selection<DisplayPoint>,
159        around: bool,
160    ) -> bool {
161        if let Some(range) = self.range(map, selection.head(), around) {
162            selection.start = range.start;
163            selection.end = range.end;
164            true
165        } else {
166            false
167        }
168    }
169}
170
171/// Return a range that surrounds the word relative_to is in
172/// If relative_to is at the start of a word, return the word.
173/// If relative_to is between words, return the space between
174fn in_word(
175    map: &DisplaySnapshot,
176    relative_to: DisplayPoint,
177    ignore_punctuation: bool,
178) -> Option<Range<DisplayPoint>> {
179    // Use motion::right so that we consider the character under the cursor when looking for the start
180    let scope = map
181        .buffer_snapshot
182        .language_scope_at(relative_to.to_point(map));
183    let start = movement::find_preceding_boundary_in_line(
184        map,
185        right(map, relative_to, 1),
186        |left, right| {
187            char_kind(&scope, left).coerce_punctuation(ignore_punctuation)
188                != char_kind(&scope, right).coerce_punctuation(ignore_punctuation)
189        },
190    );
191    let end = movement::find_boundary_in_line(map, relative_to, |left, right| {
192        char_kind(&scope, left).coerce_punctuation(ignore_punctuation)
193            != char_kind(&scope, right).coerce_punctuation(ignore_punctuation)
194    });
195
196    Some(start..end)
197}
198
199/// Return a range that surrounds the word and following whitespace
200/// relative_to is in.
201/// If relative_to is at the start of a word, return the word and following whitespace.
202/// If relative_to is between words, return the whitespace back and the following word
203
204/// if in word
205///   delete that word
206///   if there is whitespace following the word, delete that as well
207///   otherwise, delete any preceding whitespace
208/// otherwise
209///   delete whitespace around cursor
210///   delete word following the cursor
211fn around_word(
212    map: &DisplaySnapshot,
213    relative_to: DisplayPoint,
214    ignore_punctuation: bool,
215) -> Option<Range<DisplayPoint>> {
216    let scope = map
217        .buffer_snapshot
218        .language_scope_at(relative_to.to_point(map));
219    let in_word = map
220        .chars_at(relative_to)
221        .next()
222        .map(|(c, _)| char_kind(&scope, c) != CharKind::Whitespace)
223        .unwrap_or(false);
224
225    if in_word {
226        around_containing_word(map, relative_to, ignore_punctuation)
227    } else {
228        around_next_word(map, relative_to, ignore_punctuation)
229    }
230}
231
232fn around_containing_word(
233    map: &DisplaySnapshot,
234    relative_to: DisplayPoint,
235    ignore_punctuation: bool,
236) -> Option<Range<DisplayPoint>> {
237    in_word(map, relative_to, ignore_punctuation)
238        .map(|range| expand_to_include_whitespace(map, range, true))
239}
240
241fn around_next_word(
242    map: &DisplaySnapshot,
243    relative_to: DisplayPoint,
244    ignore_punctuation: bool,
245) -> Option<Range<DisplayPoint>> {
246    let scope = map
247        .buffer_snapshot
248        .language_scope_at(relative_to.to_point(map));
249    // Get the start of the word
250    let start = movement::find_preceding_boundary_in_line(
251        map,
252        right(map, relative_to, 1),
253        |left, right| {
254            char_kind(&scope, left).coerce_punctuation(ignore_punctuation)
255                != char_kind(&scope, right).coerce_punctuation(ignore_punctuation)
256        },
257    );
258
259    let mut word_found = false;
260    let end = movement::find_boundary(map, relative_to, |left, right| {
261        let left_kind = char_kind(&scope, left).coerce_punctuation(ignore_punctuation);
262        let right_kind = char_kind(&scope, right).coerce_punctuation(ignore_punctuation);
263
264        let found = (word_found && left_kind != right_kind) || right == '\n' && left == '\n';
265
266        if right_kind != CharKind::Whitespace {
267            word_found = true;
268        }
269
270        found
271    });
272
273    Some(start..end)
274}
275
276fn sentence(
277    map: &DisplaySnapshot,
278    relative_to: DisplayPoint,
279    around: bool,
280) -> Option<Range<DisplayPoint>> {
281    let mut start = None;
282    let mut previous_end = relative_to;
283
284    let mut chars = map.chars_at(relative_to).peekable();
285
286    // Search backwards for the previous sentence end or current sentence start. Include the character under relative_to
287    for (char, point) in chars
288        .peek()
289        .cloned()
290        .into_iter()
291        .chain(map.reverse_chars_at(relative_to))
292    {
293        if is_sentence_end(map, point) {
294            break;
295        }
296
297        if is_possible_sentence_start(char) {
298            start = Some(point);
299        }
300
301        previous_end = point;
302    }
303
304    // Search forward for the end of the current sentence or if we are between sentences, the start of the next one
305    let mut end = relative_to;
306    for (char, point) in chars {
307        if start.is_none() && is_possible_sentence_start(char) {
308            if around {
309                start = Some(point);
310                continue;
311            } else {
312                end = point;
313                break;
314            }
315        }
316
317        end = point;
318        *end.column_mut() += char.len_utf8() as u32;
319        end = map.clip_point(end, Bias::Left);
320
321        if is_sentence_end(map, end) {
322            break;
323        }
324    }
325
326    let mut range = start.unwrap_or(previous_end)..end;
327    if around {
328        range = expand_to_include_whitespace(map, range, false);
329    }
330
331    Some(range)
332}
333
334fn is_possible_sentence_start(character: char) -> bool {
335    !character.is_whitespace() && character != '.'
336}
337
338const SENTENCE_END_PUNCTUATION: &[char] = &['.', '!', '?'];
339const SENTENCE_END_FILLERS: &[char] = &[')', ']', '"', '\''];
340const SENTENCE_END_WHITESPACE: &[char] = &[' ', '\t', '\n'];
341fn is_sentence_end(map: &DisplaySnapshot, point: DisplayPoint) -> bool {
342    let mut next_chars = map.chars_at(point).peekable();
343    if let Some((char, _)) = next_chars.next() {
344        // We are at a double newline. This position is a sentence end.
345        if char == '\n' && next_chars.peek().map(|(c, _)| c == &'\n').unwrap_or(false) {
346            return true;
347        }
348
349        // The next text is not a valid whitespace. This is not a sentence end
350        if !SENTENCE_END_WHITESPACE.contains(&char) {
351            return false;
352        }
353    }
354
355    for (char, _) in map.reverse_chars_at(point) {
356        if SENTENCE_END_PUNCTUATION.contains(&char) {
357            return true;
358        }
359
360        if !SENTENCE_END_FILLERS.contains(&char) {
361            return false;
362        }
363    }
364
365    return false;
366}
367
368/// Expands the passed range to include whitespace on one side or the other in a line. Attempts to add the
369/// whitespace to the end first and falls back to the start if there was none.
370fn expand_to_include_whitespace(
371    map: &DisplaySnapshot,
372    mut range: Range<DisplayPoint>,
373    stop_at_newline: bool,
374) -> Range<DisplayPoint> {
375    let mut whitespace_included = false;
376
377    let mut chars = map.chars_at(range.end).peekable();
378    while let Some((char, point)) = chars.next() {
379        if char == '\n' && stop_at_newline {
380            break;
381        }
382
383        if char.is_whitespace() {
384            // Set end to the next display_point or the character position after the current display_point
385            range.end = chars.peek().map(|(_, point)| *point).unwrap_or_else(|| {
386                let mut end = point;
387                *end.column_mut() += char.len_utf8() as u32;
388                map.clip_point(end, Bias::Left)
389            });
390
391            if char != '\n' {
392                whitespace_included = true;
393            }
394        } else {
395            // Found non whitespace. Quit out.
396            break;
397        }
398    }
399
400    if !whitespace_included {
401        for (char, point) in map.reverse_chars_at(range.start) {
402            if char == '\n' && stop_at_newline {
403                break;
404            }
405
406            if !char.is_whitespace() {
407                break;
408            }
409
410            range.start = point;
411        }
412    }
413
414    range
415}
416
417fn surrounding_markers(
418    map: &DisplaySnapshot,
419    relative_to: DisplayPoint,
420    around: bool,
421    search_across_lines: bool,
422    start_marker: char,
423    end_marker: char,
424) -> Option<Range<DisplayPoint>> {
425    let mut matched_ends = 0;
426    let mut start = None;
427    for (char, mut point) in map.reverse_chars_at(relative_to) {
428        if char == start_marker {
429            if matched_ends > 0 {
430                matched_ends -= 1;
431            } else {
432                if around {
433                    start = Some(point)
434                } else {
435                    *point.column_mut() += char.len_utf8() as u32;
436                    start = Some(point)
437                }
438                break;
439            }
440        } else if char == end_marker {
441            matched_ends += 1;
442        } else if char == '\n' && !search_across_lines {
443            break;
444        }
445    }
446
447    let mut matched_starts = 0;
448    let mut end = None;
449    for (char, mut point) in map.chars_at(relative_to) {
450        if char == end_marker {
451            if start.is_none() {
452                break;
453            }
454
455            if matched_starts > 0 {
456                matched_starts -= 1;
457            } else {
458                if around {
459                    *point.column_mut() += char.len_utf8() as u32;
460                    end = Some(point);
461                } else {
462                    end = Some(point);
463                }
464
465                break;
466            }
467        }
468
469        if char == start_marker {
470            if start.is_none() {
471                if around {
472                    start = Some(point);
473                } else {
474                    *point.column_mut() += char.len_utf8() as u32;
475                    start = Some(point);
476                }
477            } else {
478                matched_starts += 1;
479            }
480        }
481
482        if char == '\n' && !search_across_lines {
483            break;
484        }
485    }
486
487    let (Some(mut start), Some(mut end)) = (start, end) else {
488        return None;
489    };
490
491    if !around {
492        // if a block starts with a newline, move the start to after the newline.
493        let mut was_newline = false;
494        for (char, point) in map.chars_at(start) {
495            if was_newline {
496                start = point;
497            } else if char == '\n' {
498                was_newline = true;
499                continue;
500            }
501            break;
502        }
503        // if a block ends with a newline, then whitespace, then the delimeter,
504        // move the end to after the newline.
505        let mut new_end = end;
506        for (char, point) in map.reverse_chars_at(end) {
507            if char == '\n' {
508                end = new_end;
509                break;
510            }
511            if !char.is_whitespace() {
512                break;
513            }
514            new_end = point
515        }
516    }
517
518    Some(start..end)
519}
520
521#[cfg(test)]
522mod test {
523    use indoc::indoc;
524
525    use crate::test::{ExemptionFeatures, NeovimBackedTestContext};
526
527    const WORD_LOCATIONS: &'static str = indoc! {"
528        The quick ˇbrowˇnˇ•••
529        fox ˇjuˇmpsˇ over
530        the lazy dogˇ••
531        ˇ
532        ˇ
533        ˇ
534        Thˇeˇ-ˇquˇickˇ ˇbrownˇ•
535        ˇ••
536        ˇ••
537        ˇ  fox-jumpˇs over
538        the lazy dogˇ•
539        ˇ
540        "
541    };
542
543    #[gpui::test]
544    async fn test_change_word_object(cx: &mut gpui::TestAppContext) {
545        let mut cx = NeovimBackedTestContext::new(cx).await;
546
547        cx.assert_binding_matches_all(["c", "i", "w"], WORD_LOCATIONS)
548            .await;
549        cx.assert_binding_matches_all(["c", "i", "shift-w"], WORD_LOCATIONS)
550            .await;
551        cx.assert_binding_matches_all(["c", "a", "w"], WORD_LOCATIONS)
552            .await;
553        cx.assert_binding_matches_all(["c", "a", "shift-w"], WORD_LOCATIONS)
554            .await;
555    }
556
557    #[gpui::test]
558    async fn test_delete_word_object(cx: &mut gpui::TestAppContext) {
559        let mut cx = NeovimBackedTestContext::new(cx).await;
560
561        cx.assert_binding_matches_all(["d", "i", "w"], WORD_LOCATIONS)
562            .await;
563        cx.assert_binding_matches_all(["d", "i", "shift-w"], WORD_LOCATIONS)
564            .await;
565        cx.assert_binding_matches_all(["d", "a", "w"], WORD_LOCATIONS)
566            .await;
567        cx.assert_binding_matches_all(["d", "a", "shift-w"], WORD_LOCATIONS)
568            .await;
569    }
570
571    #[gpui::test]
572    async fn test_visual_word_object(cx: &mut gpui::TestAppContext) {
573        let mut cx = NeovimBackedTestContext::new(cx).await;
574
575        cx.set_shared_state("The quick ˇbrown\nfox").await;
576        cx.simulate_shared_keystrokes(["v"]).await;
577        cx.assert_shared_state("The quick «bˇ»rown\nfox").await;
578        cx.simulate_shared_keystrokes(["i", "w"]).await;
579        cx.assert_shared_state("The quick «brownˇ»\nfox").await;
580
581        cx.assert_binding_matches_all(["v", "i", "w"], WORD_LOCATIONS)
582            .await;
583        cx.assert_binding_matches_all_exempted(
584            ["v", "h", "i", "w"],
585            WORD_LOCATIONS,
586            ExemptionFeatures::NonEmptyVisualTextObjects,
587        )
588        .await;
589        cx.assert_binding_matches_all_exempted(
590            ["v", "l", "i", "w"],
591            WORD_LOCATIONS,
592            ExemptionFeatures::NonEmptyVisualTextObjects,
593        )
594        .await;
595        cx.assert_binding_matches_all(["v", "i", "shift-w"], WORD_LOCATIONS)
596            .await;
597
598        cx.assert_binding_matches_all_exempted(
599            ["v", "i", "h", "shift-w"],
600            WORD_LOCATIONS,
601            ExemptionFeatures::NonEmptyVisualTextObjects,
602        )
603        .await;
604        cx.assert_binding_matches_all_exempted(
605            ["v", "i", "l", "shift-w"],
606            WORD_LOCATIONS,
607            ExemptionFeatures::NonEmptyVisualTextObjects,
608        )
609        .await;
610
611        cx.assert_binding_matches_all_exempted(
612            ["v", "a", "w"],
613            WORD_LOCATIONS,
614            ExemptionFeatures::AroundObjectLeavesWhitespaceAtEndOfLine,
615        )
616        .await;
617        cx.assert_binding_matches_all_exempted(
618            ["v", "a", "shift-w"],
619            WORD_LOCATIONS,
620            ExemptionFeatures::AroundObjectLeavesWhitespaceAtEndOfLine,
621        )
622        .await;
623    }
624
625    const SENTENCE_EXAMPLES: &[&'static str] = &[
626        "ˇThe quick ˇbrownˇ?ˇ ˇFox Jˇumpsˇ!ˇ Ovˇer theˇ lazyˇ.",
627        indoc! {"
628            ˇThe quick ˇbrownˇ
629            fox jumps over
630            the lazy doˇgˇ.ˇ ˇThe quick ˇ
631            brown fox jumps over
632        "},
633        indoc! {"
634            The quick brown fox jumps.
635            Over the lazy dog
636            ˇ
637            ˇ
638            ˇ  fox-jumpˇs over
639            the lazy dog.ˇ
640            ˇ
641        "},
642        r#"ˇThe ˇquick brownˇ.)ˇ]ˇ'ˇ" Brown ˇfox jumpsˇ.ˇ "#,
643    ];
644
645    #[gpui::test]
646    async fn test_change_sentence_object(cx: &mut gpui::TestAppContext) {
647        let mut cx = NeovimBackedTestContext::new(cx)
648            .await
649            .binding(["c", "i", "s"]);
650        cx.add_initial_state_exemptions(
651            "The quick brown fox jumps.\nOver the lazy dog\nˇ\nˇ\n  fox-jumps over\nthe lazy dog.\n\n",
652            ExemptionFeatures::SentenceOnEmptyLines);
653        cx.add_initial_state_exemptions(
654            "The quick brown fox jumps.\nOver the lazy dog\n\n\nˇ  foxˇ-ˇjumpˇs over\nthe lazy dog.\n\n",
655            ExemptionFeatures::SentenceAtStartOfLineWithWhitespace);
656        cx.add_initial_state_exemptions(
657            "The quick brown fox jumps.\nOver the lazy dog\n\n\n  fox-jumps over\nthe lazy dog.ˇ\nˇ\n",
658            ExemptionFeatures::SentenceAfterPunctuationAtEndOfFile);
659        for sentence_example in SENTENCE_EXAMPLES {
660            cx.assert_all(sentence_example).await;
661        }
662
663        let mut cx = cx.binding(["c", "a", "s"]);
664        cx.add_initial_state_exemptions(
665            "The quick brown?ˇ Fox Jumps! Over the lazy.",
666            ExemptionFeatures::IncorrectLandingPosition,
667        );
668        cx.add_initial_state_exemptions(
669            "The quick brown.)]\'\" Brown fox jumps.ˇ ",
670            ExemptionFeatures::AroundObjectLeavesWhitespaceAtEndOfLine,
671        );
672
673        for sentence_example in SENTENCE_EXAMPLES {
674            cx.assert_all(sentence_example).await;
675        }
676    }
677
678    #[gpui::test]
679    async fn test_delete_sentence_object(cx: &mut gpui::TestAppContext) {
680        let mut cx = NeovimBackedTestContext::new(cx)
681            .await
682            .binding(["d", "i", "s"]);
683        cx.add_initial_state_exemptions(
684            "The quick brown fox jumps.\nOver the lazy dog\nˇ\nˇ\n  fox-jumps over\nthe lazy dog.\n\n",
685            ExemptionFeatures::SentenceOnEmptyLines);
686        cx.add_initial_state_exemptions(
687            "The quick brown fox jumps.\nOver the lazy dog\n\n\nˇ  foxˇ-ˇjumpˇs over\nthe lazy dog.\n\n",
688            ExemptionFeatures::SentenceAtStartOfLineWithWhitespace);
689        cx.add_initial_state_exemptions(
690            "The quick brown fox jumps.\nOver the lazy dog\n\n\n  fox-jumps over\nthe lazy dog.ˇ\nˇ\n",
691            ExemptionFeatures::SentenceAfterPunctuationAtEndOfFile);
692
693        for sentence_example in SENTENCE_EXAMPLES {
694            cx.assert_all(sentence_example).await;
695        }
696
697        let mut cx = cx.binding(["d", "a", "s"]);
698        cx.add_initial_state_exemptions(
699            "The quick brown?ˇ Fox Jumps! Over the lazy.",
700            ExemptionFeatures::IncorrectLandingPosition,
701        );
702        cx.add_initial_state_exemptions(
703            "The quick brown.)]\'\" Brown fox jumps.ˇ ",
704            ExemptionFeatures::AroundObjectLeavesWhitespaceAtEndOfLine,
705        );
706
707        for sentence_example in SENTENCE_EXAMPLES {
708            cx.assert_all(sentence_example).await;
709        }
710    }
711
712    #[gpui::test]
713    async fn test_visual_sentence_object(cx: &mut gpui::TestAppContext) {
714        let mut cx = NeovimBackedTestContext::new(cx)
715            .await
716            .binding(["v", "i", "s"]);
717        for sentence_example in SENTENCE_EXAMPLES {
718            cx.assert_all_exempted(sentence_example, ExemptionFeatures::SentenceOnEmptyLines)
719                .await;
720        }
721
722        let mut cx = cx.binding(["v", "a", "s"]);
723        for sentence_example in SENTENCE_EXAMPLES {
724            cx.assert_all_exempted(
725                sentence_example,
726                ExemptionFeatures::AroundSentenceStartingBetweenIncludesWrongWhitespace,
727            )
728            .await;
729        }
730    }
731
732    // Test string with "`" for opening surrounders and "'" for closing surrounders
733    const SURROUNDING_MARKER_STRING: &str = indoc! {"
734        ˇTh'ˇe ˇ`ˇ'ˇquˇi`ˇck broˇ'wn`
735        'ˇfox juˇmps ovˇ`ˇer
736        the ˇlazy dˇ'ˇoˇ`ˇg"};
737
738    const SURROUNDING_OBJECTS: &[(char, char)] = &[
739        ('\'', '\''), // Quote
740        ('`', '`'),   // Back Quote
741        ('"', '"'),   // Double Quote
742        ('(', ')'),   // Parentheses
743        ('[', ']'),   // SquareBrackets
744        ('{', '}'),   // CurlyBrackets
745        ('<', '>'),   // AngleBrackets
746    ];
747
748    #[gpui::test]
749    async fn test_change_surrounding_character_objects(cx: &mut gpui::TestAppContext) {
750        let mut cx = NeovimBackedTestContext::new(cx).await;
751
752        for (start, end) in SURROUNDING_OBJECTS {
753            if ((start == &'\'' || start == &'`' || start == &'"')
754                && !ExemptionFeatures::QuotesSeekForward.supported())
755                || (start == &'<' && !ExemptionFeatures::AngleBracketsFreezeNeovim.supported())
756            {
757                continue;
758            }
759
760            let marked_string = SURROUNDING_MARKER_STRING
761                .replace('`', &start.to_string())
762                .replace('\'', &end.to_string());
763
764            cx.assert_binding_matches_all(["c", "i", &start.to_string()], &marked_string)
765                .await;
766            cx.assert_binding_matches_all(["c", "i", &end.to_string()], &marked_string)
767                .await;
768            cx.assert_binding_matches_all(["c", "a", &start.to_string()], &marked_string)
769                .await;
770            cx.assert_binding_matches_all(["c", "a", &end.to_string()], &marked_string)
771                .await;
772        }
773    }
774
775    #[gpui::test]
776    async fn test_multiline_surrounding_character_objects(cx: &mut gpui::TestAppContext) {
777        let mut cx = NeovimBackedTestContext::new(cx).await;
778
779        cx.set_shared_state(indoc! {
780            "func empty(a string) bool {
781               if a == \"\" {
782                  return true
783               }
784               ˇreturn false
785            }"
786        })
787        .await;
788        cx.simulate_shared_keystrokes(["v", "i", "{"]).await;
789        cx.assert_shared_state(indoc! {"
790            func empty(a string) bool {
791            «   if a == \"\" {
792                  return true
793               }
794               return false
795            ˇ»}"})
796            .await;
797        cx.set_shared_state(indoc! {
798            "func empty(a string) bool {
799                 if a == \"\" {
800                     ˇreturn true
801                 }
802                 return false
803            }"
804        })
805        .await;
806        cx.simulate_shared_keystrokes(["v", "i", "{"]).await;
807        cx.assert_shared_state(indoc! {"
808            func empty(a string) bool {
809                 if a == \"\" {
810            «         return true
811            ˇ»     }
812                 return false
813            }"})
814            .await;
815    }
816
817    #[gpui::test]
818    async fn test_delete_surrounding_character_objects(cx: &mut gpui::TestAppContext) {
819        let mut cx = NeovimBackedTestContext::new(cx).await;
820
821        for (start, end) in SURROUNDING_OBJECTS {
822            if ((start == &'\'' || start == &'`' || start == &'"')
823                && !ExemptionFeatures::QuotesSeekForward.supported())
824                || (start == &'<' && !ExemptionFeatures::AngleBracketsFreezeNeovim.supported())
825            {
826                continue;
827            }
828            let marked_string = SURROUNDING_MARKER_STRING
829                .replace('`', &start.to_string())
830                .replace('\'', &end.to_string());
831
832            cx.assert_binding_matches_all(["d", "i", &start.to_string()], &marked_string)
833                .await;
834            cx.assert_binding_matches_all(["d", "i", &end.to_string()], &marked_string)
835                .await;
836            cx.assert_binding_matches_all(["d", "a", &start.to_string()], &marked_string)
837                .await;
838            cx.assert_binding_matches_all(["d", "a", &end.to_string()], &marked_string)
839                .await;
840        }
841    }
842}