Attempt to highlight inlays

This commit is contained in:
Kirill Bulatov 2023-08-22 01:02:25 +03:00
parent 6c5761d05b
commit f8874a726c
4 changed files with 143 additions and 72 deletions

View File

@ -2,7 +2,7 @@ use crate::{
multi_buffer::{MultiBufferChunks, MultiBufferRows}, multi_buffer::{MultiBufferChunks, MultiBufferRows},
Anchor, InlayId, MultiBufferSnapshot, ToOffset, Anchor, InlayId, MultiBufferSnapshot, ToOffset,
}; };
use collections::{BTreeMap, BTreeSet}; use collections::{BTreeMap, BTreeSet, HashSet};
use gpui::fonts::HighlightStyle; use gpui::fonts::HighlightStyle;
use language::{Chunk, Edit, Point, TextSummary}; use language::{Chunk, Edit, Point, TextSummary};
use std::{ use std::{
@ -183,7 +183,7 @@ pub struct InlayBufferRows<'a> {
max_buffer_row: u32, max_buffer_row: u32,
} }
#[derive(Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
struct HighlightEndpoint { struct HighlightEndpoint {
offset: InlayOffset, offset: InlayOffset,
is_start: bool, is_start: bool,
@ -243,6 +243,7 @@ impl<'a> Iterator for InlayChunks<'a> {
return None; return None;
} }
// TODO kb highlights are not displayed still
let mut next_highlight_endpoint = InlayOffset(usize::MAX); let mut next_highlight_endpoint = InlayOffset(usize::MAX);
while let Some(endpoint) = self.highlight_endpoints.peek().copied() { while let Some(endpoint) = self.highlight_endpoints.peek().copied() {
if endpoint.offset <= self.output_offset { if endpoint.offset <= self.output_offset {
@ -980,62 +981,89 @@ impl InlaySnapshot {
let mut cursor = self.transforms.cursor::<(InlayOffset, usize)>(); let mut cursor = self.transforms.cursor::<(InlayOffset, usize)>();
cursor.seek(&range.start, Bias::Right, &()); cursor.seek(&range.start, Bias::Right, &());
let empty_text_highlights = TextHighlights::default();
let text_highlights = text_highlights.unwrap_or_else(|| &empty_text_highlights);
let empty_inlay_highlights = InlayHighlights::default();
let inlay_highlights = inlay_highlights.unwrap_or_else(|| &empty_inlay_highlights);
let mut highlight_endpoints = Vec::new(); let mut highlight_endpoints = Vec::new();
if let Some(text_highlights) = text_highlights { if !text_highlights.is_empty() || !inlay_highlights.is_empty() {
if !text_highlights.is_empty() { while cursor.start().0 < range.end {
while cursor.start().0 < range.end { let transform_start = self
let transform_start = self.buffer.anchor_after( .buffer
self.to_buffer_offset(cmp::max(range.start, cursor.start().0)), .anchor_after(self.to_buffer_offset(cmp::max(range.start, cursor.start().0)));
);
let transform_end = { let transform_end = {
let overshoot = InlayOffset(range.end.0 - cursor.start().0 .0); let overshoot = InlayOffset(range.end.0 - cursor.start().0 .0);
self.buffer.anchor_before(self.to_buffer_offset(cmp::min( self.buffer.anchor_before(self.to_buffer_offset(cmp::min(
cursor.end(&()).0, cursor.end(&()).0,
cursor.start().0 + overshoot, cursor.start().0 + overshoot,
))) )))
}; };
for (tag, highlights) in text_highlights.iter() { let mut covered_tags = HashSet::default();
let style = highlights.0; for (tag, text_highlights) in text_highlights.iter() {
let ranges = &highlights.1; covered_tags.insert(*tag);
let style = text_highlights.0;
let ranges = &text_highlights.1;
let start_ix = match ranges.binary_search_by(|probe| { let start_ix = match ranges.binary_search_by(|probe| {
let cmp = probe.end.cmp(&transform_start, &self.buffer); let cmp = probe.end.cmp(&transform_start, &self.buffer);
if cmp.is_gt() { if cmp.is_gt() {
cmp::Ordering::Greater cmp::Ordering::Greater
} else { } else {
cmp::Ordering::Less cmp::Ordering::Less
}
}) {
Ok(i) | Err(i) => i,
};
// TODO kb add a way to highlight inlay hints through here.
for range in &ranges[start_ix..] {
if range.start.cmp(&transform_end, &self.buffer).is_ge() {
break;
}
highlight_endpoints.push(HighlightEndpoint {
offset: self.to_inlay_offset(range.start.to_offset(&self.buffer)),
is_start: true,
tag: *tag,
style,
});
highlight_endpoints.push(HighlightEndpoint {
offset: self.to_inlay_offset(range.end.to_offset(&self.buffer)),
is_start: false,
tag: *tag,
style,
});
} }
}) {
Ok(i) | Err(i) => i,
};
for range in &ranges[start_ix..] {
if range.start.cmp(&transform_end, &self.buffer).is_ge() {
break;
}
highlight_endpoints.push(HighlightEndpoint {
offset: self.to_inlay_offset(range.start.to_offset(&self.buffer)),
is_start: true,
tag: *tag,
style,
});
highlight_endpoints.push(HighlightEndpoint {
offset: self.to_inlay_offset(range.end.to_offset(&self.buffer)),
is_start: false,
tag: *tag,
style,
});
} }
cursor.next(&()); if let Some(inlay_highlights) = inlay_highlights.get(tag) {
self.push_inlay_highlight_range(
inlay_highlights,
transform_start,
transform_end,
&mut highlight_endpoints,
tag,
);
}
} }
highlight_endpoints.sort();
cursor.seek(&range.start, Bias::Right, &()); for (tag, inlay_highlights) in inlay_highlights
.iter()
.filter(|(tag, _)| !covered_tags.contains(tag))
{
self.push_inlay_highlight_range(
inlay_highlights,
transform_start,
transform_end,
&mut highlight_endpoints,
tag,
);
}
cursor.next(&());
} }
highlight_endpoints.sort();
cursor.seek(&range.start, Bias::Right, &());
} }
let buffer_range = self.to_buffer_offset(range.start)..self.to_buffer_offset(range.end); let buffer_range = self.to_buffer_offset(range.start)..self.to_buffer_offset(range.end);
@ -1056,6 +1084,48 @@ impl InlaySnapshot {
} }
} }
fn push_inlay_highlight_range(
&self,
inlay_highlights: &std::sync::Arc<(
HighlightStyle,
Vec<crate::link_go_to_definition::InlayCoordinates>,
)>,
transform_start: Anchor,
transform_end: Anchor,
highlight_endpoints: &mut Vec<HighlightEndpoint>,
tag: &Option<TypeId>,
) {
let style = inlay_highlights.0;
let ranges = &inlay_highlights.1;
let start_ix = match ranges
.binary_search_by(|probe| probe.inlay_position.cmp(&transform_start, &self.buffer))
{
Ok(i) | Err(i) => i,
};
for range in &ranges[start_ix..] {
if range
.inlay_position
.cmp(&transform_end, &self.buffer)
.is_ge()
{
break;
}
highlight_endpoints.push(HighlightEndpoint {
offset: range.highlight_start,
is_start: true,
tag: *tag,
style,
});
highlight_endpoints.push(HighlightEndpoint {
offset: range.highlight_end,
is_start: false,
tag: *tag,
style,
});
}
}
#[cfg(test)] #[cfg(test)]
pub fn text(&self) -> String { pub fn text(&self) -> String {
self.chunks( self.chunks(

View File

@ -7750,17 +7750,16 @@ impl Editor {
highlights highlights
} }
pub fn clear_highlights<T: 'static>( pub fn clear_highlights<T: 'static>(&mut self, cx: &mut ViewContext<Self>) {
&mut self, let text_highlights = self
cx: &mut ViewContext<Self>,
) -> Option<Arc<(HighlightStyle, Vec<Range<Anchor>>)>> {
let highlights = self
.display_map .display_map
.update(cx, |map, _| map.clear_text_highlights(TypeId::of::<T>())); .update(cx, |map, _| map.clear_text_highlights(TypeId::of::<T>()));
if highlights.is_some() { let inlay_highlights = self
.display_map
.update(cx, |map, _| map.clear_inlay_highlights(TypeId::of::<T>()));
if text_highlights.is_some() || inlay_highlights.is_some() {
cx.notify(); cx.notify();
} }
highlights
} }
pub fn show_local_cursors(&self, cx: &AppContext) -> bool { pub fn show_local_cursors(&self, cx: &AppContext) -> bool {

View File

@ -1908,11 +1908,13 @@ fn update_inlay_link_and_hover_points(
} }
} }
project::InlayHintLabel::LabelParts(label_parts) => { project::InlayHintLabel::LabelParts(label_parts) => {
if let Some(hovered_hint_part) = find_hovered_hint_part( if let Some((hovered_hint_part, part_range)) =
label_parts, find_hovered_hint_part(
hint_start_offset..hint_end_offset, label_parts,
hovered_offset, hint_start_offset..hint_end_offset,
) { hovered_offset,
)
{
if hovered_hint_part.tooltip.is_some() { if hovered_hint_part.tooltip.is_some() {
dbg!(&hovered_hint_part.tooltip); // TODO kb dbg!(&hovered_hint_part.tooltip); // TODO kb
// hover_at_point = Some(hovered_offset); // hover_at_point = Some(hovered_offset);
@ -1928,10 +1930,9 @@ fn update_inlay_link_and_hover_points(
editor, editor,
GoToDefinitionTrigger::InlayHint( GoToDefinitionTrigger::InlayHint(
InlayCoordinates { InlayCoordinates {
inlay_id: hovered_hint.id,
inlay_position: hovered_hint.position, inlay_position: hovered_hint.position,
inlay_start: hint_start_offset, highlight_start: part_range.start,
highlight_end: hovered_offset, highlight_end: part_range.end,
}, },
LocationLink { LocationLink {
origin: Some(Location { origin: Some(Location {
@ -1976,15 +1977,17 @@ fn find_hovered_hint_part(
label_parts: Vec<InlayHintLabelPart>, label_parts: Vec<InlayHintLabelPart>,
hint_range: Range<InlayOffset>, hint_range: Range<InlayOffset>,
hovered_offset: InlayOffset, hovered_offset: InlayOffset,
) -> Option<InlayHintLabelPart> { ) -> Option<(InlayHintLabelPart, Range<InlayOffset>)> {
if hovered_offset >= hint_range.start && hovered_offset <= hint_range.end { if hovered_offset >= hint_range.start && hovered_offset <= hint_range.end {
let mut hovered_character = (hovered_offset - hint_range.start).0; let mut hovered_character = (hovered_offset - hint_range.start).0;
let mut part_start = hint_range.start;
for part in label_parts { for part in label_parts {
let part_len = part.value.chars().count(); let part_len = part.value.chars().count();
if hovered_character >= part_len { if hovered_character >= part_len {
hovered_character -= part_len; hovered_character -= part_len;
part_start.0 += part_len;
} else { } else {
return Some(part); return Some((part, part_start..InlayOffset(part_start.0 + part_len)));
} }
} }
} }

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
display_map::InlayOffset, element::PointForPosition, Anchor, DisplayPoint, Editor, display_map::InlayOffset, element::PointForPosition, Anchor, DisplayPoint, Editor,
EditorSnapshot, InlayId, SelectPhase, EditorSnapshot, SelectPhase,
}; };
use gpui::{Task, ViewContext}; use gpui::{Task, ViewContext};
use language::{Bias, ToOffset}; use language::{Bias, ToOffset};
@ -25,9 +25,8 @@ pub enum GoToDefinitionTrigger {
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct InlayCoordinates { pub struct InlayCoordinates {
pub inlay_id: InlayId,
pub inlay_position: Anchor, pub inlay_position: Anchor,
pub inlay_start: InlayOffset, pub highlight_start: InlayOffset,
pub highlight_end: InlayOffset, pub highlight_end: InlayOffset,
} }
@ -51,7 +50,7 @@ impl SymbolRange {
point_after_start && range.end.cmp(point, &snapshot.buffer_snapshot).is_ge() point_after_start && range.end.cmp(point, &snapshot.buffer_snapshot).is_ge()
} }
(SymbolRange::Inlay(range), TriggerPoint::InlayHint(point, _)) => { (SymbolRange::Inlay(range), TriggerPoint::InlayHint(point, _)) => {
range.inlay_start.cmp(&point.highlight_end).is_le() range.highlight_start.cmp(&point.highlight_end).is_le()
&& range.highlight_end.cmp(&point.highlight_end).is_ge() && range.highlight_end.cmp(&point.highlight_end).is_ge()
} }
(SymbolRange::Inlay(_), TriggerPoint::Text(_)) (SymbolRange::Inlay(_), TriggerPoint::Text(_))
@ -282,8 +281,8 @@ pub fn show_link_definition(
..snapshot.anchor_after(offset_range.end), ..snapshot.anchor_after(offset_range.end),
) )
} }
TriggerPoint::InlayHint(inlay_trigger, _) => { TriggerPoint::InlayHint(inlay_coordinates, _) => {
SymbolRange::Inlay(inlay_trigger) SymbolRange::Inlay(inlay_coordinates)
} }
}); });