Merge pull request #21083 from A4-Tacks/cfg-pred-keyval-eq

feat: Completion ` = $0` after keyval cfg predicate
This commit is contained in:
Lukas Wirth 2025-11-23 12:33:05 +00:00 committed by GitHub
commit fa1e48b2d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 38 additions and 5 deletions

View file

@ -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};
@ -56,10 +56,15 @@ pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext<'_>) {
None => ctx
.krate
.potential_cfg(ctx.db)
.get_cfg_keys()
.unique()
.map(|s| (s.as_str(), ""))
.chain(CFG_CONDITION.iter().copied())
.into_iter()
.map(|x| match x {
hir::CfgAtom::Flag(key) => (key.as_str(), "".into()),
hir::CfgAtom::KeyValue { key, .. } => {
(key.as_str(), SmolStr::from_iter([key.as_str(), " = $0"]))
}
})
.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,

View file

@ -982,6 +982,34 @@ mod cfg {
);
}
#[test]
fn complete_key_attr() {
check_edit(
"test",
r#"
//- /main.rs cfg:test,dbg=false,opt_level=2
#[cfg($0)]
"#,
r#"
#[cfg(test)]
"#,
);
}
#[test]
fn complete_key_value_attr() {
check_edit(
"opt_level",
r#"
//- /main.rs cfg:test,dbg=false,opt_level=2
#[cfg($0)]
"#,
r#"
#[cfg(opt_level = $0)]
"#,
);
}
#[test]
fn cfg_target_endian() {
check(