rust/crates/ra_assists/src/handlers/change_visibility.rs

189 lines
5.8 KiB
Rust
Raw Normal View History

2019-01-03 06:08:32 -06:00
use ra_syntax::{
ast::{self, NameOwner, VisibilityOwner},
AstNode,
SyntaxKind::{
2020-03-25 09:55:57 -05:00
ATTR, COMMENT, CONST_DEF, ENUM_DEF, FN_DEF, MODULE, STRUCT_DEF, TRAIT_DEF, VISIBILITY,
WHITESPACE,
},
2020-04-24 16:40:41 -05:00
SyntaxNode, TextSize, T,
2019-01-03 06:08:32 -06:00
};
use crate::{Assist, AssistCtx, AssistId};
2020-03-25 09:55:57 -05:00
use test_utils::tested_by;
2019-01-03 06:08:32 -06:00
2019-10-26 09:37:04 -05:00
// Assist: change_visibility
//
// Adds or changes existing visibility specifier.
//
// ```
2019-10-26 13:17:39 -05:00
// <|>fn frobnicate() {}
2019-10-26 09:37:04 -05:00
// ```
// ->
// ```
// pub(crate) fn frobnicate() {}
// ```
2020-02-06 09:58:57 -06:00
pub(crate) fn change_visibility(ctx: AssistCtx) -> Option<Assist> {
if let Some(vis) = ctx.find_node_at_offset::<ast::Visibility>() {
return change_vis(ctx, vis);
}
add_vis(ctx)
}
2020-02-06 09:58:57 -06:00
fn add_vis(ctx: AssistCtx) -> Option<Assist> {
2019-03-30 05:25:53 -05:00
let item_keyword = ctx.token_at_offset().find(|leaf| match leaf.kind() {
2020-02-27 19:15:17 -06:00
T![const] | T![fn] | T![mod] | T![struct] | T![enum] | T![trait] => true,
2019-01-03 06:08:32 -06:00
_ => false,
});
2019-02-08 15:43:13 -06:00
let (offset, target) = if let Some(keyword) = item_keyword {
2019-03-30 05:25:53 -05:00
let parent = keyword.parent();
2020-02-27 19:15:17 -06:00
let def_kws = vec![CONST_DEF, FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF];
2019-01-03 10:37:41 -06:00
// Parent is not a definition, can't add visibility
if !def_kws.iter().any(|&def_kw| def_kw == parent.kind()) {
return None;
}
// Already have visibility, do nothing
if parent.children().any(|child| child.kind() == VISIBILITY) {
return None;
}
2019-07-20 04:58:27 -05:00
(vis_offset(&parent), keyword.text_range())
2019-01-03 10:37:41 -06:00
} else {
2020-03-25 09:55:57 -05:00
let field_name: ast::Name = ctx.find_node_at_offset()?;
let field = field_name.syntax().ancestors().find_map(ast::RecordFieldDef::cast)?;
if field.name()? != field_name {
tested_by!(change_visibility_field_false_positive);
2019-01-03 10:37:41 -06:00
return None;
}
2020-03-25 09:55:57 -05:00
if field.visibility().is_some() {
return None;
}
(vis_offset(field.syntax()), field_name.syntax().text_range())
2019-01-03 10:37:41 -06:00
};
2019-01-03 06:08:32 -06:00
2020-01-14 11:32:26 -06:00
ctx.add_assist(AssistId("change_visibility"), "Change visibility to pub(crate)", |edit| {
2019-02-08 15:43:13 -06:00
edit.target(target);
2019-01-03 10:37:41 -06:00
edit.insert(offset, "pub(crate) ");
edit.set_cursor(offset);
})
2019-01-03 06:08:32 -06:00
}
2020-04-24 16:40:41 -05:00
fn vis_offset(node: &SyntaxNode) -> TextSize {
2019-03-30 05:25:53 -05:00
node.children_with_tokens()
.skip_while(|it| match it.kind() {
WHITESPACE | COMMENT | ATTR => true,
_ => false,
})
.next()
2019-07-20 04:58:27 -05:00
.map(|it| it.text_range().start())
.unwrap_or_else(|| node.text_range().start())
}
2020-02-06 09:58:57 -06:00
fn change_vis(ctx: AssistCtx, vis: ast::Visibility) -> Option<Assist> {
if vis.syntax().text() == "pub" {
2020-01-14 10:49:14 -06:00
return ctx.add_assist(
AssistId("change_visibility"),
"Change Visibility to pub(crate)",
|edit| {
edit.target(vis.syntax().text_range());
edit.replace(vis.syntax().text_range(), "pub(crate)");
edit.set_cursor(vis.syntax().text_range().start())
},
);
}
if vis.syntax().text() == "pub(crate)" {
2020-01-14 11:32:26 -06:00
return ctx.add_assist(AssistId("change_visibility"), "Change visibility to pub", |edit| {
2019-07-20 04:58:27 -05:00
edit.target(vis.syntax().text_range());
edit.replace(vis.syntax().text_range(), "pub");
edit.set_cursor(vis.syntax().text_range().start());
});
}
None
}
2019-01-03 06:08:32 -06:00
#[cfg(test)]
mod tests {
2020-03-25 09:55:57 -05:00
use test_utils::covers;
use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target};
2019-01-03 06:08:32 -06:00
use super::*;
#[test]
2019-01-03 10:37:41 -06:00
fn change_visibility_adds_pub_crate_to_items() {
2019-02-08 05:49:43 -06:00
check_assist(change_visibility, "<|>fn foo() {}", "<|>pub(crate) fn foo() {}");
check_assist(change_visibility, "f<|>n foo() {}", "<|>pub(crate) fn foo() {}");
check_assist(change_visibility, "<|>struct Foo {}", "<|>pub(crate) struct Foo {}");
check_assist(change_visibility, "<|>mod foo {}", "<|>pub(crate) mod foo {}");
check_assist(change_visibility, "<|>trait Foo {}", "<|>pub(crate) trait Foo {}");
2019-01-03 09:59:17 -06:00
check_assist(change_visibility, "m<|>od {}", "<|>pub(crate) mod {}");
check_assist(
change_visibility,
2019-01-03 06:08:32 -06:00
"unsafe f<|>n foo() {}",
"<|>pub(crate) unsafe fn foo() {}",
);
}
2019-01-03 10:37:41 -06:00
#[test]
fn change_visibility_works_with_struct_fields() {
check_assist(
change_visibility,
2020-03-25 09:55:57 -05:00
r"struct S { <|>field: u32 }",
r"struct S { <|>pub(crate) field: u32 }",
)
}
#[test]
fn change_visibility_field_false_positive() {
covers!(change_visibility_field_false_positive);
check_assist_not_applicable(
change_visibility,
r"struct S { field: [(); { let <|>x = ();}] }",
2019-01-03 10:37:41 -06:00
)
}
#[test]
fn change_visibility_pub_to_pub_crate() {
2019-02-08 05:49:43 -06:00
check_assist(change_visibility, "<|>pub fn foo() {}", "<|>pub(crate) fn foo() {}")
}
#[test]
fn change_visibility_pub_crate_to_pub() {
2019-02-08 05:49:43 -06:00
check_assist(change_visibility, "<|>pub(crate) fn foo() {}", "<|>pub fn foo() {}")
}
2020-02-27 19:15:17 -06:00
#[test]
fn change_visibility_const() {
check_assist(change_visibility, "<|>const FOO = 3u8;", "<|>pub(crate) const FOO = 3u8;");
}
#[test]
fn change_visibility_handles_comment_attrs() {
check_assist(
change_visibility,
2020-03-25 09:55:57 -05:00
r"
/// docs
// comments
#[derive(Debug)]
<|>struct Foo;
",
2020-03-25 09:55:57 -05:00
r"
/// docs
// comments
#[derive(Debug)]
<|>pub(crate) struct Foo;
",
)
}
2019-02-08 17:34:05 -06:00
#[test]
fn change_visibility_target() {
check_assist_target(change_visibility, "<|>fn foo() {}", "fn");
check_assist_target(change_visibility, "pub(crate)<|> fn foo() {}", "pub(crate)");
check_assist_target(change_visibility, "struct S { <|>field: u32 }", "field");
}
2019-01-03 06:08:32 -06:00
}