mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-03 00:39:51 +00:00
Implement MultiBuffer::set_active_selections
This commit is contained in:
parent
920daa8a8f
commit
1b67f19edc
1 changed files with 66 additions and 14 deletions
|
@ -12,7 +12,7 @@ use language::{
|
||||||
use std::{
|
use std::{
|
||||||
cell::{Ref, RefCell},
|
cell::{Ref, RefCell},
|
||||||
cmp, io,
|
cmp, io,
|
||||||
iter::{FromIterator, Peekable},
|
iter::{self, FromIterator, Peekable},
|
||||||
ops::{Range, Sub},
|
ops::{Range, Sub},
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
time::{Duration, Instant, SystemTime},
|
time::{Duration, Instant, SystemTime},
|
||||||
|
@ -346,20 +346,72 @@ impl MultiBuffer {
|
||||||
selections: &[Selection<Anchor>],
|
selections: &[Selection<Anchor>],
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) {
|
) {
|
||||||
// TODO
|
let mut selections_by_buffer: HashMap<usize, Vec<Selection<text::Anchor>>> =
|
||||||
let this = self.read(cx);
|
Default::default();
|
||||||
self.as_singleton().unwrap().update(cx, |buffer, cx| {
|
let snapshot = self.read(cx);
|
||||||
let buffer_snapshot = buffer.snapshot();
|
let mut cursor = snapshot.excerpts.cursor::<Option<&ExcerptId>>();
|
||||||
let selections = selections.iter().map(|selection| Selection {
|
for selection in selections {
|
||||||
|
cursor.seek(&Some(&selection.start.excerpt_id), Bias::Left, &());
|
||||||
|
while let Some(excerpt) = cursor.item() {
|
||||||
|
if excerpt.id > selection.end.excerpt_id {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut start = excerpt.range.start.clone();
|
||||||
|
let mut end = excerpt.range.end.clone();
|
||||||
|
if excerpt.id == selection.start.excerpt_id {
|
||||||
|
start = selection.start.text_anchor.clone();
|
||||||
|
}
|
||||||
|
if excerpt.id == selection.end.excerpt_id {
|
||||||
|
end = selection.end.text_anchor.clone();
|
||||||
|
}
|
||||||
|
selections_by_buffer
|
||||||
|
.entry(excerpt.buffer_id)
|
||||||
|
.or_default()
|
||||||
|
.push(Selection {
|
||||||
id: selection.id,
|
id: selection.id,
|
||||||
start: buffer_snapshot.anchor_before(selection.start.to_offset(&this)),
|
start,
|
||||||
end: buffer_snapshot.anchor_before(selection.end.to_offset(&this)),
|
end,
|
||||||
reversed: selection.reversed,
|
reversed: selection.reversed,
|
||||||
goal: selection.goal,
|
goal: selection.goal,
|
||||||
});
|
});
|
||||||
buffer.set_active_selections(Arc::from_iter(selections), cx);
|
|
||||||
|
cursor.next(&());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (buffer_id, mut selections) in selections_by_buffer {
|
||||||
|
self.buffers[&buffer_id].buffer.update(cx, |buffer, cx| {
|
||||||
|
selections.sort_unstable_by(|a, b| a.start.cmp(&b.start, buffer).unwrap());
|
||||||
|
let mut selections = selections.into_iter().peekable();
|
||||||
|
let merged_selections = Arc::from_iter(iter::from_fn(|| {
|
||||||
|
let mut selection = selections.next()?;
|
||||||
|
while let Some(next_selection) = selections.peek() {
|
||||||
|
if selection
|
||||||
|
.end
|
||||||
|
.cmp(&next_selection.start, buffer)
|
||||||
|
.unwrap()
|
||||||
|
.is_ge()
|
||||||
|
{
|
||||||
|
let next_selection = selections.next().unwrap();
|
||||||
|
if next_selection
|
||||||
|
.end
|
||||||
|
.cmp(&selection.end, buffer)
|
||||||
|
.unwrap()
|
||||||
|
.is_ge()
|
||||||
|
{
|
||||||
|
selection.end = next_selection.end;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(selection)
|
||||||
|
}));
|
||||||
|
buffer.set_active_selections(merged_selections, cx);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn remove_active_selections(&mut self, cx: &mut ModelContext<Self>) {
|
pub fn remove_active_selections(&mut self, cx: &mut ModelContext<Self>) {
|
||||||
for buffer in self.buffers.values() {
|
for buffer in self.buffers.values() {
|
||||||
|
@ -976,7 +1028,7 @@ impl MultiBufferSnapshot {
|
||||||
let mut summaries = Vec::new();
|
let mut summaries = Vec::new();
|
||||||
while let Some(anchor) = anchors.peek() {
|
while let Some(anchor) = anchors.peek() {
|
||||||
let excerpt_id = &anchor.excerpt_id;
|
let excerpt_id = &anchor.excerpt_id;
|
||||||
let excerpt_anchors = std::iter::from_fn(|| {
|
let excerpt_anchors = iter::from_fn(|| {
|
||||||
let anchor = anchors.peek()?;
|
let anchor = anchors.peek()?;
|
||||||
if anchor.excerpt_id == *excerpt_id {
|
if anchor.excerpt_id == *excerpt_id {
|
||||||
Some(&anchors.next().unwrap().text_anchor)
|
Some(&anchors.next().unwrap().text_anchor)
|
||||||
|
|
Loading…
Reference in a new issue