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

Commit 44c3cc1

Browse files
committed
Merge the different identifier contexts into one enum
1 parent 99fa37d commit 44c3cc1

File tree

13 files changed

+138
-91
lines changed

13 files changed

+138
-91
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use syntax::{
1818

1919
use crate::{
2020
completions::module_or_attr,
21-
context::{CompletionContext, PathCompletionCtx, PathKind, PathQualifierCtx},
21+
context::{CompletionContext, IdentContext, PathCompletionCtx, PathKind, PathQualifierCtx},
2222
item::CompletionItem,
2323
Completions,
2424
};
@@ -35,7 +35,10 @@ pub(crate) fn complete_known_attribute_input(
3535
acc: &mut Completions,
3636
ctx: &CompletionContext,
3737
) -> Option<()> {
38-
let attribute = ctx.fake_attribute_under_caret.as_ref()?;
38+
let attribute = match &ctx.ident_ctx {
39+
IdentContext::UnexpandedAttrTT { fake_attribute_under_caret: Some(it) } => it,
40+
_ => return None,
41+
};
3942
let name_ref = match attribute.path() {
4043
Some(p) => Some(p.as_single_name_ref()?),
4144
None => None,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99

1010
/// Complete dot accesses, i.e. fields or methods.
1111
pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
12-
let (dot_access, dot_receiver) = match &ctx.nameref_ctx {
12+
let (dot_access, dot_receiver) = match ctx.nameref_ctx() {
1313
Some(NameRefContext {
1414
dot_access:
1515
Some(

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use syntax::{
55
};
66

77
use crate::{
8-
completions::Completions, context::CompletionContext, CompletionItem, CompletionItemKind,
8+
completions::Completions,
9+
context::{CompletionContext, IdentContext},
10+
CompletionItem, CompletionItemKind,
911
};
1012

1113
// Most of these are feature gated, we should filter/add feature gate completions once we have them.
@@ -41,10 +43,14 @@ const SUPPORTED_CALLING_CONVENTIONS: &[&str] = &[
4143
];
4244

4345
pub(crate) fn complete_extern_abi(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
44-
if ctx.token.parent().and_then(ast::Abi::cast).is_none() {
45-
return None;
46-
}
47-
let abi_str = ast::String::cast(ctx.token.clone())?;
46+
let abi_str = match &ctx.ident_ctx {
47+
IdentContext::String { expanded: Some(expanded), .. }
48+
if expanded.syntax().parent().map_or(false, |it| ast::Abi::can_cast(it.kind())) =>
49+
{
50+
expanded
51+
}
52+
_ => return None,
53+
};
4854
let source_range = abi_str.text_range_between_quotes()?;
4955
for &abi in SUPPORTED_CALLING_CONVENTIONS {
5056
CompletionItem::new(CompletionItemKind::Keyword, source_range, abi).add_to(acc);

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22
33
use ide_db::syntax_helpers::format_string::is_format_string;
44
use itertools::Itertools;
5-
use syntax::{ast, AstToken, TextRange, TextSize};
5+
use syntax::{AstToken, TextRange, TextSize};
66

7-
use crate::{context::CompletionContext, CompletionItem, CompletionItemKind, Completions};
7+
use crate::{
8+
context::{CompletionContext, IdentContext},
9+
CompletionItem, CompletionItemKind, Completions,
10+
};
811

912
/// Complete identifiers in format strings.
1013
pub(crate) fn format_string(acc: &mut Completions, ctx: &CompletionContext) {
11-
let string = match ast::String::cast(ctx.token.clone())
12-
.zip(ast::String::cast(ctx.original_token.clone()))
13-
{
14-
Some((expanded, original)) if is_format_string(&expanded) => original,
14+
let string = match &ctx.ident_ctx {
15+
IdentContext::String { expanded: Some(expanded), original }
16+
if is_format_string(&expanded) =>
17+
{
18+
original
19+
}
1520
_ => return,
1621
};
1722
let cursor = ctx.position.offset;

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! - `self`, `super` and `crate`, as these are considered part of path completions.
33
//! - `await`, as this is a postfix completion we handle this in the postfix completions.
44
5-
use syntax::{SyntaxKind, T};
5+
use syntax::T;
66

77
use crate::{
88
context::{PathCompletionCtx, PathKind},
@@ -11,18 +11,10 @@ use crate::{
1111
};
1212

1313
pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
14-
if ctx.token.kind() == SyntaxKind::COMMENT {
15-
cov_mark::hit!(no_keyword_completion_in_comments);
16-
return;
17-
}
1814
if matches!(ctx.completion_location, Some(ImmediateLocation::RecordExpr(_))) {
1915
cov_mark::hit!(no_keyword_completion_in_record_lit);
2016
return;
2117
}
22-
if ctx.fake_attribute_under_caret.is_some() {
23-
cov_mark::hit!(no_keyword_completion_in_attr_of_expr);
24-
return;
25-
}
2618
if ctx.is_non_trivial_path() {
2719
cov_mark::hit!(no_keyword_completion_in_non_trivial_path);
2820
return;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717

1818
/// Completes lifetimes.
1919
pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext) {
20-
let (lp, lifetime) = match &ctx.lifetime_ctx {
20+
let (lp, lifetime) = match ctx.lifetime_ctx() {
2121
Some(LifetimeContext { kind: LifetimeKind::Lifetime, lifetime }) => (None, lifetime),
2222
Some(LifetimeContext {
2323
kind: LifetimeKind::LifetimeParam { is_decl: false, param },
@@ -49,7 +49,7 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext)
4949

5050
/// Completes labels.
5151
pub(crate) fn complete_label(acc: &mut Completions, ctx: &CompletionContext) {
52-
if !matches!(ctx.lifetime_ctx, Some(LifetimeContext { kind: LifetimeKind::LabelRef, .. })) {
52+
if !matches!(ctx.lifetime_ctx(), Some(LifetimeContext { kind: LifetimeKind::LabelRef, .. })) {
5353
return;
5454
}
5555
ctx.process_all_names_raw(&mut |name, res| {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616

1717
/// Complete mod declaration, i.e. `mod ;`
1818
pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
19-
let mod_under_caret = match &ctx.name_ctx {
19+
let mod_under_caret = match ctx.name_ctx() {
2020
Some(NameContext { kind: NameKind::Module(mod_under_caret), .. })
2121
if mod_under_caret.item_list().is_none() =>
2222
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
2323
return;
2424
}
2525

26-
let (dot_receiver, receiver_is_ambiguous_float_literal) = match &ctx.nameref_ctx {
26+
let (dot_receiver, receiver_is_ambiguous_float_literal) = match ctx.nameref_ctx() {
2727
Some(NameRefContext {
2828
dot_access: Some(DotAccess::Method { receiver: Some(it), .. }),
2929
..

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
};
1212

1313
pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext) {
14-
let (&is_absolute_path, qualifier, name_ref) = match &ctx.nameref_ctx {
14+
let (&is_absolute_path, qualifier, name_ref) = match ctx.nameref_ctx() {
1515
Some(NameRefContext {
1616
path_ctx:
1717
Some(PathCompletionCtx { kind: PathKind::Use, is_absolute_path, qualifier, .. }),

0 commit comments

Comments
 (0)