Skip to content

Commit acdfab6

Browse files
committed
Auto merge of rust-lang#17513 - roife:fix-issue-17500, r=Veykril
fix: completions after async kw fix rust-lang#17500 ### Changes 1. fix completions after async kw 2. fix completions for `async` kw in trait
2 parents fde5564 + 71bc013 commit acdfab6

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

src/tools/rust-analyzer/crates/ide-completion/src/completions/item_list.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,37 @@ fn add_keywords(acc: &mut Completions, ctx: &CompletionContext<'_>, kind: Option
8282
let no_vis_qualifiers = ctx.qualifier_ctx.vis_node.is_none();
8383
let in_block = kind.is_none();
8484

85+
let missing_qualifiers = [
86+
ctx.qualifier_ctx.unsafe_tok.is_none().then_some(("unsafe", "unsafe $0")),
87+
ctx.qualifier_ctx.async_tok.is_none().then_some(("async", "async $0")),
88+
];
89+
8590
if !in_trait_impl {
86-
if ctx.qualifier_ctx.unsafe_tok.is_some() {
91+
// handle qualifier tokens
92+
if missing_qualifiers.iter().any(Option::is_none) {
93+
// only complete missing qualifiers
94+
missing_qualifiers.iter().filter_map(|x| *x).for_each(|(kw, snippet)| {
95+
add_keyword(kw, snippet);
96+
});
97+
8798
if in_item_list || in_assoc_non_trait_impl {
8899
add_keyword("fn", "fn $1($2) {\n $0\n}");
89100
}
90-
if in_item_list {
101+
102+
if ctx.qualifier_ctx.unsafe_tok.is_some() && in_item_list {
91103
add_keyword("trait", "trait $1 {\n $0\n}");
92104
if no_vis_qualifiers {
93105
add_keyword("impl", "impl $1 {\n $0\n}");
94106
}
95107
}
108+
96109
return;
97110
}
98111

99112
if in_item_list {
100113
add_keyword("enum", "enum $1 {\n $0\n}");
101114
add_keyword("mod", "mod $0");
102115
add_keyword("static", "static $0");
103-
add_keyword("async", "async $0");
104116
add_keyword("struct", "struct $0");
105117
add_keyword("trait", "trait $1 {\n $0\n}");
106118
add_keyword("union", "union $1 {\n $0\n}");
@@ -129,6 +141,7 @@ fn add_keywords(acc: &mut Completions, ctx: &CompletionContext<'_>, kind: Option
129141
add_keyword("fn", "fn $1($2) {\n $0\n}");
130142
add_keyword("unsafe", "unsafe $0");
131143
add_keyword("const", "const $0");
144+
add_keyword("async", "async $0");
132145
}
133146
}
134147
}

src/tools/rust-analyzer/crates/ide-completion/src/completions/keyword.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ mod tests {
5757
check(
5858
r"fn my_fn() { unsafe $0 }",
5959
expect![[r#"
60+
kw async
6061
kw fn
6162
kw impl
6263
kw trait

src/tools/rust-analyzer/crates/ide-completion/src/context.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ pub(crate) enum Visible {
4646
/// Existing qualifiers for the thing we are currently completing.
4747
#[derive(Debug, Default)]
4848
pub(crate) struct QualifierCtx {
49+
// TODO: Add try_tok and default_tok
50+
pub(crate) async_tok: Option<SyntaxToken>,
4951
pub(crate) unsafe_tok: Option<SyntaxToken>,
5052
pub(crate) vis_node: Option<ast::Visibility>,
5153
}
5254

5355
impl QualifierCtx {
5456
pub(crate) fn none(&self) -> bool {
55-
self.unsafe_tok.is_none() && self.vis_node.is_none()
57+
self.async_tok.is_none() && self.unsafe_tok.is_none() && self.vis_node.is_none()
5658
}
5759
}
5860

src/tools/rust-analyzer/crates/ide-completion/src/context/analysis.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,10 +1287,15 @@ fn classify_name_ref(
12871287
syntax::algo::non_trivia_sibling(top.clone().into(), syntax::Direction::Prev)
12881288
{
12891289
if error_node.kind() == SyntaxKind::ERROR {
1290-
qualifier_ctx.unsafe_tok = error_node
1291-
.children_with_tokens()
1292-
.filter_map(NodeOrToken::into_token)
1293-
.find(|it| it.kind() == T![unsafe]);
1290+
for token in
1291+
error_node.children_with_tokens().filter_map(NodeOrToken::into_token)
1292+
{
1293+
match token.kind() {
1294+
SyntaxKind::UNSAFE_KW => qualifier_ctx.unsafe_tok = Some(token),
1295+
SyntaxKind::ASYNC_KW => qualifier_ctx.async_tok = Some(token),
1296+
_ => {}
1297+
}
1298+
}
12941299
qualifier_ctx.vis_node = error_node.children().find_map(ast::Visibility::cast);
12951300
}
12961301
}

src/tools/rust-analyzer/crates/ide-completion/src/tests/item_list.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,25 @@ fn after_unsafe_token() {
123123
check(
124124
r#"unsafe $0"#,
125125
expect![[r#"
126+
kw async
126127
kw fn
127128
kw impl
128129
kw trait
129130
"#]],
130131
);
131132
}
132133

134+
#[test]
135+
fn after_async_token() {
136+
check(
137+
r#"async $0"#,
138+
expect![[r#"
139+
kw fn
140+
kw unsafe
141+
"#]],
142+
);
143+
}
144+
133145
#[test]
134146
fn after_visibility() {
135147
check(
@@ -157,6 +169,7 @@ fn after_visibility_unsafe() {
157169
check(
158170
r#"pub unsafe $0"#,
159171
expect![[r#"
172+
kw async
160173
kw fn
161174
kw trait
162175
"#]],
@@ -170,6 +183,7 @@ fn in_impl_assoc_item_list() {
170183
expect![[r#"
171184
ma makro!(…) macro_rules! makro
172185
md module
186+
kw async
173187
kw const
174188
kw crate::
175189
kw fn
@@ -189,6 +203,7 @@ fn in_impl_assoc_item_list_after_attr() {
189203
expect![[r#"
190204
ma makro!(…) macro_rules! makro
191205
md module
206+
kw async
192207
kw const
193208
kw crate::
194209
kw fn
@@ -208,6 +223,7 @@ fn in_trait_assoc_item_list() {
208223
expect![[r#"
209224
ma makro!(…) macro_rules! makro
210225
md module
226+
kw async
211227
kw const
212228
kw crate::
213229
kw fn
@@ -225,6 +241,7 @@ fn in_trait_assoc_fn_missing_body() {
225241
expect![[r#"
226242
ma makro!(…) macro_rules! makro
227243
md module
244+
kw async
228245
kw const
229246
kw crate::
230247
kw fn
@@ -242,6 +259,7 @@ fn in_trait_assoc_const_missing_body() {
242259
expect![[r#"
243260
ma makro!(…) macro_rules! makro
244261
md module
262+
kw async
245263
kw const
246264
kw crate::
247265
kw fn
@@ -259,6 +277,7 @@ fn in_trait_assoc_type_aliases_missing_ty() {
259277
expect![[r#"
260278
ma makro!(…) macro_rules! makro
261279
md module
280+
kw async
262281
kw const
263282
kw crate::
264283
kw fn

0 commit comments

Comments
 (0)