]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_completion/src/render/pattern.rs
Replace some String usages with SmolStr in completions
[rust.git] / crates / ide_completion / src / render / pattern.rs
index 3717a0409a9ffbc753d04945b8400203c2207ab8..e553ec8f860d8544e9ef216492284d4fa5accb75 100644 (file)
@@ -3,8 +3,13 @@
 use hir::{db::HirDatabase, HasAttrs, HasVisibility, Name, StructKind};
 use ide_db::helpers::SnippetCap;
 use itertools::Itertools;
+use syntax::SmolStr;
 
-use crate::{item::CompletionKind, render::RenderContext, CompletionItem, CompletionItemKind};
+use crate::{
+    context::{ParamKind, PatternContext},
+    render::RenderContext,
+    CompletionItem, CompletionItemKind,
+};
 
 pub(crate) fn render_struct_pat(
     ctx: RenderContext<'_>,
@@ -21,7 +26,7 @@ pub(crate) fn render_struct_pat(
         return None;
     }
 
-    let name = local_name.unwrap_or_else(|| strukt.name(ctx.db())).to_string();
+    let name = local_name.unwrap_or_else(|| strukt.name(ctx.db())).to_smol_str();
     let pat = render_pat(&ctx, &name, strukt.kind(ctx.db()), &visible_fields, fields_omitted)?;
 
     Some(build_completion(ctx, name, pat, strukt))
@@ -39,8 +44,8 @@ pub(crate) fn render_variant_pat(
     let (visible_fields, fields_omitted) = visible_fields(&ctx, &fields, variant)?;
 
     let name = match &path {
-        Some(path) => path.to_string(),
-        None => local_name.unwrap_or_else(|| variant.name(ctx.db())).to_string(),
+        Some(path) => path.to_string().into(),
+        None => local_name.unwrap_or_else(|| variant.name(ctx.db())).to_smol_str(),
     };
     let pat = render_pat(&ctx, &name, variant.kind(ctx.db()), &visible_fields, fields_omitted)?;
 
@@ -49,19 +54,15 @@ pub(crate) fn render_variant_pat(
 
 fn build_completion(
     ctx: RenderContext<'_>,
-    name: String,
+    name: SmolStr,
     pat: String,
     def: impl HasAttrs + Copy,
 ) -> CompletionItem {
-    let mut item = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), name);
-    item.kind(CompletionItemKind::Binding)
-        .set_documentation(ctx.docs(def))
-        .set_deprecated(ctx.is_deprecated(def))
-        .detail(&pat);
-    if let Some(snippet_cap) = ctx.snippet_cap() {
-        item.insert_snippet(snippet_cap, pat);
-    } else {
-        item.insert_text(pat);
+    let mut item = CompletionItem::new(CompletionItemKind::Binding, ctx.source_range(), name);
+    item.set_documentation(ctx.docs(def)).set_deprecated(ctx.is_deprecated(def)).detail(&pat);
+    match ctx.snippet_cap() {
+        Some(snippet_cap) => item.insert_snippet(snippet_cap, pat),
+        None => item.insert_text(pat),
     };
     item.build()
 }
@@ -83,7 +84,10 @@ fn render_pat(
         _ => return None,
     };
 
-    if ctx.completion.is_param {
+    if matches!(
+        ctx.completion.pattern_ctx,
+        Some(PatternContext { is_param: Some(ParamKind::Function), .. })
+    ) {
         pat.push(':');
         pat.push(' ');
         pat.push_str(name);
@@ -139,7 +143,7 @@ fn visible_fields(
     let module = ctx.completion.scope.module()?;
     let n_fields = fields.len();
     let fields = fields
-        .into_iter()
+        .iter()
         .filter(|field| field.is_visible_from(ctx.db(), module))
         .copied()
         .collect::<Vec<_>>();