Skip to content

Commit 99fa37d

Browse files
committed
Split namelike into the corresponding completion contexts
1 parent 6a045c7 commit 99fa37d

File tree

6 files changed

+125
-106
lines changed

6 files changed

+125
-106
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ 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_receiver = match ctx.dot_receiver() {
13-
Some(expr) => expr,
12+
let (dot_access, dot_receiver) = match &ctx.nameref_ctx {
13+
Some(NameRefContext {
14+
dot_access:
15+
Some(
16+
access @ (DotAccess::Method { receiver: Some(receiver), .. }
17+
| DotAccess::Field { receiver: Some(receiver), .. }),
18+
),
19+
..
20+
}) => (access, receiver),
1421
_ => return complete_undotted_self(acc, ctx),
1522
};
1623

@@ -19,10 +26,7 @@ pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
1926
_ => return,
2027
};
2128

22-
if matches!(
23-
ctx.nameref_ctx,
24-
Some(NameRefContext { dot_access: Some(DotAccess::Method { .. }), .. }),
25-
) {
29+
if let DotAccess::Method { .. } = dot_access {
2630
cov_mark::hit!(test_no_struct_field_completion_for_method_call);
2731
} else {
2832
complete_fields(

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,9 @@ pub(crate) fn position_for_import(
217217
) -> Option<SyntaxNode> {
218218
Some(
219219
match import_candidate {
220-
Some(ImportCandidate::Path(_)) => ctx.name_syntax.as_ref()?.syntax(),
221220
Some(ImportCandidate::TraitAssocItem(_)) => ctx.path_qual()?.syntax(),
222221
Some(ImportCandidate::TraitMethod(_)) => ctx.dot_receiver()?.syntax(),
223-
None => return ctx.original_token.parent(),
222+
Some(ImportCandidate::Path(_)) | None => return ctx.original_token.parent(),
224223
}
225224
.clone(),
226225
)

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@ use syntax::{ast, TokenText};
1212

1313
use crate::{
1414
completions::Completions,
15-
context::{CompletionContext, LifetimeContext},
15+
context::{CompletionContext, LifetimeContext, LifetimeKind},
1616
};
1717

1818
/// Completes lifetimes.
1919
pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext) {
20-
let lp = match &ctx.lifetime_ctx {
21-
Some(LifetimeContext::Lifetime) => None,
22-
Some(LifetimeContext::LifetimeParam { is_decl: false, param }) => Some(param),
20+
let (lp, lifetime) = match &ctx.lifetime_ctx {
21+
Some(LifetimeContext { kind: LifetimeKind::Lifetime, lifetime }) => (None, lifetime),
22+
Some(LifetimeContext {
23+
kind: LifetimeKind::LifetimeParam { is_decl: false, param },
24+
lifetime,
25+
}) => (Some(param), lifetime),
2326
_ => return,
2427
};
25-
let param_lifetime = match (ctx.lifetime(), lp.and_then(|lp| lp.lifetime())) {
28+
let param_lifetime = match (lifetime, lp.and_then(|lp| lp.lifetime())) {
2629
(Some(lt), Some(lp)) if lp == lt.clone() => return,
2730
(Some(_), Some(lp)) => Some(lp),
2831
_ => None,
@@ -46,7 +49,7 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext)
4649

4750
/// Completes labels.
4851
pub(crate) fn complete_label(acc: &mut Completions, ctx: &CompletionContext) {
49-
if !matches!(ctx.lifetime_ctx, Some(LifetimeContext::LabelRef)) {
52+
if !matches!(ctx.lifetime_ctx, Some(LifetimeContext { kind: LifetimeKind::LabelRef, .. })) {
5053
return;
5154
}
5255
ctx.process_all_names_raw(&mut |name, res| {

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33
use std::iter;
44

55
use hir::{Module, ModuleSource};
6-
use ide_db::FxHashSet;
76
use ide_db::{
87
base_db::{SourceDatabaseExt, VfsPath},
9-
RootDatabase, SymbolKind,
8+
FxHashSet, RootDatabase, SymbolKind,
109
};
1110
use syntax::{ast, AstNode, SyntaxKind};
1211

13-
use crate::{context::NameContext, CompletionItem};
14-
15-
use crate::{context::CompletionContext, Completions};
12+
use crate::{
13+
context::{CompletionContext, NameContext, NameKind},
14+
CompletionItem, Completions,
15+
};
1616

17-
/// Complete mod declaration, i.e. `mod $0;`
17+
/// Complete mod declaration, i.e. `mod ;`
1818
pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
1919
let mod_under_caret = match &ctx.name_ctx {
20-
Some(NameContext::Module(mod_under_caret)) if mod_under_caret.item_list().is_none() => {
20+
Some(NameContext { kind: NameKind::Module(mod_under_caret), .. })
21+
if mod_under_caret.item_list().is_none() =>
22+
{
2123
mod_under_caret
2224
}
2325
_ => return None,
@@ -26,7 +28,7 @@ pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op
2628
let _p = profile::span("completion::complete_mod");
2729

2830
let mut current_module = ctx.module;
29-
// For `mod $0`, `ctx.module` is its parent, but for `mod f$0`, it's `mod f` itself, but we're
31+
// For `mod `, `ctx.module` is its parent, but for `mod f`, it's `mod f` itself, but we're
3032
// interested in its parent.
3133
if ctx.original_token.kind() == SyntaxKind::IDENT {
3234
if let Some(module) = ctx.original_token.ancestors().nth(1).and_then(ast::Module::cast) {

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ use ide_db::FxHashSet;
55
use syntax::{ast, AstNode};
66

77
use crate::{
8-
context::{CompletionContext, PathCompletionCtx, PathKind, PathQualifierCtx},
8+
context::{CompletionContext, NameRefContext, PathCompletionCtx, PathKind, PathQualifierCtx},
99
item::Builder,
1010
CompletionRelevance, Completions,
1111
};
1212

1313
pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext) {
14-
let (&is_absolute_path, qualifier) = match ctx.path_context() {
15-
Some(PathCompletionCtx { kind: PathKind::Use, is_absolute_path, qualifier, .. }) => {
16-
(is_absolute_path, qualifier)
17-
}
14+
let (&is_absolute_path, qualifier, name_ref) = match &ctx.nameref_ctx {
15+
Some(NameRefContext {
16+
path_ctx:
17+
Some(PathCompletionCtx { kind: PathKind::Use, is_absolute_path, qualifier, .. }),
18+
nameref,
19+
..
20+
}) => (is_absolute_path, qualifier, nameref),
1821
_ => return,
1922
};
2023

@@ -55,7 +58,7 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
5558
let module_scope = module.scope(ctx.db, Some(ctx.module));
5659
let unknown_is_current = |name: &hir::Name| {
5760
matches!(
58-
ctx.name_ref(),
61+
name_ref,
5962
Some(name_ref) if name_ref.syntax().text() == name.to_smol_str().as_str()
6063
)
6164
};

0 commit comments

Comments
 (0)