From fa02bd71c387fcb532229e58f72a466f7d54f0a3 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sat, 7 Jun 2025 01:47:20 +0300 Subject: [PATCH] Select applicable positions for lsp_ext methods more leniently (#32272) Closes https://github.com/zed-industries/zed/issues/27238 Release Notes: - Fixed `editor::SwitchSourceHeader` and `editor::ExpandMacroRecursively` not working with text selections --- crates/collab/src/tests/editor_tests.rs | 11 ++++++++--- crates/editor/src/lsp_ext.rs | 5 ++--- crates/editor/src/rust_analyzer_ext.rs | 3 --- crates/text/src/selection.rs | 2 ++ 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/crates/collab/src/tests/editor_tests.rs b/crates/collab/src/tests/editor_tests.rs index da37904f0c8a44e59b1ba4c45d16a299bfc2f7eb..c9855c2fdea809d272e9beae1cef421c83acb2e5 100644 --- a/crates/collab/src/tests/editor_tests.rs +++ b/crates/collab/src/tests/editor_tests.rs @@ -7,7 +7,7 @@ use editor::{ Editor, RowInfo, actions::{ ConfirmCodeAction, ConfirmCompletion, ConfirmRename, ContextMenuFirst, - ExpandMacroRecursively, Redo, Rename, ToggleCodeActions, Undo, + ExpandMacroRecursively, Redo, Rename, SelectAll, ToggleCodeActions, Undo, }, test::{ editor_test_context::{AssertionContextManager, EditorTestContext}, @@ -2712,7 +2712,7 @@ async fn test_client_can_query_lsp_ext(cx_a: &mut TestAppContext, cx_b: &mut Tes params.text_document.uri, lsp::Url::from_file_path(path!("/a/main.rs")).unwrap(), ); - assert_eq!(params.position, lsp::Position::new(0, 0),); + assert_eq!(params.position, lsp::Position::new(0, 0)); Ok(Some(ExpandedMacro { name: "test_macro_name".to_string(), expansion: "test_macro_expansion on the host".to_string(), @@ -2747,7 +2747,11 @@ async fn test_client_can_query_lsp_ext(cx_a: &mut TestAppContext, cx_b: &mut Tes params.text_document.uri, lsp::Url::from_file_path(path!("/a/main.rs")).unwrap(), ); - assert_eq!(params.position, lsp::Position::new(0, 0),); + assert_eq!( + params.position, + lsp::Position::new(0, 12), + "editor_b has selected the entire text and should query for a different position" + ); Ok(Some(ExpandedMacro { name: "test_macro_name".to_string(), expansion: "test_macro_expansion on the client".to_string(), @@ -2756,6 +2760,7 @@ async fn test_client_can_query_lsp_ext(cx_a: &mut TestAppContext, cx_b: &mut Tes ); editor_b.update_in(cx_b, |editor, window, cx| { + editor.select_all(&SelectAll, window, cx); expand_macro_recursively(editor, &ExpandMacroRecursively, window, cx) }); expand_request_b.next().await.unwrap(); diff --git a/crates/editor/src/lsp_ext.rs b/crates/editor/src/lsp_ext.rs index 810cf171902d7f08aacc01b7fefc5c73f2b6dfcb..8d078f304ca9fdc2a3d9371762adb7dc72a65ca1 100644 --- a/crates/editor/src/lsp_ext.rs +++ b/crates/editor/src/lsp_ext.rs @@ -42,8 +42,8 @@ where .selections .disjoint_anchors() .iter() - .filter(|selection| selection.start == selection.end) - .filter_map(|selection| Some((selection.start, selection.start.buffer_id?))) + .filter_map(|selection| Some((selection.head(), selection.head().buffer_id?))) + .unique_by(|(_, buffer_id)| *buffer_id) .filter_map(|(trigger_anchor, buffer_id)| { let buffer = editor.buffer().read(cx).buffer(buffer_id)?; let language = buffer.read(cx).language_at(trigger_anchor.text_anchor)?; @@ -53,7 +53,6 @@ where None } }) - .unique_by(|(_, buffer, _)| buffer.read(cx).remote_id()) .collect::>(); let applicable_buffer_tasks = applicable_buffers diff --git a/crates/editor/src/rust_analyzer_ext.rs b/crates/editor/src/rust_analyzer_ext.rs index 86153334fbe4bbcb2b5bb3e350502c0fb6d3f011..da0f11036ff683a59a658b0b22139809d393d7ed 100644 --- a/crates/editor/src/rust_analyzer_ext.rs +++ b/crates/editor/src/rust_analyzer_ext.rs @@ -132,9 +132,6 @@ pub fn expand_macro_recursively( window: &mut Window, cx: &mut Context, ) { - if editor.selections.count() == 0 { - return; - } let Some(project) = &editor.project else { return; }; diff --git a/crates/text/src/selection.rs b/crates/text/src/selection.rs index fffece26b2d8df069701be651651f1c60a385ff5..18b82dbb6a6326dfe07703ee6881e9cef8442a76 100644 --- a/crates/text/src/selection.rs +++ b/crates/text/src/selection.rs @@ -26,6 +26,7 @@ impl Default for SelectionGoal { } impl Selection { + /// A place where the selection had stopped at. pub fn head(&self) -> T { if self.reversed { self.start.clone() @@ -34,6 +35,7 @@ impl Selection { } } + /// A place where selection was initiated from. pub fn tail(&self) -> T { if self.reversed { self.end.clone()