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