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