Skip to content

Commit 254bfdd

Browse files
committed
Auto merge of rust-lang#12204 - Veykril:completions, r=Veykril
internal: Move keyword expressions to expr completions module
2 parents 4a4e9c0 + b2abe1b commit 254bfdd

File tree

8 files changed

+84
-99
lines changed

8 files changed

+84
-99
lines changed

crates/ide-completion/src/completions/expr.rs

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use hir::ScopeDef;
44
use ide_db::FxHashSet;
5+
use syntax::T;
56

67
use crate::{
78
context::{PathCompletionCtx, PathKind, PathQualifierCtx},
@@ -14,15 +15,16 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
1415
return;
1516
}
1617

17-
let (&is_absolute_path, qualifier) = match ctx.path_context() {
18-
Some(PathCompletionCtx {
19-
kind: PathKind::Expr { .. },
20-
is_absolute_path,
21-
qualifier,
22-
..
23-
}) => (is_absolute_path, qualifier),
24-
_ => return,
25-
};
18+
let (is_absolute_path, qualifier, in_block_expr, in_loop_body, in_functional_update) =
19+
match ctx.path_context() {
20+
Some(&PathCompletionCtx {
21+
kind: PathKind::Expr { in_block_expr, in_loop_body, in_functional_update },
22+
is_absolute_path,
23+
ref qualifier,
24+
..
25+
}) => (is_absolute_path, qualifier, in_block_expr, in_loop_body, in_functional_update),
26+
_ => return,
27+
};
2628

2729
let scope_def_applicable = |def| {
2830
use hir::{GenericParam::*, ModuleDef::*};
@@ -162,6 +164,64 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
162164
acc.add_resolution(ctx, name, def);
163165
}
164166
});
167+
168+
if !in_functional_update {
169+
let mut add_keyword =
170+
|kw, snippet| super::keyword::add_keyword(acc, ctx, kw, snippet);
171+
172+
if ctx.expects_expression() {
173+
if !in_block_expr {
174+
add_keyword("unsafe", "unsafe {\n $0\n}");
175+
}
176+
add_keyword("match", "match $1 {\n $0\n}");
177+
add_keyword("while", "while $1 {\n $0\n}");
178+
add_keyword("while let", "while let $1 = $2 {\n $0\n}");
179+
add_keyword("loop", "loop {\n $0\n}");
180+
add_keyword("if", "if $1 {\n $0\n}");
181+
add_keyword("if let", "if let $1 = $2 {\n $0\n}");
182+
add_keyword("for", "for $1 in $2 {\n $0\n}");
183+
add_keyword("true", "true");
184+
add_keyword("false", "false");
185+
}
186+
187+
if ctx.previous_token_is(T![if])
188+
|| ctx.previous_token_is(T![while])
189+
|| in_block_expr
190+
{
191+
add_keyword("let", "let");
192+
}
193+
194+
if ctx.after_if() {
195+
add_keyword("else", "else {\n $0\n}");
196+
add_keyword("else if", "else if $1 {\n $0\n}");
197+
}
198+
199+
if ctx.expects_ident_ref_expr() {
200+
add_keyword("mut", "mut ");
201+
}
202+
203+
if in_loop_body {
204+
if in_block_expr {
205+
add_keyword("continue", "continue;");
206+
add_keyword("break", "break;");
207+
} else {
208+
add_keyword("continue", "continue");
209+
add_keyword("break", "break");
210+
}
211+
}
212+
213+
if let Some(fn_def) = &ctx.function_def {
214+
add_keyword(
215+
"return",
216+
match (in_block_expr, fn_def.ret_type().is_some()) {
217+
(true, true) => "return ;",
218+
(true, false) => "return;",
219+
(false, true) => "return $0",
220+
(false, false) => "return",
221+
},
222+
);
223+
}
224+
}
165225
}
166226
}
167227
}

crates/ide-completion/src/completions/fn_param.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::{
1313
CompletionContext, CompletionItem, CompletionItemKind, Completions,
1414
};
1515

