Skip to content

Commit 5173c62

Browse files
committed
move find_references to references
1 parent f5bb704 commit 5173c62

File tree

3 files changed

+55
-67
lines changed

3 files changed

+55
-67
lines changed

crates/ra_ide_api/src/imp.rs

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ use hir::{
22
self, Problem, source_binder
33
};
44
use ra_ide_api_light::{self, LocalEdit, Severity};
5-
use ra_syntax::{
6-
algo::find_node_at_offset, ast::{self, NameOwner}, AstNode,
7-
SourceFile,
8-
TextRange,
9-
};
5+
use ra_syntax::ast;
106
use ra_db::SourceDatabase;
117

128
use crate::{
@@ -16,54 +12,6 @@ use crate::{
1612
};
1713

1814
impl db::RootDatabase {
19-
pub(crate) fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> {
20-
let file = self.parse(position.file_id);
21-
// Find the binding associated with the offset
22-
let (binding, descr) = match find_binding(self, &file, position) {
23-
None => return Vec::new(),
24-
Some(it) => it,
25-
};
26-
27-
let mut ret = binding
28-
.name()
29-
.into_iter()
30-
.map(|name| (position.file_id, name.syntax().range()))
31-
.collect::<Vec<_>>();
32-
ret.extend(
33-
descr
34-
.scopes(self)
35-
.find_all_refs(binding)
36-
.into_iter()
37-
.map(|ref_desc| (position.file_id, ref_desc.range)),
38-
);
39-
40-
return ret;
41-
42-
fn find_binding<'a>(
43-
db: &db::RootDatabase,
44-
source_file: &'a SourceFile,
45-
position: FilePosition,
46-
) -> Option<(&'a ast::BindPat, hir::Function)> {
47-
let syntax = source_file.syntax();
48-
if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) {
49-
let descr = source_binder::function_from_child_node(
50-
db,
51-
position.file_id,
52-
binding.syntax(),
53-
)?;
54-
return Some((binding, descr));
55-
};
56-
let name_ref = find_node_at_offset::<ast::NameRef>(syntax, position.offset)?;
57-
let descr =
58-
source_binder::function_from_child_node(db, position.file_id, name_ref.syntax())?;
59-
let scope = descr.scopes(db);
60-
let resolved = scope.resolve_local_name(name_ref)?;
61-
let resolved = resolved.ptr().to_node(source_file);
62-
let binding = find_node_at_offset::<ast::BindPat>(syntax, resolved.range().end())?;
63-
Some((binding, descr))
64-
}
65-
}
66-
6715
pub(crate) fn diagnostics(&self, file_id: FileId) -> Vec<Diagnostic> {
6816
let syntax = self.parse(file_id);
6917

crates/ra_ide_api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl Analysis {
322322

323323
/// Finds all usages of the reference at point.
324324
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
325-
self.with_db(|db| db.find_all_refs(position))
325+
self.with_db(|db| references::find_all_refs(db, position))
326326
}
327327

328328
/// Returns a short text descrbing element at position.

crates/ra_ide_api/src/references.rs

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
use relative_path::RelativePathBuf;
2-
3-
use hir::{
4-
self, ModuleSource, source_binder::module_from_declaration,
5-
};
1+
use relative_path::{RelativePath, RelativePathBuf};
2+
use hir::{ModuleSource, source_binder};
3+
use ra_db::{FileId, SourceDatabase};
64
use ra_syntax::{
5+
AstNode, SyntaxNode, TextRange, SourceFile,
6+
ast::{self, NameOwner},
77
algo::find_node_at_offset,
8-
ast,
9-
AstNode,
10-
SyntaxNode
118
};
129

1310
use crate::{
@@ -17,8 +14,51 @@ use crate::{
1714
SourceChange,
1815
SourceFileEdit,
1916
};
20-
use ra_db::SourceDatabase;
21-
use relative_path::RelativePath;
17+
18+
pub(crate) fn find_all_refs(db: &RootDatabase, position: FilePosition) -> Vec<(FileId, TextRange)> {
19+
let file = db.parse(position.file_id);
20+
// Find the binding associated with the offset
21+
let (binding, descr) = match find_binding(db, &file, position) {
22+
None => return Vec::new(),
23+
Some(it) => it,
24+
};
25+
26+
let mut ret = binding
27+
.name()
28+
.into_iter()
29+
.map(|name| (position.file_id, name.syntax().range()))
30+
.collect::<Vec<_>>();
31+
ret.extend(
32+
descr
33+
.scopes(db)
34+
.find_all_refs(binding)
35+
.into_iter()
36+
.map(|ref_desc| (position.file_id, ref_desc.range)),
37+
);
38+
39+
return ret;
40+
41+
fn find_binding<'a>(
42+
db: &RootDatabase,
43+
source_file: &'a SourceFile,
44+
position: FilePosition,
45+
) -> Option<(&'a ast::BindPat, hir::Function)> {
46+
let syntax = source_file.syntax();
47+
if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) {
48+
let descr =
49+
source_binder::function_from_child_node(db, position.file_id, binding.syntax())?;
50+
return Some((binding, descr));
51+
};
52+
let name_ref = find_node_at_offset::<ast::NameRef>(syntax, position.offset)?;
53+
let descr =
54+
source_binder::function_from_child_node(db, position.file_id, name_ref.syntax())?;
55+
let scope = descr.scopes(db);
56+
let resolved = scope.resolve_local_name(name_ref)?;
57+
let resolved = resolved.ptr().to_node(source_file);
58+
let binding = find_node_at_offset::<ast::BindPat>(syntax, resolved.range().end())?;
59+
Some((binding, descr))
60+
}
61+
}
2262

2363
pub(crate) fn rename(
2464
db: &RootDatabase,
@@ -57,7 +97,8 @@ fn rename_mod(
5797
) -> Option<SourceChange> {
5898
let mut source_file_edits = Vec::new();
5999
let mut file_system_edits = Vec::new();
60-
if let Some(module) = module_from_declaration(db, position.file_id, &ast_module) {
100+
if let Some(module) = source_binder::module_from_declaration(db, position.file_id, &ast_module)
101+
{
61102
let (file_id, module_source) = module.definition_source(db);
62103
match module_source {
63104
ModuleSource::SourceFile(..) => {
@@ -108,8 +149,7 @@ fn rename_reference(
108149
position: FilePosition,
109150
new_name: &str,
110151
) -> Option<SourceChange> {
111-
let edit = db
112-
.find_all_refs(position)
152+
let edit = find_all_refs(db, position)
113153
.iter()
114154
.map(|(file_id, text_range)| SourceFileEdit {
115155
file_id: *file_id,

0 commit comments

Comments
 (0)