Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
project: Fix ref-match rust completions dropping the leading & (#60521)
Smit Barmase001bda1a46af
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rsindex 181a4d447ba..2fa319300e5 100644--- a/crates/editor/src/editor_tests.rs+++ b/crates/editor/src/editor_tests.rs@@ -22255,6 +22255,166 @@ async fn test_completions_with_additional_edits(cx: &mut TestAppContext) {cx.assert_editor_state("fn main() { let a = Some(2)ˇ; }");}+async fn check_completion_additional_edits(+ initial_state: &str,+ keystroke: &str,+ completion_item: lsp::CompletionItem,+ state_after_primary_edit: &str,+ state_after_additional_edits: &str,+ cx: &mut TestAppContext,+) {+ init_test(cx, |_| {});++ let mut cx = EditorLspTestContext::new_rust(+ lsp::ServerCapabilities {+ completion_provider: Some(lsp::CompletionOptions {+ trigger_characters: Some(vec![".".to_string()]),+ resolve_provider: Some(true),+ ..Default::default()+ }),+ ..Default::default()+ },+ cx,+ )+ .await;++ cx.set_state(initial_state);++ let closure_completion_item = completion_item.clone();+ let mut request = cx.set_request_handler::<lsp::request::Completion, _, _>(move |_, _, _| {+ let task_completion_item = closure_completion_item.clone();+ async move {+ Ok(Some(lsp::CompletionResponse::Array(vec![+ task_completion_item,+ ])))+ }+ });++ cx.simulate_keystroke(keystroke);+ request.next().await;++ cx.condition(|editor, _| editor.context_menu_visible())+ .await;+ let apply_additional_edits = cx.update_editor(|editor, window, cx| {+ editor+ .confirm_completion(&ConfirmCompletion::default(), window, cx)+ .unwrap()+ });+ cx.assert_editor_state(state_after_primary_edit);++ cx.set_request_handler::<lsp::request::ResolveCompletionItem, _, _>(move |_, _, _| {+ let task_completion_item = completion_item.clone();+ async move { Ok(task_completion_item) }+ })+ .next()+ .await+ .unwrap();+ apply_additional_edits.await.unwrap();+ cx.assert_editor_state(state_after_additional_edits);+}++// rust-analyzer's ref-match completions (`&some_str`) deliver the `&` as a+// zero-width additionalTextEdit at the primary edit's start.+// Ref: https://github.com/zed-industries/zed/issues/56973+#[gpui::test]+async fn test_completions_with_zero_width_additional_edit_at_primary_edit_start(+ cx: &mut TestAppContext,+) {+ check_completion_additional_edits(+ // The completion must not start at (0, 0), so that this case is+ // distinct from the file-start auto-import test below.+ "bar(fˇ)",+ "o",+ lsp::CompletionItem {+ label: "&foo".to_string(),+ filter_text: Some("foo".to_string()),+ // Replaces the typed `fo` with `foo`; the committed range is 4..7.+ text_edit: Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {+ range: lsp::Range::new(lsp::Position::new(0, 4), lsp::Position::new(0, 6)),+ new_text: "foo".to_string(),+ })),+ // Zero-width insertion exactly at the committed range's start.+ additional_text_edits: Some(vec![lsp::TextEdit {+ range: lsp::Range::new(lsp::Position::new(0, 4), lsp::Position::new(0, 4)),+ new_text: "&".to_string(),+ }]),+ ..Default::default()+ },+ "bar(fooˇ)",+ "bar(&fooˇ)",+ cx,+ )+ .await;+}++// Additional edits which overlap the primary completion edit must be skipped+// while non-overlapping edits from the same completion are still applied.+// Ref: https://github.com/zed-industries/zed/pull/1871+#[gpui::test]+async fn test_completions_skip_additional_edits_overlapping_primary_edit(cx: &mut TestAppContext) {+ check_completion_additional_edits(+ "fˇ\nbar",+ "o",+ lsp::CompletionItem {+ label: "foo".to_string(),+ filter_text: Some("foo".to_string()),+ // Replaces the typed `fo` with `foo`; the committed range is 0..3.+ text_edit: Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {+ range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 2)),+ new_text: "foo".to_string(),+ })),+ additional_text_edits: Some(vec![+ // Skip text strictly inside the committed range.+ lsp::TextEdit {+ range: lsp::Range::new(lsp::Position::new(0, 1), lsp::Position::new(0, 2)),+ new_text: "XXX".to_string(),+ },+ // Apply text which does not touch the committed range.+ lsp::TextEdit {+ range: lsp::Range::new(lsp::Position::new(1, 0), lsp::Position::new(1, 3)),+ new_text: "baz".to_string(),+ },+ ]),+ ..Default::default()+ },+ "fooˇ\nbar",+ "fooˇ\nbaz",+ cx,+ )+ .await;+}++// When both the primary completion edit and an additional edit (auto-import)+// start at the very beginning of the file, the additional edit must not be+// treated as overlapping. This payload shape matches what+// typescript-language-server actually sends for auto-imports at file start.+// Ref: https://github.com/zed-industries/zed/issues/26136+#[gpui::test]+async fn test_completions_with_file_start_auto_import_additional_edit(cx: &mut TestAppContext) {+ check_completion_additional_edits(+ "ˇ",+ "f",+ lsp::CompletionItem {+ label: "foo".to_string(),+ // Replaces the typed `f` at file start; the committed range is 0..3.+ text_edit: Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {+ range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 1)),+ new_text: "foo".to_string(),+ })),+ // Auto-import inserted at the very start of the file.+ additional_text_edits: Some(vec![lsp::TextEdit {+ range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 0)),+ new_text: "bar\n".to_string(),+ }]),+ ..Default::default()+ },+ "fooˇ",+ "bar\nfooˇ",+ cx,+ )+ .await;+}+#[gpui::test]async fn test_completions_with_additional_edits_undo(cx: &mut TestAppContext) {init_test(cx, |_| {});diff --git a/crates/project/src/lsp_store.rs b/crates/project/src/lsp_store.rsindex e7ac11e773b..666ba714f97 100644--- a/crates/project/src/lsp_store.rs+++ b/crates/project/src/lsp_store.rs@@ -7238,27 +7238,19 @@ impl LspStore {buffer.start_transaction();for (range, text) in edits {- let primary = &completion.replace_range;-- // Special case: if both ranges start at the very beginning of the file (line 0, column 0),- // and the primary completion is just an insertion (empty range), then this is likely- // an auto-import scenario and should not be considered overlapping- // https://github.com/zed-industries/zed/issues/26136- let is_file_start_auto_import = {- let snapshot = buffer.snapshot();- let primary_start_point = primary.start.to_point(&snapshot);- let range_start_point = range.start.to_point(&snapshot);-- let result = primary_start_point.row == 0- && primary_start_point.column == 0- && range_start_point.row == 0- && range_start_point.column == 0;-- result- };-- let has_overlap = if is_file_start_auto_import {- false+ // Zero-width additional edits (e.g. auto-imports at file start, or+ // rust-analyzer's ref-match `&` insertions) only overlap the primary+ // edit when they fall strictly inside it. Touching its boundary is fine.+ //+ // Ref: https://github.com/zed-industries/zed/issues/26136+ // Ref: https://github.com/zed-industries/zed/issues/56973+ let is_insertion = range.start.cmp(&range.end, buffer).is_eq();+ let has_overlap = if is_insertion {+ let insert_offset = range.start.to_offset(buffer);+ all_commit_ranges.iter().any(|commit_range| {+ commit_range.start.to_offset(buffer) < insert_offset+ && insert_offset < commit_range.end.to_offset(buffer)+ })} else {all_commit_ranges.iter().any(|commit_range| {let start_within =@@ -7271,8 +7263,8 @@ impl LspStore {})};- //Skip additional edits which overlap with the primary completion edit- //https://github.com/zed-industries/zed/pull/1871+ // Skip additional edits which overlap with the primary completion edit+ // https://github.com/zed-industries/zed/pull/1871if !has_overlap {buffer.edit([(range, text)], None, cx);}