Repository: rust-analyzer/rust-analyzer
Pull request #6801 (2020-12-10 15:01:48)

Minor, more orthogonal code
bors r+
🤖


Patch:
diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs
index 3372fb1a21..544886d298 100644
--- a/crates/completion/src/completions/unqualified_path.rs
+++ b/crates/completion/src/completions/unqualified_path.rs
@@ -145,15 +145,13 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()
     })
     .filter(|(mod_path, _)| mod_path.len() > 1)
     .filter_map(|(import_path, definition)| {
-        render_resolution_with_import(
-            RenderContext::new(ctx),
-            ImportEdit {
-                import_path: import_path.clone(),
-                import_scope: import_scope.clone(),
-                merge_behavior: ctx.config.merge,
-            },
-            &definition,
-        )
+        let ie =
+            ImportEdit { import_path: import_path.clone(), import_scope: import_scope.clone() };
+        {
+            let _p = profile::span("totextedit");
+            ie.to_text_edit(ctx.config.merge);
+        }
+        render_resolution_with_import(RenderContext::new(ctx), ie, &definition)
     });
 
     acc.add_all(possible_imports);
diff --git a/crates/completion/src/item.rs b/crates/completion/src/item.rs
index 83166df4e9..65f8353e7c 100644
--- a/crates/completion/src/item.rs
+++ b/crates/completion/src/item.rs
@@ -271,19 +271,18 @@ impl CompletionItem {
 pub struct ImportEdit {
     pub import_path: ModPath,
     pub import_scope: ImportScope,
-    pub merge_behavior: Option<MergeBehavior>,
 }
 
 impl ImportEdit {
     /// Attempts to insert the import to the given scope, producing a text edit.
     /// May return no edit in edge cases, such as scope already containing the import.
-    pub fn to_text_edit(&self) -> Option<TextEdit> {
+    pub fn to_text_edit(&self, merge_behavior: Option<MergeBehavior>) -> Option<TextEdit> {
         let _p = profile::span("ImportEdit::to_text_edit");
 
         let rewriter = insert_use::insert_use(
             &self.import_scope,
             mod_path_to_ast(&self.import_path),
-            self.merge_behavior,
+            merge_behavior,
         );
         let old_ast = rewriter.rewrite_root()?;
         let mut import_insert = TextEdit::builder();
diff --git a/crates/completion/src/lib.rs b/crates/completion/src/lib.rs
index 6a4b3c1670..8e27bb1532 100644
--- a/crates/completion/src/lib.rs
+++ b/crates/completion/src/lib.rs
@@ -153,9 +153,7 @@ pub fn resolve_completion_edits(
         })
         .find(|mod_path| mod_path.to_string() == full_import_path)?;
 
-    ImportEdit { import_path, import_scope, merge_behavior: config.merge }
-        .to_text_edit()
-        .map(|edit| vec![edit])
+    ImportEdit { import_path, import_scope }.to_text_edit(config.merge).map(|edit| vec![edit])
 }
 
 #[cfg(test)]


Test patch:
diff --git a/crates/completion/src/test_utils.rs b/crates/completion/src/test_utils.rs
index 25f5f4924c..db896b2df2 100644
--- a/crates/completion/src/test_utils.rs
+++ b/crates/completion/src/test_utils.rs
@@ -98,7 +98,8 @@ pub(crate) fn check_edit_with_config(
     let mut actual = db.file_text(position.file_id).to_string();
 
     let mut combined_edit = completion.text_edit().to_owned();
-    if let Some(import_text_edit) = completion.import_to_add().and_then(|edit| edit.to_text_edit())
+    if let Some(import_text_edit) =
+        completion.import_to_add().and_then(|edit| edit.to_text_edit(config.merge))
     {
         combined_edit.union(import_text_edit).expect(
             "Failed to apply completion resolve changes: change ranges overlap, but should not",