aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_analysis/Cargo.toml2
-rw-r--r--crates/ra_analysis/src/imp.rs21
-rw-r--r--crates/ra_analysis/src/lib.rs7
-rw-r--r--crates/ra_analysis/src/runnables.rs106
-rw-r--r--crates/ra_analysis/tests/tests.rs50
-rw-r--r--crates/ra_hir/src/module.rs2
6 files changed, 64 insertions, 124 deletions
diff --git a/crates/ra_analysis/Cargo.toml b/crates/ra_analysis/Cargo.toml
index a5d4f65ab..c0174cdc5 100644
--- a/crates/ra_analysis/Cargo.toml
+++ b/crates/ra_analysis/Cargo.toml
@@ -5,6 +5,7 @@ version = "0.1.0"
5authors = ["Aleksey Kladov <[email protected]>"] 5authors = ["Aleksey Kladov <[email protected]>"]
6 6
7[dependencies] 7[dependencies]
8itertools = "0.8.0"
8log = "0.4.5" 9log = "0.4.5"
9relative-path = "0.4.0" 10relative-path = "0.4.0"
10rayon = "1.0.2" 11rayon = "1.0.2"
@@ -12,6 +13,7 @@ fst = "0.3.1"
12salsa = "0.9.0" 13salsa = "0.9.0"
13rustc-hash = "1.0" 14rustc-hash = "1.0"
14parking_lot = "0.7.0" 15parking_lot = "0.7.0"
16
15ra_syntax = { path = "../ra_syntax" } 17ra_syntax = { path = "../ra_syntax" }
16ra_editor = { path = "../ra_editor" } 18ra_editor = { path = "../ra_editor" }
17ra_text_edit = { path = "../ra_text_edit" } 19ra_text_edit = { path = "../ra_text_edit" }
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
102impl db::RootDatabase { 102impl 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 @@
1use itertools::Itertools;
1use ra_syntax::{ 2use ra_syntax::{
2 ast::{self, AstNode, NameOwner, ModuleItemOwner}, 3 ast::{self, AstNode, NameOwner, ModuleItemOwner},
3 SourceFileNode, TextRange, SyntaxNodeRef, 4 TextRange, SyntaxNodeRef,
4 TextUnit,
5};
6use crate::{
7 Analysis, FileId, FilePosition
8}; 5};
6use ra_db::{Cancelable, SyntaxDatabase};
7
8use crate::{db::RootDatabase, FileId};
9 9
10#[derive(Debug)] 10#[derive(Debug)]
11pub struct Runnable { 11pub struct Runnable {
@@ -20,53 +20,67 @@ pub enum RunnableKind {
20 Bin, 20 Bin,
21} 21}
22 22
23pub fn runnables( 23pub(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
35fn runnable<'a>(analysis: &Analysis, item: SyntaxNodeRef<'a>, file_id: FileId) -> Option<Runnable> { 33fn 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
43fn 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
60fn 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}
diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs
index 845fff3c6..3045c2e78 100644
--- a/crates/ra_analysis/tests/tests.rs
+++ b/crates/ra_analysis/tests/tests.rs
@@ -132,56 +132,6 @@ fn test_resolve_parent_module_for_inline() {
132} 132}
133 133
134#[test] 134#[test]
135fn test_path_one_layer() {
136 let (analysis, pos) = analysis_and_position(
137 "
138 //- /lib.rs
139 mod foo;
140 //- /foo/mod.rs
141 mod bla;
142 //- /foo/bla.rs
143 <|> //empty
144 ",
145 );
146 let symbols = analysis.module_path(pos).unwrap().unwrap();
147 assert_eq!("foo::bla", &symbols);
148}
149
150#[test]
151fn test_path_two_layer() {
152 let (analysis, pos) = analysis_and_position(
153 "
154 //- /lib.rs
155 mod foo;
156 //- /foo/mod.rs
157 mod bla;
158 //- /foo/bla/mod.rs
159 mod more;
160 //- /foo/bla/more.rs
161 <|> //empty
162 ",
163 );
164 let symbols = analysis.module_path(pos).unwrap().unwrap();
165 assert_eq!("foo::bla::more", &symbols);
166}
167
168#[test]
169fn test_path_in_file_mod() {
170 let (analysis, pos) = analysis_and_position(
171 "
172 //- /lib.rs
173 mod foo;
174 //- /foo.rs
175 mod bar {
176 <|> //empty
177 }
178 ",
179 );
180 let symbols = analysis.module_path(pos).unwrap().unwrap();
181 assert_eq!("foo::bar", &symbols);
182}
183
184#[test]
185fn test_resolve_crate_root() { 135fn test_resolve_crate_root() {
186 let mock = MockAnalysis::with_files( 136 let mock = MockAnalysis::with_files(
187 " 137 "
diff --git a/crates/ra_hir/src/module.rs b/crates/ra_hir/src/module.rs
index 26171d27c..a53b69d20 100644
--- a/crates/ra_hir/src/module.rs
+++ b/crates/ra_hir/src/module.rs
@@ -80,7 +80,7 @@ impl Module {
80 Some(Crate::new(crate_id)) 80 Some(Crate::new(crate_id))
81 } 81 }
82 82
83 /// Returns the all modulkes on the way to the root. 83 /// Returns the all modules on the way to the root.
84 pub fn path_to_root(&self) -> Vec<Module> { 84 pub fn path_to_root(&self) -> Vec<Module> {
85 generate(Some(self.clone()), move |it| it.parent()).collect::<Vec<Module>>() 85 generate(Some(self.clone()), move |it| it.parent()).collect::<Vec<Module>>()
86 } 86 }