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

Commit 7685245

Browse files
committed
Even more completion context filtering
1 parent ce5859e commit 7685245

File tree

17 files changed

+263
-256
lines changed

17 files changed

+263
-256
lines changed

crates/ide-completion/src/completions.rs

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ use ide_db::SymbolKind;
2727
use syntax::ast;
2828

2929
use crate::{
30-
context::Visible,
30+
context::{
31+
ItemListKind, NameContext, NameKind, NameRefContext, NameRefKind, PathKind, PatternContext,
32+
TypeLocation, Visible,
33+
},
3134
item::Builder,
3235
render::{
3336
const_::render_const,
@@ -437,3 +440,120 @@ fn enum_variants_with_paths(
437440
}
438441
}
439442
}
443+
444+
pub(super) fn complete_name(
445+
acc: &mut Completions,
446+
ctx: &CompletionContext,
447+
NameContext { name, kind }: &NameContext,
448+
) {
449+
match kind {
450+
NameKind::Const => {
451+
item_list::trait_impl::complete_trait_impl_const(acc, ctx, name);
452+
}
453+
NameKind::Function => {
454+
item_list::trait_impl::complete_trait_impl_fn(acc, ctx, name);
455+
}
456+
NameKind::IdentPat(pattern_ctx) => complete_patterns(acc, ctx, pattern_ctx),
457+
NameKind::Module(mod_under_caret) => {
458+
mod_::complete_mod(acc, ctx, mod_under_caret);
459+
}
460+
NameKind::TypeAlias => {
461+
item_list::trait_impl::complete_trait_impl_type_alias(acc, ctx, name);
462+
}
463+
NameKind::RecordField => {
464+
field::complete_field_list_record_variant(acc, ctx);
465+
}
466+
NameKind::ConstParam
467+
| NameKind::Enum
468+
| NameKind::MacroDef
469+
| NameKind::MacroRules
470+
| NameKind::Rename
471+
| NameKind::SelfParam
472+
| NameKind::Static
473+
| NameKind::Struct
474+
| NameKind::Trait
475+
| NameKind::TypeParam
476+
| NameKind::Union
477+
| NameKind::Variant => (),
478+
}
479+
}
480+
481+
pub(super) fn complete_name_ref(
482+
acc: &mut Completions,
483+
ctx: &CompletionContext,
484+
NameRefContext { nameref, kind }: &NameRefContext,
485+
) {
486+
match kind {
487+
NameRefKind::Path(path_ctx) => {
488+
flyimport::import_on_the_fly_path(acc, ctx, path_ctx);
489+
match &path_ctx.kind {
490+
PathKind::Expr { expr_ctx } => {
491+
dot::complete_undotted_self(acc, ctx, path_ctx, expr_ctx);
492+
expr::complete_expr_path(acc, ctx, path_ctx, expr_ctx);
493+
item_list::complete_item_list_in_expr(acc, ctx, path_ctx, expr_ctx);
494+
record::complete_record_expr_func_update(acc, ctx, path_ctx, expr_ctx);
495+
snippet::complete_expr_snippet(acc, ctx, path_ctx, expr_ctx);
496+
}
497+
PathKind::Type { location } => {
498+
r#type::complete_type_path(acc, ctx, path_ctx, location);
499+
match location {
500+
TypeLocation::TupleField => {
501+
field::complete_field_list_tuple_variant(acc, ctx, path_ctx);
502+
}
503+
TypeLocation::TypeAscription(ascription) => {
504+
r#type::complete_ascribed_type(acc, ctx, path_ctx, ascription);
505+
}
506+
TypeLocation::GenericArgList(_)
507+
| TypeLocation::TypeBound
508+
| TypeLocation::ImplTarget
509+
| TypeLocation::ImplTrait
510+
| TypeLocation::Other => (),
511+
}
512+
}
513+
PathKind::Attr { attr_ctx } => {
514+
attribute::complete_attribute(acc, ctx, path_ctx, attr_ctx);
515+
}
516+
PathKind::Derive { existing_derives } => {
517+
attribute::complete_derive(acc, ctx, path_ctx, existing_derives);
518+
}
519+
PathKind::Item { kind } => {
520+
item_list::complete_item_list(acc, ctx, path_ctx, kind);
521+
snippet::complete_item_snippet(acc, ctx, path_ctx, kind);
522+
if let ItemListKind::TraitImpl(impl_) = kind {
523+
item_list::trait_impl::complete_trait_impl_item_by_name(
524+
acc, ctx, path_ctx, nameref, impl_,
525+
);
526+
}
527+
}
528+
PathKind::Pat { .. } => {
529+
pattern::complete_pattern_path(acc, ctx, path_ctx);
530+
}
531+
PathKind::Vis { has_in_token } => {
532+
vis::complete_vis_path(acc, ctx, path_ctx, has_in_token);
533+
}
534+
PathKind::Use => {
535+
use_::complete_use_tree(acc, ctx, path_ctx, nameref);
536+
}
537+
}
538+
}
539+
NameRefKind::DotAccess(dot_access) => {
540+
flyimport::import_on_the_fly_dot(acc, ctx, dot_access);
541+
dot::complete_dot(acc, ctx, dot_access);
542+
postfix::complete_postfix(acc, ctx, dot_access);
543+
}
544+
NameRefKind::Keyword(item) => {
545+
keyword::complete_for_and_where(acc, ctx, item);
546+
}
547+
NameRefKind::RecordExpr(record_expr) => {
548+
record::complete_record_expr_fields(acc, ctx, record_expr);
549+
}
550+
NameRefKind::Pattern(pattern_ctx) => complete_patterns(acc, ctx, pattern_ctx),
551+
}
552+
}
553+
554+
fn complete_patterns(acc: &mut Completions, ctx: &CompletionContext, pattern_ctx: &PatternContext) {
555+
flyimport::import_on_the_fly_pat(acc, ctx, pattern_ctx);
556+
fn_param::complete_fn_param(acc, ctx, pattern_ctx);
557+
pattern::complete_pattern(acc, ctx, pattern_ctx);
558+
record::complete_record_pattern_fields(acc, ctx, pattern_ctx);
559+
}

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

