diff options
author | Aleksey Kladov <[email protected]> | 2019-01-05 12:28:07 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-05 12:28:07 +0000 |
commit | 79fd6b5c881d93c427abba6d9376965837decb24 (patch) | |
tree | 9d97aea78bc359e55217e66bd77f1aaa14cda816 /crates/ra_editor/src/assists/change_visibility.rs | |
parent | 9b5b13dfcfe32bccbf3593eee26bf88a5fd60413 (diff) |
change visibility can change pub to pub(crate)
Diffstat (limited to 'crates/ra_editor/src/assists/change_visibility.rs')
-rw-r--r-- | crates/ra_editor/src/assists/change_visibility.rs | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/crates/ra_editor/src/assists/change_visibility.rs b/crates/ra_editor/src/assists/change_visibility.rs index ac75f635e..6c8466394 100644 --- a/crates/ra_editor/src/assists/change_visibility.rs +++ b/crates/ra_editor/src/assists/change_visibility.rs | |||
@@ -7,10 +7,19 @@ use ra_syntax::{ | |||
7 | use crate::assists::{AssistCtx, Assist}; | 7 | use crate::assists::{AssistCtx, Assist}; |
8 | 8 | ||
9 | pub fn change_visibility(ctx: AssistCtx) -> Option<Assist> { | 9 | pub fn change_visibility(ctx: AssistCtx) -> Option<Assist> { |
10 | let offset = if let Some(keyword) = ctx.leaf_at_offset().find(|leaf| match leaf.kind() { | 10 | if let Some(vis) = ctx.node_at_offset::<ast::Visibility>() { |
11 | return change_vis(ctx, vis); | ||
12 | } | ||
13 | add_vis(ctx) | ||
14 | } | ||
15 | |||
16 | fn add_vis(ctx: AssistCtx) -> Option<Assist> { | ||
17 | let item_keyword = ctx.leaf_at_offset().find(|leaf| match leaf.kind() { | ||
11 | FN_KW | MOD_KW | STRUCT_KW | ENUM_KW | TRAIT_KW => true, | 18 | FN_KW | MOD_KW | STRUCT_KW | ENUM_KW | TRAIT_KW => true, |
12 | _ => false, | 19 | _ => false, |
13 | }) { | 20 | }); |
21 | |||
22 | let offset = if let Some(keyword) = item_keyword { | ||
14 | let parent = keyword.parent()?; | 23 | let parent = keyword.parent()?; |
15 | let def_kws = vec![FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF]; | 24 | let def_kws = vec![FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF]; |
16 | // Parent is not a definition, can't add visibility | 25 | // Parent is not a definition, can't add visibility |
@@ -37,6 +46,16 @@ pub fn change_visibility(ctx: AssistCtx) -> Option<Assist> { | |||
37 | }) | 46 | }) |
38 | } | 47 | } |
39 | 48 | ||
49 | fn change_vis(ctx: AssistCtx, vis: ast::Visibility) -> Option<Assist> { | ||
50 | if vis.syntax().text() != "pub" { | ||
51 | return None; | ||
52 | } | ||
53 | ctx.build("chage to pub(crate)", |edit| { | ||
54 | edit.replace(vis.syntax().range(), "pub(crate)"); | ||
55 | edit.set_cursor(vis.syntax().range().start()); | ||
56 | }) | ||
57 | } | ||
58 | |||
40 | #[cfg(test)] | 59 | #[cfg(test)] |
41 | mod tests { | 60 | mod tests { |
42 | use super::*; | 61 | use super::*; |
@@ -85,4 +104,13 @@ mod tests { | |||
85 | "struct S { <|>pub(crate) field: u32 }", | 104 | "struct S { <|>pub(crate) field: u32 }", |
86 | ) | 105 | ) |
87 | } | 106 | } |
107 | |||
108 | #[test] | ||
109 | fn change_visibility_pub_to_pub_crate() { | ||
110 | check_assist( | ||
111 | change_visibility, | ||
112 | "<|>pub fn foo() {}", | ||
113 | "<|>pub(crate) fn foo() {}", | ||
114 | ) | ||
115 | } | ||
88 | } | 116 | } |