Compare commits

...
Sign in to create a new pull request.

21 commits

Author SHA1 Message Date
Richard Feldman
7ef2a8211f
Merge remote-tracking branch 'origin/main' into inlay-hint-tooltip 2025-07-15 10:53:58 -04:00
Richard Feldman
a509ae241a
Revert "wip"
This reverts commit 0bb1a5f98a.
2025-07-11 12:00:43 -04:00
Richard Feldman
0bb1a5f98a
wip 2025-07-11 12:00:41 -04:00
Richard Feldman
79f376d752
Clean up inlay hint hover logic 2025-07-11 11:05:23 -04:00
Richard Feldman
fe8b3fe53d
Drop debug logging 2025-07-10 22:30:29 -04:00
Richard Feldman
2cd812e54f
Delete unused import 2025-07-10 22:23:52 -04:00
Richard Feldman
6adf082e43
Revert "Add a hover when hovering over inlays"
This reverts commit 0d6232b373.
2025-07-10 22:23:39 -04:00
Richard Feldman
e4963e70cc
Revert "Attempt to fix hover"
This reverts commit e7c6f228d5.
2025-07-10 22:23:34 -04:00
Richard Feldman
e7c6f228d5
Attempt to fix hover 2025-07-10 22:23:26 -04:00
Richard Feldman
0d6232b373
Add a hover when hovering over inlays 2025-07-10 19:14:36 -04:00
Richard Feldman
ca4df68f31
Remove flashed black circle 2025-07-10 17:54:22 -04:00
Richard Feldman
c96b6a06f0
Don't show loading message 2025-07-10 17:46:13 -04:00
Richard Feldman
b1cd20a435
Remove all debug logging from inlay hint hover implementation 2025-07-10 17:46:08 -04:00
Richard Feldman
509375c83c
Remove some debug logging 2025-07-10 17:39:37 -04:00
Richard Feldman
b6bd9c0682
It works 2025-07-10 17:32:12 -04:00
Richard Feldman
8ee82395b8
Kinda make this work 2025-07-10 17:14:30 -04:00
Richard Feldman
a322aa33c7
wip - currently just shows a generic message, not the docs 2025-07-09 16:37:37 -04:00
Richard Feldman
2ff30d20e3
Fix inlay hint hover by not clearing hover when mouse is over inlay
When hovering over an inlay hint, point_for_position.as_valid() returns None
because inlays don't have valid text positions. This was causing hover_at(editor, None)
to be called, which would hide any active hovers.

The fix is simple: don't call hover_at when we're over an inlay position.
The inlay hover is already handled by update_hovered_link, so we don't need
to do anything else.
2025-07-09 13:15:43 -04:00
Richard Feldman
01d7b3345b
Fix inlay hint caching 2025-07-09 13:04:11 -04:00
Richard Feldman
17f7312fc0
Extract resolve_hint
Co-authored-by: Cole Miller <cole@zed.dev>
2025-07-07 16:48:33 -04:00
Richard Feldman
a61e478152
Reproduce #33715 in a test 2025-07-02 15:50:02 -04:00

View file

