movement.rs

  1use super::{Bias, DisplayPoint, DisplaySnapshot, SelectionGoal, ToDisplayPoint};
  2use crate::{char_kind, CharKind, ToPoint};
  3use language::Point;
  4use std::ops::Range;
  5
  6pub fn left(map: &DisplaySnapshot, mut point: DisplayPoint) -> DisplayPoint {
  7    if point.column() > 0 {
  8        *point.column_mut() -= 1;
  9    } else if point.row() > 0 {
 10        *point.row_mut() -= 1;
 11        *point.column_mut() = map.line_len(point.row());
 12    }
 13    map.clip_point(point, Bias::Left)
 14}
 15
 16pub fn saturating_left(map: &DisplaySnapshot, mut point: DisplayPoint) -> DisplayPoint {
 17    if point.column() > 0 {
 18        *point.column_mut() -= 1;
 19    }
 20    map.clip_point(point, Bias::Left)
 21}
 22
 23pub fn right(map: &DisplaySnapshot, mut point: DisplayPoint) -> DisplayPoint {
 24    let max_column = map.line_len(point.row());
 25    if point.column() < max_column {
 26        *point.column_mut() += 1;
 27    } else if point.row() < map.max_point().row() {
 28        *point.row_mut() += 1;
 29        *point.column_mut() = 0;
 30    }
 31    map.clip_point(point, Bias::Right)
 32}
 33
 34pub fn saturating_right(map: &DisplaySnapshot, mut point: DisplayPoint) -> DisplayPoint {
 35    *point.column_mut() += 1;
 36    map.clip_point(point, Bias::Right)
 37}
 38
 39pub fn up(
 40    map: &DisplaySnapshot,
 41    start: DisplayPoint,
 42    goal: SelectionGoal,
 43    preserve_column_at_start: bool,
 44) -> (DisplayPoint, SelectionGoal) {
 45    up_by_rows(map, start, 1, goal, preserve_column_at_start)
 46}
 47
 48pub fn down(
 49    map: &DisplaySnapshot,
 50    start: DisplayPoint,
 51    goal: SelectionGoal,
 52    preserve_column_at_end: bool,
 53) -> (DisplayPoint, SelectionGoal) {
 54    down_by_rows(map, start, 1, goal, preserve_column_at_end)
 55}
 56
 57pub fn up_by_rows(
 58    map: &DisplaySnapshot,
 59    start: DisplayPoint,
 60    row_count: u32,
 61    goal: SelectionGoal,
 62    preserve_column_at_start: bool,
 63) -> (DisplayPoint, SelectionGoal) {
 64    let mut goal_column = if let SelectionGoal::Column(column) = goal {
 65        column
 66    } else {
 67        map.column_to_chars(start.row(), start.column())
 68    };
 69
 70    let prev_row = start.row().saturating_sub(row_count);
 71    let mut point = map.clip_point(
 72        DisplayPoint::new(prev_row, map.line_len(prev_row)),
 73        Bias::Left,
 74    );
 75    if point.row() < start.row() {
 76        *point.column_mut() = map.column_from_chars(point.row(), goal_column);
 77    } else if preserve_column_at_start {
 78        return (start, goal);
 79    } else {
 80        point = DisplayPoint::new(0, 0);
 81        goal_column = 0;
 82    }
 83
 84    let mut clipped_point = map.clip_point(point, Bias::Left);
 85    if clipped_point.row() < point.row() {
 86        clipped_point = map.clip_point(point, Bias::Right);
 87    }
 88    (clipped_point, SelectionGoal::Column(goal_column))
 89}
 90
 91pub fn down_by_rows(
 92    map: &DisplaySnapshot,
 93    start: DisplayPoint,
 94    row_count: u32,
 95    goal: SelectionGoal,
 96    preserve_column_at_end: bool,
 97) -> (DisplayPoint, SelectionGoal) {
 98    let mut goal_column = if let SelectionGoal::Column(column) = goal {
 99        column
100    } else {
101        map.column_to_chars(start.row(), start.column())
102    };
103
104    let new_row = start.row() + row_count;
105    let mut point = map.clip_point(DisplayPoint::new(new_row, 0), Bias::Right);
106    if point.row() > start.row() {
107        *point.column_mut() = map.column_from_chars(point.row(), goal_column);
108    } else if preserve_column_at_end {
109        return (start, goal);
110    } else {
111        point = map.max_point();
112        goal_column = map.column_to_chars(point.row(), point.column())
113    }
114
115    let mut clipped_point = map.clip_point(point, Bias::Right);
116    if clipped_point.row() > point.row() {
117        clipped_point = map.clip_point(point, Bias::Left);
118    }
119    (clipped_point, SelectionGoal::Column(goal_column))
120}
121
122pub fn line_beginning(
123    map: &DisplaySnapshot,
124    display_point: DisplayPoint,
125    stop_at_soft_boundaries: bool,
126) -> DisplayPoint {
127    let point = display_point.to_point(map);
128    let soft_line_start = map.clip_point(DisplayPoint::new(display_point.row(), 0), Bias::Right);
129    let line_start = map.prev_line_boundary(point).1;
130
131    if stop_at_soft_boundaries && display_point != soft_line_start {
132        soft_line_start
133    } else {
134        line_start
135    }
136}
137
138pub fn indented_line_beginning(
139    map: &DisplaySnapshot,
140    display_point: DisplayPoint,
141    stop_at_soft_boundaries: bool,
142) -> DisplayPoint {
143    let point = display_point.to_point(map);
144    let soft_line_start = map.clip_point(DisplayPoint::new(display_point.row(), 0), Bias::Right);
145    let indent_start = Point::new(
146        point.row,
147        map.buffer_snapshot.indent_size_for_line(point.row).len,
148    )
149    .to_display_point(map);
150    let line_start = map.prev_line_boundary(point).1;
151
152    if stop_at_soft_boundaries && soft_line_start > indent_start && display_point != soft_line_start
153    {
154        soft_line_start
155    } else if stop_at_soft_boundaries && display_point != indent_start {
156        indent_start
157    } else {
158        line_start
159    }
160}
161
162pub fn line_end(
163    map: &DisplaySnapshot,
164    display_point: DisplayPoint,
165    stop_at_soft_boundaries: bool,
166) -> DisplayPoint {
167    let soft_line_end = map.clip_point(
168        DisplayPoint::new(display_point.row(), map.line_len(display_point.row())),
169        Bias::Left,
170    );
171    if stop_at_soft_boundaries && display_point != soft_line_end {
172        soft_line_end
173    } else {
174        map.next_line_boundary(display_point.to_point(map)).1
175    }
176}
177
178pub fn previous_word_start(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
179    let language = map.buffer_snapshot.language_at(point);
180    find_preceding_boundary(map, point, |left, right| {
181        (char_kind(language, left) != char_kind(language, right) && !right.is_whitespace())
182            || left == '\n'
183    })
184}
185
186pub fn previous_subword_start(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
187    let language = map.buffer_snapshot.language_at(point);
188    find_preceding_boundary(map, point, |left, right| {
189        let is_word_start =
190            char_kind(language, left) != char_kind(language, right) && !right.is_whitespace();
191        let is_subword_start =
192            left == '_' && right != '_' || left.is_lowercase() && right.is_uppercase();
193        is_word_start || is_subword_start || left == '\n'
194    })
195}
196
197pub fn next_word_end(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
198    find_boundary(map, point, |left, right| {
199        (char_kind(left) != char_kind(right) && !left.is_whitespace()) || right == '\n'
200    })
201}
202
203pub fn next_subword_end(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
204    find_boundary(map, point, |left, right| {
205        let is_word_end = (char_kind(left) != char_kind(right)) && !left.is_whitespace();
206        let is_subword_end =
207            left != '_' && right == '_' || left.is_lowercase() && right.is_uppercase();
208        is_word_end || is_subword_end || right == '\n'
209    })
210}
211
212pub fn start_of_paragraph(
213    map: &DisplaySnapshot,
214    display_point: DisplayPoint,
215    mut count: usize,
216) -> DisplayPoint {
217    let point = display_point.to_point(map);
218    if point.row == 0 {
219        return map.max_point();
220    }
221
222    let mut found_non_blank_line = false;
223    for row in (0..point.row + 1).rev() {
224        let blank = map.buffer_snapshot.is_line_blank(row);
225        if found_non_blank_line && blank {
226            if count <= 1 {
227                return Point::new(row, 0).to_display_point(map);
228            }
229            count -= 1;
230            found_non_blank_line = false;
231        }
232
233        found_non_blank_line |= !blank;
234    }
235
236    DisplayPoint::zero()
237}
238
239pub fn end_of_paragraph(
240    map: &DisplaySnapshot,
241    display_point: DisplayPoint,
242    mut count: usize,
243) -> DisplayPoint {
244    let point = display_point.to_point(map);
245    if point.row == map.max_buffer_row() {
246        return DisplayPoint::zero();
247    }
248
249    let mut found_non_blank_line = false;
250    for row in point.row..map.max_buffer_row() + 1 {
251        let blank = map.buffer_snapshot.is_line_blank(row);
252        if found_non_blank_line && blank {
253            if count <= 1 {
254                return Point::new(row, 0).to_display_point(map);
255            }
256            count -= 1;
257            found_non_blank_line = false;
258        }
259
260        found_non_blank_line |= !blank;
261    }
262
263    map.max_point()
264}
265
266/// Scans for a boundary preceding the given start point `from` until a boundary is found, indicated by the
267/// given predicate returning true. The predicate is called with the character to the left and right
268/// of the candidate boundary location, and will be called with `\n` characters indicating the start
269/// or end of a line.
270pub fn find_preceding_boundary(
271    map: &DisplaySnapshot,
272    from: DisplayPoint,
273    mut is_boundary: impl FnMut(char, char) -> bool,
274) -> DisplayPoint {
275    let mut start_column = 0;
276    let mut soft_wrap_row = from.row() + 1;
277
278    let mut prev = None;
279    for (ch, point) in map.reverse_chars_at(from) {
280        // Recompute soft_wrap_indent if the row has changed
281        if point.row() != soft_wrap_row {
282            soft_wrap_row = point.row();
283
284            if point.row() == 0 {
285                start_column = 0;
286            } else if let Some(indent) = map.soft_wrap_indent(point.row() - 1) {
287                start_column = indent;
288            }
289        }
290
291        // If the current point is in the soft_wrap, skip comparing it
292        if point.column() < start_column {
293            continue;
294        }
295
296        if let Some((prev_ch, prev_point)) = prev {
297            if is_boundary(ch, prev_ch) {
298                return map.clip_point(prev_point, Bias::Left);
299            }
300        }
301
302        prev = Some((ch, point));
303    }
304    map.clip_point(DisplayPoint::zero(), Bias::Left)
305}
306
307/// Scans for a boundary preceding the given start point `from` until a boundary is found, indicated by the
308/// given predicate returning true. The predicate is called with the character to the left and right
309/// of the candidate boundary location, and will be called with `\n` characters indicating the start
310/// or end of a line. If no boundary is found, the start of the line is returned.
311pub fn find_preceding_boundary_in_line(
312    map: &DisplaySnapshot,
313    from: DisplayPoint,
314    mut is_boundary: impl FnMut(char, char) -> bool,
315) -> DisplayPoint {
316    let mut start_column = 0;
317    if from.row() > 0 {
318        if let Some(indent) = map.soft_wrap_indent(from.row() - 1) {
319            start_column = indent;
320        }
321    }
322
323    let mut prev = None;
324    for (ch, point) in map.reverse_chars_at(from) {
325        if let Some((prev_ch, prev_point)) = prev {
326            if is_boundary(ch, prev_ch) {
327                return map.clip_point(prev_point, Bias::Left);
328            }
329        }
330
331        if ch == '\n' || point.column() < start_column {
332            break;
333        }
334
335        prev = Some((ch, point));
336    }
337
338    map.clip_point(prev.map(|(_, point)| point).unwrap_or(from), Bias::Left)
339}
340
341/// Scans for a boundary following the given start point until a boundary is found, indicated by the
342/// given predicate returning true. The predicate is called with the character to the left and right
343/// of the candidate boundary location, and will be called with `\n` characters indicating the start
344/// or end of a line.
345pub fn find_boundary(
346    map: &DisplaySnapshot,
347    from: DisplayPoint,
348    mut is_boundary: impl FnMut(char, char) -> bool,
349) -> DisplayPoint {
350    let mut prev_ch = None;
351    for (ch, point) in map.chars_at(from) {
352        if let Some(prev_ch) = prev_ch {
353            if is_boundary(prev_ch, ch) {
354                return map.clip_point(point, Bias::Right);
355            }
356        }
357
358        prev_ch = Some(ch);
359    }
360    map.clip_point(map.max_point(), Bias::Right)
361}
362
363/// Scans for a boundary following the given start point until a boundary is found, indicated by the
364/// given predicate returning true. The predicate is called with the character to the left and right
365/// of the candidate boundary location, and will be called with `\n` characters indicating the start
366/// or end of a line. If no boundary is found, the end of the line is returned
367pub fn find_boundary_in_line(
368    map: &DisplaySnapshot,
369    from: DisplayPoint,
370    mut is_boundary: impl FnMut(char, char) -> bool,
371) -> DisplayPoint {
372    let mut prev = None;
373    for (ch, point) in map.chars_at(from) {
374        if let Some((prev_ch, _)) = prev {
375            if is_boundary(prev_ch, ch) {
376                return map.clip_point(point, Bias::Right);
377            }
378        }
379
380        prev = Some((ch, point));
381
382        if ch == '\n' {
383            break;
384        }
385    }
386
387    // Return the last position checked so that we give a point right before the newline or eof.
388    map.clip_point(prev.map(|(_, point)| point).unwrap_or(from), Bias::Right)
389}
390
391pub fn is_inside_word(map: &DisplaySnapshot, point: DisplayPoint) -> bool {
392    let ix = map.clip_point(point, Bias::Left).to_offset(map, Bias::Left);
393    let text = &map.buffer_snapshot;
394    let next_char_kind = text.chars_at(ix).next().map(char_kind);
395    let prev_char_kind = text.reversed_chars_at(ix).next().map(char_kind);
396    prev_char_kind.zip(next_char_kind) == Some((CharKind::Word, CharKind::Word))
397}
398
399pub fn surrounding_word(map: &DisplaySnapshot, position: DisplayPoint) -> Range<DisplayPoint> {
400    let position = map
401        .clip_point(position, Bias::Left)
402        .to_offset(map, Bias::Left);
403    let (range, _) = map.buffer_snapshot.surrounding_word(position);
404    let start = range
405        .start
406        .to_point(&map.buffer_snapshot)
407        .to_display_point(map);
408    let end = range
409        .end
410        .to_point(&map.buffer_snapshot)
411        .to_display_point(map);
412    start..end
413}
414
415pub fn split_display_range_by_lines(
416    map: &DisplaySnapshot,
417    range: Range<DisplayPoint>,
418) -> Vec<Range<DisplayPoint>> {
419    let mut result = Vec::new();
420
421    let mut start = range.start;
422    // Loop over all the covered rows until the one containing the range end
423    for row in range.start.row()..range.end.row() {
424        let row_end_column = map.line_len(row);
425        let end = map.clip_point(DisplayPoint::new(row, row_end_column), Bias::Left);
426        if start != end {
427            result.push(start..end);
428        }
429        start = map.clip_point(DisplayPoint::new(row + 1, 0), Bias::Left);
430    }
431
432    // Add the final range from the start of the last end to the original range end.
433    result.push(start..range.end);
434
435    result
436}
437
438#[cfg(test)]
439mod tests {
440    use super::*;
441    use crate::{
442        display_map::Inlay, test::marked_display_snapshot, Buffer, DisplayMap, ExcerptRange,
443        InlayId, MultiBuffer,
444    };
445    use settings::SettingsStore;
446    use util::post_inc;
447
448    #[gpui::test]
449    fn test_previous_word_start(cx: &mut gpui::AppContext) {
450        init_test(cx);
451
452        fn assert(marked_text: &str, cx: &mut gpui::AppContext) {
453            let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
454            assert_eq!(
455                previous_word_start(&snapshot, display_points[1]),
456                display_points[0]
457            );
458        }
459
460        assert("\nˇ   ˇlorem", cx);
461        assert("ˇ\nˇ   lorem", cx);
462        assert("    ˇloremˇ", cx);
463        assert("ˇ    ˇlorem", cx);
464        assert("    ˇlorˇem", cx);
465        assert("\nlorem\nˇ   ˇipsum", cx);
466        assert("\n\nˇ\nˇ", cx);
467        assert("    ˇlorem  ˇipsum", cx);
468        assert("loremˇ-ˇipsum", cx);
469        assert("loremˇ-#$@ˇipsum", cx);
470        assert("ˇlorem_ˇipsum", cx);
471        assert(" ˇdefγˇ", cx);
472        assert(" ˇbcΔˇ", cx);
473        assert(" abˇ——ˇcd", cx);
474    }
475
476    #[gpui::test]
477    fn test_previous_subword_start(cx: &mut gpui::AppContext) {
478        init_test(cx);
479
480        fn assert(marked_text: &str, cx: &mut gpui::AppContext) {
481            let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
482            assert_eq!(
483                previous_subword_start(&snapshot, display_points[1]),
484                display_points[0]
485            );
486        }
487
488        // Subword boundaries are respected
489        assert("lorem_ˇipˇsum", cx);
490        assert("lorem_ˇipsumˇ", cx);
491        assert("ˇlorem_ˇipsum", cx);
492        assert("lorem_ˇipsum_ˇdolor", cx);
493        assert("loremˇIpˇsum", cx);
494        assert("loremˇIpsumˇ", cx);
495
496        // Word boundaries are still respected
497        assert("\nˇ   ˇlorem", cx);
498        assert("    ˇloremˇ", cx);
499        assert("    ˇlorˇem", cx);
500        assert("\nlorem\nˇ   ˇipsum", cx);
501        assert("\n\nˇ\nˇ", cx);
502        assert("    ˇlorem  ˇipsum", cx);
503        assert("loremˇ-ˇipsum", cx);
504        assert("loremˇ-#$@ˇipsum", cx);
505        assert(" ˇdefγˇ", cx);
506        assert(" bcˇΔˇ", cx);
507        assert(" ˇbcδˇ", cx);
508        assert(" abˇ——ˇcd", cx);
509    }
510
511    #[gpui::test]
512    fn test_find_preceding_boundary(cx: &mut gpui::AppContext) {
513        init_test(cx);
514
515        fn assert(
516            marked_text: &str,
517            cx: &mut gpui::AppContext,
518            is_boundary: impl FnMut(char, char) -> bool,
519        ) {
520            let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
521            assert_eq!(
522                find_preceding_boundary(&snapshot, display_points[1], is_boundary),
523                display_points[0]
524            );
525        }
526
527        assert("abcˇdef\ngh\nijˇk", cx, |left, right| {
528            left == 'c' && right == 'd'
529        });
530        assert("abcdef\nˇgh\nijˇk", cx, |left, right| {
531            left == '\n' && right == 'g'
532        });
533        let mut line_count = 0;
534        assert("abcdef\nˇgh\nijˇk", cx, |left, _| {
535            if left == '\n' {
536                line_count += 1;
537                line_count == 2
538            } else {
539                false
540            }
541        });
542    }
543
544    #[gpui::test]
545    fn test_find_preceding_boundary_with_inlays(cx: &mut gpui::AppContext) {
546        init_test(cx);
547
548        let input_text = "abcdefghijklmnopqrstuvwxys";
549        let family_id = cx
550            .font_cache()
551            .load_family(&["Helvetica"], &Default::default())
552            .unwrap();
553        let font_id = cx
554            .font_cache()
555            .select_font(family_id, &Default::default())
556            .unwrap();
557        let font_size = 14.0;
558        let buffer = MultiBuffer::build_simple(input_text, cx);
559        let buffer_snapshot = buffer.read(cx).snapshot(cx);
560        let display_map =
561            cx.add_model(|cx| DisplayMap::new(buffer, font_id, font_size, None, 1, 1, cx));
562
563        // add all kinds of inlays between two word boundaries: we should be able to cross them all, when looking for another boundary
564        let mut id = 0;
565        let inlays = (0..buffer_snapshot.len())
566            .map(|offset| {
567                [
568                    Inlay {
569                        id: InlayId::Suggestion(post_inc(&mut id)),
570                        position: buffer_snapshot.anchor_at(offset, Bias::Left),
571                        text: format!("test").into(),
572                    },
573                    Inlay {
574                        id: InlayId::Suggestion(post_inc(&mut id)),
575                        position: buffer_snapshot.anchor_at(offset, Bias::Right),
576                        text: format!("test").into(),
577                    },
578                    Inlay {
579                        id: InlayId::Hint(post_inc(&mut id)),
580                        position: buffer_snapshot.anchor_at(offset, Bias::Left),
581                        text: format!("test").into(),
582                    },
583                    Inlay {
584                        id: InlayId::Hint(post_inc(&mut id)),
585                        position: buffer_snapshot.anchor_at(offset, Bias::Right),
586                        text: format!("test").into(),
587                    },
588                ]
589            })
590            .flatten()
591            .collect();
592        let snapshot = display_map.update(cx, |map, cx| {
593            map.splice_inlays(Vec::new(), inlays, cx);
594            map.snapshot(cx)
595        });
596
597        assert_eq!(
598            find_preceding_boundary(
599                &snapshot,
600                buffer_snapshot.len().to_display_point(&snapshot),
601                |left, _| left == 'a',
602            ),
603            0.to_display_point(&snapshot),
604            "Should not stop at inlays when looking for boundaries"
605        );
606
607        assert_eq!(
608            find_preceding_boundary_in_line(
609                &snapshot,
610                buffer_snapshot.len().to_display_point(&snapshot),
611                |left, _| left == 'a',
612            ),
613            0.to_display_point(&snapshot),
614            "Should not stop at inlays when looking for boundaries in line"
615        );
616    }
617
618    #[gpui::test]
619    fn test_next_word_end(cx: &mut gpui::AppContext) {
620        init_test(cx);
621
622        fn assert(marked_text: &str, cx: &mut gpui::AppContext) {
623            let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
624            assert_eq!(
625                next_word_end(&snapshot, display_points[0]),
626                display_points[1]
627            );
628        }
629
630        assert("\nˇ   loremˇ", cx);
631        assert("    ˇloremˇ", cx);
632        assert("    lorˇemˇ", cx);
633        assert("    loremˇ    ˇ\nipsum\n", cx);
634        assert("\nˇ\nˇ\n\n", cx);
635        assert("loremˇ    ipsumˇ   ", cx);
636        assert("loremˇ-ˇipsum", cx);
637        assert("loremˇ#$@-ˇipsum", cx);
638        assert("loremˇ_ipsumˇ", cx);
639        assert(" ˇbcΔˇ", cx);
640        assert(" abˇ——ˇcd", cx);
641    }
642
643    #[gpui::test]
644    fn test_next_subword_end(cx: &mut gpui::AppContext) {
645        init_test(cx);
646
647        fn assert(marked_text: &str, cx: &mut gpui::AppContext) {
648            let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
649            assert_eq!(
650                next_subword_end(&snapshot, display_points[0]),
651                display_points[1]
652            );
653        }
654
655        // Subword boundaries are respected
656        assert("loˇremˇ_ipsum", cx);
657        assert("ˇloremˇ_ipsum", cx);
658        assert("loremˇ_ipsumˇ", cx);
659        assert("loremˇ_ipsumˇ_dolor", cx);
660        assert("loˇremˇIpsum", cx);
661        assert("loremˇIpsumˇDolor", cx);
662
663        // Word boundaries are still respected
664        assert("\nˇ   loremˇ", cx);
665        assert("    ˇloremˇ", cx);
666        assert("    lorˇemˇ", cx);
667        assert("    loremˇ    ˇ\nipsum\n", cx);
668        assert("\nˇ\nˇ\n\n", cx);
669        assert("loremˇ    ipsumˇ   ", cx);
670        assert("loremˇ-ˇipsum", cx);
671        assert("loremˇ#$@-ˇipsum", cx);
672        assert("loremˇ_ipsumˇ", cx);
673        assert(" ˇbcˇΔ", cx);
674        assert(" abˇ——ˇcd", cx);
675    }
676
677    #[gpui::test]
678    fn test_find_boundary(cx: &mut gpui::AppContext) {
679        init_test(cx);
680
681        fn assert(
682            marked_text: &str,
683            cx: &mut gpui::AppContext,
684            is_boundary: impl FnMut(char, char) -> bool,
685        ) {
686            let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
687            assert_eq!(
688                find_boundary(&snapshot, display_points[0], is_boundary),
689                display_points[1]
690            );
691        }
692
693        assert("abcˇdef\ngh\nijˇk", cx, |left, right| {
694            left == 'j' && right == 'k'
695        });
696        assert("abˇcdef\ngh\nˇijk", cx, |left, right| {
697            left == '\n' && right == 'i'
698        });
699        let mut line_count = 0;
700        assert("abcˇdef\ngh\nˇijk", cx, |left, _| {
701            if left == '\n' {
702                line_count += 1;
703                line_count == 2
704            } else {
705                false
706            }
707        });
708    }
709
710    #[gpui::test]
711    fn test_surrounding_word(cx: &mut gpui::AppContext) {
712        init_test(cx);
713
714        fn assert(marked_text: &str, cx: &mut gpui::AppContext) {
715            let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
716            assert_eq!(
717                surrounding_word(&snapshot, display_points[1]),
718                display_points[0]..display_points[2]
719            );
720        }
721
722        assert("ˇˇloremˇ  ipsum", cx);
723        assert("ˇloˇremˇ  ipsum", cx);
724        assert("ˇloremˇˇ  ipsum", cx);
725        assert("loremˇ ˇ  ˇipsum", cx);
726        assert("lorem\nˇˇˇ\nipsum", cx);
727        assert("lorem\nˇˇipsumˇ", cx);
728        assert("lorem,ˇˇ ˇipsum", cx);
729        assert("ˇloremˇˇ, ipsum", cx);
730    }
731
732    #[gpui::test]
733    fn test_move_up_and_down_with_excerpts(cx: &mut gpui::AppContext) {
734        init_test(cx);
735
736        let family_id = cx
737            .font_cache()
738            .load_family(&["Helvetica"], &Default::default())
739            .unwrap();
740        let font_id = cx
741            .font_cache()
742            .select_font(family_id, &Default::default())
743            .unwrap();
744
745        let buffer = cx.add_model(|cx| Buffer::new(0, "abc\ndefg\nhijkl\nmn", cx));
746        let multibuffer = cx.add_model(|cx| {
747            let mut multibuffer = MultiBuffer::new(0);
748            multibuffer.push_excerpts(
749                buffer.clone(),
750                [
751                    ExcerptRange {
752                        context: Point::new(0, 0)..Point::new(1, 4),
753                        primary: None,
754                    },
755                    ExcerptRange {
756                        context: Point::new(2, 0)..Point::new(3, 2),
757                        primary: None,
758                    },
759                ],
760                cx,
761            );
762            multibuffer
763        });
764        let display_map =
765            cx.add_model(|cx| DisplayMap::new(multibuffer, font_id, 14.0, None, 2, 2, cx));
766        let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
767
768        assert_eq!(snapshot.text(), "\n\nabc\ndefg\n\n\nhijkl\nmn");
769
770        // Can't move up into the first excerpt's header
771        assert_eq!(
772            up(
773                &snapshot,
774                DisplayPoint::new(2, 2),
775                SelectionGoal::Column(2),
776                false
777            ),
778            (DisplayPoint::new(2, 0), SelectionGoal::Column(0)),
779        );
780        assert_eq!(
781            up(
782                &snapshot,
783                DisplayPoint::new(2, 0),
784                SelectionGoal::None,
785                false
786            ),
787            (DisplayPoint::new(2, 0), SelectionGoal::Column(0)),
788        );
789
790        // Move up and down within first excerpt
791        assert_eq!(
792            up(
793                &snapshot,
794                DisplayPoint::new(3, 4),
795                SelectionGoal::Column(4),
796                false
797            ),
798            (DisplayPoint::new(2, 3), SelectionGoal::Column(4)),
799        );
800        assert_eq!(
801            down(
802                &snapshot,
803                DisplayPoint::new(2, 3),
804                SelectionGoal::Column(4),
805                false
806            ),
807            (DisplayPoint::new(3, 4), SelectionGoal::Column(4)),
808        );
809
810        // Move up and down across second excerpt's header
811        assert_eq!(
812            up(
813                &snapshot,
814                DisplayPoint::new(6, 5),
815                SelectionGoal::Column(5),
816                false
817            ),
818            (DisplayPoint::new(3, 4), SelectionGoal::Column(5)),
819        );
820        assert_eq!(
821            down(
822                &snapshot,
823                DisplayPoint::new(3, 4),
824                SelectionGoal::Column(5),
825                false
826            ),
827            (DisplayPoint::new(6, 5), SelectionGoal::Column(5)),
828        );
829
830        // Can't move down off the end
831        assert_eq!(
832            down(
833                &snapshot,
834                DisplayPoint::new(7, 0),
835                SelectionGoal::Column(0),
836                false
837            ),
838            (DisplayPoint::new(7, 2), SelectionGoal::Column(2)),
839        );
840        assert_eq!(
841            down(
842                &snapshot,
843                DisplayPoint::new(7, 2),
844                SelectionGoal::Column(2),
845                false
846            ),
847            (DisplayPoint::new(7, 2), SelectionGoal::Column(2)),
848        );
849    }
850
851    fn init_test(cx: &mut gpui::AppContext) {
852        cx.set_global(SettingsStore::test(cx));
853        theme::init((), cx);
854        language::init(cx);
855        crate::init(cx);
856    }
857}