aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/handlers/introduce_named_lifetime.rs (renamed from crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs)62
-rw-r--r--crates/ra_assists/src/lib.rs4
-rw-r--r--crates/ra_assists/src/tests/generated.rs50
3 files changed, 58 insertions, 58 deletions
diff --git a/crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs b/crates/ra_assists/src/handlers/introduce_named_lifetime.rs
index 999aec421..beb5b7366 100644
--- a/crates/ra_assists/src/handlers/change_lifetime_anon_to_named.rs
+++ b/crates/ra_assists/src/handlers/introduce_named_lifetime.rs
@@ -1,12 +1,15 @@
1use crate::{assist_context::AssistBuilder, AssistContext, AssistId, Assists}; 1use ra_syntax::{
2use ast::{NameOwner, ParamList, TypeAscriptionOwner, TypeParamList, TypeRef}; 2 ast::{self, NameOwner, TypeAscriptionOwner, TypeParamsOwner},
3use ra_syntax::{ast, ast::TypeParamsOwner, AstNode, SyntaxKind, TextRange, TextSize}; 3 AstNode, SyntaxKind, TextRange, TextSize,
4};
4use rustc_hash::FxHashSet; 5use rustc_hash::FxHashSet;
5 6
6static ASSIST_NAME: &str = "change_lifetime_anon_to_named"; 7use crate::{assist_context::AssistBuilder, AssistContext, AssistId, Assists};
7static ASSIST_LABEL: &str = "Give anonymous lifetime a name"; 8
9static ASSIST_NAME: &str = "introduce_named_lifetime";
10static ASSIST_LABEL: &str = "Introduce named lifetime";
8 11
9// Assist: change_lifetime_anon_to_named 12// Assist: introduce_named_lifetime
10// 13//
11// Change an anonymous lifetime to a named lifetime. 14// Change an anonymous lifetime to a named lifetime.
12// 15//
@@ -31,7 +34,7 @@ static ASSIST_LABEL: &str = "Give anonymous lifetime a name";
31// ``` 34// ```
32// FIXME: How can we handle renaming any one of multiple anonymous lifetimes? 35// FIXME: How can we handle renaming any one of multiple anonymous lifetimes?
33// FIXME: should also add support for the case fun(f: &Foo) -> &<|>Foo 36// FIXME: should also add support for the case fun(f: &Foo) -> &<|>Foo
34pub(crate) fn change_lifetime_anon_to_named(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 37pub(crate) fn introduce_named_lifetime(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
35 let lifetime_token = ctx 38 let lifetime_token = ctx
36 .find_token_at_offset(SyntaxKind::LIFETIME) 39 .find_token_at_offset(SyntaxKind::LIFETIME)
37 .filter(|lifetime| lifetime.text() == "'_")?; 40 .filter(|lifetime| lifetime.text() == "'_")?;
@@ -52,7 +55,7 @@ fn generate_fn_def_assist(
52 fn_def: &ast::FnDef, 55 fn_def: &ast::FnDef,
53 lifetime_loc: TextRange, 56 lifetime_loc: TextRange,
54) -> Option<()> { 57) -> Option<()> {
55 let param_list: ParamList = fn_def.param_list()?; 58 let param_list: ast::ParamList = fn_def.param_list()?;
56 let new_lifetime_param = generate_unique_lifetime_param_name(&fn_def.type_param_list())?; 59 let new_lifetime_param = generate_unique_lifetime_param_name(&fn_def.type_param_list())?;
57 let end_of_fn_ident = fn_def.name()?.ident_token()?.text_range().end(); 60 let end_of_fn_ident = fn_def.name()?.ident_token()?.text_range().end();
58 let self_param = 61 let self_param =
@@ -67,7 +70,7 @@ fn generate_fn_def_assist(
67 let fn_params_without_lifetime: Vec<_> = param_list 70 let fn_params_without_lifetime: Vec<_> = param_list
68 .params() 71 .params()
69 .filter_map(|param| match param.ascribed_type() { 72 .filter_map(|param| match param.ascribed_type() {
70 Some(TypeRef::ReferenceType(ascribed_type)) 73 Some(ast::TypeRef::ReferenceType(ascribed_type))
71 if ascribed_type.lifetime_token() == None => 74 if ascribed_type.lifetime_token() == None =>
72 { 75 {
73 Some(ascribed_type.amp_token()?.text_range().end()) 76 Some(ascribed_type.amp_token()?.text_range().end())
@@ -106,7 +109,7 @@ fn generate_impl_def_assist(
106/// Given a type parameter list, generate a unique lifetime parameter name 109/// Given a type parameter list, generate a unique lifetime parameter name
107/// which is not in the list 110/// which is not in the list
108fn generate_unique_lifetime_param_name( 111fn generate_unique_lifetime_param_name(
109 existing_type_param_list: &Option<TypeParamList>, 112 existing_type_param_list: &Option<ast::TypeParamList>,
110) -> Option<char> { 113) -> Option<char> {
111 match existing_type_param_list { 114 match existing_type_param_list {
112 Some(type_params) => { 115 Some(type_params) => {
@@ -151,7 +154,7 @@ mod tests {
151 #[test] 154 #[test]
152 fn test_example_case() { 155 fn test_example_case() {
153 check_assist( 156 check_assist(
154 change_lifetime_anon_to_named, 157 introduce_named_lifetime,
155 r#"impl Cursor<'_<|>> { 158 r#"impl Cursor<'_<|>> {
156 fn node(self) -> &SyntaxNode { 159 fn node(self) -> &SyntaxNode {
157 match self { 160 match self {
@@ -172,7 +175,7 @@ mod tests {
172 #[test] 175 #[test]
173 fn test_example_case_simplified() { 176 fn test_example_case_simplified() {
174 check_assist( 177 check_assist(
175 change_lifetime_anon_to_named, 178 introduce_named_lifetime,
176 r#"impl Cursor<'_<|>> {"#, 179 r#"impl Cursor<'_<|>> {"#,
177 r#"impl<'a> Cursor<'a> {"#, 180 r#"impl<'a> Cursor<'a> {"#,
178 ); 181 );
@@ -181,7 +184,7 @@ mod tests {
181 #[test] 184 #[test]
182 fn test_example_case_cursor_after_tick() { 185 fn test_example_case_cursor_after_tick() {
183 check_assist( 186 check_assist(
184 change_lifetime_anon_to_named, 187 introduce_named_lifetime,
185 r#"impl Cursor<'<|>_> {"#, 188 r#"impl Cursor<'<|>_> {"#,
186 r#"impl<'a> Cursor<'a> {"#, 189 r#"impl<'a> Cursor<'a> {"#,
187 ); 190 );
@@ -190,7 +193,7 @@ mod tests {
190 #[test] 193 #[test]
191 fn test_example_case_cursor_before_tick() { 194 fn test_example_case_cursor_before_tick() {
192 check_assist( 195 check_assist(
193 change_lifetime_anon_to_named, 196 introduce_named_lifetime,
194 r#"impl Cursor<<|>'_> {"#, 197 r#"impl Cursor<<|>'_> {"#,
195 r#"impl<'a> Cursor<'a> {"#, 198 r#"impl<'a> Cursor<'a> {"#,
196 ); 199 );
@@ -198,23 +201,20 @@ mod tests {
198 201
199 #[test] 202 #[test]
200 fn test_not_applicable_cursor_position() { 203 fn test_not_applicable_cursor_position() {
201 check_assist_not_applicable(change_lifetime_anon_to_named, r#"impl Cursor<'_><|> {"#); 204 check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor<'_><|> {"#);
202 check_assist_not_applicable(change_lifetime_anon_to_named, r#"impl Cursor<|><'_> {"#); 205 check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor<|><'_> {"#);
203 } 206 }
204 207
205 #[test] 208 #[test]
206 fn test_not_applicable_lifetime_already_name() { 209 fn test_not_applicable_lifetime_already_name() {
207 check_assist_not_applicable(change_lifetime_anon_to_named, r#"impl Cursor<'a<|>> {"#); 210 check_assist_not_applicable(introduce_named_lifetime, r#"impl Cursor<'a<|>> {"#);
208 check_assist_not_applicable( 211 check_assist_not_applicable(introduce_named_lifetime, r#"fn my_fun<'a>() -> X<'a<|>>"#);
209 change_lifetime_anon_to_named,
210 r#"fn my_fun<'a>() -> X<'a<|>>"#,
211 );
212 } 212 }
213 213
214 #[test] 214 #[test]
215 fn test_with_type_parameter() { 215 fn test_with_type_parameter() {
216 check_assist( 216 check_assist(
217 change_lifetime_anon_to_named, 217 introduce_named_lifetime,
218 r#"impl<T> Cursor<T, '_<|>>"#, 218 r#"impl<T> Cursor<T, '_<|>>"#,
219 r#"impl<T, 'a> Cursor<T, 'a>"#, 219 r#"impl<T, 'a> Cursor<T, 'a>"#,
220 ); 220 );
@@ -223,7 +223,7 @@ mod tests {
223 #[test] 223 #[test]
224 fn test_with_existing_lifetime_name_conflict() { 224 fn test_with_existing_lifetime_name_conflict() {
225 check_assist( 225 check_assist(
226 change_lifetime_anon_to_named, 226 introduce_named_lifetime,
227 r#"impl<'a, 'b> Cursor<'a, 'b, '_<|>>"#, 227 r#"impl<'a, 'b> Cursor<'a, 'b, '_<|>>"#,
228 r#"impl<'a, 'b, 'c> Cursor<'a, 'b, 'c>"#, 228 r#"impl<'a, 'b, 'c> Cursor<'a, 'b, 'c>"#,
229 ); 229 );
@@ -232,7 +232,7 @@ mod tests {
232 #[test] 232 #[test]
233 fn test_function_return_value_anon_lifetime_param() { 233 fn test_function_return_value_anon_lifetime_param() {
234 check_assist( 234 check_assist(
235 change_lifetime_anon_to_named, 235 introduce_named_lifetime,
236 r#"fn my_fun() -> X<'_<|>>"#, 236 r#"fn my_fun() -> X<'_<|>>"#,
237 r#"fn my_fun<'a>() -> X<'a>"#, 237 r#"fn my_fun<'a>() -> X<'a>"#,
238 ); 238 );
@@ -241,7 +241,7 @@ mod tests {
241 #[test] 241 #[test]
242 fn test_function_return_value_anon_reference_lifetime() { 242 fn test_function_return_value_anon_reference_lifetime() {
243 check_assist( 243 check_assist(
244 change_lifetime_anon_to_named, 244 introduce_named_lifetime,
245 r#"fn my_fun() -> &'_<|> X"#, 245 r#"fn my_fun() -> &'_<|> X"#,
246 r#"fn my_fun<'a>() -> &'a X"#, 246 r#"fn my_fun<'a>() -> &'a X"#,
247 ); 247 );
@@ -250,7 +250,7 @@ mod tests {
250 #[test] 250 #[test]
251 fn test_function_param_anon_lifetime() { 251 fn test_function_param_anon_lifetime() {
252 check_assist( 252 check_assist(
253 change_lifetime_anon_to_named, 253 introduce_named_lifetime,
254 r#"fn my_fun(x: X<'_<|>>)"#, 254 r#"fn my_fun(x: X<'_<|>>)"#,
255 r#"fn my_fun<'a>(x: X<'a>)"#, 255 r#"fn my_fun<'a>(x: X<'a>)"#,
256 ); 256 );
@@ -259,7 +259,7 @@ mod tests {
259 #[test] 259 #[test]
260 fn test_function_add_lifetime_to_params() { 260 fn test_function_add_lifetime_to_params() {
261 check_assist( 261 check_assist(
262 change_lifetime_anon_to_named, 262 introduce_named_lifetime,
263 r#"fn my_fun(f: &Foo) -> X<'_<|>>"#, 263 r#"fn my_fun(f: &Foo) -> X<'_<|>>"#,
264 r#"fn my_fun<'a>(f: &'a Foo) -> X<'a>"#, 264 r#"fn my_fun<'a>(f: &'a Foo) -> X<'a>"#,
265 ); 265 );
@@ -268,7 +268,7 @@ mod tests {
268 #[test] 268 #[test]
269 fn test_function_add_lifetime_to_params_in_presence_of_other_lifetime() { 269 fn test_function_add_lifetime_to_params_in_presence_of_other_lifetime() {
270 check_assist( 270 check_assist(
271 change_lifetime_anon_to_named, 271 introduce_named_lifetime,
272 r#"fn my_fun<'other>(f: &Foo, b: &'other Bar) -> X<'_<|>>"#, 272 r#"fn my_fun<'other>(f: &Foo, b: &'other Bar) -> X<'_<|>>"#,
273 r#"fn my_fun<'other, 'a>(f: &'a Foo, b: &'other Bar) -> X<'a>"#, 273 r#"fn my_fun<'other, 'a>(f: &'a Foo, b: &'other Bar) -> X<'a>"#,
274 ); 274 );
@@ -278,7 +278,7 @@ mod tests {
278 fn test_function_not_applicable_without_self_and_multiple_unnamed_param_lifetimes() { 278 fn test_function_not_applicable_without_self_and_multiple_unnamed_param_lifetimes() {
279 // this is not permitted under lifetime elision rules 279 // this is not permitted under lifetime elision rules
280 check_assist_not_applicable( 280 check_assist_not_applicable(
281 change_lifetime_anon_to_named, 281 introduce_named_lifetime,
282 r#"fn my_fun(f: &Foo, b: &Bar) -> X<'_<|>>"#, 282 r#"fn my_fun(f: &Foo, b: &Bar) -> X<'_<|>>"#,
283 ); 283 );
284 } 284 }
@@ -286,7 +286,7 @@ mod tests {
286 #[test] 286 #[test]
287 fn test_function_add_lifetime_to_self_ref_param() { 287 fn test_function_add_lifetime_to_self_ref_param() {
288 check_assist( 288 check_assist(
289 change_lifetime_anon_to_named, 289 introduce_named_lifetime,
290 r#"fn my_fun<'other>(&self, f: &Foo, b: &'other Bar) -> X<'_<|>>"#, 290 r#"fn my_fun<'other>(&self, f: &Foo, b: &'other Bar) -> X<'_<|>>"#,
291 r#"fn my_fun<'other, 'a>(&'a self, f: &Foo, b: &'other Bar) -> X<'a>"#, 291 r#"fn my_fun<'other, 'a>(&'a self, f: &Foo, b: &'other Bar) -> X<'a>"#,
292 ); 292 );
@@ -295,7 +295,7 @@ mod tests {
295 #[test] 295 #[test]
296 fn test_function_add_lifetime_to_param_with_non_ref_self() { 296 fn test_function_add_lifetime_to_param_with_non_ref_self() {
297 check_assist( 297 check_assist(
298 change_lifetime_anon_to_named, 298 introduce_named_lifetime,
299 r#"fn my_fun<'other>(self, f: &Foo, b: &'other Bar) -> X<'_<|>>"#, 299 r#"fn my_fun<'other>(self, f: &Foo, b: &'other Bar) -> X<'_<|>>"#,
300 r#"fn my_fun<'other, 'a>(self, f: &'a Foo, b: &'other Bar) -> X<'a>"#, 300 r#"fn my_fun<'other, 'a>(self, f: &'a Foo, b: &'other Bar) -> X<'a>"#,
301 ); 301 );
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index 3f8f7ffbf..fb5d59a87 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -112,7 +112,6 @@ mod handlers {
112 mod add_turbo_fish; 112 mod add_turbo_fish;
113 mod apply_demorgan; 113 mod apply_demorgan;
114 mod auto_import; 114 mod auto_import;
115 mod change_lifetime_anon_to_named;
116 mod change_return_type_to_result; 115 mod change_return_type_to_result;
117 mod change_visibility; 116 mod change_visibility;
118 mod early_return; 117 mod early_return;
@@ -122,6 +121,7 @@ mod handlers {
122 mod flip_comma; 121 mod flip_comma;
123 mod flip_trait_bound; 122 mod flip_trait_bound;
124 mod inline_local_variable; 123 mod inline_local_variable;
124 mod introduce_named_lifetime;
125 mod introduce_variable; 125 mod introduce_variable;
126 mod invert_if; 126 mod invert_if;
127 mod merge_imports; 127 mod merge_imports;
@@ -152,7 +152,6 @@ mod handlers {
152 add_turbo_fish::add_turbo_fish, 152 add_turbo_fish::add_turbo_fish,
153 apply_demorgan::apply_demorgan, 153 apply_demorgan::apply_demorgan,
154 auto_import::auto_import, 154 auto_import::auto_import,
155 change_lifetime_anon_to_named::change_lifetime_anon_to_named,
156 change_return_type_to_result::change_return_type_to_result, 155 change_return_type_to_result::change_return_type_to_result,
157 change_visibility::change_visibility, 156 change_visibility::change_visibility,
158 early_return::convert_to_guarded_return, 157 early_return::convert_to_guarded_return,
@@ -162,6 +161,7 @@ mod handlers {
162 flip_comma::flip_comma, 161 flip_comma::flip_comma,
163 flip_trait_bound::flip_trait_bound, 162 flip_trait_bound::flip_trait_bound,
164 inline_local_variable::inline_local_variable, 163 inline_local_variable::inline_local_variable,
164 introduce_named_lifetime::introduce_named_lifetime,
165 introduce_variable::introduce_variable, 165 introduce_variable::introduce_variable,
166 invert_if::invert_if, 166 invert_if::invert_if,
167 merge_imports::merge_imports, 167 merge_imports::merge_imports,
diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs
index 73d43283d..d17504529 100644
--- a/crates/ra_assists/src/tests/generated.rs
+++ b/crates/ra_assists/src/tests/generated.rs
@@ -288,31 +288,6 @@ pub mod std { pub mod collections { pub struct HashMap { } } }
288} 288}
289 289
290#[test] 290#[test]
291fn doctest_change_lifetime_anon_to_named() {
292 check_doc_test(
293 "change_lifetime_anon_to_named",
294 r#####"
295impl Cursor<'_<|>> {
296 fn node(self) -> &SyntaxNode {
297 match self {
298 Cursor::Replace(node) | Cursor::Before(node) => node,
299 }
300 }
301}
302"#####,
303 r#####"
304impl<'a> Cursor<'a> {
305 fn node(self) -> &SyntaxNode {
306 match self {
307 Cursor::Replace(node) | Cursor::Before(node) => node,
308 }
309 }
310}
311"#####,
312 )
313}
314
315#[test]
316fn doctest_change_return_type_to_result() { 291fn doctest_change_return_type_to_result() {
317 check_doc_test( 292 check_doc_test(
318 "change_return_type_to_result", 293 "change_return_type_to_result",
@@ -477,6 +452,31 @@ fn main() {
477} 452}
478 453
479#[test] 454#[test]
455fn doctest_introduce_named_lifetime() {
456 check_doc_test(
457 "introduce_named_lifetime",
458 r#####"
459impl Cursor<'_<|>> {
460 fn node(self) -> &SyntaxNode {
461 match self {
462 Cursor::Replace(node) | Cursor::Before(node) => node,
463 }
464 }
465}
466"#####,
467 r#####"
468impl<'a> Cursor<'a> {
469 fn node(self) -> &SyntaxNode {
470 match self {
471 Cursor::Replace(node) | Cursor::Before(node) => node,
472 }
473 }
474}
475"#####,
476 )
477}
478
479#[test]
480fn doctest_introduce_variable() { 480fn doctest_introduce_variable() {
481 check_doc_test( 481 check_doc_test(
482 "introduce_variable", 482 "introduce_variable",