aboutsummaryrefslogtreecommitdiff
path: root/crates/ide
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide')
-rw-r--r--crates/ide/src/call_hierarchy.rs29
-rw-r--r--crates/ide/src/lib.rs2
-rw-r--r--crates/ide/src/references.rs79
-rw-r--r--crates/ide/src/references/rename.rs100
4 files changed, 110 insertions, 100 deletions
diff --git a/crates/ide/src/call_hierarchy.rs b/crates/ide/src/call_hierarchy.rs
index b29d1fef9..e8999a7f3 100644
--- a/crates/ide/src/call_hierarchy.rs
+++ b/crates/ide/src/call_hierarchy.rs
@@ -47,22 +47,23 @@ pub(crate) fn incoming_calls(db: &RootDatabase, position: FilePosition) -> Optio
47 47
48 let mut calls = CallLocations::default(); 48 let mut calls = CallLocations::default();
49 49
50 for reference in refs.info.references() { 50 for (&file_id, references) in refs.info.references().iter() {
51 let file_id = reference.file_range.file_id;
52 let file = sema.parse(file_id); 51 let file = sema.parse(file_id);
53 let file = file.syntax(); 52 let file = file.syntax();
54 let token = file.token_at_offset(reference.file_range.range.start()).next()?; 53 for reference in references {
55 let token = sema.descend_into_macros(token); 54 let token = file.token_at_offset(reference.range.start()).next()?;
56 let syntax = token.parent(); 55 let token = sema.descend_into_macros(token);
57 56 let syntax = token.parent();
58 // This target is the containing function 57
59 if let Some(nav) = syntax.ancestors().find_map(|node| { 58 // This target is the containing function
60 let fn_ = ast::Fn::cast(node)?; 59 if let Some(nav) = syntax.ancestors().find_map(|node| {
61 let def = sema.to_def(&fn_)?; 60 let fn_ = ast::Fn::cast(node)?;
62 def.try_to_nav(sema.db) 61 let def = sema.to_def(&fn_)?;
63 }) { 62 def.try_to_nav(sema.db)
64 let relative_range = reference.file_range.range; 63 }) {
65 calls.add(&nav, relative_range); 64 let relative_range = reference.range;
65 calls.add(&nav, relative_range);
66 }
66 } 67 }
67 } 68 }
68 69
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs
index 1f368cbd0..1e03832ec 100644
--- a/crates/ide/src/lib.rs
+++ b/crates/ide/src/lib.rs
@@ -92,7 +92,7 @@ pub use ide_db::base_db::{
92}; 92};
93pub use ide_db::{ 93pub use ide_db::{
94 call_info::CallInfo, 94 call_info::CallInfo,
95 search::{Reference, ReferenceAccess, ReferenceKind}, 95 search::{FileReference, ReferenceAccess, ReferenceKind},
96}; 96};
97pub use ide_db::{ 97pub use ide_db::{
98 label::Label, 98 label::Label,
diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs
index b774a2be1..7d4757e02 100644
--- a/crates/ide/src/references.rs
+++ b/crates/ide/src/references.rs
@@ -13,9 +13,9 @@ pub(crate) mod rename;
13 13
14use hir::Semantics; 14use hir::Semantics;
15use ide_db::{ 15use ide_db::{
16 base_db::FileId,
16 defs::{Definition, NameClass, NameRefClass}, 17 defs::{Definition, NameClass, NameRefClass},
17 search::Reference, 18 search::{FileReference, ReferenceAccess, ReferenceKind, SearchScope, UsageSearchResult},
18 search::{ReferenceAccess, ReferenceKind, SearchScope},
19 RootDatabase, 19 RootDatabase,
20}; 20};
21use syntax::{ 21use syntax::{
@@ -29,7 +29,7 @@ use crate::{display::TryToNav, FilePosition, FileRange, NavigationTarget, RangeI
29#[derive(Debug, Clone)] 29#[derive(Debug, Clone)]
30pub struct ReferenceSearchResult { 30pub struct ReferenceSearchResult {
31 declaration: Declaration, 31 declaration: Declaration,
32 references: Vec<Reference>, 32 references: UsageSearchResult,
33} 33}
34 34
35#[derive(Debug, Clone)] 35#[derive(Debug, Clone)]
@@ -48,10 +48,21 @@ impl ReferenceSearchResult {
48 &self.declaration.nav 48 &self.declaration.nav
49 } 49 }
50 50
51 pub fn references(&self) -> &[Reference] { 51 pub fn references(&self) -> &UsageSearchResult {
52 &self.references 52 &self.references
53 } 53 }
54 54
55 pub fn references_with_declaration(mut self) -> UsageSearchResult {
56 let decl_ref = FileReference {
57 range: self.declaration.nav.focus_or_full_range(),
58 kind: self.declaration.kind,
59 access: self.declaration.access,
60 };
61 let file_id = self.declaration.nav.file_id;
62 self.references.references.entry(file_id).or_default().push(decl_ref);
63 self.references
64 }
65
55 /// Total number of references 66 /// Total number of references
56 /// At least 1 since all valid references should 67 /// At least 1 since all valid references should
57 /// Have a declaration 68 /// Have a declaration
@@ -63,21 +74,11 @@ impl ReferenceSearchResult {
63// allow turning ReferenceSearchResult into an iterator 74// allow turning ReferenceSearchResult into an iterator
64// over References 75// over References
65impl IntoIterator for ReferenceSearchResult { 76impl IntoIterator for ReferenceSearchResult {
66 type Item = Reference; 77 type Item = (FileId, Vec<FileReference>);
67 type IntoIter = std::vec::IntoIter<Reference>; 78 type IntoIter = std::collections::hash_map::IntoIter<FileId, Vec<FileReference>>;
68 79
69 fn into_iter(mut self) -> Self::IntoIter { 80 fn into_iter(self) -> Self::IntoIter {
70 let mut v = Vec::with_capacity(self.len()); 81 self.references_with_declaration().into_iter()
71 v.push(Reference {
72 file_range: FileRange {
73 file_id: self.declaration.nav.file_id,
74 range: self.declaration.nav.focus_or_full_range(),
75 },
76 kind: self.declaration.kind,
77 access: self.declaration.access,
78 });
79 v.append(&mut self.references);
80 v.into_iter()
81 } 82 }
82} 83}
83 84
@@ -109,13 +110,12 @@ pub(crate) fn find_all_refs(
109 110
110 let RangeInfo { range, info: def } = find_name(&sema, &syntax, position, opt_name)?; 111 let RangeInfo { range, info: def } = find_name(&sema, &syntax, position, opt_name)?;
111 112
112 let references = def 113 let mut usages = def.usages(sema).set_scope(search_scope).all();
113 .usages(sema) 114 usages
114 .set_scope(search_scope) 115 .references
115 .all() 116 .values_mut()
116 .into_iter() 117 .for_each(|it| it.retain(|r| search_kind == ReferenceKind::Other || search_kind == r.kind));
117 .filter(|r| search_kind == ReferenceKind::Other || search_kind == r.kind) 118 usages.references.retain(|_, it| !it.is_empty());
118 .collect();
119 119
120 let nav = def.try_to_nav(sema.db)?; 120 let nav = def.try_to_nav(sema.db)?;
121 let decl_range = nav.focus_or_full_range(); 121 let decl_range = nav.focus_or_full_range();
@@ -139,7 +139,7 @@ pub(crate) fn find_all_refs(
139 139
140 let declaration = Declaration { nav, kind, access: decl_access(&def, &syntax, decl_range) }; 140 let declaration = Declaration { nav, kind, access: decl_access(&def, &syntax, decl_range) };
141 141
142 Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references })) 142 Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references: usages }))
143} 143}
144 144
145fn find_name( 145fn find_name(
@@ -255,7 +255,8 @@ fn try_find_self_references(
255 syntax: &SyntaxNode, 255 syntax: &SyntaxNode,
256 position: FilePosition, 256 position: FilePosition,
257) -> Option<RangeInfo<ReferenceSearchResult>> { 257) -> Option<RangeInfo<ReferenceSearchResult>> {
258 let self_token = syntax.token_at_offset(position.offset).find(|t| t.kind() == T![self])?; 258 let FilePosition { file_id, offset } = position;
259 let self_token = syntax.token_at_offset(offset).find(|t| t.kind() == T![self])?;
259 let parent = self_token.parent(); 260 let parent = self_token.parent();
260 match_ast! { 261 match_ast! {
261 match parent { 262 match parent {
@@ -276,7 +277,7 @@ fn try_find_self_references(
276 277
277 let declaration = Declaration { 278 let declaration = Declaration {
278 nav: NavigationTarget { 279 nav: NavigationTarget {
279 file_id: position.file_id, 280 file_id,
280 full_range: self_param.syntax().text_range(), 281 full_range: self_param.syntax().text_range(),
281 focus_range: Some(param_self_token.text_range()), 282 focus_range: Some(param_self_token.text_range()),
282 name: param_self_token.text().clone(), 283 name: param_self_token.text().clone(),
@@ -292,7 +293,7 @@ fn try_find_self_references(
292 ReferenceAccess::Read 293 ReferenceAccess::Read
293 }), 294 }),
294 }; 295 };
295 let references = function 296 let refs = function
296 .body() 297 .body()
297 .map(|body| { 298 .map(|body| {
298 body.syntax() 299 body.syntax()
@@ -306,14 +307,16 @@ fn try_find_self_references(
306 None 307 None
307 } 308 }
308 }) 309 })
309 .map(|token| Reference { 310 .map(|token| FileReference {
310 file_range: FileRange { file_id: position.file_id, range: token.text_range() }, 311 range: token.text_range(),
311 kind: ReferenceKind::SelfKw, 312 kind: ReferenceKind::SelfKw,
312 access: declaration.access, // FIXME: properly check access kind here instead of copying it from the declaration 313 access: declaration.access, // FIXME: properly check access kind here instead of copying it from the declaration
313 }) 314 })
314 .collect() 315 .collect()
315 }) 316 })
316 .unwrap_or_default(); 317 .unwrap_or_default();
318 let mut references = UsageSearchResult::default();
319 references.references.insert(file_id, refs);
317 320
318 Some(RangeInfo::new( 321 Some(RangeInfo::new(
319 param_self_token.text_range(), 322 param_self_token.text_range(),
@@ -1018,12 +1021,14 @@ impl Foo {
1018 actual += "\n\n"; 1021 actual += "\n\n";
1019 } 1022 }
1020 1023
1021 for r in &refs.references { 1024 for (file_id, references) in refs.references {
1022 format_to!(actual, "{:?} {:?} {:?}", r.file_range.file_id, r.file_range.range, r.kind); 1025 for r in references {
1023 if let Some(access) = r.access { 1026 format_to!(actual, "{:?} {:?} {:?}", file_id, r.range, r.kind);
1024 format_to!(actual, " {:?}", access); 1027 if let Some(access) = r.access {
1028 format_to!(actual, " {:?}", access);
1029 }
1030 actual += "\n";
1025 } 1031 }
1026 actual += "\n";
1027 } 1032 }
1028 expect.assert_eq(&actual) 1033 expect.assert_eq(&actual)
1029 } 1034 }
diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs
index 3edc43e08..5207388b5 100644
--- a/crates/ide/src/references/rename.rs
+++ b/crates/ide/src/references/rename.rs
@@ -6,9 +6,10 @@ use std::{
6}; 6};
7 7
8use hir::{Module, ModuleDef, ModuleSource, Semantics}; 8use hir::{Module, ModuleDef, ModuleSource, Semantics};
9use ide_db::base_db::{AnchoredPathBuf, FileId, FileRange, SourceDatabaseExt};
10use ide_db::{ 9use ide_db::{
10 base_db::{AnchoredPathBuf, FileId, FileRange, SourceDatabaseExt},
11 defs::{Definition, NameClass, NameRefClass}, 11 defs::{Definition, NameClass, NameRefClass},
12 search::FileReference,
12 RootDatabase, 13 RootDatabase,
13}; 14};
14use syntax::{ 15use syntax::{
@@ -20,8 +21,8 @@ use test_utils::mark;
20use text_edit::TextEdit; 21use text_edit::TextEdit;
21 22
22use crate::{ 23use crate::{
23 FilePosition, FileSystemEdit, RangeInfo, Reference, ReferenceKind, ReferenceSearchResult, 24 FilePosition, FileSystemEdit, RangeInfo, ReferenceKind, ReferenceSearchResult, SourceChange,
24 SourceChange, SourceFileEdit, TextRange, TextSize, 25 SourceFileEdit, TextRange, TextSize,
25}; 26};
26 27
27type RenameResult<T> = Result<T, RenameError>; 28type RenameResult<T> = Result<T, RenameError>;
@@ -173,39 +174,46 @@ fn find_all_refs(
173 .ok_or_else(|| format_err!("No references found at position")) 174 .ok_or_else(|| format_err!("No references found at position"))
174} 175}
175 176
176fn source_edit_from_reference( 177fn source_edit_from_references(
177 sema: &Semantics<RootDatabase>, 178 sema: &Semantics<RootDatabase>,
178 reference: Reference, 179 file_id: FileId,
180 references: &[FileReference],
179 new_name: &str, 181 new_name: &str,
180) -> SourceFileEdit { 182) -> SourceFileEdit {
181 let mut replacement_text = String::new(); 183 let mut edit = TextEdit::builder();
182 let range = match reference.kind { 184 for reference in references {
183 ReferenceKind::FieldShorthandForField => { 185 let mut replacement_text = String::new();
184 mark::hit!(test_rename_struct_field_for_shorthand); 186 let range = match reference.kind {
185 replacement_text.push_str(new_name); 187 ReferenceKind::FieldShorthandForField => {
186 replacement_text.push_str(": "); 188 mark::hit!(test_rename_struct_field_for_shorthand);
187 TextRange::new(reference.file_range.range.start(), reference.file_range.range.start()) 189 replacement_text.push_str(new_name);
188 } 190 replacement_text.push_str(": ");
189 ReferenceKind::FieldShorthandForLocal => { 191 TextRange::new(reference.range.start(), reference.range.start())
190 mark::hit!(test_rename_local_for_field_shorthand); 192 }
191 replacement_text.push_str(": "); 193 ReferenceKind::FieldShorthandForLocal => {
192 replacement_text.push_str(new_name); 194 mark::hit!(test_rename_local_for_field_shorthand);
193 TextRange::new(reference.file_range.range.end(), reference.file_range.range.end()) 195 replacement_text.push_str(": ");
194 } 196 replacement_text.push_str(new_name);
195 ReferenceKind::RecordFieldExprOrPat => { 197 TextRange::new(reference.range.end(), reference.range.end())
196 mark::hit!(test_rename_field_expr_pat); 198 }
197 replacement_text.push_str(new_name); 199 ReferenceKind::RecordFieldExprOrPat => {
198 edit_text_range_for_record_field_expr_or_pat(sema, reference.file_range, new_name) 200 mark::hit!(test_rename_field_expr_pat);
199 } 201 replacement_text.push_str(new_name);
200 _ => { 202 edit_text_range_for_record_field_expr_or_pat(
201 replacement_text.push_str(new_name); 203 sema,
202 reference.file_range.range 204 FileRange { file_id, range: reference.range },
203 } 205 new_name,
204 }; 206 )
205 SourceFileEdit { 207 }
206 file_id: reference.file_range.file_id, 208 _ => {
207 edit: TextEdit::replace(range, replacement_text), 209 replacement_text.push_str(new_name);
210 reference.range
211 }
212 };
213 edit.replace(range, replacement_text);
208 } 214 }
215
216 SourceFileEdit { file_id, edit: edit.finish() }
209} 217}
210 218
211fn edit_text_range_for_record_field_expr_or_pat( 219fn edit_text_range_for_record_field_expr_or_pat(
@@ -276,10 +284,9 @@ fn rename_mod(
276 } 284 }
277 285
278 let RangeInfo { range, info: refs } = find_all_refs(sema, position)?; 286 let RangeInfo { range, info: refs } = find_all_refs(sema, position)?;
279 let ref_edits = refs 287 let ref_edits = refs.references().iter().map(|(&file_id, references)| {
280 .references 288 source_edit_from_references(sema, file_id, references, new_name)
281 .into_iter() 289 });
282 .map(|reference| source_edit_from_reference(sema, reference, new_name));
283 source_file_edits.extend(ref_edits); 290 source_file_edits.extend(ref_edits);
284 291
285 Ok(RangeInfo::new(range, SourceChange::from_edits(source_file_edits, file_system_edits))) 292 Ok(RangeInfo::new(range, SourceChange::from_edits(source_file_edits, file_system_edits)))
@@ -331,17 +338,12 @@ fn rename_to_self(
331 338
332 let RangeInfo { range, info: refs } = find_all_refs(sema, position)?; 339 let RangeInfo { range, info: refs } = find_all_refs(sema, position)?;
333 340
334 let (param_ref, usages): (Vec<Reference>, Vec<Reference>) = refs 341 let mut edits = refs
335 .into_iter() 342 .references()
336 .partition(|reference| param_range.intersect(reference.file_range.range).is_some()); 343 .iter()
337 344 .map(|(&file_id, references)| {
338 if param_ref.is_empty() { 345 source_edit_from_references(sema, file_id, references, "self")
339 bail!("Parameter to rename not found"); 346 })
340 }
341
342 let mut edits = usages
343 .into_iter()
344 .map(|reference| source_edit_from_reference(sema, reference, "self"))
345 .collect::<Vec<_>>(); 347 .collect::<Vec<_>>();
346 348
347 edits.push(SourceFileEdit { 349 edits.push(SourceFileEdit {
@@ -467,7 +469,9 @@ fn rename_reference(
467 469
468 let edit = refs 470 let edit = refs
469 .into_iter() 471 .into_iter()
470 .map(|reference| source_edit_from_reference(sema, reference, new_name)) 472 .map(|(file_id, references)| {
473 source_edit_from_references(sema, file_id, &references, new_name)
474 })
471 .collect::<Vec<_>>(); 475 .collect::<Vec<_>>();
472 476
473 Ok(RangeInfo::new(range, SourceChange::from(edit))) 477 Ok(RangeInfo::new(range, SourceChange::from(edit)))