Skip to content

Commit f834858

Browse files
committed
better change visibility assist
* don't add visibility before #derive * suggest changing pub(crate) into pub
1 parent acd90bc commit f834858

File tree

1 file changed

+59
-10
lines changed

1 file changed

+59
-10
lines changed

crates/ra_ide_api_light/src/assists/change_visibility.rs

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use ra_syntax::{
2-
AstNode,
2+
AstNode, SyntaxNode, TextUnit,
33
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},
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},
55
};
66

77
use crate::assists::{AssistCtx, Assist};
@@ -30,14 +30,14 @@ fn add_vis(ctx: AssistCtx) -> Option<Assist> {
3030
if parent.children().any(|child| child.kind() == VISIBILITY) {
3131
return None;
3232
}
33-
parent.range().start()
33+
vis_offset(parent)
3434
} else {
3535
let ident = ctx.leaf_at_offset().find(|leaf| leaf.kind() == IDENT)?;
3636
let field = ident.ancestors().find_map(ast::NamedFieldDef::cast)?;
3737
if field.name()?.syntax().range() != ident.range() && field.visibility().is_some() {
3838
return None;
3939
}
40-
field.syntax().range().start()
40+
vis_offset(field.syntax())
4141
};
4242

4343
ctx.build("make pub(crate)", |edit| {
@@ -46,14 +46,31 @@ fn add_vis(ctx: AssistCtx) -> Option<Assist> {
4646
})
4747
}
4848

49+
fn 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+
4960
fn change_vis(ctx: AssistCtx, vis: &ast::Visibility) -> Option<Assist> {
50-
if vis.syntax().text() != "pub" {
51-
return None;
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+
});
5266
}
53-
ctx.build("chage to pub(crate)", |edit| {
54-
edit.replace(vis.syntax().range(), "pub(crate)");
55-
edit.set_cursor(vis.syntax().range().start());
56-
})
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
5774
}
5875

5976
#[cfg(test)]
@@ -113,4 +130,36 @@ mod tests {
113130
"<|>pub(crate) fn foo() {}",
114131
)
115132
}
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+
}
116165
}

0 commit comments

Comments
 (0)