movement.rs

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