diff options
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r-- | crates/ra_ide_api/src/lib.rs | 6 | ||||
-rw-r--r-- | crates/ra_ide_api/src/navigation_target.rs | 16 | ||||
-rw-r--r-- | crates/ra_ide_api/src/references.rs | 106 |
3 files changed, 94 insertions, 34 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 | }; |
60 | pub use ra_ide_api_light::{ | 61 | pub 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 | ||
25 | impl NavigationTarget { | 25 | impl 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..a6a0ef8ac 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs | |||
@@ -1,42 +1,77 @@ | |||
1 | use relative_path::{RelativePath, RelativePathBuf}; | 1 | use relative_path::{RelativePath, RelativePathBuf}; |
2 | use hir::{ModuleSource, source_binder}; | 2 | use hir::{ModuleSource, source_binder}; |
3 | use ra_db::{FileId, SourceDatabase}; | 3 | use ra_db::{SourceDatabase}; |
4 | use ra_syntax::{ | 4 | use 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 | ||
10 | use crate::{ | 10 | use 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 | ||
18 | pub(crate) fn find_all_refs(db: &RootDatabase, position: FilePosition) -> Vec<(FileId, TextRange)> { | 22 | #[derive(Debug, Clone)] |
23 | pub struct ReferenceSearchResult { | ||
24 | declaration: NavigationTarget, | ||
25 | references: Vec<FileRange>, | ||
26 | } | ||
27 | |||
28 | impl 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 | ||
47 | impl 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 | |||
59 | pub(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 | ||
126 | fn 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 | |||
91 | fn rename_mod( | 141 | fn 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 | } |