Make rename highlights work across multibuffer excerpts

Co-authored-by: Antonio Scandurra <me@as-cii.com>
Co-authored-by: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Keith Simmons
2022-03-24 10:22:47 -07:00
co-authored by Antonio Scandurra Nathan Sobo
parent ff4bdb3114
commit f6805eb802
9 changed files with 110 additions and 45 deletions
+31 -16
View File
@@ -2399,7 +2399,7 @@ impl Editor {
) -> Result<()> {
let replica_id = this.read_with(&cx, |this, cx| this.replica_id(cx));
// If the code action's edits are all contained within this editor, then
// If the project transaction's edits are all contained within this editor, then
// avoid opening a new editor to display them.
let mut entries = transaction.0.iter();
if let Some((buffer, transaction)) = entries.next() {
@@ -2521,7 +2521,6 @@ impl Editor {
}
let buffer_id = cursor_position.buffer_id;
let excerpt_id = cursor_position.excerpt_id.clone();
let style = this.style(cx);
let read_background = style.document_highlight_read_background;
let write_background = style.document_highlight_write_background;
@@ -2533,22 +2532,39 @@ impl Editor {
return;
}
let cursor_buffer_snapshot = cursor_buffer.read(cx);
let mut write_ranges = Vec::new();
let mut read_ranges = Vec::new();
for highlight in highlights {
let range = Anchor {
buffer_id,
excerpt_id: excerpt_id.clone(),
text_anchor: highlight.range.start,
}..Anchor {
buffer_id,
excerpt_id: excerpt_id.clone(),
text_anchor: highlight.range.end,
};
if highlight.kind == lsp::DocumentHighlightKind::WRITE {
write_ranges.push(range);
} else {
read_ranges.push(range);
for (excerpt_id, excerpt_range) in
buffer.excerpts_for_buffer(&cursor_buffer, cx)
{
let start = highlight
.range
.start
.max(&excerpt_range.start, cursor_buffer_snapshot);
let end = highlight
.range
.end
.min(&excerpt_range.end, cursor_buffer_snapshot);
if start.cmp(&end, cursor_buffer_snapshot).unwrap().is_ge() {
continue;
}
let range = Anchor {
buffer_id,
excerpt_id: excerpt_id.clone(),
text_anchor: start,
}..Anchor {
buffer_id,
excerpt_id,
text_anchor: end,
};
if highlight.kind == lsp::DocumentHighlightKind::WRITE {
write_ranges.push(range);
} else {
read_ranges.push(range);
}
}
}
@@ -4413,7 +4429,6 @@ impl Editor {
.iter()
.map(|range| range.to_point(&snapshot))
.collect::<Vec<_>>();
dbg!(point_ranges);
this.highlight_text::<Rename>(
ranges,
+30 -6
View File
@@ -211,7 +211,11 @@ impl MultiBuffer {
pub fn singleton(buffer: ModelHandle<Buffer>, cx: &mut ModelContext<Self>) -> Self {
let mut this = Self::new(buffer.read(cx).replica_id());
this.singleton = true;
this.push_excerpts(buffer, [text::Anchor::min()..text::Anchor::max()], cx);
this.push_excerpts(
buffer,
[text::Anchor::build_min()..text::Anchor::build_max()],
cx,
);
this.snapshot.borrow_mut().singleton = true;
this
}
@@ -814,11 +818,30 @@ impl MultiBuffer {
cx.notify();
}
pub fn excerpt_ids_for_buffer(&self, buffer: &ModelHandle<Buffer>) -> Vec<ExcerptId> {
self.buffers
.borrow()
pub fn excerpts_for_buffer(
&self,
buffer: &ModelHandle<Buffer>,
cx: &AppContext,
) -> Vec<(ExcerptId, Range<text::Anchor>)> {
let mut excerpts = Vec::new();
let snapshot = self.read(cx);
let buffers = self.buffers.borrow();
let mut cursor = snapshot.excerpts.cursor::<Option<&ExcerptId>>();
for excerpt_id in buffers
.get(&buffer.id())
.map_or(Vec::new(), |state| state.excerpts.clone())
.map(|state| &state.excerpts)
.into_iter()
.flatten()
{
cursor.seek_forward(&Some(excerpt_id), Bias::Left, &());
if let Some(excerpt) = cursor.item() {
if excerpt.id == *excerpt_id {
excerpts.push((excerpt.id.clone(), excerpt.range.clone()));
}
}
}
excerpts
}
pub fn excerpt_ids(&self) -> Vec<ExcerptId> {
@@ -3070,7 +3093,8 @@ mod tests {
);
let snapshot = multibuffer.update(cx, |multibuffer, cx| {
let buffer_2_excerpt_id = multibuffer.excerpt_ids_for_buffer(&buffer_2)[0].clone();
let (buffer_2_excerpt_id, _) =
multibuffer.excerpts_for_buffer(&buffer_2, cx)[0].clone();
multibuffer.remove_excerpts(&[buffer_2_excerpt_id], cx);
multibuffer.snapshot(cx)
});
+2 -2
View File
@@ -19,7 +19,7 @@ impl Anchor {
Self {
buffer_id: None,
excerpt_id: ExcerptId::min(),
text_anchor: text::Anchor::min(),
text_anchor: text::Anchor::build_min(),
}
}
@@ -27,7 +27,7 @@ impl Anchor {
Self {
buffer_id: None,
excerpt_id: ExcerptId::max(),
text_anchor: text::Anchor::max(),
text_anchor: text::Anchor::build_max(),
}
}
+9
View File
@@ -0,0 +1,9 @@
fn test(complicated: i32, test: i32) {
complicated;
test;
// 1
// 2
// 3
complicated;
test;
}