use crate::id;
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum MotionShape {
Characterwise { inclusive: bool },
Linewise,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Range {
pub start: id::ByteOffset,
pub end: id::ByteOffset,
pub shape: MotionShape,
}
impl Range {
pub fn charwise(start: impl Into<id::ByteOffset>, end: impl Into<id::ByteOffset>) -> Self {
let (start, end) = (start.into(), end.into());
debug_assert!(start <= end);
Self {
start,
end,
shape: MotionShape::Characterwise { inclusive: false },
}
}
pub fn linewise(start: impl Into<id::ByteOffset>, end: impl Into<id::ByteOffset>) -> Self {
let (start, end) = (start.into(), end.into());
debug_assert!(start <= end);
Self {
start,
end,
shape: MotionShape::Linewise,
}
}
pub fn is_linewise(&self) -> bool {
matches!(self.shape, MotionShape::Linewise)
}
pub fn with_inclusive(mut self, inclusive: bool) -> Self {
if let MotionShape::Characterwise { inclusive: i } = &mut self.shape {
*i = inclusive;
}
self
}
pub fn inclusive(&self) -> bool {
matches!(self.shape, MotionShape::Characterwise { inclusive: true })
}
pub fn len(&self) -> usize {
self.end - self.start
}
pub fn is_empty(&self) -> bool {
self.start == self.end
}
}