use continuity_text::{EditOp, Position, Range, Selection, SelectionKind};
use ropey::Rope;
use crate::selection_edit::SelectionEditPlan;
use crate::Error;
#[derive(Clone)]
pub struct EditSpec {
pub start: usize,
pub end: usize,
pub start_position: Position,
pub end_position_in_rope: Option<Position>,
pub inserted: String,
}
impl EditSpec {
pub(crate) fn delete(rope: &Rope, start: usize, end: usize) -> Result<Self, Error> {
Ok(Self {
start,
end,
start_position: Position::from_byte_offset(rope, start)?,
end_position_in_rope: Some(Position::from_byte_offset(rope, end)?),
inserted: String::new(),
})
}
pub fn insert(rope: &Rope, at: usize, text: String) -> Result<Self, Error> {
let position = Position::from_byte_offset(rope, at)?;
Ok(Self {
start: at,
end: at,
start_position: position,
end_position_in_rope: Some(position),
inserted: text,
})
}
pub(crate) fn replace(
rope: &Rope,
start: usize,
end: usize,
text: String,
) -> Result<Self, Error> {
Ok(Self {
start,
end,
start_position: Position::from_byte_offset(rope, start)?,
end_position_in_rope: Some(Position::from_byte_offset(rope, end)?),
inserted: text,
})
}
pub(crate) fn into_op(self) -> EditOp {
let range = Range::new(self.start_position, self.end_position());
if self.start == self.end {
EditOp::insert(self.start_position, self.inserted)
} else if self.inserted.is_empty() {
EditOp::delete(range)
} else {
EditOp::replace(range, self.inserted)
}
}
fn end_position(&self) -> Position {
self.end_position_in_rope.unwrap_or(self.start_position)
}
}
pub(crate) fn finalize_specs(
mut specs: Vec<EditSpec>,
selections_before: Vec<Selection>,
selections_after: Vec<Selection>,
) -> Option<SelectionEditPlan> {
if specs.is_empty() {
return None;
}
specs.sort_by_key(|spec| spec.start);
let merged = merge_specs(specs);
let ops = merged.into_iter().rev().map(EditSpec::into_op).collect();
Some(SelectionEditPlan {
ops,
selections_before,
selections_after,
})
}
pub(crate) fn finalize_specs_with_transformed_selections(
mut specs: Vec<EditSpec>,
selections_before: Vec<Selection>,
) -> Option<SelectionEditPlan> {
if specs.is_empty() {
return None;
}
specs.sort_by_key(|spec| spec.start);
let merged = merge_specs(specs);
let mut selections_after = selections_before.clone();
for spec in merged.iter().rev() {
for selection in &mut selections_after {
selection.anchor = compute_position_after_spec(selection.anchor, spec);
selection.head = compute_position_after_spec(selection.head, spec);
}
}
let ops = merged.into_iter().rev().map(EditSpec::into_op).collect();
Some(SelectionEditPlan {
ops,
selections_before,
selections_after,
})
}
fn compute_position_after_spec(position: Position, spec: &EditSpec) -> Position {
let start = spec.start_position;
let old_end = spec.end_position();
let new_end = advance_position(start, &spec.inserted);
if position < start {
return position;
}
if position <= old_end {
return new_end;
}
let lines_after_old_end = position.line.saturating_sub(old_end.line);
let byte_in_line = if position.line == old_end.line {
new_end
.byte_in_line
.saturating_add(position.byte_in_line.saturating_sub(old_end.byte_in_line))
} else {
position.byte_in_line
};
Position::new(
new_end.line.saturating_add(lines_after_old_end),
byte_in_line,
)
}
pub(crate) fn ranges_for_selection(rope: &Rope, selection: Selection) -> Result<Vec<Range>, Error> {
if selection.kind != SelectionKind::BlockWise || selection.is_collapsed() {
return Ok(vec![selection.ordered_range()]);
}
let start_line = selection.anchor.line.min(selection.head.line);
let end_line = selection.anchor.line.max(selection.head.line);
let start_col = selection
.anchor
.byte_in_line
.min(selection.head.byte_in_line);
let end_col = selection
.anchor
.byte_in_line
.max(selection.head.byte_in_line);
let mut ranges = Vec::new();
for line in start_line..=end_line {
let start = line_column(rope, line, start_col)?;
let end = line_column(rope, line, end_col)?;
ranges.push(Range::new(start, end));
}
Ok(ranges)
}
fn line_column(rope: &Rope, line: u32, column: u32) -> Result<Position, Error> {
let line = (line as usize).min(rope.len_lines().saturating_sub(1));
let start = rope.line_to_byte(line);
let end = line_content_end(rope, line);
let byte = (start + column as usize).min(end);
Ok(Position::from_byte_offset(rope, byte)?)
}
pub(crate) fn line_content_end(rope: &Rope, line: usize) -> usize {
let start = rope.line_to_byte(line);
let next = if line + 1 < rope.len_lines() {
rope.line_to_byte(line + 1)
} else {
rope.len_bytes()
};
let mut end = next;
let slice = rope.byte_slice(start..next).to_string();
if slice.ends_with('\n') {
end = end.saturating_sub(1);
if slice.ends_with("\r\n") {
end = end.saturating_sub(1);
}
}
end
}
pub(crate) fn caret_delete_range(
rope: &Rope,
byte: usize,
backward: bool,
) -> Option<(usize, usize)> {
if backward {
if byte == 0 {
return None;
}
let char_idx = rope.byte_to_char(byte);
Some((rope.char_to_byte(char_idx.saturating_sub(1)), byte))
} else {
if byte >= rope.len_bytes() {
return None;
}
let char_idx = rope.byte_to_char(byte);
let next = (char_idx + 1).min(rope.len_chars());
Some((byte, rope.char_to_byte(next)))
}
}
pub(crate) fn merge_specs(specs: Vec<EditSpec>) -> Vec<EditSpec> {
let mut merged: Vec<EditSpec> = Vec::new();
for spec in specs {
if let Some(last) = merged.last_mut() {
if spec.start <= last.end && last.inserted.is_empty() && spec.inserted.is_empty() {
last.end = last.end.max(spec.end);
last.end_position_in_rope = spec.end_position_in_rope;
continue;
}
}
merged.push(spec);
}
merged
}
pub(crate) fn advance_position(mut position: Position, text: &str) -> Position {
for ch in text.chars() {
if ch == '\n' {
position.line = position.line.saturating_add(1);
position.byte_in_line = 0;
} else {
position.byte_in_line = position.byte_in_line.saturating_add(ch.len_utf8() as u32);
}
}
position
}