diff options
author | Benjamin Coenen <[email protected]> | 2020-05-22 08:23:31 +0100 |
---|---|---|
committer | Benjamin Coenen <[email protected]> | 2020-05-22 08:23:31 +0100 |
commit | 43339058e32e8bb0d218390b9df5b5a68fe57ca7 (patch) | |
tree | 75ab113f7728d08a4093621b8a02b3005760843e | |
parent | a7c8aa7c60c05db66ba4e89ae9e05c82e62507a5 (diff) |
add support of feature flag for runnables #4464
Signed-off-by: Benjamin Coenen <[email protected]>
-rw-r--r-- | crates/ra_cfg/src/cfg_expr.rs | 17 | ||||
-rw-r--r-- | crates/ra_ide/src/runnables.rs | 13 |
2 files changed, 9 insertions, 21 deletions
diff --git a/crates/ra_cfg/src/cfg_expr.rs b/crates/ra_cfg/src/cfg_expr.rs index a4b201e0e..98f44f56d 100644 --- a/crates/ra_cfg/src/cfg_expr.rs +++ b/crates/ra_cfg/src/cfg_expr.rs | |||
@@ -35,14 +35,11 @@ impl CfgExpr { | |||
35 | } | 35 | } |
36 | 36 | ||
37 | /// Return minimal features needed | 37 | /// Return minimal features needed |
38 | pub fn minimal_features_needed(&self) -> Option<Vec<SmolStr>> { | 38 | pub fn minimal_features_needed(&self) -> Vec<SmolStr> { |
39 | let mut features = vec![]; | 39 | let mut features = vec![]; |
40 | self.collect_minimal_features_needed(&mut features); | 40 | self.collect_minimal_features_needed(&mut features); |
41 | if features.is_empty() { | 41 | |
42 | None | 42 | features |
43 | } else { | ||
44 | Some(features) | ||
45 | } | ||
46 | } | 43 | } |
47 | 44 | ||
48 | fn collect_minimal_features_needed(&self, features: &mut Vec<SmolStr>) { | 45 | fn collect_minimal_features_needed(&self, features: &mut Vec<SmolStr>) { |
@@ -169,14 +166,14 @@ mod tests { | |||
169 | let (subtree, _) = get_token_tree_generated(r#"#![cfg(feature = "baz")]"#); | 166 | let (subtree, _) = get_token_tree_generated(r#"#![cfg(feature = "baz")]"#); |
170 | let cfg_expr = parse_cfg(&subtree); | 167 | let cfg_expr = parse_cfg(&subtree); |
171 | 168 | ||
172 | assert_eq!(cfg_expr.minimal_features_needed().unwrap(), vec![SmolStr::new("baz")]); | 169 | assert_eq!(cfg_expr.minimal_features_needed(), vec![SmolStr::new("baz")]); |
173 | 170 | ||
174 | let (subtree, _) = | 171 | let (subtree, _) = |
175 | get_token_tree_generated(r#"#![cfg(all(feature = "baz", feature = "foo"))]"#); | 172 | get_token_tree_generated(r#"#![cfg(all(feature = "baz", feature = "foo"))]"#); |
176 | let cfg_expr = parse_cfg(&subtree); | 173 | let cfg_expr = parse_cfg(&subtree); |
177 | 174 | ||
178 | assert_eq!( | 175 | assert_eq!( |
179 | cfg_expr.minimal_features_needed().unwrap(), | 176 | cfg_expr.minimal_features_needed(), |
180 | vec![SmolStr::new("baz"), SmolStr::new("foo")] | 177 | vec![SmolStr::new("baz"), SmolStr::new("foo")] |
181 | ); | 178 | ); |
182 | 179 | ||
@@ -184,11 +181,11 @@ mod tests { | |||
184 | get_token_tree_generated(r#"#![cfg(any(feature = "baz", feature = "foo", unix))]"#); | 181 | get_token_tree_generated(r#"#![cfg(any(feature = "baz", feature = "foo", unix))]"#); |
185 | let cfg_expr = parse_cfg(&subtree); | 182 | let cfg_expr = parse_cfg(&subtree); |
186 | 183 | ||
187 | assert_eq!(cfg_expr.minimal_features_needed().unwrap(), vec![SmolStr::new("baz")]); | 184 | assert_eq!(cfg_expr.minimal_features_needed(), vec![SmolStr::new("baz")]); |
188 | 185 | ||
189 | let (subtree, _) = get_token_tree_generated(r#"#![cfg(foo)]"#); | 186 | let (subtree, _) = get_token_tree_generated(r#"#![cfg(foo)]"#); |
190 | let cfg_expr = parse_cfg(&subtree); | 187 | let cfg_expr = parse_cfg(&subtree); |
191 | 188 | ||
192 | assert!(cfg_expr.minimal_features_needed().is_none()); | 189 | assert!(cfg_expr.minimal_features_needed().is_empty()); |
193 | } | 190 | } |
194 | } | 191 | } |
diff --git a/crates/ra_ide/src/runnables.rs b/crates/ra_ide/src/runnables.rs index 3a3d0b0ac..a460370c5 100644 --- a/crates/ra_ide/src/runnables.rs +++ b/crates/ra_ide/src/runnables.rs | |||
@@ -190,17 +190,8 @@ fn runnable_mod( | |||
190 | 190 | ||
191 | fn get_features_needed(attrs: Attrs) -> Option<Vec<SmolStr>> { | 191 | fn get_features_needed(attrs: Attrs) -> Option<Vec<SmolStr>> { |
192 | let cfg_expr = attrs.by_key("cfg").tt_values().map(|subtree| ra_cfg::parse_cfg(subtree)); | 192 | let cfg_expr = attrs.by_key("cfg").tt_values().map(|subtree| ra_cfg::parse_cfg(subtree)); |
193 | let features_needed = cfg_expr.fold(vec![], |mut acc, cfg| { | 193 | let features_needed = cfg_expr.map(|cfg| cfg.minimal_features_needed()).flatten().collect(); |
194 | if let Some(features_needed) = cfg.minimal_features_needed() { | 194 | Some(features_needed).filter(|it: &Vec<SmolStr>| !it.is_empty()) |
195 | acc.extend(features_needed); | ||
196 | } | ||
197 | acc | ||
198 | }); | ||
199 | if features_needed.is_empty() { | ||
200 | None | ||
201 | } else { | ||
202 | Some(features_needed) | ||
203 | } | ||
204 | } | 195 | } |
205 | 196 | ||
206 | #[cfg(test)] | 197 | #[cfg(test)] |