Skip to content

Commit 062ac7a

Browse files
authored
Merge pull request #20760 from A4-Tacks/all-any-not-attr-comp
Add `all` `any` and `not` attribute completions
2 parents 4042662 + 6bb1b88 commit 062ac7a

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

crates/ide-completion/src/completions/attribute/cfg.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,33 @@ pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext<'_>) {
5353
acc.add(item.build(ctx.db));
5454
}),
5555
},
56-
None => ctx.krate.potential_cfg(ctx.db).get_cfg_keys().cloned().unique().for_each(|s| {
57-
let s = s.as_str();
58-
let item =
59-
CompletionItem::new(SymbolKind::BuiltinAttr, ctx.source_range(), s, ctx.edition);
60-
acc.add(item.build(ctx.db));
61-
}),
56+
None => ctx
57+
.krate
58+
.potential_cfg(ctx.db)
59+
.get_cfg_keys()
60+
.unique()
61+
.map(|s| (s.as_str(), ""))
62+
.chain(CFG_CONDITION.iter().copied())
63+
.for_each(|(s, snippet)| {
64+
let mut item = CompletionItem::new(
65+
SymbolKind::BuiltinAttr,
66+
ctx.source_range(),
67+
s,
68+
ctx.edition,
69+
);
70+
if let Some(cap) = ctx.config.snippet_cap
71+
&& !snippet.is_empty()
72+
{
73+
item.insert_snippet(cap, snippet);
74+
}
75+
acc.add(item.build(ctx.db));
76+
}),
6277
}
6378
}
6479

80+
const CFG_CONDITION: &[(&str, &str)] =
81+
&[("all", "all($0)"), ("any", "any($0)"), ("not", "not($0)")];
82+
6583
const KNOWN_ARCH: [&str; 20] = [
6684
"aarch64",
6785
"arm",

crates/ide-completion/src/tests/attribute.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,10 @@ mod cfg {
815815
#[cfg($0)]
816816
"#,
817817
expect![[r#"
818+
ba all
819+
ba any
818820
ba dbg
821+
ba not
819822
ba opt_level
820823
ba test
821824
ba true
@@ -827,7 +830,10 @@ mod cfg {
827830
#[cfg(b$0)]
828831
"#,
829832
expect![[r#"
833+
ba all
834+
ba any
830835
ba dbg
836+
ba not
831837
ba opt_level
832838
ba test
833839
ba true
@@ -843,7 +849,10 @@ mod cfg {
843849
#[cfg_attr($0)]
844850
"#,
845851
expect![[r#"
852+
ba all
853+
ba any
846854
ba dbg
855+
ba not
847856
ba opt_level
848857
ba test
849858
ba true
@@ -855,7 +864,10 @@ mod cfg {
855864
#[cfg_attr(b$0)]
856865
"#,
857866
expect![[r#"
867+
ba all
868+
ba any
858869
ba dbg
870+
ba not
859871
ba opt_level
860872
ba test
861873
ba true
@@ -867,7 +879,10 @@ mod cfg {
867879
#[cfg_attr($0, allow(deprecated))]
868880
"#,
869881
expect![[r#"
882+
ba all
883+
ba any
870884
ba dbg
885+
ba not
871886
ba opt_level
872887
ba test
873888
ba true
@@ -879,7 +894,10 @@ mod cfg {
879894
#[cfg_attr(b$0, allow(deprecated))]
880895
"#,
881896
expect![[r#"
897+
ba all
898+
ba any
882899
ba dbg
900+
ba not
883901
ba opt_level
884902
ba test
885903
ba true
@@ -904,6 +922,20 @@ mod cfg {
904922
"#]],
905923
);
906924
}
925+
926+
#[test]
927+
fn inside_conditional() {
928+
check_edit(
929+
"all",
930+
r#"
931+
//- /main.rs cfg:test,dbg=false,opt_level=2
932+
#[cfg($0)]
933+
"#,
934+
r#"
935+
#[cfg(all($0))]
936+
"#,
937+
);
938+
}
907939
}
908940

909941
mod derive {

0 commit comments

Comments
 (0)