use unicode_width::UnicodeWidthChar;
use super::buffer::{Position, ScanDirection, TextBuffer, WordMotion, WordStyle};
use super::line::{
byte_at_display, clamp_boundary, count_as_isize, display_column, display_with_gutter,
find_character, first_non_blank, first_non_blank_from, last_column, last_non_blank,
next_column, next_tabstop, previous_column, screen_end_column, screen_last_non_blank,
visible_source_columns,
};
use super::{Cursor, FULL_PERCENT, Motion, MotionKind, Viewport};
const DEFAULT_MOTION_PERCENT: usize = FULL_PERCENT / 2;
const PERCENT_ROUNDING_OFFSET: usize = FULL_PERCENT - 1;
const PAGE_OVERLAP_LINES: usize = 2;
pub fn apply(lines: &[&str], position: Cursor, viewport: &mut Viewport, motion: Motion) -> Cursor {
let buffer = TextBuffer::new(lines);
let mut cursor = buffer.clamp(position);
let count = motion.count().max(1);
let desired_column = viewport
.desired_column
.unwrap_or_else(|| display_column(buffer.line(cursor.line), cursor.column));
match motion.kind() {
MotionKind::Left => {
cursor = repeated_boundary(cursor, count, |mut at| {
at.column = previous_column(buffer.line(at.line), at.column);
at
});
}
MotionKind::LeftWrap => {
cursor = repeated_boundary(cursor, count, |mut at| {
if at.column == 0 && at.line > 0 {
at.line = at.line.saturating_sub(1);
at.column = last_column(buffer.line(at.line));
} else {
at.column = previous_column(buffer.line(at.line), at.column);
}
at
});
}
MotionKind::Right => {
cursor = repeated_boundary(cursor, count, |mut at| {
at.column = next_column(buffer.line(at.line), at.column);
at
});
}
MotionKind::RightWrap => {
cursor = repeated_boundary(cursor, count, |mut at| {
let line = buffer.line(at.line);
if at.column >= last_column(line) && at.line < buffer.last_line() {
at.line = at.line.saturating_add(1);
at.column = 0;
} else {
at.column = next_column(line, at.column);
}
at
});
}
MotionKind::Up => {
cursor = buffer.move_lines_to_display(cursor, -(count_as_isize(count)), desired_column)
}
MotionKind::Down => {
cursor = buffer.move_lines_to_display(cursor, count_as_isize(count), desired_column)
}
MotionKind::LineStart => cursor.column = 0,
MotionKind::FirstNonBlank => cursor.column = first_non_blank(buffer.line(cursor.line)),
MotionKind::LineEnd => {
cursor = buffer.move_lines(cursor, count_as_isize(count.saturating_sub(1)));
cursor.column = last_column(buffer.line(cursor.line));
}
MotionKind::LastNonBlank => {
cursor = buffer.move_lines(cursor, count_as_isize(count.saturating_sub(1)));
cursor.column = last_non_blank(buffer.line(cursor.line));
}
MotionKind::ScreenLineStart => {
let (source_left, _) = visible_source_columns(*viewport);
cursor.column = byte_at_display(buffer.line(cursor.line), source_left);
}
MotionKind::ScreenFirstNonBlank => {
let (source_left, _) = visible_source_columns(*viewport);
let screen = byte_at_display(buffer.line(cursor.line), source_left);
cursor.column = first_non_blank_from(buffer.line(cursor.line), screen);
}
MotionKind::ScreenLineEnd => {
cursor = buffer.move_lines(cursor, count_as_isize(count.saturating_sub(1)));
cursor.column = screen_end_column(buffer.line(cursor.line), *viewport);
}
MotionKind::ScreenLastNonBlank => {
cursor = buffer.move_lines(cursor, count_as_isize(count.saturating_sub(1)));
cursor.column = screen_last_non_blank(buffer.line(cursor.line), *viewport);
}
MotionKind::ScreenMiddle => {
let (source_left, source_end) = visible_source_columns(*viewport);
cursor.column = byte_at_display(
buffer.line(cursor.line),
source_left.saturating_add(source_end.saturating_sub(source_left) / 2),
);
}
MotionKind::LineMiddle => {
let percent = if motion.has_explicit_count() {
count.min(FULL_PERCENT)
} else {
DEFAULT_MOTION_PERCENT
};
let line = buffer.line(cursor.line);
cursor.column = byte_at_display(
line,
display_column(line, line.len()).saturating_mul(percent) / FULL_PERCENT,
);
}
MotionKind::Column => {
cursor.column = byte_at_display(buffer.line(cursor.line), count.saturating_sub(1));
}
MotionKind::ByteOffset => cursor = buffer.byte_offset(count),
MotionKind::WordForward => {
cursor = buffer.word_motion(cursor, count, WordMotion::StartForward, WordStyle::Word)
}
MotionKind::BigWordForward => {
cursor = buffer.word_motion(cursor, count, WordMotion::StartForward, WordStyle::BigWord)
}
MotionKind::WordEndForward => {
cursor = buffer.word_motion(cursor, count, WordMotion::EndForward, WordStyle::Word)
}
MotionKind::BigWordEndForward => {
cursor = buffer.word_motion(cursor, count, WordMotion::EndForward, WordStyle::BigWord)
}
MotionKind::WordBackward => {
cursor = buffer.word_motion(cursor, count, WordMotion::StartBackward, WordStyle::Word)
}
MotionKind::BigWordBackward => {
cursor =
buffer.word_motion(cursor, count, WordMotion::StartBackward, WordStyle::BigWord)
}
MotionKind::WordEndBackward => {
cursor = buffer.word_motion(cursor, count, WordMotion::EndBackward, WordStyle::Word)
}
MotionKind::BigWordEndBackward => {
cursor = buffer.word_motion(cursor, count, WordMotion::EndBackward, WordStyle::BigWord)
}
MotionKind::FindForward
| MotionKind::FindBackward
| MotionKind::TillForward
| MotionKind::TillBackward => {
if let Some(target) = motion.target() {
cursor.column = find_character(
buffer.line(cursor.line),
cursor.column,
target,
motion.kind(),
count,
motion.repetition(),
);
}
}
MotionKind::PreviousLineFirstNonBlank => {
cursor = buffer.move_lines(cursor, -(count_as_isize(count)));
cursor.column = first_non_blank(buffer.line(cursor.line));
}
MotionKind::NextLineFirstNonBlank => {
cursor = buffer.move_lines(cursor, count_as_isize(count));
cursor.column = first_non_blank(buffer.line(cursor.line));
}
MotionKind::CountedLineFirstNonBlank => {
cursor = buffer.move_lines(cursor, count_as_isize(count.saturating_sub(1)));
cursor.column = first_non_blank(buffer.line(cursor.line));
}
MotionKind::BufferTop => {
cursor.line = if motion.has_explicit_count() {
count.saturating_sub(1).min(buffer.last_line())
} else {
0
};
cursor.column = first_non_blank(buffer.line(cursor.line));
}
MotionKind::BufferBottom => {
cursor.line = buffer.last_line();
cursor.column = first_non_blank(buffer.line(cursor.line));
}
MotionKind::BufferBottomEnd => {
cursor.line = if motion.has_explicit_count() {
count.saturating_sub(1).min(buffer.last_line())
} else {
buffer.last_line()
};
cursor.column = last_column(buffer.line(cursor.line));
}
MotionKind::BufferPercentage => {
let one_based = count
.min(FULL_PERCENT)
.saturating_mul(buffer.len())
.saturating_add(PERCENT_ROUNDING_OFFSET)
/ FULL_PERCENT;
cursor.line = one_based.saturating_sub(1).min(buffer.last_line());
cursor.column = first_non_blank(buffer.line(cursor.line));
}
MotionKind::WindowTop => {
cursor.line = viewport
.top
.saturating_add(count.min(viewport.height).saturating_sub(1))
.min(buffer.last_line());
cursor.column = first_non_blank(buffer.line(cursor.line));
}
MotionKind::WindowMiddle => {
cursor.line = viewport
.top
.saturating_add(
buffer
.len()
.saturating_sub(viewport.top)
.min(viewport.height)
.saturating_sub(1)
/ 2,
)
.min(buffer.last_line());
cursor.column = first_non_blank(buffer.line(cursor.line));
}
MotionKind::WindowBottom => {
let bottom = viewport
.top
.saturating_add(viewport.height.saturating_sub(1))
.min(buffer.last_line());
cursor.line = bottom
.saturating_sub(count.saturating_sub(1))
.max(viewport.top.min(buffer.last_line()));
cursor.column = first_non_blank(buffer.line(cursor.line));
}
MotionKind::SentenceBackward => {
cursor = repeated_boundary(cursor, count, |at| buffer.sentence_backward(at));
}
MotionKind::SentenceForward => {
cursor = repeated_boundary(cursor, count, |at| buffer.sentence_forward(at));
}
MotionKind::ParagraphBackward => {
cursor = repeated_boundary(cursor, count, |at| buffer.paragraph_backward(at));
}
MotionKind::ParagraphForward => {
cursor = repeated_boundary(cursor, count, |at| buffer.paragraph_forward(at));
}
MotionKind::SectionStartBackward => {
cursor = repeated_boundary(cursor, count, |at| {
buffer.section(at, ScanDirection::Backward, '{')
});
}
MotionKind::SectionStartForward => {
cursor = repeated_boundary(cursor, count, |at| {
buffer.section(at, ScanDirection::Forward, '{')
});
}
MotionKind::SectionEndBackward => {
cursor = repeated_boundary(cursor, count, |at| {
buffer.section(at, ScanDirection::Backward, '}')
});
}
MotionKind::SectionEndForward => {
cursor = repeated_boundary(cursor, count, |at| {
buffer.section(at, ScanDirection::Forward, '}')
});
}
MotionKind::MatchingPair => {
if let Some(found) = buffer.matching_pair(cursor, ScanDirection::Forward) {
cursor = found;
}
}
MotionKind::MatchingPairBackward => {
if let Some(found) = buffer.matching_pair(cursor, ScanDirection::Backward) {
cursor = found;
}
}
MotionKind::UnmatchedOpenBackward => {
if let Some(target) = motion.target() {
cursor = repeated_boundary(cursor, count, |at| buffer.unmatched_open(at, target));
}
}
MotionKind::UnmatchedCloseForward => {
if let Some(target) = motion.target() {
cursor = repeated_boundary(cursor, count, |at| buffer.unmatched_close(at, target));
}
}
MotionKind::MethodBackward => {
let target = if motion.target() == Some('M') {
'}'
} else {
'{'
};
cursor = repeated_boundary(cursor, count, |at| {
buffer.brace(at, ScanDirection::Backward, target)
});
}
MotionKind::MethodForward => {
let target = if motion.target() == Some('M') {
'}'
} else {
'{'
};
cursor = repeated_boundary(cursor, count, |at| {
buffer.brace(at, ScanDirection::Forward, target)
});
}
MotionKind::PreprocessorBackward => {
cursor = repeated_boundary(cursor, count, |at| {
buffer.preprocessor(at, ScanDirection::Backward)
});
}
MotionKind::PreprocessorForward => {
cursor = repeated_boundary(cursor, count, |at| {
buffer.preprocessor(at, ScanDirection::Forward)
});
}
MotionKind::CommentBackward => {
cursor = repeated_boundary(cursor, count, |at| {
buffer.comment(at, ScanDirection::Backward)
});
}
MotionKind::CommentForward => {
cursor = repeated_boundary(cursor, count, |at| {
buffer.comment(at, ScanDirection::Forward)
});
}
MotionKind::DiffChangeBackward => {
cursor = repeated_boundary(cursor, count, |at| {
buffer.diff_change(at, ScanDirection::Backward)
});
}
MotionKind::DiffChangeForward => {
cursor = repeated_boundary(cursor, count, |at| {
buffer.diff_change(at, ScanDirection::Forward)
});
}
MotionKind::HalfPageDown => {
let distance = if motion.has_explicit_count() {
count
} else {
viewport.height.saturating_div(2).max(1)
};
cursor = buffer.move_lines_to_display(cursor, count_as_isize(distance), desired_column);
cursor.column = first_non_blank(buffer.line(cursor.line));
viewport.top = viewport
.top
.saturating_add(distance)
.min(buffer.last_line());
}
MotionKind::HalfPageUp => {
let distance = if motion.has_explicit_count() {
count
} else {
viewport.height.saturating_div(2).max(1)
};
cursor =
buffer.move_lines_to_display(cursor, -(count_as_isize(distance)), desired_column);
cursor.column = first_non_blank(buffer.line(cursor.line));
viewport.top = viewport.top.saturating_sub(distance);
}
MotionKind::PageDown => {
let page = viewport.height.saturating_sub(PAGE_OVERLAP_LINES).max(1);
viewport.top = viewport
.top
.saturating_add(page.saturating_mul(count))
.min(buffer.last_line());
cursor.line = viewport.top;
cursor.column = first_non_blank(buffer.line(cursor.line));
}
MotionKind::PageUp => {
let page = viewport.height.saturating_sub(PAGE_OVERLAP_LINES).max(1);
viewport.top = viewport.top.saturating_sub(page.saturating_mul(count));
cursor.line = viewport
.top
.saturating_add(viewport.height.saturating_sub(1))
.min(buffer.last_line());
cursor.column = first_non_blank(buffer.line(cursor.line));
}
MotionKind::ScrollLineDown => {
viewport.top = viewport.top.saturating_add(count).min(buffer.last_line());
if cursor.line < viewport.top {
cursor = buffer.move_to_line_display(cursor, viewport.top, desired_column);
}
}
MotionKind::ScrollLineUp => {
viewport.top = viewport.top.saturating_sub(count);
let bottom = viewport
.top
.saturating_add(viewport.height.saturating_sub(1))
.min(buffer.last_line());
if cursor.line > bottom {
cursor = buffer.move_to_line_display(cursor, bottom, desired_column);
}
}
MotionKind::CursorToWindowTop => {
if motion.has_explicit_count() {
cursor =
buffer.move_to_line_display(cursor, count.saturating_sub(1), desired_column);
}
viewport.top = cursor.line;
}
MotionKind::CursorToWindowTopFirstNonBlank => {
if motion.has_explicit_count() {
cursor.line = count.saturating_sub(1).min(buffer.last_line());
}
cursor.column = first_non_blank(buffer.line(cursor.line));
viewport.top = cursor.line;
}
MotionKind::CursorToWindowMiddle => {
if motion.has_explicit_count() {
cursor =
buffer.move_to_line_display(cursor, count.saturating_sub(1), desired_column);
}
viewport.top = cursor
.line
.saturating_sub(viewport.height.saturating_sub(1) / 2);
}
MotionKind::CursorToWindowMiddleFirstNonBlank => {
if motion.has_explicit_count() {
cursor.line = count.saturating_sub(1).min(buffer.last_line());
}
cursor.column = first_non_blank(buffer.line(cursor.line));
viewport.top = cursor
.line
.saturating_sub(viewport.height.saturating_sub(1) / 2);
}
MotionKind::CursorToWindowBottom => {
if motion.has_explicit_count() {
cursor =
buffer.move_to_line_display(cursor, count.saturating_sub(1), desired_column);
}
viewport.top = cursor
.line
.saturating_sub(viewport.height.saturating_sub(1));
}
MotionKind::CursorToWindowBottomFirstNonBlank => {
if motion.has_explicit_count() {
cursor.line = count.saturating_sub(1).min(buffer.last_line());
}
cursor.column = first_non_blank(buffer.line(cursor.line));
viewport.top = cursor
.line
.saturating_sub(viewport.height.saturating_sub(1));
}
MotionKind::NextWindowTop => {
let target = if motion.has_explicit_count() {
count.saturating_sub(1).min(buffer.last_line())
} else {
viewport
.top
.saturating_add(viewport.height)
.min(buffer.last_line())
};
cursor = buffer.move_to_line(cursor, target);
cursor.column = first_non_blank(buffer.line(cursor.line));
viewport.top = target;
}
MotionKind::PreviousWindowBottom => {
let target = if motion.has_explicit_count() {
count
.saturating_sub(viewport.height)
.min(buffer.last_line())
} else {
viewport.top.saturating_sub(1)
};
cursor = buffer.move_to_line(cursor, target);
cursor.column = first_non_blank(buffer.line(cursor.line));
viewport.top = target.saturating_sub(viewport.height.saturating_sub(1));
}
MotionKind::ScrollColumnLeft => {
viewport.left = viewport.left.saturating_sub(count);
follow_horizontal_scroll(&buffer, &mut cursor, viewport);
}
MotionKind::ScrollColumnRight => {
viewport.left = viewport.left.saturating_add(count);
follow_horizontal_scroll(&buffer, &mut cursor, viewport);
}
MotionKind::ScrollHalfScreenLeft => {
viewport.left = viewport
.left
.saturating_sub(viewport.width.saturating_div(2).saturating_mul(count));
follow_horizontal_scroll(&buffer, &mut cursor, viewport);
}
MotionKind::ScrollHalfScreenRight => {
viewport.left = viewport
.left
.saturating_add(viewport.width.saturating_div(2).saturating_mul(count));
follow_horizontal_scroll(&buffer, &mut cursor, viewport);
}
MotionKind::CursorToWindowLeft => {
viewport.left =
display_with_gutter(buffer.line(cursor.line), cursor.column, viewport.gutter);
}
MotionKind::CursorToWindowRight => {
viewport.left =
display_with_gutter(buffer.line(cursor.line), cursor.column, viewport.gutter)
.saturating_sub(viewport.width.saturating_sub(1));
}
MotionKind::RepeatCharacterSearch
| MotionKind::ReverseCharacterSearch
| MotionKind::SearchNext
| MotionKind::SearchPrevious
| MotionKind::SearchWordForward
| MotionKind::SearchWordBackward
| MotionKind::SearchPartialWordForward
| MotionKind::SearchPartialWordBackward
| MotionKind::PreviousMarkLine
| MotionKind::PreviousMarkExact
| MotionKind::NextMarkLine
| MotionKind::NextMarkExact => {}
}
cursor = buffer.clamp(cursor.into());
viewport.desired_column = if preserves_desired_column(motion.kind()) {
Some(desired_column)
} else if motion.kind() == MotionKind::LineEnd {
Some(usize::MAX)
} else if is_viewport_only_motion(motion.kind()) {
viewport.desired_column
} else {
Some(display_column(buffer.line(cursor.line), cursor.column))
};
keep_cursor_visible(&buffer, cursor, viewport, motion.kind());
cursor.into()
}
fn preserves_desired_column(kind: MotionKind) -> bool {
matches!(
kind,
MotionKind::Up | MotionKind::Down | MotionKind::ScrollLineUp | MotionKind::ScrollLineDown
)
}
fn is_viewport_only_motion(kind: MotionKind) -> bool {
matches!(
kind,
MotionKind::CursorToWindowTop
| MotionKind::CursorToWindowMiddle
| MotionKind::CursorToWindowBottom
| MotionKind::ScrollColumnLeft
| MotionKind::ScrollColumnRight
| MotionKind::ScrollHalfScreenLeft
| MotionKind::ScrollHalfScreenRight
| MotionKind::CursorToWindowLeft
| MotionKind::CursorToWindowRight
)
}
fn follow_horizontal_scroll(
buffer: &TextBuffer<'_>,
cursor: &mut Position,
viewport: &mut Viewport,
) {
let line = buffer.line(cursor.line);
let last_display = display_with_gutter(line, last_column(line), viewport.gutter);
viewport.left = viewport.left.min(last_display);
let display = display_with_gutter(line, cursor.column, viewport.gutter);
let requested = if display < viewport.left {
Some(viewport.left)
} else if display >= viewport.left.saturating_add(viewport.width) {
Some(
viewport
.left
.saturating_add(viewport.width.saturating_sub(1)),
)
} else {
None
};
if let Some(requested) = requested {
cursor.column = byte_at_display(line, requested.saturating_sub(viewport.gutter));
}
}
pub fn reveal(lines: &[&str], position: Cursor, viewport: &mut Viewport) {
let buffer = TextBuffer::new(lines);
let cursor = buffer.clamp(position);
keep_cursor_visible(&buffer, cursor, viewport, MotionKind::Left);
}
fn repeated_boundary(
mut cursor: Position,
count: usize,
mut step: impl FnMut(Position) -> Position,
) -> Position {
for _ in 0..count {
let next = step(cursor);
if next == cursor {
break;
}
cursor = next;
}
cursor
}
fn keep_cursor_visible(
buffer: &TextBuffer<'_>,
cursor: Position,
viewport: &mut Viewport,
kind: MotionKind,
) {
keep_line_cursor_visible(
buffer.line(cursor.line),
buffer.last_line(),
cursor,
viewport,
kind,
);
}
pub(crate) fn reveal_cursor_line(
line: &str,
last_line: usize,
cursor: Cursor,
viewport: &mut Viewport,
) {
keep_line_cursor_visible(
line,
last_line,
Position {
line: cursor.line(),
column: clamp_boundary(line, cursor.byte_column()),
},
viewport,
MotionKind::Left,
);
}
fn keep_line_cursor_visible(
line: &str,
last_line: usize,
cursor: Position,
viewport: &mut Viewport,
kind: MotionKind,
) {
if !matches!(
kind,
MotionKind::CursorToWindowTop
| MotionKind::CursorToWindowTopFirstNonBlank
| MotionKind::CursorToWindowMiddle
| MotionKind::CursorToWindowMiddleFirstNonBlank
| MotionKind::CursorToWindowBottom
| MotionKind::CursorToWindowBottomFirstNonBlank
| MotionKind::NextWindowTop
| MotionKind::PreviousWindowBottom
| MotionKind::ScrollLineDown
| MotionKind::ScrollLineUp
) {
if cursor.line < viewport.top {
viewport.top = cursor.line;
} else if cursor.line >= viewport.top.saturating_add(viewport.height) {
viewport.top = cursor
.line
.saturating_sub(viewport.height.saturating_sub(1));
}
}
viewport.top = viewport.top.min(last_line);
if matches!(
kind,
MotionKind::ScrollColumnLeft
| MotionKind::ScrollColumnRight
| MotionKind::ScrollHalfScreenLeft
| MotionKind::ScrollHalfScreenRight
| MotionKind::CursorToWindowLeft
| MotionKind::CursorToWindowRight
) {
return;
}
let source_display = display_column(line, cursor.column);
let display = source_display.saturating_add(viewport.gutter);
let cursor_width = line[cursor.column..].chars().next().map_or(1, |character| {
if character == '\t' {
next_tabstop(source_display)
} else {
UnicodeWidthChar::width(character).unwrap_or(0).max(1)
}
});
let end = display.saturating_add(cursor_width);
if display < viewport.left {
viewport.left = display;
} else if end > viewport.left.saturating_add(viewport.width) {
viewport.left = end.saturating_sub(viewport.width);
}
}