Skip to content

Commit 32be2d6

Browse files
bors[bot]matklad
andauthored
Merge #5877
5877: Complete `pub` in fields r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents ad7e7d6 + 18b667c commit 32be2d6

File tree

7 files changed

+76
-8
lines changed

7 files changed

+76
-8
lines changed

crates/ide/src/completion/complete_keyword.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
129129
add_keyword(ctx, acc, "break", "break");
130130
}
131131
}
132-
if ctx.has_item_list_or_source_file_parent || ctx.has_impl_parent {
133-
add_keyword(ctx, acc, "pub", "pub ")
132+
if ctx.has_item_list_or_source_file_parent || ctx.has_impl_parent | ctx.has_field_list_parent {
133+
add_keyword(ctx, acc, "pub(crate)", "pub(crate) ");
134+
add_keyword(ctx, acc, "pub", "pub ");
134135
}
135136

136137
if !ctx.is_trivial_path {
@@ -227,6 +228,7 @@ mod tests {
227228
kw impl
228229
kw mod
229230
kw pub
231+
kw pub(crate)
230232
kw static
231233
kw struct
232234
kw trait
@@ -364,6 +366,7 @@ fn quux() -> i32 {
364366
kw const
365367
kw fn
366368
kw pub
369+
kw pub(crate)
367370
kw type
368371
kw unsafe
369372
"#]],
@@ -524,4 +527,20 @@ pub mod future {
524527
"#]],
525528
)
526529
}
530+
531+
#[test]
532+
fn before_field() {
533+
check(
534+
r#"
535+
struct Foo {
536+
<|>
537+
pub f: i32,
538+
}
539+
"#,
540+
expect![[r#"
541+
kw pub
542+
kw pub(crate)
543+
"#]],
544+
)
545+
}
527546
}

crates/ide/src/completion/complete_snippet.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ fn ${1:feature}() {
6565
.add_to(acc);
6666

6767
snippet(ctx, cap, "macro_rules", "macro_rules! $1 {\n\t($2) => {\n\t\t$0\n\t};\n}").add_to(acc);
68-
snippet(ctx, cap, "pub(crate)", "pub(crate) $0").add_to(acc);
6968
}
7069

7170
#[cfg(test)]
@@ -107,7 +106,6 @@ mod tests {
107106
"#,
108107
expect![[r#"
109108
sn macro_rules
110-
sn pub(crate)
111109
sn tfn (Test function)
112110
sn tmod (Test module)
113111
"#]],

crates/ide/src/completion/completion_context.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ use crate::{
1616
call_info::ActiveParameter,
1717
completion::{
1818
patterns::{
19-
has_bind_pat_parent, has_block_expr_parent, has_impl_as_prev_sibling, has_impl_parent,
20-
has_item_list_or_source_file_parent, has_ref_parent, has_trait_as_prev_sibling,
21-
has_trait_parent, if_is_prev, is_in_loop_body, is_match_arm, unsafe_is_prev,
19+
has_bind_pat_parent, has_block_expr_parent, has_field_list_parent,
20+
has_impl_as_prev_sibling, has_impl_parent, has_item_list_or_source_file_parent,
21+
has_ref_parent, has_trait_as_prev_sibling, has_trait_parent, if_is_prev,
22+
is_in_loop_body, is_match_arm, unsafe_is_prev,
2223
},
2324
CompletionConfig,
2425
},
@@ -84,6 +85,7 @@ pub(crate) struct CompletionContext<'a> {
8485
pub(super) in_loop_body: bool,
8586
pub(super) has_trait_parent: bool,
8687
pub(super) has_impl_parent: bool,
88+
pub(super) has_field_list_parent: bool,
8789
pub(super) trait_as_prev_sibling: bool,
8890
pub(super) impl_as_prev_sibling: bool,
8991
pub(super) is_match_arm: bool,
@@ -157,6 +159,7 @@ impl<'a> CompletionContext<'a> {
157159
block_expr_parent: false,
158160
has_trait_parent: false,
159161
has_impl_parent: false,
162+
has_field_list_parent: false,
160163
trait_as_prev_sibling: false,
161164
impl_as_prev_sibling: false,
162165
if_is_prev: false,
@@ -230,6 +233,7 @@ impl<'a> CompletionContext<'a> {
230233
self.in_loop_body = is_in_loop_body(syntax_element.clone());
231234
self.has_trait_parent = has_trait_parent(syntax_element.clone());
232235
self.has_impl_parent = has_impl_parent(syntax_element.clone());
236+
self.has_field_list_parent = has_field_list_parent(syntax_element.clone());
233237
self.impl_as_prev_sibling = has_impl_as_prev_sibling(syntax_element.clone());
234238
self.trait_as_prev_sibling = has_trait_as_prev_sibling(syntax_element.clone());
235239
self.is_match_arm = is_match_arm(syntax_element.clone());

crates/ide/src/completion/patterns.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ pub(crate) fn has_impl_parent(element: SyntaxElement) -> bool {
3434
fn test_has_impl_parent() {
3535
check_pattern_is_applicable(r"impl A { f<|> }", has_impl_parent);
3636
}
37+
pub(crate) fn has_field_list_parent(element: SyntaxElement) -> bool {
38+
not_same_range_ancestor(element).filter(|it| it.kind() == RECORD_FIELD_LIST).is_some()
39+
}
40+
#[test]
41+
fn test_has_field_list_parent() {
42+
check_pattern_is_applicable(r"struct Foo { f<|> }", has_field_list_parent);
43+
check_pattern_is_applicable(r"struct Foo { f<|> pub f: i32}", has_field_list_parent);
44+
}
3745

3846
pub(crate) fn has_block_expr_parent(element: SyntaxElement) -> bool {
3947
not_same_range_ancestor(element).filter(|it| it.kind() == BLOCK_EXPR).is_some()

crates/parser/src/grammar/types.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ pub(super) const TYPE_FIRST: TokenSet = paths::PATH_FIRST.union(token_set![
1818
T![dyn],
1919
]);
2020

21-
const TYPE_RECOVERY_SET: TokenSet = token_set![R_PAREN, COMMA, L_DOLLAR];
21+
const TYPE_RECOVERY_SET: TokenSet = token_set![
22+
T![')'],
23+
T![,],
24+
L_DOLLAR,
25+
// test_err struct_field_recover
26+
// struct S { f pub g: () }
27+
T![pub],
28+
];
2229

2330
pub(crate) fn type_(p: &mut Parser) {
2431
type_with_bounds_cond(p, true);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
SOURCE_FILE@0..25
2+
STRUCT@0..24
3+
STRUCT_KW@0..6 "struct"
4+
WHITESPACE@6..7 " "
5+
NAME@7..8
6+
IDENT@7..8 "S"
7+
WHITESPACE@8..9 " "
8+
RECORD_FIELD_LIST@9..24
9+
L_CURLY@9..10 "{"
10+
WHITESPACE@10..11 " "
11+
RECORD_FIELD@11..12
12+
NAME@11..12
13+
IDENT@11..12 "f"
14+
WHITESPACE@12..13 " "
15+
RECORD_FIELD@13..22
16+
VISIBILITY@13..16
17+
PUB_KW@13..16 "pub"
18+
WHITESPACE@16..17 " "
19+
NAME@17..18
20+
IDENT@17..18 "g"
21+
COLON@18..19 ":"
22+
WHITESPACE@19..20 " "
23+
TUPLE_TYPE@20..22
24+
L_PAREN@20..21 "("
25+
R_PAREN@21..22 ")"
26+
WHITESPACE@22..23 " "
27+
R_CURLY@23..24 "}"
28+
WHITESPACE@24..25 "\n"
29+
error 12..12: expected COLON
30+
error 12..12: expected type
31+
error 12..12: expected COMMA
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
struct S { f pub g: () }

0 commit comments

Comments
 (0)