Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-completion/src/completions/attribute/cfg.rs')
-rw-r--r--crates/ide-completion/src/completions/attribute/cfg.rs44
1 files changed, 36 insertions, 8 deletions
diff --git a/crates/ide-completion/src/completions/attribute/cfg.rs b/crates/ide-completion/src/completions/attribute/cfg.rs
index 1676a8467c..0d36fb7a40 100644
--- a/crates/ide-completion/src/completions/attribute/cfg.rs
+++ b/crates/ide-completion/src/completions/attribute/cfg.rs
@@ -2,7 +2,7 @@
use ide_db::SymbolKind;
use itertools::Itertools;
-use syntax::{AstToken, Direction, NodeOrToken, SyntaxKind, algo, ast::Ident};
+use syntax::{AstToken, Direction, NodeOrToken, SmolStr, SyntaxKind, algo, ast::Ident};
use crate::{CompletionItem, completions::Completions, context::CompletionContext};
@@ -39,7 +39,7 @@ pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext<'_>) {
"target_os" => KNOWN_OS.iter().copied().for_each(add_completion),
"target_vendor" => KNOWN_VENDOR.iter().copied().for_each(add_completion),
"target_endian" => ["little", "big"].into_iter().for_each(add_completion),
- name => ctx.krate.potential_cfg(ctx.db).get_cfg_values(name).cloned().for_each(|s| {
+ name => ctx.krate.potential_cfg(ctx.db).get_cfg_values(name).for_each(|s| {
let s = s.as_str();
let insert_text = format!(r#""{s}""#);
let mut item = CompletionItem::new(
@@ -53,15 +53,43 @@ pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext<'_>) {
acc.add(item.build(ctx.db));
}),
},
- None => ctx.krate.potential_cfg(ctx.db).get_cfg_keys().cloned().unique().for_each(|s| {
- let s = s.as_str();
- let item =
- CompletionItem::new(SymbolKind::BuiltinAttr, ctx.source_range(), s, ctx.edition);
- acc.add(item.build(ctx.db));
- }),
+ None => ctx
+ .krate
+ .potential_cfg(ctx.db)
+ .into_iter()
+ .map(|x| match x {
+ hir::CfgAtom::Flag(key) => (key.as_str(), "".into()),
+ hir::CfgAtom::KeyValue { key, .. } => (
+ key.as_str(),
+ if ctx.config.snippet_cap.is_some() {
+ SmolStr::from_iter([key.as_str(), " = $0"])
+ } else {
+ SmolStr::default()
+ },
+ ),
+ })
+ .chain(CFG_CONDITION.iter().map(|&(k, snip)| (k, SmolStr::new_static(snip))))
+ .unique_by(|&(s, _)| s)
+ .for_each(|(s, snippet)| {
+ let mut item = CompletionItem::new(
+ SymbolKind::BuiltinAttr,
+ ctx.source_range(),
+ s,
+ ctx.edition,
+ );
+ if let Some(cap) = ctx.config.snippet_cap
+ && !snippet.is_empty()
+ {
+ item.insert_snippet(cap, snippet);
+ }
+ acc.add(item.build(ctx.db));
+ }),
}
}
+const CFG_CONDITION: &[(&str, &str)] =
+ &[("all", "all($0)"), ("any", "any($0)"), ("not", "not($0)")];
+
const KNOWN_ARCH: [&str; 20] = [
"aarch64",
"arm",