diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-02-17 13:45:27 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-02-17 13:45:27 +0000 |
commit | 4054525c418085db4ceb2df70475a1ac9c019aff (patch) | |
tree | 64adbc8c62df3a6ebbe8c1a799589a94ddfbf490 /crates/ide_db/src/search.rs | |
parent | 056601b41fbc5208cae5d996ec7fd18526d79e41 (diff) | |
parent | e1dbf43cf85f84c3a7e40f9731fc1f7ac96f8979 (diff) |
Merge #7699
7699: Implement ast::AstNode for NameLike and move it to node_ext r=matklad a=Veykril
With this `search`(and 2 other modules) don't necessarily go through 3 calls of `find_node_at_offset_with_descend` to find the correct node. Also makes the code that searches for NameLikes a bit easier on the eyes imo, though that can be fixed with just a helper function as well so its not that relevant.
Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/ide_db/src/search.rs')
-rw-r--r-- | crates/ide_db/src/search.rs | 66 |
1 files changed, 24 insertions, 42 deletions
diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index 22dd172f7..ba8bea002 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs | |||
@@ -53,33 +53,9 @@ impl IntoIterator for UsageSearchResult { | |||
53 | } | 53 | } |
54 | 54 | ||
55 | #[derive(Debug, Clone)] | 55 | #[derive(Debug, Clone)] |
56 | pub enum NameLike { | ||
57 | NameRef(ast::NameRef), | ||
58 | Name(ast::Name), | ||
59 | Lifetime(ast::Lifetime), | ||
60 | } | ||
61 | |||
62 | impl NameLike { | ||
63 | pub fn as_name_ref(&self) -> Option<&ast::NameRef> { | ||
64 | match self { | ||
65 | NameLike::NameRef(name_ref) => Some(name_ref), | ||
66 | _ => None, | ||
67 | } | ||
68 | } | ||
69 | } | ||
70 | |||
71 | mod __ { | ||
72 | use super::{ | ||
73 | ast::{Lifetime, Name, NameRef}, | ||
74 | NameLike, | ||
75 | }; | ||
76 | stdx::impl_from!(NameRef, Name, Lifetime for NameLike); | ||
77 | } | ||
78 | |||
79 | #[derive(Debug, Clone)] | ||
80 | pub struct FileReference { | 56 | pub struct FileReference { |
81 | pub range: TextRange, | 57 | pub range: TextRange, |
82 | pub name: NameLike, | 58 | pub name: ast::NameLike, |
83 | pub access: Option<ReferenceAccess>, | 59 | pub access: Option<ReferenceAccess>, |
84 | } | 60 | } |
85 | 61 | ||
@@ -300,6 +276,7 @@ impl<'a> FindUsages<'a> { | |||
300 | pub fn in_scope(self, scope: SearchScope) -> FindUsages<'a> { | 276 | pub fn in_scope(self, scope: SearchScope) -> FindUsages<'a> { |
301 | self.set_scope(Some(scope)) | 277 | self.set_scope(Some(scope)) |
302 | } | 278 | } |
279 | |||
303 | pub fn set_scope(mut self, scope: Option<SearchScope>) -> FindUsages<'a> { | 280 | pub fn set_scope(mut self, scope: Option<SearchScope>) -> FindUsages<'a> { |
304 | assert!(self.scope.is_none()); | 281 | assert!(self.scope.is_none()); |
305 | self.scope = scope; | 282 | self.scope = scope; |
@@ -355,18 +332,23 @@ impl<'a> FindUsages<'a> { | |||
355 | continue; | 332 | continue; |
356 | } | 333 | } |
357 | 334 | ||
358 | if let Some(name_ref) = sema.find_node_at_offset_with_descend(&tree, offset) { | 335 | if let Some(name) = sema.find_node_at_offset_with_descend(&tree, offset) { |
359 | if self.found_name_ref(&name_ref, sink) { | 336 | match name { |
360 | return; | 337 | ast::NameLike::NameRef(name_ref) => { |
361 | } | 338 | if self.found_name_ref(&name_ref, sink) { |
362 | } else if let Some(name) = sema.find_node_at_offset_with_descend(&tree, offset) { | 339 | return; |
363 | if self.found_name(&name, sink) { | 340 | } |
364 | return; | 341 | } |
365 | } | 342 | ast::NameLike::Name(name) => { |
366 | } else if let Some(lifetime) = sema.find_node_at_offset_with_descend(&tree, offset) | 343 | if self.found_name(&name, sink) { |
367 | { | 344 | return; |
368 | if self.found_lifetime(&lifetime, sink) { | 345 | } |
369 | return; | 346 | } |
347 | ast::NameLike::Lifetime(lifetime) => { | ||
348 | if self.found_lifetime(&lifetime, sink) { | ||
349 | return; | ||
350 | } | ||
351 | } | ||
370 | } | 352 | } |
371 | } | 353 | } |
372 | } | 354 | } |
@@ -383,7 +365,7 @@ impl<'a> FindUsages<'a> { | |||
383 | let FileRange { file_id, range } = self.sema.original_range(lifetime.syntax()); | 365 | let FileRange { file_id, range } = self.sema.original_range(lifetime.syntax()); |
384 | let reference = FileReference { | 366 | let reference = FileReference { |
385 | range, | 367 | range, |
386 | name: NameLike::Lifetime(lifetime.clone()), | 368 | name: ast::NameLike::Lifetime(lifetime.clone()), |
387 | access: None, | 369 | access: None, |
388 | }; | 370 | }; |
389 | sink(file_id, reference) | 371 | sink(file_id, reference) |
@@ -402,7 +384,7 @@ impl<'a> FindUsages<'a> { | |||
402 | let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax()); | 384 | let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax()); |
403 | let reference = FileReference { | 385 | let reference = FileReference { |
404 | range, | 386 | range, |
405 | name: NameLike::NameRef(name_ref.clone()), | 387 | name: ast::NameLike::NameRef(name_ref.clone()), |
406 | access: reference_access(&def, &name_ref), | 388 | access: reference_access(&def, &name_ref), |
407 | }; | 389 | }; |
408 | sink(file_id, reference) | 390 | sink(file_id, reference) |
@@ -412,12 +394,12 @@ impl<'a> FindUsages<'a> { | |||
412 | let reference = match self.def { | 394 | let reference = match self.def { |
413 | Definition::Field(_) if &field == self.def => FileReference { | 395 | Definition::Field(_) if &field == self.def => FileReference { |
414 | range, | 396 | range, |
415 | name: NameLike::NameRef(name_ref.clone()), | 397 | name: ast::NameLike::NameRef(name_ref.clone()), |
416 | access: reference_access(&field, &name_ref), | 398 | access: reference_access(&field, &name_ref), |
417 | }, | 399 | }, |
418 | Definition::Local(l) if &local == l => FileReference { | 400 | Definition::Local(l) if &local == l => FileReference { |
419 | range, | 401 | range, |
420 | name: NameLike::NameRef(name_ref.clone()), | 402 | name: ast::NameLike::NameRef(name_ref.clone()), |
421 | access: reference_access(&Definition::Local(local), &name_ref), | 403 | access: reference_access(&Definition::Local(local), &name_ref), |
422 | }, | 404 | }, |
423 | _ => return false, // not a usage | 405 | _ => return false, // not a usage |
@@ -441,7 +423,7 @@ impl<'a> FindUsages<'a> { | |||
441 | let FileRange { file_id, range } = self.sema.original_range(name.syntax()); | 423 | let FileRange { file_id, range } = self.sema.original_range(name.syntax()); |
442 | let reference = FileReference { | 424 | let reference = FileReference { |
443 | range, | 425 | range, |
444 | name: NameLike::Name(name.clone()), | 426 | name: ast::NameLike::Name(name.clone()), |
445 | // FIXME: mutable patterns should have `Write` access | 427 | // FIXME: mutable patterns should have `Write` access |
446 | access: Some(ReferenceAccess::Read), | 428 | access: Some(ReferenceAccess::Read), |
447 | }; | 429 | }; |