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(
197 map: &DisplaySnapshot,
198 display_point: DisplayPoint,
199 mut count: usize,
200) -> DisplayPoint {
201 let point = display_point.to_point(map);
202 if point.row == 0 {
203 return map.max_point();
204 }
205
206 let mut found_non_blank_line = false;
207 for row in (0..point.row + 1).rev() {
208 let blank = map.buffer_snapshot.is_line_blank(row);
209 if found_non_blank_line && blank {
210 if count <= 1 {
211 return Point::new(row, 0).to_display_point(map);
212 }
213 count -= 1;
214 found_non_blank_line = false;
215 }
216
217 found_non_blank_line |= !blank;
218 }
219
220 DisplayPoint::zero()
221}
222
223pub fn end_of_paragraph(
224 map: &DisplaySnapshot,
225 display_point: DisplayPoint,
226 mut count: usize,
227) -> DisplayPoint {
228 let point = display_point.to_point(map);
229 if point.row == map.max_buffer_row() {
230 return DisplayPoint::zero();
231 }
232
233 let mut found_non_blank_line = false;
234 for row in point.row..map.max_buffer_row() + 1 {
235 let blank = map.buffer_snapshot.is_line_blank(row);
236 if found_non_blank_line && blank {
237 if count <= 1 {
238 return Point::new(row, 0).to_display_point(map);
239 }
240 count -= 1;
241 found_non_blank_line = false;
242 }
243
244 found_non_blank_line |= !blank;
245 }
246
247 map.max_point()
248}
249
250/// Scans for a boundary preceding the given start point `from` until a boundary is found, indicated by the
251/// given predicate returning true. The predicate is called with the character to the left and right
252/// of the candidate boundary location, and will be called with `\n` characters indicating the start
253/// or end of a line.
254pub fn find_preceding_boundary(
255 map: &DisplaySnapshot,
256 from: DisplayPoint,
257 mut is_boundary: impl FnMut(char, char) -> bool,
258) -> DisplayPoint {
259 let mut start_column = 0;
260 let mut soft_wrap_row = from.row() + 1;
261
262 let mut prev = None;
263 for (ch, point) in map.reverse_chars_at(from) {
264 // Recompute soft_wrap_indent if the row has changed
265 if point.row() != soft_wrap_row {
266 soft_wrap_row = point.row();
267
268 if point.row() == 0 {
269 start_column = 0;
270 } else if let Some(indent) = map.soft_wrap_indent(point.row() - 1) {
271 start_column = indent;
272 }
273 }
274
275 // If the current point is in the soft_wrap, skip comparing it
276 if point.column() < start_column {
277 continue;
278 }
279
280 if let Some((prev_ch, prev_point)) = prev {
281 if is_boundary(ch, prev_ch) {
282 return map.clip_point(prev_point, Bias::Left);
283 }
284 }
285
286 prev = Some((ch, point));
287 }
288 map.clip_point(DisplayPoint::zero(), Bias::Left)
289}
290
291/// Scans for a boundary preceding the given start point `from` until a boundary is found, indicated by the
292/// given predicate returning true. The predicate is called with the character to the left and right
293/// of the candidate boundary location, and will be called with `\n` characters indicating the start
294/// or end of a line. If no boundary is found, the start of the line is returned.
295pub fn find_preceding_boundary_in_line(
296 map: &DisplaySnapshot,
297 from: DisplayPoint,
298 mut is_boundary: impl FnMut(char, char) -> bool,
299) -> DisplayPoint {
300 let mut start_column = 0;
301 if from.row() > 0 {
302 if let Some(indent) = map.soft_wrap_indent(from.row() - 1) {
303 start_column = indent;
304 }
305 }
306
307 let mut prev = None;
308 for (ch, point) in map.reverse_chars_at(from) {
309 if let Some((prev_ch, prev_point)) = prev {
310 if is_boundary(ch, prev_ch) {
311 return map.clip_point(prev_point, Bias::Left);
312 }
313 }
314
315 if ch == '\n' || point.column() < start_column {
316 break;
317 }
318
319 prev = Some((ch, point));
320 }
321
322 map.clip_point(prev.map(|(_, point)| point).unwrap_or(from), Bias::Left)
323}
324
325/// Scans for a boundary following the given start point until a boundary is found, indicated by the
326/// given predicate returning true. The predicate is called with the character to the left and right
327/// of the candidate boundary location, and will be called with `\n` characters indicating the start
328/// or end of a line.
329pub fn find_boundary(
330 map: &DisplaySnapshot,
331 from: DisplayPoint,
332 mut is_boundary: impl FnMut(char, char) -> bool,
333) -> DisplayPoint {
334 let mut prev_ch = None;
335 for (ch, point) in map.chars_at(from) {
336 if let Some(prev_ch) = prev_ch {
337 if is_boundary(prev_ch, ch) {
338 return map.clip_point(point, Bias::Right);
339 }
340 }
341
342 prev_ch = Some(ch);
343 }
344 map.clip_point(map.max_point(), Bias::Right)
345}
346
347/// Scans for a boundary following the given start point until a boundary is found, indicated by the
348/// given predicate returning true. The predicate is called with the character to the left and right
349/// of the candidate boundary location, and will be called with `\n` characters indicating the start
350/// or end of a line. If no boundary is found, the end of the line is returned
351pub fn find_boundary_in_line(
352 map: &DisplaySnapshot,
353 from: DisplayPoint,
354 mut is_boundary: impl FnMut(char, char) -> bool,
355) -> DisplayPoint {
356 let mut prev = None;
357 for (ch, point) in map.chars_at(from) {
358 if let Some((prev_ch, _)) = prev {
359 if is_boundary(prev_ch, ch) {
360 return map.clip_point(point, Bias::Right);
361 }
362 }
363
364 prev = Some((ch, point));
365
366 if ch == '\n' {
367 break;
368 }
369 }
370
371 // Return the last position checked so that we give a point right before the newline or eof.
372 map.clip_point(prev.map(|(_, point)| point).unwrap_or(from), Bias::Right)
373}
374
375pub fn is_inside_word(map: &DisplaySnapshot, point: DisplayPoint) -> bool {
376 let ix = map.clip_point(point, Bias::Left).to_offset(map, Bias::Left);
377 let text = &map.buffer_snapshot;
378 let next_char_kind = text.chars_at(ix).next().map(char_kind);
379 let prev_char_kind = text.reversed_chars_at(ix).next().map(char_kind);
380 prev_char_kind.zip(next_char_kind) == Some((CharKind::Word, CharKind::Word))
381}
382
383pub fn surrounding_word(map: &DisplaySnapshot, position: DisplayPoint) -> Range<DisplayPoint> {
384 let position = map
385 .clip_point(position, Bias::Left)
386 .to_offset(map, Bias::Left);
387 let (range, _) = map.buffer_snapshot.surrounding_word(position);
388 let start = range
389 .start
390 .to_point(&map.buffer_snapshot)
391 .to_display_point(map);
392 let end = range
393 .end
394 .to_point(&map.buffer_snapshot)
395 .to_display_point(map);
396 start..end
397}
398
399pub fn split_display_range_by_lines(
400 map: &DisplaySnapshot,
401 range: Range<DisplayPoint>,
402) -> Vec<Range<DisplayPoint>> {
403 let mut result = Vec::new();
404
405 let mut start = range.start;
406 // Loop over all the covered rows until the one containing the range end
407 for row in range.start.row()..range.end.row() {
408 let row_end_column = map.line_len(row);
409 let end = map.clip_point(DisplayPoint::new(row, row_end_column), Bias::Left);
410 if start != end {
411 result.push(start..end);
412 }
413 start = map.clip_point(DisplayPoint::new(row + 1, 0), Bias::Left);
414 }
415
416 // Add the final range from the start of the last end to the original range end.
417 result.push(start..range.end);
418
419 result
420}
421
422#[cfg(test)]
423mod tests {
424 use super::*;
425 use crate::{
426 display_map::Inlay, test::marked_display_snapshot, Buffer, DisplayMap, ExcerptRange,
427 InlayId, MultiBuffer,
428 };
429 use settings::SettingsStore;
430 use util::post_inc;
431
432 #[gpui::test]
433 fn test_previous_word_start(cx: &mut gpui::AppContext) {
434 init_test(cx);
435
436 fn assert(marked_text: &str, cx: &mut gpui::AppContext) {
437 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
438 assert_eq!(
439 previous_word_start(&snapshot, display_points[1]),
440 display_points[0]
441 );
442 }
443
444 assert("\nˇ ˇlorem", cx);
445 assert("ˇ\nˇ lorem", cx);
446 assert(" ˇloremˇ", cx);
447 assert("ˇ ˇlorem", cx);
448 assert(" ˇlorˇem", cx);
449 assert("\nlorem\nˇ ˇipsum", cx);
450 assert("\n\nˇ\nˇ", cx);
451 assert(" ˇlorem ˇipsum", cx);
452 assert("loremˇ-ˇipsum", cx);
453 assert("loremˇ-#$@ˇipsum", cx);
454 assert("ˇlorem_ˇipsum", cx);
455 assert(" ˇdefγˇ", cx);
456 assert(" ˇbcΔˇ", cx);
457 assert(" abˇ——ˇcd", cx);
458 }
459
460 #[gpui::test]
461 fn test_previous_subword_start(cx: &mut gpui::AppContext) {
462 init_test(cx);
463
464 fn assert(marked_text: &str, cx: &mut gpui::AppContext) {
465 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
466 assert_eq!(
467 previous_subword_start(&snapshot, display_points[1]),
468 display_points[0]
469 );
470 }
471
472 // Subword boundaries are respected
473 assert("lorem_ˇipˇsum", cx);
474 assert("lorem_ˇipsumˇ", cx);
475 assert("ˇlorem_ˇipsum", cx);
476 assert("lorem_ˇipsum_ˇdolor", cx);
477 assert("loremˇIpˇsum", cx);
478 assert("loremˇIpsumˇ", cx);
479
480 // Word boundaries are still respected
481 assert("\nˇ ˇ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(" ˇdefγˇ", cx);
490 assert(" bcˇΔˇ", cx);
491 assert(" ˇbcδˇ", cx);
492 assert(" abˇ——ˇcd", cx);
493 }
494
495 #[gpui::test]
496 fn test_find_preceding_boundary(cx: &mut gpui::AppContext) {
497 init_test(cx);
498
499 fn assert(
500 marked_text: &str,
501 cx: &mut gpui::AppContext,
502 is_boundary: impl FnMut(char, char) -> bool,
503 ) {
504 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
505 assert_eq!(
506 find_preceding_boundary(&snapshot, display_points[1], is_boundary),
507 display_points[0]
508 );
509 }
510
511 assert("abcˇdef\ngh\nijˇk", cx, |left, right| {
512 left == 'c' && right == 'd'
513 });
514 assert("abcdef\nˇgh\nijˇk", cx, |left, right| {
515 left == '\n' && right == 'g'
516 });
517 let mut line_count = 0;
518 assert("abcdef\nˇgh\nijˇk", cx, |left, _| {
519 if left == '\n' {
520 line_count += 1;
521 line_count == 2
522 } else {
523 false
524 }
525 });
526 }
527
528 #[gpui::test]
529 fn test_find_preceding_boundary_with_inlays(cx: &mut gpui::AppContext) {
530 init_test(cx);
531
532 let input_text = "abcdefghijklmnopqrstuvwxys";
533 let family_id = cx
534 .font_cache()
535 .load_family(&["Helvetica"], &Default::default())
536 .unwrap();
537 let font_id = cx
538 .font_cache()
539 .select_font(family_id, &Default::default())
540 .unwrap();
541 let font_size = 14.0;
542 let buffer = MultiBuffer::build_simple(input_text, cx);
543 let buffer_snapshot = buffer.read(cx).snapshot(cx);
544 let display_map =
545 cx.add_model(|cx| DisplayMap::new(buffer, font_id, font_size, None, 1, 1, cx));
546
547 // add all kinds of inlays between two word boundaries: we should be able to cross them all, when looking for another boundary
548 let mut id = 0;
549 let inlays = (0..buffer_snapshot.len())
550 .map(|offset| {
551 [
552 Inlay {
553 id: InlayId::Suggestion(post_inc(&mut id)),
554 position: buffer_snapshot.anchor_at(offset, Bias::Left),
555 text: format!("test").into(),
556 },
557 Inlay {
558 id: InlayId::Suggestion(post_inc(&mut id)),
559 position: buffer_snapshot.anchor_at(offset, Bias::Right),
560 text: format!("test").into(),
561 },
562 Inlay {
563 id: InlayId::Hint(post_inc(&mut id)),
564 position: buffer_snapshot.anchor_at(offset, Bias::Left),
565 text: format!("test").into(),
566 },
567 Inlay {
568 id: InlayId::Hint(post_inc(&mut id)),
569 position: buffer_snapshot.anchor_at(offset, Bias::Right),
570 text: format!("test").into(),
571 },
572 ]
573 })
574 .flatten()
575 .collect();
576 let snapshot = display_map.update(cx, |map, cx| {
577 map.splice_inlays(Vec::new(), inlays, cx);
578 map.snapshot(cx)
579 });
580
581 assert_eq!(
582 find_preceding_boundary(
583 &snapshot,
584 buffer_snapshot.len().to_display_point(&snapshot),
585 |left, _| left == 'a',
586 ),
587 0.to_display_point(&snapshot),
588 "Should not stop at inlays when looking for boundaries"
589 );
590
591 assert_eq!(
592 find_preceding_boundary_in_line(
593 &snapshot,
594 buffer_snapshot.len().to_display_point(&snapshot),
595 |left, _| left == 'a',
596 ),
597 0.to_display_point(&snapshot),
598 "Should not stop at inlays when looking for boundaries in line"
599 );
600 }
601
602 #[gpui::test]
603 fn test_next_word_end(cx: &mut gpui::AppContext) {
604 init_test(cx);
605
606 fn assert(marked_text: &str, cx: &mut gpui::AppContext) {
607 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
608 assert_eq!(
609 next_word_end(&snapshot, display_points[0]),
610 display_points[1]
611 );
612 }
613
614 assert("\nˇ loremˇ", cx);
615 assert(" ˇloremˇ", cx);
616 assert(" lorˇemˇ", cx);
617 assert(" loremˇ ˇ\nipsum\n", cx);
618 assert("\nˇ\nˇ\n\n", cx);
619 assert("loremˇ ipsumˇ ", cx);
620 assert("loremˇ-ˇipsum", cx);
621 assert("loremˇ#$@-ˇipsum", cx);
622 assert("loremˇ_ipsumˇ", cx);
623 assert(" ˇbcΔˇ", cx);
624 assert(" abˇ——ˇcd", cx);
625 }
626
627 #[gpui::test]
628 fn test_next_subword_end(cx: &mut gpui::AppContext) {
629 init_test(cx);
630
631 fn assert(marked_text: &str, cx: &mut gpui::AppContext) {
632 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
633 assert_eq!(
634 next_subword_end(&snapshot, display_points[0]),
635 display_points[1]
636 );
637 }
638
639 // Subword boundaries are respected
640 assert("loˇremˇ_ipsum", cx);
641 assert("ˇloremˇ_ipsum", cx);
642 assert("loremˇ_ipsumˇ", cx);
643 assert("loremˇ_ipsumˇ_dolor", cx);
644 assert("loˇremˇIpsum", cx);
645 assert("loremˇIpsumˇDolor", cx);
646
647 // Word boundaries are still respected
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_find_boundary(cx: &mut gpui::AppContext) {
663 init_test(cx);
664
665 fn assert(
666 marked_text: &str,
667 cx: &mut gpui::AppContext,
668 is_boundary: impl FnMut(char, char) -> bool,
669 ) {
670 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
671 assert_eq!(
672 find_boundary(&snapshot, display_points[0], is_boundary),
673 display_points[1]
674 );
675 }
676
677 assert("abcˇdef\ngh\nijˇk", cx, |left, right| {
678 left == 'j' && right == 'k'
679 });
680 assert("abˇcdef\ngh\nˇijk", cx, |left, right| {
681 left == '\n' && right == 'i'
682 });
683 let mut line_count = 0;
684 assert("abcˇdef\ngh\nˇijk", cx, |left, _| {
685 if left == '\n' {
686 line_count += 1;
687 line_count == 2
688 } else {
689 false
690 }
691 });
692 }
693
694 #[gpui::test]
695 fn test_surrounding_word(cx: &mut gpui::AppContext) {
696 init_test(cx);
697
698 fn assert(marked_text: &str, cx: &mut gpui::AppContext) {
699 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
700 assert_eq!(
701 surrounding_word(&snapshot, display_points[1]),
702 display_points[0]..display_points[2]
703 );
704 }
705
706 assert("ˇˇloremˇ ipsum", cx);
707 assert("ˇloˇremˇ ipsum", cx);
708 assert("ˇloremˇˇ ipsum", cx);
709 assert("loremˇ ˇ ˇipsum", cx);
710 assert("lorem\nˇˇˇ\nipsum", cx);
711 assert("lorem\nˇˇipsumˇ", cx);
712 assert("lorem,ˇˇ ˇipsum", cx);
713 assert("ˇloremˇˇ, ipsum", cx);
714 }
715
716 #[gpui::test]
717 fn test_move_up_and_down_with_excerpts(cx: &mut gpui::AppContext) {
718 init_test(cx);
719
720 let family_id = cx
721 .font_cache()
722 .load_family(&["Helvetica"], &Default::default())
723 .unwrap();
724 let font_id = cx
725 .font_cache()
726 .select_font(family_id, &Default::default())
727 .unwrap();
728
729 let buffer = cx.add_model(|cx| Buffer::new(0, "abc\ndefg\nhijkl\nmn", cx));
730 let multibuffer = cx.add_model(|cx| {
731 let mut multibuffer = MultiBuffer::new(0);
732 multibuffer.push_excerpts(
733 buffer.clone(),
734 [
735 ExcerptRange {
736 context: Point::new(0, 0)..Point::new(1, 4),
737 primary: None,
738 },
739 ExcerptRange {
740 context: Point::new(2, 0)..Point::new(3, 2),
741 primary: None,
742 },
743 ],
744 cx,
745 );
746 multibuffer
747 });
748 let display_map =
749 cx.add_model(|cx| DisplayMap::new(multibuffer, font_id, 14.0, None, 2, 2, cx));
750 let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
751
752 assert_eq!(snapshot.text(), "\n\nabc\ndefg\n\n\nhijkl\nmn");
753
754 // Can't move up into the first excerpt's header
755 assert_eq!(
756 up(
757 &snapshot,
758 DisplayPoint::new(2, 2),
759 SelectionGoal::Column(2),
760 false
761 ),
762 (DisplayPoint::new(2, 0), SelectionGoal::Column(0)),
763 );
764 assert_eq!(
765 up(
766 &snapshot,
767 DisplayPoint::new(2, 0),
768 SelectionGoal::None,
769 false
770 ),
771 (DisplayPoint::new(2, 0), SelectionGoal::Column(0)),
772 );
773
774 // Move up and down within first excerpt
775 assert_eq!(
776 up(
777 &snapshot,
778 DisplayPoint::new(3, 4),
779 SelectionGoal::Column(4),
780 false
781 ),
782 (DisplayPoint::new(2, 3), SelectionGoal::Column(4)),
783 );
784 assert_eq!(
785 down(
786 &snapshot,
787 DisplayPoint::new(2, 3),
788 SelectionGoal::Column(4),
789 false
790 ),
791 (DisplayPoint::new(3, 4), SelectionGoal::Column(4)),
792 );
793
794 // Move up and down across second excerpt's header
795 assert_eq!(
796 up(
797 &snapshot,
798 DisplayPoint::new(6, 5),
799 SelectionGoal::Column(5),
800 false
801 ),
802 (DisplayPoint::new(3, 4), SelectionGoal::Column(5)),
803 );
804 assert_eq!(
805 down(
806 &snapshot,
807 DisplayPoint::new(3, 4),
808 SelectionGoal::Column(5),
809 false
810 ),
811 (DisplayPoint::new(6, 5), SelectionGoal::Column(5)),
812 );
813
814 // Can't move down off the end
815 assert_eq!(
816 down(
817 &snapshot,
818 DisplayPoint::new(7, 0),
819 SelectionGoal::Column(0),
820 false
821 ),
822 (DisplayPoint::new(7, 2), SelectionGoal::Column(2)),
823 );
824 assert_eq!(
825 down(
826 &snapshot,
827 DisplayPoint::new(7, 2),
828 SelectionGoal::Column(2),
829 false
830 ),
831 (DisplayPoint::new(7, 2), SelectionGoal::Column(2)),
832 );
833 }
834
835 fn init_test(cx: &mut gpui::AppContext) {
836 cx.set_global(SettingsStore::test(cx));
837 theme::init((), cx);
838 language::init(cx);
839 crate::init(cx);
840 }
841}