mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Merge #434
434: change visibility can change pub to pub(crate) r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
0f0969b64a
2 changed files with 48 additions and 2 deletions
|
@ -7,10 +7,19 @@ use ra_syntax::{
|
|||
use crate::assists::{AssistCtx, Assist};
|
||||
|
||||
pub fn change_visibility(ctx: AssistCtx) -> Option<Assist> {
|
||||
let offset = if let Some(keyword) = ctx.leaf_at_offset().find(|leaf| match leaf.kind() {
|
||||
if let Some(vis) = ctx.node_at_offset::<ast::Visibility>() {
|
||||
return change_vis(ctx, vis);
|
||||
}
|
||||
add_vis(ctx)
|
||||
}
|
||||
|
||||
fn add_vis(ctx: AssistCtx) -> Option<Assist> {
|
||||
let item_keyword = ctx.leaf_at_offset().find(|leaf| match leaf.kind() {
|
||||
FN_KW | MOD_KW | STRUCT_KW | ENUM_KW | TRAIT_KW => true,
|
||||
_ => false,
|
||||
}) {
|
||||
});
|
||||
|
||||
let offset = if let Some(keyword) = item_keyword {
|
||||
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
|
||||
|
@ -37,6 +46,16 @@ pub fn change_visibility(ctx: AssistCtx) -> Option<Assist> {
|
|||
})
|
||||
}
|
||||
|
||||
fn change_vis(ctx: AssistCtx, vis: ast::Visibility) -> Option<Assist> {
|
||||
if vis.syntax().text() != "pub" {
|
||||
return None;
|
||||
}
|
||||
ctx.build("chage to pub(crate)", |edit| {
|
||||
edit.replace(vis.syntax().range(), "pub(crate)");
|
||||
edit.set_cursor(vis.syntax().range().start());
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -85,4 +104,13 @@ mod tests {
|
|||
"struct S { <|>pub(crate) field: u32 }",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn change_visibility_pub_to_pub_crate() {
|
||||
check_assist(
|
||||
change_visibility,
|
||||
"<|>pub fn foo() {}",
|
||||
"<|>pub(crate) fn foo() {}",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -125,3 +125,21 @@ impl From<SyntaxText<'_>> for String {
|
|||
text.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<str> for SyntaxText<'_> {
|
||||
fn eq(&self, mut rhs: &str) -> bool {
|
||||
for chunk in self.chunks() {
|
||||
if !rhs.starts_with(chunk) {
|
||||
return false;
|
||||
}
|
||||
rhs = &rhs[chunk.len()..];
|
||||
}
|
||||
rhs.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<&'_ str> for SyntaxText<'_> {
|
||||
fn eq(&self, rhs: &&str) -> bool {
|
||||
self == *rhs
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue