aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-06-16 16:45:58 +0100
committerLukas Wirth <[email protected]>2021-06-16 20:51:20 +0100
commit1a8f76a224aff472cf29bab828f313c19e31eb02 (patch)
treebf7b7c157e632be799980ed18e60b8a584b854ed /crates/ide_completion
parentd338a803941c2b0ac83decfcdfac33c09dfaa971 (diff)
Don't complete visibility accessors after existing ones
Diffstat (limited to 'crates/ide_completion')
-rw-r--r--crates/ide_completion/src/completions/keyword.rs4
-rw-r--r--crates/ide_completion/src/context.rs4
-rw-r--r--crates/ide_completion/src/patterns.rs17
-rw-r--r--crates/ide_completion/src/tests/item_list.rs2
4 files changed, 24 insertions, 3 deletions
diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs
index 2c42438d6..0bfdf9603 100644
--- a/crates/ide_completion/src/completions/keyword.rs
+++ b/crates/ide_completion/src/completions/keyword.rs
@@ -75,7 +75,9 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
75 return; 75 return;
76 } 76 }
77 77
78 if expects_item || ctx.expects_non_trait_assoc_item() || ctx.expect_record_field() { 78 if !ctx.has_visibility_prev_sibling()
79 && (expects_item || ctx.expects_non_trait_assoc_item() || ctx.expect_record_field())
80 {
79 add_keyword("pub(crate)", "pub(crate) "); 81 add_keyword("pub(crate)", "pub(crate) ");
80 add_keyword("pub", "pub "); 82 add_keyword("pub", "pub ");
81 } 83 }
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs
index a8437d81c..3885db702 100644
--- a/crates/ide_completion/src/context.rs
+++ b/crates/ide_completion/src/context.rs
@@ -302,6 +302,10 @@ impl<'a> CompletionContext<'a> {
302 ) 302 )
303 } 303 }
304 304
305 pub(crate) fn has_visibility_prev_sibling(&self) -> bool {
306 matches!(self.prev_sibling, Some(ImmediatePrevSibling::Visibility))
307 }
308
305 pub(crate) fn after_if(&self) -> bool { 309 pub(crate) fn after_if(&self) -> bool {
306 matches!(self.prev_sibling, Some(ImmediatePrevSibling::IfExpr)) 310 matches!(self.prev_sibling, Some(ImmediatePrevSibling::IfExpr))
307 } 311 }
diff --git a/crates/ide_completion/src/patterns.rs b/crates/ide_completion/src/patterns.rs
index 72e67e3c4..345977d48 100644
--- a/crates/ide_completion/src/patterns.rs
+++ b/crates/ide_completion/src/patterns.rs
@@ -19,6 +19,7 @@ pub(crate) enum ImmediatePrevSibling {
19 IfExpr, 19 IfExpr,
20 TraitDefName, 20 TraitDefName,
21 ImplDefType, 21 ImplDefType,
22 Visibility,
22} 23}
23 24
24/// Direct parent "thing" of what we are currently completing. 25/// 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
79 _ => node, 80 _ => node,
80 }; 81 };
81 let prev_sibling = non_trivia_sibling(node.into(), Direction::Prev)?.into_node()?; 82 let prev_sibling = non_trivia_sibling(node.into(), Direction::Prev)?.into_node()?;
83 if prev_sibling.kind() == ERROR {
84 let prev_sibling = prev_sibling.first_child()?;
85 let res = match_ast! {
86 match prev_sibling {
87 // vis followed by random ident will always error the parser
88 ast::Visibility(_it) => ImmediatePrevSibling::Visibility,
89 _ => return None,
90 }
91 };
92 return Some(res);
93 }
82 let res = match_ast! { 94 let res = match_ast! {
83 match prev_sibling { 95 match prev_sibling {
84 ast::ExprStmt(it) => { 96 ast::ExprStmt(it) => {
@@ -421,4 +433,9 @@ mod tests {
421 check_prev_sibling(r"fn foo() { if true {} w$0", ImmediatePrevSibling::IfExpr); 433 check_prev_sibling(r"fn foo() { if true {} w$0", ImmediatePrevSibling::IfExpr);
422 check_prev_sibling(r"fn foo() { if true {}; w$0", None); 434 check_prev_sibling(r"fn foo() { if true {}; w$0", None);
423 } 435 }
436
437 #[test]
438 fn test_vis_prev_sibling() {
439 check_prev_sibling(r"pub w$0", ImmediatePrevSibling::Visibility);
440 }
424} 441}
diff --git a/crates/ide_completion/src/tests/item_list.rs b/crates/ide_completion/src/tests/item_list.rs
index bd060a632..e7b77d7e7 100644
--- a/crates/ide_completion/src/tests/item_list.rs
+++ b/crates/ide_completion/src/tests/item_list.rs
@@ -146,8 +146,6 @@ const CONST: () = ();
146 146
147pub $0"#, 147pub $0"#,
148 expect![[r##" 148 expect![[r##"
149 kw pub(crate)
150 kw pub
151 kw unsafe 149 kw unsafe
152 kw fn 150 kw fn
153 kw const 151 kw const