diff options
Diffstat (limited to 'crates/ra_analysis/src')
-rw-r--r-- | crates/ra_analysis/src/imp.rs | 21 | ||||
-rw-r--r-- | crates/ra_analysis/src/lib.rs | 7 | ||||
-rw-r--r-- | crates/ra_analysis/src/runnables.rs | 106 |
3 files changed, 61 insertions, 73 deletions
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 0faf8b85d..ff13247de 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs | |||
@@ -100,27 +100,6 @@ impl db::RootDatabase { | |||
100 | } | 100 | } |
101 | 101 | ||
102 | impl db::RootDatabase { | 102 | impl db::RootDatabase { |
103 | pub(crate) fn module_path(&self, position: FilePosition) -> Cancelable<Option<String>> { | ||
104 | let descr = match source_binder::module_from_position(self, position)? { | ||
105 | None => return Ok(None), | ||
106 | Some(it) => it, | ||
107 | }; | ||
108 | let name = match descr.name() { | ||
109 | None => return Ok(None), | ||
110 | Some(it) => it.to_string(), | ||
111 | }; | ||
112 | |||
113 | let modules = descr.path_to_root(); | ||
114 | |||
115 | let path = modules | ||
116 | .iter() | ||
117 | .filter_map(|s| s.name()) | ||
118 | .skip(1) // name is already part of the string. | ||
119 | .fold(name, |path, it| format!("{}::{}", it, path)); | ||
120 | |||
121 | Ok(Some(path.to_string())) | ||
122 | } | ||
123 | |||
124 | /// This returns `Vec` because a module may be included from several places. We | 103 | /// This returns `Vec` because a module may be included from several places. We |
125 | /// don't handle this case yet though, so the Vec has length at most one. | 104 | /// don't handle this case yet though, so the Vec has length at most one. |
126 | pub(crate) fn parent_module( | 105 | pub(crate) fn parent_module( |
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 54eb2f4d3..a01febf4e 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs | |||
@@ -382,10 +382,6 @@ impl Analysis { | |||
382 | pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<NavigationTarget>> { | 382 | pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<NavigationTarget>> { |
383 | self.db.parent_module(position) | 383 | self.db.parent_module(position) |
384 | } | 384 | } |
385 | /// Returns `::` separated path to the current module from the crate root. | ||
386 | pub fn module_path(&self, position: FilePosition) -> Cancelable<Option<String>> { | ||
387 | self.db.module_path(position) | ||
388 | } | ||
389 | /// Returns crates this file belongs too. | 385 | /// Returns crates this file belongs too. |
390 | pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> { | 386 | pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> { |
391 | self.db.crate_for(file_id) | 387 | self.db.crate_for(file_id) |
@@ -396,8 +392,7 @@ impl Analysis { | |||
396 | } | 392 | } |
397 | /// Returns the set of possible targets to run for the current file. | 393 | /// Returns the set of possible targets to run for the current file. |
398 | pub fn runnables(&self, file_id: FileId) -> Cancelable<Vec<Runnable>> { | 394 | pub fn runnables(&self, file_id: FileId) -> Cancelable<Vec<Runnable>> { |
399 | let file = self.db.source_file(file_id); | 395 | runnables::runnables(&*self.db, file_id) |
400 | Ok(runnables::runnables(self, &file, file_id)) | ||
401 | } | 396 | } |
402 | /// Computes syntax highlighting for the given file. | 397 | /// Computes syntax highlighting for the given file. |
403 | pub fn highlight(&self, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> { | 398 | pub fn highlight(&self, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> { |
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 | } | ||