16+
// FIXME: Make this a submodule of [`pattern`]
1617
/// Complete repeated parameters, both name and type. For example, if all
1718
/// functions in a file have a `spam: &mut Spam` parameter, a completion with
1819
/// `spam: &mut Spam` insert text/label will be suggested.

crates/ide-completion/src/completions/keyword.rs

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
use syntax::T;
66

77
use crate::{
8-
context::{PathCompletionCtx, PathKind},
9-
patterns::ImmediateLocation,
10-
CompletionContext, CompletionItem, CompletionItemKind, Completions,
8+
context::PathKind, patterns::ImmediateLocation, CompletionContext, CompletionItem,
9+
CompletionItemKind, Completions,
1110
};
1211

1312
pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
@@ -83,75 +82,9 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
8382
add_keyword("struct", "struct $0");
8483
add_keyword("union", "union $1 {\n $0\n}");
8584
}
86-
87-
if ctx.expects_type() {
88-
return;
89-
}
90-
91-
if ctx.expects_expression() {
92-
if !has_block_expr_parent {
93-
add_keyword("unsafe", "unsafe {\n $0\n}");
94-
}
95-
add_keyword("match", "match $1 {\n $0\n}");
96-
add_keyword("while", "while $1 {\n $0\n}");
97-
add_keyword("while let", "while let $1 = $2 {\n $0\n}");
98-
add_keyword("loop", "loop {\n $0\n}");
99-
add_keyword("if", "if $1 {\n $0\n}");
100-
add_keyword("if let", "if let $1 = $2 {\n $0\n}");
101-
add_keyword("for", "for $1 in $2 {\n $0\n}");
102-
add_keyword("true", "true");
103-
add_keyword("false", "false");
104-
}
105-
106-
if ctx.previous_token_is(T![if]) || ctx.previous_token_is(T![while]) || has_block_expr_parent {
107-
add_keyword("let", "let");
108-
}
109-
110-
if ctx.after_if() {
111-
add_keyword("else", "else {\n $0\n}");
112-
add_keyword("else if", "else if $1 {\n $0\n}");
113-
}
114-
115-
if ctx.expects_ident_ref_expr() {
116-
add_keyword("mut", "mut ");
117-
}
118-
119-
let (can_be_stmt, in_loop_body) = match ctx.path_context() {
120-
Some(&PathCompletionCtx {
121-
is_absolute_path: false,
122-
kind: PathKind::Expr { in_block_expr, in_loop_body, .. },
123-
..
124-
}) => (in_block_expr, in_loop_body),
125-
_ => return,
126-
};
127-
128-
if in_loop_body {
129-
if can_be_stmt {
130-
add_keyword("continue", "continue;");
131-
add_keyword("break", "break;");
132-
} else {
133-
add_keyword("continue", "continue");
134-
add_keyword("break", "break");
135-
}
136-
}
137-
138-
let fn_def = match &ctx.function_def {
139-
Some(it) => it,
140-
None => return,
141-
};
142-
143-
add_keyword(
144-
"return",
145-
match (can_be_stmt, fn_def.ret_type().is_some()) {
146-
(true, true) => "return $0;",
147-
(true, false) => "return;",
148-
(false, true) => "return $0",
149-
(false, false) => "return",
150-
},
151-
)
15285
}
15386

