Compare commits

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

32 commits

Author SHA1 Message Date
David Kleingeld
1a31961dac Extract get docs and show actual hover into function 2025-08-25 17:39:52 +02:00
Richard Feldman
78ca73e0b9
Minimize diff some more
Co-authored-by: David Kleingeld <davidsk@zed.dev>
2025-08-25 11:30:45 -04:00
Richard Feldman
c70178b7ea
Go back to using previous_valid_anchor
Co-authored-by: David Kleingeld <davidsk@zed.dev>
2025-08-25 11:10:09 -04:00
Richard Feldman
4df010d33a
Remove unnecessary deferred conditional
Co-authored-by: David Kleingeld <davidsk@zed.dev>
2025-08-25 11:07:03 -04:00
Richard Feldman
c44f9dfb17
Remove redundant tag type check
Co-authored-by: David Kleingeld <davidsk@zed.dev>
2025-08-25 11:02:23 -04:00
Richard Feldman
7c5738142a
Minimize diff a bit
Co-authored-by: David Kleingeld <davidsk@zed.dev>
2025-08-25 11:00:52 -04:00
Richard Feldman
23c9eb875a
Go back to old visible inlay hint mapping
Co-authored-by: David Kleingeld <davidsk@zed.dev>
2025-08-25 11:00:00 -04:00
Richard Feldman
81578c0d9e
Restore previous hovered_offset logic
Co-authored-by: David Kleingeld <davidsk@zed.dev>
2025-08-25 10:49:28 -04:00
Richard Feldman
70918609d8
Fix has_pending_selection logic for inlay hovers
Co-authored-by: David Kleingeld <davidsk@zed.dev>
2025-08-25 10:41:34 -04:00
Richard Feldman
835651fc08
Remove conditionals reintroduced by mistake in the merge
Co-authored-by: David Kleingeld <davidsk@zed.dev>
2025-08-25 10:25:33 -04:00
Richard Feldman
267e44e513
Merge remote-tracking branch 'origin/main' into inlay-hint-tooltip 2025-08-25 09:50:07 -04:00
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::{
Anchor, Editor, EditorSettings, EditorSnapshot, FindAllReferences, GoToDefinition,
GoToTypeDefinition, GotoDefinitionKind, InlayId, Navigated, PointForPosition, SelectPhase,
display_map::InlayOffset,
editor_settings::GoToDefinitionFallback,
hover_popover::{self, InlayHover},
scroll::ScrollAmount,
@ -15,6 +16,7 @@ use project::{
};
use settings::Settings;
use std::ops::Range;
use text;
use theme::ActiveTheme as _;
use util::{ResultExt, TryFutureExt as _, maybe};
@ -121,13 +123,25 @@ impl Editor {
cx: &mut Context<Self>,
) {
let hovered_link_modifier = Editor::multi_cursor_modifier(false, &modifiers, cx);
if !hovered_link_modifier || self.has_pending_selection() {
// When you're dragging to select, and you release the drag to create the selection,
// if you happened to end over something hoverable (including an inlay hint), don't
// have the hovered link appear. That would be annoying, because all you're trying
// to do is to create a selection, not hover to see a hovered link.
if self.has_pending_selection() {
self.hide_hovered_link(cx);
return;
}
match point_for_position.as_valid() {
Some(point) => {
// Hide the underline unless you're holding the modifier key on the keyboard
// which will perform a goto definition.
if !hovered_link_modifier {
self.hide_hovered_link(cx);
return;
}
let trigger_point = TriggerPoint::Text(
snapshot
.buffer_snapshot
@ -319,6 +333,7 @@ pub fn update_inlay_link_and_hover_points(
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) {
// Check if we should process this hint for hover
match cached_hint.resolve_state {
ResolveState::CanResolve(_, _) => {
if let Some(buffer_id) = snapshot
@ -419,31 +434,46 @@ pub fn update_inlay_link_and_hover_points(
);
hover_updated = true;
}
if let Some((language_server_id, location)) =
hovered_hint_part.location
&& secondary_held
&& !editor.has_pending_nonempty_selection()
{
go_to_definition_updated = true;
show_link_definition(
shift_held,
editor,
TriggerPoint::InlayHint(
highlight,
location,
language_server_id,
),
snapshot,
window,
cx,
);
// 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 = hovered_hint_part.value.clone();
let location = location.clone();
get_docs_then_show_hover(
window, cx, highlight, hint_value, location,
project,
);
}
if secondary_held
&& !editor.has_pending_nonempty_selection()
{
go_to_definition_updated = true;
show_link_definition(
shift_held,
editor,
TriggerPoint::InlayHint(
highlight,
location,
language_server_id,
),
snapshot,
window,
cx,
);
}
}
}
}
};
}
}
ResolveState::Resolving => {}
}
};
}
}
}
@ -456,6 +486,145 @@ pub fn update_inlay_link_and_hover_points(
}
}
fn get_docs_then_show_hover(
window: &mut Window,
cx: &mut Context<'_, Editor>,
highlight: InlayHighlight,
hint_value: String,
location: lsp::Location,
project: Entity<Project>,
) {
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, // TODO
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();
}
pub fn show_link_definition(
shift_held: bool,
editor: &mut Editor,