@ -1,6 +1,7 @@
use crate::{ use crate::{
Anchor, Editor, EditorSettings, EditorSnapshot, FindAllReferences, GoToDefinition, Anchor, Editor, EditorSettings, EditorSnapshot, FindAllReferences, GoToDefinition,
GoToTypeDefinition, GotoDefinitionKind, InlayId, Navigated, PointForPosition, SelectPhase, GoToTypeDefinition, GotoDefinitionKind, InlayId, Navigated, PointForPosition, SelectPhase,
display_map::InlayOffset,
editor_settings::GoToDefinitionFallback, editor_settings::GoToDefinitionFallback,
hover_popover::{self, InlayHover}, hover_popover::{self, InlayHover},
scroll::ScrollAmount, scroll::ScrollAmount,
@ -15,6 +16,7 @@ use project::{
}; };
use settings::Settings; use settings::Settings;
use std::ops::Range; use std::ops::Range;
use text;
use theme::ActiveTheme as _; use theme::ActiveTheme as _;
use util::{ResultExt, TryFutureExt as _, maybe}; use util::{ResultExt, TryFutureExt as _, maybe};
@ -121,13 +123,14 @@ impl Editor {
cx: &mut Context<Self>, cx: &mut Context<Self>,
) { ) {
let hovered_link_modifier = Editor::multi_cursor_modifier(false, &modifiers, cx); let hovered_link_modifier = Editor::multi_cursor_modifier(false, &modifiers, cx);
if !hovered_link_modifier || self.has_pending_selection() {
self.hide_hovered_link(cx);
return;
}
match point_for_position.as_valid() { match point_for_position.as_valid() {
Some(point) => { Some(point) => {
if !hovered_link_modifier || self.has_pending_selection() {
self.hide_hovered_link(cx);
return;
}
let trigger_point = TriggerPoint::Text( let trigger_point = TriggerPoint::Text(
snapshot snapshot
.buffer_snapshot .buffer_snapshot
@ -284,114 +287,117 @@ pub fn update_inlay_link_and_hover_points(
window: &mut Window, window: &mut Window,
cx: &mut Context<Editor>, cx: &mut Context<Editor>,
) { ) {
let hovered_offset = if point_for_position.column_overshoot_after_line_end == 0 { // For inlay hints, we need to use the exact position where the mouse is
Some(snapshot.display_point_to_inlay_offset(point_for_position.exact_unclipped, Bias::Left)) // But we must clip it to valid bounds to avoid panics
} else { let clipped_point = snapshot.clip_point(point_for_position.exact_unclipped, Bias::Left);
None let hovered_offset = snapshot.display_point_to_inlay_offset(clipped_point, Bias::Left);
};
let mut go_to_definition_updated = false; let mut go_to_definition_updated = false;
let mut hover_updated = false; let mut hover_updated = false;
if let Some(hovered_offset) = hovered_offset {
let buffer_snapshot = editor.buffer().read(cx).snapshot(cx); // Get all visible inlay hints
let previous_valid_anchor = buffer_snapshot.anchor_at( let visible_hints = editor.visible_inlay_hints(cx);
point_for_position.previous_valid.to_point(snapshot),
Bias::Left, // Find if we're hovering over an inlay hint
); if let Some(hovered_inlay) = visible_hints.into_iter().find(|inlay| {
let next_valid_anchor = buffer_snapshot.anchor_at( // Only process hint inlays
point_for_position.next_valid.to_point(snapshot), if !matches!(inlay.id, InlayId::Hint(_)) {
Bias::Right, return false;
); }
if let Some(hovered_hint) = editor
.visible_inlay_hints(cx) // Check if the hovered position falls within this inlay's display range
.into_iter() let inlay_start = snapshot.anchor_to_inlay_offset(inlay.position);
.skip_while(|hint| { let inlay_end = InlayOffset(inlay_start.0 + inlay.text.len());
hint.position
.cmp(&previous_valid_anchor, &buffer_snapshot) hovered_offset >= inlay_start && hovered_offset < inlay_end
.is_lt() }) {
}) let inlay_hint_cache = editor.inlay_hint_cache();
.take_while(|hint| { let excerpt_id = hovered_inlay.position.excerpt_id;
hint.position
.cmp(&next_valid_anchor, &buffer_snapshot) // Extract the hint ID from the inlay
.is_le() if let InlayId::Hint(_hint_id) = hovered_inlay.id {
}) if let Some(cached_hint) = inlay_hint_cache.hint_by_id(excerpt_id, hovered_inlay.id) {
.max_by_key(|hint| hint.id) // Check if we should process this hint for hover
{ let should_process_hint = match cached_hint.resolve_state {
let inlay_hint_cache = editor.inlay_hint_cache();
let excerpt_id = previous_valid_anchor.excerpt_id;
if let Some(cached_hint) = inlay_hint_cache.hint_by_id(excerpt_id, hovered_hint.id) {
match cached_hint.resolve_state {
ResolveState::CanResolve(_, _) => { ResolveState::CanResolve(_, _) => {
if let Some(buffer_id) = previous_valid_anchor.buffer_id { // For unresolved hints, spawn resolution
if let Some(buffer_id) = hovered_inlay.position.buffer_id {
inlay_hint_cache.spawn_hint_resolve( inlay_hint_cache.spawn_hint_resolve(
buffer_id, buffer_id,
excerpt_id, excerpt_id,
hovered_hint.id, hovered_inlay.id,
window, window,
cx, cx,
); );
} }
false // Don't process unresolved hints
} }
ResolveState::Resolved => { ResolveState::Resolved => true,
let mut extra_shift_left = 0; ResolveState::Resolving => false,
let mut extra_shift_right = 0; };
if cached_hint.padding_left {
extra_shift_left += 1; if should_process_hint {
extra_shift_right += 1; let mut extra_shift_left = 0;
} let mut extra_shift_right = 0;
if cached_hint.padding_right { if cached_hint.padding_left {
extra_shift_right += 1; extra_shift_left += 1;
} extra_shift_right += 1;
match cached_hint.label { }
project::InlayHintLabel::String(_) => { if cached_hint.padding_right {
if let Some(tooltip) = cached_hint.tooltip { extra_shift_right += 1;
hover_popover::hover_at_inlay( }
editor, match cached_hint.label {
InlayHover { project::InlayHintLabel::String(_) => {
tooltip: match tooltip { if let Some(tooltip) = cached_hint.tooltip {
InlayHintTooltip::String(text) => HoverBlock { hover_popover::hover_at_inlay(
text, editor,
kind: HoverBlockKind::PlainText, InlayHover {
}, tooltip: match tooltip {
InlayHintTooltip::MarkupContent(content) => { InlayHintTooltip::String(text) => HoverBlock {
HoverBlock { text,
text: content.value, kind: HoverBlockKind::PlainText,
kind: content.kind, },
} InlayHintTooltip::MarkupContent(content) => {
HoverBlock {
text: content.value,
kind: content.kind,
} }
}, }
range: InlayHighlight {
inlay: hovered_hint.id,
inlay_position: hovered_hint.position,
range: extra_shift_left
..hovered_hint.text.len() + extra_shift_right,
},
}, },
window, range: InlayHighlight {
cx, inlay: hovered_inlay.id,
); inlay_position: hovered_inlay.position,
hover_updated = true; range: extra_shift_left
} ..hovered_inlay.text.len() + extra_shift_right,
},
},
window,
cx,
);
hover_updated = true;
} }
project::InlayHintLabel::LabelParts(label_parts) => { }
let hint_start = project::InlayHintLabel::LabelParts(label_parts) => {
snapshot.anchor_to_inlay_offset(hovered_hint.position); // Find the first part with actual hover information (tooltip or location)
if let Some((hovered_hint_part, part_range)) = let _hint_start =
hover_popover::find_hovered_hint_part( snapshot.anchor_to_inlay_offset(hovered_inlay.position);
label_parts, let mut part_offset = 0;
hint_start,
hovered_offset, for part in label_parts {
) let part_len = part.value.chars().count();
{
let highlight_start = if part.tooltip.is_some() || part.location.is_some() {
(part_range.start - hint_start).0 + extra_shift_left; // Found the meaningful part - show hover for it
let highlight_end = let highlight_start = part_offset + extra_shift_left;
(part_range.end - hint_start).0 + extra_shift_right; let highlight_end = part_offset + part_len + extra_shift_right;
let highlight = InlayHighlight { let highlight = InlayHighlight {
inlay: hovered_hint.id, inlay: hovered_inlay.id,
inlay_position: hovered_hint.position, inlay_position: hovered_inlay.position,
range: highlight_start..highlight_end, range: highlight_start..highlight_end,
}; };
if let Some(tooltip) = hovered_hint_part.tooltip {
if let Some(tooltip) = part.tooltip {
hover_popover::hover_at_inlay( hover_popover::hover_at_inlay(
editor, editor,
InlayHover { InlayHover {
@ -415,10 +421,160 @@ pub fn update_inlay_link_and_hover_points(
cx, cx,
); );
hover_updated = true; hover_updated = true;
} } else if let Some((_language_server_id, location)) =
if let Some((language_server_id, location)) = part.location.clone()
hovered_hint_part.location
{ {
// When there's no tooltip but we have a location, perform a "Go to Definition" style operation
let filename = location
.uri
.path()
.split('/')
.next_back()
.unwrap_or("unknown")
.to_string();
hover_popover::hover_at_inlay(
editor,
InlayHover {
tooltip: HoverBlock {
text: "Loading documentation...".to_string(),
kind: HoverBlockKind::PlainText,
},
range: highlight.clone(),
},
window,
cx,
);
hover_updated = true;
// Now perform the "Go to Definition" flow to get hover documentation
if let Some(project) = editor.project.clone() {
let highlight = highlight.clone();
let hint_value = part.value.clone();
let location_uri = location.uri.clone();
cx.spawn_in(window, async move |editor, cx| {
async move {
// Small delay to show the loading message first
cx.background_executor()
.timer(std::time::Duration::from_millis(50))
.await;
// Convert LSP URL to file path
let file_path = location.uri.to_file_path()
.map_err(|_| anyhow::anyhow!("Invalid file URL"))?;
// Open the definition file
let definition_buffer = project
.update(cx, |project, cx| {
project.open_local_buffer(file_path, cx)
})?
.await?;
// Extract documentation directly from the source
let documentation = definition_buffer.update(cx, |buffer, _| {
let line_number = location.range.start.line as usize;
// Get the text of the buffer
let text = buffer.text();
let lines: Vec<&str> = text.lines().collect();
// Look backwards from the definition line to find doc comments
let mut doc_lines = Vec::new();
let mut current_line = line_number.saturating_sub(1);
// Skip any attributes like #[derive(...)]
while current_line > 0 && lines.get(current_line).map_or(false, |line| {
let trimmed = line.trim();
trimmed.starts_with("#[") || trimmed.is_empty()
}) {
current_line = current_line.saturating_sub(1);
}
// Collect doc comments
while current_line > 0 {
if let Some(line) = lines.get(current_line) {
let trimmed = line.trim();
if trimmed.starts_with("///") {
// Remove the /// and any leading space
let doc_text = trimmed.strip_prefix("///").unwrap_or("")
.strip_prefix(" ").unwrap_or_else(|| trimmed.strip_prefix("///").unwrap_or(""));
doc_lines.push(doc_text.to_string());
} else if !trimmed.is_empty() {
// Stop at the first non-doc, non-empty line
break;
}
}
current_line = current_line.saturating_sub(1);
}
// Reverse to get correct order
doc_lines.reverse();
// Also get the actual definition line
let definition = lines.get(line_number)
.map(|s| s.trim().to_string())
.unwrap_or_else(|| hint_value.clone());
if doc_lines.is_empty() {
None
} else {
let docs = doc_lines.join("\n");
Some((definition, docs))
}
})?;
if let Some((definition, docs)) = documentation {
// Format as markdown with the definition as a code block
let formatted_docs = format!("```rust\n{}\n```\n\n{}", definition, docs);
editor.update_in(cx, |editor, window, cx| {
hover_popover::hover_at_inlay(
editor,
InlayHover {
tooltip: HoverBlock {
text: formatted_docs,
kind: HoverBlockKind::Markdown,
},
range: highlight,
},
window,
cx,
);
}).log_err();
} else {
// Fallback to showing just the location info
let fallback_text = format!(
"{}\n\nDefined in {} at line {}",
hint_value.trim(),
filename,
location.range.start.line + 1
);
editor.update_in(cx, |editor, window, cx| {
hover_popover::hover_at_inlay(
editor,
InlayHover {
tooltip: HoverBlock {
text: fallback_text,
kind: HoverBlockKind::PlainText,
},
range: highlight,
},
window,
cx,
);
}).log_err();
}
anyhow::Ok(())
}
.log_err()
.await
}).detach();
}
}
if let Some((language_server_id, location)) = &part.location {
if secondary_held if secondary_held
&& !editor.has_pending_nonempty_selection() && !editor.has_pending_nonempty_selection()
{ {
@ -428,8 +584,8 @@ pub fn update_inlay_link_and_hover_points(
editor, editor,
TriggerPoint::InlayHint( TriggerPoint::InlayHint(
highlight, highlight,
location, location.clone(),
language_server_id, *language_server_id,
), ),
snapshot, snapshot,
window, window,
@ -437,11 +593,14 @@ pub fn update_inlay_link_and_hover_points(
); );
} }
} }
break;
} }
part_offset += part_len;
} }
}; }
} };
ResolveState::Resolving => {}
} }
} }
} }