Skip to content

Commit 09ac44c

Browse files
committed
Auto merge of rust-lang#12570 - Veykril:completion, r=Veykril
Only run completion functions if their corresponding context is active
2 parents e7a0088 + 00fdb4a commit 09ac44c

29 files changed

+602
-455
lines changed

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

Lines changed: 17 additions & 16 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, IdentContext, PathCompletionCtx, PathKind, Qualified},
21+
context::{CompletionContext, PathCompletionCtx, PathKind, Qualified},
2222
item::CompletionItem,
2323
Completions,
2424
};
@@ -34,11 +34,9 @@ pub(crate) use self::derive::complete_derive;
3434
pub(crate) fn complete_known_attribute_input(
3535
acc: &mut Completions,
3636
ctx: &CompletionContext,
37+
fake_attribute_under_caret: &ast::Attr,
3738
) -> Option<()> {
38-
let attribute = match &ctx.ident_ctx {
39-
IdentContext::UnexpandedAttrTT { fake_attribute_under_caret: Some(it) } => it,
40-
_ => return None,
41-
};
39+
let attribute = fake_attribute_under_caret;
4240
let name_ref = match attribute.path() {
4341
Some(p) => Some(p.as_single_name_ref()?),
4442
None => None,
@@ -71,27 +69,30 @@ pub(crate) fn complete_known_attribute_input(
7169
Some(())
7270
}
7371

74-
pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) {
75-
let (qualified, is_inner, annotated_item_kind) = match ctx.path_context() {
76-
Some(&PathCompletionCtx {
72+
pub(crate) fn complete_attribute(
73+
acc: &mut Completions,
74+
ctx: &CompletionContext,
75+
path_ctx: &PathCompletionCtx,
76+
) {
77+
let (qualified, is_inner, annotated_item_kind) = match path_ctx {
78+
&PathCompletionCtx {
7779
kind: PathKind::Attr { kind, annotated_item_kind },
7880
ref qualified,
7981
..
80-
}) => (qualified, kind == AttrKind::Inner, annotated_item_kind),
82+
} => (qualified, kind == AttrKind::Inner, annotated_item_kind),
8183
_ => return,
8284
};
8385

8486
match qualified {
85-
Qualified::With { resolution, is_super_chain, .. } => {
87+
Qualified::With {
88+
resolution: Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))),
89+
is_super_chain,
90+
..
91+
} => {
8692
if *is_super_chain {
8793
acc.add_keyword(ctx, "super::");
8894
}
8995

90-
let module = match resolution {
91-
Some(hir::PathResolution::Def(hir::ModuleDef::Module(it))) => it,
92-
_ => return,
93-
};
94-
9596
for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
9697
if let Some(def) = module_or_attr(ctx.db, def) {
9798
acc.add_resolution(ctx, name, def);
@@ -110,7 +111,7 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
110111
});
111112
acc.add_nameref_keywords_with_colon(ctx);
112113
}
113-
Qualified::Infer => {}
114+
Qualified::Infer | Qualified::With { .. } => {}
114115
}
115116

116117
let attributes = annotated_item_kind.and_then(|kind| {

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,30 @@ use crate::{
1010
Completions,
1111
};
1212

13-
pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
14-
let (qualified, existing_derives) = match ctx.path_context() {
15-
Some(PathCompletionCtx {
16-
kind: PathKind::Derive { existing_derives }, qualified, ..
17-
}) => (qualified, existing_derives),
13+
pub(crate) fn complete_derive(
14+
acc: &mut Completions,
15+
ctx: &CompletionContext,
16+
path_ctx: &PathCompletionCtx,
17+
) {
18+
let (qualified, existing_derives) = match path_ctx {
19+
PathCompletionCtx { kind: PathKind::Derive { existing_derives }, qualified, .. } => {
20+
(qualified, existing_derives)
21+
}
1822
_ => return,
1923
};
2024

2125
let core = ctx.famous_defs().core();
2226

2327
match qualified {
24-
Qualified::With { resolution, is_super_chain, .. } => {
28+
Qualified::With {
29+
resolution: Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))),
30+
is_super_chain,
31+
..
32+
} => {
2533
if *is_super_chain {
2634
acc.add_keyword(ctx, "super::");
2735
}
2836

29-
let module = match resolution {
30-
Some(hir::PathResolution::Def(hir::ModuleDef::Module(it))) => it,
31-
_ => return,
32-
};
33-
3437
for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
3538
let add_def = match def {
3639
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => {
@@ -101,7 +104,7 @@ pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
101104
});
102105
acc.add_nameref_keywords_with_colon(ctx);
103106
}
104-
Qualified::Infer => {}
107+
Qualified::Infer | Qualified::With { .. } => {}
105108
}
106109
}
107110

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

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,16 @@ use ide_db::FxHashSet;
44

55
use crate::{
66
context::{
7-
CompletionContext, DotAccess, DotAccessKind, NameRefContext, NameRefKind,
8-
PathCompletionCtx, PathKind, Qualified,
7+
CompletionContext, DotAccess, DotAccessKind, PathCompletionCtx, PathKind, Qualified,
98
},
109
CompletionItem, CompletionItemKind, Completions,
1110
};
1211

1312
/// Complete dot accesses, i.e. fields or methods.
14-
pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
15-
let (dot_access, receiver_ty) = match ctx.nameref_ctx() {
16-
Some(NameRefContext {
17-
kind:
18-
Some(NameRefKind::DotAccess(access @ DotAccess { receiver_ty: Some(receiver_ty), .. })),
19-
..
20-
}) => (access, &receiver_ty.original),
21-
_ => return complete_undotted_self(acc, ctx),
13+
pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext, dot_access: &DotAccess) {
14+
let receiver_ty = match dot_access {
15+
DotAccess { receiver_ty: Some(receiver_ty), .. } => &receiver_ty.original,
16+
_ => return,
2217
};
2318

2419
// Suggest .await syntax for types that implement Future trait
@@ -43,18 +38,17 @@ pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
4338
complete_methods(ctx, &receiver_ty, |func| acc.add_method(ctx, func, None, None));
4439
}
4540

46-
fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
41+
pub(crate) fn complete_undotted_self(
42+
acc: &mut Completions,
43+
ctx: &CompletionContext,
44+
path_ctx: &PathCompletionCtx,
45+
) {
4746
if !ctx.config.enable_self_on_the_fly {
4847
return;
4948
}
50-
match ctx.path_context() {
51-
Some(
52-
path_ctx @ PathCompletionCtx {
53-
qualified: Qualified::No,
54-
kind: PathKind::Expr { .. },
55-
..
56-
},
57-
) if path_ctx.is_trivial_path() && ctx.qualifier_ctx.none() => {}
49+
match path_ctx {
50+
PathCompletionCtx { qualified: Qualified::No, kind: PathKind::Expr { .. }, .. }
51+
if path_ctx.is_trivial_path() && ctx.qualifier_ctx.none() => {}
5852
_ => return,
5953
}
6054

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ use crate::{
88
CompletionContext, Completions,
99
};
1010

11-
pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext) {
11+
pub(crate) fn complete_expr_path(
12+
acc: &mut Completions,
13+
ctx: &CompletionContext,
14+
name_ref_ctx: &NameRefContext,
15+
) {
1216
let _p = profile::span("complete_expr_path");
1317

1418
let (
@@ -19,8 +23,8 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
1923
after_if_expr,
2024
wants_mut_token,
2125
in_condition,
22-
) = match ctx.nameref_ctx() {
23-
Some(&NameRefContext {
26+
) = match name_ref_ctx {
27+
&NameRefContext {
2428
kind:
2529
Some(NameRefKind::Path(PathCompletionCtx {
2630
kind:
@@ -36,7 +40,7 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
3640
..
3741
})),
3842
..
39-
}) if ctx.qualifier_ctx.none() => (
43+
} if ctx.qualifier_ctx.none() => (
4044
qualified,
4145
in_block_expr,
4246
in_loop_body,
@@ -65,11 +69,8 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
6569
.into_iter()
6670
.flat_map(|it| hir::Trait::from(it).items(ctx.sema.db))
6771
.for_each(|item| add_assoc_item(acc, ctx, item)),
68-
Qualified::With { resolution, .. } => {
69-
let resolution = match resolution {
70-
Some(it) => it,
71-
None => return,
72-
};
72+
Qualified::With { resolution: None, .. } => {}
73+
Qualified::With { resolution: Some(resolution), .. } => {
7374
// Add associated types on type parameters and `Self`.
7475
ctx.scope.assoc_type_shorthand_candidates(resolution, |_, alias| {
7576
acc.add_type_alias(ctx, alias);

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

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

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

1311
// Most of these are feature gated, we should filter/add feature gate completions once we have them.
@@ -42,15 +40,15 @@ const SUPPORTED_CALLING_CONVENTIONS: &[&str] = &[
4240
"unadjusted",
4341
];
4442

45-
pub(crate) fn complete_extern_abi(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
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-
};
43+
pub(crate) fn complete_extern_abi(
44+
acc: &mut Completions,
45+
_ctx: &CompletionContext,
46+
expanded: &ast::String,
47+
) -> Option<()> {
48+
if !expanded.syntax().parent().map_or(false, |it| ast::Abi::can_cast(it.kind())) {
49+
return None;
50+
}
51+
let abi_str = expanded;
5452
let source_range = abi_str.text_range_between_quotes()?;
5553
for &abi in SUPPORTED_CALLING_CONVENTIONS {
5654
CompletionItem::new(CompletionItemKind::Keyword, source_range, abi).add_to(acc);

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
33
use crate::{
44
context::{
5-
IdentContext, NameContext, NameKind, NameRefContext, NameRefKind, PathCompletionCtx,
6-
PathKind, Qualified, TypeLocation,
5+
NameContext, NameKind, NameRefContext, NameRefKind, PathCompletionCtx, PathKind, Qualified,
6+
TypeLocation,
77
},
88
CompletionContext, Completions,
99
};
1010

11-
pub(crate) fn complete_field_list(acc: &mut Completions, ctx: &CompletionContext) {
12-
match &ctx.ident_ctx {
13-
IdentContext::Name(NameContext { kind: NameKind::RecordField, .. })
14-
| IdentContext::NameRef(NameRefContext {
11+
pub(crate) fn complete_field_list_tuple_variant(
12+
acc: &mut Completions,
13+
ctx: &CompletionContext,
14+
name_ref_ctx: &NameRefContext,
15+
) {
16+
match name_ref_ctx {
17+
NameRefContext {
1518
kind:
1619
Some(NameRefKind::Path(PathCompletionCtx {
1720
has_macro_bang: false,
@@ -22,14 +25,29 @@ pub(crate) fn complete_field_list(acc: &mut Completions, ctx: &CompletionContext
2225
..
2326
})),
2427
..
25-
}) => {
28+
} => {
2629
if ctx.qualifier_ctx.vis_node.is_none() {
2730
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
2831
add_keyword("pub(crate)", "pub(crate)");
2932
add_keyword("pub(super)", "pub(super)");
3033
add_keyword("pub", "pub");
3134
}
3235
}
33-
_ => return,
36+
_ => (),
37+
}
38+
}
39+
40+
pub(crate) fn complete_field_list_record_variant(
41+
acc: &mut Completions,
42+
ctx: &CompletionContext,
43+
name_ctx: &NameContext,
44+
) {
45+
if let NameContext { kind: NameKind::RecordField, .. } = name_ctx {
46+
if ctx.qualifier_ctx.vis_node.is_none() {
47+
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
48+
add_keyword("pub(crate)", "pub(crate)");
49+
add_keyword("pub(super)", "pub(super)");
50+
add_keyword("pub", "pub");
51+
}
3452
}
3553
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ use crate::{
1919
/// `spam: &mut Spam` insert text/label will be suggested.
2020
///
2121
/// Also complete parameters for closure or local functions from the surrounding defined locals.
22-
pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
23-
let (param_list, _, param_kind) = match &ctx.pattern_ctx {
24-
Some(PatternContext { param_ctx: Some(kind), .. }) => kind,
22+
pub(crate) fn complete_fn_param(
23+
acc: &mut Completions,
24+
ctx: &CompletionContext,
25+
pattern_ctx: &PatternContext,
26+
) -> Option<()> {
27+
let (param_list, _, param_kind) = match pattern_ctx {
28+
PatternContext { param_ctx: Some(kind), .. } => kind,
2529
_ => return None,
2630
};
2731

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

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

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

129
/// Complete identifiers in format strings.
13-
pub(crate) fn format_string(acc: &mut Completions, ctx: &CompletionContext) {
14-
let string = match &ctx.ident_ctx {
15-
IdentContext::String { expanded: Some(expanded), original }
16-
if is_format_string(&expanded) =>
17-
{
18-
original
19-
}
20-
_ => return,
21-
};
10+
pub(crate) fn format_string(
11+
acc: &mut Completions,
12+
ctx: &CompletionContext,
13+
original: &ast::String,
14+
expanded: &ast::String,
15+
) {
16+
if !is_format_string(&expanded) {
17+
return;
18+
}
2219
let cursor = ctx.position.offset;
2320
let lit_start = ctx.original_token.text_range().start();
2421
let cursor_in_lit = cursor - lit_start;
2522

26-
let prefix = &string.text()[..cursor_in_lit.into()];
23+
let prefix = &original.text()[..cursor_in_lit.into()];
2724
let braces = prefix.char_indices().rev().skip_while(|&(_, c)| c.is_alphanumeric()).next_tuple();
2825
let brace_offset = match braces {
2926
// escaped brace

0 commit comments

Comments
 (0)