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/src/references.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/src/references.rs')
-rw-r--r-- | crates/ide/src/references.rs | 38 |
1 files changed, 15 insertions, 23 deletions
diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index a83b82f1b..55f95ebae 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs | |||
@@ -101,29 +101,21 @@ fn find_def( | |||
101 | syntax: &SyntaxNode, | 101 | syntax: &SyntaxNode, |
102 | position: FilePosition, | 102 | position: FilePosition, |
103 | ) -> Option<Definition> { | 103 | ) -> Option<Definition> { |
104 | if let Some(name) = sema.find_node_at_offset_with_descend::<ast::Name>(&syntax, position.offset) | 104 | let def = match sema.find_node_at_offset_with_descend(syntax, position.offset)? { |
105 | { | 105 | ast::NameLike::NameRef(name_ref) => { |
106 | let class = NameClass::classify(sema, &name)?; | 106 | NameRefClass::classify(sema, &name_ref)?.referenced(sema.db) |
107 | Some(class.referenced_or_defined(sema.db)) | 107 | } |
108 | } else if let Some(lifetime) = | 108 | ast::NameLike::Name(name) => { |
109 | sema.find_node_at_offset_with_descend::<ast::Lifetime>(&syntax, position.offset) | 109 | NameClass::classify(sema, &name)?.referenced_or_defined(sema.db) |
110 | { | 110 | } |
111 | let def = if let Some(def) = | 111 | ast::NameLike::Lifetime(lifetime) => NameRefClass::classify_lifetime(sema, &lifetime) |
112 | NameRefClass::classify_lifetime(sema, &lifetime).map(|class| class.referenced(sema.db)) | 112 | .map(|class| class.referenced(sema.db)) |
113 | { | 113 | .or_else(|| { |
114 | def | 114 | NameClass::classify_lifetime(sema, &lifetime) |
115 | } else { | 115 | .map(|class| class.referenced_or_defined(sema.db)) |
116 | NameClass::classify_lifetime(sema, &lifetime)?.referenced_or_defined(sema.db) | 116 | })?, |
117 | }; | 117 | }; |
118 | Some(def) | 118 | Some(def) |
119 | } else if let Some(name_ref) = | ||
120 | sema.find_node_at_offset_with_descend::<ast::NameRef>(&syntax, position.offset) | ||
121 | { | ||
122 | let class = NameRefClass::classify(sema, &name_ref)?; | ||
123 | Some(class.referenced(sema.db)) | ||
124 | } else { | ||
125 | None | ||
126 | } | ||
127 | } | 119 | } |
128 | 120 | ||
129 | fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Option<ReferenceAccess> { | 121 | fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Option<ReferenceAccess> { |