1use unicode_width::UnicodeWidthChar;
9
10use super::buffer::{Position, ScanDirection, TextBuffer, WordMotion, WordStyle};
11use super::line::{
12 byte_at_display, clamp_boundary, count_as_isize, display_column, display_with_gutter,
13 find_character, first_non_blank, first_non_blank_from, last_column, last_non_blank,
14 next_column, next_tabstop, previous_column, screen_end_column, screen_last_non_blank,
15 visible_source_columns,
16};
17use super::{Cursor, FULL_PERCENT, Motion, MotionKind, Viewport};
18
19const DEFAULT_MOTION_PERCENT: usize = FULL_PERCENT / 2;
20const PERCENT_ROUNDING_OFFSET: usize = FULL_PERCENT - 1;
21const PAGE_OVERLAP_LINES: usize = 2;
22
23pub fn apply(lines: &[&str], position: Cursor, viewport: &mut Viewport, motion: Motion) -> Cursor {
36 let buffer = TextBuffer::new(lines);
37 let mut cursor = buffer.clamp(position);
38 let count = motion.count().max(1);
39 let desired_column = viewport
42 .desired_column
43 .unwrap_or_else(|| display_column(buffer.line(cursor.line), cursor.column));
44 match motion.kind() {
45 MotionKind::Left => {
46 cursor = repeated_boundary(cursor, count, |mut at| {
47 at.column = previous_column(buffer.line(at.line), at.column);
48 at
49 });
50 }
51 MotionKind::LeftWrap => {
52 cursor = repeated_boundary(cursor, count, |mut at| {
53 if at.column == 0 && at.line > 0 {
54 at.line = at.line.saturating_sub(1);
55 at.column = last_column(buffer.line(at.line));
56 } else {
57 at.column = previous_column(buffer.line(at.line), at.column);
58 }
59 at
60 });
61 }
62 MotionKind::Right => {
63 cursor = repeated_boundary(cursor, count, |mut at| {
64 at.column = next_column(buffer.line(at.line), at.column);
65 at
66 });
67 }
68 MotionKind::RightWrap => {
69 cursor = repeated_boundary(cursor, count, |mut at| {
70 let line = buffer.line(at.line);
71 if at.column >= last_column(line) && at.line < buffer.last_line() {
72 at.line = at.line.saturating_add(1);
73 at.column = 0;
74 } else {
75 at.column = next_column(line, at.column);
76 }
77 at
78 });
79 }
80 MotionKind::Up => {
81 cursor = buffer.move_lines_to_display(cursor, -(count_as_isize(count)), desired_column)
82 }
83 MotionKind::Down => {
84 cursor = buffer.move_lines_to_display(cursor, count_as_isize(count), desired_column)
85 }
86 MotionKind::LineStart => cursor.column = 0,
87 MotionKind::FirstNonBlank => cursor.column = first_non_blank(buffer.line(cursor.line)),
88 MotionKind::LineEnd => {
89 cursor = buffer.move_lines(cursor, count_as_isize(count.saturating_sub(1)));
90 cursor.column = last_column(buffer.line(cursor.line));
91 }
92 MotionKind::LastNonBlank => {
93 cursor = buffer.move_lines(cursor, count_as_isize(count.saturating_sub(1)));
94 cursor.column = last_non_blank(buffer.line(cursor.line));
95 }
96 MotionKind::ScreenLineStart => {
97 let (source_left, _) = visible_source_columns(*viewport);
98 cursor.column = byte_at_display(buffer.line(cursor.line), source_left);
99 }
100 MotionKind::ScreenFirstNonBlank => {
101 let (source_left, _) = visible_source_columns(*viewport);
102 let screen = byte_at_display(buffer.line(cursor.line), source_left);
103 cursor.column = first_non_blank_from(buffer.line(cursor.line), screen);
104 }
105 MotionKind::ScreenLineEnd => {
106 cursor = buffer.move_lines(cursor, count_as_isize(count.saturating_sub(1)));
107 cursor.column = screen_end_column(buffer.line(cursor.line), *viewport);
108 }
109 MotionKind::ScreenLastNonBlank => {
110 cursor = buffer.move_lines(cursor, count_as_isize(count.saturating_sub(1)));
111 cursor.column = screen_last_non_blank(buffer.line(cursor.line), *viewport);
112 }
113 MotionKind::ScreenMiddle => {
114 let (source_left, source_end) = visible_source_columns(*viewport);
115 cursor.column = byte_at_display(
116 buffer.line(cursor.line),
117 source_left.saturating_add(source_end.saturating_sub(source_left) / 2),
118 );
119 }
120 MotionKind::LineMiddle => {
121 let percent = if motion.has_explicit_count() {
122 count.min(FULL_PERCENT)
123 } else {
124 DEFAULT_MOTION_PERCENT
125 };
126 let line = buffer.line(cursor.line);
127 cursor.column = byte_at_display(
128 line,
129 display_column(line, line.len()).saturating_mul(percent) / FULL_PERCENT,
130 );
131 }
132 MotionKind::Column => {
133 cursor.column = byte_at_display(buffer.line(cursor.line), count.saturating_sub(1));
134 }
135 MotionKind::ByteOffset => cursor = buffer.byte_offset(count),
136 MotionKind::WordForward => {
137 cursor = buffer.word_motion(cursor, count, WordMotion::StartForward, WordStyle::Word)
138 }
139 MotionKind::BigWordForward => {
140 cursor = buffer.word_motion(cursor, count, WordMotion::StartForward, WordStyle::BigWord)
141 }
142 MotionKind::WordEndForward => {
143 cursor = buffer.word_motion(cursor, count, WordMotion::EndForward, WordStyle::Word)
144 }
145 MotionKind::BigWordEndForward => {
146 cursor = buffer.word_motion(cursor, count, WordMotion::EndForward, WordStyle::BigWord)
147 }
148 MotionKind::WordBackward => {
149 cursor = buffer.word_motion(cursor, count, WordMotion::StartBackward, WordStyle::Word)
150 }
151 MotionKind::BigWordBackward => {
152 cursor =
153 buffer.word_motion(cursor, count, WordMotion::StartBackward, WordStyle::BigWord)
154 }
155 MotionKind::WordEndBackward => {
156 cursor = buffer.word_motion(cursor, count, WordMotion::EndBackward, WordStyle::Word)
157 }
158 MotionKind::BigWordEndBackward => {
159 cursor = buffer.word_motion(cursor, count, WordMotion::EndBackward, WordStyle::BigWord)
160 }
161 MotionKind::FindForward
162 | MotionKind::FindBackward
163 | MotionKind::TillForward
164 | MotionKind::TillBackward => {
165 if let Some(target) = motion.target() {
166 cursor.column = find_character(
167 buffer.line(cursor.line),
168 cursor.column,
169 target,
170 motion.kind(),
171 count,
172 motion.repetition(),
173 );
174 }
175 }
176 MotionKind::PreviousLineFirstNonBlank => {
177 cursor = buffer.move_lines(cursor, -(count_as_isize(count)));
178 cursor.column = first_non_blank(buffer.line(cursor.line));
179 }
180 MotionKind::NextLineFirstNonBlank => {
181 cursor = buffer.move_lines(cursor, count_as_isize(count));
182 cursor.column = first_non_blank(buffer.line(cursor.line));
183 }
184 MotionKind::CountedLineFirstNonBlank => {
185 cursor = buffer.move_lines(cursor, count_as_isize(count.saturating_sub(1)));
186 cursor.column = first_non_blank(buffer.line(cursor.line));
187 }
188 MotionKind::BufferTop => {
189 cursor.line = if motion.has_explicit_count() {
190 count.saturating_sub(1).min(buffer.last_line())
191 } else {
192 0
193 };
194 cursor.column = first_non_blank(buffer.line(cursor.line));
195 }
196 MotionKind::BufferBottom => {
197 cursor.line = buffer.last_line();
198 cursor.column = first_non_blank(buffer.line(cursor.line));
199 }
200 MotionKind::BufferBottomEnd => {
201 cursor.line = if motion.has_explicit_count() {
202 count.saturating_sub(1).min(buffer.last_line())
203 } else {
204 buffer.last_line()
205 };
206 cursor.column = last_column(buffer.line(cursor.line));
207 }
208 MotionKind::BufferPercentage => {
209 let one_based = count
210 .min(FULL_PERCENT)
211 .saturating_mul(buffer.len())
212 .saturating_add(PERCENT_ROUNDING_OFFSET)
213 / FULL_PERCENT;
214 cursor.line = one_based.saturating_sub(1).min(buffer.last_line());
215 cursor.column = first_non_blank(buffer.line(cursor.line));
216 }
217 MotionKind::WindowTop => {
218 cursor.line = viewport
219 .top
220 .saturating_add(count.min(viewport.height).saturating_sub(1))
221 .min(buffer.last_line());
222 cursor.column = first_non_blank(buffer.line(cursor.line));
223 }
224 MotionKind::WindowMiddle => {
225 cursor.line = viewport
226 .top
227 .saturating_add(
228 buffer
229 .len()
230 .saturating_sub(viewport.top)
231 .min(viewport.height)
232 .saturating_sub(1)
233 / 2,
234 )
235 .min(buffer.last_line());
236 cursor.column = first_non_blank(buffer.line(cursor.line));
237 }
238 MotionKind::WindowBottom => {
239 let bottom = viewport
240 .top
241 .saturating_add(viewport.height.saturating_sub(1))
242 .min(buffer.last_line());
243 cursor.line = bottom
244 .saturating_sub(count.saturating_sub(1))
245 .max(viewport.top.min(buffer.last_line()));
246 cursor.column = first_non_blank(buffer.line(cursor.line));
247 }
248 MotionKind::SentenceBackward => {
249 cursor = repeated_boundary(cursor, count, |at| buffer.sentence_backward(at));
250 }
251 MotionKind::SentenceForward => {
252 cursor = repeated_boundary(cursor, count, |at| buffer.sentence_forward(at));
253 }
254 MotionKind::ParagraphBackward => {
255 cursor = repeated_boundary(cursor, count, |at| buffer.paragraph_backward(at));
256 }
257 MotionKind::ParagraphForward => {
258 cursor = repeated_boundary(cursor, count, |at| buffer.paragraph_forward(at));
259 }
260 MotionKind::SectionStartBackward => {
261 cursor = repeated_boundary(cursor, count, |at| {
262 buffer.section(at, ScanDirection::Backward, '{')
263 });
264 }
265 MotionKind::SectionStartForward => {
266 cursor = repeated_boundary(cursor, count, |at| {
267 buffer.section(at, ScanDirection::Forward, '{')
268 });
269 }
270 MotionKind::SectionEndBackward => {
271 cursor = repeated_boundary(cursor, count, |at| {
272 buffer.section(at, ScanDirection::Backward, '}')
273 });
274 }
275 MotionKind::SectionEndForward => {
276 cursor = repeated_boundary(cursor, count, |at| {
277 buffer.section(at, ScanDirection::Forward, '}')
278 });
279 }
280 MotionKind::MatchingPair => {
281 if let Some(found) = buffer.matching_pair(cursor, ScanDirection::Forward) {
282 cursor = found;
283 }
284 }
285 MotionKind::MatchingPairBackward => {
286 if let Some(found) = buffer.matching_pair(cursor, ScanDirection::Backward) {
287 cursor = found;
288 }
289 }
290 MotionKind::UnmatchedOpenBackward => {
291 if let Some(target) = motion.target() {
292 cursor = repeated_boundary(cursor, count, |at| buffer.unmatched_open(at, target));
293 }
294 }
295 MotionKind::UnmatchedCloseForward => {
296 if let Some(target) = motion.target() {
297 cursor = repeated_boundary(cursor, count, |at| buffer.unmatched_close(at, target));
298 }
299 }
300 MotionKind::MethodBackward => {
301 let target = if motion.target() == Some('M') {
302 '}'
303 } else {
304 '{'
305 };
306 cursor = repeated_boundary(cursor, count, |at| {
307 buffer.brace(at, ScanDirection::Backward, target)
308 });
309 }
310 MotionKind::MethodForward => {
311 let target = if motion.target() == Some('M') {
312 '}'
313 } else {
314 '{'
315 };
316 cursor = repeated_boundary(cursor, count, |at| {
317 buffer.brace(at, ScanDirection::Forward, target)
318 });
319 }
320 MotionKind::PreprocessorBackward => {
321 cursor = repeated_boundary(cursor, count, |at| {
322 buffer.preprocessor(at, ScanDirection::Backward)
323 });
324 }
325 MotionKind::PreprocessorForward => {
326 cursor = repeated_boundary(cursor, count, |at| {
327 buffer.preprocessor(at, ScanDirection::Forward)
328 });
329 }
330 MotionKind::CommentBackward => {
331 cursor = repeated_boundary(cursor, count, |at| {
332 buffer.comment(at, ScanDirection::Backward)
333 });
334 }
335 MotionKind::CommentForward => {
336 cursor = repeated_boundary(cursor, count, |at| {
337 buffer.comment(at, ScanDirection::Forward)
338 });
339 }
340 MotionKind::DiffChangeBackward => {
341 cursor = repeated_boundary(cursor, count, |at| {
342 buffer.diff_change(at, ScanDirection::Backward)
343 });
344 }
345 MotionKind::DiffChangeForward => {
346 cursor = repeated_boundary(cursor, count, |at| {
347 buffer.diff_change(at, ScanDirection::Forward)
348 });
349 }
350 MotionKind::HalfPageDown => {
351 let distance = if motion.has_explicit_count() {
352 count
353 } else {
354 viewport.height.saturating_div(2).max(1)
355 };
356 cursor = buffer.move_lines_to_display(cursor, count_as_isize(distance), desired_column);
357 cursor.column = first_non_blank(buffer.line(cursor.line));
358 viewport.top = viewport
359 .top
360 .saturating_add(distance)
361 .min(buffer.last_line());
362 }
363 MotionKind::HalfPageUp => {
364 let distance = if motion.has_explicit_count() {
365 count
366 } else {
367 viewport.height.saturating_div(2).max(1)
368 };
369 cursor =
370 buffer.move_lines_to_display(cursor, -(count_as_isize(distance)), desired_column);
371 cursor.column = first_non_blank(buffer.line(cursor.line));
372 viewport.top = viewport.top.saturating_sub(distance);
373 }
374 MotionKind::PageDown => {
375 let page = viewport.height.saturating_sub(PAGE_OVERLAP_LINES).max(1);
376 viewport.top = viewport
377 .top
378 .saturating_add(page.saturating_mul(count))
379 .min(buffer.last_line());
380 cursor.line = viewport.top;
381 cursor.column = first_non_blank(buffer.line(cursor.line));
382 }
383 MotionKind::PageUp => {
384 let page = viewport.height.saturating_sub(PAGE_OVERLAP_LINES).max(1);
385 viewport.top = viewport.top.saturating_sub(page.saturating_mul(count));
386 cursor.line = viewport
387 .top
388 .saturating_add(viewport.height.saturating_sub(1))
389 .min(buffer.last_line());
390 cursor.column = first_non_blank(buffer.line(cursor.line));
391 }
392 MotionKind::ScrollLineDown => {
393 viewport.top = viewport.top.saturating_add(count).min(buffer.last_line());
394 if cursor.line < viewport.top {
395 cursor = buffer.move_to_line_display(cursor, viewport.top, desired_column);
396 }
397 }
398 MotionKind::ScrollLineUp => {
399 viewport.top = viewport.top.saturating_sub(count);
400 let bottom = viewport
401 .top
402 .saturating_add(viewport.height.saturating_sub(1))
403 .min(buffer.last_line());
404 if cursor.line > bottom {
405 cursor = buffer.move_to_line_display(cursor, bottom, desired_column);
406 }
407 }
408 MotionKind::CursorToWindowTop => {
409 if motion.has_explicit_count() {
410 cursor =
411 buffer.move_to_line_display(cursor, count.saturating_sub(1), desired_column);
412 }
413 viewport.top = cursor.line;
414 }
415 MotionKind::CursorToWindowTopFirstNonBlank => {
416 if motion.has_explicit_count() {
417 cursor.line = count.saturating_sub(1).min(buffer.last_line());
418 }
419 cursor.column = first_non_blank(buffer.line(cursor.line));
420 viewport.top = cursor.line;
421 }
422 MotionKind::CursorToWindowMiddle => {
423 if motion.has_explicit_count() {
424 cursor =
425 buffer.move_to_line_display(cursor, count.saturating_sub(1), desired_column);
426 }
427 viewport.top = cursor
428 .line
429 .saturating_sub(viewport.height.saturating_sub(1) / 2);
430 }
431 MotionKind::CursorToWindowMiddleFirstNonBlank => {
432 if motion.has_explicit_count() {
433 cursor.line = count.saturating_sub(1).min(buffer.last_line());
434 }
435 cursor.column = first_non_blank(buffer.line(cursor.line));
436 viewport.top = cursor
437 .line
438 .saturating_sub(viewport.height.saturating_sub(1) / 2);
439 }
440 MotionKind::CursorToWindowBottom => {
441 if motion.has_explicit_count() {
442 cursor =
443 buffer.move_to_line_display(cursor, count.saturating_sub(1), desired_column);
444 }
445 viewport.top = cursor
446 .line
447 .saturating_sub(viewport.height.saturating_sub(1));
448 }
449 MotionKind::CursorToWindowBottomFirstNonBlank => {
450 if motion.has_explicit_count() {
451 cursor.line = count.saturating_sub(1).min(buffer.last_line());
452 }
453 cursor.column = first_non_blank(buffer.line(cursor.line));
454 viewport.top = cursor
455 .line
456 .saturating_sub(viewport.height.saturating_sub(1));
457 }
458 MotionKind::NextWindowTop => {
459 let target = if motion.has_explicit_count() {
460 count.saturating_sub(1).min(buffer.last_line())
461 } else {
462 viewport
463 .top
464 .saturating_add(viewport.height)
465 .min(buffer.last_line())
466 };
467 cursor = buffer.move_to_line(cursor, target);
468 cursor.column = first_non_blank(buffer.line(cursor.line));
469 viewport.top = target;
470 }
471 MotionKind::PreviousWindowBottom => {
472 let target = if motion.has_explicit_count() {
473 count
474 .saturating_sub(viewport.height)
475 .min(buffer.last_line())
476 } else {
477 viewport.top.saturating_sub(1)
478 };
479 cursor = buffer.move_to_line(cursor, target);
480 cursor.column = first_non_blank(buffer.line(cursor.line));
481 viewport.top = target.saturating_sub(viewport.height.saturating_sub(1));
482 }
483 MotionKind::ScrollColumnLeft => {
484 viewport.left = viewport.left.saturating_sub(count);
485 follow_horizontal_scroll(&buffer, &mut cursor, viewport);
486 }
487 MotionKind::ScrollColumnRight => {
488 viewport.left = viewport.left.saturating_add(count);
489 follow_horizontal_scroll(&buffer, &mut cursor, viewport);
490 }
491 MotionKind::ScrollHalfScreenLeft => {
492 viewport.left = viewport
493 .left
494 .saturating_sub(viewport.width.saturating_div(2).saturating_mul(count));
495 follow_horizontal_scroll(&buffer, &mut cursor, viewport);
496 }
497 MotionKind::ScrollHalfScreenRight => {
498 viewport.left = viewport
499 .left
500 .saturating_add(viewport.width.saturating_div(2).saturating_mul(count));
501 follow_horizontal_scroll(&buffer, &mut cursor, viewport);
502 }
503 MotionKind::CursorToWindowLeft => {
504 viewport.left =
505 display_with_gutter(buffer.line(cursor.line), cursor.column, viewport.gutter);
506 }
507 MotionKind::CursorToWindowRight => {
508 viewport.left =
509 display_with_gutter(buffer.line(cursor.line), cursor.column, viewport.gutter)
510 .saturating_sub(viewport.width.saturating_sub(1));
511 }
512 MotionKind::RepeatCharacterSearch
516 | MotionKind::ReverseCharacterSearch
517 | MotionKind::SearchNext
518 | MotionKind::SearchPrevious
519 | MotionKind::SearchWordForward
520 | MotionKind::SearchWordBackward
521 | MotionKind::SearchPartialWordForward
522 | MotionKind::SearchPartialWordBackward
523 | MotionKind::PreviousMarkLine
524 | MotionKind::PreviousMarkExact
525 | MotionKind::NextMarkLine
526 | MotionKind::NextMarkExact => {}
527 }
528 cursor = buffer.clamp(cursor.into());
529 viewport.desired_column = if preserves_desired_column(motion.kind()) {
530 Some(desired_column)
531 } else if motion.kind() == MotionKind::LineEnd {
532 Some(usize::MAX)
535 } else if is_viewport_only_motion(motion.kind()) {
536 viewport.desired_column
537 } else {
538 Some(display_column(buffer.line(cursor.line), cursor.column))
539 };
540 keep_cursor_visible(&buffer, cursor, viewport, motion.kind());
541 cursor.into()
542}
543
544fn preserves_desired_column(kind: MotionKind) -> bool {
545 matches!(
546 kind,
547 MotionKind::Up | MotionKind::Down | MotionKind::ScrollLineUp | MotionKind::ScrollLineDown
548 )
549}
550
551fn is_viewport_only_motion(kind: MotionKind) -> bool {
552 matches!(
553 kind,
554 MotionKind::CursorToWindowTop
555 | MotionKind::CursorToWindowMiddle
556 | MotionKind::CursorToWindowBottom
557 | MotionKind::ScrollColumnLeft
558 | MotionKind::ScrollColumnRight
559 | MotionKind::ScrollHalfScreenLeft
560 | MotionKind::ScrollHalfScreenRight
561 | MotionKind::CursorToWindowLeft
562 | MotionKind::CursorToWindowRight
563 )
564}
565
566fn follow_horizontal_scroll(
567 buffer: &TextBuffer<'_>,
568 cursor: &mut Position,
569 viewport: &mut Viewport,
570) {
571 let line = buffer.line(cursor.line);
572 let last_display = display_with_gutter(line, last_column(line), viewport.gutter);
573 viewport.left = viewport.left.min(last_display);
574 let display = display_with_gutter(line, cursor.column, viewport.gutter);
575 let requested = if display < viewport.left {
576 Some(viewport.left)
577 } else if display >= viewport.left.saturating_add(viewport.width) {
578 Some(
579 viewport
580 .left
581 .saturating_add(viewport.width.saturating_sub(1)),
582 )
583 } else {
584 None
585 };
586 if let Some(requested) = requested {
587 cursor.column = byte_at_display(line, requested.saturating_sub(viewport.gutter));
588 }
589}
590
591pub fn reveal(lines: &[&str], position: Cursor, viewport: &mut Viewport) {
599 let buffer = TextBuffer::new(lines);
600 let cursor = buffer.clamp(position);
601 keep_cursor_visible(&buffer, cursor, viewport, MotionKind::Left);
602}
603
604fn repeated_boundary(
605 mut cursor: Position,
606 count: usize,
607 mut step: impl FnMut(Position) -> Position,
608) -> Position {
609 for _ in 0..count {
612 let next = step(cursor);
613 if next == cursor {
614 break;
615 }
616 cursor = next;
617 }
618 cursor
619}
620
621fn keep_cursor_visible(
622 buffer: &TextBuffer<'_>,
623 cursor: Position,
624 viewport: &mut Viewport,
625 kind: MotionKind,
626) {
627 keep_line_cursor_visible(
628 buffer.line(cursor.line),
629 buffer.last_line(),
630 cursor,
631 viewport,
632 kind,
633 );
634}
635
636pub(crate) fn reveal_cursor_line(
637 line: &str,
638 last_line: usize,
639 cursor: Cursor,
640 viewport: &mut Viewport,
641) {
642 keep_line_cursor_visible(
643 line,
644 last_line,
645 Position {
646 line: cursor.line(),
647 column: clamp_boundary(line, cursor.byte_column()),
648 },
649 viewport,
650 MotionKind::Left,
651 );
652}
653
654fn keep_line_cursor_visible(
655 line: &str,
656 last_line: usize,
657 cursor: Position,
658 viewport: &mut Viewport,
659 kind: MotionKind,
660) {
661 if !matches!(
662 kind,
663 MotionKind::CursorToWindowTop
664 | MotionKind::CursorToWindowTopFirstNonBlank
665 | MotionKind::CursorToWindowMiddle
666 | MotionKind::CursorToWindowMiddleFirstNonBlank
667 | MotionKind::CursorToWindowBottom
668 | MotionKind::CursorToWindowBottomFirstNonBlank
669 | MotionKind::NextWindowTop
670 | MotionKind::PreviousWindowBottom
671 | MotionKind::ScrollLineDown
672 | MotionKind::ScrollLineUp
673 ) {
674 if cursor.line < viewport.top {
675 viewport.top = cursor.line;
676 } else if cursor.line >= viewport.top.saturating_add(viewport.height) {
677 viewport.top = cursor
678 .line
679 .saturating_sub(viewport.height.saturating_sub(1));
680 }
681 }
682 viewport.top = viewport.top.min(last_line);
683
684 if matches!(
685 kind,
686 MotionKind::ScrollColumnLeft
687 | MotionKind::ScrollColumnRight
688 | MotionKind::ScrollHalfScreenLeft
689 | MotionKind::ScrollHalfScreenRight
690 | MotionKind::CursorToWindowLeft
691 | MotionKind::CursorToWindowRight
692 ) {
693 return;
694 }
695 let source_display = display_column(line, cursor.column);
696 let display = source_display.saturating_add(viewport.gutter);
697 let cursor_width = line[cursor.column..].chars().next().map_or(1, |character| {
698 if character == '\t' {
699 next_tabstop(source_display)
700 } else {
701 UnicodeWidthChar::width(character).unwrap_or(0).max(1)
702 }
703 });
704 let end = display.saturating_add(cursor_width);
705 if display < viewport.left {
706 viewport.left = display;
707 } else if end > viewport.left.saturating_add(viewport.width) {
708 viewport.left = end.saturating_sub(viewport.width);
709 }
710}