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 settings::Settings;
340
341 #[gpui::test]
342 fn test_previous_word_start(cx: &mut gpui::MutableAppContext) {
343 cx.set_global(Settings::test(cx));
344 fn assert(marked_text: &str, cx: &mut gpui::MutableAppContext) {
345 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
346 assert_eq!(
347 previous_word_start(&snapshot, display_points[1]),
348 display_points[0]
349 );
350 }
351
352 assert("\nˇ ˇlorem", cx);
353 assert("ˇ\nˇ lorem", cx);
354 assert(" ˇloremˇ", cx);
355 assert("ˇ ˇlorem", cx);
356 assert(" ˇlorˇem", cx);
357 assert("\nlorem\nˇ ˇipsum", cx);
358 assert("\n\nˇ\nˇ", cx);
359 assert(" ˇlorem ˇipsum", cx);
360 assert("loremˇ-ˇipsum", cx);
361 assert("loremˇ-#$@ˇipsum", cx);
362 assert("ˇlorem_ˇipsum", cx);
363 assert(" ˇdefγˇ", cx);
364 assert(" ˇbcΔˇ", cx);
365 assert(" abˇ——ˇcd", cx);
366 }
367
368 #[gpui::test]
369 fn test_previous_subword_start(cx: &mut gpui::MutableAppContext) {
370 cx.set_global(Settings::test(cx));
371 fn assert(marked_text: &str, cx: &mut gpui::MutableAppContext) {
372 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
373 assert_eq!(
374 previous_subword_start(&snapshot, display_points[1]),
375 display_points[0]
376 );
377 }
378
379 // Subword boundaries are respected
380 assert("lorem_ˇipˇsum", cx);
381 assert("lorem_ˇipsumˇ", cx);
382 assert("ˇlorem_ˇipsum", cx);
383 assert("lorem_ˇipsum_ˇdolor", cx);
384 assert("loremˇIpˇsum", cx);
385 assert("loremˇIpsumˇ", cx);
386
387 // Word boundaries are still respected
388 assert("\nˇ ˇlorem", cx);
389 assert(" ˇloremˇ", cx);
390 assert(" ˇlorˇem", cx);
391 assert("\nlorem\nˇ ˇipsum", cx);
392 assert("\n\nˇ\nˇ", cx);
393 assert(" ˇlorem ˇipsum", cx);
394 assert("loremˇ-ˇipsum", cx);
395 assert("loremˇ-#$@ˇipsum", cx);
396 assert(" ˇdefγˇ", cx);
397 assert(" bcˇΔˇ", cx);
398 assert(" ˇbcδˇ", cx);
399 assert(" abˇ——ˇcd", cx);
400 }
401
402 #[gpui::test]
403 fn test_find_preceding_boundary(cx: &mut gpui::MutableAppContext) {
404 cx.set_global(Settings::test(cx));
405 fn assert(
406 marked_text: &str,
407 cx: &mut gpui::MutableAppContext,
408 is_boundary: impl FnMut(char, char) -> bool,
409 ) {
410 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
411 assert_eq!(
412 find_preceding_boundary(&snapshot, display_points[1], is_boundary),
413 display_points[0]
414 );
415 }
416
417 assert("abcˇdef\ngh\nijˇk", cx, |left, right| {
418 left == 'c' && right == 'd'
419 });
420 assert("abcdef\nˇgh\nijˇk", cx, |left, right| {
421 left == '\n' && right == 'g'
422 });
423 let mut line_count = 0;
424 assert("abcdef\nˇgh\nijˇk", cx, |left, _| {
425 if left == '\n' {
426 line_count += 1;
427 line_count == 2
428 } else {
429 false
430 }
431 });
432 }
433
434 #[gpui::test]
435 fn test_next_word_end(cx: &mut gpui::MutableAppContext) {
436 cx.set_global(Settings::test(cx));
437 fn assert(marked_text: &str, cx: &mut gpui::MutableAppContext) {
438 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
439 assert_eq!(
440 next_word_end(&snapshot, display_points[0]),
441 display_points[1]
442 );
443 }
444
445 assert("\nˇ loremˇ", cx);
446 assert(" ˇloremˇ", cx);
447 assert(" lorˇemˇ", cx);
448 assert(" loremˇ ˇ\nipsum\n", cx);
449 assert("\nˇ\nˇ\n\n", cx);
450 assert("loremˇ ipsumˇ ", cx);
451 assert("loremˇ-ˇipsum", cx);
452 assert("loremˇ#$@-ˇipsum", cx);
453 assert("loremˇ_ipsumˇ", cx);
454 assert(" ˇbcΔˇ", cx);
455 assert(" abˇ——ˇcd", cx);
456 }
457
458 #[gpui::test]
459 fn test_next_subword_end(cx: &mut gpui::MutableAppContext) {
460 cx.set_global(Settings::test(cx));
461 fn assert(marked_text: &str, cx: &mut gpui::MutableAppContext) {
462 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
463 assert_eq!(
464 next_subword_end(&snapshot, display_points[0]),
465 display_points[1]
466 );
467 }
468
469 // Subword boundaries are respected
470 assert("loˇremˇ_ipsum", cx);
471 assert("ˇloremˇ_ipsum", cx);
472 assert("loremˇ_ipsumˇ", cx);
473 assert("loremˇ_ipsumˇ_dolor", cx);
474 assert("loˇremˇIpsum", cx);
475 assert("loremˇIpsumˇDolor", cx);
476
477 // Word boundaries are still respected
478 assert("\nˇ loremˇ", cx);
479 assert(" ˇloremˇ", cx);
480 assert(" lorˇemˇ", cx);
481 assert(" loremˇ ˇ\nipsum\n", cx);
482 assert("\nˇ\nˇ\n\n", cx);
483 assert("loremˇ ipsumˇ ", cx);
484 assert("loremˇ-ˇipsum", cx);
485 assert("loremˇ#$@-ˇipsum", cx);
486 assert("loremˇ_ipsumˇ", cx);
487 assert(" ˇbcˇΔ", cx);
488 assert(" abˇ——ˇcd", cx);
489 }
490
491 #[gpui::test]
492 fn test_find_boundary(cx: &mut gpui::MutableAppContext) {
493 cx.set_global(Settings::test(cx));
494 fn assert(
495 marked_text: &str,
496 cx: &mut gpui::MutableAppContext,
497 is_boundary: impl FnMut(char, char) -> bool,
498 ) {
499 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
500 assert_eq!(
501 find_boundary(&snapshot, display_points[0], is_boundary),
502 display_points[1]
503 );
504 }
505
506 assert("abcˇdef\ngh\nijˇk", cx, |left, right| {
507 left == 'j' && right == 'k'
508 });
509 assert("abˇcdef\ngh\nˇijk", cx, |left, right| {
510 left == '\n' && right == 'i'
511 });
512 let mut line_count = 0;
513 assert("abcˇdef\ngh\nˇijk", cx, |left, _| {
514 if left == '\n' {
515 line_count += 1;
516 line_count == 2
517 } else {
518 false
519 }
520 });
521 }
522
523 #[gpui::test]
524 fn test_surrounding_word(cx: &mut gpui::MutableAppContext) {
525 cx.set_global(Settings::test(cx));
526 fn assert(marked_text: &str, cx: &mut gpui::MutableAppContext) {
527 let (snapshot, display_points) = marked_display_snapshot(marked_text, cx);
528 assert_eq!(
529 surrounding_word(&snapshot, display_points[1]),
530 display_points[0]..display_points[2]
531 );
532 }
533
534 assert("ˇˇloremˇ ipsum", cx);
535 assert("ˇloˇremˇ ipsum", cx);
536 assert("ˇloremˇˇ ipsum", cx);
537 assert("loremˇ ˇ ˇipsum", cx);
538 assert("lorem\nˇˇˇ\nipsum", cx);
539 assert("lorem\nˇˇipsumˇ", cx);
540 assert("lorem,ˇˇ ˇipsum", cx);
541 assert("ˇloremˇˇ, ipsum", cx);
542 }
543
544 #[gpui::test]
545 fn test_move_up_and_down_with_excerpts(cx: &mut gpui::MutableAppContext) {
546 cx.set_global(Settings::test(cx));
547 let family_id = cx.font_cache().load_family(&["Helvetica"]).unwrap();
548 let font_id = cx
549 .font_cache()
550 .select_font(family_id, &Default::default())
551 .unwrap();
552
553 let buffer = cx.add_model(|cx| Buffer::new(0, "abc\ndefg\nhijkl\nmn", cx));
554 let multibuffer = cx.add_model(|cx| {
555 let mut multibuffer = MultiBuffer::new(0);
556 multibuffer.push_excerpts(
557 buffer.clone(),
558 [
559 ExcerptRange {
560 context: Point::new(0, 0)..Point::new(1, 4),
561 primary: None,
562 },
563 ExcerptRange {
564 context: Point::new(2, 0)..Point::new(3, 2),
565 primary: None,
566 },
567 ],
568 cx,
569 );
570 multibuffer
571 });
572 let display_map =
573 cx.add_model(|cx| DisplayMap::new(multibuffer, font_id, 14.0, None, 2, 2, cx));
574 let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
575
576 assert_eq!(snapshot.text(), "\n\nabc\ndefg\n\n\nhijkl\nmn");
577
578 // Can't move up into the first excerpt's header
579 assert_eq!(
580 up(
581 &snapshot,
582 DisplayPoint::new(2, 2),
583 SelectionGoal::Column(2),
584 false
585 ),
586 (DisplayPoint::new(2, 0), SelectionGoal::Column(0)),
587 );
588 assert_eq!(
589 up(
590 &snapshot,
591 DisplayPoint::new(2, 0),
592 SelectionGoal::None,
593 false
594 ),
595 (DisplayPoint::new(2, 0), SelectionGoal::Column(0)),
596 );
597
598 // Move up and down within first excerpt
599 assert_eq!(
600 up(
601 &snapshot,
602 DisplayPoint::new(3, 4),
603 SelectionGoal::Column(4),
604 false
605 ),
606 (DisplayPoint::new(2, 3), SelectionGoal::Column(4)),
607 );
608 assert_eq!(
609 down(
610 &snapshot,
611 DisplayPoint::new(2, 3),
612 SelectionGoal::Column(4),
613 false
614 ),
615 (DisplayPoint::new(3, 4), SelectionGoal::Column(4)),
616 );
617
618 // Move up and down across second excerpt's header
619 assert_eq!(
620 up(
621 &snapshot,
622 DisplayPoint::new(6, 5),
623 SelectionGoal::Column(5),
624 false
625 ),
626 (DisplayPoint::new(3, 4), SelectionGoal::Column(5)),
627 );
628 assert_eq!(
629 down(
630 &snapshot,
631 DisplayPoint::new(3, 4),
632 SelectionGoal::Column(5),
633 false
634 ),
635 (DisplayPoint::new(6, 5), SelectionGoal::Column(5)),
636 );
637
638 // Can't move down off the end
639 assert_eq!(
640 down(
641 &snapshot,
642 DisplayPoint::new(7, 0),
643 SelectionGoal::Column(0),
644 false
645 ),
646 (DisplayPoint::new(7, 2), SelectionGoal::Column(2)),
647 );
648 assert_eq!(
649 down(
650 &snapshot,
651 DisplayPoint::new(7, 2),
652 SelectionGoal::Column(2),
653 false
654 ),
655 (DisplayPoint::new(7, 2), SelectionGoal::Column(2)),
656 );
657 }
658}