From 284483b347d15bee3a7bf293d33e5f19a9740102 Mon Sep 17 00:00:00 2001 From: Jamie Cunliffe Date: Sun, 30 May 2021 14:52:19 +0100 Subject: Improve completion of cfg attributes The completion of cfg will look at the enabled cfg keys when performing completion. It will also look crate features when completing a feature cfg option. A fixed list of known values for some cfg options are provided. For unknown keys it will look at the enabled values for that cfg key, which means that completion will only show enabled options for those. --- crates/base_db/src/fixture.rs | 2 + crates/base_db/src/input.rs | 13 +++ crates/cfg/src/lib.rs | 20 ++++ crates/hir/src/lib.rs | 4 + crates/ide/src/lib.rs | 1 + crates/ide_completion/src/completions/attribute.rs | 15 +++ .../src/completions/attribute/cfg.rs | 126 +++++++++++++++++++++ crates/project_model/src/workspace.rs | 4 + 8 files changed, 185 insertions(+) create mode 100644 crates/ide_completion/src/completions/attribute/cfg.rs diff --git a/crates/base_db/src/fixture.rs b/crates/base_db/src/fixture.rs index 6ce377710..6d3b1266e 100644 --- a/crates/base_db/src/fixture.rs +++ b/crates/base_db/src/fixture.rs @@ -131,6 +131,7 @@ impl ChangeFixture { meta.cfg, meta.env, Default::default(), + Default::default(), ); let prev = crates.insert(crate_name.clone(), crate_id); assert!(prev.is_none()); @@ -160,6 +161,7 @@ impl ChangeFixture { default_cfg, Env::default(), Default::default(), + Default::default(), ); } else { for (from, to) in crate_deps { diff --git a/crates/base_db/src/input.rs b/crates/base_db/src/input.rs index 23cb0c839..d99388f71 100644 --- a/crates/base_db/src/input.rs +++ b/crates/base_db/src/input.rs @@ -192,6 +192,7 @@ pub struct CrateData { pub env: Env, pub dependencies: Vec, pub proc_macro: Vec, + pub features: FxHashMap>, } #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -221,6 +222,7 @@ impl CrateGraph { cfg_options: CfgOptions, env: Env, proc_macro: Vec, + features: FxHashMap>, ) -> CrateId { let data = CrateData { root_file_id: file_id, @@ -230,6 +232,7 @@ impl CrateGraph { env, proc_macro, dependencies: Vec::new(), + features, }; let crate_id = CrateId(self.arena.len() as u32); let prev = self.arena.insert(crate_id, data); @@ -506,6 +509,7 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), + Default::default(), ); let crate2 = graph.add_crate_root( FileId(2u32), @@ -514,6 +518,7 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), + Default::default(), ); let crate3 = graph.add_crate_root( FileId(3u32), @@ -522,6 +527,7 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), + Default::default(), ); assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); @@ -538,6 +544,7 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), + Default::default(), ); let crate2 = graph.add_crate_root( FileId(2u32), @@ -546,6 +553,7 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), + Default::default(), ); assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); assert!(graph.add_dep(crate2, CrateName::new("crate2").unwrap(), crate2).is_err()); @@ -561,6 +569,7 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), + Default::default(), ); let crate2 = graph.add_crate_root( FileId(2u32), @@ -569,6 +578,7 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), + Default::default(), ); let crate3 = graph.add_crate_root( FileId(3u32), @@ -577,6 +587,7 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), + Default::default(), ); assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); @@ -592,6 +603,7 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), + Default::default(), ); let crate2 = graph.add_crate_root( FileId(2u32), @@ -600,6 +612,7 @@ mod tests { CfgOptions::default(), Env::default(), Default::default(), + Default::default(), ); assert!(graph .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) diff --git a/crates/cfg/src/lib.rs b/crates/cfg/src/lib.rs index 916d39a0b..9a4baa636 100644 --- a/crates/cfg/src/lib.rs +++ b/crates/cfg/src/lib.rs @@ -50,6 +50,26 @@ impl CfgOptions { self.enabled.remove(&atom); } } + + pub fn get_cfg_keys(&self) -> Vec<&SmolStr> { + self.enabled + .iter() + .map(|x| match x { + CfgAtom::Flag(key) => key, + CfgAtom::KeyValue { key, .. } => key, + }) + .collect() + } + + pub fn get_cfg_values(&self, cfg_key: &str) -> Vec<&SmolStr> { + self.enabled + .iter() + .filter_map(|x| match x { + CfgAtom::KeyValue { key, value } if cfg_key == key => Some(value), + _ => None, + }) + .collect() + } } #[derive(Clone, Debug, PartialEq, Eq)] diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 88490fea9..2b2aaec94 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -233,6 +233,10 @@ impl Crate { pub fn cfg(&self, db: &dyn HirDatabase) -> CfgOptions { db.crate_graph()[self.id].cfg_options.clone() } + + pub fn features(&self, db: &dyn HirDatabase) -> Vec { + db.crate_graph()[self.id].features.iter().map(|(feat, _)| feat.clone()).collect() + } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 3798f32cc..0693869a2 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -220,6 +220,7 @@ impl Analysis { cfg_options, Env::default(), Default::default(), + Default::default(), ); change.change_file(file_id, Some(Arc::new(text))); change.set_crate_graph(crate_graph); diff --git a/crates/ide_completion/src/completions/attribute.rs b/crates/ide_completion/src/completions/attribute.rs index 78fc30e16..cc4f4b2af 100644 --- a/crates/ide_completion/src/completions/attribute.rs +++ b/crates/ide_completion/src/completions/attribute.rs @@ -15,6 +15,7 @@ use crate::{ Completions, }; +mod cfg; mod derive; mod lint; mod repr; @@ -30,6 +31,9 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) lint::complete_lint(acc, ctx, token_tree.clone(), DEFAULT_LINTS); lint::complete_lint(acc, ctx, token_tree, CLIPPY_LINTS); } + "cfg" => { + cfg::complete_cfg(acc, ctx); + } _ => (), }, (None, Some(_)) => (), @@ -852,4 +856,15 @@ mod tests { "#]], ); } + + #[test] + fn test_cfg() { + check( + r#"#[cfg(target_endian = $0"#, + expect![[r#" + at little + at big +"#]], + ); + } } diff --git a/crates/ide_completion/src/completions/attribute/cfg.rs b/crates/ide_completion/src/completions/attribute/cfg.rs new file mode 100644 index 000000000..71e659563 --- /dev/null +++ b/crates/ide_completion/src/completions/attribute/cfg.rs @@ -0,0 +1,126 @@ +//! Completion for cfg + +use std::iter; + +use syntax::SyntaxKind; + +use crate::{ + completions::Completions, context::CompletionContext, item::CompletionKind, CompletionItem, + CompletionItemKind, +}; + +pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) { + let add_completion = |item: &&str| { + let mut completion = + CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), *item); + completion.insert_text(format!(r#""{}""#, item)); + completion.kind(CompletionItemKind::Attribute); + acc.add(completion.build()); + }; + + let previous = iter::successors(ctx.original_token.prev_token(), |t| { + (matches!(t.kind(), SyntaxKind::EQ) || t.kind().is_trivia()) + .then(|| t.prev_token()) + .flatten() + }) + .find(|t| matches!(t.kind(), SyntaxKind::IDENT)); + + match previous.as_ref().map(|p| p.text()) { + Some("feature") => { + ctx.krate.map(|krate| { + krate.features(ctx.db).iter().for_each(|f| { + let mut item = CompletionItem::new( + CompletionKind::Attribute, + ctx.source_range(), + f.clone(), + ); + item.insert_text(format!(r#""{}""#, f)); + + acc.add(item.build()) + }) + }); + } + Some("target_arch") => KNOWN_ARCH.iter().for_each(add_completion), + Some("target_env") => KNOWN_ENV.iter().for_each(add_completion), + Some("target_os") => KNOWN_OS.iter().for_each(add_completion), + Some("target_vendor") => KNOWN_VENDOR.iter().for_each(add_completion), + Some("target_endian") => ["little", "big"].iter().for_each(add_completion), + Some(name) => { + ctx.krate.map(|krate| { + krate.cfg(ctx.db).get_cfg_values(&name).iter().for_each(|s| { + let mut item = CompletionItem::new( + CompletionKind::Attribute, + ctx.source_range(), + s.as_str(), + ); + item.insert_text(format!(r#""{}""#, s)); + + acc.add(item.build()); + }) + }); + } + None => { + ctx.krate.map(|krate| { + krate.cfg(ctx.db).get_cfg_keys().iter().for_each(|s| { + let item = CompletionItem::new( + CompletionKind::Attribute, + ctx.source_range(), + s.as_str(), + ); + acc.add(item.build()); + }) + }); + } + }; +} + +const KNOWN_ARCH: [&'static str; 19] = [ + "aarch64", + "arm", + "avr", + "hexagon", + "mips", + "mips64", + "msp430", + "nvptx64", + "powerpc", + "powerpc64", + "riscv32", + "riscv64", + "s390x", + "sparc", + "sparc64", + "wasm32", + "wasm64", + "x86", + "x86_64", +]; + +const KNOWN_ENV: [&'static str; 7] = + ["eabihf", "gnu", "gnueabihf", "msvc", "relibc", "sgx", "uclibc"]; + +const KNOWN_OS: [&'static str; 20] = [ + "cuda", + "dragonfly", + "emscripten", + "freebsd", + "fuchsia", + "haiku", + "hermit", + "illumos", + "l4re", + "linux", + "netbsd", + "none", + "openbsd", + "psp", + "redox", + "solaris", + "uefi", + "unknown", + "vxworks", + "windows", +]; + +const KNOWN_VENDOR: [&'static str; 8] = + ["apple", "fortanix", "nvidia", "pc", "sony", "unknown", "wrs", "uwp"]; diff --git a/crates/project_model/src/workspace.rs b/crates/project_model/src/workspace.rs index d8217f714..9ee3fc1a3 100644 --- a/crates/project_model/src/workspace.rs +++ b/crates/project_model/src/workspace.rs @@ -387,6 +387,7 @@ fn project_json_to_crate_graph( cfg_options, env, proc_macro.unwrap_or_default(), + Default::default(), ), ) }) @@ -582,6 +583,7 @@ fn detached_files_to_crate_graph( cfg_options.clone(), Env::default(), Vec::new(), + Default::default(), ); for (name, krate) in public_deps.iter() { @@ -726,6 +728,7 @@ fn add_target_crate_root( cfg_options, env, proc_macro, + pkg.features.clone(), ); crate_id @@ -755,6 +758,7 @@ fn sysroot_to_crate_graph( cfg_options.clone(), env, proc_macro, + Default::default(), ); Some((krate, crate_id)) }) -- cgit v1.2.3 From ae823aa23f1c4fa55e71dd972d0b10c69148b0b4 Mon Sep 17 00:00:00 2001 From: Jamie Cunliffe Date: Mon, 31 May 2021 20:45:01 +0100 Subject: Move features into potential_cfg_options --- crates/base_db/src/fixture.rs | 5 +++-- crates/base_db/src/input.rs | 26 +++++++++++----------- crates/hir/src/lib.rs | 4 ++-- crates/ide/src/lib.rs | 2 +- .../src/completions/attribute/cfg.rs | 18 ++------------- crates/project_model/src/workspace.rs | 15 +++++++++---- 6 files changed, 32 insertions(+), 38 deletions(-) diff --git a/crates/base_db/src/fixture.rs b/crates/base_db/src/fixture.rs index 6d3b1266e..7d5d12e63 100644 --- a/crates/base_db/src/fixture.rs +++ b/crates/base_db/src/fixture.rs @@ -128,10 +128,10 @@ impl ChangeFixture { file_id, meta.edition, Some(crate_name.clone().into()), + meta.cfg.clone(), meta.cfg, meta.env, Default::default(), - Default::default(), ); let prev = crates.insert(crate_name.clone(), crate_id); assert!(prev.is_none()); @@ -158,10 +158,10 @@ impl ChangeFixture { crate_root, Edition::Edition2018, Some(CrateName::new("test").unwrap().into()), + default_cfg.clone(), default_cfg, Env::default(), Default::default(), - Default::default(), ); } else { for (from, to) in crate_deps { @@ -188,6 +188,7 @@ impl ChangeFixture { Edition::Edition2021, Some(CrateDisplayName::from_canonical_name("core".to_string())), CfgOptions::default(), + CfgOptions::default(), Env::default(), Vec::new(), ); diff --git a/crates/base_db/src/input.rs b/crates/base_db/src/input.rs index d99388f71..0c51a59a0 100644 --- a/crates/base_db/src/input.rs +++ b/crates/base_db/src/input.rs @@ -189,10 +189,10 @@ pub struct CrateData { /// `Dependency` matters), this name should only be used for UI. pub display_name: Option, pub cfg_options: CfgOptions, + pub potential_cfg_options: CfgOptions, pub env: Env, pub dependencies: Vec, pub proc_macro: Vec, - pub features: FxHashMap>, } #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -220,19 +220,19 @@ impl CrateGraph { edition: Edition, display_name: Option, cfg_options: CfgOptions, + potential_cfg_options: CfgOptions, env: Env, proc_macro: Vec, - features: FxHashMap>, ) -> CrateId { let data = CrateData { root_file_id: file_id, edition, display_name, cfg_options, + potential_cfg_options, env, proc_macro, dependencies: Vec::new(), - features, }; let crate_id = CrateId(self.arena.len() as u32); let prev = self.arena.insert(crate_id, data); @@ -507,27 +507,27 @@ mod tests { Edition2018, None, CfgOptions::default(), + CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); let crate2 = graph.add_crate_root( FileId(2u32), Edition2018, None, CfgOptions::default(), + CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); let crate3 = graph.add_crate_root( FileId(3u32), Edition2018, None, CfgOptions::default(), + CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); @@ -542,18 +542,18 @@ mod tests { Edition2018, None, CfgOptions::default(), + CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); let crate2 = graph.add_crate_root( FileId(2u32), Edition2018, None, CfgOptions::default(), + CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); assert!(graph.add_dep(crate2, CrateName::new("crate2").unwrap(), crate2).is_err()); @@ -567,27 +567,27 @@ mod tests { Edition2018, None, CfgOptions::default(), + CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); let crate2 = graph.add_crate_root( FileId(2u32), Edition2018, None, CfgOptions::default(), + CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); let crate3 = graph.add_crate_root( FileId(3u32), Edition2018, None, CfgOptions::default(), + CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); @@ -601,18 +601,18 @@ mod tests { Edition2018, None, CfgOptions::default(), + CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); let crate2 = graph.add_crate_root( FileId(2u32), Edition2018, None, CfgOptions::default(), + CfgOptions::default(), Env::default(), Default::default(), - Default::default(), ); assert!(graph .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 2b2aaec94..30cc34403 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -234,8 +234,8 @@ impl Crate { db.crate_graph()[self.id].cfg_options.clone() } - pub fn features(&self, db: &dyn HirDatabase) -> Vec { - db.crate_graph()[self.id].features.iter().map(|(feat, _)| feat.clone()).collect() + pub fn potential_cfg(&self, db: &dyn HirDatabase) -> CfgOptions { + db.crate_graph()[self.id].potential_cfg_options.clone() } } diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 0693869a2..aac084012 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -217,10 +217,10 @@ impl Analysis { file_id, Edition::Edition2018, None, + cfg_options.clone(), cfg_options, Env::default(), Default::default(), - Default::default(), ); change.change_file(file_id, Some(Arc::new(text))); change.set_crate_graph(crate_graph); diff --git a/crates/ide_completion/src/completions/attribute/cfg.rs b/crates/ide_completion/src/completions/attribute/cfg.rs index 71e659563..847e6529a 100644 --- a/crates/ide_completion/src/completions/attribute/cfg.rs +++ b/crates/ide_completion/src/completions/attribute/cfg.rs @@ -26,20 +26,6 @@ pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) { .find(|t| matches!(t.kind(), SyntaxKind::IDENT)); match previous.as_ref().map(|p| p.text()) { - Some("feature") => { - ctx.krate.map(|krate| { - krate.features(ctx.db).iter().for_each(|f| { - let mut item = CompletionItem::new( - CompletionKind::Attribute, - ctx.source_range(), - f.clone(), - ); - item.insert_text(format!(r#""{}""#, f)); - - acc.add(item.build()) - }) - }); - } Some("target_arch") => KNOWN_ARCH.iter().for_each(add_completion), Some("target_env") => KNOWN_ENV.iter().for_each(add_completion), Some("target_os") => KNOWN_OS.iter().for_each(add_completion), @@ -47,7 +33,7 @@ pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) { Some("target_endian") => ["little", "big"].iter().for_each(add_completion), Some(name) => { ctx.krate.map(|krate| { - krate.cfg(ctx.db).get_cfg_values(&name).iter().for_each(|s| { + krate.potential_cfg(ctx.db).get_cfg_values(&name).iter().for_each(|s| { let mut item = CompletionItem::new( CompletionKind::Attribute, ctx.source_range(), @@ -61,7 +47,7 @@ pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) { } None => { ctx.krate.map(|krate| { - krate.cfg(ctx.db).get_cfg_keys().iter().for_each(|s| { + krate.potential_cfg(ctx.db).get_cfg_keys().iter().for_each(|s| { let item = CompletionItem::new( CompletionKind::Attribute, ctx.source_range(), diff --git a/crates/project_model/src/workspace.rs b/crates/project_model/src/workspace.rs index 9ee3fc1a3..e67ba2bd9 100644 --- a/crates/project_model/src/workspace.rs +++ b/crates/project_model/src/workspace.rs @@ -384,10 +384,10 @@ fn project_json_to_crate_graph( file_id, krate.edition, krate.display_name.clone(), + cfg_options.clone(), cfg_options, env, proc_macro.unwrap_or_default(), - Default::default(), ), ) }) @@ -581,9 +581,9 @@ fn detached_files_to_crate_graph( Edition::Edition2018, display_name, cfg_options.clone(), + cfg_options.clone(), Env::default(), Vec::new(), - Default::default(), ); for (name, krate) in public_deps.iter() { @@ -721,14 +721,21 @@ fn add_target_crate_root( .unwrap_or_default(); let display_name = CrateDisplayName::from_canonical_name(cargo_name.to_string()); + let mut potential_cfg_options = cfg_options.clone(); + potential_cfg_options.extend( + pkg.features + .iter() + .map(|feat| CfgFlag::KeyValue { key: "feature".into(), value: feat.0.into() }), + ); + let crate_id = crate_graph.add_crate_root( file_id, edition, Some(display_name), cfg_options, + potential_cfg_options, env, proc_macro, - pkg.features.clone(), ); crate_id @@ -756,9 +763,9 @@ fn sysroot_to_crate_graph( Edition::Edition2018, Some(display_name), cfg_options.clone(), + cfg_options.clone(), env, proc_macro, - Default::default(), ); Some((krate, crate_id)) }) -- cgit v1.2.3