aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r--crates/ra_ide_api/src/lib.rs6
-rw-r--r--crates/ra_ide_api/src/navigation_target.rs16
-rw-r--r--crates/ra_ide_api/src/references.rs106
-rw-r--r--crates/ra_ide_api/tests/test/main.rs7
4 files changed, 98 insertions, 37 deletions
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs
index 1746b58ae..57a490fa7 100644
--- a/crates/ra_ide_api/src/lib.rs
+++ b/crates/ra_ide_api/src/lib.rs
@@ -56,6 +56,7 @@ pub use crate::{
56 completion::{CompletionItem, CompletionItemKind, InsertTextFormat}, 56 completion::{CompletionItem, CompletionItemKind, InsertTextFormat},
57 runnables::{Runnable, RunnableKind}, 57 runnables::{Runnable, RunnableKind},
58 navigation_target::NavigationTarget, 58 navigation_target::NavigationTarget,
59 references::ReferenceSearchResult,
59}; 60};
60pub use ra_ide_api_light::{ 61pub use ra_ide_api_light::{
61 Fold, FoldKind, HighlightedRange, Severity, StructureNode, LocalEdit, 62 Fold, FoldKind, HighlightedRange, Severity, StructureNode, LocalEdit,
@@ -319,7 +320,10 @@ impl Analysis {
319 } 320 }
320 321
321 /// Finds all usages of the reference at point. 322 /// Finds all usages of the reference at point.
322 pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> { 323 pub fn find_all_refs(
324 &self,
325 position: FilePosition,
326 ) -> Cancelable<Option<ReferenceSearchResult>> {
323 self.with_db(|db| references::find_all_refs(db, position)) 327 self.with_db(|db| references::find_all_refs(db, position))
324 } 328 }
325 329
diff --git a/crates/ra_ide_api/src/navigation_target.rs b/crates/ra_ide_api/src/navigation_target.rs
index 004921863..fd001179a 100644
--- a/crates/ra_ide_api/src/navigation_target.rs
+++ b/crates/ra_ide_api/src/navigation_target.rs
@@ -23,6 +23,12 @@ pub struct NavigationTarget {
23} 23}
24 24
25impl NavigationTarget { 25impl NavigationTarget {
26 /// When `focus_range` is specified, returns it. otherwise
27 /// returns `full_range`
28 pub fn range(&self) -> TextRange {
29 self.focus_range.unwrap_or(self.full_range)
30 }
31
26 pub fn name(&self) -> &SmolStr { 32 pub fn name(&self) -> &SmolStr {
27 &self.name 33 &self.name
28 } 34 }
@@ -43,14 +49,18 @@ impl NavigationTarget {
43 self.full_range 49 self.full_range
44 } 50 }
45 51
46 /// A "most interesting" range withing the `range_full`. 52 /// A "most interesting" range withing the `full_range`.
47 /// 53 ///
48 /// Typically, `range` is the whole syntax node, including doc comments, and 54 /// Typically, `full_range` is the whole syntax node,
49 /// `focus_range` is the range of the identifier. 55 /// including doc comments, and `focus_range` is the range of the identifier.
50 pub fn focus_range(&self) -> Option<TextRange> { 56 pub fn focus_range(&self) -> Option<TextRange> {
51 self.focus_range 57 self.focus_range
52 } 58 }
53 59
60 pub(crate) fn from_bind_pat(file_id: FileId, pat: &ast::BindPat) -> NavigationTarget {
61 NavigationTarget::from_named(file_id, pat)
62 }
63
54 pub(crate) fn from_symbol(symbol: FileSymbol) -> NavigationTarget { 64 pub(crate) fn from_symbol(symbol: FileSymbol) -> NavigationTarget {
55 NavigationTarget { 65 NavigationTarget {
56 file_id: symbol.file_id, 66 file_id: symbol.file_id,
diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs
index e7ebf9f6e..b7784e577 100644
--- a/crates/ra_ide_api/src/references.rs
+++ b/crates/ra_ide_api/src/references.rs
@@ -1,42 +1,77 @@
1use relative_path::{RelativePath, RelativePathBuf}; 1use relative_path::{RelativePath, RelativePathBuf};
2use hir::{ModuleSource, source_binder}; 2use hir::{ModuleSource, source_binder};
3use ra_db::{FileId, SourceDatabase}; 3use ra_db::{SourceDatabase};
4use ra_syntax::{ 4use ra_syntax::{
5 AstNode, SyntaxNode, TextRange, SourceFile, 5 AstNode, SyntaxNode, SourceFile,
6 ast::{self, NameOwner}, 6 ast,
7 algo::find_node_at_offset, 7 algo::find_node_at_offset,
8}; 8};
9 9
10use crate::{ 10use crate::{
11 db::RootDatabase, 11 db::RootDatabase,
12 FilePosition, 12 FilePosition,
13 FileRange,
14 FileId,
15 NavigationTarget,
13 FileSystemEdit, 16 FileSystemEdit,
14 SourceChange, 17 SourceChange,
15 SourceFileEdit, 18 SourceFileEdit,
19 TextRange,
16}; 20};
17 21
18pub(crate) fn find_all_refs(db: &RootDatabase, position: FilePosition) -> Vec<(FileId, TextRange)> { 22#[derive(Debug, Clone)]
23pub struct ReferenceSearchResult {
24 declaration: NavigationTarget,
25 references: Vec<FileRange>,
26}
27
28impl ReferenceSearchResult {
29 pub fn declaration(&self) -> &NavigationTarget {
30 &self.declaration
31 }
32
33 pub fn references(&self) -> &[FileRange] {
34 &self.references
35 }
36
37 /// Total number of references
38 /// At least 1 since all valid references should
39 /// Have a declaration
40 pub fn len(&self) -> usize {
41 self.references.len() + 1
42 }
43}
44
45// allow turning ReferenceSearchResult into an iterator
46// over FileRanges
47impl IntoIterator for ReferenceSearchResult {
48 type Item = FileRange;
49 type IntoIter = std::vec::IntoIter<FileRange>;
50
51 fn into_iter(mut self) -> Self::IntoIter {
52 let mut v = Vec::with_capacity(self.len());
53 v.push(FileRange { file_id: self.declaration.file_id(), range: self.declaration.range() });
54 v.append(&mut self.references);
55 v.into_iter()
56 }
57}
58
59pub(crate) fn find_all_refs(
60 db: &RootDatabase,
61 position: FilePosition,
62) -> Option<ReferenceSearchResult> {
19 let file = db.parse(position.file_id); 63 let file = db.parse(position.file_id);
20 // Find the binding associated with the offset 64 let (binding, descr) = find_binding(db, &file, position)?;
21 let (binding, descr) = match find_binding(db, &file, position) { 65 let declaration = NavigationTarget::from_bind_pat(position.file_id, binding);
22 None => return Vec::new(),
23 Some(it) => it,
24 };
25 66
26 let mut ret = binding 67 let references = descr
27 .name() 68 .scopes(db)
69 .find_all_refs(binding)
28 .into_iter() 70 .into_iter()
29 .map(|name| (position.file_id, name.syntax().range())) 71 .map(move |ref_desc| FileRange { file_id: position.file_id, range: ref_desc.range })
30 .collect::<Vec<_>>(); 72 .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 73
39 return ret; 74 return Some(ReferenceSearchResult { declaration, references });
40 75
41 fn find_binding<'a>( 76 fn find_binding<'a>(
42 db: &RootDatabase, 77 db: &RootDatabase,
@@ -88,6 +123,21 @@ fn find_name_and_module_at_offset(
88 None 123 None
89} 124}
90 125
126fn source_edit_from_fileid_range(
127 file_id: FileId,
128 range: TextRange,
129 new_name: &str,
130) -> SourceFileEdit {
131 SourceFileEdit {
132 file_id,
133 edit: {
134 let mut builder = ra_text_edit::TextEditBuilder::default();
135 builder.replace(range, new_name.into());
136 builder.finish()
137 },
138 }
139}
140
91fn rename_mod( 141fn rename_mod(
92 db: &RootDatabase, 142 db: &RootDatabase,
93 ast_name: &ast::Name, 143 ast_name: &ast::Name,
@@ -150,17 +200,13 @@ fn rename_reference(
150 position: FilePosition, 200 position: FilePosition,
151 new_name: &str, 201 new_name: &str,
152) -> Option<SourceChange> { 202) -> Option<SourceChange> {
153 let edit = find_all_refs(db, position) 203 let refs = find_all_refs(db, position)?;
154 .iter() 204
155 .map(|(file_id, text_range)| SourceFileEdit { 205 let edit = refs
156 file_id: *file_id, 206 .into_iter()
157 edit: { 207 .map(|range| source_edit_from_fileid_range(range.file_id, range.range, new_name))
158 let mut builder = ra_text_edit::TextEditBuilder::default();
159 builder.replace(*text_range, new_name.into());
160 builder.finish()
161 },
162 })
163 .collect::<Vec<_>>(); 208 .collect::<Vec<_>>();
209
164 if edit.is_empty() { 210 if edit.is_empty() {
165 return None; 211 return None;
166 } 212 }
diff --git a/crates/ra_ide_api/tests/test/main.rs b/crates/ra_ide_api/tests/test/main.rs
index 0526f7584..a83fbe07b 100644
--- a/crates/ra_ide_api/tests/test/main.rs
+++ b/crates/ra_ide_api/tests/test/main.rs
@@ -1,7 +1,8 @@
1use insta::assert_debug_snapshot_matches; 1use insta::assert_debug_snapshot_matches;
2use ra_ide_api::{ 2use ra_ide_api::{
3 mock_analysis::{single_file, single_file_with_position, MockAnalysis}, 3 mock_analysis::{single_file, single_file_with_position, MockAnalysis},
4 AnalysisChange, CrateGraph, Edition::Edition2018, FileId, Query, NavigationTarget 4 AnalysisChange, CrateGraph, Edition::Edition2018, Query, NavigationTarget,
5 ReferenceSearchResult,
5}; 6};
6use ra_syntax::{TextRange, SmolStr}; 7use ra_syntax::{TextRange, SmolStr};
7 8
@@ -44,9 +45,9 @@ fn test_resolve_crate_root() {
44 assert_eq!(host.analysis().crate_for(mod_file).unwrap(), vec![crate_id]); 45 assert_eq!(host.analysis().crate_for(mod_file).unwrap(), vec![crate_id]);
45} 46}
46 47
47fn get_all_refs(text: &str) -> Vec<(FileId, TextRange)> { 48fn get_all_refs(text: &str) -> ReferenceSearchResult {
48 let (analysis, position) = single_file_with_position(text); 49 let (analysis, position) = single_file_with_position(text);
49 analysis.find_all_refs(position).unwrap() 50 analysis.find_all_refs(position).unwrap().unwrap()
50} 51}
51 52
52fn get_symbols_matching(text: &str, query: &str) -> Vec<NavigationTarget> { 53fn get_symbols_matching(text: &str, query: &str) -> Vec<NavigationTarget> {