object.rs

   1use std::ops::Range;
   2
   3use crate::{
   4    motion::right,
   5    state::{Mode, Operator},
   6    Vim,
   7};
   8use editor::{
   9    display_map::{DisplaySnapshot, ToDisplayPoint},
  10    movement::{self, FindRange},
  11    Bias, DisplayPoint, Editor,
  12};
  13use gpui::{actions, impl_actions, Window};
  14use itertools::Itertools;
  15use language::{BufferSnapshot, CharKind, Point, Selection, TextObject, TreeSitterOptions};
  16use multi_buffer::MultiBufferRow;
  17use schemars::JsonSchema;
  18use serde::Deserialize;
  19use ui::Context;
  20
  21#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize, JsonSchema)]
  22#[serde(rename_all = "snake_case")]
  23pub enum Object {
  24    Word { ignore_punctuation: bool },
  25    Subword { ignore_punctuation: bool },
  26    Sentence,
  27    Paragraph,
  28    Quotes,
  29    BackQuotes,
  30    AnyQuotes,
  31    DoubleQuotes,
  32    VerticalBars,
  33    AnyBrackets,
  34    Parentheses,
  35    SquareBrackets,
  36    CurlyBrackets,
  37    AngleBrackets,
  38    Argument,
  39    IndentObj { include_below: bool },
  40    Tag,
  41    Method,
  42    Class,
  43    Comment,
  44    EntireFile,
  45}
  46
  47#[derive(Clone, Deserialize, JsonSchema, PartialEq)]
  48#[serde(deny_unknown_fields)]
  49struct Word {
  50    #[serde(default)]
  51    ignore_punctuation: bool,
  52}
  53
  54#[derive(Clone, Deserialize, JsonSchema, PartialEq)]
  55#[serde(deny_unknown_fields)]
  56struct Subword {
  57    #[serde(default)]
  58    ignore_punctuation: bool,
  59}
  60#[derive(Clone, Deserialize, JsonSchema, PartialEq)]
  61#[serde(deny_unknown_fields)]
  62struct IndentObj {
  63    #[serde(default)]
  64    include_below: bool,
  65}
  66
  67impl_actions!(vim, [Word, Subword, IndentObj]);
  68
  69actions!(
  70    vim,
  71    [
  72        Sentence,
  73        Paragraph,
  74        Quotes,
  75        BackQuotes,
  76        AnyQuotes,
  77        DoubleQuotes,
  78        VerticalBars,
  79        Parentheses,
  80        AnyBrackets,
  81        SquareBrackets,
  82        CurlyBrackets,
  83        AngleBrackets,
  84        Argument,
  85        Tag,
  86        Method,
  87        Class,
  88        Comment,
  89        EntireFile
  90    ]
  91);
  92
  93pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
  94    Vim::action(
  95        editor,
  96        cx,
  97        |vim, &Word { ignore_punctuation }: &Word, window, cx| {
  98            vim.object(Object::Word { ignore_punctuation }, window, cx)
  99        },
 100    );
 101    Vim::action(
 102        editor,
 103        cx,
 104        |vim, &Subword { ignore_punctuation }: &Subword, window, cx| {
 105            vim.object(Object::Subword { ignore_punctuation }, window, cx)
 106        },
 107    );
 108    Vim::action(editor, cx, |vim, _: &Tag, window, cx| {
 109        vim.object(Object::Tag, window, cx)
 110    });
 111    Vim::action(editor, cx, |vim, _: &Sentence, window, cx| {
 112        vim.object(Object::Sentence, window, cx)
 113    });
 114    Vim::action(editor, cx, |vim, _: &Paragraph, window, cx| {
 115        vim.object(Object::Paragraph, window, cx)
 116    });
 117    Vim::action(editor, cx, |vim, _: &Quotes, window, cx| {
 118        vim.object(Object::Quotes, window, cx)
 119    });
 120    Vim::action(editor, cx, |vim, _: &AnyQuotes, window, cx| {
 121        vim.object(Object::AnyQuotes, window, cx)
 122    });
 123    Vim::action(editor, cx, |vim, _: &AnyBrackets, window, cx| {
 124        vim.object(Object::AnyBrackets, window, cx)
 125    });
 126    Vim::action(editor, cx, |vim, _: &DoubleQuotes, window, cx| {
 127        vim.object(Object::DoubleQuotes, window, cx)
 128    });
 129    Vim::action(editor, cx, |vim, _: &DoubleQuotes, window, cx| {
 130        vim.object(Object::DoubleQuotes, window, cx)
 131    });
 132    Vim::action(editor, cx, |vim, _: &Parentheses, window, cx| {
 133        vim.object(Object::Parentheses, window, cx)
 134    });
 135    Vim::action(editor, cx, |vim, _: &SquareBrackets, window, cx| {
 136        vim.object(Object::SquareBrackets, window, cx)
 137    });
 138    Vim::action(editor, cx, |vim, _: &CurlyBrackets, window, cx| {
 139        vim.object(Object::CurlyBrackets, window, cx)
 140    });
 141    Vim::action(editor, cx, |vim, _: &AngleBrackets, window, cx| {
 142        vim.object(Object::AngleBrackets, window, cx)
 143    });
 144    Vim::action(editor, cx, |vim, _: &VerticalBars, window, cx| {
 145        vim.object(Object::VerticalBars, window, cx)
 146    });
 147    Vim::action(editor, cx, |vim, _: &Argument, window, cx| {
 148        vim.object(Object::Argument, window, cx)
 149    });
 150    Vim::action(editor, cx, |vim, _: &Method, window, cx| {
 151        vim.object(Object::Method, window, cx)
 152    });
 153    Vim::action(editor, cx, |vim, _: &Class, window, cx| {
 154        vim.object(Object::Class, window, cx)
 155    });
 156    Vim::action(editor, cx, |vim, _: &EntireFile, window, cx| {
 157        vim.object(Object::EntireFile, window, cx)
 158    });
 159    Vim::action(editor, cx, |vim, _: &Comment, window, cx| {
 160        if !matches!(vim.active_operator(), Some(Operator::Object { .. })) {
 161            vim.push_operator(Operator::Object { around: true }, window, cx);
 162        }
 163        vim.object(Object::Comment, window, cx)
 164    });
 165    Vim::action(
 166        editor,
 167        cx,
 168        |vim, &IndentObj { include_below }: &IndentObj, window, cx| {
 169            vim.object(Object::IndentObj { include_below }, window, cx)
 170        },
 171    );
 172}
 173
 174impl Vim {
 175    fn object(&mut self, object: Object, window: &mut Window, cx: &mut Context<Self>) {
 176        match self.mode {
 177            Mode::Normal => self.normal_object(object, window, cx),
 178            Mode::Visual | Mode::VisualLine | Mode::VisualBlock => {
 179                self.visual_object(object, window, cx)
 180            }
 181            Mode::Insert | Mode::Replace | Mode::HelixNormal => {
 182                // Shouldn't execute a text object in insert mode. Ignoring
 183            }
 184        }
 185    }
 186}
 187
 188impl Object {
 189    pub fn is_multiline(self) -> bool {
 190        match self {
 191            Object::Word { .. }
 192            | Object::Subword { .. }
 193            | Object::Quotes
 194            | Object::BackQuotes
 195            | Object::AnyQuotes
 196            | Object::VerticalBars
 197            | Object::DoubleQuotes => false,
 198            Object::Sentence
 199            | Object::Paragraph
 200            | Object::AnyBrackets
 201            | Object::Parentheses
 202            | Object::Tag
 203            | Object::AngleBrackets
 204            | Object::CurlyBrackets
 205            | Object::SquareBrackets
 206            | Object::Argument
 207            | Object::Method
 208            | Object::Class
 209            | Object::EntireFile
 210            | Object::Comment
 211            | Object::IndentObj { .. } => true,
 212        }
 213    }
 214
 215    pub fn always_expands_both_ways(self) -> bool {
 216        match self {
 217            Object::Word { .. }
 218            | Object::Subword { .. }
 219            | Object::Sentence
 220            | Object::Paragraph
 221            | Object::Argument
 222            | Object::IndentObj { .. } => false,
 223            Object::Quotes
 224            | Object::BackQuotes
 225            | Object::AnyQuotes
 226            | Object::DoubleQuotes
 227            | Object::VerticalBars
 228            | Object::AnyBrackets
 229            | Object::Parentheses
 230            | Object::SquareBrackets
 231            | Object::Tag
 232            | Object::Method
 233            | Object::Class
 234            | Object::Comment
 235            | Object::EntireFile
 236            | Object::CurlyBrackets
 237            | Object::AngleBrackets => true,
 238        }
 239    }
 240
 241    pub fn target_visual_mode(self, current_mode: Mode, around: bool) -> Mode {
 242        match self {
 243            Object::Word { .. }
 244            | Object::Subword { .. }
 245            | Object::Sentence
 246            | Object::Quotes
 247            | Object::AnyQuotes
 248            | Object::BackQuotes
 249            | Object::DoubleQuotes => {
 250                if current_mode == Mode::VisualBlock {
 251                    Mode::VisualBlock
 252                } else {
 253                    Mode::Visual
 254                }
 255            }
 256            Object::Parentheses
 257            | Object::AnyBrackets
 258            | Object::SquareBrackets
 259            | Object::CurlyBrackets
 260            | Object::AngleBrackets
 261            | Object::VerticalBars
 262            | Object::Tag
 263            | Object::Comment
 264            | Object::Argument
 265            | Object::IndentObj { .. } => Mode::Visual,
 266            Object::Method | Object::Class => {
 267                if around {
 268                    Mode::VisualLine
 269                } else {
 270                    Mode::Visual
 271                }
 272            }
 273            Object::Paragraph | Object::EntireFile => Mode::VisualLine,
 274        }
 275    }
 276
 277    pub fn range(
 278        self,
 279        map: &DisplaySnapshot,
 280        selection: Selection<DisplayPoint>,
 281        around: bool,
 282    ) -> Option<Range<DisplayPoint>> {
 283        let relative_to = selection.head();
 284        match self {
 285            Object::Word { ignore_punctuation } => {
 286                if around {
 287                    around_word(map, relative_to, ignore_punctuation)
 288                } else {
 289                    in_word(map, relative_to, ignore_punctuation)
 290                }
 291            }
 292            Object::Subword { ignore_punctuation } => {
 293                if around {
 294                    around_subword(map, relative_to, ignore_punctuation)
 295                } else {
 296                    in_subword(map, relative_to, ignore_punctuation)
 297                }
 298            }
 299            Object::Sentence => sentence(map, relative_to, around),
 300            Object::Paragraph => paragraph(map, relative_to, around),
 301            Object::Quotes => {
 302                surrounding_markers(map, relative_to, around, self.is_multiline(), '\'', '\'')
 303            }
 304            Object::BackQuotes => {
 305                surrounding_markers(map, relative_to, around, self.is_multiline(), '`', '`')
 306            }
 307            Object::AnyQuotes => {
 308                let quote_types = ['\'', '"', '`']; // Types of quotes to handle
 309                let relative_offset = relative_to.to_offset(map, Bias::Left) as isize;
 310
 311                // Find the closest matching quote range
 312                quote_types
 313                    .iter()
 314                    .flat_map(|&quote| {
 315                        // Get ranges for each quote type
 316                        surrounding_markers(
 317                            map,
 318                            relative_to,
 319                            around,
 320                            self.is_multiline(),
 321                            quote,
 322                            quote,
 323                        )
 324                    })
 325                    .min_by_key(|range| calculate_range_distance(range, relative_offset, map))
 326            }
 327            Object::DoubleQuotes => {
 328                surrounding_markers(map, relative_to, around, self.is_multiline(), '"', '"')
 329            }
 330            Object::VerticalBars => {
 331                surrounding_markers(map, relative_to, around, self.is_multiline(), '|', '|')
 332            }
 333            Object::Parentheses => {
 334                surrounding_markers(map, relative_to, around, self.is_multiline(), '(', ')')
 335            }
 336            Object::Tag => {
 337                let head = selection.head();
 338                let range = selection.range();
 339                surrounding_html_tag(map, head, range, around)
 340            }
 341            Object::AnyBrackets => {
 342                let bracket_pairs = [('(', ')'), ('[', ']'), ('{', '}'), ('<', '>')];
 343                let relative_offset = relative_to.to_offset(map, Bias::Left) as isize;
 344
 345                bracket_pairs
 346                    .iter()
 347                    .flat_map(|&(open_bracket, close_bracket)| {
 348                        surrounding_markers(
 349                            map,
 350                            relative_to,
 351                            around,
 352                            self.is_multiline(),
 353                            open_bracket,
 354                            close_bracket,
 355                        )
 356                    })
 357                    .min_by_key(|range| calculate_range_distance(range, relative_offset, map))
 358            }
 359            Object::SquareBrackets => {
 360                surrounding_markers(map, relative_to, around, self.is_multiline(), '[', ']')
 361            }
 362            Object::CurlyBrackets => {
 363                surrounding_markers(map, relative_to, around, self.is_multiline(), '{', '}')
 364            }
 365            Object::AngleBrackets => {
 366                surrounding_markers(map, relative_to, around, self.is_multiline(), '<', '>')
 367            }
 368            Object::Method => text_object(
 369                map,
 370                relative_to,
 371                if around {
 372                    TextObject::AroundFunction
 373                } else {
 374                    TextObject::InsideFunction
 375                },
 376            ),
 377            Object::Comment => text_object(
 378                map,
 379                relative_to,
 380                if around {
 381                    TextObject::AroundComment
 382                } else {
 383                    TextObject::InsideComment
 384                },
 385            ),
 386            Object::Class => text_object(
 387                map,
 388                relative_to,
 389                if around {
 390                    TextObject::AroundClass
 391                } else {
 392                    TextObject::InsideClass
 393                },
 394            ),
 395            Object::Argument => argument(map, relative_to, around),
 396            Object::IndentObj { include_below } => indent(map, relative_to, around, include_below),
 397            Object::EntireFile => entire_file(map),
 398        }
 399    }
 400
 401    pub fn expand_selection(
 402        self,
 403        map: &DisplaySnapshot,
 404        selection: &mut Selection<DisplayPoint>,
 405        around: bool,
 406    ) -> bool {
 407        if let Some(range) = self.range(map, selection.clone(), around) {
 408            selection.start = range.start;
 409            selection.end = range.end;
 410            true
 411        } else {
 412            false
 413        }
 414    }
 415}
 416
 417/// Returns a range that surrounds the word `relative_to` is in.
 418///
 419/// If `relative_to` is at the start of a word, return the word.
 420/// If `relative_to` is between words, return the space between.
 421fn in_word(
 422    map: &DisplaySnapshot,
 423    relative_to: DisplayPoint,
 424    ignore_punctuation: bool,
 425) -> Option<Range<DisplayPoint>> {
 426    // Use motion::right so that we consider the character under the cursor when looking for the start
 427    let classifier = map
 428        .buffer_snapshot
 429        .char_classifier_at(relative_to.to_point(map))
 430        .ignore_punctuation(ignore_punctuation);
 431    let start = movement::find_preceding_boundary_display_point(
 432        map,
 433        right(map, relative_to, 1),
 434        movement::FindRange::SingleLine,
 435        |left, right| classifier.kind(left) != classifier.kind(right),
 436    );
 437
 438    let end = movement::find_boundary(map, relative_to, FindRange::SingleLine, |left, right| {
 439        classifier.kind(left) != classifier.kind(right)
 440    });
 441
 442    Some(start..end)
 443}
 444
 445fn in_subword(
 446    map: &DisplaySnapshot,
 447    relative_to: DisplayPoint,
 448    ignore_punctuation: bool,
 449) -> Option<Range<DisplayPoint>> {
 450    let offset = relative_to.to_offset(map, Bias::Left);
 451    // Use motion::right so that we consider the character under the cursor when looking for the start
 452    let classifier = map
 453        .buffer_snapshot
 454        .char_classifier_at(relative_to.to_point(map))
 455        .ignore_punctuation(ignore_punctuation);
 456    let in_subword = map
 457        .buffer_chars_at(offset)
 458        .next()
 459        .map(|(c, _)| {
 460            if classifier.is_word('-') {
 461                !classifier.is_whitespace(c) && c != '_' && c != '-'
 462            } else {
 463                !classifier.is_whitespace(c) && c != '_'
 464            }
 465        })
 466        .unwrap_or(false);
 467
 468    let start = if in_subword {
 469        movement::find_preceding_boundary_display_point(
 470            map,
 471            right(map, relative_to, 1),
 472            movement::FindRange::SingleLine,
 473            |left, right| {
 474                let is_word_start = classifier.kind(left) != classifier.kind(right);
 475                let is_subword_start = classifier.is_word('-') && left == '-' && right != '-'
 476                    || left == '_' && right != '_'
 477                    || left.is_lowercase() && right.is_uppercase();
 478                is_word_start || is_subword_start
 479            },
 480        )
 481    } else {
 482        movement::find_boundary(map, relative_to, FindRange::SingleLine, |left, right| {
 483            let is_word_start = classifier.kind(left) != classifier.kind(right);
 484            let is_subword_start = classifier.is_word('-') && left == '-' && right != '-'
 485                || left == '_' && right != '_'
 486                || left.is_lowercase() && right.is_uppercase();
 487            is_word_start || is_subword_start
 488        })
 489    };
 490
 491    let end = movement::find_boundary(map, relative_to, FindRange::SingleLine, |left, right| {
 492        let is_word_end = classifier.kind(left) != classifier.kind(right);
 493        let is_subword_end = classifier.is_word('-') && left != '-' && right == '-'
 494            || left != '_' && right == '_'
 495            || left.is_lowercase() && right.is_uppercase();
 496        is_word_end || is_subword_end
 497    });
 498
 499    Some(start..end)
 500}
 501
 502pub fn surrounding_html_tag(
 503    map: &DisplaySnapshot,
 504    head: DisplayPoint,
 505    range: Range<DisplayPoint>,
 506    around: bool,
 507) -> Option<Range<DisplayPoint>> {
 508    fn read_tag(chars: impl Iterator<Item = char>) -> String {
 509        chars
 510            .take_while(|c| c.is_alphanumeric() || *c == ':' || *c == '-' || *c == '_' || *c == '.')
 511            .collect()
 512    }
 513    fn open_tag(mut chars: impl Iterator<Item = char>) -> Option<String> {
 514        if Some('<') != chars.next() {
 515            return None;
 516        }
 517        Some(read_tag(chars))
 518    }
 519    fn close_tag(mut chars: impl Iterator<Item = char>) -> Option<String> {
 520        if (Some('<'), Some('/')) != (chars.next(), chars.next()) {
 521            return None;
 522        }
 523        Some(read_tag(chars))
 524    }
 525
 526    let snapshot = &map.buffer_snapshot;
 527    let offset = head.to_offset(map, Bias::Left);
 528    let mut excerpt = snapshot.excerpt_containing(offset..offset)?;
 529    let buffer = excerpt.buffer();
 530    let offset = excerpt.map_offset_to_buffer(offset);
 531
 532    // Find the most closest to current offset
 533    let mut cursor = buffer.syntax_layer_at(offset)?.node().walk();
 534    let mut last_child_node = cursor.node();
 535    while cursor.goto_first_child_for_byte(offset).is_some() {
 536        last_child_node = cursor.node();
 537    }
 538
 539    let mut last_child_node = Some(last_child_node);
 540    while let Some(cur_node) = last_child_node {
 541        if cur_node.child_count() >= 2 {
 542            let first_child = cur_node.child(0);
 543            let last_child = cur_node.child(cur_node.child_count() - 1);
 544            if let (Some(first_child), Some(last_child)) = (first_child, last_child) {
 545                let open_tag = open_tag(buffer.chars_for_range(first_child.byte_range()));
 546                let close_tag = close_tag(buffer.chars_for_range(last_child.byte_range()));
 547                // It needs to be handled differently according to the selection length
 548                let is_valid = if range.end.to_offset(map, Bias::Left)
 549                    - range.start.to_offset(map, Bias::Left)
 550                    <= 1
 551                {
 552                    offset <= last_child.end_byte()
 553                } else {
 554                    range.start.to_offset(map, Bias::Left) >= first_child.start_byte()
 555                        && range.end.to_offset(map, Bias::Left) <= last_child.start_byte() + 1
 556                };
 557                if open_tag.is_some() && open_tag == close_tag && is_valid {
 558                    let range = if around {
 559                        first_child.byte_range().start..last_child.byte_range().end
 560                    } else {
 561                        first_child.byte_range().end..last_child.byte_range().start
 562                    };
 563                    if excerpt.contains_buffer_range(range.clone()) {
 564                        let result = excerpt.map_range_from_buffer(range);
 565                        return Some(
 566                            result.start.to_display_point(map)..result.end.to_display_point(map),
 567                        );
 568                    }
 569                }
 570            }
 571        }
 572        last_child_node = cur_node.parent();
 573    }
 574    None
 575}
 576
 577/// Returns a range that surrounds the word and following whitespace
 578/// relative_to is in.
 579///
 580/// If `relative_to` is at the start of a word, return the word and following whitespace.
 581/// If `relative_to` is between words, return the whitespace back and the following word.
 582///
 583/// if in word
 584///   delete that word
 585///   if there is whitespace following the word, delete that as well
 586///   otherwise, delete any preceding whitespace
 587/// otherwise
 588///   delete whitespace around cursor
 589///   delete word following the cursor
 590fn around_word(
 591    map: &DisplaySnapshot,
 592    relative_to: DisplayPoint,
 593    ignore_punctuation: bool,
 594) -> Option<Range<DisplayPoint>> {
 595    let offset = relative_to.to_offset(map, Bias::Left);
 596    let classifier = map
 597        .buffer_snapshot
 598        .char_classifier_at(offset)
 599        .ignore_punctuation(ignore_punctuation);
 600    let in_word = map
 601        .buffer_chars_at(offset)
 602        .next()
 603        .map(|(c, _)| !classifier.is_whitespace(c))
 604        .unwrap_or(false);
 605
 606    if in_word {
 607        around_containing_word(map, relative_to, ignore_punctuation)
 608    } else {
 609        around_next_word(map, relative_to, ignore_punctuation)
 610    }
 611}
 612
 613/// Calculate distance between a range and a cursor position
 614///
 615/// Returns a score where:
 616/// - Lower values indicate better matches
 617/// - Range containing cursor gets priority (returns range length)
 618/// - For non-containing ranges, uses minimum distance to boundaries as primary factor
 619/// - Range length is used as secondary factor for tiebreaking
 620fn calculate_range_distance(
 621    range: &Range<DisplayPoint>,
 622    cursor_offset: isize,
 623    map: &DisplaySnapshot,
 624) -> isize {
 625    let start_offset = range.start.to_offset(map, Bias::Left) as isize;
 626    let end_offset = range.end.to_offset(map, Bias::Right) as isize;
 627    let range_length = end_offset - start_offset;
 628
 629    // If cursor is inside the range, return range length
 630    if cursor_offset >= start_offset && cursor_offset <= end_offset {
 631        return range_length;
 632    }
 633
 634    // Calculate minimum distance to range boundaries
 635    let start_distance = (cursor_offset - start_offset).abs();
 636    let end_distance = (cursor_offset - end_offset).abs();
 637    let min_distance = start_distance.min(end_distance);
 638
 639    // Use min_distance as primary factor, range_length as secondary
 640    // Multiply by large number to ensure distance is primary factor
 641    min_distance * 10000 + range_length
 642}
 643
 644fn around_subword(
 645    map: &DisplaySnapshot,
 646    relative_to: DisplayPoint,
 647    ignore_punctuation: bool,
 648) -> Option<Range<DisplayPoint>> {
 649    // Use motion::right so that we consider the character under the cursor when looking for the start
 650    let classifier = map
 651        .buffer_snapshot
 652        .char_classifier_at(relative_to.to_point(map))
 653        .ignore_punctuation(ignore_punctuation);
 654    let start = movement::find_preceding_boundary_display_point(
 655        map,
 656        right(map, relative_to, 1),
 657        movement::FindRange::SingleLine,
 658        |left, right| {
 659            let is_word_start = classifier.kind(left) != classifier.kind(right);
 660            let is_subword_start = classifier.is_word('-') && left != '-' && right == '-'
 661                || left != '_' && right == '_'
 662                || left.is_lowercase() && right.is_uppercase();
 663            is_word_start || is_subword_start
 664        },
 665    );
 666
 667    let end = movement::find_boundary(map, relative_to, FindRange::SingleLine, |left, right| {
 668        let is_word_end = classifier.kind(left) != classifier.kind(right);
 669        let is_subword_end = classifier.is_word('-') && left != '-' && right == '-'
 670            || left != '_' && right == '_'
 671            || left.is_lowercase() && right.is_uppercase();
 672        is_word_end || is_subword_end
 673    });
 674
 675    Some(start..end).map(|range| expand_to_include_whitespace(map, range, true))
 676}
 677
 678fn around_containing_word(
 679    map: &DisplaySnapshot,
 680    relative_to: DisplayPoint,
 681    ignore_punctuation: bool,
 682) -> Option<Range<DisplayPoint>> {
 683    in_word(map, relative_to, ignore_punctuation)
 684        .map(|range| expand_to_include_whitespace(map, range, true))
 685}
 686
 687fn around_next_word(
 688    map: &DisplaySnapshot,
 689    relative_to: DisplayPoint,
 690    ignore_punctuation: bool,
 691) -> Option<Range<DisplayPoint>> {
 692    let classifier = map
 693        .buffer_snapshot
 694        .char_classifier_at(relative_to.to_point(map))
 695        .ignore_punctuation(ignore_punctuation);
 696    // Get the start of the word
 697    let start = movement::find_preceding_boundary_display_point(
 698        map,
 699        right(map, relative_to, 1),
 700        FindRange::SingleLine,
 701        |left, right| classifier.kind(left) != classifier.kind(right),
 702    );
 703
 704    let mut word_found = false;
 705    let end = movement::find_boundary(map, relative_to, FindRange::MultiLine, |left, right| {
 706        let left_kind = classifier.kind(left);
 707        let right_kind = classifier.kind(right);
 708
 709        let found = (word_found && left_kind != right_kind) || right == '\n' && left == '\n';
 710
 711        if right_kind != CharKind::Whitespace {
 712            word_found = true;
 713        }
 714
 715        found
 716    });
 717
 718    Some(start..end)
 719}
 720
 721fn entire_file(map: &DisplaySnapshot) -> Option<Range<DisplayPoint>> {
 722    Some(DisplayPoint::zero()..map.max_point())
 723}
 724
 725fn text_object(
 726    map: &DisplaySnapshot,
 727    relative_to: DisplayPoint,
 728    target: TextObject,
 729) -> Option<Range<DisplayPoint>> {
 730    let snapshot = &map.buffer_snapshot;
 731    let offset = relative_to.to_offset(map, Bias::Left);
 732
 733    let mut excerpt = snapshot.excerpt_containing(offset..offset)?;
 734    let buffer = excerpt.buffer();
 735    let offset = excerpt.map_offset_to_buffer(offset);
 736
 737    let mut matches: Vec<Range<usize>> = buffer
 738        .text_object_ranges(offset..offset, TreeSitterOptions::default())
 739        .filter_map(|(r, m)| if m == target { Some(r) } else { None })
 740        .collect();
 741    matches.sort_by_key(|r| (r.end - r.start));
 742    if let Some(buffer_range) = matches.first() {
 743        let range = excerpt.map_range_from_buffer(buffer_range.clone());
 744        return Some(range.start.to_display_point(map)..range.end.to_display_point(map));
 745    }
 746
 747    let around = target.around()?;
 748    let mut matches: Vec<Range<usize>> = buffer
 749        .text_object_ranges(offset..offset, TreeSitterOptions::default())
 750        .filter_map(|(r, m)| if m == around { Some(r) } else { None })
 751        .collect();
 752    matches.sort_by_key(|r| (r.end - r.start));
 753    let around_range = matches.first()?;
 754
 755    let mut matches: Vec<Range<usize>> = buffer
 756        .text_object_ranges(around_range.clone(), TreeSitterOptions::default())
 757        .filter_map(|(r, m)| if m == target { Some(r) } else { None })
 758        .collect();
 759    matches.sort_by_key(|r| r.start);
 760    if let Some(buffer_range) = matches.first() {
 761        if !buffer_range.is_empty() {
 762            let range = excerpt.map_range_from_buffer(buffer_range.clone());
 763            return Some(range.start.to_display_point(map)..range.end.to_display_point(map));
 764        }
 765    }
 766    let buffer_range = excerpt.map_range_from_buffer(around_range.clone());
 767    return Some(buffer_range.start.to_display_point(map)..buffer_range.end.to_display_point(map));
 768}
 769
 770fn argument(
 771    map: &DisplaySnapshot,
 772    relative_to: DisplayPoint,
 773    around: bool,
 774) -> Option<Range<DisplayPoint>> {
 775    let snapshot = &map.buffer_snapshot;
 776    let offset = relative_to.to_offset(map, Bias::Left);
 777
 778    // The `argument` vim text object uses the syntax tree, so we operate at the buffer level and map back to the display level
 779    let mut excerpt = snapshot.excerpt_containing(offset..offset)?;
 780    let buffer = excerpt.buffer();
 781
 782    fn comma_delimited_range_at(
 783        buffer: &BufferSnapshot,
 784        mut offset: usize,
 785        include_comma: bool,
 786    ) -> Option<Range<usize>> {
 787        // Seek to the first non-whitespace character
 788        offset += buffer
 789            .chars_at(offset)
 790            .take_while(|c| c.is_whitespace())
 791            .map(char::len_utf8)
 792            .sum::<usize>();
 793
 794        let bracket_filter = |open: Range<usize>, close: Range<usize>| {
 795            // Filter out empty ranges
 796            if open.end == close.start {
 797                return false;
 798            }
 799
 800            // If the cursor is outside the brackets, ignore them
 801            if open.start == offset || close.end == offset {
 802                return false;
 803            }
 804
 805            // TODO: Is there any better way to filter out string brackets?
 806            // Used to filter out string brackets
 807            matches!(
 808                buffer.chars_at(open.start).next(),
 809                Some('(' | '[' | '{' | '<' | '|')
 810            )
 811        };
 812
 813        // Find the brackets containing the cursor
 814        let (open_bracket, close_bracket) =
 815            buffer.innermost_enclosing_bracket_ranges(offset..offset, Some(&bracket_filter))?;
 816
 817        let inner_bracket_range = open_bracket.end..close_bracket.start;
 818
 819        let layer = buffer.syntax_layer_at(offset)?;
 820        let node = layer.node();
 821        let mut cursor = node.walk();
 822
 823        // Loop until we find the smallest node whose parent covers the bracket range. This node is the argument in the parent argument list
 824        let mut parent_covers_bracket_range = false;
 825        loop {
 826            let node = cursor.node();
 827            let range = node.byte_range();
 828            let covers_bracket_range =
 829                range.start == open_bracket.start && range.end == close_bracket.end;
 830            if parent_covers_bracket_range && !covers_bracket_range {
 831                break;
 832            }
 833            parent_covers_bracket_range = covers_bracket_range;
 834
 835            // Unable to find a child node with a parent that covers the bracket range, so no argument to select
 836            cursor.goto_first_child_for_byte(offset)?;
 837        }
 838
 839        let mut argument_node = cursor.node();
 840
 841        // If the child node is the open bracket, move to the next sibling.
 842        if argument_node.byte_range() == open_bracket {
 843            if !cursor.goto_next_sibling() {
 844                return Some(inner_bracket_range);
 845            }
 846            argument_node = cursor.node();
 847        }
 848        // While the child node is the close bracket or a comma, move to the previous sibling
 849        while argument_node.byte_range() == close_bracket || argument_node.kind() == "," {
 850            if !cursor.goto_previous_sibling() {
 851                return Some(inner_bracket_range);
 852            }
 853            argument_node = cursor.node();
 854            if argument_node.byte_range() == open_bracket {
 855                return Some(inner_bracket_range);
 856            }
 857        }
 858
 859        // The start and end of the argument range, defaulting to the start and end of the argument node
 860        let mut start = argument_node.start_byte();
 861        let mut end = argument_node.end_byte();
 862
 863        let mut needs_surrounding_comma = include_comma;
 864
 865        // Seek backwards to find the start of the argument - either the previous comma or the opening bracket.
 866        // We do this because multiple nodes can represent a single argument, such as with rust `vec![a.b.c, d.e.f]`
 867        while cursor.goto_previous_sibling() {
 868            let prev = cursor.node();
 869
 870            if prev.start_byte() < open_bracket.end {
 871                start = open_bracket.end;
 872                break;
 873            } else if prev.kind() == "," {
 874                if needs_surrounding_comma {
 875                    start = prev.start_byte();
 876                    needs_surrounding_comma = false;
 877                }
 878                break;
 879            } else if prev.start_byte() < start {
 880                start = prev.start_byte();
 881            }
 882        }
 883
 884        // Do the same for the end of the argument, extending to next comma or the end of the argument list
 885        while cursor.goto_next_sibling() {
 886            let next = cursor.node();
 887
 888            if next.end_byte() > close_bracket.start {
 889                end = close_bracket.start;
 890                break;
 891            } else if next.kind() == "," {
 892                if needs_surrounding_comma {
 893                    // Select up to the beginning of the next argument if there is one, otherwise to the end of the comma
 894                    if let Some(next_arg) = next.next_sibling() {
 895                        end = next_arg.start_byte();
 896                    } else {
 897                        end = next.end_byte();
 898                    }
 899                }
 900                break;
 901            } else if next.end_byte() > end {
 902                end = next.end_byte();
 903            }
 904        }
 905
 906        Some(start..end)
 907    }
 908
 909    let result = comma_delimited_range_at(buffer, excerpt.map_offset_to_buffer(offset), around)?;
 910
 911    if excerpt.contains_buffer_range(result.clone()) {
 912        let result = excerpt.map_range_from_buffer(result);
 913        Some(result.start.to_display_point(map)..result.end.to_display_point(map))
 914    } else {
 915        None
 916    }
 917}
 918
 919fn indent(
 920    map: &DisplaySnapshot,
 921    relative_to: DisplayPoint,
 922    around: bool,
 923    include_below: bool,
 924) -> Option<Range<DisplayPoint>> {
 925    let point = relative_to.to_point(map);
 926    let row = point.row;
 927
 928    let desired_indent = map.line_indent_for_buffer_row(MultiBufferRow(row));
 929
 930    // Loop backwards until we find a non-blank line with less indent
 931    let mut start_row = row;
 932    for prev_row in (0..row).rev() {
 933        let indent = map.line_indent_for_buffer_row(MultiBufferRow(prev_row));
 934        if indent.is_line_empty() {
 935            continue;
 936        }
 937        if indent.spaces < desired_indent.spaces || indent.tabs < desired_indent.tabs {
 938            if around {
 939                // When around is true, include the first line with less indent
 940                start_row = prev_row;
 941            }
 942            break;
 943        }
 944        start_row = prev_row;
 945    }
 946
 947    // Loop forwards until we find a non-blank line with less indent
 948    let mut end_row = row;
 949    let max_rows = map.buffer_snapshot.max_row().0;
 950    for next_row in (row + 1)..=max_rows {
 951        let indent = map.line_indent_for_buffer_row(MultiBufferRow(next_row));
 952        if indent.is_line_empty() {
 953            continue;
 954        }
 955        if indent.spaces < desired_indent.spaces || indent.tabs < desired_indent.tabs {
 956            if around && include_below {
 957                // When around is true and including below, include this line
 958                end_row = next_row;
 959            }
 960            break;
 961        }
 962        end_row = next_row;
 963    }
 964
 965    let end_len = map.buffer_snapshot.line_len(MultiBufferRow(end_row));
 966    let start = map.point_to_display_point(Point::new(start_row, 0), Bias::Right);
 967    let end = map.point_to_display_point(Point::new(end_row, end_len), Bias::Left);
 968    Some(start..end)
 969}
 970
 971fn sentence(
 972    map: &DisplaySnapshot,
 973    relative_to: DisplayPoint,
 974    around: bool,
 975) -> Option<Range<DisplayPoint>> {
 976    let mut start = None;
 977    let relative_offset = relative_to.to_offset(map, Bias::Left);
 978    let mut previous_end = relative_offset;
 979
 980    let mut chars = map.buffer_chars_at(previous_end).peekable();
 981
 982    // Search backwards for the previous sentence end or current sentence start. Include the character under relative_to
 983    for (char, offset) in chars
 984        .peek()
 985        .cloned()
 986        .into_iter()
 987        .chain(map.reverse_buffer_chars_at(previous_end))
 988    {
 989        if is_sentence_end(map, offset) {
 990            break;
 991        }
 992
 993        if is_possible_sentence_start(char) {
 994            start = Some(offset);
 995        }
 996
 997        previous_end = offset;
 998    }
 999
1000    // Search forward for the end of the current sentence or if we are between sentences, the start of the next one
1001    let mut end = relative_offset;
1002    for (char, offset) in chars {
1003        if start.is_none() && is_possible_sentence_start(char) {
1004            if around {
1005                start = Some(offset);
1006                continue;
1007            } else {
1008                end = offset;
1009                break;
1010            }
1011        }
1012
1013        if char != '\n' {
1014            end = offset + char.len_utf8();
1015        }
1016
1017        if is_sentence_end(map, end) {
1018            break;
1019        }
1020    }
1021
1022    let mut range = start.unwrap_or(previous_end).to_display_point(map)..end.to_display_point(map);
1023    if around {
1024        range = expand_to_include_whitespace(map, range, false);
1025    }
1026
1027    Some(range)
1028}
1029
1030fn is_possible_sentence_start(character: char) -> bool {
1031    !character.is_whitespace() && character != '.'
1032}
1033
1034const SENTENCE_END_PUNCTUATION: &[char] = &['.', '!', '?'];
1035const SENTENCE_END_FILLERS: &[char] = &[')', ']', '"', '\''];
1036const SENTENCE_END_WHITESPACE: &[char] = &[' ', '\t', '\n'];
1037fn is_sentence_end(map: &DisplaySnapshot, offset: usize) -> bool {
1038    let mut next_chars = map.buffer_chars_at(offset).peekable();
1039    if let Some((char, _)) = next_chars.next() {
1040        // We are at a double newline. This position is a sentence end.
1041        if char == '\n' && next_chars.peek().map(|(c, _)| c == &'\n').unwrap_or(false) {
1042            return true;
1043        }
1044
1045        // The next text is not a valid whitespace. This is not a sentence end
1046        if !SENTENCE_END_WHITESPACE.contains(&char) {
1047            return false;
1048        }
1049    }
1050
1051    for (char, _) in map.reverse_buffer_chars_at(offset) {
1052        if SENTENCE_END_PUNCTUATION.contains(&char) {
1053            return true;
1054        }
1055
1056        if !SENTENCE_END_FILLERS.contains(&char) {
1057            return false;
1058        }
1059    }
1060
1061    false
1062}
1063
1064/// Expands the passed range to include whitespace on one side or the other in a line. Attempts to add the
1065/// whitespace to the end first and falls back to the start if there was none.
1066fn expand_to_include_whitespace(
1067    map: &DisplaySnapshot,
1068    range: Range<DisplayPoint>,
1069    stop_at_newline: bool,
1070) -> Range<DisplayPoint> {
1071    let mut range = range.start.to_offset(map, Bias::Left)..range.end.to_offset(map, Bias::Right);
1072    let mut whitespace_included = false;
1073
1074    let chars = map.buffer_chars_at(range.end).peekable();
1075    for (char, offset) in chars {
1076        if char == '\n' && stop_at_newline {
1077            break;
1078        }
1079
1080        if char.is_whitespace() {
1081            if char != '\n' {
1082                range.end = offset + char.len_utf8();
1083                whitespace_included = true;
1084            }
1085        } else {
1086            // Found non whitespace. Quit out.
1087            break;
1088        }
1089    }
1090
1091    if !whitespace_included {
1092        for (char, point) in map.reverse_buffer_chars_at(range.start) {
1093            if char == '\n' && stop_at_newline {
1094                break;
1095            }
1096
1097            if !char.is_whitespace() {
1098                break;
1099            }
1100
1101            range.start = point;
1102        }
1103    }
1104
1105    range.start.to_display_point(map)..range.end.to_display_point(map)
1106}
1107
1108/// If not `around` (i.e. inner), returns a range that surrounds the paragraph
1109/// where `relative_to` is in. If `around`, principally returns the range ending
1110/// at the end of the next paragraph.
1111///
1112/// Here, the "paragraph" is defined as a block of non-blank lines or a block of
1113/// blank lines. If the paragraph ends with a trailing newline (i.e. not with
1114/// EOF), the returned range ends at the trailing newline of the paragraph (i.e.
1115/// the trailing newline is not subject to subsequent operations).
1116///
1117/// Edge cases:
1118/// - If `around` and if the current paragraph is the last paragraph of the
1119///   file and is blank, then the selection results in an error.
1120/// - If `around` and if the current paragraph is the last paragraph of the
1121///   file and is not blank, then the returned range starts at the start of the
1122///   previous paragraph, if it exists.
1123fn paragraph(
1124    map: &DisplaySnapshot,
1125    relative_to: DisplayPoint,
1126    around: bool,
1127) -> Option<Range<DisplayPoint>> {
1128    let mut paragraph_start = start_of_paragraph(map, relative_to);
1129    let mut paragraph_end = end_of_paragraph(map, relative_to);
1130
1131    let paragraph_end_row = paragraph_end.row();
1132    let paragraph_ends_with_eof = paragraph_end_row == map.max_point().row();
1133    let point = relative_to.to_point(map);
1134    let current_line_is_empty = map.buffer_snapshot.is_line_blank(MultiBufferRow(point.row));
1135
1136    if around {
1137        if paragraph_ends_with_eof {
1138            if current_line_is_empty {
1139                return None;
1140            }
1141
1142            let paragraph_start_row = paragraph_start.row();
1143            if paragraph_start_row.0 != 0 {
1144                let previous_paragraph_last_line_start =
1145                    DisplayPoint::new(paragraph_start_row - 1, 0);
1146                paragraph_start = start_of_paragraph(map, previous_paragraph_last_line_start);
1147            }
1148        } else {
1149            let next_paragraph_start = DisplayPoint::new(paragraph_end_row + 1, 0);
1150            paragraph_end = end_of_paragraph(map, next_paragraph_start);
1151        }
1152    }
1153
1154    let range = paragraph_start..paragraph_end;
1155    Some(range)
1156}
1157
1158/// Returns a position of the start of the current paragraph, where a paragraph
1159/// is defined as a run of non-blank lines or a run of blank lines.
1160pub fn start_of_paragraph(map: &DisplaySnapshot, display_point: DisplayPoint) -> DisplayPoint {
1161    let point = display_point.to_point(map);
1162    if point.row == 0 {
1163        return DisplayPoint::zero();
1164    }
1165
1166    let is_current_line_blank = map.buffer_snapshot.is_line_blank(MultiBufferRow(point.row));
1167
1168    for row in (0..point.row).rev() {
1169        let blank = map.buffer_snapshot.is_line_blank(MultiBufferRow(row));
1170        if blank != is_current_line_blank {
1171            return Point::new(row + 1, 0).to_display_point(map);
1172        }
1173    }
1174
1175    DisplayPoint::zero()
1176}
1177
1178/// Returns a position of the end of the current paragraph, where a paragraph
1179/// is defined as a run of non-blank lines or a run of blank lines.
1180/// The trailing newline is excluded from the paragraph.
1181pub fn end_of_paragraph(map: &DisplaySnapshot, display_point: DisplayPoint) -> DisplayPoint {
1182    let point = display_point.to_point(map);
1183    if point.row == map.buffer_snapshot.max_row().0 {
1184        return map.max_point();
1185    }
1186
1187    let is_current_line_blank = map.buffer_snapshot.is_line_blank(MultiBufferRow(point.row));
1188
1189    for row in point.row + 1..map.buffer_snapshot.max_row().0 + 1 {
1190        let blank = map.buffer_snapshot.is_line_blank(MultiBufferRow(row));
1191        if blank != is_current_line_blank {
1192            let previous_row = row - 1;
1193            return Point::new(
1194                previous_row,
1195                map.buffer_snapshot.line_len(MultiBufferRow(previous_row)),
1196            )
1197            .to_display_point(map);
1198        }
1199    }
1200
1201    map.max_point()
1202}
1203
1204fn surrounding_markers(
1205    map: &DisplaySnapshot,
1206    relative_to: DisplayPoint,
1207    around: bool,
1208    search_across_lines: bool,
1209    open_marker: char,
1210    close_marker: char,
1211) -> Option<Range<DisplayPoint>> {
1212    let point = relative_to.to_offset(map, Bias::Left);
1213
1214    let mut matched_closes = 0;
1215    let mut opening = None;
1216
1217    let mut before_ch = match movement::chars_before(map, point).next() {
1218        Some((ch, _)) => ch,
1219        _ => '\0',
1220    };
1221    if let Some((ch, range)) = movement::chars_after(map, point).next() {
1222        if ch == open_marker && before_ch != '\\' {
1223            if open_marker == close_marker {
1224                let mut total = 0;
1225                for ((ch, _), (before_ch, _)) in movement::chars_before(map, point).tuple_windows()
1226                {
1227                    if ch == '\n' {
1228                        break;
1229                    }
1230                    if ch == open_marker && before_ch != '\\' {
1231                        total += 1;
1232                    }
1233                }
1234                if total % 2 == 0 {
1235                    opening = Some(range)
1236                }
1237            } else {
1238                opening = Some(range)
1239            }
1240        }
1241    }
1242
1243    if opening.is_none() {
1244        let mut chars_before = movement::chars_before(map, point).peekable();
1245        while let Some((ch, range)) = chars_before.next() {
1246            if ch == '\n' && !search_across_lines {
1247                break;
1248            }
1249
1250            if let Some((before_ch, _)) = chars_before.peek() {
1251                if *before_ch == '\\' {
1252                    continue;
1253                }
1254            }
1255
1256            if ch == open_marker {
1257                if matched_closes == 0 {
1258                    opening = Some(range);
1259                    break;
1260                }
1261                matched_closes -= 1;
1262            } else if ch == close_marker {
1263                matched_closes += 1
1264            }
1265        }
1266    }
1267    if opening.is_none() {
1268        for (ch, range) in movement::chars_after(map, point) {
1269            if before_ch != '\\' {
1270                if ch == open_marker {
1271                    opening = Some(range);
1272                    break;
1273                } else if ch == close_marker {
1274                    break;
1275                }
1276            }
1277
1278            before_ch = ch;
1279        }
1280    }
1281
1282    let mut opening = opening?;
1283
1284    let mut matched_opens = 0;
1285    let mut closing = None;
1286    before_ch = match movement::chars_before(map, opening.end).next() {
1287        Some((ch, _)) => ch,
1288        _ => '\0',
1289    };
1290    for (ch, range) in movement::chars_after(map, opening.end) {
1291        if ch == '\n' && !search_across_lines {
1292            break;
1293        }
1294
1295        if before_ch != '\\' {
1296            if ch == close_marker {
1297                if matched_opens == 0 {
1298                    closing = Some(range);
1299                    break;
1300                }
1301                matched_opens -= 1;
1302            } else if ch == open_marker {
1303                matched_opens += 1;
1304            }
1305        }
1306
1307        before_ch = ch;
1308    }
1309
1310    let mut closing = closing?;
1311
1312    if around && !search_across_lines {
1313        let mut found = false;
1314
1315        for (ch, range) in movement::chars_after(map, closing.end) {
1316            if ch.is_whitespace() && ch != '\n' {
1317                found = true;
1318                closing.end = range.end;
1319            } else {
1320                break;
1321            }
1322        }
1323
1324        if !found {
1325            for (ch, range) in movement::chars_before(map, opening.start) {
1326                if ch.is_whitespace() && ch != '\n' {
1327                    opening.start = range.start
1328                } else {
1329                    break;
1330                }
1331            }
1332        }
1333    }
1334
1335    if !around && search_across_lines {
1336        if let Some((ch, range)) = movement::chars_after(map, opening.end).next() {
1337            if ch == '\n' {
1338                opening.end = range.end
1339            }
1340        }
1341
1342        let mut last_newline_end = None;
1343        for (ch, range) in movement::chars_before(map, closing.start) {
1344            if !ch.is_whitespace() {
1345                break;
1346            }
1347            if ch == '\n' {
1348                last_newline_end = Some(range.end);
1349                break;
1350            }
1351        }
1352        // Adjust closing.start to exclude whitespace after a newline, if present
1353        if let Some(end) = last_newline_end {
1354            closing.start = end;
1355        }
1356    }
1357
1358    let result = if around {
1359        opening.start..closing.end
1360    } else {
1361        opening.end..closing.start
1362    };
1363
1364    Some(
1365        map.clip_point(result.start.to_display_point(map), Bias::Left)
1366            ..map.clip_point(result.end.to_display_point(map), Bias::Right),
1367    )
1368}
1369
1370#[cfg(test)]
1371mod test {
1372    use gpui::KeyBinding;
1373    use indoc::indoc;
1374
1375    use crate::{
1376        object::AnyBrackets,
1377        state::Mode,
1378        test::{NeovimBackedTestContext, VimTestContext},
1379    };
1380
1381    const WORD_LOCATIONS: &str = indoc! {"
1382        The quick ˇbrowˇnˇ•••
1383        fox ˇjuˇmpsˇ over
1384        the lazy dogˇ••
1385        ˇ
1386        ˇ
1387        ˇ
1388        Thˇeˇ-ˇquˇickˇ ˇbrownˇ•
1389        ˇ••
1390        ˇ••
1391        ˇ  fox-jumpˇs over
1392        the lazy dogˇ•
1393        ˇ
1394        "
1395    };
1396
1397    #[gpui::test]
1398    async fn test_change_word_object(cx: &mut gpui::TestAppContext) {
1399        let mut cx = NeovimBackedTestContext::new(cx).await;
1400
1401        cx.simulate_at_each_offset("c i w", WORD_LOCATIONS)
1402            .await
1403            .assert_matches();
1404        cx.simulate_at_each_offset("c i shift-w", WORD_LOCATIONS)
1405            .await
1406            .assert_matches();
1407        cx.simulate_at_each_offset("c a w", WORD_LOCATIONS)
1408            .await
1409            .assert_matches();
1410        cx.simulate_at_each_offset("c a shift-w", WORD_LOCATIONS)
1411            .await
1412            .assert_matches();
1413    }
1414
1415    #[gpui::test]
1416    async fn test_delete_word_object(cx: &mut gpui::TestAppContext) {
1417        let mut cx = NeovimBackedTestContext::new(cx).await;
1418
1419        cx.simulate_at_each_offset("d i w", WORD_LOCATIONS)
1420            .await
1421            .assert_matches();
1422        cx.simulate_at_each_offset("d i shift-w", WORD_LOCATIONS)
1423            .await
1424            .assert_matches();
1425        cx.simulate_at_each_offset("d a w", WORD_LOCATIONS)
1426            .await
1427            .assert_matches();
1428        cx.simulate_at_each_offset("d a shift-w", WORD_LOCATIONS)
1429            .await
1430            .assert_matches();
1431    }
1432
1433    #[gpui::test]
1434    async fn test_visual_word_object(cx: &mut gpui::TestAppContext) {
1435        let mut cx = NeovimBackedTestContext::new(cx).await;
1436
1437        /*
1438                cx.set_shared_state("The quick ˇbrown\nfox").await;
1439                cx.simulate_shared_keystrokes(["v"]).await;
1440                cx.assert_shared_state("The quick «bˇ»rown\nfox").await;
1441                cx.simulate_shared_keystrokes(["i", "w"]).await;
1442                cx.assert_shared_state("The quick «brownˇ»\nfox").await;
1443        */
1444        cx.set_shared_state("The quick brown\nˇ\nfox").await;
1445        cx.simulate_shared_keystrokes("v").await;
1446        cx.shared_state()
1447            .await
1448            .assert_eq("The quick brown\n«\nˇ»fox");
1449        cx.simulate_shared_keystrokes("i w").await;
1450        cx.shared_state()
1451            .await
1452            .assert_eq("The quick brown\n«\nˇ»fox");
1453
1454        cx.simulate_at_each_offset("v i w", WORD_LOCATIONS)
1455            .await
1456            .assert_matches();
1457        cx.simulate_at_each_offset("v i shift-w", WORD_LOCATIONS)
1458            .await
1459            .assert_matches();
1460    }
1461
1462    const PARAGRAPH_EXAMPLES: &[&str] = &[
1463        // Single line
1464        "ˇThe quick brown fox jumpˇs over the lazy dogˇ.ˇ",
1465        // Multiple lines without empty lines
1466        indoc! {"
1467            ˇThe quick brownˇ
1468            ˇfox jumps overˇ
1469            the lazy dog.ˇ
1470        "},
1471        // Heading blank paragraph and trailing normal paragraph
1472        indoc! {"
1473            ˇ
1474            ˇ
1475            ˇThe quick brown fox jumps
1476            ˇover the lazy dog.
1477            ˇ
1478            ˇ
1479            ˇThe quick brown fox jumpsˇ
1480            ˇover the lazy dog.ˇ
1481        "},
1482        // Inserted blank paragraph and trailing blank paragraph
1483        indoc! {"
1484            ˇThe quick brown fox jumps
1485            ˇover the lazy dog.
1486            ˇ
1487            ˇ
1488            ˇ
1489            ˇThe quick brown fox jumpsˇ
1490            ˇover the lazy dog.ˇ
1491            ˇ
1492            ˇ
1493            ˇ
1494        "},
1495        // "Blank" paragraph with whitespace characters
1496        indoc! {"
1497            ˇThe quick brown fox jumps
1498            over the lazy dog.
1499
1500            ˇ \t
1501
1502            ˇThe quick brown fox jumps
1503            over the lazy dog.ˇ
1504            ˇ
1505            ˇ \t
1506            \t \t
1507        "},
1508        // Single line "paragraphs", where selection size might be zero.
1509        indoc! {"
1510            ˇThe quick brown fox jumps over the lazy dog.
1511            ˇ
1512            ˇThe quick brown fox jumpˇs over the lazy dog.ˇ
1513            ˇ
1514        "},
1515    ];
1516
1517    #[gpui::test]
1518    async fn test_change_paragraph_object(cx: &mut gpui::TestAppContext) {
1519        let mut cx = NeovimBackedTestContext::new(cx).await;
1520
1521        for paragraph_example in PARAGRAPH_EXAMPLES {
1522            cx.simulate_at_each_offset("c i p", paragraph_example)
1523                .await
1524                .assert_matches();
1525            cx.simulate_at_each_offset("c a p", paragraph_example)
1526                .await
1527                .assert_matches();
1528        }
1529    }
1530
1531    #[gpui::test]
1532    async fn test_delete_paragraph_object(cx: &mut gpui::TestAppContext) {
1533        let mut cx = NeovimBackedTestContext::new(cx).await;
1534
1535        for paragraph_example in PARAGRAPH_EXAMPLES {
1536            cx.simulate_at_each_offset("d i p", paragraph_example)
1537                .await
1538                .assert_matches();
1539            cx.simulate_at_each_offset("d a p", paragraph_example)
1540                .await
1541                .assert_matches();
1542        }
1543    }
1544
1545    #[gpui::test]
1546    async fn test_visual_paragraph_object(cx: &mut gpui::TestAppContext) {
1547        let mut cx = NeovimBackedTestContext::new(cx).await;
1548
1549        const EXAMPLES: &[&str] = &[
1550            indoc! {"
1551                ˇThe quick brown
1552                fox jumps over
1553                the lazy dog.
1554            "},
1555            indoc! {"
1556                ˇ
1557
1558                ˇThe quick brown fox jumps
1559                over the lazy dog.
1560                ˇ
1561
1562                ˇThe quick brown fox jumps
1563                over the lazy dog.
1564            "},
1565            indoc! {"
1566                ˇThe quick brown fox jumps over the lazy dog.
1567                ˇ
1568                ˇThe quick brown fox jumps over the lazy dog.
1569
1570            "},
1571        ];
1572
1573        for paragraph_example in EXAMPLES {
1574            cx.simulate_at_each_offset("v i p", paragraph_example)
1575                .await
1576                .assert_matches();
1577            cx.simulate_at_each_offset("v a p", paragraph_example)
1578                .await
1579                .assert_matches();
1580        }
1581    }
1582
1583    // Test string with "`" for opening surrounders and "'" for closing surrounders
1584    const SURROUNDING_MARKER_STRING: &str = indoc! {"
1585        ˇTh'ˇe ˇ`ˇ'ˇquˇi`ˇck broˇ'wn`
1586        'ˇfox juˇmps ov`ˇer
1587        the ˇlazy d'o`ˇg"};
1588
1589    const SURROUNDING_OBJECTS: &[(char, char)] = &[
1590        ('"', '"'), // Double Quote
1591        ('(', ')'), // Parentheses
1592    ];
1593
1594    #[gpui::test]
1595    async fn test_change_surrounding_character_objects(cx: &mut gpui::TestAppContext) {
1596        let mut cx = NeovimBackedTestContext::new(cx).await;
1597
1598        for (start, end) in SURROUNDING_OBJECTS {
1599            let marked_string = SURROUNDING_MARKER_STRING
1600                .replace('`', &start.to_string())
1601                .replace('\'', &end.to_string());
1602
1603            cx.simulate_at_each_offset(&format!("c i {start}"), &marked_string)
1604                .await
1605                .assert_matches();
1606            cx.simulate_at_each_offset(&format!("c i {end}"), &marked_string)
1607                .await
1608                .assert_matches();
1609            cx.simulate_at_each_offset(&format!("c a {start}"), &marked_string)
1610                .await
1611                .assert_matches();
1612            cx.simulate_at_each_offset(&format!("c a {end}"), &marked_string)
1613                .await
1614                .assert_matches();
1615        }
1616    }
1617    #[gpui::test]
1618    async fn test_singleline_surrounding_character_objects(cx: &mut gpui::TestAppContext) {
1619        let mut cx = NeovimBackedTestContext::new(cx).await;
1620        cx.set_shared_wrap(12).await;
1621
1622        cx.set_shared_state(indoc! {
1623            "\"ˇhello world\"!"
1624        })
1625        .await;
1626        cx.simulate_shared_keystrokes("v i \"").await;
1627        cx.shared_state().await.assert_eq(indoc! {
1628            "\"«hello worldˇ»\"!"
1629        });
1630
1631        cx.set_shared_state(indoc! {
1632            "\"hˇello world\"!"
1633        })
1634        .await;
1635        cx.simulate_shared_keystrokes("v i \"").await;
1636        cx.shared_state().await.assert_eq(indoc! {
1637            "\"«hello worldˇ»\"!"
1638        });
1639
1640        cx.set_shared_state(indoc! {
1641            "helˇlo \"world\"!"
1642        })
1643        .await;
1644        cx.simulate_shared_keystrokes("v i \"").await;
1645        cx.shared_state().await.assert_eq(indoc! {
1646            "hello \"«worldˇ»\"!"
1647        });
1648
1649        cx.set_shared_state(indoc! {
1650            "hello \"wˇorld\"!"
1651        })
1652        .await;
1653        cx.simulate_shared_keystrokes("v i \"").await;
1654        cx.shared_state().await.assert_eq(indoc! {
1655            "hello \"«worldˇ»\"!"
1656        });
1657
1658        cx.set_shared_state(indoc! {
1659            "hello \"wˇorld\"!"
1660        })
1661        .await;
1662        cx.simulate_shared_keystrokes("v a \"").await;
1663        cx.shared_state().await.assert_eq(indoc! {
1664            "hello« \"world\"ˇ»!"
1665        });
1666
1667        cx.set_shared_state(indoc! {
1668            "hello \"wˇorld\" !"
1669        })
1670        .await;
1671        cx.simulate_shared_keystrokes("v a \"").await;
1672        cx.shared_state().await.assert_eq(indoc! {
1673            "hello «\"world\" ˇ»!"
1674        });
1675
1676        cx.set_shared_state(indoc! {
1677            "hello \"wˇorld\"1678            goodbye"
1679        })
1680        .await;
1681        cx.simulate_shared_keystrokes("v a \"").await;
1682        cx.shared_state().await.assert_eq(indoc! {
1683            "hello «\"world\" ˇ»
1684            goodbye"
1685        });
1686    }
1687
1688    #[gpui::test]
1689    async fn test_multiline_surrounding_character_objects(cx: &mut gpui::TestAppContext) {
1690        let mut cx = NeovimBackedTestContext::new(cx).await;
1691
1692        cx.set_shared_state(indoc! {
1693            "func empty(a string) bool {
1694               if a == \"\" {
1695                  return true
1696               }
1697               ˇreturn false
1698            }"
1699        })
1700        .await;
1701        cx.simulate_shared_keystrokes("v i {").await;
1702        cx.shared_state().await.assert_eq(indoc! {"
1703            func empty(a string) bool {
1704            «   if a == \"\" {
1705                  return true
1706               }
1707               return false
1708            ˇ»}"});
1709        cx.set_shared_state(indoc! {
1710            "func empty(a string) bool {
1711                 if a == \"\" {
1712                     ˇreturn true
1713                 }
1714                 return false
1715            }"
1716        })
1717        .await;
1718        cx.simulate_shared_keystrokes("v i {").await;
1719        cx.shared_state().await.assert_eq(indoc! {"
1720            func empty(a string) bool {
1721                 if a == \"\" {
1722            «         return true
1723            ˇ»     }
1724                 return false
1725            }"});
1726
1727        cx.set_shared_state(indoc! {
1728            "func empty(a string) bool {
1729                 if a == \"\" ˇ{
1730                     return true
1731                 }
1732                 return false
1733            }"
1734        })
1735        .await;
1736        cx.simulate_shared_keystrokes("v i {").await;
1737        cx.shared_state().await.assert_eq(indoc! {"
1738            func empty(a string) bool {
1739                 if a == \"\" {
1740            «         return true
1741            ˇ»     }
1742                 return false
1743            }"});
1744    }
1745
1746    #[gpui::test]
1747    async fn test_singleline_surrounding_character_objects_with_escape(
1748        cx: &mut gpui::TestAppContext,
1749    ) {
1750        let mut cx = NeovimBackedTestContext::new(cx).await;
1751        cx.set_shared_state(indoc! {
1752            "h\"e\\\"lˇlo \\\"world\"!"
1753        })
1754        .await;
1755        cx.simulate_shared_keystrokes("v i \"").await;
1756        cx.shared_state().await.assert_eq(indoc! {
1757            "h\"«e\\\"llo \\\"worldˇ»\"!"
1758        });
1759
1760        cx.set_shared_state(indoc! {
1761            "hello \"teˇst \\\"inside\\\" world\""
1762        })
1763        .await;
1764        cx.simulate_shared_keystrokes("v i \"").await;
1765        cx.shared_state().await.assert_eq(indoc! {
1766            "hello \"«test \\\"inside\\\" worldˇ»\""
1767        });
1768    }
1769
1770    #[gpui::test]
1771    async fn test_vertical_bars(cx: &mut gpui::TestAppContext) {
1772        let mut cx = VimTestContext::new(cx, true).await;
1773        cx.set_state(
1774            indoc! {"
1775            fn boop() {
1776                baz(ˇ|a, b| { bar(|j, k| { })})
1777            }"
1778            },
1779            Mode::Normal,
1780        );
1781        cx.simulate_keystrokes("c i |");
1782        cx.assert_state(
1783            indoc! {"
1784            fn boop() {
1785                baz(|ˇ| { bar(|j, k| { })})
1786            }"
1787            },
1788            Mode::Insert,
1789        );
1790        cx.simulate_keystrokes("escape 1 8 |");
1791        cx.assert_state(
1792            indoc! {"
1793            fn boop() {
1794                baz(|| { bar(ˇ|j, k| { })})
1795            }"
1796            },
1797            Mode::Normal,
1798        );
1799
1800        cx.simulate_keystrokes("v a |");
1801        cx.assert_state(
1802            indoc! {"
1803            fn boop() {
1804                baz(|| { bar(«|j, k| ˇ»{ })})
1805            }"
1806            },
1807            Mode::Visual,
1808        );
1809    }
1810
1811    #[gpui::test]
1812    async fn test_argument_object(cx: &mut gpui::TestAppContext) {
1813        let mut cx = VimTestContext::new(cx, true).await;
1814
1815        // Generic arguments
1816        cx.set_state("fn boop<A: ˇDebug, B>() {}", Mode::Normal);
1817        cx.simulate_keystrokes("v i a");
1818        cx.assert_state("fn boop<«A: Debugˇ», B>() {}", Mode::Visual);
1819
1820        // Function arguments
1821        cx.set_state(
1822            "fn boop(ˇarg_a: (Tuple, Of, Types), arg_b: String) {}",
1823            Mode::Normal,
1824        );
1825        cx.simulate_keystrokes("d a a");
1826        cx.assert_state("fn boop(ˇarg_b: String) {}", Mode::Normal);
1827
1828        cx.set_state("std::namespace::test(\"strinˇg\", a.b.c())", Mode::Normal);
1829        cx.simulate_keystrokes("v a a");
1830        cx.assert_state("std::namespace::test(«\"string\", ˇ»a.b.c())", Mode::Visual);
1831
1832        // Tuple, vec, and array arguments
1833        cx.set_state(
1834            "fn boop(arg_a: (Tuple, Ofˇ, Types), arg_b: String) {}",
1835            Mode::Normal,
1836        );
1837        cx.simulate_keystrokes("c i a");
1838        cx.assert_state(
1839            "fn boop(arg_a: (Tuple, ˇ, Types), arg_b: String) {}",
1840            Mode::Insert,
1841        );
1842
1843        cx.set_state("let a = (test::call(), 'p', my_macro!{ˇ});", Mode::Normal);
1844        cx.simulate_keystrokes("c a a");
1845        cx.assert_state("let a = (test::call(), 'p'ˇ);", Mode::Insert);
1846
1847        cx.set_state("let a = [test::call(ˇ), 300];", Mode::Normal);
1848        cx.simulate_keystrokes("c i a");
1849        cx.assert_state("let a = [ˇ, 300];", Mode::Insert);
1850
1851        cx.set_state(
1852            "let a = vec![Vec::new(), vecˇ![test::call(), 300]];",
1853            Mode::Normal,
1854        );
1855        cx.simulate_keystrokes("c a a");
1856        cx.assert_state("let a = vec![Vec::new()ˇ];", Mode::Insert);
1857
1858        // Cursor immediately before / after brackets
1859        cx.set_state("let a = [test::call(first_arg)ˇ]", Mode::Normal);
1860        cx.simulate_keystrokes("v i a");
1861        cx.assert_state("let a = [«test::call(first_arg)ˇ»]", Mode::Visual);
1862
1863        cx.set_state("let a = [test::callˇ(first_arg)]", Mode::Normal);
1864        cx.simulate_keystrokes("v i a");
1865        cx.assert_state("let a = [«test::call(first_arg)ˇ»]", Mode::Visual);
1866    }
1867
1868    #[gpui::test]
1869    async fn test_indent_object(cx: &mut gpui::TestAppContext) {
1870        let mut cx = VimTestContext::new(cx, true).await;
1871
1872        // Base use case
1873        cx.set_state(
1874            indoc! {"
1875                fn boop() {
1876                    // Comment
1877                    baz();ˇ
1878
1879                    loop {
1880                        bar(1);
1881                        bar(2);
1882                    }
1883
1884                    result
1885                }
1886            "},
1887            Mode::Normal,
1888        );
1889        cx.simulate_keystrokes("v i i");
1890        cx.assert_state(
1891            indoc! {"
1892                fn boop() {
1893                «    // Comment
1894                    baz();
1895
1896                    loop {
1897                        bar(1);
1898                        bar(2);
1899                    }
1900
1901                    resultˇ»
1902                }
1903            "},
1904            Mode::Visual,
1905        );
1906
1907        // Around indent (include line above)
1908        cx.set_state(
1909            indoc! {"
1910                const ABOVE: str = true;
1911                fn boop() {
1912
1913                    hello();
1914                    worˇld()
1915                }
1916            "},
1917            Mode::Normal,
1918        );
1919        cx.simulate_keystrokes("v a i");
1920        cx.assert_state(
1921            indoc! {"
1922                const ABOVE: str = true;
1923                «fn boop() {
1924
1925                    hello();
1926                    world()ˇ»
1927                }
1928            "},
1929            Mode::Visual,
1930        );
1931
1932        // Around indent (include line above & below)
1933        cx.set_state(
1934            indoc! {"
1935                const ABOVE: str = true;
1936                fn boop() {
1937                    hellˇo();
1938                    world()
1939
1940                }
1941                const BELOW: str = true;
1942            "},
1943            Mode::Normal,
1944        );
1945        cx.simulate_keystrokes("c a shift-i");
1946        cx.assert_state(
1947            indoc! {"
1948                const ABOVE: str = true;
1949                ˇ
1950                const BELOW: str = true;
1951            "},
1952            Mode::Insert,
1953        );
1954    }
1955
1956    #[gpui::test]
1957    async fn test_delete_surrounding_character_objects(cx: &mut gpui::TestAppContext) {
1958        let mut cx = NeovimBackedTestContext::new(cx).await;
1959
1960        for (start, end) in SURROUNDING_OBJECTS {
1961            let marked_string = SURROUNDING_MARKER_STRING
1962                .replace('`', &start.to_string())
1963                .replace('\'', &end.to_string());
1964
1965            cx.simulate_at_each_offset(&format!("d i {start}"), &marked_string)
1966                .await
1967                .assert_matches();
1968            cx.simulate_at_each_offset(&format!("d i {end}"), &marked_string)
1969                .await
1970                .assert_matches();
1971            cx.simulate_at_each_offset(&format!("d a {start}"), &marked_string)
1972                .await
1973                .assert_matches();
1974            cx.simulate_at_each_offset(&format!("d a {end}"), &marked_string)
1975                .await
1976                .assert_matches();
1977        }
1978    }
1979
1980    #[gpui::test]
1981    async fn test_anyquotes_object(cx: &mut gpui::TestAppContext) {
1982        let mut cx = VimTestContext::new(cx, true).await;
1983
1984        const TEST_CASES: &[(&str, &str, &str, Mode)] = &[
1985            // Single quotes
1986            (
1987                "c i q",
1988                "Thisˇ is a 'quote' example.",
1989                "This is a 'ˇ' example.",
1990                Mode::Insert,
1991            ),
1992            (
1993                "c a q",
1994                "Thisˇ is a 'quote' example.",
1995                "This is a ˇexample.",
1996                Mode::Insert,
1997            ),
1998            (
1999                "c i q",
2000                "This is a \"simple 'qˇuote'\" example.",
2001                "This is a \"simple 'ˇ'\" example.",
2002                Mode::Insert,
2003            ),
2004            (
2005                "c a q",
2006                "This is a \"simple 'qˇuote'\" example.",
2007                "This is a \"simpleˇ\" example.",
2008                Mode::Insert,
2009            ),
2010            (
2011                "c i q",
2012                "This is a 'qˇuote' example.",
2013                "This is a 'ˇ' example.",
2014                Mode::Insert,
2015            ),
2016            (
2017                "c a q",
2018                "This is a 'qˇuote' example.",
2019                "This is a ˇexample.",
2020                Mode::Insert,
2021            ),
2022            (
2023                "d i q",
2024                "This is a 'qˇuote' example.",
2025                "This is a 'ˇ' example.",
2026                Mode::Normal,
2027            ),
2028            (
2029                "d a q",
2030                "This is a 'qˇuote' example.",
2031                "This is a ˇexample.",
2032                Mode::Normal,
2033            ),
2034            // Double quotes
2035            (
2036                "c i q",
2037                "This is a \"qˇuote\" example.",
2038                "This is a \"ˇ\" example.",
2039                Mode::Insert,
2040            ),
2041            (
2042                "c a q",
2043                "This is a \"qˇuote\" example.",
2044                "This is a ˇexample.",
2045                Mode::Insert,
2046            ),
2047            (
2048                "d i q",
2049                "This is a \"qˇuote\" example.",
2050                "This is a \"ˇ\" example.",
2051                Mode::Normal,
2052            ),
2053            (
2054                "d a q",
2055                "This is a \"qˇuote\" example.",
2056                "This is a ˇexample.",
2057                Mode::Normal,
2058            ),
2059            // Back quotes
2060            (
2061                "c i q",
2062                "This is a `qˇuote` example.",
2063                "This is a `ˇ` example.",
2064                Mode::Insert,
2065            ),
2066            (
2067                "c a q",
2068                "This is a `qˇuote` example.",
2069                "This is a ˇexample.",
2070                Mode::Insert,
2071            ),
2072            (
2073                "d i q",
2074                "This is a `qˇuote` example.",
2075                "This is a `ˇ` example.",
2076                Mode::Normal,
2077            ),
2078            (
2079                "d a q",
2080                "This is a `qˇuote` example.",
2081                "This is a ˇexample.",
2082                Mode::Normal,
2083            ),
2084        ];
2085
2086        for (keystrokes, initial_state, expected_state, expected_mode) in TEST_CASES {
2087            cx.set_state(initial_state, Mode::Normal);
2088
2089            cx.simulate_keystrokes(keystrokes);
2090
2091            cx.assert_state(expected_state, *expected_mode);
2092        }
2093
2094        const INVALID_CASES: &[(&str, &str, Mode)] = &[
2095            ("c i q", "this is a 'qˇuote example.", Mode::Normal), // Missing closing simple quote
2096            ("c a q", "this is a 'qˇuote example.", Mode::Normal), // Missing closing simple quote
2097            ("d i q", "this is a 'qˇuote example.", Mode::Normal), // Missing closing simple quote
2098            ("d a q", "this is a 'qˇuote example.", Mode::Normal), // Missing closing simple quote
2099            ("c i q", "this is a \"qˇuote example.", Mode::Normal), // Missing closing double quote
2100            ("c a q", "this is a \"qˇuote example.", Mode::Normal), // Missing closing double quote
2101            ("d i q", "this is a \"qˇuote example.", Mode::Normal), // Missing closing double quote
2102            ("d a q", "this is a \"qˇuote example.", Mode::Normal), // Missing closing back quote
2103            ("c i q", "this is a `qˇuote example.", Mode::Normal), // Missing closing back quote
2104            ("c a q", "this is a `qˇuote example.", Mode::Normal), // Missing closing back quote
2105            ("d i q", "this is a `qˇuote example.", Mode::Normal), // Missing closing back quote
2106            ("d a q", "this is a `qˇuote example.", Mode::Normal), // Missing closing back quote
2107        ];
2108
2109        for (keystrokes, initial_state, mode) in INVALID_CASES {
2110            cx.set_state(initial_state, Mode::Normal);
2111
2112            cx.simulate_keystrokes(keystrokes);
2113
2114            cx.assert_state(initial_state, *mode);
2115        }
2116    }
2117
2118    #[gpui::test]
2119    async fn test_anybrackets_object(cx: &mut gpui::TestAppContext) {
2120        let mut cx = VimTestContext::new(cx, true).await;
2121        cx.update(|_, cx| {
2122            cx.bind_keys([KeyBinding::new(
2123                "b",
2124                AnyBrackets,
2125                Some("vim_operator == a || vim_operator == i || vim_operator == cs"),
2126            )]);
2127        });
2128
2129        const TEST_CASES: &[(&str, &str, &str, Mode)] = &[
2130            // Bracket (Parentheses)
2131            (
2132                "c i b",
2133                "Thisˇ is a (simple [quote]) example.",
2134                "This is a (ˇ) example.",
2135                Mode::Insert,
2136            ),
2137            (
2138                "c i b",
2139                "This is a [simple (qˇuote)] example.",
2140                "This is a [simple (ˇ)] example.",
2141                Mode::Insert,
2142            ),
2143            (
2144                "c a b",
2145                "This is a [simple (qˇuote)] example.",
2146                "This is a [simple ˇ] example.",
2147                Mode::Insert,
2148            ),
2149            (
2150                "c a b",
2151                "Thisˇ is a (simple [quote]) example.",
2152                "This is a ˇ example.",
2153                Mode::Insert,
2154            ),
2155            (
2156                "c i b",
2157                "This is a (qˇuote) example.",
2158                "This is a (ˇ) example.",
2159                Mode::Insert,
2160            ),
2161            (
2162                "c a b",
2163                "This is a (qˇuote) example.",
2164                "This is a ˇ example.",
2165                Mode::Insert,
2166            ),
2167            (
2168                "d i b",
2169                "This is a (qˇuote) example.",
2170                "This is a (ˇ) example.",
2171                Mode::Normal,
2172            ),
2173            (
2174                "d a b",
2175                "This is a (qˇuote) example.",
2176                "This is a ˇ example.",
2177                Mode::Normal,
2178            ),
2179            // Square brackets
2180            (
2181                "c i b",
2182                "This is a [qˇuote] example.",
2183                "This is a [ˇ] example.",
2184                Mode::Insert,
2185            ),
2186            (
2187                "c a b",
2188                "This is a [qˇuote] example.",
2189                "This is a ˇ example.",
2190                Mode::Insert,
2191            ),
2192            (
2193                "d i b",
2194                "This is a [qˇuote] example.",
2195                "This is a [ˇ] example.",
2196                Mode::Normal,
2197            ),
2198            (
2199                "d a b",
2200                "This is a [qˇuote] example.",
2201                "This is a ˇ example.",
2202                Mode::Normal,
2203            ),
2204            // Curly brackets
2205            (
2206                "c i b",
2207                "This is a {qˇuote} example.",
2208                "This is a {ˇ} example.",
2209                Mode::Insert,
2210            ),
2211            (
2212                "c a b",
2213                "This is a {qˇuote} example.",
2214                "This is a ˇ example.",
2215                Mode::Insert,
2216            ),
2217            (
2218                "d i b",
2219                "This is a {qˇuote} example.",
2220                "This is a {ˇ} example.",
2221                Mode::Normal,
2222            ),
2223            (
2224                "d a b",
2225                "This is a {qˇuote} example.",
2226                "This is a ˇ example.",
2227                Mode::Normal,
2228            ),
2229        ];
2230
2231        for (keystrokes, initial_state, expected_state, expected_mode) in TEST_CASES {
2232            cx.set_state(initial_state, Mode::Normal);
2233
2234            cx.simulate_keystrokes(keystrokes);
2235
2236            cx.assert_state(expected_state, *expected_mode);
2237        }
2238
2239        const INVALID_CASES: &[(&str, &str, Mode)] = &[
2240            ("c i b", "this is a (qˇuote example.", Mode::Normal), // Missing closing bracket
2241            ("c a b", "this is a (qˇuote example.", Mode::Normal), // Missing closing bracket
2242            ("d i b", "this is a (qˇuote example.", Mode::Normal), // Missing closing bracket
2243            ("d a b", "this is a (qˇuote example.", Mode::Normal), // Missing closing bracket
2244            ("c i b", "this is a [qˇuote example.", Mode::Normal), // Missing closing square bracket
2245            ("c a b", "this is a [qˇuote example.", Mode::Normal), // Missing closing square bracket
2246            ("d i b", "this is a [qˇuote example.", Mode::Normal), // Missing closing square bracket
2247            ("d a b", "this is a [qˇuote example.", Mode::Normal), // Missing closing square bracket
2248            ("c i b", "this is a {qˇuote example.", Mode::Normal), // Missing closing curly bracket
2249            ("c a b", "this is a {qˇuote example.", Mode::Normal), // Missing closing curly bracket
2250            ("d i b", "this is a {qˇuote example.", Mode::Normal), // Missing closing curly bracket
2251            ("d a b", "this is a {qˇuote example.", Mode::Normal), // Missing closing curly bracket
2252        ];
2253
2254        for (keystrokes, initial_state, mode) in INVALID_CASES {
2255            cx.set_state(initial_state, Mode::Normal);
2256
2257            cx.simulate_keystrokes(keystrokes);
2258
2259            cx.assert_state(initial_state, *mode);
2260        }
2261    }
2262
2263    #[gpui::test]
2264    async fn test_anybrackets_trailing_space(cx: &mut gpui::TestAppContext) {
2265        let mut cx = NeovimBackedTestContext::new(cx).await;
2266
2267        cx.set_shared_state("(trailingˇ whitespace          )")
2268            .await;
2269        cx.simulate_shared_keystrokes("v i b").await;
2270        cx.shared_state().await.assert_matches();
2271        cx.simulate_shared_keystrokes("escape y i b").await;
2272        cx.shared_clipboard()
2273            .await
2274            .assert_eq("trailing whitespace          ");
2275    }
2276
2277    #[gpui::test]
2278    async fn test_tags(cx: &mut gpui::TestAppContext) {
2279        let mut cx = VimTestContext::new_html(cx).await;
2280
2281        cx.set_state("<html><head></head><body><b>hˇi!</b></body>", Mode::Normal);
2282        cx.simulate_keystrokes("v i t");
2283        cx.assert_state(
2284            "<html><head></head><body><b>«hi!ˇ»</b></body>",
2285            Mode::Visual,
2286        );
2287        cx.simulate_keystrokes("a t");
2288        cx.assert_state(
2289            "<html><head></head><body>«<b>hi!</b>ˇ»</body>",
2290            Mode::Visual,
2291        );
2292        cx.simulate_keystrokes("a t");
2293        cx.assert_state(
2294            "<html><head></head>«<body><b>hi!</b></body>ˇ»",
2295            Mode::Visual,
2296        );
2297
2298        // The cursor is before the tag
2299        cx.set_state(
2300            "<html><head></head><body> ˇ  <b>hi!</b></body>",
2301            Mode::Normal,
2302        );
2303        cx.simulate_keystrokes("v i t");
2304        cx.assert_state(
2305            "<html><head></head><body>   <b>«hi!ˇ»</b></body>",
2306            Mode::Visual,
2307        );
2308        cx.simulate_keystrokes("a t");
2309        cx.assert_state(
2310            "<html><head></head><body>   «<b>hi!</b>ˇ»</body>",
2311            Mode::Visual,
2312        );
2313
2314        // The cursor is in the open tag
2315        cx.set_state(
2316            "<html><head></head><body><bˇ>hi!</b><b>hello!</b></body>",
2317            Mode::Normal,
2318        );
2319        cx.simulate_keystrokes("v a t");
2320        cx.assert_state(
2321            "<html><head></head><body>«<b>hi!</b>ˇ»<b>hello!</b></body>",
2322            Mode::Visual,
2323        );
2324        cx.simulate_keystrokes("i t");
2325        cx.assert_state(
2326            "<html><head></head><body>«<b>hi!</b><b>hello!</b>ˇ»</body>",
2327            Mode::Visual,
2328        );
2329
2330        // current selection length greater than 1
2331        cx.set_state(
2332            "<html><head></head><body><«b>hi!ˇ»</b></body>",
2333            Mode::Visual,
2334        );
2335        cx.simulate_keystrokes("i t");
2336        cx.assert_state(
2337            "<html><head></head><body><b>«hi!ˇ»</b></body>",
2338            Mode::Visual,
2339        );
2340        cx.simulate_keystrokes("a t");
2341        cx.assert_state(
2342            "<html><head></head><body>«<b>hi!</b>ˇ»</body>",
2343            Mode::Visual,
2344        );
2345
2346        cx.set_state(
2347            "<html><head></head><body><«b>hi!</ˇ»b></body>",
2348            Mode::Visual,
2349        );
2350        cx.simulate_keystrokes("a t");
2351        cx.assert_state(
2352            "<html><head></head>«<body><b>hi!</b></body>ˇ»",
2353            Mode::Visual,
2354        );
2355    }
2356}