Fix add visibility false-positive
This commit is contained in:
parent
b3665fccfb
commit
72c6fc3ff0
@ -2,13 +2,14 @@ use ra_syntax::{
|
|||||||
ast::{self, NameOwner, VisibilityOwner},
|
ast::{self, NameOwner, VisibilityOwner},
|
||||||
AstNode,
|
AstNode,
|
||||||
SyntaxKind::{
|
SyntaxKind::{
|
||||||
ATTR, COMMENT, CONST_DEF, ENUM_DEF, FN_DEF, IDENT, MODULE, STRUCT_DEF, TRAIT_DEF,
|
ATTR, COMMENT, CONST_DEF, ENUM_DEF, FN_DEF, MODULE, STRUCT_DEF, TRAIT_DEF, VISIBILITY,
|
||||||
VISIBILITY, WHITESPACE,
|
WHITESPACE,
|
||||||
},
|
},
|
||||||
SyntaxNode, TextUnit, T,
|
SyntaxNode, TextUnit, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{Assist, AssistCtx, AssistId};
|
use crate::{Assist, AssistCtx, AssistId};
|
||||||
|
use test_utils::tested_by;
|
||||||
|
|
||||||
// Assist: change_visibility
|
// Assist: change_visibility
|
||||||
//
|
//
|
||||||
@ -47,13 +48,16 @@ fn add_vis(ctx: AssistCtx) -> Option<Assist> {
|
|||||||
}
|
}
|
||||||
(vis_offset(&parent), keyword.text_range())
|
(vis_offset(&parent), keyword.text_range())
|
||||||
} else {
|
} else {
|
||||||
let ident = ctx.token_at_offset().find(|leaf| leaf.kind() == IDENT)?;
|
let field_name: ast::Name = ctx.find_node_at_offset()?;
|
||||||
let field = ident.parent().ancestors().find_map(ast::RecordFieldDef::cast)?;
|
let field = field_name.syntax().ancestors().find_map(ast::RecordFieldDef::cast)?;
|
||||||
if field.name()?.syntax().text_range() != ident.text_range() && field.visibility().is_some()
|
if field.name()? != field_name {
|
||||||
{
|
tested_by!(change_visibility_field_false_positive);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
(vis_offset(field.syntax()), ident.text_range())
|
if field.visibility().is_some() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
(vis_offset(field.syntax()), field_name.syntax().text_range())
|
||||||
};
|
};
|
||||||
|
|
||||||
ctx.add_assist(AssistId("change_visibility"), "Change visibility to pub(crate)", |edit| {
|
ctx.add_assist(AssistId("change_visibility"), "Change visibility to pub(crate)", |edit| {
|
||||||
@ -98,8 +102,11 @@ fn change_vis(ctx: AssistCtx, vis: ast::Visibility) -> Option<Assist> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use test_utils::covers;
|
||||||
|
|
||||||
|
use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::helpers::{check_assist, check_assist_target};
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn change_visibility_adds_pub_crate_to_items() {
|
fn change_visibility_adds_pub_crate_to_items() {
|
||||||
@ -120,8 +127,17 @@ mod tests {
|
|||||||
fn change_visibility_works_with_struct_fields() {
|
fn change_visibility_works_with_struct_fields() {
|
||||||
check_assist(
|
check_assist(
|
||||||
change_visibility,
|
change_visibility,
|
||||||
"struct S { <|>field: u32 }",
|
r"struct S { <|>field: u32 }",
|
||||||
"struct S { <|>pub(crate) field: u32 }",
|
r"struct S { <|>pub(crate) field: u32 }",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn change_visibility_field_false_positive() {
|
||||||
|
covers!(change_visibility_field_false_positive);
|
||||||
|
check_assist_not_applicable(
|
||||||
|
change_visibility,
|
||||||
|
r"struct S { field: [(); { let <|>x = ();}] }",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,7 +160,7 @@ mod tests {
|
|||||||
fn change_visibility_handles_comment_attrs() {
|
fn change_visibility_handles_comment_attrs() {
|
||||||
check_assist(
|
check_assist(
|
||||||
change_visibility,
|
change_visibility,
|
||||||
"
|
r"
|
||||||
/// docs
|
/// docs
|
||||||
|
|
||||||
// comments
|
// comments
|
||||||
@ -152,7 +168,7 @@ mod tests {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
<|>struct Foo;
|
<|>struct Foo;
|
||||||
",
|
",
|
||||||
"
|
r"
|
||||||
/// docs
|
/// docs
|
||||||
|
|
||||||
// comments
|
// comments
|
||||||
|
@ -7,4 +7,5 @@ test_utils::marks![
|
|||||||
not_applicable_outside_of_bind_pat
|
not_applicable_outside_of_bind_pat
|
||||||
test_not_inline_mut_variable
|
test_not_inline_mut_variable
|
||||||
test_not_applicable_if_variable_unused
|
test_not_applicable_if_variable_unused
|
||||||
|
change_visibility_field_false_positive
|
||||||
];
|
];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user