]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_completion/src/completions/attribute/cfg.rs
Replace some String usages with SmolStr in completions
[rust.git] / crates / ide_completion / src / completions / attribute / cfg.rs
index 847e6529a1bf272fcd386c10a6914aada06d8535..1a10531a1bd61665ba4a45b60ac294b3e34a26b4 100644 (file)
@@ -5,16 +5,14 @@
 use syntax::SyntaxKind;
 
 use crate::{
-    completions::Completions, context::CompletionContext, item::CompletionKind, CompletionItem,
-    CompletionItemKind,
+    completions::Completions, context::CompletionContext, CompletionItem, CompletionItemKind,
 };
 
 pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) {
-    let add_completion = |item: &&str| {
+    let add_completion = |item: &str| {
         let mut completion =
-            CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), *item);
+            CompletionItem::new(CompletionItemKind::Attribute, ctx.source_range(), item);
         completion.insert_text(format!(r#""{}""#, item));
-        completion.kind(CompletionItemKind::Attribute);
         acc.add(completion.build());
     };
 
@@ -26,41 +24,36 @@ pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) {
     .find(|t| matches!(t.kind(), SyntaxKind::IDENT));
 
     match previous.as_ref().map(|p| p.text()) {
-        Some("target_arch") => KNOWN_ARCH.iter().for_each(add_completion),
-        Some("target_env") => KNOWN_ENV.iter().for_each(add_completion),
-        Some("target_os") => KNOWN_OS.iter().for_each(add_completion),
-        Some("target_vendor") => KNOWN_VENDOR.iter().for_each(add_completion),
-        Some("target_endian") => ["little", "big"].iter().for_each(add_completion),
+        Some("target_arch") => KNOWN_ARCH.iter().copied().for_each(add_completion),
+        Some("target_env") => KNOWN_ENV.iter().copied().for_each(add_completion),
+        Some("target_os") => KNOWN_OS.iter().copied().for_each(add_completion),
+        Some("target_vendor") => KNOWN_VENDOR.iter().copied().for_each(add_completion),
+        Some("target_endian") => ["little", "big"].into_iter().for_each(add_completion),
         Some(name) => {
-            ctx.krate.map(|krate| {
-                krate.potential_cfg(ctx.db).get_cfg_values(&name).iter().for_each(|s| {
-                    let mut item = CompletionItem::new(
-                        CompletionKind::Attribute,
-                        ctx.source_range(),
-                        s.as_str(),
-                    );
-                    item.insert_text(format!(r#""{}""#, s));
+            if let Some(krate) = ctx.krate {
+                krate.potential_cfg(ctx.db).get_cfg_values(&name).cloned().for_each(|s| {
+                    let insert_text = format!(r#""{}""#, s);
+                    let mut item =
+                        CompletionItem::new(CompletionItemKind::Attribute, ctx.source_range(), s);
+                    item.insert_text(insert_text);
 
                     acc.add(item.build());
                 })
-            });
+            };
         }
         None => {
-            ctx.krate.map(|krate| {
-                krate.potential_cfg(ctx.db).get_cfg_keys().iter().for_each(|s| {
-                    let item = CompletionItem::new(
-                        CompletionKind::Attribute,
-                        ctx.source_range(),
-                        s.as_str(),
-                    );
+            if let Some(krate) = ctx.krate {
+                krate.potential_cfg(ctx.db).get_cfg_keys().cloned().for_each(|s| {
+                    let item =
+                        CompletionItem::new(CompletionItemKind::Attribute, ctx.source_range(), s);
                     acc.add(item.build());
                 })
-            });
+            }
         }
     };
 }
 
-const KNOWN_ARCH: [&'static str; 19] = [
+const KNOWN_ARCH: [&str; 19] = [
     "aarch64",
     "arm",
     "avr",
@@ -82,10 +75,9 @@ pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) {
     "x86_64",
 ];
 
-const KNOWN_ENV: [&'static str; 7] =
-    ["eabihf", "gnu", "gnueabihf", "msvc", "relibc", "sgx", "uclibc"];
+const KNOWN_ENV: [&str; 7] = ["eabihf", "gnu", "gnueabihf", "msvc", "relibc", "sgx", "uclibc"];
 
-const KNOWN_OS: [&'static str; 20] = [
+const KNOWN_OS: [&str; 20] = [
     "cuda",
     "dragonfly",
     "emscripten",
@@ -108,5 +100,5 @@ pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) {
     "windows",
 ];
 
-const KNOWN_VENDOR: [&'static str; 8] =
+const KNOWN_VENDOR: [&str; 8] =
     ["apple", "fortanix", "nvidia", "pc", "sony", "unknown", "wrs", "uwp"];