More messy progress towards selections in editors

This commit is contained in:
Nathan Sobo 2021-12-11 00:29:34 -07:00
parent 0639c8331c
commit 4dd0752e80
11 changed files with 298 additions and 479 deletions

View file

@ -1,85 +1,6 @@
use super::{Anchor, MultiBufferSnapshot, ToOffset};
use std::{
ops::{Range, Sub},
sync::Arc,
};
use sum_tree::Bias;
use text::{rope::TextDimension, Selection, SelectionSetId};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SelectionSet {
pub id: SelectionSetId,
pub active: bool,
pub selections: Arc<[Selection<Anchor>]>,
}
impl SelectionSet {
pub fn len(&self) -> usize {
self.selections.len()
}
pub fn selections<'a, D>(
&'a self,
snapshot: &'a MultiBufferSnapshot,
) -> impl 'a + Iterator<Item = Selection<D>>
where
D: TextDimension + Ord + Sub<D, Output = D>,
{
resolve_selections(&self.selections, snapshot)
}
pub fn intersecting_selections<'a, D, I>(
&'a self,
range: Range<(I, Bias)>,
snapshot: &'a MultiBufferSnapshot,
) -> impl 'a + Iterator<Item = Selection<D>>
where
D: TextDimension + Ord + Sub<D, Output = D>,
I: 'a + ToOffset,
{
let start = snapshot.anchor_at(range.start.0, range.start.1);
let end = snapshot.anchor_at(range.end.0, range.end.1);
let start_ix = match self
.selections
.binary_search_by(|probe| probe.end.cmp(&start, snapshot).unwrap())
{
Ok(ix) | Err(ix) => ix,
};
let end_ix = match self
.selections
.binary_search_by(|probe| probe.start.cmp(&end, snapshot).unwrap())
{
Ok(ix) | Err(ix) => ix,
};
resolve_selections(&self.selections[start_ix..end_ix], snapshot)
}
pub fn oldest_selection<'a, D>(
&'a self,
snapshot: &'a MultiBufferSnapshot,
) -> Option<Selection<D>>
where
D: TextDimension + Ord + Sub<D, Output = D>,
{
self.selections
.iter()
.min_by_key(|selection| selection.id)
.map(|selection| resolve_selection(selection, snapshot))
}
pub fn newest_selection<'a, D>(
&'a self,
snapshot: &'a MultiBufferSnapshot,
) -> Option<Selection<D>>
where
D: TextDimension + Ord + Sub<D, Output = D>,
{
self.selections
.iter()
.max_by_key(|selection| selection.id)
.map(|selection| resolve_selection(selection, snapshot))
}
}
use super::{Anchor, MultiBufferSnapshot};
use std::ops::Sub;
use text::{rope::TextDimension, Selection};
fn resolve_selection<'a, D>(
selection: &'a Selection<Anchor>,