diff options
author | Aleksey Kladov <[email protected]> | 2020-05-20 13:13:17 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-05-20 13:13:17 +0100 |
commit | ba3a58d1b2d76bae2ac84923d12918a32ad680f6 (patch) | |
tree | 0eb3b895a9f37fea9d056151a4ec63fab120ada3 /crates/ra_assists/src | |
parent | c446fd76a2a6191adce87b20707a37bd46cb85a9 (diff) |
Snippetify fix_visibility
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r-- | crates/ra_assists/src/handlers/fix_visibility.rs | 64 | ||||
-rw-r--r-- | crates/ra_assists/src/tests/generated.rs | 2 |
2 files changed, 35 insertions, 31 deletions
diff --git a/crates/ra_assists/src/handlers/fix_visibility.rs b/crates/ra_assists/src/handlers/fix_visibility.rs index 48ce07ca5..9ec42f568 100644 --- a/crates/ra_assists/src/handlers/fix_visibility.rs +++ b/crates/ra_assists/src/handlers/fix_visibility.rs | |||
@@ -25,7 +25,7 @@ use crate::{AssistContext, AssistId, Assists}; | |||
25 | // -> | 25 | // -> |
26 | // ``` | 26 | // ``` |
27 | // mod m { | 27 | // mod m { |
28 | // pub(crate) fn frobnicate() {} | 28 | // $0pub(crate) fn frobnicate() {} |
29 | // } | 29 | // } |
30 | // fn main() { | 30 | // fn main() { |
31 | // m::frobnicate() {} | 31 | // m::frobnicate() {} |
@@ -62,10 +62,12 @@ fn add_vis_to_referenced_module_def(acc: &mut Assists, ctx: &AssistContext) -> O | |||
62 | Some(name) => format!("Change visibility of {} to {}", name, missing_visibility), | 62 | Some(name) => format!("Change visibility of {} to {}", name, missing_visibility), |
63 | }; | 63 | }; |
64 | 64 | ||
65 | acc.add(AssistId("fix_visibility"), assist_label, target, |edit| { | 65 | acc.add(AssistId("fix_visibility"), assist_label, target, |builder| { |
66 | edit.set_file(target_file); | 66 | builder.set_file(target_file); |
67 | edit.insert(offset, format!("{} ", missing_visibility)); | 67 | match ctx.config.snippet_cap { |
68 | edit.set_cursor(offset); | 68 | Some(cap) => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)), |
69 | None => builder.insert(offset, format!("{} ", missing_visibility)), | ||
70 | } | ||
69 | }) | 71 | }) |
70 | } | 72 | } |
71 | 73 | ||
@@ -103,10 +105,12 @@ fn add_vis_to_referenced_record_field(acc: &mut Assists, ctx: &AssistContext) -> | |||
103 | let assist_label = | 105 | let assist_label = |
104 | format!("Change visibility of {}.{} to {}", parent_name, target_name, missing_visibility); | 106 | format!("Change visibility of {}.{} to {}", parent_name, target_name, missing_visibility); |
105 | 107 | ||
106 | acc.add(AssistId("fix_visibility"), assist_label, target, |edit| { | 108 | acc.add(AssistId("fix_visibility"), assist_label, target, |builder| { |
107 | edit.set_file(target_file); | 109 | builder.set_file(target_file); |
108 | edit.insert(offset, format!("{} ", missing_visibility)); | 110 | match ctx.config.snippet_cap { |
109 | edit.set_cursor(offset) | 111 | Some(cap) => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)), |
112 | None => builder.insert(offset, format!("{} ", missing_visibility)), | ||
113 | } | ||
110 | }) | 114 | }) |
111 | } | 115 | } |
112 | 116 | ||
@@ -196,7 +200,7 @@ mod tests { | |||
196 | fix_visibility, | 200 | fix_visibility, |
197 | r"mod foo { fn foo() {} } | 201 | r"mod foo { fn foo() {} } |
198 | fn main() { foo::foo<|>() } ", | 202 | fn main() { foo::foo<|>() } ", |
199 | r"mod foo { <|>pub(crate) fn foo() {} } | 203 | r"mod foo { $0pub(crate) fn foo() {} } |
200 | fn main() { foo::foo() } ", | 204 | fn main() { foo::foo() } ", |
201 | ); | 205 | ); |
202 | check_assist_not_applicable( | 206 | check_assist_not_applicable( |
@@ -212,7 +216,7 @@ mod tests { | |||
212 | fix_visibility, | 216 | fix_visibility, |
213 | r"mod foo { struct Foo; } | 217 | r"mod foo { struct Foo; } |
214 | fn main() { foo::Foo<|> } ", | 218 | fn main() { foo::Foo<|> } ", |
215 | r"mod foo { <|>pub(crate) struct Foo; } | 219 | r"mod foo { $0pub(crate) struct Foo; } |
216 | fn main() { foo::Foo } ", | 220 | fn main() { foo::Foo } ", |
217 | ); | 221 | ); |
218 | check_assist_not_applicable( | 222 | check_assist_not_applicable( |
@@ -224,7 +228,7 @@ mod tests { | |||
224 | fix_visibility, | 228 | fix_visibility, |
225 | r"mod foo { enum Foo; } | 229 | r"mod foo { enum Foo; } |
226 | fn main() { foo::Foo<|> } ", | 230 | fn main() { foo::Foo<|> } ", |
227 | r"mod foo { <|>pub(crate) enum Foo; } | 231 | r"mod foo { $0pub(crate) enum Foo; } |
228 | fn main() { foo::Foo } ", | 232 | fn main() { foo::Foo } ", |
229 | ); | 233 | ); |
230 | check_assist_not_applicable( | 234 | check_assist_not_applicable( |
@@ -236,7 +240,7 @@ mod tests { | |||
236 | fix_visibility, | 240 | fix_visibility, |
237 | r"mod foo { union Foo; } | 241 | r"mod foo { union Foo; } |
238 | fn main() { foo::Foo<|> } ", | 242 | fn main() { foo::Foo<|> } ", |
239 | r"mod foo { <|>pub(crate) union Foo; } | 243 | r"mod foo { $0pub(crate) union Foo; } |
240 | fn main() { foo::Foo } ", | 244 | fn main() { foo::Foo } ", |
241 | ); | 245 | ); |
242 | check_assist_not_applicable( | 246 | check_assist_not_applicable( |
@@ -258,7 +262,7 @@ mod tests { | |||
258 | //- /foo.rs | 262 | //- /foo.rs |
259 | struct Foo; | 263 | struct Foo; |
260 | ", | 264 | ", |
261 | r"<|>pub(crate) struct Foo; | 265 | r"$0pub(crate) struct Foo; |
262 | 266 | ||
263 | ", | 267 | ", |
264 | ); | 268 | ); |
@@ -270,7 +274,7 @@ mod tests { | |||
270 | fix_visibility, | 274 | fix_visibility, |
271 | r"mod foo { pub struct Foo { bar: (), } } | 275 | r"mod foo { pub struct Foo { bar: (), } } |
272 | fn main() { foo::Foo { <|>bar: () }; } ", | 276 | fn main() { foo::Foo { <|>bar: () }; } ", |
273 | r"mod foo { pub struct Foo { <|>pub(crate) bar: (), } } | 277 | r"mod foo { pub struct Foo { $0pub(crate) bar: (), } } |
274 | fn main() { foo::Foo { bar: () }; } ", | 278 | fn main() { foo::Foo { bar: () }; } ", |
275 | ); | 279 | ); |
276 | check_assist( | 280 | check_assist( |
@@ -281,7 +285,7 @@ mod tests { | |||
281 | //- /foo.rs | 285 | //- /foo.rs |
282 | pub struct Foo { bar: () } | 286 | pub struct Foo { bar: () } |
283 | ", | 287 | ", |
284 | r"pub struct Foo { <|>pub(crate) bar: () } | 288 | r"pub struct Foo { $0pub(crate) bar: () } |
285 | 289 | ||
286 | ", | 290 | ", |
287 | ); | 291 | ); |
@@ -307,7 +311,7 @@ mod tests { | |||
307 | fix_visibility, | 311 | fix_visibility, |
308 | r"mod foo { pub enum Foo { Bar { bar: () } } } | 312 | r"mod foo { pub enum Foo { Bar { bar: () } } } |
309 | fn main() { foo::Foo::Bar { <|>bar: () }; } ", | 313 | fn main() { foo::Foo::Bar { <|>bar: () }; } ", |
310 | r"mod foo { pub enum Foo { Bar { <|>pub(crate) bar: () } } } | 314 | r"mod foo { pub enum Foo { Bar { $0pub(crate) bar: () } } } |
311 | fn main() { foo::Foo::Bar { bar: () }; } ", | 315 | fn main() { foo::Foo::Bar { bar: () }; } ", |
312 | ); | 316 | ); |
313 | check_assist( | 317 | check_assist( |
@@ -318,7 +322,7 @@ mod tests { | |||
318 | //- /foo.rs | 322 | //- /foo.rs |
319 | pub enum Foo { Bar { bar: () } } | 323 | pub enum Foo { Bar { bar: () } } |
320 | ", | 324 | ", |
321 | r"pub enum Foo { Bar { <|>pub(crate) bar: () } } | 325 | r"pub enum Foo { Bar { $0pub(crate) bar: () } } |
322 | 326 | ||
323 | ", | 327 | ", |
324 | ); | 328 | ); |
@@ -346,7 +350,7 @@ mod tests { | |||
346 | fix_visibility, | 350 | fix_visibility, |
347 | r"mod foo { pub union Foo { bar: (), } } | 351 | r"mod foo { pub union Foo { bar: (), } } |
348 | fn main() { foo::Foo { <|>bar: () }; } ", | 352 | fn main() { foo::Foo { <|>bar: () }; } ", |
349 | r"mod foo { pub union Foo { <|>pub(crate) bar: (), } } | 353 | r"mod foo { pub union Foo { $0pub(crate) bar: (), } } |
350 | fn main() { foo::Foo { bar: () }; } ", | 354 | fn main() { foo::Foo { bar: () }; } ", |
351 | ); | 355 | ); |
352 | check_assist( | 356 | check_assist( |
@@ -357,7 +361,7 @@ mod tests { | |||
357 | //- /foo.rs | 361 | //- /foo.rs |
358 | pub union Foo { bar: () } | 362 | pub union Foo { bar: () } |
359 | ", | 363 | ", |
360 | r"pub union Foo { <|>pub(crate) bar: () } | 364 | r"pub union Foo { $0pub(crate) bar: () } |
361 | 365 | ||
362 | ", | 366 | ", |
363 | ); | 367 | ); |
@@ -383,7 +387,7 @@ mod tests { | |||
383 | fix_visibility, | 387 | fix_visibility, |
384 | r"mod foo { const FOO: () = (); } | 388 | r"mod foo { const FOO: () = (); } |
385 | fn main() { foo::FOO<|> } ", | 389 | fn main() { foo::FOO<|> } ", |
386 | r"mod foo { <|>pub(crate) const FOO: () = (); } | 390 | r"mod foo { $0pub(crate) const FOO: () = (); } |
387 | fn main() { foo::FOO } ", | 391 | fn main() { foo::FOO } ", |
388 | ); | 392 | ); |
389 | check_assist_not_applicable( | 393 | check_assist_not_applicable( |
@@ -399,7 +403,7 @@ mod tests { | |||
399 | fix_visibility, | 403 | fix_visibility, |
400 | r"mod foo { static FOO: () = (); } | 404 | r"mod foo { static FOO: () = (); } |
401 | fn main() { foo::FOO<|> } ", | 405 | fn main() { foo::FOO<|> } ", |
402 | r"mod foo { <|>pub(crate) static FOO: () = (); } | 406 | r"mod foo { $0pub(crate) static FOO: () = (); } |
403 | fn main() { foo::FOO } ", | 407 | fn main() { foo::FOO } ", |
404 | ); | 408 | ); |
405 | check_assist_not_applicable( | 409 | check_assist_not_applicable( |
@@ -415,7 +419,7 @@ mod tests { | |||
415 | fix_visibility, | 419 | fix_visibility, |
416 | r"mod foo { trait Foo { fn foo(&self) {} } } | 420 | r"mod foo { trait Foo { fn foo(&self) {} } } |
417 | fn main() { let x: &dyn foo::<|>Foo; } ", | 421 | fn main() { let x: &dyn foo::<|>Foo; } ", |
418 | r"mod foo { <|>pub(crate) trait Foo { fn foo(&self) {} } } | 422 | r"mod foo { $0pub(crate) trait Foo { fn foo(&self) {} } } |
419 | fn main() { let x: &dyn foo::Foo; } ", | 423 | fn main() { let x: &dyn foo::Foo; } ", |
420 | ); | 424 | ); |
421 | check_assist_not_applicable( | 425 | check_assist_not_applicable( |
@@ -431,7 +435,7 @@ mod tests { | |||
431 | fix_visibility, | 435 | fix_visibility, |
432 | r"mod foo { type Foo = (); } | 436 | r"mod foo { type Foo = (); } |
433 | fn main() { let x: foo::Foo<|>; } ", | 437 | fn main() { let x: foo::Foo<|>; } ", |
434 | r"mod foo { <|>pub(crate) type Foo = (); } | 438 | r"mod foo { $0pub(crate) type Foo = (); } |
435 | fn main() { let x: foo::Foo; } ", | 439 | fn main() { let x: foo::Foo; } ", |
436 | ); | 440 | ); |
437 | check_assist_not_applicable( | 441 | check_assist_not_applicable( |
@@ -447,7 +451,7 @@ mod tests { | |||
447 | fix_visibility, | 451 | fix_visibility, |
448 | r"mod foo { mod bar { fn bar() {} } } | 452 | r"mod foo { mod bar { fn bar() {} } } |
449 | fn main() { foo::bar<|>::bar(); } ", | 453 | fn main() { foo::bar<|>::bar(); } ", |
450 | r"mod foo { <|>pub(crate) mod bar { fn bar() {} } } | 454 | r"mod foo { $0pub(crate) mod bar { fn bar() {} } } |
451 | fn main() { foo::bar::bar(); } ", | 455 | fn main() { foo::bar::bar(); } ", |
452 | ); | 456 | ); |
453 | 457 | ||
@@ -463,7 +467,7 @@ mod tests { | |||
463 | pub fn baz() {} | 467 | pub fn baz() {} |
464 | } | 468 | } |
465 | ", | 469 | ", |
466 | r"<|>pub(crate) mod bar { | 470 | r"$0pub(crate) mod bar { |
467 | pub fn baz() {} | 471 | pub fn baz() {} |
468 | } | 472 | } |
469 | 473 | ||
@@ -493,7 +497,7 @@ mod tests { | |||
493 | pub fn baz() {} | 497 | pub fn baz() {} |
494 | } | 498 | } |
495 | ", | 499 | ", |
496 | r"<|>pub(crate) mod bar; | 500 | r"$0pub(crate) mod bar; |
497 | ", | 501 | ", |
498 | ); | 502 | ); |
499 | } | 503 | } |
@@ -510,7 +514,7 @@ mod tests { | |||
510 | mod bar { | 514 | mod bar { |
511 | pub fn baz() {} | 515 | pub fn baz() {} |
512 | }", | 516 | }", |
513 | r"<|>pub(crate) mod bar { | 517 | r"$0pub(crate) mod bar { |
514 | pub fn baz() {} | 518 | pub fn baz() {} |
515 | } | 519 | } |
516 | ", | 520 | ", |
@@ -525,7 +529,7 @@ mod tests { | |||
525 | foo::Bar<|> | 529 | foo::Bar<|> |
526 | //- /lib.rs crate:foo | 530 | //- /lib.rs crate:foo |
527 | struct Bar;", | 531 | struct Bar;", |
528 | r"<|>pub struct Bar; | 532 | r"$0pub struct Bar; |
529 | ", | 533 | ", |
530 | ) | 534 | ) |
531 | } | 535 | } |
@@ -545,7 +549,7 @@ mod tests { | |||
545 | ", | 549 | ", |
546 | r" | 550 | r" |
547 | mod foo { | 551 | mod foo { |
548 | <|>pub(crate) use bar::Baz; | 552 | $0pub(crate) use bar::Baz; |
549 | mod bar { pub(super) struct Baz; } | 553 | mod bar { pub(super) struct Baz; } |
550 | } | 554 | } |
551 | foo::Baz | 555 | foo::Baz |
diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs index d841b6f56..3e6654c17 100644 --- a/crates/ra_assists/src/tests/generated.rs +++ b/crates/ra_assists/src/tests/generated.rs | |||
@@ -358,7 +358,7 @@ fn main() { | |||
358 | "#####, | 358 | "#####, |
359 | r#####" | 359 | r#####" |
360 | mod m { | 360 | mod m { |
361 | pub(crate) fn frobnicate() {} | 361 | $0pub(crate) fn frobnicate() {} |
362 | } | 362 | } |
363 | fn main() { | 363 | fn main() { |
364 | m::frobnicate() {} | 364 | m::frobnicate() {} |