Skip to content

Commit 7de925b

Browse files
committed
Collapse more CompletionContext booleans into enums
1 parent d6ed315 commit 7de925b

File tree

6 files changed

+133
-98
lines changed

6 files changed

+133
-98
lines changed

crates/ide_completion/src/completions/flyimport.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
114114
|| ctx.attribute_under_caret.is_some()
115115
|| ctx.mod_declaration_under_caret.is_some()
116116
|| ctx.record_lit_syntax.is_some()
117-
|| ctx.has_trait_parent
118-
|| ctx.has_impl_parent
117+
|| ctx.has_impl_or_trait_parent()
119118
{
120119
return None;
121120
}

crates/ide_completion/src/completions/keyword.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,35 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
4949
return;
5050
}
5151

52-
let has_trait_or_impl_parent = ctx.has_impl_parent || ctx.has_trait_parent;
53-
if ctx.trait_as_prev_sibling || ctx.impl_as_prev_sibling {
52+
let has_trait_or_impl_parent = ctx.has_impl_or_trait_parent();
53+
let has_block_expr_parent = ctx.has_block_expr_parent();
54+
let has_item_list_parent = ctx.has_item_list_parent();
55+
if ctx.has_impl_or_trait_prev_sibling() {
5456
add_keyword(ctx, acc, "where", "where ");
5557
return;
5658
}
5759
if ctx.previous_token_is(T![unsafe]) {
58-
if ctx.has_item_list_or_source_file_parent || ctx.block_expr_parent {
60+
if has_item_list_parent || has_block_expr_parent {
5961
add_keyword(ctx, acc, "fn", "fn $1($2) {\n $0\n}")
6062
}
6163

62-
if (ctx.has_item_list_or_source_file_parent) || ctx.block_expr_parent {
64+
if has_item_list_parent || has_block_expr_parent {
6365
add_keyword(ctx, acc, "trait", "trait $1 {\n $0\n}");
6466
add_keyword(ctx, acc, "impl", "impl $1 {\n $0\n}");
6567
}
6668

6769
return;
6870
}
69-
if ctx.has_item_list_or_source_file_parent || has_trait_or_impl_parent || ctx.block_expr_parent
70-
{
71+
if has_item_list_parent || has_trait_or_impl_parent || has_block_expr_parent {
7172
add_keyword(ctx, acc, "fn", "fn $1($2) {\n $0\n}");
7273
}
73-
if (ctx.has_item_list_or_source_file_parent) || ctx.block_expr_parent {
74+
if has_item_list_parent || has_block_expr_parent {
7475
add_keyword(ctx, acc, "use", "use ");
7576
add_keyword(ctx, acc, "impl", "impl $1 {\n $0\n}");
7677
add_keyword(ctx, acc, "trait", "trait $1 {\n $0\n}");
7778
}
7879

79-
if ctx.has_item_list_or_source_file_parent {
80+
if has_item_list_parent {
8081
add_keyword(ctx, acc, "enum", "enum $1 {\n $0\n}");
8182
add_keyword(ctx, acc, "struct", "struct $0");
8283
add_keyword(ctx, acc, "union", "union $1 {\n $0\n}");
@@ -92,35 +93,31 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
9293
add_keyword(ctx, acc, "for", "for $1 in $2 {\n $0\n}");
9394
}
9495

95-
if ctx.previous_token_is(T![if]) || ctx.previous_token_is(T![while]) || ctx.block_expr_parent {
96+
if ctx.previous_token_is(T![if]) || ctx.previous_token_is(T![while]) || has_block_expr_parent {
9697
add_keyword(ctx, acc, "let", "let ");
9798
}
9899

99100
if ctx.after_if {
100101
add_keyword(ctx, acc, "else", "else {\n $0\n}");
101102
add_keyword(ctx, acc, "else if", "else if $1 {\n $0\n}");
102103
}
103-
if (ctx.has_item_list_or_source_file_parent) || ctx.block_expr_parent {
104+
if has_item_list_parent || has_block_expr_parent {
104105
add_keyword(ctx, acc, "mod", "mod $0");
105106
}
106-
if ctx.bind_pat_parent || ctx.ref_pat_parent {
107+
if ctx.has_ident_or_ref_pat_parent() {
107108
add_keyword(ctx, acc, "mut", "mut ");
108109
}
109-
if ctx.has_item_list_or_source_file_parent || has_trait_or_impl_parent || ctx.block_expr_parent
110-
{
110+
if has_item_list_parent || has_trait_or_impl_parent || has_block_expr_parent {
111111
add_keyword(ctx, acc, "const", "const ");
112112
add_keyword(ctx, acc, "type", "type ");
113113
}
114-
if (ctx.has_item_list_or_source_file_parent) || ctx.block_expr_parent {
114+
if has_item_list_parent || has_block_expr_parent {
115115
add_keyword(ctx, acc, "static", "static ");
116116
};
117-
if (ctx.has_item_list_or_source_file_parent) || ctx.block_expr_parent {
117+
if has_item_list_parent || has_block_expr_parent {
118118
add_keyword(ctx, acc, "extern", "extern ");
119119
}
120-
if ctx.has_item_list_or_source_file_parent
121-
|| has_trait_or_impl_parent
122-
|| ctx.block_expr_parent
123-
|| ctx.is_match_arm
120+
if has_item_list_parent || has_trait_or_impl_parent || has_block_expr_parent || ctx.is_match_arm
124121
{
125122
add_keyword(ctx, acc, "unsafe", "unsafe ");
126123
}
@@ -133,15 +130,15 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
133130
add_keyword(ctx, acc, "break", "break");
134131
}
135132
}
136-
if ctx.has_item_list_or_source_file_parent || ctx.has_impl_parent | ctx.has_field_list_parent {
133+
if has_item_list_parent || ctx.has_impl_parent() || ctx.has_field_list_parent() {
137134
add_keyword(ctx, acc, "pub(crate)", "pub(crate) ");
138135
add_keyword(ctx, acc, "pub", "pub ");
139136
}
140137

141138
if !ctx.is_trivial_path {
142139
return;
143140
}
144-
let fn_def = match &ctx.function_syntax {
141+
let fn_def = match &ctx.function_def {
145142
Some(it) => it,
146143
None => return,
147144
};

crates/ide_completion/src/completions/snippet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str)
1414
}
1515

1616
pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
17-
if !(ctx.is_trivial_path && ctx.function_syntax.is_some()) {
17+
if !(ctx.is_trivial_path && ctx.function_def.is_some()) {
1818
return;
1919
}
2020
let cap = match ctx.config.snippet_cap {

crates/ide_completion/src/completions/unqualified_path.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
1313
|| ctx.record_pat_syntax.is_some()
1414
|| ctx.attribute_under_caret.is_some()
1515
|| ctx.mod_declaration_under_caret.is_some()
16-
|| ctx.has_impl_parent
17-
|| ctx.has_trait_parent
16+
|| ctx.has_impl_or_trait_parent()
1817
{
1918
return;
2019
}

crates/ide_completion/src/context.rs

Lines changed: 94 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ pub(crate) enum PatternRefutability {
3131
Irrefutable,
3232
}
3333

34+
/// Direct parent container of the cursor position
35+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
36+
pub(crate) enum ImmediateLocation {
37+
Impl,
38+
Trait,
39+
RecordFieldList,
40+
RefPatOrExpr,
41+
IdentPat,
42+
BlockExpr,
43+
ItemList,
44+
}
45+
46+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
47+
pub enum PrevSibling {
48+
Trait,
49+
Impl,
50+
}
51+
3452
/// `CompletionContext` is created early during completion to figure out, where
3553
/// exactly is the cursor, syntax-wise.
3654
#[derive(Debug)]
@@ -48,14 +66,19 @@ pub(crate) struct CompletionContext<'a> {
4866
pub(super) expected_name: Option<NameOrNameRef>,
4967
pub(super) expected_type: Option<Type>,
5068
pub(super) name_ref_syntax: Option<ast::NameRef>,
51-
pub(super) function_syntax: Option<ast::Fn>,
69+
5270
pub(super) use_item_syntax: Option<ast::Use>,
53-
pub(super) record_lit_syntax: Option<ast::RecordExpr>,
54-
pub(super) record_pat_syntax: Option<ast::RecordPat>,
55-
pub(super) record_field_syntax: Option<ast::RecordExprField>,
71+
72+
/// The parent function of the cursor position if it exists.
73+
pub(super) function_def: Option<ast::Fn>,
5674
/// The parent impl of the cursor position if it exists.
5775
pub(super) impl_def: Option<ast::Impl>,
5876

77+
/// RecordExpr the token is a field of
78+
pub(super) record_lit_syntax: Option<ast::RecordExpr>,
79+
/// RecordPat the token is a field of
80+
pub(super) record_pat_syntax: Option<ast::RecordPat>,
81+
5982
// potentially set if we are completing a lifetime
6083
pub(super) lifetime_syntax: Option<ast::Lifetime>,
6184
pub(super) lifetime_param_syntax: Option<ast::LifetimeParam>,
@@ -66,6 +89,8 @@ pub(crate) struct CompletionContext<'a> {
6689
pub(super) is_pat_or_const: Option<PatternRefutability>,
6790
pub(super) is_param: bool,
6891

92+
pub(super) completion_location: Option<ImmediateLocation>,
93+
6994
/// FIXME: `ActiveParameter` is string-based, which is very very wrong
7095
pub(super) active_parameter: Option<ActiveParameter>,
7196
/// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
@@ -94,20 +119,12 @@ pub(crate) struct CompletionContext<'a> {
94119
pub(super) locals: Vec<(String, Local)>,
95120

96121
pub(super) mod_declaration_under_caret: Option<ast::Module>,
97-
pub(super) has_trait_parent: bool,
98-
pub(super) has_impl_parent: bool,
99122

100123
// keyword patterns
101124
pub(super) previous_token: Option<SyntaxToken>,
102-
pub(super) block_expr_parent: bool,
103-
pub(super) bind_pat_parent: bool,
104-
pub(super) ref_pat_parent: bool,
105125
pub(super) in_loop_body: bool,
106-
pub(super) has_field_list_parent: bool,
107-
pub(super) trait_as_prev_sibling: bool,
108-
pub(super) impl_as_prev_sibling: bool,
126+
pub(super) prev_sibling: Option<PrevSibling>,
109127
pub(super) is_match_arm: bool,
110-
pub(super) has_item_list_or_source_file_parent: bool,
111128
pub(super) incomplete_let: bool,
112129

113130
no_completion_required: bool,
@@ -159,11 +176,10 @@ impl<'a> CompletionContext<'a> {
159176
name_ref_syntax: None,
160177
lifetime_syntax: None,
161178
lifetime_param_syntax: None,
162-
function_syntax: None,
179+
function_def: None,
163180
use_item_syntax: None,
164181
record_lit_syntax: None,
165182
record_pat_syntax: None,
166-
record_field_syntax: None,
167183
impl_def: None,
168184
active_parameter: ActiveParameter::at(db, position),
169185
is_label_ref: false,
@@ -185,17 +201,10 @@ impl<'a> CompletionContext<'a> {
185201
attribute_under_caret: None,
186202
mod_declaration_under_caret: None,
187203
previous_token: None,
188-
block_expr_parent: false,
189-
bind_pat_parent: false,
190-
ref_pat_parent: false,
191204
in_loop_body: false,
192-
has_trait_parent: false,
193-
has_impl_parent: false,
194-
has_field_list_parent: false,
195-
trait_as_prev_sibling: false,
196-
impl_as_prev_sibling: false,
205+
completion_location: None,
206+
prev_sibling: None,
197207
is_match_arm: false,
198-
has_item_list_or_source_file_parent: false,
199208
no_completion_required: false,
200209
incomplete_let: false,
201210
locals,
@@ -274,23 +283,68 @@ impl<'a> CompletionContext<'a> {
274283
self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind)
275284
}
276285

286+
pub(crate) fn has_impl_or_trait_parent(&self) -> bool {
287+
matches!(
288+
self.completion_location,
289+
Some(ImmediateLocation::Trait) | Some(ImmediateLocation::Impl)
290+
)
291+
}
292+
293+
pub(crate) fn has_block_expr_parent(&self) -> bool {
294+
matches!(self.completion_location, Some(ImmediateLocation::BlockExpr))
295+
}
296+
297+
pub(crate) fn has_item_list_parent(&self) -> bool {
298+
matches!(self.completion_location, Some(ImmediateLocation::ItemList))
299+
}
300+
301+
pub(crate) fn has_ident_or_ref_pat_parent(&self) -> bool {
302+
matches!(
303+
self.completion_location,
304+
Some(ImmediateLocation::IdentPat) | Some(ImmediateLocation::RefPatOrExpr)
305+
)
306+
}
307+
308+
pub(crate) fn has_impl_parent(&self) -> bool {
309+
matches!(self.completion_location, Some(ImmediateLocation::Impl))
310+
}
311+
312+
pub(crate) fn has_field_list_parent(&self) -> bool {
313+
matches!(self.completion_location, Some(ImmediateLocation::RecordFieldList))
314+
}
315+
316+
pub(crate) fn has_impl_or_trait_prev_sibling(&self) -> bool {
317+
self.prev_sibling.is_some()
318+
}
319+
277320
fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: TextSize) {
278321
let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap();
279322
let syntax_element = NodeOrToken::Token(fake_ident_token);
280323
self.previous_token = previous_token(syntax_element.clone());
281-
self.block_expr_parent = has_block_expr_parent(syntax_element.clone());
282-
self.bind_pat_parent = has_bind_pat_parent(syntax_element.clone());
283-
self.ref_pat_parent = has_ref_parent(syntax_element.clone());
284324
self.in_loop_body = is_in_loop_body(syntax_element.clone());
285-
self.has_trait_parent = has_trait_parent(syntax_element.clone());
286-
self.has_impl_parent = has_impl_parent(syntax_element.clone());
287-
self.has_field_list_parent = has_field_list_parent(syntax_element.clone());
288-
self.impl_as_prev_sibling = has_impl_as_prev_sibling(syntax_element.clone());
289-
self.trait_as_prev_sibling = has_trait_as_prev_sibling(syntax_element.clone());
290325
self.is_match_arm = is_match_arm(syntax_element.clone());
326+
if has_impl_as_prev_sibling(syntax_element.clone()) {
327+
self.prev_sibling = Some(PrevSibling::Impl)
328+
} else if has_trait_as_prev_sibling(syntax_element.clone()) {
329+
self.prev_sibling = Some(PrevSibling::Trait)
330+
}
331+
332+
if has_block_expr_parent(syntax_element.clone()) {
333+
self.completion_location = Some(ImmediateLocation::BlockExpr);
334+
} else if has_bind_pat_parent(syntax_element.clone()) {
335+
self.completion_location = Some(ImmediateLocation::IdentPat);
336+
} else if has_ref_parent(syntax_element.clone()) {
337+
self.completion_location = Some(ImmediateLocation::RefPatOrExpr);
338+
} else if has_impl_parent(syntax_element.clone()) {
339+
self.completion_location = Some(ImmediateLocation::Impl);
340+
} else if has_field_list_parent(syntax_element.clone()) {
341+
self.completion_location = Some(ImmediateLocation::RecordFieldList);
342+
} else if has_trait_parent(syntax_element.clone()) {
343+
self.completion_location = Some(ImmediateLocation::Trait);
344+
} else if has_item_list_or_source_file_parent(syntax_element.clone()) {
345+
self.completion_location = Some(ImmediateLocation::ItemList);
346+
}
291347

292-
self.has_item_list_or_source_file_parent =
293-
has_item_list_or_source_file_parent(syntax_element.clone());
294348
self.mod_declaration_under_caret =
295349
find_node_at_offset::<ast::Module>(&file_with_fake_ident, offset)
296350
.filter(|module| module.item_list().is_none());
@@ -542,31 +596,20 @@ impl<'a> CompletionContext<'a> {
542596
.last()
543597
.unwrap();
544598

545-
match top_node.parent().map(|it| it.kind()) {
546-
Some(SOURCE_FILE) | Some(ITEM_LIST) => {
547-
self.is_new_item = true;
548-
return;
549-
}
550-
_ => (),
599+
if matches!(top_node.parent().map(|it| it.kind()), Some(SOURCE_FILE) | Some(ITEM_LIST)) {
600+
self.is_new_item = true;
601+
return;
551602
}
552603

553604
self.use_item_syntax =
554605
self.sema.token_ancestors_with_macros(self.token.clone()).find_map(ast::Use::cast);
555606

556-
self.function_syntax = self
607+
self.function_def = self
557608
.sema
558609
.token_ancestors_with_macros(self.token.clone())
559610
.take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE)
560611
.find_map(ast::Fn::cast);
561612

562-
self.record_field_syntax = self
563-
.sema
564-
.token_ancestors_with_macros(self.token.clone())
565-
.take_while(|it| {
566-
it.kind() != SOURCE_FILE && it.kind() != MODULE && it.kind() != CALL_EXPR
567-
})
568-
.find_map(ast::RecordExprField::cast);
569-
570613
let parent = match name_ref.syntax().parent() {
571614
Some(it) => it,
572615
None => return,
@@ -639,6 +682,7 @@ impl<'a> CompletionContext<'a> {
639682
}
640683
}
641684
}
685+
642686
if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) {
643687
// The receiver comes before the point of insertion of the fake
644688
// ident, so it should have the same range in the non-modified file
@@ -656,6 +700,7 @@ impl<'a> CompletionContext<'a> {
656700
false
657701
};
658702
}
703+
659704
if let Some(method_call_expr) = ast::MethodCallExpr::cast(parent) {
660705
// As above
661706
self.dot_receiver = method_call_expr

0 commit comments

Comments
 (0)