diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-03-11 09:22:09 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-03-11 09:22:09 +0000 |
commit | bf77850c8106a71b317fb6c707a28e00d258884c (patch) | |
tree | 314afb27e80d7de8ccaf3f2eaa7b2e309de2e87f /crates/ra_ide_db | |
parent | 0714a065d578e8b22b0451bfc64378c875fe858f (diff) | |
parent | 13ccbb2919dff8e98d0a242d9d6b7edd17a6bd2c (diff) |
Merge #3542
3542: Renames work on struct field shorthands r=matklad a=m-n
When renaming either a local or a struct field, struct field shorthands are now renamed correctly.
Happy to refactor this if it doesn't fit the design of the code. Thanks for adding the suggestion of where to start on the issue.
I wasn't sure if I should also look at the behavior of renaming when placing the cursor at the field shorthand; the following describes the behavior with this patch:
```rust
#[test]
fn test_rename_field_shorthand_for_unspecified() {
// when renaming a shorthand, should we have a way to specify
// between renaming the field and the local?
//
// If not is this the correct default?
test_rename(
r#"
struct Foo {
i: i32,
}
impl Foo {
fn new(i: i32) -> Self {
Self { i<|> }
}
}
"#,
"j",
r#"
struct Foo {
i: i32,
}
impl Foo {
fn new(j: i32) -> Self {
Self { i: j }
}
}
"#,
);
}
```
Resolves #3431
Co-authored-by: Matt Niemeir <[email protected]>
Diffstat (limited to 'crates/ra_ide_db')
-rw-r--r-- | crates/ra_ide_db/src/search.rs | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/crates/ra_ide_db/src/search.rs b/crates/ra_ide_db/src/search.rs index 6f198df04..cf78d3e41 100644 --- a/crates/ra_ide_db/src/search.rs +++ b/crates/ra_ide_db/src/search.rs | |||
@@ -17,7 +17,7 @@ use rustc_hash::FxHashMap; | |||
17 | use test_utils::tested_by; | 17 | use test_utils::tested_by; |
18 | 18 | ||
19 | use crate::{ | 19 | use crate::{ |
20 | defs::{classify_name_ref, Definition}, | 20 | defs::{classify_name_ref, Definition, NameRefClass}, |
21 | RootDatabase, | 21 | RootDatabase, |
22 | }; | 22 | }; |
23 | 23 | ||
@@ -30,6 +30,8 @@ pub struct Reference { | |||
30 | 30 | ||
31 | #[derive(Debug, Clone, PartialEq)] | 31 | #[derive(Debug, Clone, PartialEq)] |
32 | pub enum ReferenceKind { | 32 | pub enum ReferenceKind { |
33 | StructFieldShorthandForField, | ||
34 | StructFieldShorthandForLocal, | ||
33 | StructLiteral, | 35 | StructLiteral, |
34 | Other, | 36 | Other, |
35 | } | 37 | } |
@@ -237,9 +239,8 @@ impl Definition { | |||
237 | // FIXME: reuse sb | 239 | // FIXME: reuse sb |
238 | // See https://github.com/rust-lang/rust/pull/68198#issuecomment-574269098 | 240 | // See https://github.com/rust-lang/rust/pull/68198#issuecomment-574269098 |
239 | 241 | ||
240 | if let Some(d) = classify_name_ref(&sema, &name_ref) { | 242 | match classify_name_ref(&sema, &name_ref) { |
241 | let d = d.definition(); | 243 | Some(NameRefClass::Definition(def)) if &def == self => { |
242 | if &d == self { | ||
243 | let kind = if is_record_lit_name_ref(&name_ref) | 244 | let kind = if is_record_lit_name_ref(&name_ref) |
244 | || is_call_expr_name_ref(&name_ref) | 245 | || is_call_expr_name_ref(&name_ref) |
245 | { | 246 | { |
@@ -252,9 +253,26 @@ impl Definition { | |||
252 | refs.push(Reference { | 253 | refs.push(Reference { |
253 | file_range, | 254 | file_range, |
254 | kind, | 255 | kind, |
255 | access: reference_access(&d, &name_ref), | 256 | access: reference_access(&def, &name_ref), |
256 | }); | 257 | }); |
257 | } | 258 | } |
259 | Some(NameRefClass::FieldShorthand { local, field }) => { | ||
260 | match self { | ||
261 | Definition::StructField(_) if &field == self => refs.push(Reference { | ||
262 | file_range: sema.original_range(name_ref.syntax()), | ||
263 | kind: ReferenceKind::StructFieldShorthandForField, | ||
264 | access: reference_access(&field, &name_ref), | ||
265 | }), | ||
266 | Definition::Local(l) if &local == l => refs.push(Reference { | ||
267 | file_range: sema.original_range(name_ref.syntax()), | ||
268 | kind: ReferenceKind::StructFieldShorthandForLocal, | ||
269 | access: reference_access(&Definition::Local(local), &name_ref), | ||
270 | }), | ||
271 | |||
272 | _ => {} // not a usage | ||
273 | }; | ||
274 | } | ||
275 | _ => {} // not a usage | ||
258 | } | 276 | } |
259 | } | 277 | } |
260 | } | 278 | } |