Unnamed repository; edit this file 'description' to name the repository.
Extract the code for formatting struct and enum-variant literal labels out into a common function
Morgan Thomas 2022-03-12
parent b3640ce · commit d430ddd
-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 19bc53203a..a1a199c6e2 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 6f358bfd4c..5b485005d3 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 124b465773..a686be6691 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))