diff options
Diffstat (limited to 'crates/ide_db/src/search.rs')
-rw-r--r-- | crates/ide_db/src/search.rs | 164 |
1 files changed, 112 insertions, 52 deletions
diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index ff10f71c3..b5fa46642 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs | |||
@@ -18,9 +18,43 @@ use crate::{ | |||
18 | RootDatabase, | 18 | RootDatabase, |
19 | }; | 19 | }; |
20 | 20 | ||
21 | #[derive(Debug, Default, Clone)] | ||
22 | pub struct UsageSearchResult { | ||
23 | pub references: FxHashMap<FileId, Vec<FileReference>>, | ||
24 | } | ||
25 | |||
26 | impl UsageSearchResult { | ||
27 | pub fn is_empty(&self) -> bool { | ||
28 | self.references.is_empty() | ||
29 | } | ||
30 | |||
31 | pub fn len(&self) -> usize { | ||
32 | self.references.len() | ||
33 | } | ||
34 | |||
35 | pub fn iter(&self) -> impl Iterator<Item = (&FileId, &Vec<FileReference>)> + '_ { | ||
36 | self.references.iter() | ||
37 | } | ||
38 | |||
39 | pub fn file_ranges(&self) -> impl Iterator<Item = FileRange> + '_ { | ||
40 | self.references.iter().flat_map(|(&file_id, refs)| { | ||
41 | refs.iter().map(move |&FileReference { range, .. }| FileRange { file_id, range }) | ||
42 | }) | ||
43 | } | ||
44 | } | ||
45 | |||
46 | impl IntoIterator for UsageSearchResult { | ||
47 | type Item = (FileId, Vec<FileReference>); | ||
48 | type IntoIter = <FxHashMap<FileId, Vec<FileReference>> as IntoIterator>::IntoIter; | ||
49 | |||
50 | fn into_iter(self) -> Self::IntoIter { | ||
51 | self.references.into_iter() | ||
52 | } | ||
53 | } | ||
54 | |||
21 | #[derive(Debug, Clone)] | 55 | #[derive(Debug, Clone)] |
22 | pub struct Reference { | 56 | pub struct FileReference { |
23 | pub file_range: FileRange, | 57 | pub range: TextRange, |
24 | pub kind: ReferenceKind, | 58 | pub kind: ReferenceKind, |
25 | pub access: Option<ReferenceAccess>, | 59 | pub access: Option<ReferenceAccess>, |
26 | } | 60 | } |
@@ -121,31 +155,55 @@ impl Definition { | |||
121 | 155 | ||
122 | if let Definition::Local(var) = self { | 156 | if let Definition::Local(var) = self { |
123 | let range = match var.parent(db) { | 157 | let range = match var.parent(db) { |
124 | DefWithBody::Function(f) => f.source(db).value.syntax().text_range(), | 158 | DefWithBody::Function(f) => { |
125 | DefWithBody::Const(c) => c.source(db).value.syntax().text_range(), | 159 | f.source(db).and_then(|src| Some(src.value.syntax().text_range())) |
126 | DefWithBody::Static(s) => s.source(db).value.syntax().text_range(), | 160 | } |
161 | DefWithBody::Const(c) => { | ||
162 | c.source(db).and_then(|src| Some(src.value.syntax().text_range())) | ||
163 | } | ||
164 | DefWithBody::Static(s) => { | ||
165 | s.source(db).and_then(|src| Some(src.value.syntax().text_range())) | ||
166 | } | ||
127 | }; | 167 | }; |
128 | let mut res = FxHashMap::default(); | 168 | let mut res = FxHashMap::default(); |
129 | res.insert(file_id, Some(range)); | 169 | res.insert(file_id, range); |
130 | return SearchScope::new(res); | 170 | return SearchScope::new(res); |
131 | } | 171 | } |
132 | 172 | ||
133 | if let Definition::LifetimeParam(param) = self { | 173 | if let Definition::GenericParam(hir::GenericParam::LifetimeParam(param)) = self { |
134 | let range = match param.parent(db) { | 174 | let range = match param.parent(db) { |
135 | hir::GenericDef::Function(it) => it.source(db).value.syntax().text_range(), | 175 | hir::GenericDef::Function(it) => { |
176 | it.source(db).and_then(|src| Some(src.value.syntax().text_range())) | ||
177 | } | ||
136 | hir::GenericDef::Adt(it) => match it { | 178 | hir::GenericDef::Adt(it) => match it { |
137 | hir::Adt::Struct(it) => it.source(db).value.syntax().text_range(), | 179 | hir::Adt::Struct(it) => { |
138 | hir::Adt::Union(it) => it.source(db).value.syntax().text_range(), | 180 | it.source(db).and_then(|src| Some(src.value.syntax().text_range())) |
139 | hir::Adt::Enum(it) => it.source(db).value.syntax().text_range(), | 181 | } |
182 | hir::Adt::Union(it) => { | ||
183 | it.source(db).and_then(|src| Some(src.value.syntax().text_range())) | ||
184 | } | ||
185 | hir::Adt::Enum(it) => { | ||
186 | it.source(db).and_then(|src| Some(src.value.syntax().text_range())) | ||
187 | } | ||
140 | }, | 188 | }, |
141 | hir::GenericDef::Trait(it) => it.source(db).value.syntax().text_range(), | 189 | hir::GenericDef::Trait(it) => { |
142 | hir::GenericDef::TypeAlias(it) => it.source(db).value.syntax().text_range(), | 190 | it.source(db).and_then(|src| Some(src.value.syntax().text_range())) |
143 | hir::GenericDef::Impl(it) => it.source(db).value.syntax().text_range(), | 191 | } |
144 | hir::GenericDef::Variant(it) => it.source(db).value.syntax().text_range(), | 192 | hir::GenericDef::TypeAlias(it) => { |
145 | hir::GenericDef::Const(it) => it.source(db).value.syntax().text_range(), | 193 | it.source(db).and_then(|src| Some(src.value.syntax().text_range())) |
194 | } | ||
195 | hir::GenericDef::Impl(it) => { | ||
196 | it.source(db).and_then(|src| Some(src.value.syntax().text_range())) | ||
197 | } | ||
198 | hir::GenericDef::Variant(it) => { | ||
199 | it.source(db).and_then(|src| Some(src.value.syntax().text_range())) | ||
200 | } | ||
201 | hir::GenericDef::Const(it) => { | ||
202 | it.source(db).and_then(|src| Some(src.value.syntax().text_range())) | ||
203 | } | ||
146 | }; | 204 | }; |
147 | let mut res = FxHashMap::default(); | 205 | let mut res = FxHashMap::default(); |
148 | res.insert(file_id, Some(range)); | 206 | res.insert(file_id, range); |
149 | return SearchScope::new(res); | 207 | return SearchScope::new(res); |
150 | } | 208 | } |
151 | 209 | ||
@@ -228,23 +286,23 @@ impl<'a> FindUsages<'a> { | |||
228 | 286 | ||
229 | pub fn at_least_one(self) -> bool { | 287 | pub fn at_least_one(self) -> bool { |
230 | let mut found = false; | 288 | let mut found = false; |
231 | self.search(&mut |_reference| { | 289 | self.search(&mut |_, _| { |
232 | found = true; | 290 | found = true; |
233 | true | 291 | true |
234 | }); | 292 | }); |
235 | found | 293 | found |
236 | } | 294 | } |
237 | 295 | ||
238 | pub fn all(self) -> Vec<Reference> { | 296 | pub fn all(self) -> UsageSearchResult { |
239 | let mut res = Vec::new(); | 297 | let mut res = UsageSearchResult::default(); |
240 | self.search(&mut |reference| { | 298 | self.search(&mut |file_id, reference| { |
241 | res.push(reference); | 299 | res.references.entry(file_id).or_default().push(reference); |
242 | false | 300 | false |
243 | }); | 301 | }); |
244 | res | 302 | res |
245 | } | 303 | } |
246 | 304 | ||
247 | fn search(self, sink: &mut dyn FnMut(Reference) -> bool) { | 305 | fn search(self, sink: &mut dyn FnMut(FileId, FileReference) -> bool) { |
248 | let _p = profile::span("FindUsages:search"); | 306 | let _p = profile::span("FindUsages:search"); |
249 | let sema = self.sema; | 307 | let sema = self.sema; |
250 | 308 | ||
@@ -296,16 +354,14 @@ impl<'a> FindUsages<'a> { | |||
296 | fn found_lifetime( | 354 | fn found_lifetime( |
297 | &self, | 355 | &self, |
298 | lifetime: &ast::Lifetime, | 356 | lifetime: &ast::Lifetime, |
299 | sink: &mut dyn FnMut(Reference) -> bool, | 357 | sink: &mut dyn FnMut(FileId, FileReference) -> bool, |
300 | ) -> bool { | 358 | ) -> bool { |
301 | match NameRefClass::classify_lifetime(self.sema, lifetime) { | 359 | match NameRefClass::classify_lifetime(self.sema, lifetime) { |
302 | Some(NameRefClass::Definition(def)) if &def == self.def => { | 360 | Some(NameRefClass::Definition(def)) if &def == self.def => { |
303 | let reference = Reference { | 361 | let FileRange { file_id, range } = self.sema.original_range(lifetime.syntax()); |
304 | file_range: self.sema.original_range(lifetime.syntax()), | 362 | let reference = |
305 | kind: ReferenceKind::Lifetime, | 363 | FileReference { range, kind: ReferenceKind::Lifetime, access: None }; |
306 | access: None, | 364 | sink(file_id, reference) |
307 | }; | ||
308 | sink(reference) | ||
309 | } | 365 | } |
310 | _ => false, // not a usage | 366 | _ => false, // not a usage |
311 | } | 367 | } |
@@ -314,7 +370,7 @@ impl<'a> FindUsages<'a> { | |||
314 | fn found_name_ref( | 370 | fn found_name_ref( |
315 | &self, | 371 | &self, |
316 | name_ref: &ast::NameRef, | 372 | name_ref: &ast::NameRef, |
317 | sink: &mut dyn FnMut(Reference) -> bool, | 373 | sink: &mut dyn FnMut(FileId, FileReference) -> bool, |
318 | ) -> bool { | 374 | ) -> bool { |
319 | match NameRefClass::classify(self.sema, &name_ref) { | 375 | match NameRefClass::classify(self.sema, &name_ref) { |
320 | Some(NameRefClass::Definition(def)) if &def == self.def => { | 376 | Some(NameRefClass::Definition(def)) if &def == self.def => { |
@@ -328,46 +384,50 @@ impl<'a> FindUsages<'a> { | |||
328 | ReferenceKind::Other | 384 | ReferenceKind::Other |
329 | }; | 385 | }; |
330 | 386 | ||
331 | let reference = Reference { | 387 | let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax()); |
332 | file_range: self.sema.original_range(name_ref.syntax()), | 388 | let reference = |
333 | kind, | 389 | FileReference { range, kind, access: reference_access(&def, &name_ref) }; |
334 | access: reference_access(&def, &name_ref), | 390 | sink(file_id, reference) |
335 | }; | ||
336 | sink(reference) | ||
337 | } | 391 | } |
338 | Some(NameRefClass::FieldShorthand { local_ref: local, field_ref: field }) => { | 392 | Some(NameRefClass::FieldShorthand { local_ref: local, field_ref: field }) => { |
393 | let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax()); | ||
339 | let reference = match self.def { | 394 | let reference = match self.def { |
340 | Definition::Field(_) if &field == self.def => Reference { | 395 | Definition::Field(_) if &field == self.def => FileReference { |
341 | file_range: self.sema.original_range(name_ref.syntax()), | 396 | range, |
342 | kind: ReferenceKind::FieldShorthandForField, | 397 | kind: ReferenceKind::FieldShorthandForField, |
343 | access: reference_access(&field, &name_ref), | 398 | access: reference_access(&field, &name_ref), |
344 | }, | 399 | }, |
345 | Definition::Local(l) if &local == l => Reference { | 400 | Definition::Local(l) if &local == l => FileReference { |
346 | file_range: self.sema.original_range(name_ref.syntax()), | 401 | range, |
347 | kind: ReferenceKind::FieldShorthandForLocal, | 402 | kind: ReferenceKind::FieldShorthandForLocal, |
348 | access: reference_access(&Definition::Local(local), &name_ref), | 403 | access: reference_access(&Definition::Local(local), &name_ref), |
349 | }, | 404 | }, |
350 | _ => return false, // not a usage | 405 | _ => return false, // not a usage |
351 | }; | 406 | }; |
352 | sink(reference) | 407 | sink(file_id, reference) |
353 | } | 408 | } |
354 | _ => false, // not a usage | 409 | _ => false, // not a usage |
355 | } | 410 | } |
356 | } | 411 | } |
357 | 412 | ||
358 | fn found_name(&self, name: &ast::Name, sink: &mut dyn FnMut(Reference) -> bool) -> bool { | 413 | fn found_name( |
414 | &self, | ||
415 | name: &ast::Name, | ||
416 | sink: &mut dyn FnMut(FileId, FileReference) -> bool, | ||
417 | ) -> bool { | ||
359 | match NameClass::classify(self.sema, name) { | 418 | match NameClass::classify(self.sema, name) { |
360 | Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) => { | 419 | Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) => { |
361 | let reference = match self.def { | 420 | if !matches!(self.def, Definition::Field(_) if &field_ref == self.def) { |
362 | Definition::Field(_) if &field_ref == self.def => Reference { | 421 | return false; |
363 | file_range: self.sema.original_range(name.syntax()), | 422 | } |
364 | kind: ReferenceKind::FieldShorthandForField, | 423 | let FileRange { file_id, range } = self.sema.original_range(name.syntax()); |
365 | // FIXME: mutable patterns should have `Write` access | 424 | let reference = FileReference { |
366 | access: Some(ReferenceAccess::Read), | 425 | range, |
367 | }, | 426 | kind: ReferenceKind::FieldShorthandForField, |
368 | _ => return false, // not a usage | 427 | // FIXME: mutable patterns should have `Write` access |
428 | access: Some(ReferenceAccess::Read), | ||
369 | }; | 429 | }; |
370 | sink(reference) | 430 | sink(file_id, reference) |
371 | } | 431 | } |
372 | _ => false, // not a usage | 432 | _ => false, // not a usage |
373 | } | 433 | } |