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