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