Skip to content

Commit 81ccebf

Browse files
committed
internal: Refactor lifetime completion context fields
1 parent 2fbd52c commit 81ccebf

File tree

2 files changed

+45
-43
lines changed

2 files changed

+45
-43
lines changed

crates/ide_completion/src/completions/lifetime.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,27 @@
1010
use hir::ScopeDef;
1111
use syntax::ast;
1212

13-
use crate::{completions::Completions, context::CompletionContext};
13+
use crate::{
14+
completions::Completions,
15+
context::{CompletionContext, LifetimeContext},
16+
};
1417

1518
/// Completes lifetimes.
1619
pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext) {
17-
if !ctx.lifetime_allowed {
18-
return;
19-
}
20+
let lp = match &ctx.lifetime_ctx {
21+
Some(LifetimeContext::Lifetime) => None,
22+
Some(LifetimeContext::LifetimeParam(param)) => param.as_ref(),
23+
_ => return,
24+
};
2025
let lp_string;
21-
let param_lifetime =
22-
match (&ctx.name_syntax, ctx.lifetime_param_syntax.as_ref().and_then(|lp| lp.lifetime())) {
23-
(Some(ast::NameLike::Lifetime(lt)), Some(lp)) if lp == lt.clone() => return,
24-
(Some(_), Some(lp)) => {
25-
lp_string = lp.to_string();
26-
Some(&*lp_string)
27-
}
28-
_ => None,
29-
};
26+
let param_lifetime = match (&ctx.name_syntax, lp.and_then(|lp| lp.lifetime())) {
27+
(Some(ast::NameLike::Lifetime(lt)), Some(lp)) if lp == lt.clone() => return,
28+
(Some(_), Some(lp)) => {
29+
lp_string = lp.to_string();
30+
Some(&*lp_string)
31+
}
32+
_ => None,
33+
};
3034

3135
ctx.scope.process_all_names(&mut |name, res| {
3236
if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
@@ -42,7 +46,7 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext)
4246

4347
/// Completes labels.
4448
pub(crate) fn complete_label(acc: &mut Completions, ctx: &CompletionContext) {
45-
if !ctx.is_label_ref {
49+
if !matches!(ctx.lifetime_ctx, Some(LifetimeContext::LabelRef)) {
4650
return;
4751
}
4852
ctx.scope.process_all_names(&mut |name, res| {

crates/ide_completion/src/context.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ pub(super) struct PatternContext {
6060
pub(super) is_param: Option<ParamKind>,
6161
}
6262

63+
#[derive(Debug)]
64+
pub(super) enum LifetimeContext {
65+
LifetimeParam(Option<ast::LifetimeParam>),
66+
Lifetime,
67+
LabelRef,
68+
LabelDef,
69+
}
70+
6371
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
6472
pub(crate) enum CallKind {
6573
Pat,
@@ -96,16 +104,12 @@ pub(crate) struct CompletionContext<'a> {
96104
pub(super) impl_def: Option<ast::Impl>,
97105
pub(super) name_syntax: Option<ast::NameLike>,
98106

99-
// potentially set if we are completing a lifetime
100-
pub(super) lifetime_param_syntax: Option<ast::LifetimeParam>,
101-
pub(super) lifetime_allowed: bool,
102-
pub(super) is_label_ref: bool,
103-
104107
pub(super) completion_location: Option<ImmediateLocation>,
105108
pub(super) prev_sibling: Option<ImmediatePrevSibling>,
106109
pub(super) attribute_under_caret: Option<ast::Attr>,
107110
pub(super) previous_token: Option<SyntaxToken>,
108111

112+
pub(super) lifetime_ctx: Option<LifetimeContext>,
109113
pub(super) pattern_ctx: Option<PatternContext>,
110114
pub(super) path_context: Option<PathCompletionContext>,
111115
pub(super) locals: Vec<(String, Local)>,
@@ -161,9 +165,7 @@ impl<'a> CompletionContext<'a> {
161165
function_def: None,
162166
impl_def: None,
163167
name_syntax: None,
164-
lifetime_param_syntax: None,
165-
lifetime_allowed: false,
166-
is_label_ref: false,
168+
lifetime_ctx: None,
167169
pattern_ctx: None,
168170
completion_location: None,
169171
prev_sibling: None,
@@ -294,8 +296,14 @@ impl<'a> CompletionContext<'a> {
294296
self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind)
295297
}
296298

297-
pub(crate) fn expects_assoc_item(&self) -> bool {
298-
matches!(self.completion_location, Some(ImmediateLocation::Trait | ImmediateLocation::Impl))
299+
pub(crate) fn dot_receiver(&self) -> Option<&ast::Expr> {
300+
match &self.completion_location {
301+
Some(
302+
ImmediateLocation::MethodCall { receiver, .. }
303+
| ImmediateLocation::FieldAccess { receiver, .. },
304+
) => receiver.as_ref(),
305+
_ => None,
306+
}
299307
}
300308

301309
pub(crate) fn has_dot_receiver(&self) -> bool {
@@ -306,14 +314,8 @@ impl<'a> CompletionContext<'a> {
306314
)
307315
}
308316

309-
pub(crate) fn dot_receiver(&self) -> Option<&ast::Expr> {
310-
match &self.completion_location {
311-
Some(
312-
ImmediateLocation::MethodCall { receiver, .. }
313-
| ImmediateLocation::FieldAccess { receiver, .. },
314-
) => receiver.as_ref(),
315-
_ => None,
316-
}
317+
pub(crate) fn expects_assoc_item(&self) -> bool {
318+
matches!(self.completion_location, Some(ImmediateLocation::Trait | ImmediateLocation::Impl))
317319
}
318320

319321
pub(crate) fn expects_non_trait_assoc_item(&self) -> bool {
@@ -676,19 +678,15 @@ impl<'a> CompletionContext<'a> {
676678
return;
677679
}
678680

679-
match_ast! {
681+
self.lifetime_ctx = Some(match_ast! {
680682
match parent {
681-
ast::LifetimeParam(_it) => {
682-
self.lifetime_allowed = true;
683-
self.lifetime_param_syntax =
684-
self.sema.find_node_at_offset_with_macros(original_file, offset);
685-
},
686-
ast::BreakExpr(_it) => self.is_label_ref = true,
687-
ast::ContinueExpr(_it) => self.is_label_ref = true,
688-
ast::Label(_it) => (),
689-
_ => self.lifetime_allowed = true,
683+
ast::LifetimeParam(_it) => LifetimeContext::LifetimeParam(self.sema.find_node_at_offset_with_macros(original_file, offset)),
684+
ast::BreakExpr(_it) => LifetimeContext::LabelRef,
685+
ast::ContinueExpr(_it) => LifetimeContext::LabelRef,
686+
ast::Label(_it) => LifetimeContext::LabelDef,
687+
_ => LifetimeContext::Lifetime,
690688
}
691-
}
689+
});
692690
}
693691
}
694692

0 commit comments

Comments
 (0)