2019-02-03 12:26:35 -06:00
|
|
|
use hir::db::HirDatabase;
|
2019-01-03 06:08:32 -06:00
|
|
|
use ra_syntax::{
|
2019-01-10 10:18:15 -06:00
|
|
|
AstNode, SyntaxNode, TextUnit,
|
2019-01-03 10:37:41 -06:00
|
|
|
ast::{self, VisibilityOwner, NameOwner},
|
2019-01-10 10:18:15 -06:00
|
|
|
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},
|
2019-01-03 06:08:32 -06:00
|
|
|
};
|
|
|
|
|
2019-02-03 12:26:35 -06:00
|
|
|
use crate::{AssistCtx, Assist};
|
2019-01-03 06:08:32 -06:00
|
|
|
|
2019-02-03 12:26:35 -06:00
|
|
|
pub(crate) fn change_visibility(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
2019-01-05 06:28:07 -06:00
|
|
|
if let Some(vis) = ctx.node_at_offset::<ast::Visibility>() {
|
|
|
|
return change_vis(ctx, vis);
|
|
|
|
}
|
|
|
|
add_vis(ctx)
|
|
|
|
}
|
|
|
|
|
2019-02-11 11:07:21 -06:00
|
|
|
fn add_vis(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
2019-01-05 06:28:07 -06:00
|
|
|
let item_keyword = ctx.leaf_at_offset().find(|leaf| match leaf.kind() {
|
2019-01-03 06:08:32 -06:00
|
|
|
FN_KW | MOD_KW | STRUCT_KW | ENUM_KW | TRAIT_KW => true,
|
|
|
|
_ => false,
|
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 {
|
2019-01-03 10:37:41 -06:00
|
|
|
let parent = keyword.parent()?;
|
|
|
|
let def_kws = vec![FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF];
|
|
|
|
// 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-02-08 17:34:05 -06:00
|
|
|
(vis_offset(parent), keyword.range())
|
2019-01-03 10:37:41 -06:00
|
|
|
} else {
|
|
|
|
let ident = ctx.leaf_at_offset().find(|leaf| leaf.kind() == IDENT)?;
|
|
|
|
let field = ident.ancestors().find_map(ast::NamedFieldDef::cast)?;
|
|
|
|
if field.name()?.syntax().range() != ident.range() && field.visibility().is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
2019-02-08 17:34:05 -06:00
|
|
|
(vis_offset(field.syntax()), ident.range())
|
2019-01-03 10:37:41 -06:00
|
|
|
};
|
2019-01-03 06:08:32 -06:00
|
|
|
|
2019-02-11 11:07:21 -06:00
|
|
|
ctx.add_action("make 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-02-11 11:07:21 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
ctx.build()
|
2019-01-03 06:08:32 -06:00
|
|
|
}
|
|
|
|
|
2019-01-10 10:18:15 -06:00
|
|
|
fn vis_offset(node: &SyntaxNode) -> TextUnit {
|
|
|
|
node.children()
|
|
|
|
.skip_while(|it| match it.kind() {
|
|
|
|
WHITESPACE | COMMENT | ATTR => true,
|
|
|
|
_ => false,
|
|
|
|
})
|
|
|
|
.next()
|
|
|
|
.map(|it| it.range().start())
|
|
|
|
.unwrap_or(node.range().start())
|
|
|
|
}
|
|
|
|
|
2019-02-11 11:07:21 -06:00
|
|
|
fn change_vis(mut ctx: AssistCtx<impl HirDatabase>, vis: &ast::Visibility) -> Option<Assist> {
|
2019-01-10 10:18:15 -06:00
|
|
|
if vis.syntax().text() == "pub" {
|
2019-02-11 11:07:21 -06:00
|
|
|
ctx.add_action("change to pub(crate)", |edit| {
|
2019-02-08 15:43:13 -06:00
|
|
|
edit.target(vis.syntax().range());
|
2019-01-10 10:18:15 -06:00
|
|
|
edit.replace(vis.syntax().range(), "pub(crate)");
|
2019-02-11 11:07:21 -06:00
|
|
|
edit.set_cursor(vis.syntax().range().start())
|
2019-01-10 10:18:15 -06:00
|
|
|
});
|
2019-02-11 11:07:21 -06:00
|
|
|
|
|
|
|
return ctx.build();
|
2019-01-05 06:28:07 -06:00
|
|
|
}
|
2019-01-10 10:18:15 -06:00
|
|
|
if vis.syntax().text() == "pub(crate)" {
|
2019-02-11 11:07:21 -06:00
|
|
|
ctx.add_action("change to pub", |edit| {
|
2019-02-08 15:43:13 -06:00
|
|
|
edit.target(vis.syntax().range());
|
2019-01-10 10:18:15 -06:00
|
|
|
edit.replace(vis.syntax().range(), "pub");
|
|
|
|
edit.set_cursor(vis.syntax().range().start());
|
|
|
|
});
|
2019-02-11 11:07:21 -06:00
|
|
|
|
|
|
|
return ctx.build();
|
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 {
|
|
|
|
use super::*;
|
2019-02-08 17:34:05 -06:00
|
|
|
use crate::helpers::{check_assist, check_assist_target};
|
2019-01-03 06:08:32 -06:00
|
|
|
|
|
|
|
#[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,
|
|
|
|
"struct S { <|>field: u32 }",
|
|
|
|
"struct S { <|>pub(crate) field: u32 }",
|
|
|
|
)
|
|
|
|
}
|
2019-01-05 06:28:07 -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() {}")
|
2019-01-05 06:28:07 -06:00
|
|
|
}
|
2019-01-10 10:18:15 -06:00
|
|
|
|
|
|
|
#[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() {}")
|
2019-01-10 10:18:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn change_visibility_handles_comment_attrs() {
|
|
|
|
check_assist(
|
|
|
|
change_visibility,
|
|
|
|
"
|
|
|
|
/// docs
|
|
|
|
|
|
|
|
// comments
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
<|>struct Foo;
|
|
|
|
",
|
|
|
|
"
|
|
|
|
/// 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
|
|
|
}
|