More inlay hover fixes (#2898)

Better handle edge cases around cmd+hover around inlays:
* distinguish between same text anchors' trigger: inlay and text
triggers can have the same anchor, but are different
* forbid cmd+click on inlay that has no label part with location
selected
* properly omit throttled inlays that are outside of the visible range

Release Notes:

- N/A
This commit is contained in:
Kirill Bulatov
2023-08-27 19:57:31 +03:00
committed by GitHub
5 changed files with 162 additions and 149 deletions
+5 -3
View File
@@ -2792,11 +2792,13 @@ impl Editor {
} }
} }
InlayHintRefreshReason::ExcerptsRemoved(excerpts_removed) => { InlayHintRefreshReason::ExcerptsRemoved(excerpts_removed) => {
let InlaySplice { if let Some(InlaySplice {
to_remove, to_remove,
to_insert, to_insert,
} = self.inlay_hint_cache.remove_excerpts(excerpts_removed); }) = self.inlay_hint_cache.remove_excerpts(excerpts_removed)
self.splice_inlay_hints(to_remove, to_insert, cx); {
self.splice_inlay_hints(to_remove, to_insert, cx);
}
return; return;
} }
InlayHintRefreshReason::NewLinesShown => (InvalidationStrategy::None, None), InlayHintRefreshReason::NewLinesShown => (InvalidationStrategy::None, None),
+1 -1
View File
@@ -2755,7 +2755,7 @@ impl PointForPosition {
} }
} }
fn as_valid(&self) -> Option<DisplayPoint> { pub fn as_valid(&self) -> Option<DisplayPoint> {
if self.previous_valid == self.exact_unclipped && self.next_valid == self.exact_unclipped { if self.previous_valid == self.exact_unclipped && self.next_valid == self.exact_unclipped {
Some(self.previous_valid) Some(self.previous_valid)
} else { } else {
+30 -24
View File
@@ -57,8 +57,6 @@ pub struct InlayHover {
pub fn find_hovered_hint_part( pub fn find_hovered_hint_part(
label_parts: Vec<InlayHintLabelPart>, label_parts: Vec<InlayHintLabelPart>,
padding_left: bool,
padding_right: bool,
hint_range: Range<InlayOffset>, hint_range: Range<InlayOffset>,
hovered_offset: InlayOffset, hovered_offset: InlayOffset,
) -> Option<(InlayHintLabelPart, Range<InlayOffset>)> { ) -> Option<(InlayHintLabelPart, Range<InlayOffset>)> {
@@ -67,19 +65,11 @@ pub fn find_hovered_hint_part(
let mut part_start = hint_range.start; 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; part_start.0 += part_len;
} else { } else {
let mut part_end = InlayOffset(part_start.0 + part_len); let part_end = InlayOffset(part_start.0 + part_len);
if padding_left {
part_start.0 += 1;
part_end.0 += 1;
}
if padding_right {
part_start.0 += 1;
part_end.0 += 1;
}
return Some((part, part_start..part_end)); return Some((part, part_start..part_end));
} }
} }
@@ -1386,13 +1376,21 @@ mod tests {
.unwrap(); .unwrap();
let new_type_hint_part_hover_position = cx.update_editor(|editor, cx| { let new_type_hint_part_hover_position = cx.update_editor(|editor, cx| {
let snapshot = editor.snapshot(cx); let snapshot = editor.snapshot(cx);
let previous_valid = inlay_range.start.to_display_point(&snapshot);
let next_valid = inlay_range.end.to_display_point(&snapshot);
assert_eq!(previous_valid.row(), next_valid.row());
assert!(previous_valid.column() < next_valid.column());
let exact_unclipped = DisplayPoint::new(
previous_valid.row(),
previous_valid.column()
+ (entire_hint_label.find(new_type_label).unwrap() + new_type_label.len() / 2)
as u32,
);
PointForPosition { PointForPosition {
previous_valid: inlay_range.start.to_display_point(&snapshot), previous_valid,
next_valid: inlay_range.end.to_display_point(&snapshot), next_valid,
exact_unclipped: inlay_range.end.to_display_point(&snapshot), exact_unclipped,
column_overshoot_after_line_end: (entire_hint_label.find(new_type_label).unwrap() column_overshoot_after_line_end: 0,
+ new_type_label.len() / 2)
as u32,
} }
}); });
cx.update_editor(|editor, cx| { cx.update_editor(|editor, cx| {
@@ -1514,13 +1512,21 @@ mod tests {
let struct_hint_part_hover_position = cx.update_editor(|editor, cx| { let struct_hint_part_hover_position = cx.update_editor(|editor, cx| {
let snapshot = editor.snapshot(cx); let snapshot = editor.snapshot(cx);
let previous_valid = inlay_range.start.to_display_point(&snapshot);
let next_valid = inlay_range.end.to_display_point(&snapshot);
assert_eq!(previous_valid.row(), next_valid.row());
assert!(previous_valid.column() < next_valid.column());
let exact_unclipped = DisplayPoint::new(
previous_valid.row(),
previous_valid.column()
+ (entire_hint_label.find(struct_label).unwrap() + struct_label.len() / 2)
as u32,
);
PointForPosition { PointForPosition {
previous_valid: inlay_range.start.to_display_point(&snapshot), previous_valid,
next_valid: inlay_range.end.to_display_point(&snapshot), next_valid,
exact_unclipped: inlay_range.end.to_display_point(&snapshot), exact_unclipped,
column_overshoot_after_line_end: (entire_hint_label.find(struct_label).unwrap() column_overshoot_after_line_end: 0,
+ struct_label.len() / 2)
as u32,
} }
}); });
cx.update_editor(|editor, cx| { cx.update_editor(|editor, cx| {
+77 -90
View File
@@ -104,37 +104,34 @@ impl TasksForRanges {
invalidate: InvalidationStrategy, invalidate: InvalidationStrategy,
spawn_task: impl FnOnce(QueryRanges) -> Task<()>, spawn_task: impl FnOnce(QueryRanges) -> Task<()>,
) { ) {
let query_ranges = match invalidate { let query_ranges = if invalidate.should_invalidate() {
InvalidationStrategy::None => { self.tasks.clear();
let mut updated_ranges = query_ranges; self.sorted_ranges.clear();
updated_ranges.before_visible = updated_ranges query_ranges
.before_visible } else {
.into_iter() let mut non_cached_query_ranges = query_ranges;
.flat_map(|query_range| { non_cached_query_ranges.before_visible = non_cached_query_ranges
self.remove_cached_ranges_from_query(buffer_snapshot, query_range) .before_visible
}) .into_iter()
.collect(); .flat_map(|query_range| {
updated_ranges.visible = updated_ranges self.remove_cached_ranges_from_query(buffer_snapshot, query_range)
.visible })
.into_iter() .collect();
.flat_map(|query_range| { non_cached_query_ranges.visible = non_cached_query_ranges
self.remove_cached_ranges_from_query(buffer_snapshot, query_range) .visible
}) .into_iter()
.collect(); .flat_map(|query_range| {
updated_ranges.after_visible = updated_ranges self.remove_cached_ranges_from_query(buffer_snapshot, query_range)
.after_visible })
.into_iter() .collect();
.flat_map(|query_range| { non_cached_query_ranges.after_visible = non_cached_query_ranges
self.remove_cached_ranges_from_query(buffer_snapshot, query_range) .after_visible
}) .into_iter()
.collect(); .flat_map(|query_range| {
updated_ranges self.remove_cached_ranges_from_query(buffer_snapshot, query_range)
} })
InvalidationStrategy::RefreshRequested | InvalidationStrategy::BufferEdited => { .collect();
self.tasks.clear(); non_cached_query_ranges
self.sorted_ranges.clear();
query_ranges
}
}; };
if !query_ranges.is_empty() { if !query_ranges.is_empty() {
@@ -205,45 +202,31 @@ impl TasksForRanges {
ranges_to_query ranges_to_query
} }
fn remove_from_cached_ranges( fn invalidate_range(&mut self, buffer: &BufferSnapshot, range: &Range<language::Anchor>) {
&mut self,
buffer: &BufferSnapshot,
range_to_remove: &Range<language::Anchor>,
) {
self.sorted_ranges = self self.sorted_ranges = self
.sorted_ranges .sorted_ranges
.drain(..) .drain(..)
.filter_map(|mut cached_range| { .filter_map(|mut cached_range| {
if cached_range.start.cmp(&range_to_remove.end, buffer).is_gt() if cached_range.start.cmp(&range.end, buffer).is_gt()
|| cached_range.end.cmp(&range_to_remove.start, buffer).is_lt() || cached_range.end.cmp(&range.start, buffer).is_lt()
{ {
Some(vec![cached_range]) Some(vec![cached_range])
} else if cached_range } else if cached_range.start.cmp(&range.start, buffer).is_ge()
.start && cached_range.end.cmp(&range.end, buffer).is_le()
.cmp(&range_to_remove.start, buffer)
.is_ge()
&& cached_range.end.cmp(&range_to_remove.end, buffer).is_le()
{ {
None None
} else if range_to_remove } else if range.start.cmp(&cached_range.start, buffer).is_ge()
.start && range.end.cmp(&cached_range.end, buffer).is_le()
.cmp(&cached_range.start, buffer)
.is_ge()
&& range_to_remove.end.cmp(&cached_range.end, buffer).is_le()
{ {
Some(vec![ Some(vec![
cached_range.start..range_to_remove.start, cached_range.start..range.start,
range_to_remove.end..cached_range.end, range.end..cached_range.end,
]) ])
} else if cached_range } else if cached_range.start.cmp(&range.start, buffer).is_ge() {
.start cached_range.start = range.end;
.cmp(&range_to_remove.start, buffer)
.is_ge()
{
cached_range.start = range_to_remove.end;
Some(vec![cached_range]) Some(vec![cached_range])
} else { } else {
cached_range.end = range_to_remove.start; cached_range.end = range.start;
Some(vec![cached_range]) Some(vec![cached_range])
} }
}) })
@@ -474,7 +457,7 @@ impl InlayHintCache {
} }
} }
pub fn remove_excerpts(&mut self, excerpts_removed: Vec<ExcerptId>) -> InlaySplice { pub fn remove_excerpts(&mut self, excerpts_removed: Vec<ExcerptId>) -> Option<InlaySplice> {
let mut to_remove = Vec::new(); let mut to_remove = Vec::new();
for excerpt_to_remove in excerpts_removed { for excerpt_to_remove in excerpts_removed {
self.update_tasks.remove(&excerpt_to_remove); self.update_tasks.remove(&excerpt_to_remove);
@@ -483,17 +466,21 @@ impl InlayHintCache {
to_remove.extend(cached_hints.hints.iter().map(|(id, _)| *id)); to_remove.extend(cached_hints.hints.iter().map(|(id, _)| *id));
} }
} }
if !to_remove.is_empty() { if to_remove.is_empty() {
None
} else {
self.version += 1; self.version += 1;
} Some(InlaySplice {
InlaySplice { to_remove,
to_remove, to_insert: Vec::new(),
to_insert: Vec::new(), })
} }
} }
pub fn clear(&mut self) { pub fn clear(&mut self) {
self.version += 1; if !self.update_tasks.is_empty() || !self.hints.is_empty() {
self.version += 1;
}
self.update_tasks.clear(); self.update_tasks.clear();
self.hints.clear(); self.hints.clear();
} }
@@ -818,7 +805,7 @@ fn new_update_task(
.update_tasks .update_tasks
.get_mut(&query.excerpt_id) .get_mut(&query.excerpt_id)
{ {
task_ranges.remove_from_cached_ranges(&buffer_snapshot, &range); task_ranges.invalidate_range(&buffer_snapshot, &range);
} }
}) })
.ok() .ok()
@@ -877,33 +864,33 @@ async fn fetch_and_update_hints(
let inlay_hints_fetch_task = editor let inlay_hints_fetch_task = editor
.update(&mut cx, |editor, cx| { .update(&mut cx, |editor, cx| {
if got_throttled { if got_throttled {
if let Some((_, _, current_visible_range)) = editor let query_not_around_visible_range = match editor.excerpt_visible_offsets(None, cx).remove(&query.excerpt_id) {
.excerpt_visible_offsets(None, cx) Some((_, _, current_visible_range)) => {
.remove(&query.excerpt_id) let visible_offset_length = current_visible_range.len();
{ let double_visible_range = current_visible_range
let visible_offset_length = current_visible_range.len(); .start
let double_visible_range = current_visible_range .saturating_sub(visible_offset_length)
.start ..current_visible_range
.saturating_sub(visible_offset_length) .end
..current_visible_range .saturating_add(visible_offset_length)
.end .min(buffer_snapshot.len());
.saturating_add(visible_offset_length) !double_visible_range
.min(buffer_snapshot.len()); .contains(&fetch_range.start.to_offset(&buffer_snapshot))
if !double_visible_range && !double_visible_range
.contains(&fetch_range.start.to_offset(&buffer_snapshot)) .contains(&fetch_range.end.to_offset(&buffer_snapshot))
&& !double_visible_range },
.contains(&fetch_range.end.to_offset(&buffer_snapshot)) None => true,
};
if query_not_around_visible_range {
log::trace!("Fetching inlay hints for range {fetch_range_to_log:?} got throttled and fell off the current visible range, skipping.");
if let Some(task_ranges) = editor
.inlay_hint_cache
.update_tasks
.get_mut(&query.excerpt_id)
{ {
log::trace!("Fetching inlay hints for range {fetch_range_to_log:?} got throttled and fell off the current visible range, skipping."); task_ranges.invalidate_range(&buffer_snapshot, &fetch_range);
if let Some(task_ranges) = editor
.inlay_hint_cache
.update_tasks
.get_mut(&query.excerpt_id)
{
task_ranges.remove_from_cached_ranges(&buffer_snapshot, &fetch_range);
}
return None;
} }
return None;
} }
} }
editor editor
+49 -31
View File
@@ -23,6 +23,7 @@ pub struct LinkGoToDefinitionState {
pub task: Option<Task<Option<()>>>, pub task: Option<Task<Option<()>>>,
} }
#[derive(Debug)]
pub enum GoToDefinitionTrigger { pub enum GoToDefinitionTrigger {
Text(DisplayPoint), Text(DisplayPoint),
InlayHint(InlayRange, lsp::Location, LanguageServerId), InlayHint(InlayRange, lsp::Location, LanguageServerId),
@@ -81,7 +82,7 @@ impl TriggerPoint {
fn anchor(&self) -> &Anchor { fn anchor(&self) -> &Anchor {
match self { match self {
TriggerPoint::Text(anchor) => anchor, TriggerPoint::Text(anchor) => anchor,
TriggerPoint::InlayHint(coordinates, _, _) => &coordinates.inlay_position, TriggerPoint::InlayHint(range, _, _) => &range.inlay_position,
} }
} }
@@ -127,11 +128,22 @@ pub fn update_go_to_definition_link(
&trigger_point, &trigger_point,
&editor.link_go_to_definition_state.last_trigger_point, &editor.link_go_to_definition_state.last_trigger_point,
) { ) {
if a.anchor() match (a, b) {
.cmp(b.anchor(), &snapshot.buffer_snapshot) (TriggerPoint::Text(anchor_a), TriggerPoint::Text(anchor_b)) => {
.is_eq() if anchor_a.cmp(anchor_b, &snapshot.buffer_snapshot).is_eq() {
{ return;
return; }
}
(TriggerPoint::InlayHint(range_a, _, _), TriggerPoint::InlayHint(range_b, _, _)) => {
if range_a
.inlay_position
.cmp(&range_b.inlay_position, &snapshot.buffer_snapshot)
.is_eq()
{
return;
}
}
_ => {}
} }
} }
@@ -165,11 +177,8 @@ pub fn update_inlay_link_and_hover_points(
snapshot.display_point_to_inlay_offset(point_for_position.previous_valid, Bias::Left); snapshot.display_point_to_inlay_offset(point_for_position.previous_valid, Bias::Left);
let hint_end_offset = let hint_end_offset =
snapshot.display_point_to_inlay_offset(point_for_position.next_valid, Bias::Right); snapshot.display_point_to_inlay_offset(point_for_position.next_valid, Bias::Right);
let offset_overshoot = point_for_position.column_overshoot_after_line_end as usize; let hovered_offset = if point_for_position.column_overshoot_after_line_end == 0 {
let hovered_offset = if offset_overshoot == 0 {
Some(snapshot.display_point_to_inlay_offset(point_for_position.exact_unclipped, Bias::Left)) Some(snapshot.display_point_to_inlay_offset(point_for_position.exact_unclipped, Bias::Left))
} else if (hint_end_offset - hint_start_offset).0 >= offset_overshoot {
Some(InlayOffset(hint_start_offset.0 + offset_overshoot))
} else { } else {
None None
}; };
@@ -215,17 +224,18 @@ pub fn update_inlay_link_and_hover_points(
} }
} }
ResolveState::Resolved => { ResolveState::Resolved => {
let mut actual_hint_start = hint_start_offset;
let mut actual_hint_end = hint_end_offset;
if cached_hint.padding_left {
actual_hint_start.0 += 1;
actual_hint_end.0 += 1;
}
if cached_hint.padding_right {
actual_hint_start.0 += 1;
actual_hint_end.0 += 1;
}
match cached_hint.label { match cached_hint.label {
project::InlayHintLabel::String(_) => { project::InlayHintLabel::String(_) => {
let mut highlight_start = hint_start_offset;
let mut highlight_end = hint_end_offset;
if cached_hint.padding_left {
highlight_start.0 += 1;
highlight_end.0 += 1;
}
if cached_hint.padding_right {
highlight_end.0 -= 1;
}
if let Some(tooltip) = cached_hint.tooltip { if let Some(tooltip) = cached_hint.tooltip {
hover_popover::hover_at_inlay( hover_popover::hover_at_inlay(
editor, editor,
@@ -246,8 +256,8 @@ pub fn update_inlay_link_and_hover_points(
triggered_from: hovered_offset, triggered_from: hovered_offset,
range: InlayRange { range: InlayRange {
inlay_position: hovered_hint.position, inlay_position: hovered_hint.position,
highlight_start, highlight_start: actual_hint_start,
highlight_end, highlight_end: actual_hint_end,
}, },
}, },
cx, cx,
@@ -259,9 +269,7 @@ pub fn update_inlay_link_and_hover_points(
if let Some((hovered_hint_part, part_range)) = if let Some((hovered_hint_part, part_range)) =
hover_popover::find_hovered_hint_part( hover_popover::find_hovered_hint_part(
label_parts, label_parts,
cached_hint.padding_left, actual_hint_start..actual_hint_end,
cached_hint.padding_right,
hint_start_offset..hint_end_offset,
hovered_offset, hovered_offset,
) )
{ {
@@ -588,9 +596,11 @@ fn go_to_fetched_definition_of_kind(
cx, cx,
); );
match kind { if point.as_valid().is_some() {
LinkDefinitionKind::Symbol => editor.go_to_definition(&Default::default(), cx), match kind {
LinkDefinitionKind::Type => editor.go_to_type_definition(&Default::default(), cx), LinkDefinitionKind::Symbol => editor.go_to_definition(&Default::default(), cx),
LinkDefinitionKind::Type => editor.go_to_type_definition(&Default::default(), cx),
}
} }
} }
} }
@@ -1160,11 +1170,19 @@ mod tests {
.unwrap(); .unwrap();
let hint_hover_position = cx.update_editor(|editor, cx| { let hint_hover_position = cx.update_editor(|editor, cx| {
let snapshot = editor.snapshot(cx); let snapshot = editor.snapshot(cx);
let previous_valid = inlay_range.start.to_display_point(&snapshot);
let next_valid = inlay_range.end.to_display_point(&snapshot);
assert_eq!(previous_valid.row(), next_valid.row());
assert!(previous_valid.column() < next_valid.column());
let exact_unclipped = DisplayPoint::new(
previous_valid.row(),
previous_valid.column() + (hint_label.len() / 2) as u32,
);
PointForPosition { PointForPosition {
previous_valid: inlay_range.start.to_display_point(&snapshot), previous_valid,
next_valid: inlay_range.end.to_display_point(&snapshot), next_valid,
exact_unclipped: inlay_range.end.to_display_point(&snapshot), exact_unclipped,
column_overshoot_after_line_end: (hint_label.len() / 2) as u32, column_overshoot_after_line_end: 0,
} }
}); });
// Press cmd to trigger highlight // Press cmd to trigger highlight