4265: Make `change_visibility` assist work for tuple struct fields r=matklad a=Jesse-Bakker

Fixes #4206 

Co-authored-by: Jesse Bakker <git@jessebakker.com>
This commit is contained in:
bors[bot] 2020-05-05 14:42:33 +00:00 committed by GitHub
commit f68e099777
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -47,8 +47,7 @@ fn add_vis(ctx: AssistCtx) -> Option<Assist> {
return None;
}
(vis_offset(&parent), keyword.text_range())
} else {
let field_name: ast::Name = ctx.find_node_at_offset()?;
} else if let Some(field_name) = ctx.find_node_at_offset::<ast::Name>() {
let field = field_name.syntax().ancestors().find_map(ast::RecordFieldDef::cast)?;
if field.name()? != field_name {
tested_by!(change_visibility_field_false_positive);
@ -58,6 +57,13 @@ fn add_vis(ctx: AssistCtx) -> Option<Assist> {
return None;
}
(vis_offset(field.syntax()), field_name.syntax().text_range())
} else if let Some(field) = ctx.find_node_at_offset::<ast::TupleFieldDef>() {
if field.visibility().is_some() {
return None;
}
(vis_offset(field.syntax()), field.syntax().text_range())
} else {
return None;
};
ctx.add_assist(AssistId("change_visibility"), "Change visibility to pub(crate)", |edit| {
@ -129,7 +135,8 @@ mod tests {
change_visibility,
r"struct S { <|>field: u32 }",
r"struct S { <|>pub(crate) field: u32 }",
)
);
check_assist(change_visibility, r"struct S ( <|>u32 )", r"struct S ( <|>pub(crate) u32 )");
}
#[test]