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 clip_bias = if point.column() == map.line_len(point.row()) {
73 Bias::Left
74 } else {
75 Bias::Right
76 };
77
78 (
79 map.clip_point(point, clip_bias),
80 SelectionGoal::Column(goal_column),
81 )
82}
83
84pub fn down_by_rows(
85 map: &DisplaySnapshot,
86 start: DisplayPoint,
87 row_count: u32,
88 goal: SelectionGoal,
89 preserve_column_at_end: bool,
90) -> (DisplayPoint, SelectionGoal) {
91 let mut goal_column = if let SelectionGoal::Column(column) = goal {
92 column
93 } else {
94 map.column_to_chars(start.row(), start.column())
95 };
96
97 let new_row = start.row() + row_count;
98 let mut point = map.clip_point(DisplayPoint::new(new_row, 0), Bias::Right);
99 if point.row() > start.row() {
100 *point.column_mut() = map.column_from_chars(point.row(), goal_column);
101 } else if preserve_column_at_end {
102 return (start, goal);
103 } else {
104 point = map.max_point();
105 goal_column = map.column_to_chars(point.row(), point.column())
106 }
107
108 let clip_bias = if point.column() == map.line_len(point.row()) {
109 Bias::Left
110 } else {
111 Bias::Right
112 };
113
114 (
115 map.clip_point(point, clip_bias),
116 SelectionGoal::Column(goal_column),
117 )
118}
119
120pub fn line_beginning(
121 map: &DisplaySnapshot,
122 display_point: DisplayPoint,
123 stop_at_soft_boundaries: bool,
124) -> DisplayPoint {
125 let point = display_point.to_point(map);
126 let soft_line_start = map.clip_point(DisplayPoint::new(display_point.row(), 0), Bias::Right);
127 let line_start = map.prev_line_boundary(point).1;
128
129 if stop_at_soft_boundaries && display_point != soft_line_start {
130 soft_line_start
131 } else {
132 line_start
133 }
134}
135
136pub fn indented_line_beginning(
137 map: &DisplaySnapshot,
138 display_point: DisplayPoint,
139 stop_at_soft_boundaries: bool,
140) -> DisplayPoint {
141 let point = display_point.to_point(map);
142 let soft_line_start = map.clip_point(DisplayPoint::new(display_point.row(), 0), Bias::Right);
143 let indent_start = Point::new(
144 point.row,
145 map.buffer_snapshot.indent_size_for_line(point.row).len,
146 )
147 .to_display_point(map);
148 let line_start = map.prev_line_boundary(point).1;
149
150 if stop_at_soft_boundaries && soft_line_start > indent_start && display_point != soft_line_start
151 {
152 soft_line_start
153 } else if stop_at_soft_boundaries && display_point != indent_start {
154 indent_start
155 } else {
156 line_start
157 }
158}
159
160pub fn line_end(
161 map: &DisplaySnapshot,
162 display_point: DisplayPoint,
163 stop_at_soft_boundaries: bool,
164) -> DisplayPoint {
165 let soft_line_end = map.clip_point(
166 DisplayPoint::new(display_point.row(), map.line_len(display_point.row())),
167 Bias::Left,
168 );
169 if stop_at_soft_boundaries && display_point != soft_line_end {
170 soft_line_end
171 } else {
172 map.next_line_boundary(display_point.to_point(map)).1
173 }
174}
175
176pub fn previous_word_start(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
177 find_preceding_boundary(map, point, |left, right| {
178 (char_kind(left) != char_kind(right) && !right.is_whitespace()) || left == '\n'
179 })
180}
181
182pub fn previous_subword_start(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
183 find_preceding_boundary(map, point, |left, right| {
184 let is_word_start = char_kind(left) != char_kind(right) && !right.is_whitespace();
185 let is_subword_start =
186 left == '_' && right != '_' || left.is_lowercase() && right.is_uppercase();
187 is_word_start || is_subword_start || left == '\n'
188 })
189}
190
191pub fn next_word_end(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
192 find_boundary(map, point, |left, right| {
193 (char_kind(left) != char_kind(right) && !left.is_whitespace()) || right == '\n'
194 })
195}
196
197pub fn next_subword_end(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
198 find_boundary(map, point, |left, right| {
199 let is_word_end = (char_kind(left) != char_kind(right)) && !left.is_whitespace();
200 let is_subword_end =
201 left != '_' && right == '_' || left.is_lowercase() && right.is_uppercase();
202 is_word_end || is_subword_end || right == '\n'
203 })
204}
205
206/// Scans for a boundary preceding the given start point `from` until a boundary is found, indicated by the
207/// given predicate returning true. The predicate is called with the character to the left and right
208/// of the candidate boundary location, and will be called with `\n` characters indicating the start
209/// or end of a line.
210pub fn find_preceding_boundary(
211 map: &DisplaySnapshot,
212 from: DisplayPoint,
213 mut is_boundary: impl FnMut(char, char) -> bool,
214) -> DisplayPoint {
215 let mut start_column = 0;
216 let mut soft_wrap_row = from.row() + 1;
217
218 let mut prev = None;
219 for (ch, point) in map.reverse_chars_at(from) {
220 // Recompute soft_wrap_indent if the row has changed
221 if point.row() != soft_wrap_row {
222 soft_wrap_row = point.row();
223
224 if point.row() == 0 {
225 start_column = 0;
226 } else if let Some(indent) = map.soft_wrap_indent(point.row() - 1) {
227 start_column = indent;
228 }
229 }
230
231 // If the current point is in the soft_wrap, skip comparing it
232 if point.column() < start_column {
233 continue;
234 }
235
236 if let Some((prev_ch, prev_point)) = prev {
237 if is_boundary(ch, prev_ch) {
238 return prev_point;
239 }
240 }
241
242 prev = Some((ch, point));
243 }
244 DisplayPoint::zero()
245}
246
247/// Scans for a boundary preceding the given start point `from` until a boundary is found, indicated by the
248/// given predicate returning true. The predicate is called with the character to the left and right
249/// of the candidate boundary location, and will be called with `\n` characters indicating the start
250/// or end of a line. If no boundary is found, the start of the line is returned.
251pub fn find_preceding_boundary_in_line(
252 map: &DisplaySnapshot,
253 from: DisplayPoint,
254 mut is_boundary: impl FnMut(char, char) -> bool,
255) -> DisplayPoint {
256 let mut start_column = 0;
257 if from.row() > 0 {
258 if let Some(indent) = map.soft_wrap_indent(from.row() - 1) {
259 start_column = indent;
260 }
261 }
262
263 let mut prev = None;
264 for (ch, point) in map.reverse_chars_at(from) {
265 if let Some((prev_ch, prev_point)) = prev {
266 if is_boundary(ch, prev_ch) {
267 return prev_point;
268 }
269 }
270
271 if ch == '\n' || point.column() < start_column {
272 break;
273 }
274
275 prev = Some((ch, point));
276 }
277
278 prev.map(|(_, point)| point).unwrap_or(from)
279}
280
281/// Scans for a boundary following the given start point until a boundary is found, indicated by the
282/// given predicate returning true. The predicate is called with the character to the left and right
283/// of the candidate boundary location, and will be called with `\n` characters indicating the start
284/// or end of a line.
285pub fn find_boundary(
286 map: &DisplaySnapshot,
287 from: DisplayPoint,
288 mut is_boundary: impl FnMut(char, char) -> bool,
289) -> DisplayPoint {
290 let mut prev_ch = None;
291 for (ch, point) in map.chars_at(from) {
292 if let Some(prev_ch) = prev_ch {
293 if is_boundary(prev_ch, ch) {
294 return map.clip_point(point, Bias::Right);
295 }
296 }
297
298 prev_ch = Some(ch);
299 }
300 map.clip_point(map.max_point(), Bias::Right)
301}
302
303/// Scans for a boundary following the given start point until a boundary is found, indicated by the
304/// given predicate returning true. The predicate is called with the character to the left and right
305/// of the candidate boundary location, and will be called with `\n` characters indicating the start
306/// or end of a line. If no boundary is found, the end of the line is returned
307pub fn find_boundary_in_line(
308 map: &DisplaySnapshot,
309 from: DisplayPoint,
310 mut is_boundary: impl FnMut(char, char) -> bool,
311) -> DisplayPoint {
312 let mut prev = None;
313 for (ch, point) in map.chars_at(from) {
314 if let Some((prev_ch, _)) = prev {
315 if is_boundary(prev_ch, ch) {
316 return map.clip_point(point, Bias::Right);
317 }
318 }
319
320 prev = Some((ch, point));
321
322 if ch == '\n' {
323 break;
324 }
325 }
326
327 // Return the last position checked so that we give a point right before the newline or eof.
328 map.clip_point(prev.map(|(_, point)| point).unwrap_or(from), Bias::Right)
329}
330
331pub fn is_inside_word(map: &DisplaySnapshot, point: DisplayPoint) -> bool {
332 let ix = map.clip_point(point, Bias::Left).to_offset(map, Bias::Left);
333 let text = &map.buffer_snapshot;
334 let next_char_kind = text.chars_at(ix).next().map(char_kind);
335 let prev_char_kind = text.reversed_chars_at(ix).next().map(char_kind);
336 prev_char_kind.zip(next_char_kind) == Some((CharKind::Word, CharKind::Word))
337}
338
339pub fn surrounding_word(map: &DisplaySnapshot, position: DisplayPoint) -> Range<DisplayPoint> {
340 let position = map
341 .clip_point(position, Bias::Left)
342 .to_offset(map, Bias::Left);
343 let (range, _) = map.buffer_snapshot.surrounding_word(position);
344 let start = range
345 .start
346 .to_point(&map.buffer_snapshot)
347 .to_display_point(map);
348 let end = range
349 .end
350 .to_point(&map.buffer_snapshot)
351 .to_display_point(map);
352 start..end
353}
354
355#[cfg(test)]
356mod tests {
357 use super::*;
358 use crate::{test::marked_display_snapshot, Buffer, DisplayMap, ExcerptRange, MultiBuffer};
359 use settings::Settings;
360
361 #[gpui::test]
362 fn test_previous_word_start(cx: &mut gpui::MutableAppContext) {
363 cx.set_global(Settings::test(cx));
364 fn assert(marked_text: &str, cx: &mut gpui::MutableAppContext) {
365 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
366 assert_eq!(
367 previous_word_start(&snapshot, display_points[1]),
368 display_points[0]
369 );
370 }
371
372 assert("\nˇ ˇlorem", cx);
373 assert("ˇ\nˇ lorem", cx);
374 assert(" ˇloremˇ", cx);
375 assert("ˇ ˇlorem", cx);
376 assert(" ˇlorˇem", cx);
377 assert("\nlorem\nˇ ˇipsum", cx);
378 assert("\n\nˇ\nˇ", cx);
379 assert(" ˇlorem ˇipsum", cx);
380 assert("loremˇ-ˇipsum", cx);
381 assert("loremˇ-#$@ˇipsum", cx);
382 assert("ˇlorem_ˇipsum", cx);
383 assert(" ˇdefγˇ", cx);
384 assert(" ˇbcΔˇ", cx);
385 assert(" abˇ——ˇcd", cx);
386 }
387
388 #[gpui::test]
389 fn test_previous_subword_start(cx: &mut gpui::MutableAppContext) {
390 cx.set_global(Settings::test(cx));
391 fn assert(marked_text: &str, cx: &mut gpui::MutableAppContext) {
392 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
393 assert_eq!(
394 previous_subword_start(&snapshot, display_points[1]),
395 display_points[0]
396 );
397 }
398
399 // Subword boundaries are respected
400 assert("lorem_ˇipˇsum", cx);
401 assert("lorem_ˇipsumˇ", cx);
402 assert("ˇlorem_ˇipsum", cx);
403 assert("lorem_ˇipsum_ˇdolor", cx);
404 assert("loremˇIpˇsum", cx);
405 assert("loremˇIpsumˇ", cx);
406
407 // Word boundaries are still respected
408 assert("\nˇ ˇlorem", cx);
409 assert(" ˇloremˇ", cx);
410 assert(" ˇlorˇem", cx);
411 assert("\nlorem\nˇ ˇipsum", cx);
412 assert("\n\nˇ\nˇ", cx);
413 assert(" ˇlorem ˇipsum", cx);
414 assert("loremˇ-ˇipsum", cx);
415 assert("loremˇ-#$@ˇipsum", cx);
416 assert(" ˇdefγˇ", cx);
417 assert(" bcˇΔˇ", cx);
418 assert(" ˇbcδˇ", cx);
419 assert(" abˇ——ˇcd", cx);
420 }
421
422 #[gpui::test]
423 fn test_find_preceding_boundary(cx: &mut gpui::MutableAppContext) {
424 cx.set_global(Settings::test(cx));
425 fn assert(
426 marked_text: &str,
427 cx: &mut gpui::MutableAppContext,
428 is_boundary: impl FnMut(char, char) -> bool,
429 ) {
430 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
431 assert_eq!(
432 find_preceding_boundary(&snapshot, display_points[1], is_boundary),
433 display_points[0]
434 );
435 }
436
437 assert("abcˇdef\ngh\nijˇk", cx, |left, right| {
438 left == 'c' && right == 'd'
439 });
440 assert("abcdef\nˇgh\nijˇk", cx, |left, right| {
441 left == '\n' && right == 'g'
442 });
443 let mut line_count = 0;
444 assert("abcdef\nˇgh\nijˇk", cx, |left, _| {
445 if left == '\n' {
446 line_count += 1;
447 line_count == 2
448 } else {
449 false
450 }
451 });
452 }
453
454 #[gpui::test]
455 fn test_next_word_end(cx: &mut gpui::MutableAppContext) {
456 cx.set_global(Settings::test(cx));
457 fn assert(marked_text: &str, cx: &mut gpui::MutableAppContext) {
458 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
459 assert_eq!(
460 next_word_end(&snapshot, display_points[0]),
461 display_points[1]
462 );
463 }
464
465 assert("\nˇ loremˇ", cx);
466 assert(" ˇloremˇ", cx);
467 assert(" lorˇemˇ", cx);
468 assert(" loremˇ ˇ\nipsum\n", cx);
469 assert("\nˇ\nˇ\n\n", cx);
470 assert("loremˇ ipsumˇ ", cx);
471 assert("loremˇ-ˇipsum", cx);
472 assert("loremˇ#$@-ˇipsum", cx);
473 assert("loremˇ_ipsumˇ", cx);
474 assert(" ˇbcΔˇ", cx);
475 assert(" abˇ——ˇcd", cx);
476 }
477
478 #[gpui::test]
479 fn test_next_subword_end(cx: &mut gpui::MutableAppContext) {
480 cx.set_global(Settings::test(cx));
481 fn assert(marked_text: &str, cx: &mut gpui::MutableAppContext) {
482 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
483 assert_eq!(
484 next_subword_end(&snapshot, display_points[0]),
485 display_points[1]
486 );
487 }
488
489 // Subword boundaries are respected
490 assert("loˇremˇ_ipsum", cx);
491 assert("ˇloremˇ_ipsum", cx);
492 assert("loremˇ_ipsumˇ", cx);
493 assert("loremˇ_ipsumˇ_dolor", cx);
494 assert("loˇremˇIpsum", cx);
495 assert("loremˇIpsumˇDolor", cx);
496
497 // Word boundaries are still respected
498 assert("\nˇ loremˇ", cx);
499 assert(" ˇloremˇ", cx);
500 assert(" lorˇemˇ", cx);
501 assert(" loremˇ ˇ\nipsum\n", cx);
502 assert("\nˇ\nˇ\n\n", cx);
503 assert("loremˇ ipsumˇ ", cx);
504 assert("loremˇ-ˇipsum", cx);
505 assert("loremˇ#$@-ˇipsum", cx);
506 assert("loremˇ_ipsumˇ", cx);
507 assert(" ˇbcˇΔ", cx);
508 assert(" abˇ——ˇcd", cx);
509 }
510
511 #[gpui::test]
512 fn test_find_boundary(cx: &mut gpui::MutableAppContext) {
513 cx.set_global(Settings::test(cx));
514 fn assert(
515 marked_text: &str,
516 cx: &mut gpui::MutableAppContext,
517 is_boundary: impl FnMut(char, char) -> bool,
518 ) {
519 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
520 assert_eq!(
521 find_boundary(&snapshot, display_points[0], is_boundary),
522 display_points[1]
523 );
524 }
525
526 assert("abcˇdef\ngh\nijˇk", cx, |left, right| {
527 left == 'j' && right == 'k'
528 });
529 assert("abˇcdef\ngh\nˇijk", cx, |left, right| {
530 left == '\n' && right == 'i'
531 });
532 let mut line_count = 0;
533 assert("abcˇdef\ngh\nˇijk", cx, |left, _| {
534 if left == '\n' {
535 line_count += 1;
536 line_count == 2
537 } else {
538 false
539 }
540 });
541 }
542
543 #[gpui::test]
544 fn test_surrounding_word(cx: &mut gpui::MutableAppContext) {
545 cx.set_global(Settings::test(cx));
546 fn assert(marked_text: &str, cx: &mut gpui::MutableAppContext) {
547 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
548 assert_eq!(
549 surrounding_word(&snapshot, display_points[1]),
550 display_points[0]..display_points[2]
551 );
552 }
553
554 assert("ˇˇloremˇ ipsum", cx);
555 assert("ˇloˇremˇ ipsum", cx);
556 assert("ˇloremˇˇ ipsum", cx);
557 assert("loremˇ ˇ ˇipsum", cx);
558 assert("lorem\nˇˇˇ\nipsum", cx);
559 assert("lorem\nˇˇipsumˇ", cx);
560 assert("lorem,ˇˇ ˇipsum", cx);
561 assert("ˇloremˇˇ, ipsum", cx);
562 }
563
564 #[gpui::test]
565 fn test_move_up_and_down_with_excerpts(cx: &mut gpui::MutableAppContext) {
566 cx.set_global(Settings::test(cx));
567 let family_id = cx.font_cache().load_family(&["Helvetica"]).unwrap();
568 let font_id = cx
569 .font_cache()
570 .select_font(family_id, &Default::default())
571 .unwrap();
572
573 let buffer = cx.add_model(|cx| Buffer::new(0, "abc\ndefg\nhijkl\nmn", cx));
574 let multibuffer = cx.add_model(|cx| {
575 let mut multibuffer = MultiBuffer::new(0);
576 multibuffer.push_excerpts(
577 buffer.clone(),
578 [
579 ExcerptRange {
580 context: Point::new(0, 0)..Point::new(1, 4),
581 primary: None,
582 },
583 ExcerptRange {
584 context: Point::new(2, 0)..Point::new(3, 2),
585 primary: None,
586 },
587 ],
588 cx,
589 );
590 multibuffer
591 });
592 let display_map =
593 cx.add_model(|cx| DisplayMap::new(multibuffer, font_id, 14.0, None, 2, 2, cx));
594 let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
595
596 assert_eq!(snapshot.text(), "\n\nabc\ndefg\n\n\nhijkl\nmn");
597
598 // Can't move up into the first excerpt's header
599 assert_eq!(
600 up(
601 &snapshot,
602 DisplayPoint::new(2, 2),
603 SelectionGoal::Column(2),
604 false
605 ),
606 (DisplayPoint::new(2, 0), SelectionGoal::Column(0)),
607 );
608 assert_eq!(
609 up(
610 &snapshot,
611 DisplayPoint::new(2, 0),
612 SelectionGoal::None,
613 false
614 ),
615 (DisplayPoint::new(2, 0), SelectionGoal::Column(0)),
616 );
617
618 // Move up and down within first excerpt
619 assert_eq!(
620 up(
621 &snapshot,
622 DisplayPoint::new(3, 4),
623 SelectionGoal::Column(4),
624 false
625 ),
626 (DisplayPoint::new(2, 3), SelectionGoal::Column(4)),
627 );
628 assert_eq!(
629 down(
630 &snapshot,
631 DisplayPoint::new(2, 3),
632 SelectionGoal::Column(4),
633 false
634 ),
635 (DisplayPoint::new(3, 4), SelectionGoal::Column(4)),
636 );
637
638 // Move up and down across second excerpt's header
639 assert_eq!(
640 up(
641 &snapshot,
642 DisplayPoint::new(6, 5),
643 SelectionGoal::Column(5),
644 false
645 ),
646 (DisplayPoint::new(3, 4), SelectionGoal::Column(5)),
647 );
648 assert_eq!(
649 down(
650 &snapshot,
651 DisplayPoint::new(3, 4),
652 SelectionGoal::Column(5),
653 false
654 ),
655 (DisplayPoint::new(6, 5), SelectionGoal::Column(5)),
656 );
657
658 // Can't move down off the end
659 assert_eq!(
660 down(
661 &snapshot,
662 DisplayPoint::new(7, 0),
663 SelectionGoal::Column(0),
664 false
665 ),
666 (DisplayPoint::new(7, 2), SelectionGoal::Column(2)),
667 );
668 assert_eq!(
669 down(
670 &snapshot,
671 DisplayPoint::new(7, 2),
672 SelectionGoal::Column(2),
673 false
674 ),
675 (DisplayPoint::new(7, 2), SelectionGoal::Column(2)),
676 );
677 }
678}