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