From 99c4a41cd1a9f9ef0ee4f067f0069a23123b949a Mon Sep 17 00:00:00 2001
From: yonip23 <yoni@codota.com>
Date: Thu, 11 Mar 2021 17:46:41 +0200
Subject: use references in CompletionItem's builder

---
 crates/ide_completion/src/render/builder_ext.rs  |  6 +++---
 crates/ide_completion/src/render/const_.rs       | 10 +++++-----
 crates/ide_completion/src/render/enum_variant.rs | 18 +++++++++---------
 crates/ide_completion/src/render/function.rs     | 12 +++++++++---
 crates/ide_completion/src/render/macro_.rs       | 23 +++++++++++++----------
 crates/ide_completion/src/render/pattern.rs      |  9 +++++----
 crates/ide_completion/src/render/type_alias.rs   | 10 +++++-----
 7 files changed, 49 insertions(+), 39 deletions(-)

(limited to 'crates/ide_completion/src/render')

diff --git a/crates/ide_completion/src/render/builder_ext.rs b/crates/ide_completion/src/render/builder_ext.rs
index 95a7596c1..6d062b3b9 100644
--- a/crates/ide_completion/src/render/builder_ext.rs
+++ b/crates/ide_completion/src/render/builder_ext.rs
@@ -52,11 +52,11 @@ impl Builder {
     }
 
     pub(super) fn add_call_parens(
-        mut self,
+        &mut self,
         ctx: &CompletionContext,
         name: String,
         params: Params,
-    ) -> Builder {
+    ) -> &mut Builder {
         if !self.should_add_parens(ctx) {
             return self;
         }
@@ -71,7 +71,7 @@ impl Builder {
         let (snippet, label) = if params.is_empty() {
             (format!("{}()$0", name), format!("{}()", name))
         } else {
-            self = self.trigger_call_info();
+            self.trigger_call_info();
             let snippet = match (ctx.config.add_call_argument_snippets, params) {
                 (true, Params::Named(params)) => {
                     let function_params_snippet =
diff --git a/crates/ide_completion/src/render/const_.rs b/crates/ide_completion/src/render/const_.rs
index 5010b642a..8add369e4 100644
--- a/crates/ide_completion/src/render/const_.rs
+++ b/crates/ide_completion/src/render/const_.rs
@@ -36,17 +36,17 @@ impl<'a> ConstRender<'a> {
         let name = self.name()?;
         let detail = self.detail();
 
-        let item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name)
-            .kind(SymbolKind::Const)
+        let mut item =
+            CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name);
+        item.kind(SymbolKind::Const)
             .set_documentation(self.ctx.docs(self.const_))
             .set_deprecated(
                 self.ctx.is_deprecated(self.const_)
                     || self.ctx.is_deprecated_assoc_item(self.const_),
             )
-            .detail(detail)
-            .build();
+            .detail(detail);
 
-        Some(item)
+        Some(item.build())
     }
 
     fn name(&self) -> Option<String> {
diff --git a/crates/ide_completion/src/render/enum_variant.rs b/crates/ide_completion/src/render/enum_variant.rs
index ed055c1fb..23c43496e 100644
--- a/crates/ide_completion/src/render/enum_variant.rs
+++ b/crates/ide_completion/src/render/enum_variant.rs
@@ -59,20 +59,20 @@ impl<'a> EnumRender<'a> {
             CompletionKind::Reference,
             self.ctx.source_range(),
             self.qualified_name.clone(),
-        )
-        .kind(SymbolKind::Variant)
-        .set_documentation(self.variant.docs(self.ctx.db()))
-        .set_deprecated(self.ctx.is_deprecated(self.variant))
-        .add_import(import_to_add)
-        .detail(self.detail());
+        );
+        builder
+            .kind(SymbolKind::Variant)
+            .set_documentation(self.variant.docs(self.ctx.db()))
+            .set_deprecated(self.ctx.is_deprecated(self.variant))
+            .add_import(import_to_add)
+            .detail(self.detail());
 
         if self.variant_kind == StructKind::Tuple {
             cov_mark::hit!(inserts_parens_for_tuple_enums);
             let params = Params::Anonymous(self.variant.fields(self.ctx.db()).len());
-            builder =
-                builder.add_call_parens(self.ctx.completion, self.short_qualified_name, params);
+            builder.add_call_parens(self.ctx.completion, self.short_qualified_name, params);
         } else if self.path.is_some() {
-            builder = builder.lookup_by(self.short_qualified_name);
+            builder.lookup_by(self.short_qualified_name);
         }
 
         builder.build()
diff --git a/crates/ide_completion/src/render/function.rs b/crates/ide_completion/src/render/function.rs
index 5931945a8..1e2f45f14 100644
--- a/crates/ide_completion/src/render/function.rs
+++ b/crates/ide_completion/src/render/function.rs
@@ -41,7 +41,12 @@ impl<'a> FunctionRender<'a> {
 
     fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem {
         let params = self.params();
-        CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone())
+        let mut builder = CompletionItem::new(
+            CompletionKind::Reference,
+            self.ctx.source_range(),
+            self.name.clone(),
+        );
+        builder
             .kind(self.kind())
             .set_documentation(self.ctx.docs(self.func))
             .set_deprecated(
@@ -49,8 +54,9 @@ impl<'a> FunctionRender<'a> {
             )
             .detail(self.detail())
             .add_call_parens(self.ctx.completion, self.name, params)
-            .add_import(import_to_add)
-            .build()
+            .add_import(import_to_add);
+
+        builder.build()
     }
 
     fn detail(&self) -> String {
diff --git a/crates/ide_completion/src/render/macro_.rs b/crates/ide_completion/src/render/macro_.rs
index a6cf3e479..727f5e02e 100644
--- a/crates/ide_completion/src/render/macro_.rs
+++ b/crates/ide_completion/src/render/macro_.rs
@@ -40,24 +40,27 @@ impl<'a> MacroRender<'a> {
 
     fn render(&self, import_to_add: Option<ImportEdit>) -> Option<CompletionItem> {
         let mut builder =
-            CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), &self.label())
-                .kind(SymbolKind::Macro)
-                .set_documentation(self.docs.clone())
-                .set_deprecated(self.ctx.is_deprecated(self.macro_))
-                .add_import(import_to_add)
-                .set_detail(self.detail());
+            CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), &self.label());
+        builder
+            .kind(SymbolKind::Macro)
+            .set_documentation(self.docs.clone())
+            .set_deprecated(self.ctx.is_deprecated(self.macro_))
+            .add_import(import_to_add)
+            .set_detail(self.detail());
 
         let needs_bang = self.needs_bang();
-        builder = match self.ctx.snippet_cap() {
+        match self.ctx.snippet_cap() {
             Some(cap) if needs_bang => {
                 let snippet = self.snippet();
                 let lookup = self.lookup();
-                builder.insert_snippet(cap, snippet).lookup_by(lookup)
+                builder.insert_snippet(cap, snippet).lookup_by(lookup);
+            }
+            None if needs_bang => {
+                builder.insert_text(self.banged_name());
             }
-            None if needs_bang => builder.insert_text(self.banged_name()),
             _ => {
                 cov_mark::hit!(dont_insert_macro_call_parens_unncessary);
-                builder.insert_text(&self.name)
+                builder.insert_text(&self.name);
             }
         };
 
diff --git a/crates/ide_completion/src/render/pattern.rs b/crates/ide_completion/src/render/pattern.rs
index 465dfe00c..4d8f2b17d 100644
--- a/crates/ide_completion/src/render/pattern.rs
+++ b/crates/ide_completion/src/render/pattern.rs
@@ -71,15 +71,16 @@ fn build_completion(
     pat: String,
     item: impl HasAttrs + Copy,
 ) -> CompletionItem {
-    let completion = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), name)
+    let mut completion = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), name);
+    completion
         .kind(CompletionItemKind::Binding)
         .set_documentation(ctx.docs(item))
         .set_deprecated(ctx.is_deprecated(item))
         .detail(&pat);
-    let completion = if let Some(snippet_cap) = ctx.snippet_cap() {
-        completion.insert_snippet(snippet_cap, pat)
+    if let Some(snippet_cap) = ctx.snippet_cap() {
+        completion.insert_snippet(snippet_cap, pat);
     } else {
-        completion.insert_text(pat)
+        completion.insert_text(pat);
     };
     completion.build()
 }
diff --git a/crates/ide_completion/src/render/type_alias.rs b/crates/ide_completion/src/render/type_alias.rs
index bd97c3692..e47b4c745 100644
--- a/crates/ide_completion/src/render/type_alias.rs
+++ b/crates/ide_completion/src/render/type_alias.rs
@@ -36,17 +36,17 @@ impl<'a> TypeAliasRender<'a> {
         let name = self.name()?;
         let detail = self.detail();
 
-        let item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name)
-            .kind(SymbolKind::TypeAlias)
+        let mut item =
+            CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name);
+        item.kind(SymbolKind::TypeAlias)
             .set_documentation(self.ctx.docs(self.type_alias))
             .set_deprecated(
                 self.ctx.is_deprecated(self.type_alias)
                     || self.ctx.is_deprecated_assoc_item(self.type_alias),
             )
-            .detail(detail)
-            .build();
+            .detail(detail);
 
-        Some(item)
+        Some(item.build())
     }
 
     fn name(&self) -> Option<String> {
-- 
cgit v1.2.3