mod buffer;
mod engine;
mod line;
#[cfg(test)]
mod tests;
pub(crate) use engine::reveal_cursor_line;
pub use engine::{apply, reveal};
pub(crate) const FULL_PERCENT: usize = 100;
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct Cursor {
line: usize,
byte_column: usize,
}
impl Cursor {
#[must_use]
pub const fn new(line: usize, byte_column: usize) -> Self {
Self { line, byte_column }
}
#[must_use]
pub const fn line(self) -> usize {
self.line
}
#[must_use]
pub const fn byte_column(self) -> usize {
self.byte_column
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CountSource {
Implicit,
Explicit,
}
impl CountSource {
#[must_use]
pub const fn is_explicit(self) -> bool {
matches!(self, Self::Explicit)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum Repetition {
Original,
Repeated,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Motion {
kind: MotionKind,
count: usize,
count_source: CountSource,
target: Option<char>,
repetition: Repetition,
}
impl Motion {
#[must_use]
pub const fn new(kind: MotionKind) -> Self {
Self {
kind,
count: 1,
count_source: CountSource::Implicit,
target: None,
repetition: Repetition::Original,
}
}
#[must_use]
pub const fn kind(self) -> MotionKind {
self.kind
}
#[must_use]
pub const fn count(self) -> usize {
self.count
}
#[must_use]
pub const fn has_explicit_count(self) -> bool {
self.count_source.is_explicit()
}
#[must_use]
pub const fn target(self) -> Option<char> {
self.target
}
#[must_use]
pub const fn counted(mut self, count: usize, source: CountSource) -> Self {
self.count = if count == 0 { 1 } else { count };
self.count_source = source;
self
}
#[must_use]
pub const fn targeting(mut self, target: char) -> Self {
self.target = Some(target);
self
}
#[must_use]
pub const fn repeating(mut self) -> Self {
self.repetition = Repetition::Repeated;
self
}
#[must_use]
pub const fn is_repeated(self) -> bool {
matches!(self.repetition, Repetition::Repeated)
}
pub(super) const fn repetition(self) -> Repetition {
self.repetition
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum MotionKind {
Left,
LeftWrap,
Right,
RightWrap,
Up,
Down,
LineStart,
FirstNonBlank,
LineEnd,
LastNonBlank,
ScreenLineStart,
ScreenFirstNonBlank,
ScreenLineEnd,
ScreenLastNonBlank,
ScreenMiddle,
LineMiddle,
Column,
ByteOffset,
WordForward,
BigWordForward,
WordEndForward,
BigWordEndForward,
WordBackward,
BigWordBackward,
WordEndBackward,
BigWordEndBackward,
FindForward,
FindBackward,
TillForward,
TillBackward,
RepeatCharacterSearch,
ReverseCharacterSearch,
PreviousLineFirstNonBlank,
NextLineFirstNonBlank,
CountedLineFirstNonBlank,
BufferTop,
BufferBottom,
BufferBottomEnd,
BufferPercentage,
WindowTop,
WindowMiddle,
WindowBottom,
SentenceBackward,
SentenceForward,
ParagraphBackward,
ParagraphForward,
SectionStartBackward,
SectionStartForward,
SectionEndBackward,
SectionEndForward,
MatchingPair,
MatchingPairBackward,
UnmatchedOpenBackward,
UnmatchedCloseForward,
MethodBackward,
MethodForward,
PreprocessorBackward,
PreprocessorForward,
CommentBackward,
CommentForward,
DiffChangeBackward,
DiffChangeForward,
SearchNext,
SearchPrevious,
SearchWordForward,
SearchWordBackward,
SearchPartialWordForward,
SearchPartialWordBackward,
PreviousMarkLine,
PreviousMarkExact,
NextMarkLine,
NextMarkExact,
HalfPageDown,
HalfPageUp,
PageDown,
PageUp,
ScrollLineDown,
ScrollLineUp,
CursorToWindowTop,
CursorToWindowTopFirstNonBlank,
CursorToWindowMiddle,
CursorToWindowMiddleFirstNonBlank,
CursorToWindowBottom,
CursorToWindowBottomFirstNonBlank,
NextWindowTop,
PreviousWindowBottom,
ScrollColumnLeft,
ScrollColumnRight,
ScrollHalfScreenLeft,
ScrollHalfScreenRight,
CursorToWindowLeft,
CursorToWindowRight,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Viewport {
top: usize,
left: usize,
height: usize,
width: usize,
gutter: usize,
desired_column: Option<usize>,
}
impl Viewport {
#[must_use]
pub fn new(top: usize, left: usize, height: usize, width: usize, gutter: usize) -> Self {
Self {
top,
left,
height: height.max(1),
width: width.max(1),
gutter,
desired_column: None,
}
}
#[must_use]
pub fn with_desired_column(mut self, desired_column: Option<usize>) -> Self {
self.desired_column = desired_column;
self
}
#[must_use]
pub const fn top(&self) -> usize {
self.top
}
#[must_use]
pub const fn left(&self) -> usize {
self.left
}
#[must_use]
pub const fn height(&self) -> usize {
self.height
}
#[must_use]
pub const fn width(&self) -> usize {
self.width
}
#[must_use]
pub const fn gutter(&self) -> usize {
self.gutter
}
#[must_use]
pub const fn desired_column(&self) -> Option<usize> {
self.desired_column
}
}