aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Cunliffe <[email protected]>2021-05-31 20:45:01 +0100
committerJamie Cunliffe <[email protected]>2021-06-21 17:54:05 +0100
commitae823aa23f1c4fa55e71dd972d0b10c69148b0b4 (patch)
tree9c39a29bdee4f45dcbd497c25350cbfbb0c5e9c3
parent284483b347d15bee3a7bf293d33e5f19a9740102 (diff)
Move features into potential_cfg_options
-rw-r--r--crates/base_db/src/fixture.rs5
-rw-r--r--crates/base_db/src/input.rs26
-rw-r--r--crates/hir/src/lib.rs4
-rw-r--r--crates/ide/src/lib.rs2
-rw-r--r--crates/ide_completion/src/completions/attribute/cfg.rs18
-rw-r--r--crates/project_model/src/workspace.rs15
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 {
128 file_id, 128 file_id,
129 meta.edition, 129 meta.edition,
130 Some(crate_name.clone().into()), 130 Some(crate_name.clone().into()),
131 meta.cfg.clone(),
131 meta.cfg, 132 meta.cfg,
132 meta.env, 133 meta.env,
133 Default::default(), 134 Default::default(),
134 Default::default(),
135 ); 135 );
136 let prev = crates.insert(crate_name.clone(), crate_id); 136 let prev = crates.insert(crate_name.clone(), crate_id);
137 assert!(prev.is_none()); 137 assert!(prev.is_none());
@@ -158,10 +158,10 @@ impl ChangeFixture {
158 crate_root, 158 crate_root,
159 Edition::Edition2018, 159 Edition::Edition2018,
160 Some(CrateName::new("test").unwrap().into()), 160 Some(CrateName::new("test").unwrap().into()),
161 default_cfg.clone(),
161 default_cfg, 162 default_cfg,
162 Env::default(), 163 Env::default(),
163 Default::default(), 164 Default::default(),
164 Default::default(),
165 ); 165 );
166 } else { 166 } else {
167 for (from, to) in crate_deps { 167 for (from, to) in crate_deps {
@@ -188,6 +188,7 @@ impl ChangeFixture {
188 Edition::Edition2021, 188 Edition::Edition2021,
189 Some(CrateDisplayName::from_canonical_name("core".to_string())), 189 Some(CrateDisplayName::from_canonical_name("core".to_string())),
190 CfgOptions::default(), 190 CfgOptions::default(),
191 CfgOptions::default(),
191 Env::default(), 192 Env::default(),
192 Vec::new(), 193 Vec::new(),
193 ); 194 );
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 {
189 /// `Dependency` matters), this name should only be used for UI. 189 /// `Dependency` matters), this name should only be used for UI.
190 pub display_name: Option<CrateDisplayName>, 190 pub display_name: Option<CrateDisplayName>,
191 pub cfg_options: CfgOptions, 191 pub cfg_options: CfgOptions,
192 pub potential_cfg_options: CfgOptions,
192 pub env: Env, 193 pub env: Env,
193 pub dependencies: Vec<Dependency>, 194 pub dependencies: Vec<Dependency>,
194 pub proc_macro: Vec<ProcMacro>, 195 pub proc_macro: Vec<ProcMacro>,
195 pub features: FxHashMap<String, Vec<String>>,
196} 196}
197 197
198#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 198#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -220,19 +220,19 @@ impl CrateGraph {
220 edition: Edition, 220 edition: Edition,
221 display_name: Option<CrateDisplayName>, 221 display_name: Option<CrateDisplayName>,
222 cfg_options: CfgOptions, 222 cfg_options: CfgOptions,
223 potential_cfg_options: CfgOptions,
223 env: Env, 224 env: Env,
224 proc_macro: Vec<ProcMacro>, 225 proc_macro: Vec<ProcMacro>,
225 features: FxHashMap<String, Vec<String>>,
226 ) -> CrateId { 226 ) -> CrateId {
227 let data = CrateData { 227 let data = CrateData {
228 root_file_id: file_id, 228 root_file_id: file_id,
229 edition, 229 edition,
230 display_name, 230 display_name,
231 cfg_options, 231 cfg_options,
232 potential_cfg_options,
232 env, 233 env,
233 proc_macro, 234 proc_macro,
234 dependencies: Vec::new(), 235 dependencies: Vec::new(),
235 features,
236 }; 236 };
237 let crate_id = CrateId(self.arena.len() as u32); 237 let crate_id = CrateId(self.arena.len() as u32);
238 let prev = self.arena.insert(crate_id, data); 238 let prev = self.arena.insert(crate_id, data);
@@ -507,27 +507,27 @@ mod tests {
507 Edition2018, 507 Edition2018,
508 None, 508 None,
509 CfgOptions::default(), 509 CfgOptions::default(),
510 CfgOptions::default(),
510 Env::default(), 511 Env::default(),
511 Default::default(), 512 Default::default(),
512 Default::default(),
513 ); 513 );
514 let crate2 = graph.add_crate_root( 514 let crate2 = graph.add_crate_root(
515 FileId(2u32), 515 FileId(2u32),
516 Edition2018, 516 Edition2018,
517 None, 517 None,
518 CfgOptions::default(), 518 CfgOptions::default(),
519 CfgOptions::default(),
519 Env::default(), 520 Env::default(),
520 Default::default(), 521 Default::default(),
521 Default::default(),
522 ); 522 );
523 let crate3 = graph.add_crate_root( 523 let crate3 = graph.add_crate_root(
524 FileId(3u32), 524 FileId(3u32),
525 Edition2018, 525 Edition2018,
526 None, 526 None,
527 CfgOptions::default(), 527 CfgOptions::default(),
528 CfgOptions::default(),
528 Env::default(), 529 Env::default(),
529 Default::default(), 530 Default::default(),
530 Default::default(),
531 ); 531 );
532 assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); 532 assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
533 assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); 533 assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
@@ -542,18 +542,18 @@ mod tests {
542 Edition2018, 542 Edition2018,
543 None, 543 None,
544 CfgOptions::default(), 544 CfgOptions::default(),
545 CfgOptions::default(),
545 Env::default(), 546 Env::default(),
546 Default::default(), 547 Default::default(),
547 Default::default(),
548 ); 548 );
549 let crate2 = graph.add_crate_root( 549 let crate2 = graph.add_crate_root(
550 FileId(2u32), 550 FileId(2u32),
551 Edition2018, 551 Edition2018,
552 None, 552 None,
553 CfgOptions::default(), 553 CfgOptions::default(),
554 CfgOptions::default(),
554 Env::default(), 555 Env::default(),
555 Default::default(), 556 Default::default(),
556 Default::default(),
557 ); 557 );
558 assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); 558 assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
559 assert!(graph.add_dep(crate2, CrateName::new("crate2").unwrap(), crate2).is_err()); 559 assert!(graph.add_dep(crate2, CrateName::new("crate2").unwrap(), crate2).is_err());
@@ -567,27 +567,27 @@ mod tests {
567 Edition2018, 567 Edition2018,
568 None, 568 None,
569 CfgOptions::default(), 569 CfgOptions::default(),
570 CfgOptions::default(),
570 Env::default(), 571 Env::default(),
571 Default::default(), 572 Default::default(),
572 Default::default(),
573 ); 573 );
574 let crate2 = graph.add_crate_root( 574 let crate2 = graph.add_crate_root(
575 FileId(2u32), 575 FileId(2u32),
576 Edition2018, 576 Edition2018,
577 None, 577 None,
578 CfgOptions::default(), 578 CfgOptions::default(),
579 CfgOptions::default(),
579 Env::default(), 580 Env::default(),
580 Default::default(), 581 Default::default(),
581 Default::default(),
582 ); 582 );
583 let crate3 = graph.add_crate_root( 583 let crate3 = graph.add_crate_root(
584 FileId(3u32), 584 FileId(3u32),
585 Edition2018, 585 Edition2018,
586 None, 586 None,
587 CfgOptions::default(), 587 CfgOptions::default(),
588 CfgOptions::default(),
588 Env::default(), 589 Env::default(),
589 Default::default(), 590 Default::default(),
590 Default::default(),
591 ); 591 );
592 assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); 592 assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
593 assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); 593 assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
@@ -601,18 +601,18 @@ mod tests {
601 Edition2018, 601 Edition2018,
602 None, 602 None,
603 CfgOptions::default(), 603 CfgOptions::default(),
604 CfgOptions::default(),
604 Env::default(), 605 Env::default(),
605 Default::default(), 606 Default::default(),
606 Default::default(),
607 ); 607 );
608 let crate2 = graph.add_crate_root( 608 let crate2 = graph.add_crate_root(
609 FileId(2u32), 609 FileId(2u32),
610 Edition2018, 610 Edition2018,
611 None, 611 None,
612 CfgOptions::default(), 612 CfgOptions::default(),
613 CfgOptions::default(),
613 Env::default(), 614 Env::default(),
614 Default::default(), 615 Default::default(),
615 Default::default(),
616 ); 616 );
617 assert!(graph 617 assert!(graph
618 .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) 618 .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 {
234 db.crate_graph()[self.id].cfg_options.clone() 234 db.crate_graph()[self.id].cfg_options.clone()
235 } 235 }
236 236
237 pub fn features(&self, db: &dyn HirDatabase) -> Vec<String> { 237 pub fn potential_cfg(&self, db: &dyn HirDatabase) -> CfgOptions {
238 db.crate_graph()[self.id].features.iter().map(|(feat, _)| feat.clone()).collect() 238 db.crate_graph()[self.id].potential_cfg_options.clone()
239 } 239 }
240} 240}
241 241
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 {
217 file_id, 217 file_id,
218 Edition::Edition2018, 218 Edition::Edition2018,
219 None, 219 None,
220 cfg_options.clone(),
220 cfg_options, 221 cfg_options,
221 Env::default(), 222 Env::default(),
222 Default::default(), 223 Default::default(),
223 Default::default(),
224 ); 224 );
225 change.change_file(file_id, Some(Arc::new(text))); 225 change.change_file(file_id, Some(Arc::new(text)));
226 change.set_crate_graph(crate_graph); 226 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) {
26 .find(|t| matches!(t.kind(), SyntaxKind::IDENT)); 26 .find(|t| matches!(t.kind(), SyntaxKind::IDENT));
27 27
28 match previous.as_ref().map(|p| p.text()) { 28 match previous.as_ref().map(|p| p.text()) {
29 Some("feature") => {
30 ctx.krate.map(|krate| {
31 krate.features(ctx.db).iter().for_each(|f| {
32 let mut item = CompletionItem::new(
33 CompletionKind::Attribute,
34 ctx.source_range(),
35 f.clone(),
36 );
37 item.insert_text(format!(r#""{}""#, f));
38
39 acc.add(item.build())
40 })
41 });
42 }
43 Some("target_arch") => KNOWN_ARCH.iter().for_each(add_completion), 29 Some("target_arch") => KNOWN_ARCH.iter().for_each(add_completion),
44 Some("target_env") => KNOWN_ENV.iter().for_each(add_completion), 30 Some("target_env") => KNOWN_ENV.iter().for_each(add_completion),
45 Some("target_os") => KNOWN_OS.iter().for_each(add_completion), 31 Some("target_os") => KNOWN_OS.iter().for_each(add_completion),
@@ -47,7 +33,7 @@ pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) {
47 Some("target_endian") => ["little", "big"].iter().for_each(add_completion), 33 Some("target_endian") => ["little", "big"].iter().for_each(add_completion),
48 Some(name) => { 34 Some(name) => {
49 ctx.krate.map(|krate| { 35 ctx.krate.map(|krate| {
50 krate.cfg(ctx.db).get_cfg_values(&name).iter().for_each(|s| { 36 krate.potential_cfg(ctx.db).get_cfg_values(&name).iter().for_each(|s| {
51 let mut item = CompletionItem::new( 37 let mut item = CompletionItem::new(
52 CompletionKind::Attribute, 38 CompletionKind::Attribute,
53 ctx.source_range(), 39 ctx.source_range(),
@@ -61,7 +47,7 @@ pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) {
61 } 47 }
62 None => { 48 None => {
63 ctx.krate.map(|krate| { 49 ctx.krate.map(|krate| {
64 krate.cfg(ctx.db).get_cfg_keys().iter().for_each(|s| { 50 krate.potential_cfg(ctx.db).get_cfg_keys().iter().for_each(|s| {
65 let item = CompletionItem::new( 51 let item = CompletionItem::new(
66 CompletionKind::Attribute, 52 CompletionKind::Attribute,
67 ctx.source_range(), 53 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(
384 file_id, 384 file_id,
385 krate.edition, 385 krate.edition,
386 krate.display_name.clone(), 386 krate.display_name.clone(),
387 cfg_options.clone(),
387 cfg_options, 388 cfg_options,
388 env, 389 env,
389 proc_macro.unwrap_or_default(), 390 proc_macro.unwrap_or_default(),
390 Default::default(),
391 ), 391 ),
392 ) 392 )
393 }) 393 })
@@ -581,9 +581,9 @@ fn detached_files_to_crate_graph(
581 Edition::Edition2018, 581 Edition::Edition2018,
582 display_name, 582 display_name,
583 cfg_options.clone(), 583 cfg_options.clone(),
584 cfg_options.clone(),
584 Env::default(), 585 Env::default(),
585 Vec::new(), 586 Vec::new(),
586 Default::default(),
587 ); 587 );
588 588
589 for (name, krate) in public_deps.iter() { 589 for (name, krate) in public_deps.iter() {
@@ -721,14 +721,21 @@ fn add_target_crate_root(
721 .unwrap_or_default(); 721 .unwrap_or_default();
722 722
723 let display_name = CrateDisplayName::from_canonical_name(cargo_name.to_string()); 723 let display_name = CrateDisplayName::from_canonical_name(cargo_name.to_string());
724 let mut potential_cfg_options = cfg_options.clone();
725 potential_cfg_options.extend(
726 pkg.features
727 .iter()
728 .map(|feat| CfgFlag::KeyValue { key: "feature".into(), value: feat.0.into() }),
729 );
730
724 let crate_id = crate_graph.add_crate_root( 731 let crate_id = crate_graph.add_crate_root(
725 file_id, 732 file_id,
726 edition, 733 edition,
727 Some(display_name), 734 Some(display_name),
728 cfg_options, 735 cfg_options,
736 potential_cfg_options,
729 env, 737 env,
730 proc_macro, 738 proc_macro,
731 pkg.features.clone(),
732 ); 739 );
733 740
734 crate_id 741 crate_id
@@ -756,9 +763,9 @@ fn sysroot_to_crate_graph(
756 Edition::Edition2018, 763 Edition::Edition2018,
757 Some(display_name), 764 Some(display_name),
758 cfg_options.clone(), 765 cfg_options.clone(),
766 cfg_options.clone(),
759 env, 767 env,
760 proc_macro, 768 proc_macro,
761 Default::default(),
762 ); 769 );
763 Some((krate, crate_id)) 770 Some((krate, crate_id))
764 }) 771 })