mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 14:51:48 +00:00
Simplify completion render functionality
This commit is contained in:
parent
4fe5f03c7f
commit
02b401b130
14 changed files with 490 additions and 345 deletions
|
@ -1,6 +1,6 @@
|
|||
//! Code common to structs, unions, and enum variants.
|
||||
|
||||
use crate::render::RenderContext;
|
||||
use crate::context::CompletionContext;
|
||||
use hir::{db::HirDatabase, HasAttrs, HasVisibility, HirDisplay, StructKind};
|
||||
use ide_db::SnippetCap;
|
||||
use itertools::Itertools;
|
||||
|
@ -16,11 +16,11 @@ pub(crate) struct RenderedLiteral {
|
|||
|
||||
/// Render a record type (or sub-type) to a `RenderedCompound`. Use `None` for
|
||||
/// the `name` argument for an anonymous type.
|
||||
pub(crate) fn render_record(
|
||||
pub(crate) fn render_record_lit(
|
||||
db: &dyn HirDatabase,
|
||||
snippet_cap: Option<SnippetCap>,
|
||||
fields: &[hir::Field],
|
||||
name: Option<&str>,
|
||||
path: &str,
|
||||
) -> RenderedLiteral {
|
||||
let completions = fields.iter().enumerate().format_with(", ", |(idx, field), f| {
|
||||
if snippet_cap.is_some() {
|
||||
|
@ -35,18 +35,18 @@ pub(crate) fn render_record(
|
|||
});
|
||||
|
||||
RenderedLiteral {
|
||||
literal: format!("{} {{ {} }}", name.unwrap_or(""), completions),
|
||||
detail: format!("{} {{ {} }}", name.unwrap_or(""), types),
|
||||
literal: format!("{} {{ {} }}", path, completions),
|
||||
detail: format!("{} {{ {} }}", path, types),
|
||||
}
|
||||
}
|
||||
|
||||
/// Render a tuple type (or sub-type) to a `RenderedCompound`. Use `None` for
|
||||
/// the `name` argument for an anonymous type.
|
||||
pub(crate) fn render_tuple(
|
||||
pub(crate) fn render_tuple_lit(
|
||||
db: &dyn HirDatabase,
|
||||
snippet_cap: Option<SnippetCap>,
|
||||
fields: &[hir::Field],
|
||||
name: Option<&str>,
|
||||
path: &str,
|
||||
) -> RenderedLiteral {
|
||||
let completions = fields.iter().enumerate().format_with(", ", |(idx, _), f| {
|
||||
if snippet_cap.is_some() {
|
||||
|
@ -59,8 +59,8 @@ pub(crate) fn render_tuple(
|
|||
let types = fields.iter().format_with(", ", |field, f| f(&field.ty(db).display(db)));
|
||||
|
||||
RenderedLiteral {
|
||||
literal: format!("{}({})", name.unwrap_or(""), completions),
|
||||
detail: format!("{}({})", name.unwrap_or(""), types),
|
||||
literal: format!("{}({})", path, completions),
|
||||
detail: format!("{}({})", path, types),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -68,20 +68,20 @@ pub(crate) fn render_tuple(
|
|||
/// fields, plus a boolean for whether the list is comprehensive (contains no
|
||||
/// private fields and its item is not marked `#[non_exhaustive]`).
|
||||
pub(crate) fn visible_fields(
|
||||
ctx: &RenderContext<'_>,
|
||||
ctx: &CompletionContext,
|
||||
fields: &[hir::Field],
|
||||
item: impl HasAttrs,
|
||||
) -> Option<(Vec<hir::Field>, bool)> {
|
||||
let module = ctx.completion.module?;
|
||||
let module = ctx.module?;
|
||||
let n_fields = fields.len();
|
||||
let fields = fields
|
||||
.iter()
|
||||
.filter(|field| field.is_visible_from(ctx.db(), module))
|
||||
.filter(|field| field.is_visible_from(ctx.db, module))
|
||||
.copied()
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let fields_omitted =
|
||||
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))
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue