2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2021-09-27 05:54:24 -05:00
|
|
|
ast::{self, HasName, HasVisibility},
|
2019-07-04 15:05:17 -05:00
|
|
|
AstNode,
|
2021-06-11 08:40:56 -05:00
|
|
|
SyntaxKind::{
|
2023-07-22 09:16:51 -05:00
|
|
|
self, ASSOC_ITEM_LIST, CONST, ENUM, FN, MACRO_DEF, MODULE, SOURCE_FILE, STATIC, STRUCT,
|
|
|
|
TRAIT, TYPE_ALIAS, USE, VISIBILITY,
|
2021-06-11 08:40:56 -05:00
|
|
|
},
|
2023-07-22 09:16:51 -05:00
|
|
|
SyntaxNode, T,
|
2019-01-03 06:08:32 -06:00
|
|
|
};
|
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
use crate::{utils::vis_offset, AssistContext, AssistId, AssistKind, Assists};
|
2020-05-06 11:45:35 -05:00
|
|
|
|
2019-10-26 09:37:04 -05:00
|
|
|
// Assist: change_visibility
|
|
|
|
//
|
|
|
|
// Adds or changes existing visibility specifier.
|
|
|
|
//
|
|
|
|
// ```
|
2021-01-06 14:15:48 -06:00
|
|
|
// $0fn frobnicate() {}
|
2019-10-26 09:37:04 -05:00
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// pub(crate) fn frobnicate() {}
|
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn change_visibility(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2019-10-27 03:48:40 -05:00
|
|
|
if let Some(vis) = ctx.find_node_at_offset::<ast::Visibility>() {
|
2020-05-06 11:45:35 -05:00
|
|
|
return change_vis(acc, vis);
|
2019-01-05 06:28:07 -06:00
|
|
|
}
|
2020-05-06 11:45:35 -05:00
|
|
|
add_vis(acc, ctx)
|
2019-01-05 06:28:07 -06:00
|
|
|
}
|
|
|
|
|
2022-07-20 08:02:08 -05:00
|
|
|
fn add_vis(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2020-06-27 20:02:03 -05:00
|
|
|
let item_keyword = ctx.token_at_offset().find(|leaf| {
|
2020-07-11 18:49:51 -05:00
|
|
|
matches!(
|
|
|
|
leaf.kind(),
|
2020-10-18 10:04:12 -05:00
|
|
|
T![const]
|
|
|
|
| T![static]
|
|
|
|
| T![fn]
|
|
|
|
| T![mod]
|
|
|
|
| T![struct]
|
|
|
|
| T![enum]
|
|
|
|
| T![trait]
|
|
|
|
| T![type]
|
2021-06-11 08:40:56 -05:00
|
|
|
| T![use]
|
|
|
|
| T![macro]
|
2020-07-11 18:49:51 -05:00
|
|
|
)
|
2019-01-05 06:28:07 -06:00
|
|
|
});
|
|
|
|
|
2019-02-08 15:43:13 -06:00
|
|
|
let (offset, target) = if let Some(keyword) = item_keyword {
|
2021-01-30 09:19:21 -06:00
|
|
|
let parent = keyword.parent()?;
|
2023-07-22 09:16:51 -05:00
|
|
|
|
|
|
|
if !can_add(&parent) {
|
2019-01-03 10:37:41 -06:00
|
|
|
return None;
|
|
|
|
}
|
2023-07-22 09:16:51 -05:00
|
|
|
// Already has visibility, do nothing
|
2019-01-03 10:37:41 -06:00
|
|
|
if parent.children().any(|child| child.kind() == VISIBILITY) {
|
|
|
|
return None;
|
|
|
|
}
|
2019-07-20 04:58:27 -05:00
|
|
|
(vis_offset(&parent), keyword.text_range())
|
2020-05-02 11:10:23 -05:00
|
|
|
} else if let Some(field_name) = ctx.find_node_at_offset::<ast::Name>() {
|
2020-07-30 09:49:13 -05:00
|
|
|
let field = field_name.syntax().ancestors().find_map(ast::RecordField::cast)?;
|
2020-03-25 09:55:57 -05:00
|
|
|
if field.name()? != field_name {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(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())
|
2020-07-30 09:49:13 -05:00
|
|
|
} else if let Some(field) = ctx.find_node_at_offset::<ast::TupleField>() {
|
2020-05-02 11:10:23 -05:00
|
|
|
if field.visibility().is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
(vis_offset(field.syntax()), field.syntax().text_range())
|
|
|
|
} else {
|
|
|
|
return None;
|
2019-01-03 10:37:41 -06:00
|
|
|
};
|
2019-01-03 06:08:32 -06:00
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
acc.add(
|
2020-07-02 16:48:35 -05:00
|
|
|
AssistId("change_visibility", AssistKind::RefactorRewrite),
|
2020-06-28 17:36:05 -05:00
|
|
|
"Change visibility to pub(crate)",
|
|
|
|
target,
|
|
|
|
|edit| {
|
|
|
|
edit.insert(offset, "pub(crate) ");
|
|
|
|
},
|
|
|
|
)
|
2019-01-03 06:08:32 -06:00
|
|
|
}
|
|
|
|
|
2023-07-22 09:16:51 -05:00
|
|
|
fn can_add(node: &SyntaxNode) -> bool {
|
|
|
|
const LEGAL: &[SyntaxKind] =
|
|
|
|
&[CONST, STATIC, TYPE_ALIAS, FN, MODULE, STRUCT, ENUM, TRAIT, USE, MACRO_DEF];
|
|
|
|
|
|
|
|
LEGAL.contains(&node.kind()) && {
|
|
|
|
let Some(p) = node.parent() else {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
if p.kind() == ASSOC_ITEM_LIST {
|
|
|
|
p.parent()
|
|
|
|
.and_then(|it| ast::Impl::cast(it))
|
|
|
|
// inherent impls i.e 'non-trait impls' have a non-local
|
|
|
|
// effect, thus can have visibility even when nested.
|
|
|
|
// so filter them out
|
|
|
|
.filter(|imp| imp.for_token().is_none())
|
|
|
|
.is_some()
|
|
|
|
} else {
|
|
|
|
matches!(p.kind(), SOURCE_FILE | MODULE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 11:45:35 -05:00
|
|
|
fn change_vis(acc: &mut Assists, vis: ast::Visibility) -> Option<()> {
|
2019-01-10 10:18:15 -06:00
|
|
|
if vis.syntax().text() == "pub" {
|
2020-05-06 05:51:28 -05:00
|
|
|
let target = vis.syntax().text_range();
|
2020-05-06 11:45:35 -05:00
|
|
|
return acc.add(
|
2020-07-02 16:48:35 -05:00
|
|
|
AssistId("change_visibility", AssistKind::RefactorRewrite),
|
2020-01-14 10:49:14 -06:00
|
|
|
"Change Visibility to pub(crate)",
|
2020-05-06 05:51:28 -05:00
|
|
|
target,
|
2020-01-14 10:49:14 -06:00
|
|
|
|edit| {
|
|
|
|
edit.replace(vis.syntax().text_range(), "pub(crate)");
|
|
|
|
},
|
|
|
|
);
|
2019-01-05 06:28:07 -06:00
|
|
|
}
|
2019-01-10 10:18:15 -06:00
|
|
|
if vis.syntax().text() == "pub(crate)" {
|
2020-05-06 05:51:28 -05:00
|
|
|
let target = vis.syntax().text_range();
|
2020-05-06 11:45:35 -05:00
|
|
|
return acc.add(
|
2020-07-02 16:48:35 -05:00
|
|
|
AssistId("change_visibility", AssistKind::RefactorRewrite),
|
2020-05-06 05:51:28 -05:00
|
|
|
"Change visibility to pub",
|
|
|
|
target,
|
|
|
|
|edit| {
|
|
|
|
edit.replace(vis.syntax().text_range(), "pub");
|
|
|
|
},
|
|
|
|
);
|
2019-01-10 10:18:15 -06:00
|
|
|
}
|
|
|
|
None
|
2019-01-05 06:28:07 -06:00
|
|
|
}
|
|
|
|
|
2019-01-03 06:08:32 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-05-06 03:16:55 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
|
2020-03-25 09:55:57 -05:00
|
|
|
|
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() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist(change_visibility, "$0fn foo() {}", "pub(crate) fn foo() {}");
|
|
|
|
check_assist(change_visibility, "f$0n foo() {}", "pub(crate) fn foo() {}");
|
|
|
|
check_assist(change_visibility, "$0struct Foo {}", "pub(crate) struct Foo {}");
|
|
|
|
check_assist(change_visibility, "$0mod foo {}", "pub(crate) mod foo {}");
|
|
|
|
check_assist(change_visibility, "$0trait Foo {}", "pub(crate) trait Foo {}");
|
|
|
|
check_assist(change_visibility, "m$0od {}", "pub(crate) mod {}");
|
|
|
|
check_assist(change_visibility, "unsafe f$0n foo() {}", "pub(crate) unsafe fn foo() {}");
|
2021-06-11 08:40:56 -05:00
|
|
|
check_assist(change_visibility, "$0macro foo() {}", "pub(crate) macro foo() {}");
|
|
|
|
check_assist(change_visibility, "$0use foo;", "pub(crate) use foo;");
|
2023-07-22 09:16:51 -05:00
|
|
|
check_assist(
|
|
|
|
change_visibility,
|
|
|
|
"impl Foo { f$0n foo() {} }",
|
|
|
|
"impl Foo { pub(crate) fn foo() {} }",
|
|
|
|
);
|
|
|
|
check_assist(
|
|
|
|
change_visibility,
|
|
|
|
"fn bar() { impl Foo { f$0n foo() {} } }",
|
|
|
|
"fn bar() { impl Foo { pub(crate) fn foo() {} } }",
|
|
|
|
);
|
2019-01-03 06:08:32 -06:00
|
|
|
}
|
2019-01-03 10:37:41 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_visibility_works_with_struct_fields() {
|
|
|
|
check_assist(
|
|
|
|
change_visibility,
|
2021-01-06 14:15:48 -06:00
|
|
|
r"struct S { $0field: u32 }",
|
2020-05-20 15:55:37 -05:00
|
|
|
r"struct S { pub(crate) field: u32 }",
|
2020-05-02 11:10:23 -05:00
|
|
|
);
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist(change_visibility, r"struct S ( $0u32 )", r"struct S ( pub(crate) u32 )");
|
2020-03-25 09:55:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_visibility_field_false_positive() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(change_visibility_field_false_positive);
|
2020-03-25 09:55:57 -05:00
|
|
|
check_assist_not_applicable(
|
|
|
|
change_visibility,
|
2021-01-06 14:15:48 -06:00
|
|
|
r"struct S { field: [(); { let $0x = ();}] }",
|
2019-01-03 10:37:41 -06:00
|
|
|
)
|
|
|
|
}
|
2019-01-05 06:28:07 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_visibility_pub_to_pub_crate() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist(change_visibility, "$0pub fn foo() {}", "pub(crate) fn foo() {}")
|
2019-01-05 06:28:07 -06:00
|
|
|
}
|
2019-01-10 10:18:15 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_visibility_pub_crate_to_pub() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist(change_visibility, "$0pub(crate) fn foo() {}", "pub fn foo() {}")
|
2019-01-10 10:18:15 -06:00
|
|
|
}
|
|
|
|
|
2020-02-27 19:15:17 -06:00
|
|
|
#[test]
|
|
|
|
fn change_visibility_const() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist(change_visibility, "$0const FOO = 3u8;", "pub(crate) const FOO = 3u8;");
|
2020-02-27 19:15:17 -06:00
|
|
|
}
|
|
|
|
|
2020-07-11 18:49:51 -05:00
|
|
|
#[test]
|
|
|
|
fn change_visibility_static() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist(change_visibility, "$0static FOO = 3u8;", "pub(crate) static FOO = 3u8;");
|
2020-07-11 18:49:51 -05:00
|
|
|
}
|
|
|
|
|
2020-10-18 10:04:12 -05:00
|
|
|
#[test]
|
|
|
|
fn change_visibility_type_alias() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist(change_visibility, "$0type T = ();", "pub(crate) type T = ();");
|
2020-10-18 10:04:12 -05:00
|
|
|
}
|
|
|
|
|
2019-01-10 10:18:15 -06:00
|
|
|
#[test]
|
|
|
|
fn change_visibility_handles_comment_attrs() {
|
|
|
|
check_assist(
|
|
|
|
change_visibility,
|
2020-03-25 09:55:57 -05:00
|
|
|
r"
|
2019-01-10 10:18:15 -06:00
|
|
|
/// docs
|
|
|
|
|
|
|
|
// comments
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2021-01-06 14:15:48 -06:00
|
|
|
$0struct Foo;
|
2019-01-10 10:18:15 -06:00
|
|
|
",
|
2020-03-25 09:55:57 -05:00
|
|
|
r"
|
2019-01-10 10:18:15 -06:00
|
|
|
/// docs
|
|
|
|
|
|
|
|
// comments
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2020-05-20 15:55:37 -05:00
|
|
|
pub(crate) struct Foo;
|
2019-01-10 10:18:15 -06:00
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
2019-02-08 17:34:05 -06:00
|
|
|
|
2020-05-02 18:01:59 -05:00
|
|
|
#[test]
|
|
|
|
fn not_applicable_for_enum_variants() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
change_visibility,
|
|
|
|
r"mod foo { pub enum Foo {Foo1} }
|
2021-01-06 14:15:48 -06:00
|
|
|
fn main() { foo::Foo::Foo1$0 } ",
|
2020-05-02 18:01:59 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-08 17:34:05 -06:00
|
|
|
#[test]
|
|
|
|
fn change_visibility_target() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist_target(change_visibility, "$0fn foo() {}", "fn");
|
|
|
|
check_assist_target(change_visibility, "pub(crate)$0 fn foo() {}", "pub(crate)");
|
|
|
|
check_assist_target(change_visibility, "struct S { $0field: u32 }", "field");
|
2019-02-08 17:34:05 -06:00
|
|
|
}
|
2023-07-22 09:16:51 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_applicable_for_items_within_traits() {
|
|
|
|
check_assist_not_applicable(change_visibility, "trait Foo { f$0n run() {} }");
|
|
|
|
check_assist_not_applicable(change_visibility, "trait Foo { con$0st FOO: u8 = 69; }");
|
|
|
|
check_assist_not_applicable(change_visibility, "impl Foo for Bar { f$0n quox() {} }");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not_applicable_for_items_within_fns() {
|
|
|
|
check_assist_not_applicable(change_visibility, "fn foo() { f$0n inner() {} }");
|
|
|
|
check_assist_not_applicable(change_visibility, "fn foo() { unsafe f$0n inner() {} }");
|
|
|
|
check_assist_not_applicable(change_visibility, "fn foo() { const f$0n inner() {} }");
|
|
|
|
check_assist_not_applicable(change_visibility, "fn foo() { con$0st FOO: u8 = 69; }");
|
|
|
|
check_assist_not_applicable(change_visibility, "fn foo() { en$0um Foo {} }");
|
|
|
|
check_assist_not_applicable(change_visibility, "fn foo() { stru$0ct Foo {} }");
|
|
|
|
check_assist_not_applicable(change_visibility, "fn foo() { mo$0d foo {} }");
|
|
|
|
check_assist_not_applicable(change_visibility, "fn foo() { $0use foo; }");
|
|
|
|
check_assist_not_applicable(change_visibility, "fn foo() { $0type Foo = Bar<T>; }");
|
|
|
|
check_assist_not_applicable(change_visibility, "fn foo() { tr$0ait Foo {} }");
|
|
|
|
check_assist_not_applicable(
|
|
|
|
change_visibility,
|
|
|
|
"fn foo() { impl Trait for Bar { f$0n bar() {} } }",
|
|
|
|
);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
change_visibility,
|
|
|
|
"fn foo() { impl Trait for Bar { con$0st FOO: u8 = 69; } }",
|
|
|
|
);
|
|
|
|
}
|
2019-01-03 06:08:32 -06:00
|
|
|
}
|