Skip to content

Commit ba3a58d

Browse files
committed
Snippetify fix_visibility
1 parent c446fd7 commit ba3a58d

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
lines changed

crates/ra_assists/src/handlers/fix_visibility.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{AssistContext, AssistId, Assists};
2525
// ->
2626
// ```
2727
// mod m {
28-
// pub(crate) fn frobnicate() {}
28+
// $0pub(crate) fn frobnicate() {}
2929
// }
3030
// fn main() {
3131
// m::frobnicate() {}
@@ -62,10 +62,12 @@ fn add_vis_to_referenced_module_def(acc: &mut Assists, ctx: &AssistContext) -> O
6262
Some(name) => format!("Change visibility of {} to {}", name, missing_visibility),
6363
};
6464

65-
acc.add(AssistId("fix_visibility"), assist_label, target, |edit| {
66-
edit.set_file(target_file);
67-
edit.insert(offset, format!("{} ", missing_visibility));
68-
edit.set_cursor(offset);
65+
acc.add(AssistId("fix_visibility"), assist_label, target, |builder| {
66+
builder.set_file(target_file);
67+
match ctx.config.snippet_cap {
68+
Some(cap) => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)),
69+
None => builder.insert(offset, format!("{} ", missing_visibility)),
70+
}
6971
})
7072
}
7173

@@ -103,10 +105,12 @@ fn add_vis_to_referenced_record_field(acc: &mut Assists, ctx: &AssistContext) ->
103105
let assist_label =
104106
format!("Change visibility of {}.{} to {}", parent_name, target_name, missing_visibility);
105107

106-
acc.add(AssistId("fix_visibility"), assist_label, target, |edit| {
107-
edit.set_file(target_file);
108-
edit.insert(offset, format!("{} ", missing_visibility));
109-
edit.set_cursor(offset)
108+
acc.add(AssistId("fix_visibility"), assist_label, target, |builder| {
109+
builder.set_file(target_file);
110+
match ctx.config.snippet_cap {
111+
Some(cap) => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)),
112+
None => builder.insert(offset, format!("{} ", missing_visibility)),
113+
}
110114
})
111115
}
112116

