Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

editor: Stop re-requesting hover and definitions for the same position (#58244)

Veesh Goldmanf8cfad3420ad
diff --git a/crates/editor/src/hover_links.rs b/crates/editor/src/hover_links.rs
index 215389e3037..2b72ff8ac1d 100644
--- a/crates/editor/src/hover_links.rs
+++ b/crates/editor/src/hover_links.rs
@@ -409,6 +409,11 @@ pub fn show_link_definition(
let project = editor.project.clone();
let provider = editor.semantics_provider.clone();
+ // Record the requested position so a mouse move on the same point short-circuits
+ // instead of re-querying, even when the server returns no `originSelectionRange`
+ // (which would otherwise leave `symbol_range` empty).
+ hovered_link_state.last_trigger_point = trigger_point.clone();
+
hovered_link_state.task = Some(cx.spawn_in(window, async move |this, cx| {
async move {
// LSP document links take priority: the server explicitly
@@ -1066,6 +1071,8 @@ mod tests {
use multi_buffer::MultiBufferOffset;
use settings::InlayHintSettingsContent;
use std::str::FromStr;
+ use std::sync::Arc;
+ use std::sync::atomic::{AtomicUsize, Ordering};
use util::{assert_set_eq, path};
use workspace::item::Item;
@@ -1238,6 +1245,70 @@ mod tests {
"});
}
+ #[gpui::test]
+ async fn test_go_to_definition_link_dedup(cx: &mut gpui::TestAppContext) {
+ // Jiggling the mouse over an already-queried position must not re-issue a
+ // definition request, even when the server replies with a bare `Location`
+ // and no `originSelectionRange` (issue #56193).
+ init_test(cx, |_| {});
+
+ let mut cx = EditorLspTestContext::new_rust(
+ lsp::ServerCapabilities {
+ hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
+ definition_provider: Some(lsp::OneOf::Left(true)),
+ ..Default::default()
+ },
+ cx,
+ )
+ .await;
+
+ cx.set_state(indoc! {"
+ fn ˇtest() { do_work(); }
+ fn do_work() { test(); }
+ "});
+
+ let request_count = Arc::new(AtomicUsize::new(0));
+ let _requests = cx.set_request_handler::<GotoDefinition, _, _>({
+ let request_count = request_count.clone();
+ move |url, _, _| {
+ let request_count = request_count.clone();
+ async move {
+ request_count.fetch_add(1, Ordering::SeqCst);
+ // Bare `Location`, no `originSelectionRange` (like phpactor).
+ Ok(Some(lsp::GotoDefinitionResponse::Scalar(lsp::Location {
+ uri: url,
+ range: lsp::Range::default(),
+ })))
+ }
+ }
+ });
+
+ let first_point = cx.pixel_position(indoc! {"
+ fn test() { do_wˇork(); }
+ fn do_work() { test(); }
+ "});
+ let second_point = cx.pixel_position(indoc! {"
+ fn test() { do_woˇrk(); }
+ fn do_work() { test(); }
+ "});
+
+ cx.simulate_mouse_move(first_point, None, Modifiers::secondary_key());
+ cx.run_until_parked();
+
+ cx.simulate_mouse_move(second_point, None, Modifiers::secondary_key());
+ cx.run_until_parked();
+
+ // Jiggle within the same character: no new request.
+ cx.simulate_mouse_move(second_point, None, Modifiers::secondary_key());
+ cx.run_until_parked();
+
+ assert_eq!(
+ request_count.load(Ordering::SeqCst),
+ 2,
+ "expected one definition request per distinct position"
+ );
+ }
+
#[gpui::test]
async fn test_hover_links(cx: &mut gpui::TestAppContext) {
init_test(cx, |_| {});
diff --git a/crates/editor/src/hover_popover.rs b/crates/editor/src/hover_popover.rs
index 5c5eb651e35..53f24cc1fd1 100644
--- a/crates/editor/src/hover_popover.rs
+++ b/crates/editor/src/hover_popover.rs
@@ -253,7 +253,6 @@ pub fn hide_hover(editor: &mut Editor, cx: &mut Context<Editor>) -> bool {
let did_hide = info_popovers.count() > 0 || diagnostics_popover.is_some();
editor.hover_state.info_task = None;
- editor.hover_state.triggered_from = None;
editor.hover_state.hiding_delay_task = None;
editor.hover_state.closest_mouse_distance = None;
@@ -309,15 +308,6 @@ fn show_hover(
}
}
- // Don't request again if the location is the same as the previous request
- if let Some(triggered_from) = &editor.hover_state.triggered_from
- && triggered_from
- .cmp(&anchor, &snapshot.buffer_snapshot())
- .is_eq()
- {
- return None;
- }
-
let hover_popover_delay = EditorSettings::get_global(cx).hover_popover_delay.0;
let all_diagnostics_active = editor.all_diagnostics_active();
let active_group_id = editor.active_diagnostic_group_id();
@@ -864,7 +854,6 @@ pub fn open_markdown_url(link: SharedString, window: &mut Window, cx: &mut App)
pub struct HoverState {
pub info_popovers: Vec<InfoPopover>,
pub diagnostic_popover: Option<DiagnosticPopover>,
- pub triggered_from: Option<Anchor>,
pub info_task: Option<Task<Option<()>>>,
pub closest_mouse_distance: Option<Pixels>,
pub hiding_delay_task: Option<Task<()>>,
@@ -2692,10 +2681,6 @@ mod tests {
editor.hover_state.info_task.is_none(),
"No hover info task should be scheduled when hover is disabled"
);
- assert!(
- editor.hover_state.triggered_from.is_none(),
- "No hover trigger should be recorded when hover is disabled"
- );
});
}
}