movement.rs

   1//! Movement module contains helper functions for calculating intended position
   2//! in editor given a given motion (e.g. it handles converting a "move left" command into coordinates in editor). It is exposed mostly for use by vim crate.
   3
   4use super::{Bias, DisplayPoint, DisplaySnapshot, SelectionGoal, ToDisplayPoint};
   5use crate::{CharKind, DisplayRow, EditorStyle, ToOffset, ToPoint, scroll::ScrollAnchor};
   6use gpui::{Pixels, WindowTextSystem};
   7use language::Point;
   8use multi_buffer::{MultiBufferRow, MultiBufferSnapshot};
   9use serde::Deserialize;
  10use workspace::searchable::Direction;
  11
  12use std::{ops::Range, sync::Arc};
  13
  14/// Defines search strategy for items in `movement` module.
  15/// `FindRange::SingeLine` only looks for a match on a single line at a time, whereas
  16/// `FindRange::MultiLine` keeps going until the end of a string.
  17#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
  18pub enum FindRange {
  19    SingleLine,
  20    MultiLine,
  21}
  22
  23/// TextLayoutDetails encompasses everything we need to move vertically
  24/// taking into account variable width characters.
  25pub struct TextLayoutDetails {
  26    pub(crate) text_system: Arc<WindowTextSystem>,
  27    pub(crate) editor_style: EditorStyle,
  28    pub(crate) rem_size: Pixels,
  29    pub scroll_anchor: ScrollAnchor,
  30    pub visible_rows: Option<f32>,
  31    pub vertical_scroll_margin: f32,
  32}
  33
  34/// Returns a column to the left of the current point, wrapping
  35/// to the previous line if that point is at the start of line.
  36pub fn left(map: &DisplaySnapshot, mut point: DisplayPoint) -> DisplayPoint {
  37    if point.column() > 0 {
  38        *point.column_mut() -= 1;
  39    } else if point.row().0 > 0 {
  40        *point.row_mut() -= 1;
  41        *point.column_mut() = map.line_len(point.row());
  42    }
  43    map.clip_point(point, Bias::Left)
  44}
  45
  46/// Returns a column to the left of the current point, doing nothing if
  47/// that point is already at the start of line.
  48pub fn saturating_left(map: &DisplaySnapshot, mut point: DisplayPoint) -> DisplayPoint {
  49    if point.column() > 0 {
  50        *point.column_mut() -= 1;
  51    } else if point.column() == 0 {
  52        // If the current sofr_wrap mode is used, the column corresponding to the display is 0,
  53        //  which does not necessarily mean that the actual beginning of a paragraph
  54        if map.display_point_to_fold_point(point, Bias::Left).column() > 0 {
  55            return left(map, point);
  56        }
  57    }
  58    map.clip_point(point, Bias::Left)
  59}
  60
  61/// Returns a column to the right of the current point, wrapping
  62/// to the next line if that point is at the end of line.
  63pub fn right(map: &DisplaySnapshot, mut point: DisplayPoint) -> DisplayPoint {
  64    if point.column() < map.line_len(point.row()) {
  65        *point.column_mut() += 1;
  66    } else if point.row() < map.max_point().row() {
  67        *point.row_mut() += 1;
  68        *point.column_mut() = 0;
  69    }
  70    map.clip_point(point, Bias::Right)
  71}
  72
  73/// Returns a column to the right of the current point, not performing any wrapping
  74/// if that point is already at the end of line.
  75pub fn saturating_right(map: &DisplaySnapshot, mut point: DisplayPoint) -> DisplayPoint {
  76    *point.column_mut() += 1;
  77    map.clip_point(point, Bias::Right)
  78}
  79
  80/// Returns a display point for the preceding displayed line (which might be a soft-wrapped line).
  81pub fn up(
  82    map: &DisplaySnapshot,
  83    start: DisplayPoint,
  84    goal: SelectionGoal,
  85    preserve_column_at_start: bool,
  86    text_layout_details: &TextLayoutDetails,
  87) -> (DisplayPoint, SelectionGoal) {
  88    up_by_rows(
  89        map,
  90        start,
  91        1,
  92        goal,
  93        preserve_column_at_start,
  94        text_layout_details,
  95    )
  96}
  97
  98/// Returns a display point for the next displayed line (which might be a soft-wrapped line).
  99pub fn down(
 100    map: &DisplaySnapshot,
 101    start: DisplayPoint,
 102    goal: SelectionGoal,
 103    preserve_column_at_end: bool,
 104    text_layout_details: &TextLayoutDetails,
 105) -> (DisplayPoint, SelectionGoal) {
 106    down_by_rows(
 107        map,
 108        start,
 109        1,
 110        goal,
 111        preserve_column_at_end,
 112        text_layout_details,
 113    )
 114}
 115
 116pub(crate) fn up_by_rows(
 117    map: &DisplaySnapshot,
 118    start: DisplayPoint,
 119    row_count: u32,
 120    goal: SelectionGoal,
 121    preserve_column_at_start: bool,
 122    text_layout_details: &TextLayoutDetails,
 123) -> (DisplayPoint, SelectionGoal) {
 124    let goal_x = match goal {
 125        SelectionGoal::HorizontalPosition(x) => x.into(),
 126        SelectionGoal::WrappedHorizontalPosition((_, x)) => x.into(),
 127        SelectionGoal::HorizontalRange { end, .. } => end.into(),
 128        _ => map.x_for_display_point(start, text_layout_details),
 129    };
 130
 131    let prev_row = DisplayRow(start.row().0.saturating_sub(row_count));
 132    let mut point = map.clip_point(
 133        DisplayPoint::new(prev_row, map.line_len(prev_row)),
 134        Bias::Left,
 135    );
 136    if point.row() < start.row() {
 137        *point.column_mut() = map.display_column_for_x(point.row(), goal_x, text_layout_details)
 138    } else if preserve_column_at_start {
 139        return (start, goal);
 140    } else {
 141        point = DisplayPoint::new(DisplayRow(0), 0);
 142    }
 143
 144    let mut clipped_point = map.clip_point(point, Bias::Left);
 145    if clipped_point.row() < point.row() {
 146        clipped_point = map.clip_point(point, Bias::Right);
 147    }
 148    (
 149        clipped_point,
 150        SelectionGoal::HorizontalPosition(goal_x.into()),
 151    )
 152}
 153
 154pub(crate) fn down_by_rows(
 155    map: &DisplaySnapshot,
 156    start: DisplayPoint,
 157    row_count: u32,
 158    goal: SelectionGoal,
 159    preserve_column_at_end: bool,
 160    text_layout_details: &TextLayoutDetails,
 161) -> (DisplayPoint, SelectionGoal) {
 162    let goal_x = match goal {
 163        SelectionGoal::HorizontalPosition(x) => x.into(),
 164        SelectionGoal::WrappedHorizontalPosition((_, x)) => x.into(),
 165        SelectionGoal::HorizontalRange { end, .. } => end.into(),
 166        _ => map.x_for_display_point(start, text_layout_details),
 167    };
 168
 169    let new_row = DisplayRow(start.row().0 + row_count);
 170    let mut point = map.clip_point(DisplayPoint::new(new_row, 0), Bias::Right);
 171    if point.row() > start.row() {
 172        *point.column_mut() = map.display_column_for_x(point.row(), goal_x, text_layout_details)
 173    } else if preserve_column_at_end {
 174        return (start, goal);
 175    } else {
 176        point = map.max_point();
 177    }
 178
 179    let mut clipped_point = map.clip_point(point, Bias::Right);
 180    if clipped_point.row() > point.row() {
 181        clipped_point = map.clip_point(point, Bias::Left);
 182    }
 183    (
 184        clipped_point,
 185        SelectionGoal::HorizontalPosition(goal_x.into()),
 186    )
 187}
 188
 189/// Returns a position of the start of line.
 190/// If `stop_at_soft_boundaries` is true, the returned position is that of the
 191/// displayed line (e.g. it could actually be in the middle of a text line if that line is soft-wrapped).
 192/// Otherwise it's always going to be the start of a logical line.
 193pub fn line_beginning(
 194    map: &DisplaySnapshot,
 195    display_point: DisplayPoint,
 196    stop_at_soft_boundaries: bool,
 197) -> DisplayPoint {
 198    let point = display_point.to_point(map);
 199    let soft_line_start = map.clip_point(DisplayPoint::new(display_point.row(), 0), Bias::Right);
 200    let line_start = map.prev_line_boundary(point).1;
 201
 202    if stop_at_soft_boundaries && display_point != soft_line_start {
 203        soft_line_start
 204    } else {
 205        line_start
 206    }
 207}
 208
 209/// Returns the last indented position on a given line.
 210/// If `stop_at_soft_boundaries` is true, the returned [`DisplayPoint`] is that of a
 211/// displayed line (e.g. if there's soft wrap it's gonna be returned),
 212/// otherwise it's always going to be a start of a logical line.
 213pub fn indented_line_beginning(
 214    map: &DisplaySnapshot,
 215    display_point: DisplayPoint,
 216    stop_at_soft_boundaries: bool,
 217    stop_at_indent: bool,
 218) -> DisplayPoint {
 219    let point = display_point.to_point(map);
 220    let soft_line_start = map.clip_point(DisplayPoint::new(display_point.row(), 0), Bias::Right);
 221    let indent_start = Point::new(
 222        point.row,
 223        map.buffer_snapshot
 224            .indent_size_for_line(MultiBufferRow(point.row))
 225            .len,
 226    )
 227    .to_display_point(map);
 228    let line_start = map.prev_line_boundary(point).1;
 229
 230    if stop_at_soft_boundaries && soft_line_start > indent_start && display_point != soft_line_start
 231    {
 232        soft_line_start
 233    } else if stop_at_indent && display_point != indent_start {
 234        indent_start
 235    } else {
 236        line_start
 237    }
 238}
 239
 240/// Returns a position of the end of line.
 241///
 242/// If `stop_at_soft_boundaries` is true, the returned position is that of the
 243/// displayed line (e.g. it could actually be in the middle of a text line if that line is soft-wrapped).
 244/// Otherwise it's always going to be the end of a logical line.
 245pub fn line_end(
 246    map: &DisplaySnapshot,
 247    display_point: DisplayPoint,
 248    stop_at_soft_boundaries: bool,
 249) -> DisplayPoint {
 250    let soft_line_end = map.clip_point(
 251        DisplayPoint::new(display_point.row(), map.line_len(display_point.row())),
 252        Bias::Left,
 253    );
 254    if stop_at_soft_boundaries && display_point != soft_line_end {
 255        soft_line_end
 256    } else {
 257        map.next_line_boundary(display_point.to_point(map)).1
 258    }
 259}
 260
 261/// Returns a position of the previous word boundary, where a word character is defined as either
 262/// uppercase letter, lowercase letter, '_' character or language-specific word character (like '-' in CSS).
 263pub fn previous_word_start(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
 264    let raw_point = point.to_point(map);
 265    let classifier = map.buffer_snapshot.char_classifier_at(raw_point);
 266
 267    let mut is_first_iteration = true;
 268    find_preceding_boundary_display_point(map, point, FindRange::MultiLine, |left, right| {
 269        // Make alt-left skip punctuation to respect VSCode behaviour. For example: hello.| goes to |hello.
 270        if is_first_iteration
 271            && classifier.is_punctuation(right)
 272            && !classifier.is_punctuation(left)
 273            && left != '\n'
 274        {
 275            is_first_iteration = false;
 276            return false;
 277        }
 278        is_first_iteration = false;
 279
 280        (classifier.kind(left) != classifier.kind(right) && !classifier.is_whitespace(right))
 281            || left == '\n'
 282    })
 283}
 284
 285/// Returns a position of the previous word boundary, where a word character is defined as either
 286/// uppercase letter, lowercase letter, '_' character, language-specific word character (like '-' in CSS) or newline.
 287pub fn previous_word_start_or_newline(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
 288    let raw_point = point.to_point(map);
 289    let classifier = map.buffer_snapshot.char_classifier_at(raw_point);
 290
 291    find_preceding_boundary_display_point(map, point, FindRange::MultiLine, |left, right| {
 292        (classifier.kind(left) != classifier.kind(right) && !right.is_whitespace())
 293            || left == '\n'
 294            || right == '\n'
 295    })
 296}
 297
 298/// Returns a position of the previous subword boundary, where a subword is defined as a run of
 299/// word characters of the same "subkind" - where subcharacter kinds are '_' character,
 300/// lowerspace characters and uppercase characters.
 301pub fn previous_subword_start(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
 302    let raw_point = point.to_point(map);
 303    let classifier = map.buffer_snapshot.char_classifier_at(raw_point);
 304
 305    find_preceding_boundary_display_point(map, point, FindRange::MultiLine, |left, right| {
 306        let is_word_start =
 307            classifier.kind(left) != classifier.kind(right) && !right.is_whitespace();
 308        let is_subword_start = classifier.is_word('-') && left == '-' && right != '-'
 309            || left == '_' && right != '_'
 310            || left.is_lowercase() && right.is_uppercase();
 311        is_word_start || is_subword_start || left == '\n'
 312    })
 313}
 314
 315/// Returns a position of the next word boundary, where a word character is defined as either
 316/// uppercase letter, lowercase letter, '_' character or language-specific word character (like '-' in CSS).
 317pub fn next_word_end(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
 318    let raw_point = point.to_point(map);
 319    let classifier = map.buffer_snapshot.char_classifier_at(raw_point);
 320    let mut is_first_iteration = true;
 321    find_boundary(map, point, FindRange::MultiLine, |left, right| {
 322        // Make alt-right skip punctuation to respect VSCode behaviour. For example: |.hello goes to .hello|
 323        if is_first_iteration
 324            && classifier.is_punctuation(left)
 325            && !classifier.is_punctuation(right)
 326            && right != '\n'
 327        {
 328            is_first_iteration = false;
 329            return false;
 330        }
 331        is_first_iteration = false;
 332
 333        (classifier.kind(left) != classifier.kind(right) && !classifier.is_whitespace(left))
 334            || right == '\n'
 335    })
 336}
 337
 338/// Returns a position of the next word boundary, where a word character is defined as either
 339/// uppercase letter, lowercase letter, '_' character, language-specific word character (like '-' in CSS) or newline.
 340pub fn next_word_end_or_newline(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
 341    let raw_point = point.to_point(map);
 342    let classifier = map.buffer_snapshot.char_classifier_at(raw_point);
 343
 344    let mut on_starting_row = true;
 345    find_boundary(map, point, FindRange::MultiLine, |left, right| {
 346        if left == '\n' {
 347            on_starting_row = false;
 348        }
 349        (classifier.kind(left) != classifier.kind(right)
 350            && ((on_starting_row && !left.is_whitespace())
 351                || (!on_starting_row && !right.is_whitespace())))
 352            || right == '\n'
 353    })
 354}
 355
 356/// Returns a position of the next subword boundary, where a subword is defined as a run of
 357/// word characters of the same "subkind" - where subcharacter kinds are '_' character,
 358/// lowerspace characters and uppercase characters.
 359pub fn next_subword_end(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
 360    let raw_point = point.to_point(map);
 361    let classifier = map.buffer_snapshot.char_classifier_at(raw_point);
 362
 363    find_boundary(map, point, FindRange::MultiLine, |left, right| {
 364        let is_word_end =
 365            (classifier.kind(left) != classifier.kind(right)) && !classifier.is_whitespace(left);
 366        let is_subword_end = classifier.is_word('-') && left != '-' && right == '-'
 367            || left != '_' && right == '_'
 368            || left.is_lowercase() && right.is_uppercase();
 369        is_word_end || is_subword_end || right == '\n'
 370    })
 371}
 372
 373/// Returns a position of the start of the current paragraph, where a paragraph
 374/// is defined as a run of non-blank lines.
 375pub fn start_of_paragraph(
 376    map: &DisplaySnapshot,
 377    display_point: DisplayPoint,
 378    mut count: usize,
 379) -> DisplayPoint {
 380    let point = display_point.to_point(map);
 381    if point.row == 0 {
 382        return DisplayPoint::zero();
 383    }
 384
 385    let mut found_non_blank_line = false;
 386    for row in (0..point.row + 1).rev() {
 387        let blank = map.buffer_snapshot.is_line_blank(MultiBufferRow(row));
 388        if found_non_blank_line && blank {
 389            if count <= 1 {
 390                return Point::new(row, 0).to_display_point(map);
 391            }
 392            count -= 1;
 393            found_non_blank_line = false;
 394        }
 395
 396        found_non_blank_line |= !blank;
 397    }
 398
 399    DisplayPoint::zero()
 400}
 401
 402/// Returns a position of the end of the current paragraph, where a paragraph
 403/// is defined as a run of non-blank lines.
 404pub fn end_of_paragraph(
 405    map: &DisplaySnapshot,
 406    display_point: DisplayPoint,
 407    mut count: usize,
 408) -> DisplayPoint {
 409    let point = display_point.to_point(map);
 410    if point.row == map.buffer_snapshot.max_row().0 {
 411        return map.max_point();
 412    }
 413
 414    let mut found_non_blank_line = false;
 415    for row in point.row..=map.buffer_snapshot.max_row().0 {
 416        let blank = map.buffer_snapshot.is_line_blank(MultiBufferRow(row));
 417        if found_non_blank_line && blank {
 418            if count <= 1 {
 419                return Point::new(row, 0).to_display_point(map);
 420            }
 421            count -= 1;
 422            found_non_blank_line = false;
 423        }
 424
 425        found_non_blank_line |= !blank;
 426    }
 427
 428    map.max_point()
 429}
 430
 431pub fn start_of_excerpt(
 432    map: &DisplaySnapshot,
 433    display_point: DisplayPoint,
 434    direction: Direction,
 435) -> DisplayPoint {
 436    let point = map.display_point_to_point(display_point, Bias::Left);
 437    let Some(excerpt) = map.buffer_snapshot.excerpt_containing(point..point) else {
 438        return display_point;
 439    };
 440    match direction {
 441        Direction::Prev => {
 442            let mut start = excerpt.start_anchor().to_display_point(&map);
 443            if start >= display_point && start.row() > DisplayRow(0) {
 444                let Some(excerpt) = map.buffer_snapshot.excerpt_before(excerpt.id()) else {
 445                    return display_point;
 446                };
 447                start = excerpt.start_anchor().to_display_point(&map);
 448            }
 449            start
 450        }
 451        Direction::Next => {
 452            let mut end = excerpt.end_anchor().to_display_point(&map);
 453            *end.row_mut() += 1;
 454            map.clip_point(end, Bias::Right)
 455        }
 456    }
 457}
 458
 459pub fn end_of_excerpt(
 460    map: &DisplaySnapshot,
 461    display_point: DisplayPoint,
 462    direction: Direction,
 463) -> DisplayPoint {
 464    let point = map.display_point_to_point(display_point, Bias::Left);
 465    let Some(excerpt) = map.buffer_snapshot.excerpt_containing(point..point) else {
 466        return display_point;
 467    };
 468    match direction {
 469        Direction::Prev => {
 470            let mut start = excerpt.start_anchor().to_display_point(&map);
 471            if start.row() > DisplayRow(0) {
 472                *start.row_mut() -= 1;
 473            }
 474            start = map.clip_point(start, Bias::Left);
 475            *start.column_mut() = 0;
 476            start
 477        }
 478        Direction::Next => {
 479            let mut end = excerpt.end_anchor().to_display_point(&map);
 480            *end.column_mut() = 0;
 481            if end <= display_point {
 482                *end.row_mut() += 1;
 483                let point_end = map.display_point_to_point(end, Bias::Right);
 484                let Some(excerpt) = map.buffer_snapshot.excerpt_containing(point_end..point_end)
 485                else {
 486                    return display_point;
 487                };
 488                end = excerpt.end_anchor().to_display_point(&map);
 489                *end.column_mut() = 0;
 490            }
 491            end
 492        }
 493    }
 494}
 495
 496/// Scans for a boundary preceding the given start point `from` until a boundary is found,
 497/// indicated by the given predicate returning true.
 498/// The predicate is called with the character to the left and right of the candidate boundary location.
 499/// If FindRange::SingleLine is specified and no boundary is found before the start of the current line, the start of the current line will be returned.
 500pub fn find_preceding_boundary_point(
 501    buffer_snapshot: &MultiBufferSnapshot,
 502    from: Point,
 503    find_range: FindRange,
 504    mut is_boundary: impl FnMut(char, char) -> bool,
 505) -> Point {
 506    let mut prev_ch = None;
 507    let mut offset = from.to_offset(buffer_snapshot);
 508
 509    for ch in buffer_snapshot.reversed_chars_at(offset) {
 510        if find_range == FindRange::SingleLine && ch == '\n' {
 511            break;
 512        }
 513        if let Some(prev_ch) = prev_ch {
 514            if is_boundary(ch, prev_ch) {
 515                break;
 516            }
 517        }
 518
 519        offset -= ch.len_utf8();
 520        prev_ch = Some(ch);
 521    }
 522
 523    offset.to_point(buffer_snapshot)
 524}
 525
 526/// Scans for a boundary preceding the given start point `from` until a boundary is found,
 527/// indicated by the given predicate returning true.
 528/// The predicate is called with the character to the left and right of the candidate boundary location.
 529/// If FindRange::SingleLine is specified and no boundary is found before the start of the current line, the start of the current line will be returned.
 530pub fn find_preceding_boundary_display_point(
 531    map: &DisplaySnapshot,
 532    from: DisplayPoint,
 533    find_range: FindRange,
 534    is_boundary: impl FnMut(char, char) -> bool,
 535) -> DisplayPoint {
 536    let result = find_preceding_boundary_point(
 537        &map.buffer_snapshot,
 538        from.to_point(map),
 539        find_range,
 540        is_boundary,
 541    );
 542    map.clip_point(result.to_display_point(map), Bias::Left)
 543}
 544
 545/// Scans for a boundary following the given start point until a boundary is found, indicated by the
 546/// given predicate returning true. The predicate is called with the character to the left and right
 547/// of the candidate boundary location, and will be called with `\n` characters indicating the start
 548/// or end of a line. The function supports optionally returning the point just before the boundary
 549/// is found via return_point_before_boundary.
 550pub fn find_boundary_point(
 551    map: &DisplaySnapshot,
 552    from: DisplayPoint,
 553    find_range: FindRange,
 554    mut is_boundary: impl FnMut(char, char) -> bool,
 555    return_point_before_boundary: bool,
 556) -> DisplayPoint {
 557    let mut offset = from.to_offset(map, Bias::Right);
 558    let mut prev_offset = offset;
 559    let mut prev_ch = None;
 560
 561    for ch in map.buffer_snapshot.chars_at(offset) {
 562        if find_range == FindRange::SingleLine && ch == '\n' {
 563            break;
 564        }
 565        if let Some(prev_ch) = prev_ch {
 566            if is_boundary(prev_ch, ch) {
 567                if return_point_before_boundary {
 568                    return map.clip_point(prev_offset.to_display_point(map), Bias::Right);
 569                } else {
 570                    break;
 571                }
 572            }
 573        }
 574        prev_offset = offset;
 575        offset += ch.len_utf8();
 576        prev_ch = Some(ch);
 577    }
 578    map.clip_point(offset.to_display_point(map), Bias::Right)
 579}
 580
 581pub fn find_preceding_boundary_trail(
 582    map: &DisplaySnapshot,
 583    head: DisplayPoint,
 584    mut is_boundary: impl FnMut(char, char) -> bool,
 585) -> (Option<DisplayPoint>, DisplayPoint) {
 586    let mut offset = head.to_offset(map, Bias::Left);
 587    let mut trail_offset = None;
 588
 589    let mut prev_ch = map.buffer_snapshot.chars_at(offset).next();
 590    let mut forward = map.buffer_snapshot.reversed_chars_at(offset).peekable();
 591
 592    // Skip newlines
 593    while let Some(&ch) = forward.peek() {
 594        if ch == '\n' {
 595            prev_ch = forward.next();
 596            offset -= ch.len_utf8();
 597            trail_offset = Some(offset);
 598        } else {
 599            break;
 600        }
 601    }
 602
 603    // Find the boundary
 604    let start_offset = offset;
 605    for ch in forward {
 606        if let Some(prev_ch) = prev_ch {
 607            if is_boundary(prev_ch, ch) {
 608                if start_offset == offset {
 609                    trail_offset = Some(offset);
 610                } else {
 611                    break;
 612                }
 613            }
 614        }
 615        offset -= ch.len_utf8();
 616        prev_ch = Some(ch);
 617    }
 618
 619    let trail = trail_offset
 620        .map(|trail_offset: usize| map.clip_point(trail_offset.to_display_point(map), Bias::Left));
 621
 622    (
 623        trail,
 624        map.clip_point(offset.to_display_point(map), Bias::Left),
 625    )
 626}
 627
 628/// Finds the location of a boundary
 629pub fn find_boundary_trail(
 630    map: &DisplaySnapshot,
 631    head: DisplayPoint,
 632    mut is_boundary: impl FnMut(char, char) -> bool,
 633) -> (Option<DisplayPoint>, DisplayPoint) {
 634    let mut offset = head.to_offset(map, Bias::Right);
 635    let mut trail_offset = None;
 636
 637    let mut prev_ch = map.buffer_snapshot.reversed_chars_at(offset).next();
 638    let mut forward = map.buffer_snapshot.chars_at(offset).peekable();
 639
 640    // Skip newlines
 641    while let Some(&ch) = forward.peek() {
 642        if ch == '\n' {
 643            prev_ch = forward.next();
 644            offset += ch.len_utf8();
 645            trail_offset = Some(offset);
 646        } else {
 647            break;
 648        }
 649    }
 650
 651    // Find the boundary
 652    let start_offset = offset;
 653    for ch in forward {
 654        if let Some(prev_ch) = prev_ch {
 655            if is_boundary(prev_ch, ch) {
 656                if start_offset == offset {
 657                    trail_offset = Some(offset);
 658                } else {
 659                    break;
 660                }
 661            }
 662        }
 663        offset += ch.len_utf8();
 664        prev_ch = Some(ch);
 665    }
 666
 667    let trail = trail_offset
 668        .map(|trail_offset: usize| map.clip_point(trail_offset.to_display_point(map), Bias::Right));
 669
 670    (
 671        trail,
 672        map.clip_point(offset.to_display_point(map), Bias::Right),
 673    )
 674}
 675
 676pub fn find_boundary(
 677    map: &DisplaySnapshot,
 678    from: DisplayPoint,
 679    find_range: FindRange,
 680    is_boundary: impl FnMut(char, char) -> bool,
 681) -> DisplayPoint {
 682    find_boundary_point(map, from, find_range, is_boundary, false)
 683}
 684
 685pub fn find_boundary_exclusive(
 686    map: &DisplaySnapshot,
 687    from: DisplayPoint,
 688    find_range: FindRange,
 689    is_boundary: impl FnMut(char, char) -> bool,
 690) -> DisplayPoint {
 691    find_boundary_point(map, from, find_range, is_boundary, true)
 692}
 693
 694/// Returns an iterator over the characters following a given offset in the [`DisplaySnapshot`].
 695/// The returned value also contains a range of the start/end of a returned character in
 696/// the [`DisplaySnapshot`]. The offsets are relative to the start of a buffer.
 697pub fn chars_after(
 698    map: &DisplaySnapshot,
 699    mut offset: usize,
 700) -> impl Iterator<Item = (char, Range<usize>)> + '_ {
 701    map.buffer_snapshot.chars_at(offset).map(move |ch| {
 702        let before = offset;
 703        offset += ch.len_utf8();
 704        (ch, before..offset)
 705    })
 706}
 707
 708/// Returns a reverse iterator over the characters following a given offset in the [`DisplaySnapshot`].
 709/// The returned value also contains a range of the start/end of a returned character in
 710/// the [`DisplaySnapshot`]. The offsets are relative to the start of a buffer.
 711pub fn chars_before(
 712    map: &DisplaySnapshot,
 713    mut offset: usize,
 714) -> impl Iterator<Item = (char, Range<usize>)> + '_ {
 715    map.buffer_snapshot
 716        .reversed_chars_at(offset)
 717        .map(move |ch| {
 718            let after = offset;
 719            offset -= ch.len_utf8();
 720            (ch, offset..after)
 721        })
 722}
 723
 724pub(crate) fn is_inside_word(map: &DisplaySnapshot, point: DisplayPoint) -> bool {
 725    let raw_point = point.to_point(map);
 726    let classifier = map.buffer_snapshot.char_classifier_at(raw_point);
 727    let ix = map.clip_point(point, Bias::Left).to_offset(map, Bias::Left);
 728    let text = &map.buffer_snapshot;
 729    let next_char_kind = text.chars_at(ix).next().map(|c| classifier.kind(c));
 730    let prev_char_kind = text
 731        .reversed_chars_at(ix)
 732        .next()
 733        .map(|c| classifier.kind(c));
 734    prev_char_kind.zip(next_char_kind) == Some((CharKind::Word, CharKind::Word))
 735}
 736
 737pub(crate) fn surrounding_word(
 738    map: &DisplaySnapshot,
 739    position: DisplayPoint,
 740) -> Range<DisplayPoint> {
 741    let position = map
 742        .clip_point(position, Bias::Left)
 743        .to_offset(map, Bias::Left);
 744    let (range, _) = map.buffer_snapshot.surrounding_word(position, false);
 745    let start = range
 746        .start
 747        .to_point(&map.buffer_snapshot)
 748        .to_display_point(map);
 749    let end = range
 750        .end
 751        .to_point(&map.buffer_snapshot)
 752        .to_display_point(map);
 753    start..end
 754}
 755
 756/// Returns a list of lines (represented as a [`DisplayPoint`] range) contained
 757/// within a passed range.
 758///
 759/// The line ranges are **always* going to be in bounds of a requested range, which means that
 760/// the first and the last lines might not necessarily represent the
 761/// full range of a logical line (as their `.start`/`.end` values are clipped to those of a passed in range).
 762pub fn split_display_range_by_lines(
 763    map: &DisplaySnapshot,
 764    range: Range<DisplayPoint>,
 765) -> Vec<Range<DisplayPoint>> {
 766    let mut result = Vec::new();
 767
 768    let mut start = range.start;
 769    // Loop over all the covered rows until the one containing the range end
 770    for row in range.start.row().0..range.end.row().0 {
 771        let row_end_column = map.line_len(DisplayRow(row));
 772        let end = map.clip_point(
 773            DisplayPoint::new(DisplayRow(row), row_end_column),
 774            Bias::Left,
 775        );
 776        if start != end {
 777            result.push(start..end);
 778        }
 779        start = map.clip_point(DisplayPoint::new(DisplayRow(row + 1), 0), Bias::Left);
 780    }
 781
 782    // Add the final range from the start of the last end to the original range end.
 783    result.push(start..range.end);
 784
 785    result
 786}
 787
 788#[cfg(test)]
 789mod tests {
 790    use super::*;
 791    use crate::{
 792        Buffer, DisplayMap, DisplayRow, ExcerptRange, FoldPlaceholder, InlayId, MultiBuffer,
 793        display_map::Inlay,
 794        test::{editor_test_context::EditorTestContext, marked_display_snapshot},
 795    };
 796    use gpui::{AppContext as _, font, px};
 797    use language::Capability;
 798    use project::{Project, project_settings::DiagnosticSeverity};
 799    use settings::SettingsStore;
 800    use util::post_inc;
 801
 802    #[gpui::test]
 803    fn test_previous_word_start(cx: &mut gpui::App) {
 804        init_test(cx);
 805
 806        fn assert(marked_text: &str, cx: &mut gpui::App) {
 807            let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
 808            let actual = previous_word_start(&snapshot, display_points[1]);
 809            let expected = display_points[0];
 810            if actual != expected {
 811                eprintln!(
 812                    "previous_word_start mismatch for '{}': actual={:?}, expected={:?}",
 813                    marked_text, actual, expected
 814                );
 815            }
 816            assert_eq!(actual, expected);
 817        }
 818
 819        assert("\nˇ   ˇlorem", cx);
 820        assert("ˇ\nˇ   lorem", cx);
 821        assert("    ˇloremˇ", cx);
 822        assert("ˇ    ˇlorem", cx);
 823        assert("    ˇlorˇem", cx);
 824        assert("\nlorem\nˇ   ˇipsum", cx);
 825        assert("\n\nˇ\nˇ", cx);
 826        assert("    ˇlorem  ˇipsum", cx);
 827        assert("ˇlorem-ˇipsum", cx);
 828        assert("loremˇ-#$@ˇipsum", cx);
 829        assert("ˇlorem_ˇipsum", cx);
 830        assert(" ˇdefγˇ", cx);
 831        assert(" ˇbcΔˇ", cx);
 832        // Test punctuation skipping behavior
 833        assert("ˇhello.ˇ", cx);
 834        assert("helloˇ...ˇ", cx);
 835        assert("helloˇ.---..ˇtest", cx);
 836        assert("test  ˇ.--ˇtest", cx);
 837        assert("oneˇ,;:!?ˇtwo", cx);
 838    }
 839
 840    #[gpui::test]
 841    fn test_previous_subword_start(cx: &mut gpui::App) {
 842        init_test(cx);
 843
 844        fn assert(marked_text: &str, cx: &mut gpui::App) {
 845            let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
 846            assert_eq!(
 847                previous_subword_start(&snapshot, display_points[1]),
 848                display_points[0]
 849            );
 850        }
 851
 852        // Subword boundaries are respected
 853        assert("lorem_ˇipˇsum", cx);
 854        assert("lorem_ˇipsumˇ", cx);
 855        assert("ˇlorem_ˇipsum", cx);
 856        assert("lorem_ˇipsum_ˇdolor", cx);
 857        assert("loremˇIpˇsum", cx);
 858        assert("loremˇIpsumˇ", cx);
 859
 860        // Word boundaries are still respected
 861        assert("\nˇ   ˇlorem", cx);
 862        assert("    ˇloremˇ", cx);
 863        assert("    ˇlorˇem", cx);
 864        assert("\nlorem\nˇ   ˇipsum", cx);
 865        assert("\n\nˇ\nˇ", cx);
 866        assert("    ˇlorem  ˇipsum", cx);
 867        assert("loremˇ-ˇipsum", cx);
 868        assert("loremˇ-#$@ˇipsum", cx);
 869        assert(" ˇdefγˇ", cx);
 870        assert(" bcˇΔˇ", cx);
 871        assert(" ˇbcδˇ", cx);
 872        assert(" abˇ——ˇcd", cx);
 873    }
 874
 875    #[gpui::test]
 876    fn test_find_preceding_boundary(cx: &mut gpui::App) {
 877        init_test(cx);
 878
 879        fn assert(
 880            marked_text: &str,
 881            cx: &mut gpui::App,
 882            is_boundary: impl FnMut(char, char) -> bool,
 883        ) {
 884            let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
 885            assert_eq!(
 886                find_preceding_boundary_display_point(
 887                    &snapshot,
 888                    display_points[1],
 889                    FindRange::MultiLine,
 890                    is_boundary
 891                ),
 892                display_points[0]
 893            );
 894        }
 895
 896        assert("abcˇdef\ngh\nijˇk", cx, |left, right| {
 897            left == 'c' && right == 'd'
 898        });
 899        assert("abcdef\nˇgh\nijˇk", cx, |left, right| {
 900            left == '\n' && right == 'g'
 901        });
 902        let mut line_count = 0;
 903        assert("abcdef\nˇgh\nijˇk", cx, |left, _| {
 904            if left == '\n' {
 905                line_count += 1;
 906                line_count == 2
 907            } else {
 908                false
 909            }
 910        });
 911    }
 912
 913    #[gpui::test]
 914    fn test_find_preceding_boundary_with_inlays(cx: &mut gpui::App) {
 915        init_test(cx);
 916
 917        let input_text = "abcdefghijklmnopqrstuvwxys";
 918        let font = font("Helvetica");
 919        let font_size = px(14.0);
 920        let buffer = MultiBuffer::build_simple(input_text, cx);
 921        let buffer_snapshot = buffer.read(cx).snapshot(cx);
 922
 923        let display_map = cx.new(|cx| {
 924            DisplayMap::new(
 925                buffer,
 926                font,
 927                font_size,
 928                None,
 929                1,
 930                1,
 931                FoldPlaceholder::test(),
 932                DiagnosticSeverity::Warning,
 933                cx,
 934            )
 935        });
 936
 937        // add all kinds of inlays between two word boundaries: we should be able to cross them all, when looking for another boundary
 938        let mut id = 0;
 939        let inlays = (0..buffer_snapshot.len())
 940            .flat_map(|offset| {
 941                [
 942                    Inlay {
 943                        id: InlayId::InlineCompletion(post_inc(&mut id)),
 944                        position: buffer_snapshot.anchor_at(offset, Bias::Left),
 945                        text: "test".into(),
 946                    },
 947                    Inlay {
 948                        id: InlayId::InlineCompletion(post_inc(&mut id)),
 949                        position: buffer_snapshot.anchor_at(offset, Bias::Right),
 950                        text: "test".into(),
 951                    },
 952                    Inlay {
 953                        id: InlayId::Hint(post_inc(&mut id)),
 954                        position: buffer_snapshot.anchor_at(offset, Bias::Left),
 955                        text: "test".into(),
 956                    },
 957                    Inlay {
 958                        id: InlayId::Hint(post_inc(&mut id)),
 959                        position: buffer_snapshot.anchor_at(offset, Bias::Right),
 960                        text: "test".into(),
 961                    },
 962                ]
 963            })
 964            .collect();
 965        let snapshot = display_map.update(cx, |map, cx| {
 966            map.splice_inlays(&[], inlays, cx);
 967            map.snapshot(cx)
 968        });
 969
 970        assert_eq!(
 971            find_preceding_boundary_display_point(
 972                &snapshot,
 973                buffer_snapshot.len().to_display_point(&snapshot),
 974                FindRange::MultiLine,
 975                |left, _| left == 'e',
 976            ),
 977            snapshot
 978                .buffer_snapshot
 979                .offset_to_point(5)
 980                .to_display_point(&snapshot),
 981            "Should not stop at inlays when looking for boundaries"
 982        );
 983    }
 984
 985    #[gpui::test]
 986    fn test_next_word_end(cx: &mut gpui::App) {
 987        init_test(cx);
 988
 989        fn assert(marked_text: &str, cx: &mut gpui::App) {
 990            let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
 991            let actual = next_word_end(&snapshot, display_points[0]);
 992            let expected = display_points[1];
 993            if actual != expected {
 994                eprintln!(
 995                    "next_word_end mismatch for '{}': actual={:?}, expected={:?}",
 996                    marked_text, actual, expected
 997                );
 998            }
 999            assert_eq!(actual, expected);
1000        }
1001
1002        assert("\nˇ   loremˇ", cx);
1003        assert("    ˇloremˇ", cx);
1004        assert("    lorˇemˇ", cx);
1005        assert("    loremˇ    ˇ\nipsum\n", cx);
1006        assert("\nˇ\nˇ\n\n", cx);
1007        assert("loremˇ    ipsumˇ   ", cx);
1008        assert("loremˇ-ipsumˇ", cx);
1009        assert("loremˇ#$@-ˇipsum", cx);
1010        assert("loremˇ_ipsumˇ", cx);
1011        assert(" ˇbcΔˇ", cx);
1012        assert(" abˇ——ˇcd", cx);
1013        // Test punctuation skipping behavior
1014        assert("ˇ.helloˇ", cx);
1015        assert("display_pointsˇ[0ˇ]", cx);
1016        assert("ˇ...ˇhello", cx);
1017        assert("helloˇ.---..ˇtest", cx);
1018        assert("testˇ.--ˇ test", cx);
1019        assert("oneˇ,;:!?ˇtwo", cx);
1020    }
1021
1022    #[gpui::test]
1023    fn test_next_subword_end(cx: &mut gpui::App) {
1024        init_test(cx);
1025
1026        fn assert(marked_text: &str, cx: &mut gpui::App) {
1027            let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
1028            assert_eq!(
1029                next_subword_end(&snapshot, display_points[0]),
1030                display_points[1]
1031            );
1032        }
1033
1034        // Subword boundaries are respected
1035        assert("loˇremˇ_ipsum", cx);
1036        assert("ˇloremˇ_ipsum", cx);
1037        assert("loremˇ_ipsumˇ", cx);
1038        assert("loremˇ_ipsumˇ_dolor", cx);
1039        assert("loˇremˇIpsum", cx);
1040        assert("loremˇIpsumˇDolor", cx);
1041
1042        // Word boundaries are still respected
1043        assert("\nˇ   loremˇ", cx);
1044        assert("    ˇloremˇ", cx);
1045        assert("    lorˇemˇ", cx);
1046        assert("    loremˇ    ˇ\nipsum\n", cx);
1047        assert("\nˇ\nˇ\n\n", cx);
1048        assert("loremˇ    ipsumˇ   ", cx);
1049        assert("loremˇ-ˇipsum", cx);
1050        assert("loremˇ#$@-ˇipsum", cx);
1051        assert("loremˇ_ipsumˇ", cx);
1052        assert(" ˇbcˇΔ", cx);
1053        assert(" abˇ——ˇcd", cx);
1054    }
1055
1056    #[gpui::test]
1057    fn test_find_boundary(cx: &mut gpui::App) {
1058        init_test(cx);
1059
1060        fn assert(
1061            marked_text: &str,
1062            cx: &mut gpui::App,
1063            is_boundary: impl FnMut(char, char) -> bool,
1064        ) {
1065            let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
1066            assert_eq!(
1067                find_boundary(
1068                    &snapshot,
1069                    display_points[0],
1070                    FindRange::MultiLine,
1071                    is_boundary,
1072                ),
1073                display_points[1]
1074            );
1075        }
1076
1077        assert("abcˇdef\ngh\nijˇk", cx, |left, right| {
1078            left == 'j' && right == 'k'
1079        });
1080        assert("abˇcdef\ngh\nˇijk", cx, |left, right| {
1081            left == '\n' && right == 'i'
1082        });
1083        let mut line_count = 0;
1084        assert("abcˇdef\ngh\nˇijk", cx, |left, _| {
1085            if left == '\n' {
1086                line_count += 1;
1087                line_count == 2
1088            } else {
1089                false
1090            }
1091        });
1092    }
1093
1094    #[gpui::test]
1095    fn test_surrounding_word(cx: &mut gpui::App) {
1096        init_test(cx);
1097
1098        fn assert(marked_text: &str, cx: &mut gpui::App) {
1099            let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
1100            assert_eq!(
1101                surrounding_word(&snapshot, display_points[1]),
1102                display_points[0]..display_points[2],
1103                "{}",
1104                marked_text
1105            );
1106        }
1107
1108        assert("ˇˇloremˇ  ipsum", cx);
1109        assert("ˇloˇremˇ  ipsum", cx);
1110        assert("ˇloremˇˇ  ipsum", cx);
1111        assert("loremˇ ˇ  ˇipsum", cx);
1112        assert("lorem\nˇˇˇ\nipsum", cx);
1113        assert("lorem\nˇˇipsumˇ", cx);
1114        assert("loremˇ,ˇˇ ipsum", cx);
1115        assert("ˇloremˇˇ, ipsum", cx);
1116    }
1117
1118    #[gpui::test]
1119    async fn test_move_up_and_down_with_excerpts(cx: &mut gpui::TestAppContext) {
1120        cx.update(|cx| {
1121            init_test(cx);
1122        });
1123
1124        let mut cx = EditorTestContext::new(cx).await;
1125        let editor = cx.editor.clone();
1126        let window = cx.window;
1127        _ = cx.update_window(window, |_, window, cx| {
1128            let text_layout_details = editor.read(cx).text_layout_details(window);
1129
1130            let font = font("Helvetica");
1131
1132            let buffer = cx.new(|cx| Buffer::local("abc\ndefg\nhijkl\nmn", cx));
1133            let multibuffer = cx.new(|cx| {
1134                let mut multibuffer = MultiBuffer::new(Capability::ReadWrite);
1135                multibuffer.push_excerpts(
1136                    buffer.clone(),
1137                    [
1138                        ExcerptRange::new(Point::new(0, 0)..Point::new(1, 4)),
1139                        ExcerptRange::new(Point::new(2, 0)..Point::new(3, 2)),
1140                    ],
1141                    cx,
1142                );
1143                multibuffer
1144            });
1145            let display_map = cx.new(|cx| {
1146                DisplayMap::new(
1147                    multibuffer,
1148                    font,
1149                    px(14.0),
1150                    None,
1151                    0,
1152                    1,
1153                    FoldPlaceholder::test(),
1154                    DiagnosticSeverity::Warning,
1155                    cx,
1156                )
1157            });
1158            let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
1159
1160            assert_eq!(snapshot.text(), "abc\ndefg\n\nhijkl\nmn");
1161
1162            let col_2_x = snapshot
1163                .x_for_display_point(DisplayPoint::new(DisplayRow(0), 2), &text_layout_details);
1164
1165            // Can't move up into the first excerpt's header
1166            assert_eq!(
1167                up(
1168                    &snapshot,
1169                    DisplayPoint::new(DisplayRow(0), 2),
1170                    SelectionGoal::HorizontalPosition(col_2_x.0),
1171                    false,
1172                    &text_layout_details
1173                ),
1174                (
1175                    DisplayPoint::new(DisplayRow(0), 0),
1176                    SelectionGoal::HorizontalPosition(col_2_x.0),
1177                ),
1178            );
1179            assert_eq!(
1180                up(
1181                    &snapshot,
1182                    DisplayPoint::new(DisplayRow(0), 0),
1183                    SelectionGoal::None,
1184                    false,
1185                    &text_layout_details
1186                ),
1187                (
1188                    DisplayPoint::new(DisplayRow(0), 0),
1189                    SelectionGoal::HorizontalPosition(0.0),
1190                ),
1191            );
1192
1193            let col_4_x = snapshot
1194                .x_for_display_point(DisplayPoint::new(DisplayRow(1), 4), &text_layout_details);
1195
1196            // Move up and down within first excerpt
1197            assert_eq!(
1198                up(
1199                    &snapshot,
1200                    DisplayPoint::new(DisplayRow(1), 4),
1201                    SelectionGoal::HorizontalPosition(col_4_x.0),
1202                    false,
1203                    &text_layout_details
1204                ),
1205                (
1206                    DisplayPoint::new(DisplayRow(0), 3),
1207                    SelectionGoal::HorizontalPosition(col_4_x.0)
1208                ),
1209            );
1210            assert_eq!(
1211                down(
1212                    &snapshot,
1213                    DisplayPoint::new(DisplayRow(0), 3),
1214                    SelectionGoal::HorizontalPosition(col_4_x.0),
1215                    false,
1216                    &text_layout_details
1217                ),
1218                (
1219                    DisplayPoint::new(DisplayRow(1), 4),
1220                    SelectionGoal::HorizontalPosition(col_4_x.0)
1221                ),
1222            );
1223
1224            let col_5_x = snapshot
1225                .x_for_display_point(DisplayPoint::new(DisplayRow(3), 5), &text_layout_details);
1226
1227            // Move up and down across second excerpt's header
1228            assert_eq!(
1229                up(
1230                    &snapshot,
1231                    DisplayPoint::new(DisplayRow(3), 5),
1232                    SelectionGoal::HorizontalPosition(col_5_x.0),
1233                    false,
1234                    &text_layout_details
1235                ),
1236                (
1237                    DisplayPoint::new(DisplayRow(1), 4),
1238                    SelectionGoal::HorizontalPosition(col_5_x.0)
1239                ),
1240            );
1241            assert_eq!(
1242                down(
1243                    &snapshot,
1244                    DisplayPoint::new(DisplayRow(1), 4),
1245                    SelectionGoal::HorizontalPosition(col_5_x.0),
1246                    false,
1247                    &text_layout_details
1248                ),
1249                (
1250                    DisplayPoint::new(DisplayRow(3), 5),
1251                    SelectionGoal::HorizontalPosition(col_5_x.0)
1252                ),
1253            );
1254
1255            let max_point_x = snapshot
1256                .x_for_display_point(DisplayPoint::new(DisplayRow(4), 2), &text_layout_details);
1257
1258            // Can't move down off the end, and attempting to do so leaves the selection goal unchanged
1259            assert_eq!(
1260                down(
1261                    &snapshot,
1262                    DisplayPoint::new(DisplayRow(4), 0),
1263                    SelectionGoal::HorizontalPosition(0.0),
1264                    false,
1265                    &text_layout_details
1266                ),
1267                (
1268                    DisplayPoint::new(DisplayRow(4), 2),
1269                    SelectionGoal::HorizontalPosition(0.0)
1270                ),
1271            );
1272            assert_eq!(
1273                down(
1274                    &snapshot,
1275                    DisplayPoint::new(DisplayRow(4), 2),
1276                    SelectionGoal::HorizontalPosition(max_point_x.0),
1277                    false,
1278                    &text_layout_details
1279                ),
1280                (
1281                    DisplayPoint::new(DisplayRow(4), 2),
1282                    SelectionGoal::HorizontalPosition(max_point_x.0)
1283                ),
1284            );
1285        });
1286    }
1287
1288    fn init_test(cx: &mut gpui::App) {
1289        let settings_store = SettingsStore::test(cx);
1290        cx.set_global(settings_store);
1291        workspace::init_settings(cx);
1292        theme::init(theme::LoadThemes::JustBase, cx);
1293        language::init(cx);
1294        crate::init(cx);
1295        Project::init_settings(cx);
1296    }
1297}