@@ -196,7 +200,7 @@ mod tests {
196200
fix_visibility,
197201
r"mod foo { fn foo() {} }
198202
fn main() { foo::foo<|>() } ",
199-
r"mod foo { <|>pub(crate) fn foo() {} }
203+
r"mod foo { $0pub(crate) fn foo() {} }
200204
fn main() { foo::foo() } ",
201205
);
202206
check_assist_not_applicable(
@@ -212,7 +216,7 @@ mod tests {
212216
fix_visibility,
213217
r"mod foo { struct Foo; }
214218
fn main() { foo::Foo<|> } ",
215-
r"mod foo { <|>pub(crate) struct Foo; }
219+
r"mod foo { $0pub(crate) struct Foo; }
216220
fn main() { foo::Foo } ",
217221
);
218222
check_assist_not_applicable(
@@ -224,7 +228,7 @@ mod tests {
224228
fix_visibility,
225229
r"mod foo { enum Foo; }
226230
fn main() { foo::Foo<|> } ",
227-
r"mod foo { <|>pub(crate) enum Foo; }
231+
r"mod foo { $0pub(crate) enum Foo; }
228232
fn main() { foo::Foo } ",
229233
);
230234
check_assist_not_applicable(
@@ -236,7 +240,7 @@ mod tests {
236240
fix_visibility,
237241
r"mod foo { union Foo; }
238242
fn main() { foo::Foo<|> } ",
239-
r"mod foo { <|>pub(crate) union Foo; }
243+
r"mod foo { $0pub(crate) union Foo; }
240244
fn main() { foo::Foo } ",
241245
);
242246
check_assist_not_applicable(
@@ -258,7 +262,7 @@ mod tests {
258262
//- /foo.rs
259263
struct Foo;
260264
",
261-
r"<|>pub(crate) struct Foo;
265+
r"$0pub(crate) struct Foo;
262266
263267
",
264268
);
@@ -270,7 +274,7 @@ mod tests {
270274
fix_visibility,
271275
r"mod foo { pub struct Foo { bar: (), } }
272276
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: (), } }
274278
fn main() { foo::Foo { bar: () }; } ",
275279
);
276280
check_assist(
@@ -281,7 +285,7 @@ mod tests {
281285
//- /foo.rs
282286
pub struct Foo { bar: () }
283287
",
284-
r"pub struct Foo { <|>pub(crate) bar: () }
288+
r"pub struct Foo { $0pub(crate) bar: () }
285289
286290
",
287291
);
@@ -307,7 +311,7 @@ mod tests {
307311
fix_visibility,
308312
r"mod foo { pub enum Foo { Bar { bar: () } } }
309313
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: () } } }
311315
fn main() { foo::Foo::Bar { bar: () }; } ",
312316
);
313317
check_assist(
@@ -318,7 +322,7 @@ mod tests {
318322
//- /foo.rs
319323
pub enum Foo { Bar { bar: () } }
320324
",
321-
r"pub enum Foo { Bar { <|>pub(crate) bar: () } }
325+
r"pub enum Foo { Bar { $0pub(crate) bar: () } }
322326
323327
",
324328
);
@@ -346,7 +350,7 @@ mod tests {
346350
fix_visibility,
347351
r"mod foo { pub union Foo { bar: (), } }
348352
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: (), } }
350354
fn main() { foo::Foo { bar: () }; } ",
351355
);
352356
check_assist(
@@ -357,7 +361,7 @@ mod tests {
357361
//- /foo.rs
358362
pub union Foo { bar: () }
359363
",
360-
r"pub union Foo { <|>pub(crate) bar: () }
364+
r"pub union Foo { $0pub(crate) bar: () }
361365
362366
",
363367
);
@@ -383,7 +387,7 @@ mod tests {
383387
fix_visibility,
384388
r"mod foo { const FOO: () = (); }
385389
fn main() { foo::FOO<|> } ",
386-
r"mod foo { <|>pub(crate) const FOO: () = (); }
390+
r"mod foo { $0pub(crate) const FOO: () = (); }
387391
fn main() { foo::FOO } ",
388392
);
389393
check_assist_not_applicable(
@@ -399,7 +403,7 @@ mod tests {
399403
fix_visibility,
400404
r"mod foo { static FOO: () = (); }
401405
fn main() { foo::FOO<|> } ",
402-
r"mod foo { <|>pub(crate) static FOO: () = (); }
406+
r"mod foo { $0pub(crate) static FOO: () = (); }
403407
fn main() { foo::FOO } ",
404408
);
405409
check_assist_not_applicable(
@@ -415,7 +419,7 @@ mod tests {
415419
fix_visibility,
416420
r"mod foo { trait Foo { fn foo(&self) {} } }
417421
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) {} } }
419423
fn main() { let x: &dyn foo::Foo; } ",
420424
);
421425
check_assist_not_applicable(
@@ -431,7 +435,7 @@ mod tests {
431435
fix_visibility,
432436
r"mod foo { type Foo = (); }
433437
fn main() { let x: foo::Foo<|>; } ",
434-
r"mod foo { <|>pub(crate) type Foo = (); }
438+
r"mod foo { $0pub(crate) type Foo = (); }
435439
fn main() { let x: foo::Foo; } ",
436440
);
437441
check_assist_not_applicable(
@@ -447,7 +451,7 @@ mod tests {
447451
fix_visibility,
448452
r"mod foo { mod bar { fn bar() {} } }
449453
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() {} } }
451455
fn main() { foo::bar::bar(); } ",
452456
);
453457

@@ -463,7 +467,7 @@ mod tests {
463467
pub fn baz() {}
464468
}
465469
",
466-
r"<|>pub(crate) mod bar {
470+
r"$0pub(crate) mod bar {
467471
pub fn baz() {}
468472
}
469473
@@ -493,7 +497,7 @@ mod tests {
493497
pub fn baz() {}
494498
}
495499
",
496-
r"<|>pub(crate) mod bar;
500+
r"$0pub(crate) mod bar;
497501
",
498502
);
499503
}
@@ -510,7 +514,7 @@ mod tests {
510514
mod bar {
511515
pub fn baz() {}
512516
}",
513-
r"<|>pub(crate) mod bar {
517+
r"$0pub(crate) mod bar {
514518
pub fn baz() {}
515519
}
516520
",
@@ -525,7 +529,7 @@ mod tests {
525529
foo::Bar<|>
526530
//- /lib.rs crate:foo
527531
struct Bar;",
528-
r"<|>pub struct Bar;
532+
r"$0pub struct Bar;
529533
",
530534
)
531535
}
@@ -545,7 +549,7 @@ mod tests {
545549
",
546550
r"
547551
mod foo {
548-
<|>pub(crate) use bar::Baz;
552+
$0pub(crate) use bar::Baz;
549553
mod bar { pub(super) struct Baz; }
550554
}
551555
foo::Baz

crates/ra_assists/src/tests/generated.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ fn main() {
358358
"#####,
359359
r#####"
360360
mod m {
361-
pub(crate) fn frobnicate() {}
361+
$0pub(crate) fn frobnicate() {}
362362
}
363363
fn main() {
364364
m::frobnicate() {}

docs/user/assists.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ fn main() {
346346

347347
// AFTER
348348
mod m {
349-
pub(crate) fn frobnicate() {}
349+
$0pub(crate) fn frobnicate() {}
350350
}
351351
fn main() {
352352
m::frobnicate() {}

0 commit comments

Comments
 (0)