diff options
Diffstat (limited to 'crates/ide/src/references')
-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, |