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