Don't complete visibility accessors after existing ones

This commit is contained in:
Lukas Wirth 2021-06-16 17:45:58 +02:00
parent d338a80394
commit 1a8f76a224
4 changed files with 24 additions and 3 deletions

View File

@ -75,7 +75,9 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
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", "pub ");
}

View File

@ -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 {
matches!(self.prev_sibling, Some(ImmediatePrevSibling::IfExpr))
}

View File

@ -19,6 +19,7 @@ pub(crate) enum ImmediatePrevSibling {
IfExpr,
TraitDefName,
ImplDefType,
Visibility,
}
/// 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,
};
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! {
match prev_sibling {
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", None);
}
#[test]
fn test_vis_prev_sibling() {
check_prev_sibling(r"pub w$0", ImmediatePrevSibling::Visibility);
}
}

View File

@ -146,8 +146,6 @@ const CONST: () = ();
pub $0"#,
expect![[r##"
kw pub(crate)
kw pub
kw unsafe
kw fn
kw const