aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/change_visibility.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/change_visibility.rs')
-rw-r--r--crates/ra_assists/src/change_visibility.rs22
1 files changed, 16 insertions, 6 deletions
diff --git a/crates/ra_assists/src/change_visibility.rs b/crates/ra_assists/src/change_visibility.rs
index fa5f231c8..6d9a4eec2 100644
--- a/crates/ra_assists/src/change_visibility.rs
+++ b/crates/ra_assists/src/change_visibility.rs
@@ -20,7 +20,7 @@ fn add_vis(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
20 _ => false, 20 _ => false,
21 }); 21 });
22 22
23 let offset = if let Some(keyword) = item_keyword { 23 let (offset, target) = if let Some(keyword) = item_keyword {
24 let parent = keyword.parent()?; 24 let parent = keyword.parent()?;
25 let def_kws = vec![FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF]; 25 let def_kws = vec![FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF];
26 // Parent is not a definition, can't add visibility 26 // Parent is not a definition, can't add visibility
@@ -31,17 +31,18 @@ fn add_vis(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
31 if parent.children().any(|child| child.kind() == VISIBILITY) { 31 if parent.children().any(|child| child.kind() == VISIBILITY) {
32 return None; 32 return None;
33 } 33 }
34 vis_offset(parent) 34 (vis_offset(parent), keyword.range())
35 } else { 35 } else {
36 let ident = ctx.leaf_at_offset().find(|leaf| leaf.kind() == IDENT)?; 36 let ident = ctx.leaf_at_offset().find(|leaf| leaf.kind() == IDENT)?;
37 let field = ident.ancestors().find_map(ast::NamedFieldDef::cast)?; 37 let field = ident.ancestors().find_map(ast::NamedFieldDef::cast)?;
38 if field.name()?.syntax().range() != ident.range() && field.visibility().is_some() { 38 if field.name()?.syntax().range() != ident.range() && field.visibility().is_some() {
39 return None; 39 return None;
40 } 40 }
41 vis_offset(field.syntax()) 41 (vis_offset(field.syntax()), ident.range())
42 }; 42 };
43 43
44 ctx.build("make pub(crate)", |edit| { 44 ctx.build("make pub(crate)", |edit| {
45 edit.target(target);
45 edit.insert(offset, "pub(crate) "); 46 edit.insert(offset, "pub(crate) ");
46 edit.set_cursor(offset); 47 edit.set_cursor(offset);
47 }) 48 })
@@ -60,13 +61,15 @@ fn vis_offset(node: &SyntaxNode) -> TextUnit {
60 61
61fn change_vis(ctx: AssistCtx<impl HirDatabase>, vis: &ast::Visibility) -> Option<Assist> { 62fn change_vis(ctx: AssistCtx<impl HirDatabase>, vis: &ast::Visibility) -> Option<Assist> {
62 if vis.syntax().text() == "pub" { 63 if vis.syntax().text() == "pub" {
63 return ctx.build("chage to pub(crate)", |edit| { 64 return ctx.build("change to pub(crate)", |edit| {
65 edit.target(vis.syntax().range());
64 edit.replace(vis.syntax().range(), "pub(crate)"); 66 edit.replace(vis.syntax().range(), "pub(crate)");
65 edit.set_cursor(vis.syntax().range().start()); 67 edit.set_cursor(vis.syntax().range().start());
66 }); 68 });
67 } 69 }
68 if vis.syntax().text() == "pub(crate)" { 70 if vis.syntax().text() == "pub(crate)" {
69 return ctx.build("chage to pub", |edit| { 71 return ctx.build("change to pub", |edit| {
72 edit.target(vis.syntax().range());
70 edit.replace(vis.syntax().range(), "pub"); 73 edit.replace(vis.syntax().range(), "pub");
71 edit.set_cursor(vis.syntax().range().start()); 74 edit.set_cursor(vis.syntax().range().start());
72 }); 75 });
@@ -77,7 +80,7 @@ fn change_vis(ctx: AssistCtx<impl HirDatabase>, vis: &ast::Visibility) -> Option
77#[cfg(test)] 80#[cfg(test)]
78mod tests { 81mod tests {
79 use super::*; 82 use super::*;
80 use crate::helpers::check_assist; 83 use crate::helpers::{check_assist, check_assist_target};
81 84
82 #[test] 85 #[test]
83 fn change_visibility_adds_pub_crate_to_items() { 86 fn change_visibility_adds_pub_crate_to_items() {
@@ -135,4 +138,11 @@ mod tests {
135 ", 138 ",
136 ) 139 )
137 } 140 }
141
142 #[test]
143 fn change_visibility_target() {
144 check_assist_target(change_visibility, "<|>fn foo() {}", "fn");
145 check_assist_target(change_visibility, "pub(crate)<|> fn foo() {}", "pub(crate)");
146 check_assist_target(change_visibility, "struct S { <|>field: u32 }", "field");
147 }
138} 148}