154-
fn add_keyword(acc: &mut Completions, ctx: &CompletionContext, kw: &str, snippet: &str) {
87+
pub(super) fn add_keyword(acc: &mut Completions, ctx: &CompletionContext, kw: &str, snippet: &str) {
15588
let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw);
15689

15790
match ctx.config.snippet_cap {

crates/ide-completion/src/completions/trait_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ enum ImplCompletionKind {
5252
Const,
5353
}
5454

55+
// FIXME: Make this a submodule of [`item_list`]
5556
pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
5657
if let Some((kind, replacement_range, impl_def)) = completion_match(ctx) {
5758
if let Some(hir_impl) = ctx.sema.to_def(&impl_def) {

crates/ide-completion/src/completions/vis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
Completions,
88
};
99

10-
pub(crate) fn complete_vis(acc: &mut Completions, ctx: &CompletionContext) {
10+
pub(crate) fn complete_vis_path(acc: &mut Completions, ctx: &CompletionContext) {
1111
let (&is_absolute_path, qualifier, &has_in_token) = match ctx.path_context() {
1212
Some(PathCompletionCtx {
1313
kind: PathKind::Vis { has_in_token },

crates/ide-completion/src/context.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub(super) enum PathKind {
4848
Expr {
4949
in_block_expr: bool,
5050
in_loop_body: bool,
51+
in_functional_update: bool,
5152
},
5253
Type,
5354
Attr {
@@ -392,10 +393,6 @@ impl<'a> CompletionContext<'a> {
392393
matches!(self.path_context(), Some(PathCompletionCtx { kind: PathKind::Expr { .. }, .. }))
393394
}
394395

395-
pub(crate) fn expects_type(&self) -> bool {
396-
matches!(self.path_context(), Some(PathCompletionCtx { kind: PathKind::Type, .. }))
397-
}
398-
399396
pub(crate) fn is_non_trivial_path(&self) -> bool {
400397
matches!(
401398
self.path_context(),
@@ -1104,6 +1101,9 @@ impl<'a> CompletionContext<'a> {
11041101
})
11051102
.unwrap_or(false)
11061103
};
1104+
let is_in_func_update = |it: &SyntaxNode| {
1105+
it.parent().map_or(false, |it| ast::RecordExprFieldList::can_cast(it.kind()))
1106+
};
11071107

11081108
let kind = path.syntax().ancestors().find_map(|it| {
11091109
// using Option<Option<PathKind>> as extra controlflow
@@ -1114,8 +1114,8 @@ impl<'a> CompletionContext<'a> {
11141114
path_ctx.has_call_parens = it.syntax().parent().map_or(false, |it| ast::CallExpr::can_cast(it.kind()));
11151115
let in_block_expr = is_in_block(it.syntax());
11161116
let in_loop_body = is_in_loop_body(it.syntax());
1117-
1118-
Some(PathKind::Expr { in_block_expr, in_loop_body })
1117+
let in_functional_update = is_in_func_update(it.syntax());
1118+
Some(PathKind::Expr { in_block_expr, in_loop_body, in_functional_update })
11191119
},
11201120
ast::TupleStructPat(it) => {
11211121
path_ctx.has_call_parens = true;
@@ -1149,7 +1149,8 @@ impl<'a> CompletionContext<'a> {
11491149
return Some(parent.and_then(ast::MacroExpr::cast).map(|it| {
11501150
let in_loop_body = is_in_loop_body(it.syntax());
11511151
let in_block_expr = is_in_block(it.syntax());
1152-
PathKind::Expr { in_block_expr, in_loop_body }
1152+
let in_functional_update = is_in_func_update(it.syntax());
1153+
PathKind::Expr { in_block_expr, in_loop_body, in_functional_update }
11531154
}));
11541155
},
11551156
}

crates/ide-completion/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub fn completions(
173173
completions::r#type::complete_type_path(acc, ctx);
174174
completions::r#type::complete_inferred_type(acc, ctx);
175175
completions::use_::complete_use_tree(acc, ctx);
176-
completions::vis::complete_vis(acc, ctx);
176+
completions::vis::complete_vis_path(acc, ctx);
177177
}
178178

179179
Some(acc)

crates/ide-completion/src/tests/record.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,8 @@ fn main() {
168168
tt Sized
169169
bt u32
170170
kw crate::
171-
kw false
172-
kw for
173-
kw if
174-
kw if let
175-
kw loop
176-
kw match
177-
kw return
178171
kw self::
179172
kw super::
180-
kw true
181-
kw unsafe
182-
kw while
183-
kw while let
184173
"#]],
185174
);
186175
check(

0 commit comments

Comments
 (0)