Prepare background highlights for inlay highlights

This commit is contained in:
Kirill Bulatov 2023-08-23 13:24:17 +03:00
parent 12ffbe54fb
commit 4b78678923
4 changed files with 96 additions and 86 deletions

View file

@ -416,8 +416,18 @@ impl DisplaySnapshot {
.to_offset(self.display_point_to_inlay_point(point, bias)) .to_offset(self.display_point_to_inlay_point(point, bias))
} }
pub fn inlay_point_to_inlay_offset(&self, point: InlayPoint) -> InlayOffset { pub fn anchor_to_inlay_offset(&self, anchor: Anchor) -> InlayOffset {
self.inlay_snapshot.to_offset(point) self.inlay_snapshot
.to_inlay_offset(anchor.to_offset(&self.buffer_snapshot))
}
pub fn inlay_offset_to_display_point(&self, offset: InlayOffset, bias: Bias) -> DisplayPoint {
let inlay_point = self.inlay_snapshot.to_point(offset);
let fold_point = self.fold_snapshot.to_fold_point(inlay_point, bias);
let tab_point = self.tab_snapshot.to_tab_point(fold_point);
let wrap_point = self.wrap_snapshot.tab_point_to_wrap_point(tab_point);
let block_point = self.block_snapshot.to_block_point(wrap_point);
DisplayPoint(block_point)
} }
fn display_point_to_inlay_point(&self, point: DisplayPoint, bias: Bias) -> InlayPoint { fn display_point_to_inlay_point(&self, point: DisplayPoint, bias: Bias) -> InlayPoint {

View file

@ -392,6 +392,10 @@ impl InlayPoint {
pub fn row(self) -> u32 { pub fn row(self) -> u32 {
self.0.row self.0.row
} }
pub fn column(self) -> u32 {
self.0.column
}
} }
impl InlayMap { impl InlayMap {

View file

@ -535,6 +535,8 @@ type CompletionId = usize;
type GetFieldEditorTheme = dyn Fn(&theme::Theme) -> theme::FieldEditor; type GetFieldEditorTheme = dyn Fn(&theme::Theme) -> theme::FieldEditor;
type OverrideTextStyle = dyn Fn(&EditorStyle) -> Option<HighlightStyle>; type OverrideTextStyle = dyn Fn(&EditorStyle) -> Option<HighlightStyle>;
type BackgroundHighlight = (fn(&Theme) -> Color, Vec<DocumentRange>);
pub struct Editor { pub struct Editor {
handle: WeakViewHandle<Self>, handle: WeakViewHandle<Self>,
buffer: ModelHandle<MultiBuffer>, buffer: ModelHandle<MultiBuffer>,
@ -564,8 +566,7 @@ pub struct Editor {
show_wrap_guides: Option<bool>, show_wrap_guides: Option<bool>,
placeholder_text: Option<Arc<str>>, placeholder_text: Option<Arc<str>>,
highlighted_rows: Option<Range<u32>>, highlighted_rows: Option<Range<u32>>,
#[allow(clippy::type_complexity)] background_highlights: BTreeMap<TypeId, BackgroundHighlight>,
background_highlights: BTreeMap<TypeId, (fn(&Theme) -> Color, Vec<Range<Anchor>>)>,
nav_history: Option<ItemNavHistory>, nav_history: Option<ItemNavHistory>,
context_menu: Option<ContextMenu>, context_menu: Option<ContextMenu>,
mouse_context_menu: ViewHandle<context_menu::ContextMenu>, mouse_context_menu: ViewHandle<context_menu::ContextMenu>,
@ -6758,10 +6759,18 @@ impl Editor {
let rename_range = if let Some(range) = prepare_rename.await? { let rename_range = if let Some(range) = prepare_rename.await? {
Some(range) Some(range)
} else { } else {
this.read_with(&cx, |this, cx| { this.update(&mut cx, |this, cx| {
let buffer = this.buffer.read(cx).snapshot(cx); let buffer = this.buffer.read(cx).snapshot(cx);
let display_snapshot = this
.display_map
.update(cx, |display_map, cx| display_map.snapshot(cx));
let mut buffer_highlights = this let mut buffer_highlights = this
.document_highlights_for_position(selection.head(), &buffer) .document_highlights_for_position(
selection.head(),
&buffer,
&display_snapshot,
)
.filter_map(|highlight| highlight.as_text_range())
.filter(|highlight| { .filter(|highlight| {
highlight.start.excerpt_id() == selection.head().excerpt_id() highlight.start.excerpt_id() == selection.head().excerpt_id()
&& highlight.end.excerpt_id() == selection.head().excerpt_id() && highlight.end.excerpt_id() == selection.head().excerpt_id()
@ -6816,11 +6825,15 @@ impl Editor {
let ranges = this let ranges = this
.clear_background_highlights::<DocumentHighlightWrite>(cx) .clear_background_highlights::<DocumentHighlightWrite>(cx)
.into_iter() .into_iter()
.flat_map(|(_, ranges)| ranges) .flat_map(|(_, ranges)| {
ranges.into_iter().filter_map(|range| range.as_text_range())
})
.chain( .chain(
this.clear_background_highlights::<DocumentHighlightRead>(cx) this.clear_background_highlights::<DocumentHighlightRead>(cx)
.into_iter() .into_iter()
.flat_map(|(_, ranges)| ranges), .flat_map(|(_, ranges)| {
ranges.into_iter().filter_map(|range| range.as_text_range())
}),
) )
.collect(); .collect();
@ -7488,16 +7501,20 @@ impl Editor {
color_fetcher: fn(&Theme) -> Color, color_fetcher: fn(&Theme) -> Color,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
self.background_highlights self.background_highlights.insert(
.insert(TypeId::of::<T>(), (color_fetcher, ranges)); TypeId::of::<T>(),
(
color_fetcher,
ranges.into_iter().map(DocumentRange::Text).collect(),
),
);
cx.notify(); cx.notify();
} }
#[allow(clippy::type_complexity)]
pub fn clear_background_highlights<T: 'static>( pub fn clear_background_highlights<T: 'static>(
&mut self, &mut self,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Option<(fn(&Theme) -> Color, Vec<Range<Anchor>>)> { ) -> Option<BackgroundHighlight> {
let highlights = self.background_highlights.remove(&TypeId::of::<T>()); let highlights = self.background_highlights.remove(&TypeId::of::<T>());
if highlights.is_some() { if highlights.is_some() {
cx.notify(); cx.notify();
@ -7522,7 +7539,8 @@ impl Editor {
&'a self, &'a self,
position: Anchor, position: Anchor,
buffer: &'a MultiBufferSnapshot, buffer: &'a MultiBufferSnapshot,
) -> impl 'a + Iterator<Item = &Range<Anchor>> { display_snapshot: &'a DisplaySnapshot,
) -> impl 'a + Iterator<Item = &DocumentRange> {
let read_highlights = self let read_highlights = self
.background_highlights .background_highlights
.get(&TypeId::of::<DocumentHighlightRead>()) .get(&TypeId::of::<DocumentHighlightRead>())
@ -7531,14 +7549,16 @@ impl Editor {
.background_highlights .background_highlights
.get(&TypeId::of::<DocumentHighlightWrite>()) .get(&TypeId::of::<DocumentHighlightWrite>())
.map(|h| &h.1); .map(|h| &h.1);
let left_position = position.bias_left(buffer); let left_position = display_snapshot.anchor_to_inlay_offset(position.bias_left(buffer));
let right_position = position.bias_right(buffer); let right_position = display_snapshot.anchor_to_inlay_offset(position.bias_right(buffer));
read_highlights read_highlights
.into_iter() .into_iter()
.chain(write_highlights) .chain(write_highlights)
.flat_map(move |ranges| { .flat_map(move |ranges| {
let start_ix = match ranges.binary_search_by(|probe| { let start_ix = match ranges.binary_search_by(|probe| {
let cmp = probe.end.cmp(&left_position, buffer); let cmp = document_to_inlay_range(probe, display_snapshot)
.end
.cmp(&left_position);
if cmp.is_ge() { if cmp.is_ge() {
Ordering::Greater Ordering::Greater
} else { } else {
@ -7549,9 +7569,12 @@ impl Editor {
}; };
let right_position = right_position.clone(); let right_position = right_position.clone();
ranges[start_ix..] ranges[start_ix..].iter().take_while(move |range| {
.iter() document_to_inlay_range(range, &display_snapshot)
.take_while(move |range| range.start.cmp(&right_position, buffer).is_le()) .start
.cmp(&right_position)
.is_le()
})
}) })
} }
@ -7561,12 +7584,15 @@ impl Editor {
display_snapshot: &DisplaySnapshot, display_snapshot: &DisplaySnapshot,
theme: &Theme, theme: &Theme,
) -> Vec<(Range<DisplayPoint>, Color)> { ) -> Vec<(Range<DisplayPoint>, Color)> {
let search_range = display_snapshot.anchor_to_inlay_offset(search_range.start)
..display_snapshot.anchor_to_inlay_offset(search_range.end);
let mut results = Vec::new(); let mut results = Vec::new();
let buffer = &display_snapshot.buffer_snapshot;
for (color_fetcher, ranges) in self.background_highlights.values() { for (color_fetcher, ranges) in self.background_highlights.values() {
let color = color_fetcher(theme); let color = color_fetcher(theme);
let start_ix = match ranges.binary_search_by(|probe| { let start_ix = match ranges.binary_search_by(|probe| {
let cmp = probe.end.cmp(&search_range.start, buffer); let cmp = document_to_inlay_range(probe, display_snapshot)
.end
.cmp(&search_range.start);
if cmp.is_gt() { if cmp.is_gt() {
Ordering::Greater Ordering::Greater
} else { } else {
@ -7576,63 +7602,18 @@ impl Editor {
Ok(i) | Err(i) => i, Ok(i) | Err(i) => i,
}; };
for range in &ranges[start_ix..] { for range in &ranges[start_ix..] {
if range.start.cmp(&search_range.end, buffer).is_ge() { let range = document_to_inlay_range(range, display_snapshot);
if range.start.cmp(&search_range.end).is_ge() {
break; break;
} }
let start = range
.start let start = display_snapshot.inlay_offset_to_display_point(range.start, Bias::Left);
.to_point(buffer) let end = display_snapshot.inlay_offset_to_display_point(range.end, Bias::Right);
.to_display_point(display_snapshot);
let end = range
.end
.to_point(buffer)
.to_display_point(display_snapshot);
results.push((start..end, color)) results.push((start..end, color))
} }
} }
results results
} }
pub fn background_highlights_in_range_for<T: 'static>(
&self,
search_range: Range<Anchor>,
display_snapshot: &DisplaySnapshot,
theme: &Theme,
) -> Vec<(Range<DisplayPoint>, Color)> {
let mut results = Vec::new();
let buffer = &display_snapshot.buffer_snapshot;
let Some((color_fetcher, ranges)) = self.background_highlights
.get(&TypeId::of::<T>()) else {
return vec![];
};
let color = color_fetcher(theme);
let start_ix = match ranges.binary_search_by(|probe| {
let cmp = probe.end.cmp(&search_range.start, buffer);
if cmp.is_gt() {
Ordering::Greater
} else {
Ordering::Less
}
}) {
Ok(i) | Err(i) => i,
};
for range in &ranges[start_ix..] {
if range.start.cmp(&search_range.end, buffer).is_ge() {
break;
}
let start = range
.start
.to_point(buffer)
.to_display_point(display_snapshot);
let end = range
.end
.to_point(buffer)
.to_display_point(display_snapshot);
results.push((start..end, color))
}
results
}
pub fn background_highlight_row_ranges<T: 'static>( pub fn background_highlight_row_ranges<T: 'static>(
&self, &self,
@ -7640,15 +7621,18 @@ impl Editor {
display_snapshot: &DisplaySnapshot, display_snapshot: &DisplaySnapshot,
count: usize, count: usize,
) -> Vec<RangeInclusive<DisplayPoint>> { ) -> Vec<RangeInclusive<DisplayPoint>> {
let search_range = display_snapshot.anchor_to_inlay_offset(search_range.start)
..display_snapshot.anchor_to_inlay_offset(search_range.end);
let mut results = Vec::new(); let mut results = Vec::new();
let buffer = &display_snapshot.buffer_snapshot;
let Some((_, ranges)) = self.background_highlights let Some((_, ranges)) = self.background_highlights
.get(&TypeId::of::<T>()) else { .get(&TypeId::of::<T>()) else {
return vec![]; return vec![];
}; };
let start_ix = match ranges.binary_search_by(|probe| { let start_ix = match ranges.binary_search_by(|probe| {
let cmp = probe.end.cmp(&search_range.start, buffer); let cmp = document_to_inlay_range(probe, display_snapshot)
.end
.cmp(&search_range.start);
if cmp.is_gt() { if cmp.is_gt() {
Ordering::Greater Ordering::Greater
} else { } else {
@ -7657,30 +7641,28 @@ impl Editor {
}) { }) {
Ok(i) | Err(i) => i, Ok(i) | Err(i) => i,
}; };
let mut push_region = |start: Option<Point>, end: Option<Point>| { let mut push_region = |start: Option<DisplayPoint>, end: Option<DisplayPoint>| {
if let (Some(start_display), Some(end_display)) = (start, end) { if let (Some(start_display), Some(end_display)) = (start, end) {
results.push( results.push(start_display..=end_display);
start_display.to_display_point(display_snapshot)
..=end_display.to_display_point(display_snapshot),
);
} }
}; };
let mut start_row: Option<Point> = None; let mut start_row: Option<DisplayPoint> = None;
let mut end_row: Option<Point> = None; let mut end_row: Option<DisplayPoint> = None;
if ranges.len() > count { if ranges.len() > count {
return vec![]; return Vec::new();
} }
for range in &ranges[start_ix..] { for range in &ranges[start_ix..] {
if range.start.cmp(&search_range.end, buffer).is_ge() { let range = document_to_inlay_range(range, display_snapshot);
if range.start.cmp(&search_range.end).is_ge() {
break; break;
} }
let end = range.end.to_point(buffer); let end = display_snapshot.inlay_offset_to_display_point(range.end, Bias::Right);
if let Some(current_row) = &end_row { if let Some(current_row) = &end_row {
if end.row == current_row.row { if end.row() == current_row.row() {
continue; continue;
} }
} }
let start = range.start.to_point(buffer); let start = display_snapshot.inlay_offset_to_display_point(range.start, Bias::Left);
if start_row.is_none() { if start_row.is_none() {
assert_eq!(end_row, None); assert_eq!(end_row, None);
@ -7689,7 +7671,7 @@ impl Editor {
continue; continue;
} }
if let Some(current_end) = end_row.as_mut() { if let Some(current_end) = end_row.as_mut() {
if start.row > current_end.row + 1 { if start.row() > current_end.row() + 1 {
push_region(start_row, end_row); push_region(start_row, end_row);
start_row = Some(start); start_row = Some(start);
end_row = Some(end); end_row = Some(end);
@ -8133,6 +8115,19 @@ impl Editor {
} }
} }
fn document_to_inlay_range(
range: &DocumentRange,
snapshot: &DisplaySnapshot,
) -> Range<InlayOffset> {
match range {
DocumentRange::Text(text_range) => {
snapshot.anchor_to_inlay_offset(text_range.start)
..snapshot.anchor_to_inlay_offset(text_range.end)
}
DocumentRange::Inlay(inlay_range) => inlay_range.highlight_start..inlay_range.highlight_end,
}
}
fn inlay_hint_settings( fn inlay_hint_settings(
location: Anchor, location: Anchor,
snapshot: &MultiBufferSnapshot, snapshot: &MultiBufferSnapshot,

View file

@ -225,6 +225,7 @@ impl<'a> EditorTestContext<'a> {
.map(|h| h.1.clone()) .map(|h| h.1.clone())
.unwrap_or_default() .unwrap_or_default()
.into_iter() .into_iter()
.filter_map(|range| range.as_text_range())
.map(|range| range.to_offset(&snapshot.buffer_snapshot)) .map(|range| range.to_offset(&snapshot.buffer_snapshot))
.collect() .collect()
}); });