Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit bcf10cd

Browse files
committed
internal: Remove previous_token field from CompletionContext
1 parent 9048332 commit bcf10cd

File tree

7 files changed

+50
-37
lines changed

7 files changed

+50
-37
lines changed

crates/ide-completion/src/completions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,8 @@ pub(super) fn complete_name_ref(
541541
NameRefKind::Keyword(item) => {
542542
keyword::complete_for_and_where(acc, ctx, item);
543543
}
544-
NameRefKind::RecordExpr(record_expr) => {
545-
record::complete_record_expr_fields(acc, ctx, record_expr);
544+
NameRefKind::RecordExpr { dot_prefix, expr } => {
545+
record::complete_record_expr_fields(acc, ctx, expr, dot_prefix);
546546
}
547547
NameRefKind::Pattern(pattern_ctx) => complete_patterns(acc, ctx, pattern_ctx),
548548
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub(crate) use self::derive::complete_derive_path;
3333
pub(crate) fn complete_known_attribute_input(
3434
acc: &mut Completions,
3535
ctx: &CompletionContext,
36+
&colon_prefix: &bool,
3637
fake_attribute_under_caret: &ast::Attr,
3738
) -> Option<()> {
3839
let attribute = fake_attribute_under_caret;
@@ -47,7 +48,9 @@ pub(crate) fn complete_known_attribute_input(
4748

4849
match path.text().as_str() {
4950
"repr" => repr::complete_repr(acc, ctx, tt),
50-
"feature" => lint::complete_lint(acc, ctx, &parse_tt_as_comma_sep_paths(tt)?, FEATURES),
51+
"feature" => {
52+
lint::complete_lint(acc, ctx, colon_prefix, &parse_tt_as_comma_sep_paths(tt)?, FEATURES)
53+
}
5154
"allow" | "warn" | "deny" | "forbid" => {
5255
let existing_lints = parse_tt_as_comma_sep_paths(tt)?;
5356

@@ -60,7 +63,7 @@ pub(crate) fn complete_known_attribute_input(
6063
.cloned()
6164
.collect();
6265

63-
lint::complete_lint(acc, ctx, &existing_lints, &lints);
66+
lint::complete_lint(acc, ctx, colon_prefix, &existing_lints, &lints);
6467
}
6568
"cfg" => cfg::complete_cfg(acc, ctx),
6669
_ => (),

crates/ide-completion/src/completions/attribute/lint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
//! Completion for lints
22
use ide_db::{generated::lints::Lint, SymbolKind};
3-
use syntax::{ast, T};
3+
use syntax::ast;
44

55
use crate::{context::CompletionContext, item::CompletionItem, Completions};
66

77
pub(super) fn complete_lint(
88
acc: &mut Completions,
99
ctx: &CompletionContext,
10+
is_qualified: bool,
1011
existing_lints: &[ast::Path],
1112
lints_completions: &[Lint],
1213
) {
13-
let is_qualified = ctx.previous_token_is(T![:]);
1414
for &Lint { label, description } in lints_completions {
1515
let (qual, name) = {
1616
// FIXME: change `Lint`'s label to not store a path in it but split the prefix off instead?

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! Complete fields in record literals and patterns.
22
use ide_db::SymbolKind;
3-
use syntax::{
4-
ast::{self, Expr},
5-
T,
6-
};
3+
use syntax::ast::{self, Expr};
74

85
use crate::{
96
context::{ExprCtx, PathCompletionCtx, PatternContext, Qualified},
@@ -24,6 +21,7 @@ pub(crate) fn complete_record_expr_fields(
2421
acc: &mut Completions,
2522
ctx: &CompletionContext,
2623
record_expr: &ast::RecordExpr,
24+
&dot_prefix: &bool,
2725
) {
2826
let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_expr.clone()));
2927

@@ -45,7 +43,7 @@ pub(crate) fn complete_record_expr_fields(
4543
let missing_fields = ctx.sema.record_literal_missing_fields(record_expr);
4644

4745
add_default_update(acc, ctx, ty, &missing_fields);
48-
if ctx.previous_token_is(T![.]) {
46+
if dot_prefix {
4947
let mut item =
5048
CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), "..");
5149
item.insert_text(".");

crates/ide-completion/src/context.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,11 @@ pub(super) enum NameRefKind {
259259
DotAccess(DotAccess),
260260
/// Position where we are only interested in keyword completions
261261
Keyword(ast::Item),
262-
/// The record expression this nameref is a field of
263-
RecordExpr(ast::RecordExpr),
262+
/// The record expression this nameref is a field of and whether a dot precedes the completion identifier.
263+
RecordExpr {
264+
dot_prefix: bool,
265+
expr: ast::RecordExpr,
266+
},
264267
Pattern(PatternContext),
265268
}
266269

@@ -279,6 +282,7 @@ pub(super) enum IdentContext {
279282
},
280283
/// Set if we are currently completing in an unexpanded attribute, this usually implies a builtin attribute like `allow($0)`
281284
UnexpandedAttrTT {
285+
colon_prefix: bool,
282286
fake_attribute_under_caret: Option<ast::Attr>,
283287
},
284288
}
@@ -334,9 +338,6 @@ pub(crate) struct CompletionContext<'a> {
334338
/// The expected type of what we are completing.
335339
pub(super) expected_type: Option<Type>,
336340

337-
// FIXME: This shouldn't exist
338-
pub(super) previous_token: Option<SyntaxToken>,
339-
340341
// We might wanna split these out of CompletionContext
341342
pub(super) ident_ctx: IdentContext,
342343
pub(super) qualifier_ctx: QualifierCtx,
@@ -361,11 +362,6 @@ impl<'a> CompletionContext<'a> {
361362
}
362363
}
363364

364-
// FIXME: This shouldn't exist
365-
pub(crate) fn previous_token_is(&self, kind: SyntaxKind) -> bool {
366-
self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind)
367-
}
368-
369365
pub(crate) fn famous_defs(&self) -> FamousDefs {
370366
FamousDefs(&self.sema, self.krate)
371367
}
@@ -507,9 +503,11 @@ impl<'a> CompletionContext<'a> {
507503
module,
508504
expected_name: None,
509505
expected_type: None,
510-
previous_token: None,
511506
// dummy value, will be overwritten
512-
ident_ctx: IdentContext::UnexpandedAttrTT { fake_attribute_under_caret: None },
507+
ident_ctx: IdentContext::UnexpandedAttrTT {
508+
fake_attribute_under_caret: None,
509+
colon_prefix: false,
510+
},
513511
qualifier_ctx: Default::default(),
514512
locals,
515513
};

crates/ide-completion/src/context/analysis.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ impl<'a> CompletionContext<'a> {
254254
// match foo { $0 }
255255
// match foo { ..., pat => $0 }
256256
ast::MatchExpr(it) => {
257-
let ty = if self.previous_token_is(T![=>]) {
257+
let on_arrow = previous_non_trivia_token(self.token.clone()).map_or(false, |it| T![=>] == it.kind());
258+
259+
let ty = if on_arrow {
258260
// match foo { ..., pat => $0 }
259261
cov_mark::hit!(expected_type_match_arm_body_without_leading_char);
260262
cov_mark::hit!(expected_type_match_arm_body_with_leading_char);
@@ -327,9 +329,6 @@ impl<'a> CompletionContext<'a> {
327329
return None;
328330
}
329331

330-
self.previous_token =
331-
syntax_element.clone().into_token().and_then(previous_non_trivia_token);
332-
333332
(self.expected_type, self.expected_name) = self.expected_type_and_name();
334333

335334
// Overwrite the path kind for derives
@@ -368,19 +367,19 @@ impl<'a> CompletionContext<'a> {
368367
} else {
369368
// Fix up trailing whitespace problem
370369
// #[attr(foo = $0
371-
let token = if self.token.kind() == SyntaxKind::WHITESPACE {
372-
self.previous_token.as_ref()?
373-
} else {
374-
&self.token
375-
};
370+
let token =
371+
syntax::algo::skip_trivia_token(self.token.clone(), Direction::Prev)?;
376372
let p = token.parent()?;
377373
if p.kind() == SyntaxKind::TOKEN_TREE
378374
&& p.ancestors().any(|it| it.kind() == SyntaxKind::META)
379375
{
376+
let colon_prefix = previous_non_trivia_token(self.token.clone())
377+
.map_or(false, |it| T![:] == it.kind());
380378
self.ident_ctx = IdentContext::UnexpandedAttrTT {
381379
fake_attribute_under_caret: syntax_element
382380
.ancestors()
383381
.find_map(ast::Attr::cast),
382+
colon_prefix,
384383
};
385384
} else {
386385
return None;
@@ -493,12 +492,15 @@ impl<'a> CompletionContext<'a> {
493492
|kind| (NameRefContext { nameref: nameref.clone(), kind }, Default::default());
494493

495494
if let Some(record_field) = ast::RecordExprField::for_field_name(&name_ref) {
495+
let dot_prefix = previous_non_trivia_token(name_ref.syntax().clone())
496+
.map_or(false, |it| T![.] == it.kind());
497+
496498
return find_node_in_file_compensated(
497499
sema,
498500
original_file,
499501
&record_field.parent_record_lit(),
500502
)
501-
.map(NameRefKind::RecordExpr)
503+
.map(|expr| NameRefKind::RecordExpr { expr, dot_prefix })
502504
.map(make_res);
503505
}
504506
if let Some(record_field) = ast::RecordPatField::for_field_name_ref(&name_ref) {
@@ -1180,8 +1182,12 @@ pub(crate) fn is_in_loop_body(node: &SyntaxNode) -> bool {
11801182
.is_some()
11811183
}
11821184

1183-
fn previous_non_trivia_token(token: SyntaxToken) -> Option<SyntaxToken> {
1184-
let mut token = token.prev_token();
1185+
fn previous_non_trivia_token(e: impl Into<SyntaxElement>) -> Option<SyntaxToken> {
1186+
let mut token = match e.into() {
1187+
SyntaxElement::Node(n) => n.first_token()?,
1188+
SyntaxElement::Token(t) => t,
1189+
}
1190+
.prev_token();
11851191
while let Some(inner) = token {
11861192
if !inner.kind().is_trivia() {
11871193
return Some(inner);

crates/ide-completion/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,16 @@ pub fn completions(
181181
completions::extern_abi::complete_extern_abi(acc, ctx, expanded);
182182
completions::format_string::format_string(acc, ctx, original, expanded);
183183
}
184-
IdentContext::UnexpandedAttrTT { fake_attribute_under_caret: Some(attr) } => {
185-
completions::attribute::complete_known_attribute_input(acc, ctx, attr);
184+
IdentContext::UnexpandedAttrTT {
185+
colon_prefix,
186+
fake_attribute_under_caret: Some(attr),
187+
} => {
188+
completions::attribute::complete_known_attribute_input(
189+
acc,
190+
ctx,
191+
colon_prefix,
192+
attr,
193+
);
186194
}
187195
IdentContext::UnexpandedAttrTT { .. } | IdentContext::String { .. } => (),
188196
}

0 commit comments

Comments
 (0)