diff options
author | Aleksey Kladov <[email protected]> | 2020-08-25 16:20:29 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-08-25 16:22:23 +0100 |
commit | 18b667cfcb31b2c5e421d12a34b34e83165603f4 (patch) | |
tree | c9a8da95da29f52b35e7f06171a34ecaeb9b8cfa | |
parent | 6b8bc132dc6dee538964fffa0720480beb5b25dd (diff) |
Complete `pub` in fields
7 files changed, 76 insertions, 8 deletions
diff --git a/crates/ide/src/completion/complete_keyword.rs b/crates/ide/src/completion/complete_keyword.rs index 95e4ff1ac..53ba76e0e 100644 --- a/crates/ide/src/completion/complete_keyword.rs +++ b/crates/ide/src/completion/complete_keyword.rs | |||
@@ -129,8 +129,9 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte | |||
129 | add_keyword(ctx, acc, "break", "break"); | 129 | add_keyword(ctx, acc, "break", "break"); |
130 | } | 130 | } |
131 | } | 131 | } |
132 | if ctx.has_item_list_or_source_file_parent || ctx.has_impl_parent { | 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", "pub ") | 133 | add_keyword(ctx, acc, "pub(crate)", "pub(crate) "); |
134 | add_keyword(ctx, acc, "pub", "pub "); | ||
134 | } | 135 | } |
135 | 136 | ||
136 | if !ctx.is_trivial_path { | 137 | if !ctx.is_trivial_path { |
@@ -227,6 +228,7 @@ mod tests { | |||
227 | kw impl | 228 | kw impl |
228 | kw mod | 229 | kw mod |
229 | kw pub | 230 | kw pub |
231 | kw pub(crate) | ||
230 | kw static | 232 | kw static |
231 | kw struct | 233 | kw struct |
232 | kw trait | 234 | kw trait |
@@ -364,6 +366,7 @@ fn quux() -> i32 { | |||
364 | kw const | 366 | kw const |
365 | kw fn | 367 | kw fn |
366 | kw pub | 368 | kw pub |
369 | kw pub(crate) | ||
367 | kw type | 370 | kw type |
368 | kw unsafe | 371 | kw unsafe |
369 | "#]], | 372 | "#]], |
@@ -524,4 +527,20 @@ pub mod future { | |||
524 | "#]], | 527 | "#]], |
525 | ) | 528 | ) |
526 | } | 529 | } |
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 | } | ||
527 | } | 546 | } |
diff --git a/crates/ide/src/completion/complete_snippet.rs b/crates/ide/src/completion/complete_snippet.rs index c3b03b199..4837d2910 100644 --- a/crates/ide/src/completion/complete_snippet.rs +++ b/crates/ide/src/completion/complete_snippet.rs | |||
@@ -65,7 +65,6 @@ fn ${1:feature}() { | |||
65 | .add_to(acc); | 65 | .add_to(acc); |
66 | 66 | ||
67 | snippet(ctx, cap, "macro_rules", "macro_rules! $1 {\n\t($2) => {\n\t\t$0\n\t};\n}").add_to(acc); | 67 | 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); | ||
69 | } | 68 | } |
70 | 69 | ||
71 | #[cfg(test)] | 70 | #[cfg(test)] |
@@ -107,7 +106,6 @@ mod tests { | |||
107 | "#, | 106 | "#, |
108 | expect![[r#" | 107 | expect![[r#" |
109 | sn macro_rules | 108 | sn macro_rules |
110 | sn pub(crate) | ||
111 | sn tfn (Test function) | 109 | sn tfn (Test function) |
112 | sn tmod (Test module) | 110 | sn tmod (Test module) |
113 | "#]], | 111 | "#]], |
diff --git a/crates/ide/src/completion/completion_context.rs b/crates/ide/src/completion/completion_context.rs index 5adac7ebc..47355d5dc 100644 --- a/crates/ide/src/completion/completion_context.rs +++ b/crates/ide/src/completion/completion_context.rs | |||
@@ -16,9 +16,10 @@ use crate::{ | |||
16 | call_info::ActiveParameter, | 16 | call_info::ActiveParameter, |
17 | completion::{ | 17 | completion::{ |
18 | patterns::{ | 18 | patterns::{ |
19 | has_bind_pat_parent, has_block_expr_parent, has_impl_as_prev_sibling, has_impl_parent, | 19 | has_bind_pat_parent, has_block_expr_parent, has_field_list_parent, |
20 | has_item_list_or_source_file_parent, has_ref_parent, has_trait_as_prev_sibling, | 20 | has_impl_as_prev_sibling, has_impl_parent, has_item_list_or_source_file_parent, |
21 | has_trait_parent, if_is_prev, is_in_loop_body, is_match_arm, unsafe_is_prev, | 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, | ||
22 | }, | 23 | }, |
23 | CompletionConfig, | 24 | CompletionConfig, |
24 | }, | 25 | }, |
@@ -84,6 +85,7 @@ pub(crate) struct CompletionContext<'a> { | |||
84 | pub(super) in_loop_body: bool, | 85 | pub(super) in_loop_body: bool, |
85 | pub(super) has_trait_parent: bool, | 86 | pub(super) has_trait_parent: bool, |
86 | pub(super) has_impl_parent: bool, | 87 | pub(super) has_impl_parent: bool, |
88 | pub(super) has_field_list_parent: bool, | ||
87 | pub(super) trait_as_prev_sibling: bool, | 89 | pub(super) trait_as_prev_sibling: bool, |
88 | pub(super) impl_as_prev_sibling: bool, | 90 | pub(super) impl_as_prev_sibling: bool, |
89 | pub(super) is_match_arm: bool, | 91 | pub(super) is_match_arm: bool, |
@@ -157,6 +159,7 @@ impl<'a> CompletionContext<'a> { | |||
157 | block_expr_parent: false, | 159 | block_expr_parent: false, |
158 | has_trait_parent: false, | 160 | has_trait_parent: false, |
159 | has_impl_parent: false, | 161 | has_impl_parent: false, |
162 | has_field_list_parent: false, | ||
160 | trait_as_prev_sibling: false, | 163 | trait_as_prev_sibling: false, |
161 | impl_as_prev_sibling: false, | 164 | impl_as_prev_sibling: false, |
162 | if_is_prev: false, | 165 | if_is_prev: false, |
@@ -230,6 +233,7 @@ impl<'a> CompletionContext<'a> { | |||
230 | self.in_loop_body = is_in_loop_body(syntax_element.clone()); | 233 | self.in_loop_body = is_in_loop_body(syntax_element.clone()); |
231 | self.has_trait_parent = has_trait_parent(syntax_element.clone()); | 234 | self.has_trait_parent = has_trait_parent(syntax_element.clone()); |
232 | self.has_impl_parent = has_impl_parent(syntax_element.clone()); | 235 | self.has_impl_parent = has_impl_parent(syntax_element.clone()); |
236 | self.has_field_list_parent = has_field_list_parent(syntax_element.clone()); | ||
233 | self.impl_as_prev_sibling = has_impl_as_prev_sibling(syntax_element.clone()); | 237 | self.impl_as_prev_sibling = has_impl_as_prev_sibling(syntax_element.clone()); |
234 | self.trait_as_prev_sibling = has_trait_as_prev_sibling(syntax_element.clone()); | 238 | self.trait_as_prev_sibling = has_trait_as_prev_sibling(syntax_element.clone()); |
235 | self.is_match_arm = is_match_arm(syntax_element.clone()); | 239 | self.is_match_arm = is_match_arm(syntax_element.clone()); |
diff --git a/crates/ide/src/completion/patterns.rs b/crates/ide/src/completion/patterns.rs index ffc97c076..c6ae589db 100644 --- a/crates/ide/src/completion/patterns.rs +++ b/crates/ide/src/completion/patterns.rs | |||
@@ -34,6 +34,14 @@ pub(crate) fn has_impl_parent(element: SyntaxElement) -> bool { | |||
34 | fn test_has_impl_parent() { | 34 | fn test_has_impl_parent() { |
35 | check_pattern_is_applicable(r"impl A { f<|> }", has_impl_parent); | 35 | check_pattern_is_applicable(r"impl A { f<|> }", has_impl_parent); |
36 | } | 36 | } |
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 | } | ||
37 | 45 | ||
38 | pub(crate) fn has_block_expr_parent(element: SyntaxElement) -> bool { | 46 | pub(crate) fn has_block_expr_parent(element: SyntaxElement) -> bool { |
39 | not_same_range_ancestor(element).filter(|it| it.kind() == BLOCK_EXPR).is_some() | 47 | not_same_range_ancestor(element).filter(|it| it.kind() == BLOCK_EXPR).is_some() |
diff --git a/crates/parser/src/grammar/types.rs b/crates/parser/src/grammar/types.rs index c876545f4..9d00eb9b9 100644 --- a/crates/parser/src/grammar/types.rs +++ b/crates/parser/src/grammar/types.rs | |||
@@ -18,7 +18,14 @@ pub(super) const TYPE_FIRST: TokenSet = paths::PATH_FIRST.union(token_set![ | |||
18 | T![dyn], | 18 | T![dyn], |
19 | ]); | 19 | ]); |
20 | 20 | ||
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 | ]; | ||
22 | 29 | ||
23 | pub(crate) fn type_(p: &mut Parser) { | 30 | pub(crate) fn type_(p: &mut Parser) { |
24 | type_with_bounds_cond(p, true); | 31 | type_with_bounds_cond(p, true); |
diff --git a/crates/syntax/test_data/parser/inline/err/0014_struct_field_recover.rast b/crates/syntax/test_data/parser/inline/err/0014_struct_field_recover.rast new file mode 100644 index 000000000..ba8e50993 --- /dev/null +++ b/crates/syntax/test_data/parser/inline/err/0014_struct_field_recover.rast | |||
@@ -0,0 +1,31 @@ | |||
1 | [email protected] | ||
2 | [email protected] | ||
3 | [email protected] "struct" | ||
4 | [email protected] " " | ||
5 | [email protected] | ||
6 | [email protected] "S" | ||
7 | [email protected] " " | ||
8 | [email protected] | ||
9 | [email protected] "{" | ||
10 | [email protected] " " | ||
11 | [email protected] | ||
12 | [email protected] | ||
13 | [email protected] "f" | ||
14 | [email protected] " " | ||
15 | [email protected] | ||
16 | [email protected] | ||
17 | [email protected] "pub" | ||
18 | [email protected] " " | ||
19 | [email protected] | ||
20 | [email protected] "g" | ||
21 | [email protected] ":" | ||
22 | [email protected] " " | ||
23 | [email protected] | ||
24 | [email protected] "(" | ||
25 | [email protected] ")" | ||
26 | [email protected] " " | ||
27 | [email protected] "}" | ||
28 | [email protected] "\n" | ||
29 | error 12..12: expected COLON | ||
30 | error 12..12: expected type | ||
31 | error 12..12: expected COMMA | ||
diff --git a/crates/syntax/test_data/parser/inline/err/0014_struct_field_recover.rs b/crates/syntax/test_data/parser/inline/err/0014_struct_field_recover.rs new file mode 100644 index 000000000..da32227ad --- /dev/null +++ b/crates/syntax/test_data/parser/inline/err/0014_struct_field_recover.rs | |||
@@ -0,0 +1 @@ | |||
struct S { f pub g: () } | |||