use serde::{Deserialize, Serialize};
#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default,
)]
pub struct Position {
pub line: usize,
pub col: usize,
}
impl Position {
pub const ZERO: Self = Self { line: 0, col: 0 };
pub const fn new(line: usize, col: usize) -> Self {
Self { line, col }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Selection {
pub anchor: Position,
pub focus: Position,
}
impl Selection {
pub const fn point(pos: Position) -> Self {
Self {
anchor: pos,
focus: pos,
}
}
pub const fn range(anchor: Position, focus: Position) -> Self {
Self { anchor, focus }
}
pub fn is_collapsed(&self) -> bool {
self.anchor == self.focus
}
pub fn ordered_range(&self) -> (Position, Position) {
if self.anchor <= self.focus {
(self.anchor, self.focus)
} else {
(self.focus, self.anchor)
}
}
pub fn contains(&self, pos: Position) -> bool {
let (start, end) = self.ordered_range();
pos >= start && pos <= end
}
pub fn touches_or_overlaps(&self, other: &Selection) -> bool {
let (s1, e1) = self.ordered_range();
let (s2, e2) = other.ordered_range();
!(e1 < s2 || e2 < s1)
}
pub fn merge(&self, other: &Selection) -> Self {
let (s1, e1) = self.ordered_range();
let (s2, e2) = other.ordered_range();
let min_pos = s1.min(s2);
let max_pos = e1.max(e2);
Self {
anchor: min_pos,
focus: max_pos,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MultiCursor {
selections: Vec<Selection>,
primary_idx: usize,
}
impl Default for MultiCursor {
fn default() -> Self {
Self::new()
}
}
impl MultiCursor {
pub fn new() -> Self {
Self {
selections: vec![Selection::point(Position::ZERO)],
primary_idx: 0,
}
}
pub fn single(pos: Position) -> Self {
Self {
selections: vec![Selection::point(pos)],
primary_idx: 0,
}
}
pub fn from_selections(selections: Vec<Selection>) -> Self {
if selections.is_empty() {
Self::new()
} else {
let mut mc = Self {
selections,
primary_idx: 0,
};
mc.normalize();
mc
}
}
pub fn primary(&self) -> &Selection {
&self.selections[self.primary_idx]
}
pub fn all(&self) -> &[Selection] {
&self.selections
}
pub fn all_mut(&mut self) -> &mut [Selection] {
&mut self.selections
}
pub fn clear(&mut self) {
self.selections.clear();
self.primary_idx = 0;
}
pub fn set_single(&mut self, pos: Position) {
self.selections = vec![Selection::point(pos)];
self.primary_idx = 0;
}
pub fn add_selection(&mut self, sel: Selection) {
self.selections.push(sel);
self.normalize();
}
pub fn add_cursor(&mut self, pos: Position) {
self.add_selection(Selection::point(pos));
}
pub fn count(&self) -> usize {
self.selections.len()
}
pub fn add_column_cursors(&mut self, start_line: usize, end_line: usize, col: usize) {
let min_l = start_line.min(end_line);
let max_l = start_line.max(end_line);
self.selections.clear();
for l in min_l..=max_l {
self.selections
.push(Selection::point(Position::new(l, col)));
}
self.primary_idx = 0;
self.normalize();
}
pub fn normalize(&mut self) {
if self.selections.len() <= 1 {
return;
}
self.selections.sort_by_key(|s| s.ordered_range().0);
let mut merged: Vec<Selection> = Vec::with_capacity(self.selections.len());
for sel in self.selections.drain(..) {
if let Some(last) = merged.last_mut() {
if last.touches_or_overlaps(&sel) {
*last = last.merge(&sel);
continue;
}
}
merged.push(sel);
}
self.selections = merged;
if self.primary_idx >= self.selections.len() {
self.primary_idx = self.selections.len().saturating_sub(1);
}
}
pub fn selections_descending(&self) -> Vec<Selection> {
let mut sorted = self.selections.clone();
sorted.sort_by_key(|a| std::cmp::Reverse(a.ordered_range().0));
sorted
}
}
pub fn shift_position_after_edit(
pos: Position,
edit_start: Position,
edit_end: Position,
inserted_lines: &[&str],
) -> Position {
if pos < edit_start {
return pos;
}
let delta_lines =
(inserted_lines.len() as isize - 1) - (edit_end.line as isize - edit_start.line as isize);
if pos <= edit_end {
if inserted_lines.len() == 1 {
Position::new(edit_start.line, edit_start.col + inserted_lines[0].len())
} else {
let last_line_len = inserted_lines.last().map_or(0, |l| l.len());
Position::new(edit_start.line + inserted_lines.len() - 1, last_line_len)
}
} else if pos.line == edit_end.line {
let col_offset = pos.col - edit_end.col;
if inserted_lines.len() == 1 {
Position::new(
edit_start.line,
edit_start.col + inserted_lines[0].len() + col_offset,
)
} else {
let last_line_len = inserted_lines.last().map_or(0, |l| l.len());
let target_line = (pos.line as isize + delta_lines).max(0) as usize;
Position::new(target_line, last_line_len + col_offset)
}
} else {
let target_line = (pos.line as isize + delta_lines).max(0) as usize;
Position::new(target_line, pos.col)
}
}