aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/descriptors/mod.rs
diff options
context:
space:
mode:
authorJeremy A. Kolb <[email protected]>2018-10-24 19:24:32 +0100
committerAleksey Kladov <[email protected]>2018-10-31 20:30:57 +0000
commit406f366ccc8b903f457cc694dc1214794c0cfc88 (patch)
tree73fded51f2b3f41a0f08683291cda25b0cfef59a /crates/ra_analysis/src/descriptors/mod.rs
parent55ebe6380aef233fff86b7e6cead361787bf1f65 (diff)
Add DeclarationDescriptor and ReferenceDescriptor
Fixes #142 Fixes #146
Diffstat (limited to 'crates/ra_analysis/src/descriptors/mod.rs')
-rw-r--r--crates/ra_analysis/src/descriptors/mod.rs57
1 files changed, 54 insertions, 3 deletions
diff --git a/crates/ra_analysis/src/descriptors/mod.rs b/crates/ra_analysis/src/descriptors/mod.rs
index 0c4991757..e27f8314a 100644
--- a/crates/ra_analysis/src/descriptors/mod.rs
+++ b/crates/ra_analysis/src/descriptors/mod.rs
@@ -5,16 +5,17 @@ use std::sync::Arc;
5 5
6use ra_syntax::{ 6use ra_syntax::{
7 SmolStr, 7 SmolStr,
8 ast::{FnDefNode}, 8 ast::{self, AstNode, FnDefNode},
9 TextRange
9}; 10};
10 11
11use crate::{ 12use crate::{
12 FileId, Cancelable, 13 FileId, Cancelable,
13 db::SyntaxDatabase, 14 db::SyntaxDatabase,
14 descriptors::module::{ModuleTree, ModuleId, ModuleScope}, 15 descriptors::module::{ModuleTree, ModuleId, ModuleScope},
15 descriptors::function::{FnId, FnScopes}, 16 descriptors::function::{FnId, FnScopes, resolve_local_name},
16 input::SourceRootId, 17 input::SourceRootId,
17 syntax_ptr::SyntaxPtrDatabase, 18 syntax_ptr::{SyntaxPtrDatabase, LocalSyntaxPtr},
18}; 19};
19 20
20 21
@@ -44,3 +45,53 @@ salsa::query_group! {
44 } 45 }
45 } 46 }
46} 47}
48
49#[derive(Debug)]
50pub struct ReferenceDescriptor {
51 pub range: TextRange,
52 pub name: String
53}
54
55#[derive(Debug)]
56pub struct DeclarationDescriptor<'a> {
57 pat: ast::BindPat<'a>,
58 pub range: TextRange
59}
60
61impl<'a> DeclarationDescriptor<'a> {
62 pub fn new(pat: ast::BindPat) -> DeclarationDescriptor {
63 let range = pat.syntax().range();
64
65 DeclarationDescriptor {
66 pat,
67 range
68 }
69 }
70
71 pub fn find_all_refs(&self) -> Vec<ReferenceDescriptor> {
72 let name_ptr = LocalSyntaxPtr::new(self.pat.syntax());
73
74 let fn_def = match self.pat.syntax().ancestors().find_map(ast::FnDef::cast) {
75 Some(def) => def,
76 None => return Default::default()
77 };
78
79 let fn_scopes = FnScopes::new(fn_def);
80
81 let refs : Vec<_> = fn_def.syntax().descendants()
82 .filter_map(ast::NameRef::cast)
83 .filter(|name_ref| {
84 match resolve_local_name(*name_ref, &fn_scopes) {
85 None => false,
86 Some(entry) => entry.ptr() == name_ptr,
87 }
88 })
89 .map(|name_ref| ReferenceDescriptor {
90 name: name_ref.syntax().text().to_string(),
91 range : name_ref.syntax().range(),
92 })
93 .collect();
94
95 refs
96 }
97}