Lines changed: 4 additions & 10 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::{AttrCtx, CompletionContext, PathCompletionCtx, PathKind, Qualified},
21+
context::{AttrCtx, CompletionContext, PathCompletionCtx, Qualified},
2222
item::CompletionItem,
2323
Completions,
2424
};
@@ -72,16 +72,10 @@ pub(crate) fn complete_known_attribute_input(
7272
pub(crate) fn complete_attribute(
7373
acc: &mut Completions,
7474
ctx: &CompletionContext,
75-
path_ctx: &PathCompletionCtx,
75+
PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
76+
&AttrCtx { kind, annotated_item_kind }: &AttrCtx,
7677
) {
77-
let (qualified, is_inner, annotated_item_kind) = match path_ctx {
78-
&PathCompletionCtx {
79-
kind: PathKind::Attr { attr_ctx: AttrCtx { kind, annotated_item_kind } },
80-
ref qualified,
81-
..
82-
} => (qualified, kind == AttrKind::Inner, annotated_item_kind),
83-
_ => return,
84-
};
78+
let is_inner = kind == AttrKind::Inner;
8579

8680
match qualified {
8781
Qualified::With {

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,17 @@ use itertools::Itertools;
55
use syntax::SmolStr;
66

77
use crate::{
8-
context::{CompletionContext, PathCompletionCtx, PathKind, Qualified},
8+
context::{CompletionContext, ExistingDerives, PathCompletionCtx, Qualified},
99
item::CompletionItem,
1010
Completions,
1111
};
1212

1313
pub(crate) fn complete_derive(
1414
acc: &mut Completions,
1515
ctx: &CompletionContext,
16-
path_ctx: &PathCompletionCtx,
16+
PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
17+
existing_derives: &ExistingDerives,
1718
) {
18-
let (qualified, existing_derives) = match path_ctx {
19-
PathCompletionCtx { kind: PathKind::Derive { existing_derives }, qualified, .. } => {
20-
(qualified, existing_derives)
21-
}
22-
_ => return,
23-
};
24-
2519
let core = ctx.famous_defs().core();
2620

2721
match qualified {

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
use ide_db::FxHashSet;
44

55
use crate::{
6-
context::{
7-
CompletionContext, DotAccess, DotAccessKind, ExprCtx, PathCompletionCtx, PathKind,
8-
Qualified,
9-
},
6+
context::{CompletionContext, DotAccess, DotAccessKind, ExprCtx, PathCompletionCtx, Qualified},
107
CompletionItem, CompletionItemKind, Completions,
118
};
129

@@ -43,16 +40,22 @@ pub(crate) fn complete_undotted_self(
4340
acc: &mut Completions,
4441
ctx: &CompletionContext,
4542
path_ctx: &PathCompletionCtx,
43+
expr_ctx: &ExprCtx,
4644
) {
4745
if !ctx.config.enable_self_on_the_fly {
4846
return;
4947
}
50-
let self_param = match path_ctx {
51-
PathCompletionCtx {
52-
qualified: Qualified::No,
53-
kind: PathKind::Expr { expr_ctx: ExprCtx { self_param: Some(self_param), .. } },
54-
..
55-
} if path_ctx.is_trivial_path() && ctx.qualifier_ctx.none() => self_param,
48+
if !path_ctx.is_trivial_path() {
49+
return;
50+
}
51+
if !ctx.qualifier_ctx.none() {
52+
return;
53+
}
54+
if !matches!(path_ctx.qualified, Qualified::No) {
55+
return;
56+
}
57+
let self_param = match expr_ctx {
58+
ExprCtx { self_param: Some(self_param), .. } => self_param,
5659
_ => return,
5760
};
5861

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

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,31 @@ use hir::ScopeDef;
44
use ide_db::FxHashSet;
55

66
use crate::{
7-
context::{ExprCtx, PathCompletionCtx, PathKind, Qualified},
7+
context::{ExprCtx, PathCompletionCtx, Qualified},
88
CompletionContext, Completions,
99
};
1010

1111
pub(crate) fn complete_expr_path(
1212
acc: &mut Completions,
1313
ctx: &CompletionContext,
14-
path_ctx: &PathCompletionCtx,
14+
PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
15+
&ExprCtx {
16+
in_block_expr,
17+
in_loop_body,
18+
after_if_expr,
19+
in_condition,
20+
incomplete_let,
21+
ref ref_expr_parent,
22+
ref is_func_update,
23+
ref innermost_ret_ty,
24+
ref impl_,
25+
..
26+
}: &ExprCtx,
1527
) {
1628
let _p = profile::span("complete_expr_path");
1729
if !ctx.qualifier_ctx.none() {
1830
return;
1931
}
20-
let (
21-
qualified,
22-
&ExprCtx {
23-
in_block_expr,
24-
in_loop_body,
25-
after_if_expr,
26-
in_condition,
27-
incomplete_let,
28-
ref ref_expr_parent,
29-
ref is_func_update,
30-
ref innermost_ret_ty,
31-
ref impl_,
32-
..
33-
},
34-
) = match path_ctx {
35-
PathCompletionCtx { kind: PathKind::Expr { expr_ctx }, qualified, .. } => {
36-
(qualified, expr_ctx)
37-
}
38-
_ => return,
39-
};
4032

4133
let wants_mut_token =
4234
ref_expr_parent.as_ref().map(|it| it.mut_token().is_none()).unwrap_or(false);

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Completion of field list position.
22
33
use crate::{
4-
context::{PathCompletionCtx, PathKind, Qualified, TypeLocation},
4+
context::{PathCompletionCtx, Qualified},
55
CompletionContext, Completions,
66
};
77

@@ -10,21 +10,21 @@ pub(crate) fn complete_field_list_tuple_variant(
1010
ctx: &CompletionContext,
1111
path_ctx: &PathCompletionCtx,
1212
) {
13+
if ctx.qualifier_ctx.vis_node.is_some() {
14+
return;
15+
}
1316
match path_ctx {
1417
PathCompletionCtx {
1518
has_macro_bang: false,
1619
qualified: Qualified::No,
1720
parent: None,
18-
kind: PathKind::Type { location: TypeLocation::TupleField },
1921
has_type_args: false,
2022
..
2123
} => {
22-
if ctx.qualifier_ctx.vis_node.is_none() {
23-
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
24-
add_keyword("pub(crate)", "pub(crate)");
25-
add_keyword("pub(super)", "pub(super)");
26-
add_keyword("pub", "pub");
27-
}
24+
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
25+
add_keyword("pub(crate)", "pub(crate)");
26+
add_keyword("pub(super)", "pub(super)");
27+
add_keyword("pub", "pub");
2828
}
2929
_ => (),
3030
}

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,37 @@
22
33
use crate::{
44
completions::module_or_fn_macro,
5-
context::{ExprCtx, ItemListKind, PathCompletionCtx, PathKind, Qualified},
5+
context::{ExprCtx, ItemListKind, PathCompletionCtx, Qualified},
66
CompletionContext, Completions,
77
};
88

99
pub(crate) mod trait_impl;
1010

11-
pub(crate) fn complete_item_list(
11+
pub(crate) fn complete_item_list_in_expr(
1212
acc: &mut Completions,
1313
ctx: &CompletionContext,
1414
path_ctx: &PathCompletionCtx,
15+
expr_ctx: &ExprCtx,
16+
) {
17+
if !expr_ctx.in_block_expr {
18+
return;
19+
}
20+
if !path_ctx.is_trivial_path() {
21+
return;
22+
}
23+
add_keywords(acc, ctx, None);
24+
}
25+
26+
pub(crate) fn complete_item_list(
27+
acc: &mut Completions,
28+
ctx: &CompletionContext,
29+
path_ctx @ PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
30+
kind: &ItemListKind,
1531
) {
1632
let _p = profile::span("complete_item_list");
17-
let qualified = match path_ctx {
18-
PathCompletionCtx { kind: PathKind::Item { kind }, qualified, .. } => {
19-
if path_ctx.is_trivial_path() {
20-
add_keywords(acc, ctx, Some(kind));
21-
}
22-
qualified
23-
}
24-
PathCompletionCtx {
25-
kind: PathKind::Expr { expr_ctx: ExprCtx { in_block_expr: true, .. } },
26-
..
27-
} if path_ctx.is_trivial_path() => {
28-
add_keywords(acc, ctx, None);
29-
return;
30-
}
31-
_ => return,
32-
};
33+
if path_ctx.is_trivial_path() {
34+
add_keywords(acc, ctx, Some(kind));
35+
}
3336

3437
match qualified {
3538
Qualified::With {

0 commit comments

Comments
 (0)