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/rename.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/rename.rs')
-rw-r--r-- | crates/ide/src/references/rename.rs | 51 |
1 files changed, 16 insertions, 35 deletions
diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index a4b320227..175ddd759 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs | |||
@@ -6,7 +6,7 @@ use hir::{HasSource, InFile, Module, ModuleDef, ModuleSource, Semantics}; | |||
6 | use ide_db::{ | 6 | use ide_db::{ |
7 | base_db::{AnchoredPathBuf, FileId}, | 7 | base_db::{AnchoredPathBuf, FileId}, |
8 | defs::{Definition, NameClass, NameRefClass}, | 8 | defs::{Definition, NameClass, NameRefClass}, |
9 | search::{FileReference, NameLike}, | 9 | search::FileReference, |
10 | RootDatabase, | 10 | RootDatabase, |
11 | }; | 11 | }; |
12 | use stdx::never; | 12 | use stdx::never; |
@@ -47,12 +47,13 @@ pub(crate) fn prepare_rename( | |||
47 | let sema = Semantics::new(db); | 47 | let sema = Semantics::new(db); |
48 | let source_file = sema.parse(position.file_id); | 48 | let source_file = sema.parse(position.file_id); |
49 | let syntax = source_file.syntax(); | 49 | let syntax = source_file.syntax(); |
50 | let range = match &find_name_like(&sema, &syntax, position) | 50 | let range = match &sema |
51 | .find_node_at_offset_with_descend(&syntax, position.offset) | ||
51 | .ok_or_else(|| format_err!("No references found at position"))? | 52 | .ok_or_else(|| format_err!("No references found at position"))? |
52 | { | 53 | { |
53 | NameLike::Name(it) => it.syntax(), | 54 | ast::NameLike::Name(it) => it.syntax(), |
54 | NameLike::NameRef(it) => it.syntax(), | 55 | ast::NameLike::NameRef(it) => it.syntax(), |
55 | NameLike::Lifetime(it) => it.syntax(), | 56 | ast::NameLike::Lifetime(it) => it.syntax(), |
56 | } | 57 | } |
57 | .text_range(); | 58 | .text_range(); |
58 | Ok(RangeInfo::new(range, ())) | 59 | Ok(RangeInfo::new(range, ())) |
@@ -121,50 +122,28 @@ fn check_identifier(new_name: &str) -> RenameResult<IdentifierKind> { | |||
121 | } | 122 | } |
122 | } | 123 | } |
123 | 124 | ||
124 | fn find_name_like( | ||
125 | sema: &Semantics<RootDatabase>, | ||
126 | syntax: &SyntaxNode, | ||
127 | position: FilePosition, | ||
128 | ) -> Option<NameLike> { | ||
129 | let namelike = if let Some(name_ref) = | ||
130 | sema.find_node_at_offset_with_descend::<ast::NameRef>(syntax, position.offset) | ||
131 | { | ||
132 | NameLike::NameRef(name_ref) | ||
133 | } else if let Some(name) = | ||
134 | sema.find_node_at_offset_with_descend::<ast::Name>(syntax, position.offset) | ||
135 | { | ||
136 | NameLike::Name(name) | ||
137 | } else if let Some(lifetime) = | ||
138 | sema.find_node_at_offset_with_descend::<ast::Lifetime>(syntax, position.offset) | ||
139 | { | ||
140 | NameLike::Lifetime(lifetime) | ||
141 | } else { | ||
142 | return None; | ||
143 | }; | ||
144 | Some(namelike) | ||
145 | } | ||
146 | |||
147 | fn find_definition( | 125 | fn find_definition( |
148 | sema: &Semantics<RootDatabase>, | 126 | sema: &Semantics<RootDatabase>, |
149 | syntax: &SyntaxNode, | 127 | syntax: &SyntaxNode, |
150 | position: FilePosition, | 128 | position: FilePosition, |
151 | ) -> RenameResult<Definition> { | 129 | ) -> RenameResult<Definition> { |
152 | match find_name_like(sema, syntax, position) | 130 | match sema |
131 | .find_node_at_offset_with_descend(syntax, position.offset) | ||
153 | .ok_or_else(|| format_err!("No references found at position"))? | 132 | .ok_or_else(|| format_err!("No references found at position"))? |
154 | { | 133 | { |
155 | // renaming aliases would rename the item being aliased as the HIR doesn't track aliases yet | 134 | // renaming aliases would rename the item being aliased as the HIR doesn't track aliases yet |
156 | NameLike::Name(name) | 135 | ast::NameLike::Name(name) |
157 | if name.syntax().parent().map_or(false, |it| ast::Rename::can_cast(it.kind())) => | 136 | if name.syntax().parent().map_or(false, |it| ast::Rename::can_cast(it.kind())) => |
158 | { | 137 | { |
159 | bail!("Renaming aliases is currently unsupported") | 138 | bail!("Renaming aliases is currently unsupported") |
160 | } | 139 | } |
161 | NameLike::Name(name) => { | 140 | ast::NameLike::Name(name) => { |
162 | NameClass::classify(sema, &name).map(|class| class.referenced_or_defined(sema.db)) | 141 | NameClass::classify(sema, &name).map(|class| class.referenced_or_defined(sema.db)) |
163 | } | 142 | } |
164 | NameLike::NameRef(name_ref) => { | 143 | ast::NameLike::NameRef(name_ref) => { |
165 | NameRefClass::classify(sema, &name_ref).map(|class| class.referenced(sema.db)) | 144 | NameRefClass::classify(sema, &name_ref).map(|class| class.referenced(sema.db)) |
166 | } | 145 | } |
167 | NameLike::Lifetime(lifetime) => NameRefClass::classify_lifetime(sema, &lifetime) | 146 | ast::NameLike::Lifetime(lifetime) => NameRefClass::classify_lifetime(sema, &lifetime) |
168 | .map(|class| NameRefClass::referenced(class, sema.db)) | 147 | .map(|class| NameRefClass::referenced(class, sema.db)) |
169 | .or_else(|| { | 148 | .or_else(|| { |
170 | NameClass::classify_lifetime(sema, &lifetime) | 149 | NameClass::classify_lifetime(sema, &lifetime) |
@@ -187,10 +166,12 @@ fn source_edit_from_references( | |||
187 | // if the ranges differ then the node is inside a macro call, we can't really attempt | 166 | // if the ranges differ then the node is inside a macro call, we can't really attempt |
188 | // to make special rewrites like shorthand syntax and such, so just rename the node in | 167 | // to make special rewrites like shorthand syntax and such, so just rename the node in |
189 | // the macro input | 168 | // the macro input |
190 | NameLike::NameRef(name_ref) if name_ref.syntax().text_range() == reference.range => { | 169 | ast::NameLike::NameRef(name_ref) |
170 | if name_ref.syntax().text_range() == reference.range => | ||
171 | { | ||
191 | source_edit_from_name_ref(name_ref, new_name, def) | 172 | source_edit_from_name_ref(name_ref, new_name, def) |
192 | } | 173 | } |
193 | NameLike::Name(name) if name.syntax().text_range() == reference.range => { | 174 | ast::NameLike::Name(name) if name.syntax().text_range() == reference.range => { |
194 | source_edit_from_name(name, new_name) | 175 | source_edit_from_name(name, new_name) |
195 | } | 176 | } |
196 | _ => None, | 177 | _ => None, |