Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #20760 from A4-Tacks/all-any-not-attr-comp
Add `all` `any` and `not` attribute completions
Shoyu Vanilla (Flint) 7 months ago
parent 4042662 · parent 6bb1b88 · commit 062ac7a
-rw-r--r--crates/ide-completion/src/completions/attribute/cfg.rs30
-rw-r--r--crates/ide-completion/src/tests/attribute.rs32
2 files changed, 56 insertions, 6 deletions
diff --git a/crates/ide-completion/src/completions/attribute/cfg.rs b/crates/ide-completion/src/completions/attribute/cfg.rs
index 1676a8467c..b2e8efde8b 100644
--- a/crates/ide-completion/src/completions/attribute/cfg.rs
+++ b/crates/ide-completion/src/completions/attribute/cfg.rs
@@ -53,15 +53,33 @@ 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)
+ .get_cfg_keys()
+ .unique()
+ .map(|s| (s.as_str(), ""))
+ .chain(CFG_CONDITION.iter().copied())
+ .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",
diff --git a/crates/ide-completion/src/tests/attribute.rs b/crates/ide-completion/src/tests/attribute.rs
index 30e1e108c6..1d2a9c7c8d 100644
--- a/crates/ide-completion/src/tests/attribute.rs
+++ b/crates/ide-completion/src/tests/attribute.rs
@@ -815,7 +815,10 @@ mod cfg {
#[cfg($0)]
"#,
expect![[r#"
+ ba all
+ ba any
ba dbg
+ ba not
ba opt_level
ba test
ba true
@@ -827,7 +830,10 @@ mod cfg {
#[cfg(b$0)]
"#,
expect![[r#"
+ ba all
+ ba any
ba dbg
+ ba not
ba opt_level
ba test
ba true
@@ -843,7 +849,10 @@ mod cfg {
#[cfg_attr($0)]
"#,
expect![[r#"
+ ba all
+ ba any
ba dbg
+ ba not
ba opt_level
ba test
ba true
@@ -855,7 +864,10 @@ mod cfg {
#[cfg_attr(b$0)]
"#,
expect![[r#"
+ ba all
+ ba any
ba dbg
+ ba not
ba opt_level
ba test
ba true
@@ -867,7 +879,10 @@ mod cfg {
#[cfg_attr($0, allow(deprecated))]
"#,
expect![[r#"
+ ba all
+ ba any
ba dbg
+ ba not
ba opt_level
ba test
ba true
@@ -879,7 +894,10 @@ mod cfg {
#[cfg_attr(b$0, allow(deprecated))]
"#,
expect![[r#"
+ ba all
+ ba any
ba dbg
+ ba not
ba opt_level
ba test
ba true
@@ -904,6 +922,20 @@ mod cfg {
"#]],
);
}
+
+ #[test]
+ fn inside_conditional() {
+ check_edit(
+ "all",
+ r#"
+//- /main.rs cfg:test,dbg=false,opt_level=2
+#[cfg($0)]
+"#,
+ r#"
+#[cfg(all($0))]
+"#,
+ );
+ }
}
mod derive {