mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 21:13:37 +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.
|
//! Code common to structs, unions, and enum variants.
|
||||||
|
|
||||||
use crate::render::RenderContext;
|
use crate::render::RenderContext;
|
||||||
use hir::{db::HirDatabase, HasAttrs, HasVisibility, HirDisplay};
|
use hir::{db::HirDatabase, HasAttrs, HasVisibility, HirDisplay, StructKind};
|
||||||
use ide_db::SnippetCap;
|
use ide_db::SnippetCap;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
use syntax::SmolStr;
|
||||||
|
|
||||||
/// A rendered struct, union, or enum variant, split into fields for actual
|
/// A rendered struct, union, or enum variant, split into fields for actual
|
||||||
/// auto-completion (`literal`, using `field: ()`) and display in the
|
/// 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();
|
n_fields - fields.len() > 0 || item.attrs(ctx.db()).by_key("non_exhaustive").exists();
|
||||||
Some((fields, fields_omitted))
|
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::{
|
use crate::{
|
||||||
item::{CompletionItem, ImportEdit},
|
item::{CompletionItem, ImportEdit},
|
||||||
render::{
|
render::{
|
||||||
compound::{render_record, render_tuple, RenderedCompound},
|
compound::{format_literal_label, render_record, render_tuple, RenderedCompound},
|
||||||
compute_ref_match, compute_type_match, RenderContext,
|
compute_ref_match, compute_type_match, RenderContext,
|
||||||
},
|
},
|
||||||
CompletionRelevance,
|
CompletionRelevance,
|
||||||
|
@ -67,11 +67,7 @@ fn render(
|
||||||
let mut item = CompletionItem::new(
|
let mut item = CompletionItem::new(
|
||||||
SymbolKind::Variant,
|
SymbolKind::Variant,
|
||||||
ctx.source_range(),
|
ctx.source_range(),
|
||||||
match variant_kind {
|
format_literal_label(&qualified_name, variant_kind),
|
||||||
StructKind::Tuple => SmolStr::from_iter([&qualified_name, "(…)"]),
|
|
||||||
StructKind::Record => SmolStr::from_iter([&qualified_name, " {…}"]),
|
|
||||||
StructKind::Unit => qualified_name.into(),
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
|
|
||||||
item.set_documentation(variant.docs(db))
|
item.set_documentation(variant.docs(db))
|
||||||
|
|
|
@ -4,7 +4,9 @@ use hir::{HasAttrs, Name, StructKind};
|
||||||
use syntax::SmolStr;
|
use syntax::SmolStr;
|
||||||
|
|
||||||
use crate::{
|
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,
|
render::RenderContext,
|
||||||
CompletionItem, CompletionItemKind,
|
CompletionItem, CompletionItemKind,
|
||||||
};
|
};
|
||||||
|
@ -42,10 +44,7 @@ fn build_completion(
|
||||||
let mut item = CompletionItem::new(
|
let mut item = CompletionItem::new(
|
||||||
CompletionItemKind::Snippet,
|
CompletionItemKind::Snippet,
|
||||||
ctx.source_range(),
|
ctx.source_range(),
|
||||||
match kind {
|
format_literal_label(&name, kind),
|
||||||
StructKind::Tuple => SmolStr::from_iter([&name, "(…)"]),
|
|
||||||
_ => SmolStr::from_iter([&name, " {…}"]),
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
|
|
||||||
item.set_documentation(ctx.docs(def))
|
item.set_documentation(ctx.docs(def))
|
||||||
|
|
Loading…
Reference in a new issue