use std::ops::Range;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SyntaxEdit {
pub start_byte: usize,
pub old_end_byte: usize,
pub new_end_byte: usize,
pub start_row: u32,
pub start_col: u32,
pub old_end_row: u32,
pub old_end_col: u32,
pub new_end_row: u32,
pub new_end_col: u32,
}
impl SyntaxEdit {
#[must_use]
#[allow(clippy::too_many_arguments)]
pub const fn new(
start_byte: usize,
old_end_byte: usize,
new_end_byte: usize,
start_row: u32,
start_col: u32,
old_end_row: u32,
old_end_col: u32,
new_end_row: u32,
new_end_col: u32,
) -> Self {
Self {
start_byte,
old_end_byte,
new_end_byte,
start_row,
start_col,
old_end_row,
old_end_col,
new_end_row,
new_end_col,
}
}
#[must_use]
pub const fn insert(
start_byte: usize,
start_row: u32,
start_col: u32,
new_end_byte: usize,
new_end_row: u32,
new_end_col: u32,
) -> Self {
Self {
start_byte,
old_end_byte: start_byte,
new_end_byte,
start_row,
start_col,
old_end_row: start_row,
old_end_col: start_col,
new_end_row,
new_end_col,
}
}
#[must_use]
pub const fn delete(
start_byte: usize,
start_row: u32,
start_col: u32,
old_end_byte: usize,
old_end_row: u32,
old_end_col: u32,
) -> Self {
Self {
start_byte,
old_end_byte,
new_end_byte: start_byte,
start_row,
start_col,
old_end_row,
old_end_col,
new_end_row: start_row,
new_end_col: start_col,
}
}
#[must_use]
pub const fn affected_range(&self) -> Range<usize> {
let end = if self.old_end_byte > self.new_end_byte {
self.old_end_byte
} else {
self.new_end_byte
};
self.start_byte..end
}
#[must_use]
pub const fn bytes_removed(&self) -> usize {
self.old_end_byte - self.start_byte
}
#[must_use]
pub const fn bytes_inserted(&self) -> usize {
self.new_end_byte - self.start_byte
}
#[must_use]
#[allow(clippy::cast_possible_wrap)]
pub const fn byte_delta(&self) -> isize {
self.new_end_byte as isize - self.old_end_byte as isize
}
#[must_use]
pub const fn is_insert(&self) -> bool {
self.old_end_byte == self.start_byte
}
#[must_use]
pub const fn is_delete(&self) -> bool {
self.new_end_byte == self.start_byte
}
#[must_use]
pub const fn is_replace(&self) -> bool {
self.old_end_byte != self.start_byte && self.new_end_byte != self.start_byte
}
}
#[cfg(test)]
#[path = "edit_tests.rs"]
mod tests;