aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-03-23 13:06:40 +0000
committerGitHub <[email protected]>2020-03-23 13:06:40 +0000
commita2f7ca27c082bef625bc33e1eca622457102bae4 (patch)
treebff240491d11d1bd3ceb011ef9a8606d7a41aa63 /crates/ra_ide
parent851a03492e5fa0a4fc6c7c94a6904078297b1ab9 (diff)
parentaf8c37cb57fcde849ef333100b8057c427bfa9a9 (diff)
Merge pull request #3678 from edwin0cheng/refactor-rename
Fix rename argument in macro call
Diffstat (limited to 'crates/ra_ide')
-rw-r--r--crates/ra_ide/src/references.rs32
-rw-r--r--crates/ra_ide/src/references/rename.rs57
2 files changed, 80 insertions, 9 deletions
diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs
index 3ea0ac230..746cc86ba 100644
--- a/crates/ra_ide/src/references.rs
+++ b/crates/ra_ide/src/references.rs
@@ -94,12 +94,16 @@ pub(crate) fn find_all_refs(
94 let sema = Semantics::new(db); 94 let sema = Semantics::new(db);
95 let syntax = sema.parse(position.file_id).syntax().clone(); 95 let syntax = sema.parse(position.file_id).syntax().clone();
96 96
97 let (opt_name, search_kind) = 97 let (opt_name, search_kind) = if let Some(name) =
98 if let Some(name) = get_struct_def_name_for_struct_literal_search(&syntax, position) { 98 get_struct_def_name_for_struct_literal_search(&sema, &syntax, position)
99 (Some(name), ReferenceKind::StructLiteral) 99 {
100 } else { 100 (Some(name), ReferenceKind::StructLiteral)
101 (find_node_at_offset::<ast::Name>(&syntax, position.offset), ReferenceKind::Other) 101 } else {
102 }; 102 (
103 sema.find_node_at_offset_with_descend::<ast::Name>(&syntax, position.offset),
104 ReferenceKind::Other,
105 )
106 };
103 107
104 let RangeInfo { range, info: def } = find_name(&sema, &syntax, position, opt_name)?; 108 let RangeInfo { range, info: def } = find_name(&sema, &syntax, position, opt_name)?;
105 109
@@ -131,7 +135,8 @@ fn find_name(
131 let range = name.syntax().text_range(); 135 let range = name.syntax().text_range();
132 return Some(RangeInfo::new(range, def)); 136 return Some(RangeInfo::new(range, def));
133 } 137 }
134 let name_ref = find_node_at_offset::<ast::NameRef>(&syntax, position.offset)?; 138 let name_ref =
139 sema.find_node_at_offset_with_descend::<ast::NameRef>(&syntax, position.offset)?;
135 let def = classify_name_ref(sema, &name_ref)?.definition(); 140 let def = classify_name_ref(sema, &name_ref)?.definition();
136 let range = name_ref.syntax().text_range(); 141 let range = name_ref.syntax().text_range();
137 Some(RangeInfo::new(range, def)) 142 Some(RangeInfo::new(range, def))
@@ -157,6 +162,7 @@ fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Optio
157} 162}
158 163
159fn get_struct_def_name_for_struct_literal_search( 164fn get_struct_def_name_for_struct_literal_search(
165 sema: &Semantics<RootDatabase>,
160 syntax: &SyntaxNode, 166 syntax: &SyntaxNode,
161 position: FilePosition, 167 position: FilePosition,
162) -> Option<ast::Name> { 168) -> Option<ast::Name> {
@@ -164,10 +170,18 @@ fn get_struct_def_name_for_struct_literal_search(
164 if right.kind() != SyntaxKind::L_CURLY && right.kind() != SyntaxKind::L_PAREN { 170 if right.kind() != SyntaxKind::L_CURLY && right.kind() != SyntaxKind::L_PAREN {
165 return None; 171 return None;
166 } 172 }
167 if let Some(name) = find_node_at_offset::<ast::Name>(&syntax, left.text_range().start()) { 173 if let Some(name) =
174 sema.find_node_at_offset_with_descend::<ast::Name>(&syntax, left.text_range().start())
175 {
168 return name.syntax().ancestors().find_map(ast::StructDef::cast).and_then(|l| l.name()); 176 return name.syntax().ancestors().find_map(ast::StructDef::cast).and_then(|l| l.name());
169 } 177 }
170 if find_node_at_offset::<ast::TypeParamList>(&syntax, left.text_range().start()).is_some() { 178 if sema
179 .find_node_at_offset_with_descend::<ast::TypeParamList>(
180 &syntax,
181 left.text_range().start(),
182 )
183 .is_some()
184 {
171 return left.ancestors().find_map(ast::StructDef::cast).and_then(|l| l.name()); 185 return left.ancestors().find_map(ast::StructDef::cast).and_then(|l| l.name());
172 } 186 }
173 } 187 }
diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs
index 7d1190af9..9acc6158a 100644
--- a/crates/ra_ide/src/references/rename.rs
+++ b/crates/ra_ide/src/references/rename.rs
@@ -250,6 +250,63 @@ mod tests {
250 } 250 }
251 251
252 #[test] 252 #[test]
253 fn test_rename_for_macro_args_rev() {
254 test_rename(
255 r#"
256 macro_rules! foo {($i:ident) => {$i} }
257 fn main() {
258 let a = "test";
259 foo!(a<|>);
260 }"#,
261 "b",
262 r#"
263 macro_rules! foo {($i:ident) => {$i} }
264 fn main() {
265 let b = "test";
266 foo!(b);
267 }"#,
268 );
269 }
270
271 #[test]
272 fn test_rename_for_macro_define_fn() {
273 test_rename(
274 r#"
275 macro_rules! define_fn {($id:ident) => { fn $id{} }}
276 define_fn!(foo);
277 fn main() {
278 fo<|>o();
279 }"#,
280 "bar",
281 r#"
282 macro_rules! define_fn {($id:ident) => { fn $id{} }}
283 define_fn!(bar);
284 fn main() {
285 bar();
286 }"#,
287 );
288 }
289
290 #[test]
291 fn test_rename_for_macro_define_fn_rev() {
292 test_rename(
293 r#"
294 macro_rules! define_fn {($id:ident) => { fn $id{} }}
295 define_fn!(fo<|>o);
296 fn main() {
297 foo();
298 }"#,
299 "bar",
300 r#"
301 macro_rules! define_fn {($id:ident) => { fn $id{} }}
302 define_fn!(bar);
303 fn main() {
304 bar();
305 }"#,
306 );
307 }
308
309 #[test]
253 fn test_rename_for_param_inside() { 310 fn test_rename_for_param_inside() {
254 test_rename( 311 test_rename(
255 r#" 312 r#"