diff options
Diffstat (limited to 'crates/ra_analysis/src/runnables.rs')
-rw-r--r-- | crates/ra_analysis/src/runnables.rs | 106 |
1 files changed, 60 insertions, 46 deletions
diff --git a/crates/ra_analysis/src/runnables.rs b/crates/ra_analysis/src/runnables.rs index 61ca0930a..474267605 100644 --- a/crates/ra_analysis/src/runnables.rs +++ b/crates/ra_analysis/src/runnables.rs | |||
@@ -1,11 +1,11 @@ | |||
1 | use itertools::Itertools; | ||
1 | use ra_syntax::{ | 2 | use ra_syntax::{ |
2 | ast::{self, AstNode, NameOwner, ModuleItemOwner}, | 3 | ast::{self, AstNode, NameOwner, ModuleItemOwner}, |
3 | SourceFileNode, TextRange, SyntaxNodeRef, | 4 | TextRange, SyntaxNodeRef, |
4 | TextUnit, | ||
5 | }; | ||
6 | use crate::{ | ||
7 | Analysis, FileId, FilePosition | ||
8 | }; | 5 | }; |
6 | use ra_db::{Cancelable, SyntaxDatabase}; | ||
7 | |||
8 | use crate::{db::RootDatabase, FileId}; | ||
9 | 9 | ||
10 | #[derive(Debug)] | 10 | #[derive(Debug)] |
11 | pub struct Runnable { | 11 | pub struct Runnable { |
@@ -20,53 +20,67 @@ pub enum RunnableKind { | |||
20 | Bin, | 20 | Bin, |
21 | } | 21 | } |
22 | 22 | ||
23 | pub fn runnables( | 23 | pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Cancelable<Vec<Runnable>> { |
24 | analysis: &Analysis, | 24 | let source_file = db.source_file(file_id); |
25 | file_node: &SourceFileNode, | 25 | let res = source_file |
26 | file_id: FileId, | ||
27 | ) -> Vec<Runnable> { | ||
28 | file_node | ||
29 | .syntax() | 26 | .syntax() |
30 | .descendants() | 27 | .descendants() |
31 | .filter_map(|i| runnable(analysis, i, file_id)) | 28 | .filter_map(|i| runnable(db, file_id, i)) |
32 | .collect() | 29 | .collect(); |
30 | Ok(res) | ||
33 | } | 31 | } |
34 | 32 | ||
35 | fn runnable<'a>(analysis: &Analysis, item: SyntaxNodeRef<'a>, file_id: FileId) -> Option<Runnable> { | 33 | fn runnable(db: &RootDatabase, file_id: FileId, item: SyntaxNodeRef) -> Option<Runnable> { |
36 | if let Some(f) = ast::FnDef::cast(item) { | 34 | if let Some(fn_def) = ast::FnDef::cast(item) { |
37 | let name = f.name()?.text(); | 35 | runnable_fn(fn_def) |
38 | let kind = if name == "main" { | ||
39 | RunnableKind::Bin | ||
40 | } else if f.has_atom_attr("test") { | ||
41 | RunnableKind::Test { | ||
42 | name: name.to_string(), | ||
43 | } | ||
44 | } else { | ||
45 | return None; | ||
46 | }; | ||
47 | Some(Runnable { | ||
48 | range: f.syntax().range(), | ||
49 | kind, | ||
50 | }) | ||
51 | } else if let Some(m) = ast::Module::cast(item) { | 36 | } else if let Some(m) = ast::Module::cast(item) { |
52 | if m.item_list()? | 37 | runnable_mod(db, file_id, m) |
53 | .items() | ||
54 | .map(ast::ModuleItem::syntax) | ||
55 | .filter_map(ast::FnDef::cast) | ||
56 | .any(|f| f.has_atom_attr("test")) | ||
57 | { | ||
58 | let postition = FilePosition { | ||
59 | file_id, | ||
60 | offset: m.syntax().range().start() + TextUnit::from_usize(1), | ||
61 | }; | ||
62 | analysis.module_path(postition).ok()?.map(|path| Runnable { | ||
63 | range: m.syntax().range(), | ||
64 | kind: RunnableKind::TestMod { path }, | ||
65 | }) | ||
66 | } else { | ||
67 | None | ||
68 | } | ||
69 | } else { | 38 | } else { |
70 | None | 39 | None |
71 | } | 40 | } |
72 | } | 41 | } |
42 | |||
43 | fn runnable_fn(fn_def: ast::FnDef) -> Option<Runnable> { | ||
44 | let name = fn_def.name()?.text(); | ||
45 | let kind = if name == "main" { | ||
46 | RunnableKind::Bin | ||
47 | } else if fn_def.has_atom_attr("test") { | ||
48 | RunnableKind::Test { | ||
49 | name: name.to_string(), | ||
50 | } | ||
51 | } else { | ||
52 | return None; | ||
53 | }; | ||
54 | Some(Runnable { | ||
55 | range: fn_def.syntax().range(), | ||
56 | kind, | ||
57 | }) | ||
58 | } | ||
59 | |||
60 | fn runnable_mod(db: &RootDatabase, file_id: FileId, module: ast::Module) -> Option<Runnable> { | ||
61 | let has_test_function = module | ||
62 | .item_list()? | ||
63 | .items() | ||
64 | .filter_map(|it| match it { | ||
65 | ast::ModuleItem::FnDef(it) => Some(it), | ||
66 | _ => None, | ||
67 | }) | ||
68 | .any(|f| f.has_atom_attr("test")); | ||
69 | if !has_test_function { | ||
70 | return None; | ||
71 | } | ||
72 | let range = module.syntax().range(); | ||
73 | let module = | ||
74 | hir::source_binder::module_from_child_node(db, file_id, module.syntax()).ok()??; | ||
75 | let path = module | ||
76 | .path_to_root() | ||
77 | .into_iter() | ||
78 | .rev() | ||
79 | .into_iter() | ||
80 | .filter_map(|it| it.name().map(Clone::clone)) | ||
81 | .join("::"); | ||
82 | Some(Runnable { | ||
83 | range, | ||
84 | kind: RunnableKind::TestMod { path }, | ||
85 | }) | ||
86 | } | ||