Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Use raw head selection for rename (#60594)
Neel86b2831c7c2e
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex 96567329721..87589880b53 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -7707,16 +7707,21 @@ impl Editor {let cursor = self.rename_target_anchor(&selection, cx);let (cursor_buffer, cursor_buffer_position) =self.buffer.read(cx).text_anchor_for_position(cursor, cx)?;+ let (head_buffer, head_buffer_position) = self+ .buffer+ .read(cx)+ .text_anchor_for_position(selection.head(), cx)?;let (tail_buffer, cursor_buffer_position_end) = self.buffer.read(cx).text_anchor_for_position(selection.tail(), cx)?;- if tail_buffer != cursor_buffer {+ if tail_buffer != cursor_buffer || head_buffer != cursor_buffer {return None;}let snapshot = cursor_buffer.read(cx).snapshot();let cursor_buffer_offset = cursor_buffer_position.to_offset(&snapshot);+ let head_buffer_offset = head_buffer_position.to_offset(&snapshot);let cursor_buffer_offset_end = cursor_buffer_position_end.to_offset(&snapshot);let prepare_rename = provider.range_for_rename(&cursor_buffer, cursor_buffer_position, cx);drop(snapshot);@@ -7729,7 +7734,9 @@ impl Editor {let rename_buffer_range = rename_range.to_offset(&snapshot);let cursor_offset_in_rename_range =cursor_buffer_offset.saturating_sub(rename_buffer_range.start);- let cursor_offset_in_rename_range_end =+ let head_offset_in_rename_range =+ head_buffer_offset.saturating_sub(rename_buffer_range.start);+ let tail_offset_in_rename_range =cursor_buffer_offset_end.saturating_sub(rename_buffer_range.start);this.take_rename(false, window, cx);@@ -7770,24 +7777,23 @@ impl Editor {cx,)});- let cursor_offset_in_rename_range =- MultiBufferOffset(cursor_offset_in_rename_range);- let cursor_offset_in_rename_range_end =- MultiBufferOffset(cursor_offset_in_rename_range_end);- let rename_selection_range = match cursor_offset_in_rename_range- .cmp(&cursor_offset_in_rename_range_end)- {- Ordering::Equal => {- editor.select_all(&SelectAll, window, cx);- return editor;- }- Ordering::Less => {- cursor_offset_in_rename_range..cursor_offset_in_rename_range_end- }- Ordering::Greater => {- cursor_offset_in_rename_range_end..cursor_offset_in_rename_range- }- };+ let head_offset_in_rename_range =+ MultiBufferOffset(head_offset_in_rename_range);+ let tail_offset_in_rename_range =+ MultiBufferOffset(tail_offset_in_rename_range);+ let rename_selection_range =+ match head_offset_in_rename_range.cmp(&tail_offset_in_rename_range) {+ Ordering::Equal => {+ editor.select_all(&SelectAll, window, cx);+ return editor;+ }+ Ordering::Less => {+ head_offset_in_rename_range..tail_offset_in_rename_range+ }+ Ordering::Greater => {+ tail_offset_in_rename_range..head_offset_in_rename_range+ }+ };if rename_selection_range.end.0 > old_name.len() {editor.select_all(&SelectAll, window, cx);} else {diff --git a/crates/vim/src/helix.rs b/crates/vim/src/helix.rsindex 79c65b227bb..2ab20aef36e 100644--- a/crates/vim/src/helix.rs+++ b/crates/vim/src/helix.rs@@ -4350,7 +4350,7 @@ mod test {.set_request_handler::<lsp::request::PrepareRenameRequest, _, _>(move |_, params, _| async move {assert_eq!(params.position, expected_position);- Ok(Some(lsp::PrepareRenameResponse::Range(def_range)))+ Ok(Some(lsp::PrepareRenameResponse::Range(tgt_range)))},);let mut rename_request = cx.set_request_handler::<lsp::request::Rename, _, _>(diff --git a/crates/vim/src/test.rs b/crates/vim/src/test.rsindex c426127dc07..4a874591f72 100644--- a/crates/vim/src/test.rs+++ b/crates/vim/src/test.rs@@ -1272,7 +1272,7 @@ async fn test_visual_rename_uses_visible_cursor_position(cx: &mut gpui::TestAppClet mut prepare_request = cx.set_request_handler::<lsp::request::PrepareRenameRequest, _, _>(move |_, params, _| async move {assert_eq!(params.position, expected_position);- Ok(Some(lsp::PrepareRenameResponse::Range(def_range)))+ Ok(Some(lsp::PrepareRenameResponse::Range(tgt_range)))},);let mut rename_request =