use hir::{db::HirDatabase, HasAttrs, HasVisibility, Name, StructKind};
use ide_db::helpers::SnippetCap;
use itertools::Itertools;
+use syntax::SmolStr;
use crate::{render::RenderContext, CompletionItem, CompletionItemKind};
pub(crate) fn render_struct_literal(
ctx: RenderContext<'_>,
strukt: hir::Struct,
+ path: Option<hir::ModPath>,
local_name: Option<Name>,
) -> Option<CompletionItem> {
let _p = profile::span("render_struct_literal");
return None;
}
- let name = local_name.unwrap_or_else(|| strukt.name(ctx.db())).to_string();
- let literal = render_literal(&ctx, &name, strukt.kind(ctx.db()), &visible_fields)?;
+ let name = local_name.unwrap_or_else(|| strukt.name(ctx.db())).to_smol_str();
+
+ let literal = render_literal(&ctx, path, &name, strukt.kind(ctx.db()), &visible_fields)?;
Some(build_completion(ctx, name, literal, strukt))
}
fn build_completion(
ctx: RenderContext<'_>,
- name: String,
+ name: SmolStr,
literal: String,
def: impl HasAttrs + Copy,
) -> CompletionItem {
- let mut item =
- CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), name + " {…}");
+ let mut item = CompletionItem::new(
+ CompletionItemKind::Snippet,
+ ctx.source_range(),
+ SmolStr::from_iter([&name, " {…}"]),
+ );
item.set_documentation(ctx.docs(def)).set_deprecated(ctx.is_deprecated(def)).detail(&literal);
match ctx.snippet_cap() {
Some(snippet_cap) => item.insert_snippet(snippet_cap, literal),
fn render_literal(
ctx: &RenderContext<'_>,
+ path: Option<hir::ModPath>,
name: &str,
kind: StructKind,
fields: &[hir::Field],
) -> Option<String> {
+ let path_string;
+
+ let qualified_name = if let Some(path) = path {
+ path_string = path.to_string();
+ &path_string
+ } else {
+ name
+ };
+
let mut literal = match kind {
- StructKind::Tuple if ctx.snippet_cap().is_some() => render_tuple_as_literal(fields, name),
- StructKind::Record => render_record_as_literal(ctx.db(), ctx.snippet_cap(), fields, name),
+ StructKind::Tuple if ctx.snippet_cap().is_some() => {
+ render_tuple_as_literal(fields, qualified_name)
+ }
+ StructKind::Record => {
+ render_record_as_literal(ctx.db(), ctx.snippet_cap(), fields, qualified_name)
+ }
_ => return None,
};
fields: &[hir::Field],
item: impl HasAttrs,
) -> Option<(Vec<hir::Field>, bool)> {
- let module = ctx.completion.scope.module()?;
+ let module = ctx.completion.module?;
let n_fields = fields.len();
let fields = fields
.iter()