diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-02-12 17:16:02 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-02-12 17:16:02 +0000 |
commit | 4d51b5644458c7dcb97a4d445f1b379cd2548a78 (patch) | |
tree | e67df57ec40bd510028d0002306debd8c73f61b8 /crates/assists/src/handlers/generate_getter_mut.rs | |
parent | 29bc6b45fbcefc3369c14035126a8b0510b2ef60 (diff) | |
parent | ac959b82b3408fafd22f4fbb59e10383a18c545f (diff) |
Merge #7650
7650: Add `find_impl_block_end` assist helper r=Veykril a=yoshuawuyts
Fixes #7605. This makes it so assists can use helpers to either append a method to the start or the end of an `impl` block. Thanks!
@Veykril if this is merged, perhaps it could be good to update the gif in https://github.com/rust-analyzer/rust-analyzer/pull/7617#issuecomment-776622135 ? -- this should fix the ordering issue when generating multiple methods.
Co-authored-by: Yoshua Wuyts <[email protected]>
Diffstat (limited to 'crates/assists/src/handlers/generate_getter_mut.rs')
-rw-r--r-- | crates/assists/src/handlers/generate_getter_mut.rs | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/crates/assists/src/handlers/generate_getter_mut.rs b/crates/assists/src/handlers/generate_getter_mut.rs index b5085035e..bf0d99881 100644 --- a/crates/assists/src/handlers/generate_getter_mut.rs +++ b/crates/assists/src/handlers/generate_getter_mut.rs | |||
@@ -3,7 +3,7 @@ use syntax::ast::VisibilityOwner; | |||
3 | use syntax::ast::{self, AstNode, NameOwner}; | 3 | use syntax::ast::{self, AstNode, NameOwner}; |
4 | 4 | ||
5 | use crate::{ | 5 | use crate::{ |
6 | utils::{find_impl_block, find_struct_impl, generate_impl_text}, | 6 | utils::{find_impl_block_end, find_struct_impl, generate_impl_text}, |
7 | AssistContext, AssistId, AssistKind, Assists, | 7 | AssistContext, AssistId, AssistKind, Assists, |
8 | }; | 8 | }; |
9 | 9 | ||
@@ -76,7 +76,7 @@ pub(crate) fn generate_getter_mut(acc: &mut Assists, ctx: &AssistContext) -> Opt | |||
76 | ); | 76 | ); |
77 | 77 | ||
78 | let start_offset = impl_def | 78 | let start_offset = impl_def |
79 | .and_then(|impl_def| find_impl_block(impl_def, &mut buf)) | 79 | .and_then(|impl_def| find_impl_block_end(impl_def, &mut buf)) |
80 | .unwrap_or_else(|| { | 80 | .unwrap_or_else(|| { |
81 | buf = generate_impl_text(&ast::Adt::Struct(strukt.clone()), &buf); | 81 | buf = generate_impl_text(&ast::Adt::Struct(strukt.clone()), &buf); |
82 | strukt.syntax().text_range().end() | 82 | strukt.syntax().text_range().end() |
@@ -156,4 +156,40 @@ impl<T: Clone> Context<T> { | |||
156 | }"#, | 156 | }"#, |
157 | ); | 157 | ); |
158 | } | 158 | } |
159 | |||
160 | #[test] | ||
161 | fn test_multiple_generate_getter_mut() { | ||
162 | check_assist( | ||
163 | generate_getter_mut, | ||
164 | r#" | ||
165 | struct Context<T: Clone> { | ||
166 | data: T, | ||
167 | cou$0nt: usize, | ||
168 | } | ||
169 | |||
170 | impl<T: Clone> Context<T> { | ||
171 | /// Get a mutable reference to the context's data. | ||
172 | fn data_mut(&mut self) -> &mut T { | ||
173 | &mut self.data | ||
174 | } | ||
175 | }"#, | ||
176 | r#" | ||
177 | struct Context<T: Clone> { | ||
178 | data: T, | ||
179 | count: usize, | ||
180 | } | ||
181 | |||
182 | impl<T: Clone> Context<T> { | ||
183 | /// Get a mutable reference to the context's data. | ||
184 | fn data_mut(&mut self) -> &mut T { | ||
185 | &mut self.data | ||
186 | } | ||
187 | |||
188 | /// Get a mutable reference to the context's count. | ||
189 | fn count_mut(&mut self) -> &mut usize { | ||
190 | &mut self.count | ||
191 | } | ||
192 | }"#, | ||
193 | ); | ||
194 | } | ||
159 | } | 195 | } |