Skip to content

Commit 3b5bd73

Browse files
committed
Add DeclarationDescriptor and ReferenceDescriptor
Fixes #142 Fixes #146
1 parent 8ada1a2 commit 3b5bd73

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

crates/ra_analysis/src/descriptors/mod.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
pub(crate) mod module;
22

3+
use ra_editor::resolve_local_name;
4+
35
use ra_syntax::{
46
ast::{self, AstNode, NameOwner},
57
text_utils::is_subrange,
8+
TextRange
69
};
710

811
#[derive(Debug, Clone)]
@@ -61,3 +64,49 @@ impl FnDescriptor {
6164
res
6265
}
6366
}
67+
68+
#[derive(Debug)]
69+
pub struct ReferenceDescriptor {
70+
pub range: TextRange,
71+
pub name: String
72+
}
73+
74+
#[derive(Debug)]
75+
pub struct DeclarationDescriptor<'a> {
76+
pat: ast::BindPat<'a>,
77+
pub range: TextRange
78+
}
79+
80+
impl<'a> DeclarationDescriptor<'a> {
81+
pub fn new(pat: ast::BindPat) -> DeclarationDescriptor {
82+
let range = pat.syntax().range();
83+
84+
DeclarationDescriptor {
85+
pat,
86+
range
87+
}
88+
}
89+
90+
pub fn find_all_refs(&self) -> Vec<ReferenceDescriptor> {
91+
let name = match self.pat.name() {
92+
Some(name) => name,
93+
None => return Default::default()
94+
};
95+
96+
let fn_def = match name.syntax().ancestors().find_map(ast::FnDef::cast) {
97+
Some(def) => def,
98+
None => return Default::default()
99+
};
100+
101+
let refs : Vec<_> = fn_def.syntax().descendants()
102+
.filter_map(ast::NameRef::cast)
103+
.filter(|name_ref| resolve_local_name(*name_ref) == Some((name.text(), name.syntax().range())))
104+
.map(|name_ref| ReferenceDescriptor {
105+
name: name_ref.syntax().text().to_string(),
106+
range : name_ref.syntax().range(),
107+
})
108+
.collect();
109+
110+
refs
111+
}
112+
}

crates/ra_analysis/src/imp.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_hash::FxHashSet;
1717

1818
use crate::{
1919
descriptors::module::{ModuleTree, Problem},
20-
descriptors::{FnDescriptor},
20+
descriptors::{DeclarationDescriptor, FnDescriptor},
2121
roots::{ReadonlySourceRoot, SourceRoot, WritableSourceRoot},
2222
CrateGraph, CrateId, Diagnostic, FileId, FileResolver, FileSystemEdit, Position,
2323
Query, SourceChange, SourceFileEdit, Cancelable,
@@ -258,6 +258,17 @@ impl AnalysisImpl {
258258

259259
let mut ret = vec![];
260260

261+
if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, offset) {
262+
let decl = DeclarationDescriptor::new(binding);
263+
264+
ret.push((file_id, decl.range));
265+
266+
ret.extend(decl.find_all_refs().into_iter()
267+
.map(|ref_desc| (file_id, ref_desc.range )));
268+
269+
return ret;
270+
}
271+
261272
// Find the symbol we are looking for
262273
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) {
263274

crates/ra_analysis/tests/tests.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,14 @@ fn test_find_all_refs_for_param_inside() {
264264
let refs = get_all_refs(code);
265265
assert_eq!(refs.len(), 2);
266266
}
267+
268+
#[test]
269+
fn test_find_all_refs_for_fn_param() {
270+
let code = r#"
271+
fn foo(i<|> : u32) -> u32 {
272+
i
273+
}"#;
274+
275+
let refs = get_all_refs(code);
276+
assert_eq!(refs.len(), 2);
277+
}

0 commit comments

Comments
 (0)