Skip to content

Commit ca1bde3

Browse files
committed
Completion = $0 after keyval cfg predicate
Example --- ```rust //- /main.rs cfg:test,dbg=false,opt_level=2 #[cfg($0)] ``` **Before this PR** ```rust #[cfg(opt_level)] ``` **After this PR** ```rust #[cfg(opt_level = $0)] ```
1 parent 509b181 commit ca1bde3

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use ide_db::SymbolKind;
44
use itertools::Itertools;
5-
use syntax::{AstToken, Direction, NodeOrToken, SyntaxKind, algo, ast::Ident};
5+
use syntax::{AstToken, Direction, NodeOrToken, SmolStr, SyntaxKind, algo, ast::Ident};
66

77
use crate::{CompletionItem, completions::Completions, context::CompletionContext};
88

@@ -56,10 +56,15 @@ pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext<'_>) {
5656
None => ctx
5757
.krate
5858
.potential_cfg(ctx.db)
59-
.get_cfg_keys()
60-
.unique()
61-
.map(|s| (s.as_str(), ""))
62-
.chain(CFG_CONDITION.iter().copied())
59+
.into_iter()
60+
.map(|x| match x {
61+
hir::CfgAtom::Flag(key) => (key.as_str(), "".into()),
62+
hir::CfgAtom::KeyValue { key, .. } => {
63+
(key.as_str(), SmolStr::from_iter([key.as_str(), " = $0"]))
64+
}
65+
})
66+
.chain(CFG_CONDITION.iter().map(|&(k, snip)| (k, SmolStr::new_static(&snip))))
67+
.unique_by(|&(s, _)| s)
6368
.for_each(|(s, snippet)| {
6469
let mut item = CompletionItem::new(
6570
SymbolKind::BuiltinAttr,

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,34 @@ mod cfg {
982982
);
983983
}
984984

985+
#[test]
986+
fn complete_key_attr() {
987+
check_edit(
988+
"test",
989+
r#"
990+
//- /main.rs cfg:test,dbg=false,opt_level=2
991+
#[cfg($0)]
992+
"#,
993+
r#"
994+
#[cfg(test)]
995+
"#,
996+
);
997+
}
998+
999+
#[test]
1000+
fn complete_key_value_attr() {
1001+
check_edit(
1002+
"opt_level",
1003+
r#"
1004+
//- /main.rs cfg:test,dbg=false,opt_level=2
1005+
#[cfg($0)]
1006+
"#,
1007+
r#"
1008+
#[cfg(opt_level = $0)]
1009+
"#,
1010+
);
1011+
}
1012+
9851013
#[test]
9861014
fn cfg_target_endian() {
9871015
check(

0 commit comments

Comments
 (0)