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