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