Implement MultiBufferSnapshot::remote_selections_in_range

This commit is contained in:
Antonio Scandurra 2021-12-14 11:34:26 +01:00
parent 174b37cdf0
commit 163ce95171
2 changed files with 53 additions and 33 deletions

View file

@ -3081,16 +3081,14 @@ impl Editor {
.collect(),
);
for (replica_id, selections) in display_map
for (replica_id, selection) in display_map
.buffer_snapshot
.remote_selections_in_range(start..end)
.remote_selections_in_range(&(start..end))
{
result.insert(
replica_id,
selections
.map(|s| display_selection(&s, &display_map))
.collect(),
);
result
.entry(replica_id)
.or_insert(Vec::new())
.push(display_selection(&selection, &display_map));
}
result

View file

@ -1110,32 +1110,54 @@ impl MultiBufferSnapshot {
pub fn remote_selections_in_range<'a>(
&'a self,
range: Range<Anchor>,
) -> impl 'a + Iterator<Item = (ReplicaId, impl 'a + Iterator<Item = Selection<Anchor>>)> {
// TODO
let excerpt_id = self.excerpts.first().unwrap().id.clone();
self.as_singleton()
.unwrap()
.remote_selections_in_range(range.start.text_anchor..range.end.text_anchor)
.map(move |(replica_id, selections)| {
let excerpt_id = excerpt_id.clone();
range: &'a Range<Anchor>,
) -> impl 'a + Iterator<Item = (ReplicaId, Selection<Anchor>)> {
let mut cursor = self.excerpts.cursor::<Option<&ExcerptId>>();
cursor.seek(&Some(&range.start.excerpt_id), Bias::Left, &());
cursor
.take_while(move |excerpt| excerpt.id <= range.end.excerpt_id)
.flat_map(move |excerpt| {
let mut query_range = excerpt.range.start.clone()..excerpt.range.end.clone();
if excerpt.id == range.start.excerpt_id {
query_range.start = range.start.text_anchor.clone();
}
if excerpt.id == range.end.excerpt_id {
query_range.end = range.end.text_anchor.clone();
}
excerpt
.buffer
.remote_selections_in_range(query_range)
.flat_map(move |(replica_id, selections)| {
selections.map(move |selection| {
let mut start = Anchor {
excerpt_id: excerpt.id.clone(),
text_anchor: selection.start.clone(),
};
let mut end = Anchor {
excerpt_id: excerpt.id.clone(),
text_anchor: selection.end.clone(),
};
if range.start.cmp(&start, self).unwrap().is_gt() {
start = range.start.clone();
}
if range.end.cmp(&end, self).unwrap().is_lt() {
end = range.end.clone();
}
(
replica_id,
selections.map(move |s| Selection {
id: s.id,
start: Anchor {
excerpt_id: excerpt_id.clone(),
text_anchor: s.start.clone(),
Selection {
id: selection.id,
start,
end,
reversed: selection.reversed,
goal: selection.goal,
},
end: Anchor {
excerpt_id: excerpt_id.clone(),
text_anchor: s.end.clone(),
},
reversed: s.reversed,
goal: s.goal,
}),
)
})
})
})
}
}