mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 04:53:34 +00:00
Extract the code for formatting struct and enum-variant literal labels out into a common function
This commit is contained in:
parent
b3640ce424
commit
d430ddd809
3 changed files with 17 additions and 12 deletions
|
@ -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(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue