about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMorgan Thomas <corp@m0rg.dev>2022-03-12 04:40:05 -0800
committerMorgan Thomas <corp@m0rg.dev>2022-03-12 04:40:05 -0800
commitd430ddd809c7bb9721f4a5550cc1ff63eb700845 (patch)
treef64b94ad40495b5017649e7d5524aea5cc17bdb0
parentb3640ce424f5440190277df702ebe885827b128c (diff)
downloadrust-d430ddd809c7bb9721f4a5550cc1ff63eb700845.tar.gz
rust-d430ddd809c7bb9721f4a5550cc1ff63eb700845.zip
Extract the code for formatting struct and enum-variant literal labels out into a common function
-rw-r--r--crates/ide_completion/src/render/compound.rs12
-rw-r--r--crates/ide_completion/src/render/enum_variant.rs8
-rw-r--r--crates/ide_completion/src/render/struct_literal.rs9
3 files changed, 17 insertions, 12 deletions
diff --git a/crates/ide_completion/src/render/compound.rs b/crates/ide_completion/src/render/compound.rs
index 19bc53203a5..a1a199c6e2e 100644
--- a/crates/ide_completion/src/render/compound.rs
+++ b/crates/ide_completion/src/render/compound.rs
@@ -1,9 +1,10 @@
 //! Code common to structs, unions, and enum variants.
 
 use crate::render::RenderContext;
-use hir::{db::HirDatabase, HasAttrs, HasVisibility, HirDisplay};
+use hir::{db::HirDatabase, HasAttrs, HasVisibility, HirDisplay, StructKind};
 use ide_db::SnippetCap;
 use itertools::Itertools;
+use syntax::SmolStr;
 
 /// A rendered struct, union, or enum variant, split into fields for actual
 /// auto-completion (`literal`, using `field: ()`) and display in the
@@ -91,3 +92,12 @@ pub(crate) fn visible_fields(
         n_fields - fields.len() > 0 || item.attrs(ctx.db()).by_key("non_exhaustive").exists();
     Some((fields, fields_omitted))
 }
+
+/// Format a struct, etc. literal option for display in the completions menu.
+pub(crate) fn format_literal_label(name: &str, kind: StructKind) -> SmolStr {
+    match kind {
+        StructKind::Tuple => SmolStr::from_iter([name, "(…)"]),
+        StructKind::Record => SmolStr::from_iter([name, " {…}"]),
+        StructKind::Unit => name.into(),
+    }
+}
diff --git a/crates/ide_completion/src/render/enum_variant.rs b/crates/ide_completion/src/render/enum_variant.rs
index 6f358bfd4c9..5b485005d3f 100644
--- a/crates/ide_completion/src/render/enum_variant.rs
+++ b/crates/ide_completion/src/render/enum_variant.rs
@@ -7,7 +7,7 @@ use syntax::SmolStr;
 use crate::{
     item::{CompletionItem, ImportEdit},
     render::{
-        compound::{render_record, render_tuple, RenderedCompound},
+        compound::{format_literal_label, render_record, render_tuple, RenderedCompound},
         compute_ref_match, compute_type_match, RenderContext,
     },
     CompletionRelevance,
@@ -67,11 +67,7 @@ fn render(
     let mut item = CompletionItem::new(
         SymbolKind::Variant,
         ctx.source_range(),
-        match variant_kind {
-            StructKind::Tuple => SmolStr::from_iter([&qualified_name, "(…)"]),
-            StructKind::Record => SmolStr::from_iter([&qualified_name, " {…}"]),
-            StructKind::Unit => qualified_name.into(),
-        },
+        format_literal_label(&qualified_name, variant_kind),
     );
 
     item.set_documentation(variant.docs(db))
diff --git a/crates/ide_completion/src/render/struct_literal.rs b/crates/ide_completion/src/render/struct_literal.rs
index 124b465773a..a686be66913 100644
--- a/crates/ide_completion/src/render/struct_literal.rs
+++ b/crates/ide_completion/src/render/struct_literal.rs
@@ -4,7 +4,9 @@ use hir::{HasAttrs, Name, StructKind};
 use syntax::SmolStr;
 
 use crate::{
-    render::compound::{render_record, render_tuple, visible_fields, RenderedCompound},
+    render::compound::{
+        format_literal_label, render_record, render_tuple, visible_fields, RenderedCompound,
+    },
     render::RenderContext,
     CompletionItem, CompletionItemKind,
 };
@@ -42,10 +44,7 @@ fn build_completion(
     let mut item = CompletionItem::new(
         CompletionItemKind::Snippet,
         ctx.source_range(),
-        match kind {
-            StructKind::Tuple => SmolStr::from_iter([&name, "(…)"]),
-            _ => SmolStr::from_iter([&name, " {…}"]),
-        },
+        format_literal_label(&name, kind),
     );
 
     item.set_documentation(ctx.docs(def))