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