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