diff options
Diffstat (limited to 'crates/ide_db/src')
-rw-r--r-- | crates/ide_db/src/helpers.rs | 89 | ||||
-rw-r--r-- | crates/ide_db/src/helpers/famous_defs_fixture.rs | 120 | ||||
-rw-r--r-- | crates/ide_db/src/search.rs | 110 |
3 files changed, 194 insertions, 125 deletions
diff --git a/crates/ide_db/src/helpers.rs b/crates/ide_db/src/helpers.rs index e3e5670f1..c6763ae36 100644 --- a/crates/ide_db/src/helpers.rs +++ b/crates/ide_db/src/helpers.rs | |||
@@ -38,94 +38,7 @@ pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Option<Cr | |||
38 | 38 | ||
39 | #[allow(non_snake_case)] | 39 | #[allow(non_snake_case)] |
40 | impl FamousDefs<'_, '_> { | 40 | impl FamousDefs<'_, '_> { |
41 | pub const FIXTURE: &'static str = r#"//- /libcore.rs crate:core | 41 | pub const FIXTURE: &'static str = include_str!("helpers/famous_defs_fixture.rs"); |
42 | pub mod convert { | ||
43 | pub trait From<T> { | ||
44 | fn from(t: T) -> Self; | ||
45 | } | ||
46 | } | ||
47 | |||
48 | pub mod default { | ||
49 | pub trait Default { | ||
50 | fn default() -> Self; | ||
51 | } | ||
52 | } | ||
53 | |||
54 | pub mod iter { | ||
55 | pub use self::traits::{collect::IntoIterator, iterator::Iterator}; | ||
56 | mod traits { | ||
57 | pub(crate) mod iterator { | ||
58 | use crate::option::Option; | ||
59 | pub trait Iterator { | ||
60 | type Item; | ||
61 | fn next(&mut self) -> Option<Self::Item>; | ||
62 | fn by_ref(&mut self) -> &mut Self { | ||
63 | self | ||
64 | } | ||
65 | fn take(self, n: usize) -> crate::iter::Take<Self> { | ||
66 | crate::iter::Take { inner: self } | ||
67 | } | ||
68 | } | ||
69 | |||
70 | impl<I: Iterator> Iterator for &mut I { | ||
71 | type Item = I::Item; | ||
72 | fn next(&mut self) -> Option<I::Item> { | ||
73 | (**self).next() | ||
74 | } | ||
75 | } | ||
76 | } | ||
77 | pub(crate) mod collect { | ||
78 | pub trait IntoIterator { | ||
79 | type Item; | ||
80 | } | ||
81 | } | ||
82 | } | ||
83 | |||
84 | pub use self::sources::*; | ||
85 | pub(crate) mod sources { | ||
86 | use super::Iterator; | ||
87 | use crate::option::Option::{self, *}; | ||
88 | pub struct Repeat<A> { | ||
89 | element: A, | ||
90 | } | ||
91 | |||
92 | pub fn repeat<T>(elt: T) -> Repeat<T> { | ||
93 | Repeat { element: elt } | ||
94 | } | ||
95 | |||
96 | impl<A> Iterator for Repeat<A> { | ||
97 | type Item = A; | ||
98 | |||
99 | fn next(&mut self) -> Option<A> { | ||
100 | None | ||
101 | } | ||
102 | } | ||
103 | } | ||
104 | |||
105 | pub use self::adapters::*; | ||
106 | pub(crate) mod adapters { | ||
107 | use super::Iterator; | ||
108 | use crate::option::Option::{self, *}; | ||
109 | pub struct Take<I> { pub(crate) inner: I } | ||
110 | impl<I> Iterator for Take<I> where I: Iterator { | ||
111 | type Item = <I as Iterator>::Item; | ||
112 | fn next(&mut self) -> Option<<I as Iterator>::Item> { | ||
113 | None | ||
114 | } | ||
115 | } | ||
116 | } | ||
117 | } | ||
118 | |||
119 | pub mod option { | ||
120 | pub enum Option<T> { None, Some(T)} | ||
121 | } | ||
122 | |||
123 | pub mod prelude { | ||
124 | pub use crate::{convert::From, iter::{IntoIterator, Iterator}, option::Option::{self, *}, default::Default}; | ||
125 | } | ||
126 | #[prelude_import] | ||
127 | pub use prelude::*; | ||
128 | "#; | ||
129 | 42 | ||
130 | pub fn core(&self) -> Option<Crate> { | 43 | pub fn core(&self) -> Option<Crate> { |
131 | self.find_crate("core") | 44 | self.find_crate("core") |
diff --git a/crates/ide_db/src/helpers/famous_defs_fixture.rs b/crates/ide_db/src/helpers/famous_defs_fixture.rs new file mode 100644 index 000000000..5e88de64d --- /dev/null +++ b/crates/ide_db/src/helpers/famous_defs_fixture.rs | |||
@@ -0,0 +1,120 @@ | |||
1 | //- /libcore.rs crate:core | ||
2 | //! Signatures of traits, types and functions from the core lib for use in tests. | ||
3 | pub mod convert { | ||
4 | pub trait From<T> { | ||
5 | fn from(t: T) -> Self; | ||
6 | } | ||
7 | } | ||
8 | |||
9 | pub mod default { | ||
10 | pub trait Default { | ||
11 | fn default() -> Self; | ||
12 | } | ||
13 | } | ||
14 | |||
15 | pub mod iter { | ||
16 | pub use self::traits::{collect::IntoIterator, iterator::Iterator}; | ||
17 | mod traits { | ||
18 | pub(crate) mod iterator { | ||
19 | use crate::option::Option; | ||
20 | pub trait Iterator { | ||
21 | type Item; | ||
22 | fn next(&mut self) -> Option<Self::Item>; | ||
23 | fn by_ref(&mut self) -> &mut Self { | ||
24 | self | ||
25 | } | ||
26 | fn take(self, n: usize) -> crate::iter::Take<Self> { | ||
27 | crate::iter::Take { inner: self } | ||
28 | } | ||
29 | } | ||
30 | |||
31 | impl<I: Iterator> Iterator for &mut I { | ||
32 | type Item = I::Item; | ||
33 | fn next(&mut self) -> Option<I::Item> { | ||
34 | (**self).next() | ||
35 | } | ||
36 | } | ||
37 | } | ||
38 | pub(crate) mod collect { | ||
39 | pub trait IntoIterator { | ||
40 | type Item; | ||
41 | } | ||
42 | } | ||
43 | } | ||
44 | |||
45 | pub use self::sources::*; | ||
46 | pub(crate) mod sources { | ||
47 | use super::Iterator; | ||
48 | use crate::option::Option::{self, *}; | ||
49 | pub struct Repeat<A> { | ||
50 | element: A, | ||
51 | } | ||
52 | |||
53 | pub fn repeat<T>(elt: T) -> Repeat<T> { | ||
54 | Repeat { element: elt } | ||
55 | } | ||
56 | |||
57 | impl<A> Iterator for Repeat<A> { | ||
58 | type Item = A; | ||
59 | |||
60 | fn next(&mut self) -> Option<A> { | ||
61 | None | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | |||
66 | pub use self::adapters::*; | ||
67 | pub(crate) mod adapters { | ||
68 | use super::Iterator; | ||
69 | use crate::option::Option::{self, *}; | ||
70 | pub struct Take<I> { | ||
71 | pub(crate) inner: I, | ||
72 | } | ||
73 | impl<I> Iterator for Take<I> | ||
74 | where | ||
75 | I: Iterator, | ||
76 | { | ||
77 | type Item = <I as Iterator>::Item; | ||
78 | fn next(&mut self) -> Option<<I as Iterator>::Item> { | ||
79 | None | ||
80 | } | ||
81 | } | ||
82 | } | ||
83 | } | ||
84 | |||
85 | pub mod ops { | ||
86 | #[lang = "fn"] | ||
87 | pub trait Fn<Args>: FnMut<Args> { | ||
88 | extern "rust-call" fn call(&self, args: Args) -> Self::Output; | ||
89 | } | ||
90 | |||
91 | #[lang = "fn_mut"] | ||
92 | pub trait FnMut<Args>: FnOnce<Args> { | ||
93 | extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output; | ||
94 | } | ||
95 | #[lang = "fn_once"] | ||
96 | pub trait FnOnce<Args> { | ||
97 | #[lang = "fn_once_output"] | ||
98 | type Output; | ||
99 | extern "rust-call" fn call_once(self, args: Args) -> Self::Output; | ||
100 | } | ||
101 | } | ||
102 | |||
103 | pub mod option { | ||
104 | pub enum Option<T> { | ||
105 | None, | ||
106 | Some(T), | ||
107 | } | ||
108 | } | ||
109 | |||
110 | pub mod prelude { | ||
111 | pub use crate::{ | ||
112 | convert::From, | ||
113 | default::Default, | ||
114 | iter::{IntoIterator, Iterator}, | ||
115 | ops::{Fn, FnMut, FnOnce}, | ||
116 | option::Option::{self, *}, | ||
117 | }; | ||
118 | } | ||
119 | #[prelude_import] | ||
120 | pub use prelude::*; | ||
diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index 773bfbc2c..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 | } |
@@ -252,23 +286,23 @@ impl<'a> FindUsages<'a> { | |||
252 | 286 | ||
253 | pub fn at_least_one(self) -> bool { | 287 | pub fn at_least_one(self) -> bool { |
254 | let mut found = false; | 288 | let mut found = false; |
255 | self.search(&mut |_reference| { | 289 | self.search(&mut |_, _| { |
256 | found = true; | 290 | found = true; |
257 | true | 291 | true |
258 | }); | 292 | }); |
259 | found | 293 | found |
260 | } | 294 | } |
261 | 295 | ||
262 | pub fn all(self) -> Vec<Reference> { | 296 | pub fn all(self) -> UsageSearchResult { |
263 | let mut res = Vec::new(); | 297 | let mut res = UsageSearchResult::default(); |
264 | self.search(&mut |reference| { | 298 | self.search(&mut |file_id, reference| { |
265 | res.push(reference); | 299 | res.references.entry(file_id).or_default().push(reference); |
266 | false | 300 | false |
267 | }); | 301 | }); |
268 | res | 302 | res |
269 | } | 303 | } |
270 | 304 | ||
271 | fn search(self, sink: &mut dyn FnMut(Reference) -> bool) { | 305 | fn search(self, sink: &mut dyn FnMut(FileId, FileReference) -> bool) { |
272 | let _p = profile::span("FindUsages:search"); | 306 | let _p = profile::span("FindUsages:search"); |
273 | let sema = self.sema; | 307 | let sema = self.sema; |
274 | 308 | ||
@@ -320,16 +354,14 @@ impl<'a> FindUsages<'a> { | |||
320 | fn found_lifetime( | 354 | fn found_lifetime( |
321 | &self, | 355 | &self, |
322 | lifetime: &ast::Lifetime, | 356 | lifetime: &ast::Lifetime, |
323 | sink: &mut dyn FnMut(Reference) -> bool, | 357 | sink: &mut dyn FnMut(FileId, FileReference) -> bool, |
324 | ) -> bool { | 358 | ) -> bool { |
325 | match NameRefClass::classify_lifetime(self.sema, lifetime) { | 359 | match NameRefClass::classify_lifetime(self.sema, lifetime) { |
326 | Some(NameRefClass::Definition(def)) if &def == self.def => { | 360 | Some(NameRefClass::Definition(def)) if &def == self.def => { |
327 | let reference = Reference { | 361 | let FileRange { file_id, range } = self.sema.original_range(lifetime.syntax()); |
328 | file_range: self.sema.original_range(lifetime.syntax()), | 362 | let reference = |
329 | kind: ReferenceKind::Lifetime, | 363 | FileReference { range, kind: ReferenceKind::Lifetime, access: None }; |
330 | access: None, | 364 | sink(file_id, reference) |
331 | }; | ||
332 | sink(reference) | ||
333 | } | 365 | } |
334 | _ => false, // not a usage | 366 | _ => false, // not a usage |
335 | } | 367 | } |
@@ -338,7 +370,7 @@ impl<'a> FindUsages<'a> { | |||
338 | fn found_name_ref( | 370 | fn found_name_ref( |
339 | &self, | 371 | &self, |
340 | name_ref: &ast::NameRef, | 372 | name_ref: &ast::NameRef, |
341 | sink: &mut dyn FnMut(Reference) -> bool, | 373 | sink: &mut dyn FnMut(FileId, FileReference) -> bool, |
342 | ) -> bool { | 374 | ) -> bool { |
343 | match NameRefClass::classify(self.sema, &name_ref) { | 375 | match NameRefClass::classify(self.sema, &name_ref) { |
344 | Some(NameRefClass::Definition(def)) if &def == self.def => { | 376 | Some(NameRefClass::Definition(def)) if &def == self.def => { |
@@ -352,46 +384,50 @@ impl<'a> FindUsages<'a> { | |||
352 | ReferenceKind::Other | 384 | ReferenceKind::Other |
353 | }; | 385 | }; |
354 | 386 | ||
355 | let reference = Reference { | 387 | let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax()); |
356 | file_range: self.sema.original_range(name_ref.syntax()), | 388 | let reference = |
357 | kind, | 389 | FileReference { range, kind, access: reference_access(&def, &name_ref) }; |
358 | access: reference_access(&def, &name_ref), | 390 | sink(file_id, reference) |
359 | }; | ||
360 | sink(reference) | ||
361 | } | 391 | } |
362 | 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()); | ||
363 | let reference = match self.def { | 394 | let reference = match self.def { |
364 | Definition::Field(_) if &field == self.def => Reference { | 395 | Definition::Field(_) if &field == self.def => FileReference { |
365 | file_range: self.sema.original_range(name_ref.syntax()), | 396 | range, |
366 | kind: ReferenceKind::FieldShorthandForField, | 397 | kind: ReferenceKind::FieldShorthandForField, |
367 | access: reference_access(&field, &name_ref), | 398 | access: reference_access(&field, &name_ref), |
368 | }, | 399 | }, |
369 | Definition::Local(l) if &local == l => Reference { | 400 | Definition::Local(l) if &local == l => FileReference { |
370 | file_range: self.sema.original_range(name_ref.syntax()), | 401 | range, |
371 | kind: ReferenceKind::FieldShorthandForLocal, | 402 | kind: ReferenceKind::FieldShorthandForLocal, |
372 | access: reference_access(&Definition::Local(local), &name_ref), | 403 | access: reference_access(&Definition::Local(local), &name_ref), |
373 | }, | 404 | }, |
374 | _ => return false, // not a usage | 405 | _ => return false, // not a usage |
375 | }; | 406 | }; |
376 | sink(reference) | 407 | sink(file_id, reference) |
377 | } | 408 | } |
378 | _ => false, // not a usage | 409 | _ => false, // not a usage |
379 | } | 410 | } |
380 | } | 411 | } |
381 | 412 | ||
382 | 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 { | ||
383 | match NameClass::classify(self.sema, name) { | 418 | match NameClass::classify(self.sema, name) { |
384 | Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) => { | 419 | Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) => { |
385 | let reference = match self.def { | 420 | if !matches!(self.def, Definition::Field(_) if &field_ref == self.def) { |
386 | Definition::Field(_) if &field_ref == self.def => Reference { | 421 | return false; |
387 | file_range: self.sema.original_range(name.syntax()), | 422 | } |
388 | kind: ReferenceKind::FieldShorthandForField, | 423 | let FileRange { file_id, range } = self.sema.original_range(name.syntax()); |
389 | // FIXME: mutable patterns should have `Write` access | 424 | let reference = FileReference { |
390 | access: Some(ReferenceAccess::Read), | 425 | range, |
391 | }, | 426 | kind: ReferenceKind::FieldShorthandForField, |
392 | _ => return false, // not a usage | 427 | // FIXME: mutable patterns should have `Write` access |
428 | access: Some(ReferenceAccess::Read), | ||
393 | }; | 429 | }; |
394 | sink(reference) | 430 | sink(file_id, reference) |
395 | } | 431 | } |
396 | _ => false, // not a usage | 432 | _ => false, // not a usage |
397 | } | 433 | } |