diff options
Diffstat (limited to 'crates/ra_analysis/src')
-rw-r--r-- | crates/ra_analysis/src/extend_selection.rs | 29 | ||||
-rw-r--r-- | crates/ra_analysis/src/imp.rs | 22 | ||||
-rw-r--r-- | crates/ra_analysis/src/lib.rs | 14 | ||||
-rw-r--r-- | crates/ra_analysis/src/runnables.rs | 72 | ||||
-rw-r--r-- | crates/ra_analysis/src/syntax_highlighting.rs | 4 |
5 files changed, 123 insertions, 18 deletions
diff --git a/crates/ra_analysis/src/extend_selection.rs b/crates/ra_analysis/src/extend_selection.rs index cde6ee101..805e9059e 100644 --- a/crates/ra_analysis/src/extend_selection.rs +++ b/crates/ra_analysis/src/extend_selection.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use ra_db::SyntaxDatabase; | 1 | use ra_db::SyntaxDatabase; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | SyntaxNodeRef, AstNode, | 3 | SyntaxNodeRef, AstNode, SourceFileNode, |
4 | ast, algo::find_covering_node, | 4 | ast, algo::find_covering_node, |
5 | }; | 5 | }; |
6 | 6 | ||
@@ -11,18 +11,23 @@ use crate::{ | |||
11 | 11 | ||
12 | pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRange { | 12 | pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRange { |
13 | let source_file = db.source_file(frange.file_id); | 13 | let source_file = db.source_file(frange.file_id); |
14 | if let Some(macro_call) = find_macro_call(source_file.syntax(), frange.range) { | 14 | if let Some(range) = extend_selection_in_macro(db, &source_file, frange) { |
15 | if let Some(exp) = crate::macros::expand(db, frange.file_id, macro_call) { | 15 | return range; |
16 | if let Some(dst_range) = exp.map_range_forward(frange.range) { | ||
17 | if let Some(dst_range) = ra_editor::extend_selection(exp.source_file(), dst_range) { | ||
18 | if let Some(src_range) = exp.map_range_back(dst_range) { | ||
19 | return src_range; | ||
20 | } | ||
21 | } | ||
22 | } | ||
23 | } | ||
24 | } | 16 | } |
25 | ra_editor::extend_selection(&source_file, frange.range).unwrap_or(frange.range) | 17 | ra_editor::extend_selection(source_file.syntax(), frange.range).unwrap_or(frange.range) |
18 | } | ||
19 | |||
20 | fn extend_selection_in_macro( | ||
21 | db: &RootDatabase, | ||
22 | source_file: &SourceFileNode, | ||
23 | frange: FileRange, | ||
24 | ) -> Option<TextRange> { | ||
25 | let macro_call = find_macro_call(source_file.syntax(), frange.range)?; | ||
26 | let exp = crate::macros::expand(db, frange.file_id, macro_call)?; | ||
27 | let dst_range = exp.map_range_forward(frange.range)?; | ||
28 | let dst_range = ra_editor::extend_selection(exp.source_file().syntax(), dst_range)?; | ||
29 | let src_range = exp.map_range_back(dst_range)?; | ||
30 | Some(src_range) | ||
26 | } | 31 | } |
27 | 32 | ||
28 | fn find_macro_call(node: SyntaxNodeRef, range: TextRange) -> Option<ast::MacroCall> { | 33 | fn find_macro_call(node: SyntaxNodeRef, range: TextRange) -> Option<ast::MacroCall> { |
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 5ed374c79..5669aa94d 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs | |||
@@ -181,6 +181,28 @@ impl AnalysisImpl { | |||
181 | }; | 181 | }; |
182 | Ok(query.search(&buf)) | 182 | Ok(query.search(&buf)) |
183 | } | 183 | } |
184 | |||
185 | pub(crate) fn module_path(&self, position: FilePosition) -> Cancelable<Option<String>> { | ||
186 | let descr = match source_binder::module_from_position(&*self.db, position)? { | ||
187 | None => return Ok(None), | ||
188 | Some(it) => it, | ||
189 | }; | ||
190 | let name = match descr.name() { | ||
191 | None => return Ok(None), | ||
192 | Some(it) => it.to_string(), | ||
193 | }; | ||
194 | |||
195 | let modules = descr.path_to_root(); | ||
196 | |||
197 | let path = modules | ||
198 | .iter() | ||
199 | .filter_map(|s| s.name()) | ||
200 | .skip(1) // name is already part of the string. | ||
201 | .fold(name, |path, it| format!("{}::{}", it, path)); | ||
202 | |||
203 | Ok(Some(path.to_string())) | ||
204 | } | ||
205 | |||
184 | /// This returns `Vec` because a module may be included from several places. We | 206 | /// This returns `Vec` because a module may be included from several places. We |
185 | /// don't handle this case yet though, so the Vec has length at most one. | 207 | /// don't handle this case yet though, so the Vec has length at most one. |
186 | pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<(FileId, FileSymbol)>> { | 208 | pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<(FileId, FileSymbol)>> { |
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index e56168510..e6cfaecc3 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs | |||
@@ -15,6 +15,7 @@ mod imp; | |||
15 | mod completion; | 15 | mod completion; |
16 | mod symbol_index; | 16 | mod symbol_index; |
17 | pub mod mock_analysis; | 17 | pub mod mock_analysis; |
18 | mod runnables; | ||
18 | 19 | ||
19 | mod extend_selection; | 20 | mod extend_selection; |
20 | mod syntax_highlighting; | 21 | mod syntax_highlighting; |
@@ -33,10 +34,12 @@ use crate::{ | |||
33 | symbol_index::SymbolIndex, | 34 | symbol_index::SymbolIndex, |
34 | }; | 35 | }; |
35 | 36 | ||
36 | pub use crate::completion::{CompletionItem, CompletionItemKind, InsertText}; | 37 | pub use crate::{ |
38 | completion::{CompletionItem, CompletionItemKind, InsertText}, | ||
39 | runnables::{Runnable, RunnableKind} | ||
40 | }; | ||
37 | pub use ra_editor::{ | 41 | pub use ra_editor::{ |
38 | FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, | 42 | FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, StructureNode, Severity |
39 | Severity | ||
40 | }; | 43 | }; |
41 | pub use hir::FnSignatureInfo; | 44 | pub use hir::FnSignatureInfo; |
42 | 45 | ||
@@ -336,6 +339,9 @@ impl Analysis { | |||
336 | pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<(FileId, FileSymbol)>> { | 339 | pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<(FileId, FileSymbol)>> { |
337 | self.imp.parent_module(position) | 340 | self.imp.parent_module(position) |
338 | } | 341 | } |
342 | pub fn module_path(&self, position: FilePosition) -> Cancelable<Option<String>> { | ||
343 | self.imp.module_path(position) | ||
344 | } | ||
339 | pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> { | 345 | pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> { |
340 | self.imp.crate_for(file_id) | 346 | self.imp.crate_for(file_id) |
341 | } | 347 | } |
@@ -344,7 +350,7 @@ impl Analysis { | |||
344 | } | 350 | } |
345 | pub fn runnables(&self, file_id: FileId) -> Cancelable<Vec<Runnable>> { | 351 | pub fn runnables(&self, file_id: FileId) -> Cancelable<Vec<Runnable>> { |
346 | let file = self.imp.file_syntax(file_id); | 352 | let file = self.imp.file_syntax(file_id); |
347 | Ok(ra_editor::runnables(&file)) | 353 | Ok(runnables::runnables(self, &file, file_id)) |
348 | } | 354 | } |
349 | pub fn highlight(&self, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> { | 355 | pub fn highlight(&self, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> { |
350 | syntax_highlighting::highlight(&*self.imp.db, file_id) | 356 | syntax_highlighting::highlight(&*self.imp.db, file_id) |
diff --git a/crates/ra_analysis/src/runnables.rs b/crates/ra_analysis/src/runnables.rs new file mode 100644 index 000000000..61ca0930a --- /dev/null +++ b/crates/ra_analysis/src/runnables.rs | |||
@@ -0,0 +1,72 @@ | |||
1 | use ra_syntax::{ | ||
2 | ast::{self, AstNode, NameOwner, ModuleItemOwner}, | ||
3 | SourceFileNode, TextRange, SyntaxNodeRef, | ||
4 | TextUnit, | ||
5 | }; | ||
6 | use crate::{ | ||
7 | Analysis, FileId, FilePosition | ||
8 | }; | ||
9 | |||
10 | #[derive(Debug)] | ||
11 | pub struct Runnable { | ||
12 | pub range: TextRange, | ||
13 | pub kind: RunnableKind, | ||
14 | } | ||
15 | |||
16 | #[derive(Debug)] | ||
17 | pub enum RunnableKind { | ||
18 | Test { name: String }, | ||
19 | TestMod { path: String }, | ||
20 | Bin, | ||
21 | } | ||
22 | |||
23 | pub fn runnables( | ||
24 | analysis: &Analysis, | ||
25 | file_node: &SourceFileNode, | ||
26 | file_id: FileId, | ||
27 | ) -> Vec<Runnable> { | ||
28 | file_node | ||
29 | .syntax() | ||
30 | .descendants() | ||
31 | .filter_map(|i| runnable(analysis, i, file_id)) | ||
32 | .collect() | ||
33 | } | ||
34 | |||
35 | fn runnable<'a>(analysis: &Analysis, item: SyntaxNodeRef<'a>, file_id: FileId) -> Option<Runnable> { | ||
36 | if let Some(f) = ast::FnDef::cast(item) { | ||
37 | let name = f.name()?.text(); | ||
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) { | ||
52 | if m.item_list()? | ||
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 { | ||
70 | None | ||
71 | } | ||
72 | } | ||
diff --git a/crates/ra_analysis/src/syntax_highlighting.rs b/crates/ra_analysis/src/syntax_highlighting.rs index 38219da71..7e9139a74 100644 --- a/crates/ra_analysis/src/syntax_highlighting.rs +++ b/crates/ra_analysis/src/syntax_highlighting.rs | |||
@@ -9,14 +9,14 @@ use crate::{ | |||
9 | 9 | ||
10 | pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> { | 10 | pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> { |
11 | let source_file = db.source_file(file_id); | 11 | let source_file = db.source_file(file_id); |
12 | let mut res = ra_editor::highlight(&source_file); | 12 | let mut res = ra_editor::highlight(source_file.syntax()); |
13 | for macro_call in source_file | 13 | for macro_call in source_file |
14 | .syntax() | 14 | .syntax() |
15 | .descendants() | 15 | .descendants() |
16 | .filter_map(ast::MacroCall::cast) | 16 | .filter_map(ast::MacroCall::cast) |
17 | { | 17 | { |
18 | if let Some(exp) = crate::macros::expand(db, file_id, macro_call) { | 18 | if let Some(exp) = crate::macros::expand(db, file_id, macro_call) { |
19 | let mapped_ranges = ra_editor::highlight(exp.source_file()) | 19 | let mapped_ranges = ra_editor::highlight(exp.source_file().syntax()) |
20 | .into_iter() | 20 | .into_iter() |
21 | .filter_map(|r| { | 21 | .filter_map(|r| { |
22 | let mapped_range = exp.map_range_back(r.range)?; | 22 | let mapped_range = exp.map_range_back(r.range)?; |