aboutsummaryrefslogtreecommitdiff
path: root/crates/ide
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide')
-rw-r--r--crates/ide/src/references.rs38
-rw-r--r--crates/ide/src/references/rename.rs51
2 files changed, 31 insertions, 58 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
129fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Option<ReferenceAccess> { 121fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Option<ReferenceAccess> {
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};
6use ide_db::{ 6use 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};
12use stdx::never; 12use 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
124fn 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
147fn find_definition( 125fn 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,