aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/descriptors
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-10-31 20:41:43 +0000
committerAleksey Kladov <[email protected]>2018-10-31 20:41:43 +0000
commit6be50f7d5de3737464853a589673375fc0cafa97 (patch)
tree2c6da7f3a1234c3f2fd3f330d2c9445953979598 /crates/ra_analysis/src/descriptors
parent857c1650efdb51650458f9ec1119adaa49b34371 (diff)
Reformat all
Diffstat (limited to 'crates/ra_analysis/src/descriptors')
-rw-r--r--crates/ra_analysis/src/descriptors/function/imp.rs12
-rw-r--r--crates/ra_analysis/src/descriptors/function/mod.rs29
-rw-r--r--crates/ra_analysis/src/descriptors/function/scope.rs7
-rw-r--r--crates/ra_analysis/src/descriptors/mod.rs39
-rw-r--r--crates/ra_analysis/src/descriptors/module/imp.rs33
-rw-r--r--crates/ra_analysis/src/descriptors/module/mod.rs30
-rw-r--r--crates/ra_analysis/src/descriptors/module/scope.rs5
7 files changed, 72 insertions, 83 deletions
diff --git a/crates/ra_analysis/src/descriptors/function/imp.rs b/crates/ra_analysis/src/descriptors/function/imp.rs
index 0a006f733..11fffeefc 100644
--- a/crates/ra_analysis/src/descriptors/function/imp.rs
+++ b/crates/ra_analysis/src/descriptors/function/imp.rs
@@ -1,14 +1,10 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use ra_syntax::{ 3use ra_syntax::ast::{AstNode, FnDef, FnDefNode};
4 ast::{AstNode, FnDef, FnDefNode},
5};
6 4
7use crate::{ 5use crate::descriptors::{
8 descriptors::{ 6 function::{FnId, FnScopes},
9 DescriptorDatabase, 7 DescriptorDatabase,
10 function::{FnId, FnScopes},
11 },
12}; 8};
13 9
14/// Resolve `FnId` to the corresponding `SyntaxNode` 10/// Resolve `FnId` to the corresponding `SyntaxNode`
diff --git a/crates/ra_analysis/src/descriptors/function/mod.rs b/crates/ra_analysis/src/descriptors/function/mod.rs
index ae40f3e8f..d5db28a64 100644
--- a/crates/ra_analysis/src/descriptors/function/mod.rs
+++ b/crates/ra_analysis/src/descriptors/function/mod.rs
@@ -1,20 +1,16 @@
1pub(super) mod imp; 1pub(super) mod imp;
2mod scope; 2mod scope;
3 3
4use std::cmp::{min, max}; 4use std::cmp::{max, min};
5 5
6use ra_syntax::{ 6use ra_syntax::{
7 ast::{self, AstNode, DocCommentsOwner, NameOwner}, 7 ast::{self, AstNode, DocCommentsOwner, NameOwner},
8 TextRange, TextUnit 8 TextRange, TextUnit,
9}; 9};
10 10
11use crate::{ 11use crate::{syntax_ptr::SyntaxPtr, FileId};
12 FileId,
13 syntax_ptr::SyntaxPtr
14};
15
16pub(crate) use self::scope::{FnScopes, resolve_local_name};
17 12
13pub(crate) use self::scope::{resolve_local_name, FnScopes};
18 14
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
20pub(crate) struct FnId(SyntaxPtr); 16pub(crate) struct FnId(SyntaxPtr);
@@ -26,14 +22,13 @@ impl FnId {
26 } 22 }
27} 23}
28 24
29
30#[derive(Debug, Clone)] 25#[derive(Debug, Clone)]
31pub struct FnDescriptor { 26pub struct FnDescriptor {
32 pub name: String, 27 pub name: String,
33 pub label: String, 28 pub label: String,
34 pub ret_type: Option<String>, 29 pub ret_type: Option<String>,
35 pub params: Vec<String>, 30 pub params: Vec<String>,
36 pub doc: Option<String> 31 pub doc: Option<String>,
37} 32}
38 33
39impl FnDescriptor { 34impl FnDescriptor {
@@ -57,7 +52,9 @@ impl FnDescriptor {
57 }; 52 };
58 53
59 if let Some((comment_range, docs)) = FnDescriptor::extract_doc_comments(node) { 54 if let Some((comment_range, docs)) = FnDescriptor::extract_doc_comments(node) {
60 let comment_range = comment_range.checked_sub(node.syntax().range().start()).unwrap(); 55 let comment_range = comment_range
56 .checked_sub(node.syntax().range().start())
57 .unwrap();
61 let start = comment_range.start().to_usize(); 58 let start = comment_range.start().to_usize();
62 let end = comment_range.end().to_usize(); 59 let end = comment_range.end().to_usize();
63 60
@@ -94,7 +91,7 @@ impl FnDescriptor {
94 ret_type, 91 ret_type,
95 params, 92 params,
96 label: label.trim().to_owned(), 93 label: label.trim().to_owned(),
97 doc 94 doc,
98 }) 95 })
99 } 96 }
100 97
@@ -105,10 +102,13 @@ impl FnDescriptor {
105 102
106 let comment_text = node.doc_comment_text(); 103 let comment_text = node.doc_comment_text();
107 104
108 let (begin, end) = node.doc_comments() 105 let (begin, end) = node
106 .doc_comments()
109 .map(|comment| comment.syntax().range()) 107 .map(|comment| comment.syntax().range())
110 .map(|range| (range.start().to_usize(), range.end().to_usize())) 108 .map(|range| (range.start().to_usize(), range.end().to_usize()))
111 .fold((std::usize::MAX, std::usize::MIN), |acc, range| (min(acc.0, range.0), max(acc.1, range.1))); 109 .fold((std::usize::MAX, std::usize::MIN), |acc, range| {
110 (min(acc.0, range.0), max(acc.1, range.1))
111 });
112 112
113 let range = TextRange::from_to(TextUnit::from_usize(begin), TextUnit::from_usize(end)); 113 let range = TextRange::from_to(TextUnit::from_usize(begin), TextUnit::from_usize(end));
114 114
@@ -134,4 +134,3 @@ impl FnDescriptor {
134 res 134 res
135 } 135 }
136} 136}
137
diff --git a/crates/ra_analysis/src/descriptors/function/scope.rs b/crates/ra_analysis/src/descriptors/function/scope.rs
index d9929414c..62b46ffba 100644
--- a/crates/ra_analysis/src/descriptors/function/scope.rs
+++ b/crates/ra_analysis/src/descriptors/function/scope.rs
@@ -51,9 +51,7 @@ impl FnScopes {
51 &self.get(scope).entries 51 &self.get(scope).entries
52 } 52 }
53 pub fn scope_chain<'a>(&'a self, node: SyntaxNodeRef) -> impl Iterator<Item = ScopeId> + 'a { 53 pub fn scope_chain<'a>(&'a self, node: SyntaxNodeRef) -> impl Iterator<Item = ScopeId> + 'a {
54 generate(self.scope_for(node), move |&scope| { 54 generate(self.scope_for(node), move |&scope| self.get(scope).parent)
55 self.get(scope).parent
56 })
57 } 55 }
58 fn root_scope(&mut self) -> ScopeId { 56 fn root_scope(&mut self) -> ScopeId {
59 let res = ScopeId(self.scopes.len() as u32); 57 let res = ScopeId(self.scopes.len() as u32);
@@ -273,13 +271,12 @@ pub fn resolve_local_name<'a>(
273 271
274#[cfg(test)] 272#[cfg(test)]
275mod tests { 273mod tests {
274 use ra_editor::find_node_at_offset;
276 use ra_syntax::File; 275 use ra_syntax::File;
277 use test_utils::extract_offset; 276 use test_utils::extract_offset;
278 use ra_editor::{find_node_at_offset};
279 277
280 use super::*; 278 use super::*;
281 279
282
283 fn do_check(code: &str, expected: &[&str]) { 280 fn do_check(code: &str, expected: &[&str]) {
284 let (off, code) = extract_offset(code); 281 let (off, code) = extract_offset(code);
285 let code = { 282 let code = {
diff --git a/crates/ra_analysis/src/descriptors/mod.rs b/crates/ra_analysis/src/descriptors/mod.rs
index e27f8314a..c28764336 100644
--- a/crates/ra_analysis/src/descriptors/mod.rs
+++ b/crates/ra_analysis/src/descriptors/mod.rs
@@ -1,24 +1,22 @@
1pub(crate) mod module;
2pub(crate) mod function; 1pub(crate) mod function;
2pub(crate) mod module;
3 3
4use std::sync::Arc; 4use std::sync::Arc;
5 5
6use ra_syntax::{ 6use ra_syntax::{
7 SmolStr,
8 ast::{self, AstNode, FnDefNode}, 7 ast::{self, AstNode, FnDefNode},
9 TextRange 8 SmolStr, TextRange,
10}; 9};
11 10
12use crate::{ 11use crate::{
13 FileId, Cancelable,
14 db::SyntaxDatabase, 12 db::SyntaxDatabase,
15 descriptors::module::{ModuleTree, ModuleId, ModuleScope}, 13 descriptors::function::{resolve_local_name, FnId, FnScopes},
16 descriptors::function::{FnId, FnScopes, resolve_local_name}, 14 descriptors::module::{ModuleId, ModuleScope, ModuleTree},
17 input::SourceRootId, 15 input::SourceRootId,
18 syntax_ptr::{SyntaxPtrDatabase, LocalSyntaxPtr}, 16 syntax_ptr::{LocalSyntaxPtr, SyntaxPtrDatabase},
17 Cancelable, FileId,
19}; 18};
20 19
21
22salsa::query_group! { 20salsa::query_group! {
23 pub(crate) trait DescriptorDatabase: SyntaxDatabase + SyntaxPtrDatabase { 21 pub(crate) trait DescriptorDatabase: SyntaxDatabase + SyntaxPtrDatabase {
24 fn module_tree(source_root_id: SourceRootId) -> Cancelable<Arc<ModuleTree>> { 22 fn module_tree(source_root_id: SourceRootId) -> Cancelable<Arc<ModuleTree>> {
@@ -49,23 +47,20 @@ salsa::query_group! {
49#[derive(Debug)] 47#[derive(Debug)]
50pub struct ReferenceDescriptor { 48pub struct ReferenceDescriptor {
51 pub range: TextRange, 49 pub range: TextRange,
52 pub name: String 50 pub name: String,
53} 51}
54 52
55#[derive(Debug)] 53#[derive(Debug)]
56pub struct DeclarationDescriptor<'a> { 54pub struct DeclarationDescriptor<'a> {
57 pat: ast::BindPat<'a>, 55 pat: ast::BindPat<'a>,
58 pub range: TextRange 56 pub range: TextRange,
59} 57}
60 58
61impl<'a> DeclarationDescriptor<'a> { 59impl<'a> DeclarationDescriptor<'a> {
62 pub fn new(pat: ast::BindPat) -> DeclarationDescriptor { 60 pub fn new(pat: ast::BindPat) -> DeclarationDescriptor {
63 let range = pat.syntax().range(); 61 let range = pat.syntax().range();
64 62
65 DeclarationDescriptor { 63 DeclarationDescriptor { pat, range }
66 pat,
67 range
68 }
69 } 64 }
70 65
71 pub fn find_all_refs(&self) -> Vec<ReferenceDescriptor> { 66 pub fn find_all_refs(&self) -> Vec<ReferenceDescriptor> {
@@ -73,22 +68,22 @@ impl<'a> DeclarationDescriptor<'a> {
73 68
74 let fn_def = match self.pat.syntax().ancestors().find_map(ast::FnDef::cast) { 69 let fn_def = match self.pat.syntax().ancestors().find_map(ast::FnDef::cast) {
75 Some(def) => def, 70 Some(def) => def,
76 None => return Default::default() 71 None => return Default::default(),
77 }; 72 };
78 73
79 let fn_scopes = FnScopes::new(fn_def); 74 let fn_scopes = FnScopes::new(fn_def);
80 75
81 let refs : Vec<_> = fn_def.syntax().descendants() 76 let refs: Vec<_> = fn_def
77 .syntax()
78 .descendants()
82 .filter_map(ast::NameRef::cast) 79 .filter_map(ast::NameRef::cast)
83 .filter(|name_ref| { 80 .filter(|name_ref| match resolve_local_name(*name_ref, &fn_scopes) {
84 match resolve_local_name(*name_ref, &fn_scopes) { 81 None => false,
85 None => false, 82 Some(entry) => entry.ptr() == name_ptr,
86 Some(entry) => entry.ptr() == name_ptr,
87 }
88 }) 83 })
89 .map(|name_ref| ReferenceDescriptor { 84 .map(|name_ref| ReferenceDescriptor {
90 name: name_ref.syntax().text().to_string(), 85 name: name_ref.syntax().text().to_string(),
91 range : name_ref.syntax().range(), 86 range: name_ref.syntax().range(),
92 }) 87 })
93 .collect(); 88 .collect();
94 89
diff --git a/crates/ra_analysis/src/descriptors/module/imp.rs b/crates/ra_analysis/src/descriptors/module/imp.rs
index dae3a356d..1c102f4e5 100644
--- a/crates/ra_analysis/src/descriptors/module/imp.rs
+++ b/crates/ra_analysis/src/descriptors/module/imp.rs
@@ -1,24 +1,25 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use relative_path::RelativePathBuf;
4use rustc_hash::{FxHashMap, FxHashSet};
5use ra_syntax::{ 3use ra_syntax::{
6 SmolStr,
7 ast::{self, NameOwner}, 4 ast::{self, NameOwner},
5 SmolStr,
8}; 6};
7use relative_path::RelativePathBuf;
8use rustc_hash::{FxHashMap, FxHashSet};
9 9
10use crate::{ 10use crate::{
11 FileId, Cancelable, FileResolverImp, db, 11 db,
12 input::{SourceRoot, SourceRootId},
13 descriptors::DescriptorDatabase, 12 descriptors::DescriptorDatabase,
13 input::{SourceRoot, SourceRootId},
14 Cancelable, FileId, FileResolverImp,
14}; 15};
15 16
16use super::{ 17use super::{LinkData, LinkId, ModuleData, ModuleId, ModuleScope, ModuleTree, Problem};
17 ModuleData, ModuleTree, ModuleId, LinkId, LinkData, Problem, ModuleScope
18};
19
20 18
21pub(crate) fn submodules(db: &impl DescriptorDatabase, file_id: FileId) -> Cancelable<Arc<Vec<SmolStr>>> { 19pub(crate) fn submodules(
20 db: &impl DescriptorDatabase,
21 file_id: FileId,
22) -> Cancelable<Arc<Vec<SmolStr>>> {
22 db::check_canceled(db)?; 23 db::check_canceled(db)?;
23 let file = db.file_syntax(file_id); 24 let file = db.file_syntax(file_id);
24 let root = file.ast(); 25 let root = file.ast();
@@ -57,13 +58,11 @@ pub(crate) fn module_tree(
57 Ok(Arc::new(res)) 58 Ok(Arc::new(res))
58} 59}
59 60
60
61#[derive(Clone, Hash, PartialEq, Eq, Debug)] 61#[derive(Clone, Hash, PartialEq, Eq, Debug)]
62pub struct Submodule { 62pub struct Submodule {
63 pub name: SmolStr, 63 pub name: SmolStr,
64} 64}
65 65
66
67fn create_module_tree<'a>( 66fn create_module_tree<'a>(
68 db: &impl DescriptorDatabase, 67 db: &impl DescriptorDatabase,
69 source_root: SourceRootId, 68 source_root: SourceRootId,
@@ -82,7 +81,15 @@ fn create_module_tree<'a>(
82 continue; // TODO: use explicit crate_roots here 81 continue; // TODO: use explicit crate_roots here
83 } 82 }
84 assert!(!roots.contains_key(&file_id)); 83 assert!(!roots.contains_key(&file_id));
85 let module_id = build_subtree(db, &source_root, &mut tree, &mut visited, &mut roots, None, file_id)?; 84 let module_id = build_subtree(
85 db,
86 &source_root,
87 &mut tree,
88 &mut visited,
89 &mut roots,
90 None,
91 file_id,
92 )?;
86 roots.insert(file_id, module_id); 93 roots.insert(file_id, module_id);
87 } 94 }
88 Ok(tree) 95 Ok(tree)
diff --git a/crates/ra_analysis/src/descriptors/module/mod.rs b/crates/ra_analysis/src/descriptors/module/mod.rs
index 667553f74..302e3e81c 100644
--- a/crates/ra_analysis/src/descriptors/module/mod.rs
+++ b/crates/ra_analysis/src/descriptors/module/mod.rs
@@ -1,8 +1,11 @@
1pub(super) mod imp; 1pub(super) mod imp;
2pub(crate) mod scope; 2pub(crate) mod scope;
3 3
4use ra_syntax::{
5 ast::{self, AstNode, NameOwner},
6 SmolStr, SyntaxNode,
7};
4use relative_path::RelativePathBuf; 8use relative_path::RelativePathBuf;
5use ra_syntax::{ast::{self, NameOwner, AstNode}, SmolStr, SyntaxNode};
6 9
7use crate::FileId; 10use crate::FileId;
8 11
@@ -16,9 +19,11 @@ pub(crate) struct ModuleTree {
16 19
17impl ModuleTree { 20impl ModuleTree {
18 pub(crate) fn modules_for_file(&self, file_id: FileId) -> Vec<ModuleId> { 21 pub(crate) fn modules_for_file(&self, file_id: FileId) -> Vec<ModuleId> {
19 self.mods.iter() 22 self.mods
23 .iter()
20 .enumerate() 24 .enumerate()
21 .filter(|(_idx, it)| it.file_id == file_id).map(|(idx, _)| ModuleId(idx as u32)) 25 .filter(|(_idx, it)| it.file_id == file_id)
26 .map(|(idx, _)| ModuleId(idx as u32))
22 .collect() 27 .collect()
23 } 28 }
24 29
@@ -50,7 +55,7 @@ impl ModuleId {
50 } 55 }
51 pub(crate) fn parent_link(self, tree: &ModuleTree) -> Option<LinkId> { 56 pub(crate) fn parent_link(self, tree: &ModuleTree) -> Option<LinkId> {
52 tree.module(self).parent 57 tree.module(self).parent
53 } 58 }
54 pub(crate) fn parent(self, tree: &ModuleTree) -> Option<ModuleId> { 59 pub(crate) fn parent(self, tree: &ModuleTree) -> Option<ModuleId> {
55 let link = self.parent_link(tree)?; 60 let link = self.parent_link(tree)?;
56 Some(tree.link(link).owner) 61 Some(tree.link(link).owner)
@@ -69,18 +74,15 @@ impl ModuleId {
69 curr 74 curr
70 } 75 }
71 pub(crate) fn child(self, tree: &ModuleTree, name: &str) -> Option<ModuleId> { 76 pub(crate) fn child(self, tree: &ModuleTree, name: &str) -> Option<ModuleId> {
72 let link = tree.module(self) 77 let link = tree
78 .module(self)
73 .children 79 .children
74 .iter() 80 .iter()
75 .map(|&it| tree.link(it)) 81 .map(|&it| tree.link(it))
76 .find(|it| it.name == name)?; 82 .find(|it| it.name == name)?;
77 Some(*link.points_to.first()?) 83 Some(*link.points_to.first()?)
78 } 84 }
79 pub(crate) fn problems( 85 pub(crate) fn problems(self, tree: &ModuleTree, root: ast::Root) -> Vec<(SyntaxNode, Problem)> {
80 self,
81 tree: &ModuleTree,
82 root: ast::Root,
83 ) -> Vec<(SyntaxNode, Problem)> {
84 tree.module(self) 86 tree.module(self)
85 .children 87 .children
86 .iter() 88 .iter()
@@ -98,11 +100,7 @@ impl LinkId {
98 pub(crate) fn owner(self, tree: &ModuleTree) -> ModuleId { 100 pub(crate) fn owner(self, tree: &ModuleTree) -> ModuleId {
99 tree.link(self).owner 101 tree.link(self).owner
100 } 102 }
101 pub(crate) fn bind_source<'a>( 103 pub(crate) fn bind_source<'a>(self, tree: &ModuleTree, root: ast::Root<'a>) -> ast::Module<'a> {
102 self,
103 tree: &ModuleTree,
104 root: ast::Root<'a>,
105 ) -> ast::Module<'a> {
106 imp::modules(root) 104 imp::modules(root)
107 .find(|(name, _)| name == &tree.link(self).name) 105 .find(|(name, _)| name == &tree.link(self).name)
108 .unwrap() 106 .unwrap()
@@ -125,7 +123,6 @@ struct LinkData {
125 problem: Option<Problem>, 123 problem: Option<Problem>,
126} 124}
127 125
128
129impl ModuleTree { 126impl ModuleTree {
130 fn module(&self, id: ModuleId) -> &ModuleData { 127 fn module(&self, id: ModuleId) -> &ModuleData {
131 &self.mods[id.0 as usize] 128 &self.mods[id.0 as usize]
@@ -152,4 +149,3 @@ impl ModuleTree {
152 id 149 id
153 } 150 }
154} 151}
155
diff --git a/crates/ra_analysis/src/descriptors/module/scope.rs b/crates/ra_analysis/src/descriptors/module/scope.rs
index 846b8b44f..681e272c2 100644
--- a/crates/ra_analysis/src/descriptors/module/scope.rs
+++ b/crates/ra_analysis/src/descriptors/module/scope.rs
@@ -1,9 +1,8 @@
1//! Backend for module-level scope resolution & completion 1//! Backend for module-level scope resolution & completion
2 2
3
4use ra_syntax::{ 3use ra_syntax::{
5 ast::{self, ModuleItemOwner}, 4 ast::{self, ModuleItemOwner},
6 File, AstNode, SmolStr, 5 AstNode, File, SmolStr,
7}; 6};
8 7
9use crate::syntax_ptr::LocalSyntaxPtr; 8use crate::syntax_ptr::LocalSyntaxPtr;
@@ -103,7 +102,7 @@ fn collect_imports(tree: ast::UseTree, acc: &mut Vec<Entry>) {
103#[cfg(test)] 102#[cfg(test)]
104mod tests { 103mod tests {
105 use super::*; 104 use super::*;
106 use ra_syntax::{File}; 105 use ra_syntax::File;
107 106
108 fn do_check(code: &str, expected: &[&str]) { 107 fn do_check(code: &str, expected: &[&str]) {
109 let file = File::parse(&code); 108 let file = File::parse(&code);