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