1use super::{Bias, DisplayPoint, DisplaySnapshot, SelectionGoal, ToDisplayPoint};
2use crate::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(map: &DisplaySnapshot, point: DisplayPoint) -> Range<DisplayPoint> {
187 let mut start = map.clip_point(point, Bias::Left).to_offset(map, Bias::Left);
188 let mut end = start;
189
190 let text = &map.buffer_snapshot;
191 let mut next_chars = text.chars_at(start).peekable();
192 let mut prev_chars = text.reversed_chars_at(start).peekable();
193 let word_kind = cmp::max(
194 prev_chars.peek().copied().map(char_kind),
195 next_chars.peek().copied().map(char_kind),
196 );
197
198 for ch in prev_chars {
199 if Some(char_kind(ch)) == word_kind {
200 start -= ch.len_utf8();
201 } else {
202 break;
203 }
204 }
205
206 for ch in next_chars {
207 if Some(char_kind(ch)) == word_kind {
208 end += ch.len_utf8();
209 } else {
210 break;
211 }
212 }
213
214 start.to_point(&map.buffer_snapshot).to_display_point(map)
215 ..end.to_point(&map.buffer_snapshot).to_display_point(map)
216}
217
218#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
219enum CharKind {
220 Newline,
221 Punctuation,
222 Whitespace,
223 Word,
224}
225
226fn char_kind(c: char) -> CharKind {
227 if c == '\n' {
228 CharKind::Newline
229 } else if c.is_whitespace() {
230 CharKind::Whitespace
231 } else if c.is_alphanumeric() || c == '_' {
232 CharKind::Word
233 } else {
234 CharKind::Punctuation
235 }
236}
237
238#[cfg(test)]
239mod tests {
240 use super::*;
241 use crate::{
242 display_map::{BlockDisposition, BlockProperties},
243 Buffer, DisplayMap, ExcerptProperties, MultiBuffer,
244 };
245 use gpui::{elements::Empty, Element};
246 use language::Point;
247 use std::sync::Arc;
248
249 #[gpui::test]
250 fn test_move_up_and_down_with_excerpts(cx: &mut gpui::MutableAppContext) {
251 let family_id = cx.font_cache().load_family(&["Helvetica"]).unwrap();
252 let font_id = cx
253 .font_cache()
254 .select_font(family_id, &Default::default())
255 .unwrap();
256
257 let buffer = cx.add_model(|cx| Buffer::new(0, "abc\ndefg\nhijkl\nmn", cx));
258 let mut excerpt1_header_position = None;
259 let mut excerpt2_header_position = None;
260 let multibuffer = cx.add_model(|cx| {
261 let mut multibuffer = MultiBuffer::new(0);
262 let excerpt1_id = multibuffer.push_excerpt(
263 ExcerptProperties {
264 buffer: &buffer,
265 range: Point::new(0, 0)..Point::new(1, 4),
266 },
267 cx,
268 );
269 let excerpt2_id = multibuffer.push_excerpt(
270 ExcerptProperties {
271 buffer: &buffer,
272 range: Point::new(2, 0)..Point::new(3, 2),
273 },
274 cx,
275 );
276
277 excerpt1_header_position = Some(
278 multibuffer
279 .read(cx)
280 .anchor_in_excerpt(excerpt1_id, language::Anchor::min()),
281 );
282 excerpt2_header_position = Some(
283 multibuffer
284 .read(cx)
285 .anchor_in_excerpt(excerpt2_id, language::Anchor::min()),
286 );
287 multibuffer
288 });
289
290 let display_map =
291 cx.add_model(|cx| DisplayMap::new(multibuffer, 2, font_id, 14.0, None, cx));
292 display_map.update(cx, |display_map, cx| {
293 display_map.insert_blocks(
294 [
295 BlockProperties {
296 position: excerpt1_header_position.unwrap(),
297 height: 2,
298 render: Arc::new(|_| Empty::new().boxed()),
299 disposition: BlockDisposition::Above,
300 },
301 BlockProperties {
302 position: excerpt2_header_position.unwrap(),
303 height: 3,
304 render: Arc::new(|_| Empty::new().boxed()),
305 disposition: BlockDisposition::Above,
306 },
307 ],
308 cx,
309 )
310 });
311
312 let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
313 assert_eq!(snapshot.text(), "\n\nabc\ndefg\n\n\n\nhijkl\nmn");
314
315 // Can't move up into the first excerpt's header
316 assert_eq!(
317 up(&snapshot, DisplayPoint::new(2, 2), SelectionGoal::Column(2)).unwrap(),
318 (DisplayPoint::new(2, 0), SelectionGoal::Column(0)),
319 );
320 assert_eq!(
321 up(&snapshot, DisplayPoint::new(2, 0), SelectionGoal::None).unwrap(),
322 (DisplayPoint::new(2, 0), SelectionGoal::Column(0)),
323 );
324
325 // Move up and down within first excerpt
326 assert_eq!(
327 up(&snapshot, DisplayPoint::new(3, 4), SelectionGoal::Column(4)).unwrap(),
328 (DisplayPoint::new(2, 3), SelectionGoal::Column(4)),
329 );
330 assert_eq!(
331 down(&snapshot, DisplayPoint::new(2, 3), SelectionGoal::Column(4)).unwrap(),
332 (DisplayPoint::new(3, 4), SelectionGoal::Column(4)),
333 );
334
335 // Move up and down across second excerpt's header
336 assert_eq!(
337 up(&snapshot, DisplayPoint::new(7, 5), SelectionGoal::Column(5)).unwrap(),
338 (DisplayPoint::new(3, 4), SelectionGoal::Column(5)),
339 );
340 assert_eq!(
341 down(&snapshot, DisplayPoint::new(3, 4), SelectionGoal::Column(5)).unwrap(),
342 (DisplayPoint::new(7, 5), SelectionGoal::Column(5)),
343 );
344
345 // Can't move down off the end
346 assert_eq!(
347 down(&snapshot, DisplayPoint::new(8, 0), SelectionGoal::Column(0)).unwrap(),
348 (DisplayPoint::new(8, 2), SelectionGoal::Column(2)),
349 );
350 assert_eq!(
351 down(&snapshot, DisplayPoint::new(8, 2), SelectionGoal::Column(2)).unwrap(),
352 (DisplayPoint::new(8, 2), SelectionGoal::Column(2)),
353 );
354 }
355
356 #[gpui::test]
357 fn test_prev_next_word_boundary_multibyte(cx: &mut gpui::MutableAppContext) {
358 let tab_size = 4;
359 let family_id = cx.font_cache().load_family(&["Helvetica"]).unwrap();
360 let font_id = cx
361 .font_cache()
362 .select_font(family_id, &Default::default())
363 .unwrap();
364 let font_size = 14.0;
365
366 let buffer = MultiBuffer::build_simple("a bcΔ defγ hi—jk", cx);
367 let display_map =
368 cx.add_model(|cx| DisplayMap::new(buffer, tab_size, font_id, font_size, None, cx));
369 let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
370 assert_eq!(
371 prev_word_boundary(&snapshot, DisplayPoint::new(0, 12)),
372 DisplayPoint::new(0, 7)
373 );
374 assert_eq!(
375 prev_word_boundary(&snapshot, DisplayPoint::new(0, 7)),
376 DisplayPoint::new(0, 2)
377 );
378 assert_eq!(
379 prev_word_boundary(&snapshot, DisplayPoint::new(0, 6)),
380 DisplayPoint::new(0, 2)
381 );
382 assert_eq!(
383 prev_word_boundary(&snapshot, DisplayPoint::new(0, 2)),
384 DisplayPoint::new(0, 0)
385 );
386 assert_eq!(
387 prev_word_boundary(&snapshot, DisplayPoint::new(0, 1)),
388 DisplayPoint::new(0, 0)
389 );
390
391 assert_eq!(
392 next_word_boundary(&snapshot, DisplayPoint::new(0, 0)),
393 DisplayPoint::new(0, 1)
394 );
395 assert_eq!(
396 next_word_boundary(&snapshot, DisplayPoint::new(0, 1)),
397 DisplayPoint::new(0, 6)
398 );
399 assert_eq!(
400 next_word_boundary(&snapshot, DisplayPoint::new(0, 2)),
401 DisplayPoint::new(0, 6)
402 );
403 assert_eq!(
404 next_word_boundary(&snapshot, DisplayPoint::new(0, 6)),
405 DisplayPoint::new(0, 12)
406 );
407 assert_eq!(
408 next_word_boundary(&snapshot, DisplayPoint::new(0, 7)),
409 DisplayPoint::new(0, 12)
410 );
411 }
412
413 #[gpui::test]
414 fn test_surrounding_word(cx: &mut gpui::MutableAppContext) {
415 let tab_size = 4;
416 let family_id = cx.font_cache().load_family(&["Helvetica"]).unwrap();
417 let font_id = cx
418 .font_cache()
419 .select_font(family_id, &Default::default())
420 .unwrap();
421 let font_size = 14.0;
422 let buffer = MultiBuffer::build_simple("lorem ipsum dolor\n sit", cx);
423 let display_map =
424 cx.add_model(|cx| DisplayMap::new(buffer, tab_size, font_id, font_size, None, cx));
425 let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
426
427 assert_eq!(
428 surrounding_word(&snapshot, DisplayPoint::new(0, 0)),
429 DisplayPoint::new(0, 0)..DisplayPoint::new(0, 5)
430 );
431 assert_eq!(
432 surrounding_word(&snapshot, DisplayPoint::new(0, 2)),
433 DisplayPoint::new(0, 0)..DisplayPoint::new(0, 5)
434 );
435 assert_eq!(
436 surrounding_word(&snapshot, DisplayPoint::new(0, 5)),
437 DisplayPoint::new(0, 0)..DisplayPoint::new(0, 5)
438 );
439 assert_eq!(
440 surrounding_word(&snapshot, DisplayPoint::new(0, 6)),
441 DisplayPoint::new(0, 6)..DisplayPoint::new(0, 11)
442 );
443 assert_eq!(
444 surrounding_word(&snapshot, DisplayPoint::new(0, 7)),
445 DisplayPoint::new(0, 6)..DisplayPoint::new(0, 11)
446 );
447 assert_eq!(
448 surrounding_word(&snapshot, DisplayPoint::new(0, 11)),
449 DisplayPoint::new(0, 6)..DisplayPoint::new(0, 11)
450 );
451 assert_eq!(
452 surrounding_word(&snapshot, DisplayPoint::new(0, 13)),
453 DisplayPoint::new(0, 11)..DisplayPoint::new(0, 14)
454 );
455 assert_eq!(
456 surrounding_word(&snapshot, DisplayPoint::new(0, 14)),
457 DisplayPoint::new(0, 14)..DisplayPoint::new(0, 19)
458 );
459 assert_eq!(
460 surrounding_word(&snapshot, DisplayPoint::new(0, 17)),
461 DisplayPoint::new(0, 14)..DisplayPoint::new(0, 19)
462 );
463 assert_eq!(
464 surrounding_word(&snapshot, DisplayPoint::new(0, 19)),
465 DisplayPoint::new(0, 14)..DisplayPoint::new(0, 19)
466 );
467 assert_eq!(
468 surrounding_word(&snapshot, DisplayPoint::new(1, 0)),
469 DisplayPoint::new(1, 0)..DisplayPoint::new(1, 4)
470 );
471 assert_eq!(
472 surrounding_word(&snapshot, DisplayPoint::new(1, 1)),
473 DisplayPoint::new(1, 0)..DisplayPoint::new(1, 4)
474 );
475 assert_eq!(
476 surrounding_word(&snapshot, DisplayPoint::new(1, 6)),
477 DisplayPoint::new(1, 4)..DisplayPoint::new(1, 7)
478 );
479 assert_eq!(
480 surrounding_word(&snapshot, DisplayPoint::new(1, 7)),
481 DisplayPoint::new(1, 4)..DisplayPoint::new(1, 7)
482 );
483 }
484}