aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/handlers
diff options
context:
space:
mode:
authorBenjamin Coenen <[email protected]>2021-01-21 08:53:24 +0000
committerBenjamin Coenen <[email protected]>2021-01-21 08:53:24 +0000
commit06f1c8f5a10f0114cbd94111312ea58d59570efc (patch)
treeb4b967dc4a9361d6c6be070edd971d9ea6f13438 /crates/assists/src/handlers
parent35d9944c17d1477fa78cb0683fe60a8445458586 (diff)
Add assist: add lifetime to type #7200
Signed-off-by: Benjamin Coenen <[email protected]>
Diffstat (limited to 'crates/assists/src/handlers')
-rw-r--r--crates/assists/src/handlers/add_lifetime_to_type.rs37
1 files changed, 21 insertions, 16 deletions
diff --git a/crates/assists/src/handlers/add_lifetime_to_type.rs b/crates/assists/src/handlers/add_lifetime_to_type.rs
index 3743858a8..c1603e972 100644
--- a/crates/assists/src/handlers/add_lifetime_to_type.rs
+++ b/crates/assists/src/handlers/add_lifetime_to_type.rs
@@ -8,8 +8,8 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
8// Adds a new lifetime to a struct, enum or union. 8// Adds a new lifetime to a struct, enum or union.
9// 9//
10// ``` 10// ```
11// struct Point$0 { 11// struct Point {
12// x: &u32, 12// x: &$0u32,
13// y: u32, 13// y: u32,
14// } 14// }
15// ``` 15// ```
@@ -21,6 +21,11 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
21// } 21// }
22// ``` 22// ```
23pub(crate) fn add_lifetime_to_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 23pub(crate) fn add_lifetime_to_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
24 let ref_type_focused = ctx.find_node_at_offset::<ast::RefType>()?;
25 if ref_type_focused.lifetime().is_some() {
26 return None;
27 }
28
24 let node = ctx.find_node_at_offset::<ast::AdtDef>()?; 29 let node = ctx.find_node_at_offset::<ast::AdtDef>()?;
25 let has_lifetime = node 30 let has_lifetime = node
26 .generic_param_list() 31 .generic_param_list()
@@ -148,13 +153,13 @@ mod tests {
148 fn add_lifetime_to_struct() { 153 fn add_lifetime_to_struct() {
149 check_assist( 154 check_assist(
150 add_lifetime_to_type, 155 add_lifetime_to_type,
151 "struct Foo$0 { a: &i32 }", 156 "struct Foo { a: &$0i32 }",
152 "struct Foo<'a> { a: &'a i32 }", 157 "struct Foo<'a> { a: &'a i32 }",
153 ); 158 );
154 159
155 check_assist( 160 check_assist(
156 add_lifetime_to_type, 161 add_lifetime_to_type,
157 "struct Foo$0 { a: &i32, b: &usize }", 162 "struct Foo { a: &$0i32, b: &usize }",
158 "struct Foo<'a> { a: &'a i32, b: &'a usize }", 163 "struct Foo<'a> { a: &'a i32, b: &'a usize }",
159 ); 164 );
160 165
@@ -166,58 +171,58 @@ mod tests {
166 171
167 check_assist( 172 check_assist(
168 add_lifetime_to_type, 173 add_lifetime_to_type,
169 "struct Foo<T>$0 { a: &T, b: usize }", 174 "struct Foo<T> { a: &$0T, b: usize }",
170 "struct Foo<'a, T> { a: &'a T, b: usize }", 175 "struct Foo<'a, T> { a: &'a T, b: usize }",
171 ); 176 );
172 177
173 check_assist_not_applicable(add_lifetime_to_type, "struct Foo<'a>$0 { a: &'a i32 }"); 178 check_assist_not_applicable(add_lifetime_to_type, "struct Foo<'a> { a: &$0'a i32 }");
174 check_assist_not_applicable(add_lifetime_to_type, "struct Foo$0 { a: &'a i32 }"); 179 check_assist_not_applicable(add_lifetime_to_type, "struct Foo { a: &'a$0 i32 }");
175 } 180 }
176 181
177 #[test] 182 #[test]
178 fn add_lifetime_to_enum() { 183 fn add_lifetime_to_enum() {
179 check_assist( 184 check_assist(
180 add_lifetime_to_type, 185 add_lifetime_to_type,
181 "enum Foo$0 { Bar { a: i32 }, Other, Tuple(u32, &u32)}", 186 "enum Foo { Bar { a: i32 }, Other, Tuple(u32, &$0u32)}",
182 "enum Foo<'a> { Bar { a: i32 }, Other, Tuple(u32, &'a u32)}", 187 "enum Foo<'a> { Bar { a: i32 }, Other, Tuple(u32, &'a u32)}",
183 ); 188 );
184 189
185 check_assist( 190 check_assist(
186 add_lifetime_to_type, 191 add_lifetime_to_type,
187 "enum Foo$0 { Bar { a: &i32 }}", 192 "enum Foo { Bar { a: &$0i32 }}",
188 "enum Foo<'a> { Bar { a: &'a i32 }}", 193 "enum Foo<'a> { Bar { a: &'a i32 }}",
189 ); 194 );
190 195
191 check_assist( 196 check_assist(
192 add_lifetime_to_type, 197 add_lifetime_to_type,
193 "enum Foo<T>$0 { Bar { a: &i32, b: &T }}", 198 "enum Foo<T> { Bar { a: &$0i32, b: &T }}",
194 "enum Foo<'a, T> { Bar { a: &'a i32, b: &'a T }}", 199 "enum Foo<'a, T> { Bar { a: &'a i32, b: &'a T }}",
195 ); 200 );
196 201
197 check_assist_not_applicable(add_lifetime_to_type, "enum Foo<'a>$0 { Bar { a: &'a i32 }}"); 202 check_assist_not_applicable(add_lifetime_to_type, "enum Foo<'a> { Bar { a: &$0'a i32 }}");
198 check_assist_not_applicable(add_lifetime_to_type, "enum Foo$0 { Bar, Misc }"); 203 check_assist_not_applicable(add_lifetime_to_type, "enum Foo { Bar, $0Misc }");
199 } 204 }
200 205
201 #[test] 206 #[test]
202 fn add_lifetime_to_union() { 207 fn add_lifetime_to_union() {
203 check_assist( 208 check_assist(
204 add_lifetime_to_type, 209 add_lifetime_to_type,
205 "union Foo$0 { a: &i32 }", 210 "union Foo { a: &$0i32 }",
206 "union Foo<'a> { a: &'a i32 }", 211 "union Foo<'a> { a: &'a i32 }",
207 ); 212 );
208 213
209 check_assist( 214 check_assist(
210 add_lifetime_to_type, 215 add_lifetime_to_type,
211 "union Foo$0 { a: &i32, b: &usize }", 216 "union Foo { a: &$0i32, b: &usize }",
212 "union Foo<'a> { a: &'a i32, b: &'a usize }", 217 "union Foo<'a> { a: &'a i32, b: &'a usize }",
213 ); 218 );
214 219
215 check_assist( 220 check_assist(
216 add_lifetime_to_type, 221 add_lifetime_to_type,
217 "union Foo<T>$0 { a: &T, b: usize }", 222 "union Foo<T> { a: &$0T, b: usize }",
218 "union Foo<'a, T> { a: &'a T, b: usize }", 223 "union Foo<'a, T> { a: &'a T, b: usize }",
219 ); 224 );
220 225
221 check_assist_not_applicable(add_lifetime_to_type, "struct Foo<'a>$0 { a: &'a i32 }"); 226 check_assist_not_applicable(add_lifetime_to_type, "struct Foo<'a> { a: &'a $0i32 }");
222 } 227 }
223} 228}