mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
Don't complete visibility accessors after existing ones
This commit is contained in:
parent
d338a80394
commit
1a8f76a224
4 changed files with 24 additions and 3 deletions
|
@ -75,7 +75,9 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if expects_item || ctx.expects_non_trait_assoc_item() || ctx.expect_record_field() {
|
if !ctx.has_visibility_prev_sibling()
|
||||||
|
&& (expects_item || ctx.expects_non_trait_assoc_item() || ctx.expect_record_field())
|
||||||
|
{
|
||||||
add_keyword("pub(crate)", "pub(crate) ");
|
add_keyword("pub(crate)", "pub(crate) ");
|
||||||
add_keyword("pub", "pub ");
|
add_keyword("pub", "pub ");
|
||||||
}
|
}
|
||||||
|
|
|
@ -302,6 +302,10 @@ impl<'a> CompletionContext<'a> {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn has_visibility_prev_sibling(&self) -> bool {
|
||||||
|
matches!(self.prev_sibling, Some(ImmediatePrevSibling::Visibility))
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn after_if(&self) -> bool {
|
pub(crate) fn after_if(&self) -> bool {
|
||||||
matches!(self.prev_sibling, Some(ImmediatePrevSibling::IfExpr))
|
matches!(self.prev_sibling, Some(ImmediatePrevSibling::IfExpr))
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ pub(crate) enum ImmediatePrevSibling {
|
||||||
IfExpr,
|
IfExpr,
|
||||||
TraitDefName,
|
TraitDefName,
|
||||||
ImplDefType,
|
ImplDefType,
|
||||||
|
Visibility,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Direct parent "thing" of what we are currently completing.
|
/// Direct parent "thing" of what we are currently completing.
|
||||||
|
@ -79,6 +80,17 @@ pub(crate) fn determine_prev_sibling(name_like: &ast::NameLike) -> Option<Immedi
|
||||||
_ => node,
|
_ => node,
|
||||||
};
|
};
|
||||||
let prev_sibling = non_trivia_sibling(node.into(), Direction::Prev)?.into_node()?;
|
let prev_sibling = non_trivia_sibling(node.into(), Direction::Prev)?.into_node()?;
|
||||||
|
if prev_sibling.kind() == ERROR {
|
||||||
|
let prev_sibling = prev_sibling.first_child()?;
|
||||||
|
let res = match_ast! {
|
||||||
|
match prev_sibling {
|
||||||
|
// vis followed by random ident will always error the parser
|
||||||
|
ast::Visibility(_it) => ImmediatePrevSibling::Visibility,
|
||||||
|
_ => return None,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return Some(res);
|
||||||
|
}
|
||||||
let res = match_ast! {
|
let res = match_ast! {
|
||||||
match prev_sibling {
|
match prev_sibling {
|
||||||
ast::ExprStmt(it) => {
|
ast::ExprStmt(it) => {
|
||||||
|
@ -421,4 +433,9 @@ mod tests {
|
||||||
check_prev_sibling(r"fn foo() { if true {} w$0", ImmediatePrevSibling::IfExpr);
|
check_prev_sibling(r"fn foo() { if true {} w$0", ImmediatePrevSibling::IfExpr);
|
||||||
check_prev_sibling(r"fn foo() { if true {}; w$0", None);
|
check_prev_sibling(r"fn foo() { if true {}; w$0", None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_vis_prev_sibling() {
|
||||||
|
check_prev_sibling(r"pub w$0", ImmediatePrevSibling::Visibility);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -146,8 +146,6 @@ const CONST: () = ();
|
||||||
|
|
||||||
pub $0"#,
|
pub $0"#,
|
||||||
expect![[r##"
|
expect![[r##"
|
||||||
kw pub(crate)
|
|
||||||
kw pub
|
|
||||||
kw unsafe
|
kw unsafe
|
||||||
kw fn
|
kw fn
|
||||||
kw const
|
kw const
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue