1use super::{Bias, DisplayPoint, DisplaySnapshot, SelectionGoal, ToDisplayPoint};
2use crate::{char_kind, CharKind, ToPoint};
3use anyhow::Result;
4use std::{cmp, ops::Range};
5
6pub fn left(map: &DisplaySnapshot, mut point: DisplayPoint) -> Result<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 Ok(map.clip_point(point, Bias::Left))
14}
15
16pub fn right(map: &DisplaySnapshot, mut point: DisplayPoint) -> Result<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 Ok(map.clip_point(point, Bias::Right))
25}
26
27pub fn up(
28 map: &DisplaySnapshot,
29 start: DisplayPoint,
30 goal: SelectionGoal,
31) -> Result<(DisplayPoint, SelectionGoal)> {
32 let mut goal_column = if let SelectionGoal::Column(column) = goal {
33 column
34 } else {
35 map.column_to_chars(start.row(), start.column())
36 };
37
38 let prev_row = start.row().saturating_sub(1);
39 let mut point = map.clip_point(
40 DisplayPoint::new(prev_row, map.line_len(prev_row)),
41 Bias::Left,
42 );
43 if point.row() < start.row() {
44 *point.column_mut() = map.column_from_chars(point.row(), goal_column);
45 } else {
46 point = DisplayPoint::new(0, 0);
47 goal_column = 0;
48 }
49
50 let clip_bias = if point.column() == map.line_len(point.row()) {
51 Bias::Left
52 } else {
53 Bias::Right
54 };
55
56 Ok((
57 map.clip_point(point, clip_bias),
58 SelectionGoal::Column(goal_column),
59 ))
60}
61
62pub fn down(
63 map: &DisplaySnapshot,
64 start: DisplayPoint,
65 goal: SelectionGoal,
66) -> Result<(DisplayPoint, SelectionGoal)> {
67 let mut goal_column = if let SelectionGoal::Column(column) = goal {
68 column
69 } else {
70 map.column_to_chars(start.row(), start.column())
71 };
72
73 let next_row = start.row() + 1;
74 let mut point = map.clip_point(DisplayPoint::new(next_row, 0), Bias::Right);
75 if point.row() > start.row() {
76 *point.column_mut() = map.column_from_chars(point.row(), goal_column);
77 } else {
78 point = map.max_point();
79 goal_column = map.column_to_chars(point.row(), point.column())
80 }
81
82 let clip_bias = if point.column() == map.line_len(point.row()) {
83 Bias::Left
84 } else {
85 Bias::Right
86 };
87
88 Ok((
89 map.clip_point(point, clip_bias),
90 SelectionGoal::Column(goal_column),
91 ))
92}
93
94pub fn line_beginning(
95 map: &DisplaySnapshot,
96 point: DisplayPoint,
97 toggle_indent: bool,
98) -> DisplayPoint {
99 let (indent, is_blank) = map.line_indent(point.row());
100 if toggle_indent && !is_blank && point.column() != indent {
101 DisplayPoint::new(point.row(), indent)
102 } else {
103 DisplayPoint::new(point.row(), 0)
104 }
105}
106
107pub fn line_end(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
108 let line_end = DisplayPoint::new(point.row(), map.line_len(point.row()));
109 map.clip_point(line_end, Bias::Left)
110}
111
112pub fn prev_word_boundary(map: &DisplaySnapshot, mut point: DisplayPoint) -> DisplayPoint {
113 let mut line_start = 0;
114 if point.row() > 0 {
115 if let Some(indent) = map.soft_wrap_indent(point.row() - 1) {
116 line_start = indent;
117 }
118 }
119
120 if point.column() == line_start {
121 if point.row() == 0 {
122 return DisplayPoint::new(0, 0);
123 } else {
124 let row = point.row() - 1;
125 point = map.clip_point(DisplayPoint::new(row, map.line_len(row)), Bias::Left);
126 }
127 }
128
129 let mut boundary = DisplayPoint::new(point.row(), 0);
130 let mut column = 0;
131 let mut prev_char_kind = CharKind::Newline;
132 for c in map.chars_at(DisplayPoint::new(point.row(), 0)) {
133 if column >= point.column() {
134 break;
135 }
136
137 let char_kind = char_kind(c);
138 if char_kind != prev_char_kind
139 && char_kind != CharKind::Whitespace
140 && char_kind != CharKind::Newline
141 {
142 *boundary.column_mut() = column;
143 }
144
145 prev_char_kind = char_kind;
146 column += c.len_utf8() as u32;
147 }
148 boundary
149}
150
151pub fn next_word_boundary(map: &DisplaySnapshot, mut point: DisplayPoint) -> DisplayPoint {
152 let mut prev_char_kind = None;
153 for c in map.chars_at(point) {
154 let char_kind = char_kind(c);
155 if let Some(prev_char_kind) = prev_char_kind {
156 if c == '\n' {
157 break;
158 }
159 if prev_char_kind != char_kind
160 && prev_char_kind != CharKind::Whitespace
161 && prev_char_kind != CharKind::Newline
162 {
163 break;
164 }
165 }
166
167 if c == '\n' {
168 *point.row_mut() += 1;
169 *point.column_mut() = 0;
170 } else {
171 *point.column_mut() += c.len_utf8() as u32;
172 }
173 prev_char_kind = Some(char_kind);
174 }
175 map.clip_point(point, Bias::Right)
176}
177
178pub fn is_inside_word(map: &DisplaySnapshot, point: DisplayPoint) -> bool {
179 let ix = map.clip_point(point, Bias::Left).to_offset(map, Bias::Left);
180 let text = &map.buffer_snapshot;
181 let next_char_kind = text.chars_at(ix).next().map(char_kind);
182 let prev_char_kind = text.reversed_chars_at(ix).next().map(char_kind);
183 prev_char_kind.zip(next_char_kind) == Some((CharKind::Word, CharKind::Word))
184}
185
186pub fn surrounding_word(
187 map: &DisplaySnapshot,
188 point: DisplayPoint,
189) -> (Range<DisplayPoint>, Option<CharKind>) {
190 let mut start = map.clip_point(point, Bias::Left).to_offset(map, Bias::Left);
191 let mut end = start;
192
193 let text = &map.buffer_snapshot;
194 let mut next_chars = text.chars_at(start).peekable();
195 let mut prev_chars = text.reversed_chars_at(start).peekable();
196 let word_kind = cmp::max(
197 prev_chars.peek().copied().map(char_kind),
198 next_chars.peek().copied().map(char_kind),
199 );
200
201 for ch in prev_chars {
202 if Some(char_kind(ch)) == word_kind {
203 start -= ch.len_utf8();
204 } else {
205 break;
206 }
207 }
208
209 for ch in next_chars {
210 if Some(char_kind(ch)) == word_kind {
211 end += ch.len_utf8();
212 } else {
213 break;
214 }
215 }
216
217 (
218 start.to_point(&map.buffer_snapshot).to_display_point(map)
219 ..end.to_point(&map.buffer_snapshot).to_display_point(map),
220 word_kind,
221 )
222}
223
224#[cfg(test)]
225mod tests {
226 use super::*;
227 use crate::{
228 display_map::{BlockDisposition, BlockProperties},
229 Buffer, DisplayMap, ExcerptProperties, MultiBuffer,
230 };
231 use gpui::{elements::Empty, Element};
232 use language::Point;
233 use std::sync::Arc;
234
235 #[gpui::test]
236 fn test_move_up_and_down_with_excerpts(cx: &mut gpui::MutableAppContext) {
237 let family_id = cx.font_cache().load_family(&["Helvetica"]).unwrap();
238 let font_id = cx
239 .font_cache()
240 .select_font(family_id, &Default::default())
241 .unwrap();
242
243 let buffer = cx.add_model(|cx| Buffer::new(0, "abc\ndefg\nhijkl\nmn", cx));
244 let mut excerpt1_header_position = None;
245 let mut excerpt2_header_position = None;
246 let multibuffer = cx.add_model(|cx| {
247 let mut multibuffer = MultiBuffer::new(0);
248 let excerpt1_id = multibuffer.push_excerpt(
249 ExcerptProperties {
250 buffer: &buffer,
251 range: Point::new(0, 0)..Point::new(1, 4),
252 },
253 cx,
254 );
255 let excerpt2_id = multibuffer.push_excerpt(
256 ExcerptProperties {
257 buffer: &buffer,
258 range: Point::new(2, 0)..Point::new(3, 2),
259 },
260 cx,
261 );
262
263 excerpt1_header_position = Some(
264 multibuffer
265 .read(cx)
266 .anchor_in_excerpt(excerpt1_id, language::Anchor::min()),
267 );
268 excerpt2_header_position = Some(
269 multibuffer
270 .read(cx)
271 .anchor_in_excerpt(excerpt2_id, language::Anchor::min()),
272 );
273 multibuffer
274 });
275
276 let display_map =
277 cx.add_model(|cx| DisplayMap::new(multibuffer, 2, font_id, 14.0, None, cx));
278 display_map.update(cx, |display_map, cx| {
279 display_map.insert_blocks(
280 [
281 BlockProperties {
282 position: excerpt1_header_position.unwrap(),
283 height: 2,
284 render: Arc::new(|_| Empty::new().boxed()),
285 disposition: BlockDisposition::Above,
286 },
287 BlockProperties {
288 position: excerpt2_header_position.unwrap(),
289 height: 3,
290 render: Arc::new(|_| Empty::new().boxed()),
291 disposition: BlockDisposition::Above,
292 },
293 ],
294 cx,
295 )
296 });
297
298 let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
299 assert_eq!(snapshot.text(), "\n\nabc\ndefg\n\n\n\nhijkl\nmn");
300
301 // Can't move up into the first excerpt's header
302 assert_eq!(
303 up(&snapshot, DisplayPoint::new(2, 2), SelectionGoal::Column(2)).unwrap(),
304 (DisplayPoint::new(2, 0), SelectionGoal::Column(0)),
305 );
306 assert_eq!(
307 up(&snapshot, DisplayPoint::new(2, 0), SelectionGoal::None).unwrap(),
308 (DisplayPoint::new(2, 0), SelectionGoal::Column(0)),
309 );
310
311 // Move up and down within first excerpt
312 assert_eq!(
313 up(&snapshot, DisplayPoint::new(3, 4), SelectionGoal::Column(4)).unwrap(),
314 (DisplayPoint::new(2, 3), SelectionGoal::Column(4)),
315 );
316 assert_eq!(
317 down(&snapshot, DisplayPoint::new(2, 3), SelectionGoal::Column(4)).unwrap(),
318 (DisplayPoint::new(3, 4), SelectionGoal::Column(4)),
319 );
320
321 // Move up and down across second excerpt's header
322 assert_eq!(
323 up(&snapshot, DisplayPoint::new(7, 5), SelectionGoal::Column(5)).unwrap(),
324 (DisplayPoint::new(3, 4), SelectionGoal::Column(5)),
325 );
326 assert_eq!(
327 down(&snapshot, DisplayPoint::new(3, 4), SelectionGoal::Column(5)).unwrap(),
328 (DisplayPoint::new(7, 5), SelectionGoal::Column(5)),
329 );
330
331 // Can't move down off the end
332 assert_eq!(
333 down(&snapshot, DisplayPoint::new(8, 0), SelectionGoal::Column(0)).unwrap(),
334 (DisplayPoint::new(8, 2), SelectionGoal::Column(2)),
335 );
336 assert_eq!(
337 down(&snapshot, DisplayPoint::new(8, 2), SelectionGoal::Column(2)).unwrap(),
338 (DisplayPoint::new(8, 2), SelectionGoal::Column(2)),
339 );
340 }
341
342 #[gpui::test]
343 fn test_prev_next_word_boundary_multibyte(cx: &mut gpui::MutableAppContext) {
344 let tab_size = 4;
345 let family_id = cx.font_cache().load_family(&["Helvetica"]).unwrap();
346 let font_id = cx
347 .font_cache()
348 .select_font(family_id, &Default::default())
349 .unwrap();
350 let font_size = 14.0;
351
352 let buffer = MultiBuffer::build_simple("a bcΔ defγ hi—jk", cx);
353 let display_map =
354 cx.add_model(|cx| DisplayMap::new(buffer, tab_size, font_id, font_size, None, cx));
355 let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
356 assert_eq!(
357 prev_word_boundary(&snapshot, DisplayPoint::new(0, 12)),
358 DisplayPoint::new(0, 7)
359 );
360 assert_eq!(
361 prev_word_boundary(&snapshot, DisplayPoint::new(0, 7)),
362 DisplayPoint::new(0, 2)
363 );
364 assert_eq!(
365 prev_word_boundary(&snapshot, DisplayPoint::new(0, 6)),
366 DisplayPoint::new(0, 2)
367 );
368 assert_eq!(
369 prev_word_boundary(&snapshot, DisplayPoint::new(0, 2)),
370 DisplayPoint::new(0, 0)
371 );
372 assert_eq!(
373 prev_word_boundary(&snapshot, DisplayPoint::new(0, 1)),
374 DisplayPoint::new(0, 0)
375 );
376
377 assert_eq!(
378 next_word_boundary(&snapshot, DisplayPoint::new(0, 0)),
379 DisplayPoint::new(0, 1)
380 );
381 assert_eq!(
382 next_word_boundary(&snapshot, DisplayPoint::new(0, 1)),
383 DisplayPoint::new(0, 6)
384 );
385 assert_eq!(
386 next_word_boundary(&snapshot, DisplayPoint::new(0, 2)),
387 DisplayPoint::new(0, 6)
388 );
389 assert_eq!(
390 next_word_boundary(&snapshot, DisplayPoint::new(0, 6)),
391 DisplayPoint::new(0, 12)
392 );
393 assert_eq!(
394 next_word_boundary(&snapshot, DisplayPoint::new(0, 7)),
395 DisplayPoint::new(0, 12)
396 );
397 }
398
399 #[gpui::test]
400 fn test_surrounding_word(cx: &mut gpui::MutableAppContext) {
401 let tab_size = 4;
402 let family_id = cx.font_cache().load_family(&["Helvetica"]).unwrap();
403 let font_id = cx
404 .font_cache()
405 .select_font(family_id, &Default::default())
406 .unwrap();
407 let font_size = 14.0;
408 let buffer = MultiBuffer::build_simple("lorem ipsum dolor\n sit", cx);
409 let display_map =
410 cx.add_model(|cx| DisplayMap::new(buffer, tab_size, font_id, font_size, None, cx));
411 let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
412
413 assert_eq!(
414 surrounding_word(&snapshot, DisplayPoint::new(0, 0)),
415 (
416 DisplayPoint::new(0, 0)..DisplayPoint::new(0, 5),
417 Some(CharKind::Word)
418 )
419 );
420 assert_eq!(
421 surrounding_word(&snapshot, DisplayPoint::new(0, 2)),
422 (
423 DisplayPoint::new(0, 0)..DisplayPoint::new(0, 5),
424 Some(CharKind::Word)
425 )
426 );
427 assert_eq!(
428 surrounding_word(&snapshot, DisplayPoint::new(0, 5)),
429 (
430 DisplayPoint::new(0, 0)..DisplayPoint::new(0, 5),
431 Some(CharKind::Word)
432 )
433 );
434 assert_eq!(
435 surrounding_word(&snapshot, DisplayPoint::new(0, 6)),
436 (
437 DisplayPoint::new(0, 6)..DisplayPoint::new(0, 11),
438 Some(CharKind::Word)
439 )
440 );
441 assert_eq!(
442 surrounding_word(&snapshot, DisplayPoint::new(0, 7)),
443 (
444 DisplayPoint::new(0, 6)..DisplayPoint::new(0, 11),
445 Some(CharKind::Word)
446 )
447 );
448 assert_eq!(
449 surrounding_word(&snapshot, DisplayPoint::new(0, 11)),
450 (
451 DisplayPoint::new(0, 6)..DisplayPoint::new(0, 11),
452 Some(CharKind::Word)
453 )
454 );
455 assert_eq!(
456 surrounding_word(&snapshot, DisplayPoint::new(0, 13)),
457 (
458 DisplayPoint::new(0, 11)..DisplayPoint::new(0, 14),
459 Some(CharKind::Whitespace)
460 )
461 );
462 assert_eq!(
463 surrounding_word(&snapshot, DisplayPoint::new(0, 14)),
464 (
465 DisplayPoint::new(0, 14)..DisplayPoint::new(0, 19),
466 Some(CharKind::Word)
467 )
468 );
469 assert_eq!(
470 surrounding_word(&snapshot, DisplayPoint::new(0, 17)),
471 (
472 DisplayPoint::new(0, 14)..DisplayPoint::new(0, 19),
473 Some(CharKind::Word)
474 )
475 );
476 assert_eq!(
477 surrounding_word(&snapshot, DisplayPoint::new(0, 19)),
478 (
479 DisplayPoint::new(0, 14)..DisplayPoint::new(0, 19),
480 Some(CharKind::Word)
481 )
482 );
483 assert_eq!(
484 surrounding_word(&snapshot, DisplayPoint::new(1, 0)),
485 (
486 DisplayPoint::new(1, 0)..DisplayPoint::new(1, 4),
487 Some(CharKind::Whitespace)
488 )
489 );
490 assert_eq!(
491 surrounding_word(&snapshot, DisplayPoint::new(1, 1)),
492 (
493 DisplayPoint::new(1, 0)..DisplayPoint::new(1, 4),
494 Some(CharKind::Whitespace)
495 )
496 );
497 assert_eq!(
498 surrounding_word(&snapshot, DisplayPoint::new(1, 6)),
499 (
500 DisplayPoint::new(1, 4)..DisplayPoint::new(1, 7),
501 Some(CharKind::Word)
502 )
503 );
504 assert_eq!(
505 surrounding_word(&snapshot, DisplayPoint::new(1, 7)),
506 (
507 DisplayPoint::new(1, 4)..DisplayPoint::new(1, 7),
508 Some(CharKind::Word)
509 )
510 );
511 }
512}