aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/change_visibility.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/handlers/change_visibility.rs')
-rw-r--r--crates/ra_assists/src/handlers/change_visibility.rs50
1 files changed, 26 insertions, 24 deletions
diff --git a/crates/ra_assists/src/handlers/change_visibility.rs b/crates/ra_assists/src/handlers/change_visibility.rs
index e631766ef..c21d75be0 100644
--- a/crates/ra_assists/src/handlers/change_visibility.rs
+++ b/crates/ra_assists/src/handlers/change_visibility.rs
@@ -7,7 +7,7 @@ use ra_syntax::{
7 }, 7 },
8 SyntaxNode, TextSize, T, 8 SyntaxNode, TextSize, T,
9}; 9};
10use test_utils::tested_by; 10use test_utils::mark;
11 11
12use crate::{AssistContext, AssistId, Assists}; 12use crate::{AssistContext, AssistId, Assists};
13 13
@@ -50,7 +50,7 @@ fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
50 } else if let Some(field_name) = ctx.find_node_at_offset::<ast::Name>() { 50 } else if let Some(field_name) = ctx.find_node_at_offset::<ast::Name>() {
51 let field = field_name.syntax().ancestors().find_map(ast::RecordFieldDef::cast)?; 51 let field = field_name.syntax().ancestors().find_map(ast::RecordFieldDef::cast)?;
52 if field.name()? != field_name { 52 if field.name()? != field_name {
53 tested_by!(change_visibility_field_false_positive); 53 mark::hit!(change_visibility_field_false_positive);
54 return None; 54 return None;
55 } 55 }
56 if field.visibility().is_some() { 56 if field.visibility().is_some() {
@@ -68,7 +68,6 @@ fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
68 68
69 acc.add(AssistId("change_visibility"), "Change visibility to pub(crate)", target, |edit| { 69 acc.add(AssistId("change_visibility"), "Change visibility to pub(crate)", target, |edit| {
70 edit.insert(offset, "pub(crate) "); 70 edit.insert(offset, "pub(crate) ");
71 edit.set_cursor(offset);
72 }) 71 })
73} 72}
74 73
@@ -92,7 +91,6 @@ fn change_vis(acc: &mut Assists, vis: ast::Visibility) -> Option<()> {
92 target, 91 target,
93 |edit| { 92 |edit| {
94 edit.replace(vis.syntax().text_range(), "pub(crate)"); 93 edit.replace(vis.syntax().text_range(), "pub(crate)");
95 edit.set_cursor(vis.syntax().text_range().start())
96 }, 94 },
97 ); 95 );
98 } 96 }
@@ -104,7 +102,6 @@ fn change_vis(acc: &mut Assists, vis: ast::Visibility) -> Option<()> {
104 target, 102 target,
105 |edit| { 103 |edit| {
106 edit.replace(vis.syntax().text_range(), "pub"); 104 edit.replace(vis.syntax().text_range(), "pub");
107 edit.set_cursor(vis.syntax().text_range().start());
108 }, 105 },
109 ); 106 );
110 } 107 }
@@ -113,7 +110,7 @@ fn change_vis(acc: &mut Assists, vis: ast::Visibility) -> Option<()> {
113 110
114#[cfg(test)] 111#[cfg(test)]
115mod tests { 112mod tests {
116 use test_utils::covers; 113 use test_utils::mark;
117 114
118 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; 115 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
119 116
@@ -121,17 +118,13 @@ mod tests {
121 118
122 #[test] 119 #[test]
123 fn change_visibility_adds_pub_crate_to_items() { 120 fn change_visibility_adds_pub_crate_to_items() {
124 check_assist(change_visibility, "<|>fn foo() {}", "<|>pub(crate) fn foo() {}"); 121 check_assist(change_visibility, "<|>fn foo() {}", "pub(crate) fn foo() {}");
125 check_assist(change_visibility, "f<|>n foo() {}", "<|>pub(crate) fn foo() {}"); 122 check_assist(change_visibility, "f<|>n foo() {}", "pub(crate) fn foo() {}");
126 check_assist(change_visibility, "<|>struct Foo {}", "<|>pub(crate) struct Foo {}"); 123 check_assist(change_visibility, "<|>struct Foo {}", "pub(crate) struct Foo {}");
127 check_assist(change_visibility, "<|>mod foo {}", "<|>pub(crate) mod foo {}"); 124 check_assist(change_visibility, "<|>mod foo {}", "pub(crate) mod foo {}");
128 check_assist(change_visibility, "<|>trait Foo {}", "<|>pub(crate) trait Foo {}"); 125 check_assist(change_visibility, "<|>trait Foo {}", "pub(crate) trait Foo {}");
129 check_assist(change_visibility, "m<|>od {}", "<|>pub(crate) mod {}"); 126 check_assist(change_visibility, "m<|>od {}", "pub(crate) mod {}");
130 check_assist( 127 check_assist(change_visibility, "unsafe f<|>n foo() {}", "pub(crate) unsafe fn foo() {}");
131 change_visibility,
132 "unsafe f<|>n foo() {}",
133 "<|>pub(crate) unsafe fn foo() {}",
134 );
135 } 128 }
136 129
137 #[test] 130 #[test]
@@ -139,14 +132,14 @@ mod tests {
139 check_assist( 132 check_assist(
140 change_visibility, 133 change_visibility,
141 r"struct S { <|>field: u32 }", 134 r"struct S { <|>field: u32 }",
142 r"struct S { <|>pub(crate) field: u32 }", 135 r"struct S { pub(crate) field: u32 }",
143 ); 136 );
144 check_assist(change_visibility, r"struct S ( <|>u32 )", r"struct S ( <|>pub(crate) u32 )"); 137 check_assist(change_visibility, r"struct S ( <|>u32 )", r"struct S ( pub(crate) u32 )");
145 } 138 }
146 139
147 #[test] 140 #[test]
148 fn change_visibility_field_false_positive() { 141 fn change_visibility_field_false_positive() {
149 covers!(change_visibility_field_false_positive); 142 mark::check!(change_visibility_field_false_positive);
150 check_assist_not_applicable( 143 check_assist_not_applicable(
151 change_visibility, 144 change_visibility,
152 r"struct S { field: [(); { let <|>x = ();}] }", 145 r"struct S { field: [(); { let <|>x = ();}] }",
@@ -155,17 +148,17 @@ mod tests {
155 148
156 #[test] 149 #[test]
157 fn change_visibility_pub_to_pub_crate() { 150 fn change_visibility_pub_to_pub_crate() {
158 check_assist(change_visibility, "<|>pub fn foo() {}", "<|>pub(crate) fn foo() {}") 151 check_assist(change_visibility, "<|>pub fn foo() {}", "pub(crate) fn foo() {}")
159 } 152 }
160 153
161 #[test] 154 #[test]
162 fn change_visibility_pub_crate_to_pub() { 155 fn change_visibility_pub_crate_to_pub() {
163 check_assist(change_visibility, "<|>pub(crate) fn foo() {}", "<|>pub fn foo() {}") 156 check_assist(change_visibility, "<|>pub(crate) fn foo() {}", "pub fn foo() {}")
164 } 157 }
165 158
166 #[test] 159 #[test]
167 fn change_visibility_const() { 160 fn change_visibility_const() {
168 check_assist(change_visibility, "<|>const FOO = 3u8;", "<|>pub(crate) const FOO = 3u8;"); 161 check_assist(change_visibility, "<|>const FOO = 3u8;", "pub(crate) const FOO = 3u8;");
169 } 162 }
170 163
171 #[test] 164 #[test]
@@ -186,12 +179,21 @@ mod tests {
186 // comments 179 // comments
187 180
188 #[derive(Debug)] 181 #[derive(Debug)]
189 <|>pub(crate) struct Foo; 182 pub(crate) struct Foo;
190 ", 183 ",
191 ) 184 )
192 } 185 }
193 186
194 #[test] 187 #[test]
188 fn not_applicable_for_enum_variants() {
189 check_assist_not_applicable(
190 change_visibility,
191 r"mod foo { pub enum Foo {Foo1} }
192 fn main() { foo::Foo::Foo1<|> } ",
193 );
194 }
195
196 #[test]
195 fn change_visibility_target() { 197 fn change_visibility_target() {
196 check_assist_target(change_visibility, "<|>fn foo() {}", "fn"); 198 check_assist_target(change_visibility, "<|>fn foo() {}", "fn");
197 check_assist_target(change_visibility, "pub(crate)<|> fn foo() {}", "pub(crate)"); 199 check_assist_target(change_visibility, "pub(crate)<|> fn foo() {}", "pub(crate)");