Refactor retrieving oldest and newest selection

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2021-11-18 15:04:12 +01:00
parent 0a6293bcda
commit 401b59be5c
4 changed files with 149 additions and 30 deletions

View file

@ -194,6 +194,66 @@ impl<T> AnchorRangeMap<T> {
.iter()
.map(|(range, value)| (range.start.0..range.end.0, value))
}
pub fn min_by_key<'a, C, D, F, K>(
&self,
content: C,
mut extract_key: F,
) -> Option<(Range<D>, &T)>
where
C: Into<Content<'a>>,
D: 'a + TextDimension<'a>,
F: FnMut(&T) -> K,
K: Ord,
{
let content = content.into();
self.entries
.iter()
.min_by_key(|(_, value)| extract_key(value))
.map(|(range, value)| (self.resolve_range(range, &content), value))
}
pub fn max_by_key<'a, C, D, F, K>(
&self,
content: C,
mut extract_key: F,
) -> Option<(Range<D>, &T)>
where
C: Into<Content<'a>>,
D: 'a + TextDimension<'a>,
F: FnMut(&T) -> K,
K: Ord,
{
let content = content.into();
self.entries
.iter()
.max_by_key(|(_, value)| extract_key(value))
.map(|(range, value)| (self.resolve_range(range, &content), value))
}
fn resolve_range<'a, D>(
&self,
range: &Range<(FullOffset, Bias)>,
content: &Content<'a>,
) -> Range<D>
where
D: 'a + TextDimension<'a>,
{
let (start, start_bias) = range.start;
let mut anchor = Anchor {
full_offset: start,
bias: start_bias,
version: self.version.clone(),
};
let start = content.summary_for_anchor(&anchor);
let (end, end_bias) = range.end;
anchor.full_offset = end;
anchor.bias = end_bias;
let end = content.summary_for_anchor(&anchor);
start..end
}
}
impl<T: PartialEq> PartialEq for AnchorRangeMap<T> {

View file

@ -116,4 +116,36 @@ impl SelectionSet {
goal: state.goal,
})
}
pub fn oldest_selection<'a, D, C>(&'a self, content: C) -> Option<Selection<D>>
where
D: 'a + TextDimension<'a>,
C: 'a + Into<Content<'a>>,
{
self.selections
.min_by_key(content, |selection| selection.id)
.map(|(range, state)| Selection {
id: state.id,
start: range.start,
end: range.end,
reversed: state.reversed,
goal: state.goal,
})
}
pub fn newest_selection<'a, D, C>(&'a self, content: C) -> Option<Selection<D>>
where
D: 'a + TextDimension<'a>,
C: 'a + Into<Content<'a>>,
{
self.selections
.max_by_key(content, |selection| selection.id)
.map(|(range, state)| Selection {
id: state.id,
start: range.start,
end: range.end,
reversed: state.reversed,
goal: state.goal,
})
}
}