Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Check anchor validity for hovered links (#61686)
Jakub Konkaafd17238ed25
diff --git a/crates/editor/src/hover_links.rs b/crates/editor/src/hover_links.rsindex b0137979526..fa530f4434e 100644--- a/crates/editor/src/hover_links.rs+++ b/crates/editor/src/hover_links.rs@@ -51,8 +51,16 @@ impl RangeInEditor {) -> bool {match (self, trigger_point) {(Self::Text(range), TriggerPoint::Text(point)) => {- let point_after_start = range.start.cmp(point, &snapshot.buffer_snapshot()).is_le();- point_after_start && range.end.cmp(point, &snapshot.buffer_snapshot()).is_ge()+ let buffer_snapshot = snapshot.buffer_snapshot();+ if !range.start.is_valid(&buffer_snapshot)+ || !range.end.is_valid(&buffer_snapshot)+ || !point.is_valid(&buffer_snapshot)+ {+ return false;+ }+ let point_after_start = range.start.cmp(point, &buffer_snapshot).is_le();+ let point_after_end = range.end.cmp(point, &buffer_snapshot).is_ge();+ point_after_start && point_after_end}(Self::Inlay(highlight), TriggerPoint::InlayHint(point, _, _)) => {highlight.inlay == point.inlay@@ -1066,8 +1074,9 @@ mod tests {use futures::StreamExt;use gpui::{Modifiers, MousePressureEvent, PressureStage};use indoc::indoc;+ use language::Point;use lsp::request::{GotoDefinition, GotoTypeDefinition};- use multi_buffer::MultiBufferOffset;+ use multi_buffer::{MultiBufferOffset, PathKey};use settings::InlayHintSettingsContent;use std::str::FromStr;use std::sync::Arc;@@ -1195,6 +1204,71 @@ mod tests {"});}+ #[gpui::test]+ async fn test_hover_link_after_multibuffer_path_changes(cx: &mut gpui::TestAppContext) {+ init_test(cx, |_| {});++ let mut cx = EditorLspTestContext::new_rust(Default::default(), cx).await;+ cx.set_state("https://zed.dev/ˇreleases");+ let old_snapshot = cx.update_editor(|editor, window, cx| editor.snapshot(window, cx));+ let link_start = MultiBufferOffset(17).to_display_point(&old_snapshot.display_snapshot);+ let link_end = MultiBufferOffset(22).to_display_point(&old_snapshot.display_snapshot);+ let point_for_position = |point| PointForPosition {+ previous_valid: point,+ next_valid: point,+ nearest_valid: point,+ exact_unclipped: point,+ column_overshoot_after_line_end: 0,+ };++ let buffer = cx.editor(|editor, _, cx| {+ editor+ .buffer()+ .read(cx)+ .as_singleton()+ .expect("test editor should contain a singleton buffer")+ });+ cx.update_multibuffer(|multibuffer, cx| {+ let max_point = buffer.read(cx).max_point();+ multibuffer.set_excerpts_for_path(+ PathKey::sorted(1),+ buffer,+ [Point::zero()..max_point],+ 0,+ cx,+ );+ });+ cx.run_until_parked();++ let modifiers = if cfg!(target_os = "macos") {+ Modifiers::command_shift()+ } else {+ Modifiers::control_shift()+ };+ cx.update_editor(|editor, window, cx| {+ editor.update_hovered_link(+ point_for_position(link_start),+ None,+ &old_snapshot,+ modifiers,+ window,+ cx,+ );+ });+ cx.run_until_parked();++ cx.update_editor(|editor, window, cx| {+ editor.update_hovered_link(+ point_for_position(link_end),+ None,+ &old_snapshot,+ modifiers,+ window,+ cx,+ );+ });+ }+#[gpui::test]async fn test_go_to_definition_link_dedup(cx: &mut gpui::TestAppContext) {init_test(cx, |_| {});