aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/change_visibility.rs
diff options
context:
space:
mode:
authorZac Pullar-Strecker <[email protected]>2020-07-31 03:12:44 +0100
committerZac Pullar-Strecker <[email protected]>2020-07-31 03:12:44 +0100
commitf05d7b41a719d848844b054a16477b29d0f063c6 (patch)
tree0a8a0946e8aef2ce64d4c13d0035ba41cce2daf3 /crates/ra_assists/src/handlers/change_visibility.rs
parent73ff610e41959e3e7c78a2b4b25b086883132956 (diff)
parent6b7cb8b5ab539fc4333ce34bc29bf77c976f232a (diff)
Merge remote-tracking branch 'upstream/master' into 503-hover-doc-links
Hasn't fixed tests yet.
Diffstat (limited to 'crates/ra_assists/src/handlers/change_visibility.rs')
-rw-r--r--crates/ra_assists/src/handlers/change_visibility.rs35
1 files changed, 24 insertions, 11 deletions
diff --git a/crates/ra_assists/src/handlers/change_visibility.rs b/crates/ra_assists/src/handlers/change_visibility.rs
index 157c7b665..724daa93f 100644
--- a/crates/ra_assists/src/handlers/change_visibility.rs
+++ b/crates/ra_assists/src/handlers/change_visibility.rs
@@ -1,12 +1,12 @@
1use ra_syntax::{ 1use ra_syntax::{
2 ast::{self, NameOwner, VisibilityOwner}, 2 ast::{self, NameOwner, VisibilityOwner},
3 AstNode, 3 AstNode,
4 SyntaxKind::{CONST_DEF, ENUM_DEF, FN_DEF, MODULE, STRUCT_DEF, TRAIT_DEF, VISIBILITY}, 4 SyntaxKind::{CONST, ENUM, FN, MODULE, STATIC, STRUCT, TRAIT, VISIBILITY},
5 T, 5 T,
6}; 6};
7use test_utils::mark; 7use test_utils::mark;
8 8
9use crate::{utils::vis_offset, AssistContext, AssistId, Assists}; 9use crate::{utils::vis_offset, AssistContext, AssistId, AssistKind, Assists};
10 10
11// Assist: change_visibility 11// Assist: change_visibility
12// 12//
@@ -28,12 +28,15 @@ pub(crate) fn change_visibility(acc: &mut Assists, ctx: &AssistContext) -> Optio
28 28
29fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 29fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
30 let item_keyword = ctx.token_at_offset().find(|leaf| { 30 let item_keyword = ctx.token_at_offset().find(|leaf| {
31 matches!(leaf.kind(), T![const] | T![fn] | T![mod] | T![struct] | T![enum] | T![trait]) 31 matches!(
32 leaf.kind(),
33 T![const] | T![static] | T![fn] | T![mod] | T![struct] | T![enum] | T![trait]
34 )
32 }); 35 });
33 36
34 let (offset, target) = if let Some(keyword) = item_keyword { 37 let (offset, target) = if let Some(keyword) = item_keyword {
35 let parent = keyword.parent(); 38 let parent = keyword.parent();
36 let def_kws = vec![CONST_DEF, FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF]; 39 let def_kws = vec![CONST, STATIC, FN, MODULE, STRUCT, ENUM, TRAIT];
37 // Parent is not a definition, can't add visibility 40 // Parent is not a definition, can't add visibility
38 if !def_kws.iter().any(|&def_kw| def_kw == parent.kind()) { 41 if !def_kws.iter().any(|&def_kw| def_kw == parent.kind()) {
39 return None; 42 return None;
@@ -44,7 +47,7 @@ fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
44 } 47 }
45 (vis_offset(&parent), keyword.text_range()) 48 (vis_offset(&parent), keyword.text_range())
46 } else if let Some(field_name) = ctx.find_node_at_offset::<ast::Name>() { 49 } else if let Some(field_name) = ctx.find_node_at_offset::<ast::Name>() {
47 let field = field_name.syntax().ancestors().find_map(ast::RecordFieldDef::cast)?; 50 let field = field_name.syntax().ancestors().find_map(ast::RecordField::cast)?;
48 if field.name()? != field_name { 51 if field.name()? != field_name {
49 mark::hit!(change_visibility_field_false_positive); 52 mark::hit!(change_visibility_field_false_positive);
50 return None; 53 return None;
@@ -53,7 +56,7 @@ fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
53 return None; 56 return None;
54 } 57 }
55 (vis_offset(field.syntax()), field_name.syntax().text_range()) 58 (vis_offset(field.syntax()), field_name.syntax().text_range())
56 } else if let Some(field) = ctx.find_node_at_offset::<ast::TupleFieldDef>() { 59 } else if let Some(field) = ctx.find_node_at_offset::<ast::TupleField>() {
57 if field.visibility().is_some() { 60 if field.visibility().is_some() {
58 return None; 61 return None;
59 } 62 }
@@ -62,16 +65,21 @@ fn add_vis(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
62 return None; 65 return None;
63 }; 66 };
64 67
65 acc.add(AssistId("change_visibility"), "Change visibility to pub(crate)", target, |edit| { 68 acc.add(
66 edit.insert(offset, "pub(crate) "); 69 AssistId("change_visibility", AssistKind::RefactorRewrite),
67 }) 70 "Change visibility to pub(crate)",
71 target,
72 |edit| {
73 edit.insert(offset, "pub(crate) ");
74 },
75 )
68} 76}
69 77
70fn change_vis(acc: &mut Assists, vis: ast::Visibility) -> Option<()> { 78fn change_vis(acc: &mut Assists, vis: ast::Visibility) -> Option<()> {
71 if vis.syntax().text() == "pub" { 79 if vis.syntax().text() == "pub" {
72 let target = vis.syntax().text_range(); 80 let target = vis.syntax().text_range();
73 return acc.add( 81 return acc.add(
74 AssistId("change_visibility"), 82 AssistId("change_visibility", AssistKind::RefactorRewrite),
75 "Change Visibility to pub(crate)", 83 "Change Visibility to pub(crate)",
76 target, 84 target,
77 |edit| { 85 |edit| {
@@ -82,7 +90,7 @@ fn change_vis(acc: &mut Assists, vis: ast::Visibility) -> Option<()> {
82 if vis.syntax().text() == "pub(crate)" { 90 if vis.syntax().text() == "pub(crate)" {
83 let target = vis.syntax().text_range(); 91 let target = vis.syntax().text_range();
84 return acc.add( 92 return acc.add(
85 AssistId("change_visibility"), 93 AssistId("change_visibility", AssistKind::RefactorRewrite),
86 "Change visibility to pub", 94 "Change visibility to pub",
87 target, 95 target,
88 |edit| { 96 |edit| {
@@ -147,6 +155,11 @@ mod tests {
147 } 155 }
148 156
149 #[test] 157 #[test]
158 fn change_visibility_static() {
159 check_assist(change_visibility, "<|>static FOO = 3u8;", "pub(crate) static FOO = 3u8;");
160 }
161
162 #[test]
150 fn change_visibility_handles_comment_attrs() { 163 fn change_visibility_handles_comment_attrs() {
151 check_assist( 164 check_assist(
152 change_visibility, 165 change_visibility,