From c6143742bd4e625d391ac3ea860be7578ab9f53f Mon Sep 17 00:00:00 2001 From: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> Date: Thu, 21 May 2020 10:48:42 +0200 Subject: add support of feature flag for runnables #4464 Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> --- crates/ra_ide/src/runnables.rs | 146 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 133 insertions(+), 13 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/runnables.rs b/crates/ra_ide/src/runnables.rs index fa8a9d92c..4f7eb2c5b 100644 --- a/crates/ra_ide/src/runnables.rs +++ b/crates/ra_ide/src/runnables.rs @@ -1,11 +1,11 @@ //! FIXME: write short doc here -use hir::Semantics; +use hir::{Attrs, HirFileId, InFile, Semantics}; use itertools::Itertools; use ra_ide_db::RootDatabase; use ra_syntax::{ ast::{self, AstNode, AttrsOwner, ModuleItemOwner, NameOwner}, - match_ast, SyntaxNode, TextRange, + match_ast, SmolStr, SyntaxNode, TextRange, }; use crate::FileId; @@ -16,6 +16,7 @@ use std::fmt::Display; pub struct Runnable { pub range: TextRange, pub kind: RunnableKind, + pub features_needed: Option>, } #[derive(Debug)] @@ -45,20 +46,24 @@ pub enum RunnableKind { pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec { let sema = Semantics::new(db); let source_file = sema.parse(file_id); - source_file.syntax().descendants().filter_map(|i| runnable(&sema, i)).collect() + source_file.syntax().descendants().filter_map(|i| runnable(&sema, i, file_id)).collect() } -fn runnable(sema: &Semantics, item: SyntaxNode) -> Option { +fn runnable(sema: &Semantics, item: SyntaxNode, file_id: FileId) -> Option { match_ast! { match item { - ast::FnDef(it) => runnable_fn(sema, it), - ast::Module(it) => runnable_mod(sema, it), + ast::FnDef(it) => runnable_fn(sema, it, file_id), + ast::Module(it) => runnable_mod(sema, it, file_id), _ => None, } } } -fn runnable_fn(sema: &Semantics, fn_def: ast::FnDef) -> Option { +fn runnable_fn( + sema: &Semantics, + fn_def: ast::FnDef, + file_id: FileId, +) -> Option { let name_string = fn_def.name()?.text().to_string(); let kind = if name_string == "main" { @@ -89,7 +94,11 @@ fn runnable_fn(sema: &Semantics, fn_def: ast::FnDef) -> Option bool { fn_def.doc_comment_text().map_or(false, |comment| comment.contains("```")) } -fn runnable_mod(sema: &Semantics, module: ast::Module) -> Option { +fn runnable_mod( + sema: &Semantics, + module: ast::Module, + file_id: FileId, +) -> Option { let has_test_function = module .item_list()? .items() @@ -138,11 +151,34 @@ fn runnable_mod(sema: &Semantics, module: ast::Module) -> Option Option> { + let cfg_expr = attrs.by_key("cfg").tt_values().map(|subtree| ra_cfg::parse_cfg(subtree)); + let features_needed = cfg_expr.fold(vec![], |mut acc, cfg| { + if let Some(features_needed) = cfg.minimal_features_needed() { + acc.extend(features_needed); + } + acc + }); + if features_needed.is_empty() { + None + } else { + Some(features_needed) + } } #[cfg(test)] @@ -174,6 +210,7 @@ mod tests { Runnable { range: 1..21, kind: Bin, + features_needed: None, }, Runnable { range: 22..46, @@ -185,6 +222,7 @@ mod tests { ignore: false, }, }, + features_needed: None, }, Runnable { range: 47..81, @@ -196,6 +234,7 @@ mod tests { ignore: true, }, }, + features_needed: None, }, ] "### @@ -223,6 +262,7 @@ mod tests { Runnable { range: 1..21, kind: Bin, + features_needed: None, }, Runnable { range: 22..64, @@ -231,6 +271,7 @@ mod tests { "foo", ), }, + features_needed: None, }, ] "### @@ -258,6 +299,7 @@ mod tests { kind: TestMod { path: "test_mod", }, + features_needed: None, }, Runnable { range: 28..57, @@ -269,6 +311,7 @@ mod tests { ignore: false, }, }, + features_needed: None, }, ] "### @@ -298,6 +341,7 @@ mod tests { kind: TestMod { path: "foo::test_mod", }, + features_needed: None, }, Runnable { range: 46..79, @@ -309,6 +353,7 @@ mod tests { ignore: false, }, }, + features_needed: None, }, ] "### @@ -340,6 +385,7 @@ mod tests { kind: TestMod { path: "foo::bar::test_mod", }, + features_needed: None, }, Runnable { range: 68..105, @@ -351,6 +397,80 @@ mod tests { ignore: false, }, }, + features_needed: None, + }, + ] + "### + ); + } + + #[test] + fn test_runnables_with_feature() { + let (analysis, pos) = analysis_and_position( + r#" + //- /lib.rs crate:foo cfg:feature=foo + <|> //empty + #[test] + #[cfg(feature = "foo")] + fn test_foo1() {} + "#, + ); + let runnables = analysis.runnables(pos.file_id).unwrap(); + assert_debug_snapshot!(&runnables, + @r###" + [ + Runnable { + range: 1..58, + kind: Test { + test_id: Name( + "test_foo1", + ), + attr: TestAttr { + ignore: false, + }, + }, + features_needed: Some( + [ + "foo", + ], + ), + }, + ] + "### + ); + } + + #[test] + fn test_runnables_with_features() { + let (analysis, pos) = analysis_and_position( + r#" + //- /lib.rs crate:foo cfg:feature=foo,feature=bar + <|> //empty + #[test] + #[cfg(all(feature = "foo", feature = "bar"))] + fn test_foo1() {} + "#, + ); + let runnables = analysis.runnables(pos.file_id).unwrap(); + assert_debug_snapshot!(&runnables, + @r###" + [ + Runnable { + range: 1..80, + kind: Test { + test_id: Name( + "test_foo1", + ), + attr: TestAttr { + ignore: false, + }, + }, + features_needed: Some( + [ + "foo", + "bar", + ], + ), }, ] "### -- cgit v1.2.3