diff options
| author | A4-Tacks <wdsjxhno1001@163.com> | 2025-09-28 08:41:15 +0800 |
|---|---|---|
| committer | A4-Tacks <wdsjxhno1001@163.com> | 2025-09-28 08:42:18 +0800 |
| commit | fdacc0cf5f11769427cb86c3133051314f9e8485 (patch) | |
| tree | e9a30bc7730a8cfc115311a35f7ccc1442a88936 /src/tools | |
| parent | 7de38d36eb9bf6bc607bbccc79efd85cde6ec00c (diff) | |
| download | rust-fdacc0cf5f11769427cb86c3133051314f9e8485.tar.gz rust-fdacc0cf5f11769427cb86c3133051314f9e8485.zip | |
Add `all` `any` and `not` attribute completions
Example --- `#[cfg($0)]` -> `#[cfg(any($0))]`
Diffstat (limited to 'src/tools')
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-completion/src/completions/attribute/cfg.rs | 30 | ||||
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-completion/src/tests/attribute.rs | 32 |
2 files changed, 56 insertions, 6 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/completions/attribute/cfg.rs b/src/tools/rust-analyzer/crates/ide-completion/src/completions/attribute/cfg.rs index 1676a8467c8..b2e8efde8be 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/completions/attribute/cfg.rs +++ b/src/tools/rust-analyzer/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/src/tools/rust-analyzer/crates/ide-completion/src/tests/attribute.rs b/src/tools/rust-analyzer/crates/ide-completion/src/tests/attribute.rs index 30e1e108c6c..1d2a9c7c8d3 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/tests/attribute.rs +++ b/src/tools/rust-analyzer/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 { |
