2019-01-03 12:08:32 +00:00
|
|
|
use ra_syntax::{
|
2019-01-03 16:37:41 +00:00
|
|
|
AstNode,
|
|
|
|
ast::{self, VisibilityOwner, NameOwner},
|
|
|
|
SyntaxKind::{VISIBILITY, FN_KW, MOD_KW, STRUCT_KW, ENUM_KW, TRAIT_KW, FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF, IDENT},
|
2019-01-03 12:08:32 +00:00
|
|
|
};
|
|
|
|
|
2019-01-03 15:59:17 +00:00
|
|
|
use crate::assists::{AssistCtx, Assist};
|
2019-01-03 12:08:32 +00:00
|
|
|
|
2019-01-03 15:59:17 +00:00
|
|
|
pub fn change_visibility(ctx: AssistCtx) -> Option<Assist> {
|
2019-01-03 16:37:41 +00:00
|
|
|
let offset = if let Some(keyword) = ctx.leaf_at_offset().find(|leaf| match leaf.kind() {
|
2019-01-03 12:08:32 +00:00
|
|
|
FN_KW | MOD_KW | STRUCT_KW | ENUM_KW | TRAIT_KW => true,
|
|
|
|
_ => false,
|
2019-01-03 16:37:41 +00: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;
|
|
|
|
}
|
|
|
|
parent.range().start()
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
field.syntax().range().start()
|
|
|
|
};
|
2019-01-03 12:08:32 +00:00
|
|
|
|
2019-01-03 16:37:41 +00:00
|
|
|
ctx.build("make pub(crate)", |edit| {
|
|
|
|
edit.insert(offset, "pub(crate) ");
|
|
|
|
edit.set_cursor(offset);
|
2019-01-03 12:08:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-01-03 15:59:17 +00:00
|
|
|
use crate::assists::check_assist;
|
2019-01-03 12:08:32 +00:00
|
|
|
|
|
|
|
#[test]
|
2019-01-03 16:37:41 +00:00
|
|
|
fn change_visibility_adds_pub_crate_to_items() {
|
2019-01-03 15:59:17 +00:00
|
|
|
check_assist(
|
|
|
|
change_visibility,
|
2019-01-03 12:08:32 +00:00
|
|
|
"<|>fn foo() {}",
|
|
|
|
"<|>pub(crate) fn foo() {}",
|
|
|
|
);
|
2019-01-03 15:59:17 +00:00
|
|
|
check_assist(
|
|
|
|
change_visibility,
|
2019-01-03 12:08:32 +00:00
|
|
|
"f<|>n foo() {}",
|
|
|
|
"<|>pub(crate) fn foo() {}",
|
|
|
|
);
|
2019-01-03 15:59:17 +00:00
|
|
|
check_assist(
|
|
|
|
change_visibility,
|
2019-01-03 12:08:32 +00:00
|
|
|
"<|>struct Foo {}",
|
|
|
|
"<|>pub(crate) struct Foo {}",
|
|
|
|
);
|
2019-01-03 15:59:17 +00:00
|
|
|
check_assist(
|
|
|
|
change_visibility,
|
|
|
|
"<|>mod foo {}",
|
|
|
|
"<|>pub(crate) mod foo {}",
|
|
|
|
);
|
|
|
|
check_assist(
|
|
|
|
change_visibility,
|
2019-01-03 12:08:32 +00:00
|
|
|
"<|>trait Foo {}",
|
|
|
|
"<|>pub(crate) trait Foo {}",
|
|
|
|
);
|
2019-01-03 15:59:17 +00:00
|
|
|
check_assist(change_visibility, "m<|>od {}", "<|>pub(crate) mod {}");
|
|
|
|
check_assist(
|
|
|
|
change_visibility,
|
2019-01-03 12:08:32 +00:00
|
|
|
"unsafe f<|>n foo() {}",
|
|
|
|
"<|>pub(crate) unsafe fn foo() {}",
|
|
|
|
);
|
|
|
|
}
|
2019-01-03 16:37:41 +00: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-03 12:08:32 +00:00
|
|
|
}
|