diff options
Diffstat (limited to 'crates/ra_assists/src/assists/change_visibility.rs')
-rw-r--r-- | crates/ra_assists/src/assists/change_visibility.rs | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/crates/ra_assists/src/assists/change_visibility.rs b/crates/ra_assists/src/assists/change_visibility.rs new file mode 100644 index 000000000..60c74debc --- /dev/null +++ b/crates/ra_assists/src/assists/change_visibility.rs | |||
@@ -0,0 +1,159 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use ra_syntax::{ | ||
3 | ast::{self, NameOwner, VisibilityOwner}, | ||
4 | AstNode, | ||
5 | SyntaxKind::{ | ||
6 | ATTR, COMMENT, ENUM_DEF, FN_DEF, IDENT, MODULE, STRUCT_DEF, TRAIT_DEF, VISIBILITY, | ||
7 | WHITESPACE, | ||
8 | }, | ||
9 | SyntaxNode, TextUnit, T, | ||
10 | }; | ||
11 | |||
12 | use crate::{Assist, AssistCtx, AssistId}; | ||
13 | |||
14 | pub(crate) fn change_visibility(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | ||
15 | if let Some(vis) = ctx.node_at_offset::<ast::Visibility>() { | ||
16 | return change_vis(ctx, vis); | ||
17 | } | ||
18 | add_vis(ctx) | ||
19 | } | ||
20 | |||
21 | fn add_vis(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | ||
22 | let item_keyword = ctx.token_at_offset().find(|leaf| match leaf.kind() { | ||
23 | T![fn] | T![mod] | T![struct] | T![enum] | T![trait] => true, | ||
24 | _ => false, | ||
25 | }); | ||
26 | |||
27 | let (offset, target) = if let Some(keyword) = item_keyword { | ||
28 | let parent = keyword.parent(); | ||
29 | let def_kws = vec![FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF]; | ||
30 | // Parent is not a definition, can't add visibility | ||
31 | if !def_kws.iter().any(|&def_kw| def_kw == parent.kind()) { | ||
32 | return None; | ||
33 | } | ||
34 | // Already have visibility, do nothing | ||
35 | if parent.children().any(|child| child.kind() == VISIBILITY) { | ||
36 | return None; | ||
37 | } | ||
38 | (vis_offset(&parent), keyword.text_range()) | ||
39 | } else { | ||
40 | let ident = ctx.token_at_offset().find(|leaf| leaf.kind() == IDENT)?; | ||
41 | let field = ident.parent().ancestors().find_map(ast::RecordFieldDef::cast)?; | ||
42 | if field.name()?.syntax().text_range() != ident.text_range() && field.visibility().is_some() | ||
43 | { | ||
44 | return None; | ||
45 | } | ||
46 | (vis_offset(field.syntax()), ident.text_range()) | ||
47 | }; | ||
48 | |||
49 | ctx.add_action(AssistId("change_visibility"), "make pub(crate)", |edit| { | ||
50 | edit.target(target); | ||
51 | edit.insert(offset, "pub(crate) "); | ||
52 | edit.set_cursor(offset); | ||
53 | }); | ||
54 | |||
55 | ctx.build() | ||
56 | } | ||
57 | |||
58 | fn vis_offset(node: &SyntaxNode) -> TextUnit { | ||
59 | node.children_with_tokens() | ||
60 | .skip_while(|it| match it.kind() { | ||
61 | WHITESPACE | COMMENT | ATTR => true, | ||
62 | _ => false, | ||
63 | }) | ||
64 | .next() | ||
65 | .map(|it| it.text_range().start()) | ||
66 | .unwrap_or_else(|| node.text_range().start()) | ||
67 | } | ||
68 | |||
69 | fn change_vis(mut ctx: AssistCtx<impl HirDatabase>, vis: ast::Visibility) -> Option<Assist> { | ||
70 | if vis.syntax().text() == "pub" { | ||
71 | ctx.add_action(AssistId("change_visibility"), "change to pub(crate)", |edit| { | ||
72 | edit.target(vis.syntax().text_range()); | ||
73 | edit.replace(vis.syntax().text_range(), "pub(crate)"); | ||
74 | edit.set_cursor(vis.syntax().text_range().start()) | ||
75 | }); | ||
76 | |||
77 | return ctx.build(); | ||
78 | } | ||
79 | if vis.syntax().text() == "pub(crate)" { | ||
80 | ctx.add_action(AssistId("change_visibility"), "change to pub", |edit| { | ||
81 | edit.target(vis.syntax().text_range()); | ||
82 | edit.replace(vis.syntax().text_range(), "pub"); | ||
83 | edit.set_cursor(vis.syntax().text_range().start()); | ||
84 | }); | ||
85 | |||
86 | return ctx.build(); | ||
87 | } | ||
88 | None | ||
89 | } | ||
90 | |||
91 | #[cfg(test)] | ||
92 | mod tests { | ||
93 | use super::*; | ||
94 | use crate::helpers::{check_assist, check_assist_target}; | ||
95 | |||
96 | #[test] | ||
97 | fn change_visibility_adds_pub_crate_to_items() { | ||
98 | check_assist(change_visibility, "<|>fn foo() {}", "<|>pub(crate) fn foo() {}"); | ||
99 | check_assist(change_visibility, "f<|>n foo() {}", "<|>pub(crate) fn foo() {}"); | ||
100 | check_assist(change_visibility, "<|>struct Foo {}", "<|>pub(crate) struct Foo {}"); | ||
101 | check_assist(change_visibility, "<|>mod foo {}", "<|>pub(crate) mod foo {}"); | ||
102 | check_assist(change_visibility, "<|>trait Foo {}", "<|>pub(crate) trait Foo {}"); | ||
103 | check_assist(change_visibility, "m<|>od {}", "<|>pub(crate) mod {}"); | ||
104 | check_assist( | ||
105 | change_visibility, | ||
106 | "unsafe f<|>n foo() {}", | ||
107 | "<|>pub(crate) unsafe fn foo() {}", | ||
108 | ); | ||
109 | } | ||
110 | |||
111 | #[test] | ||
112 | fn change_visibility_works_with_struct_fields() { | ||
113 | check_assist( | ||
114 | change_visibility, | ||
115 | "struct S { <|>field: u32 }", | ||
116 | "struct S { <|>pub(crate) field: u32 }", | ||
117 | ) | ||
118 | } | ||
119 | |||
120 | #[test] | ||
121 | fn change_visibility_pub_to_pub_crate() { | ||
122 | check_assist(change_visibility, "<|>pub fn foo() {}", "<|>pub(crate) fn foo() {}") | ||
123 | } | ||
124 | |||
125 | #[test] | ||
126 | fn change_visibility_pub_crate_to_pub() { | ||
127 | check_assist(change_visibility, "<|>pub(crate) fn foo() {}", "<|>pub fn foo() {}") | ||
128 | } | ||
129 | |||
130 | #[test] | ||
131 | fn change_visibility_handles_comment_attrs() { | ||
132 | check_assist( | ||
133 | change_visibility, | ||
134 | " | ||
135 | /// docs | ||
136 | |||
137 | // comments | ||
138 | |||
139 | #[derive(Debug)] | ||
140 | <|>struct Foo; | ||
141 | ", | ||
142 | " | ||
143 | /// docs | ||
144 | |||
145 | // comments | ||
146 | |||
147 | #[derive(Debug)] | ||
148 | <|>pub(crate) struct Foo; | ||
149 | ", | ||
150 | ) | ||
151 | } | ||
152 | |||
153 | #[test] | ||
154 | fn change_visibility_target() { | ||
155 | check_assist_target(change_visibility, "<|>fn foo() {}", "fn"); | ||
156 | check_assist_target(change_visibility, "pub(crate)<|> fn foo() {}", "pub(crate)"); | ||
157 | check_assist_target(change_visibility, "struct S { <|>field: u32 }", "field"); | ||
158 | } | ||
159 | } | ||