Skip to content

Commit bea4ba0

Browse files
committed
Auto merge of rust-lang#12564 - Veykril:completion, r=Veykril
internal: Collapse completion ctx path `qualifier` and `is_absolute_path` into enum
2 parents 9c0b727 + 531060f commit bea4ba0

File tree

14 files changed

+141
-151
lines changed

14 files changed

+141
-151
lines changed

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

+10-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use syntax::{
1818

1919
use crate::{
2020
completions::module_or_attr,
21-
context::{CompletionContext, IdentContext, PathCompletionCtx, PathKind, PathQualifierCtx},
21+
context::{
22+
CompletionContext, IdentContext, PathCompletionCtx, PathKind, PathQualifierCtx, Qualified,
23+
},
2224
item::CompletionItem,
2325
Completions,
2426
};
@@ -72,18 +74,17 @@ pub(crate) fn complete_known_attribute_input(
7274
}
7375

7476
pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) {
75-
let (is_absolute_path, qualifier, is_inner, annotated_item_kind) = match ctx.path_context() {
77+
let (qualified, is_inner, annotated_item_kind) = match ctx.path_context() {
7678
Some(&PathCompletionCtx {
7779
kind: PathKind::Attr { kind, annotated_item_kind },
78-
is_absolute_path,
79-
ref qualifier,
80+
ref qualified,
8081
..
81-
}) => (is_absolute_path, qualifier, kind == AttrKind::Inner, annotated_item_kind),
82+
}) => (qualified, kind == AttrKind::Inner, annotated_item_kind),
8283
_ => return,
8384
};
8485

85-
match qualifier {
86-
Some(PathQualifierCtx { resolution, is_super_chain, .. }) => {
86+
match qualified {
87+
Qualified::With(PathQualifierCtx { resolution, is_super_chain, .. }) => {
8788
if *is_super_chain {
8889
acc.add_keyword(ctx, "super::");
8990
}
@@ -101,9 +102,9 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
101102
return;
102103
}
103104
// fresh use tree with leading colon2, only show crate roots
104-
None if is_absolute_path => acc.add_crate_roots(ctx),
105+
Qualified::Absolute => acc.add_crate_roots(ctx),
105106
// only show modules in a fresh UseTree
106-
None => {
107+
Qualified::No => {
107108
ctx.process_all_names(&mut |name, def| {
108109
if let Some(def) = module_or_attr(ctx.db, def) {
109110
acc.add_resolution(ctx, name, def);

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

+8-13
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,21 @@ use itertools::Itertools;
55
use syntax::SmolStr;
66

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

1313
pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
14-
let (qualifier, is_absolute_path) = match ctx.path_context() {
15-
Some(&PathCompletionCtx {
16-
kind: PathKind::Derive,
17-
ref qualifier,
18-
is_absolute_path,
19-
..
20-
}) => (qualifier, is_absolute_path),
14+
let qualified = match ctx.path_context() {
15+
Some(&PathCompletionCtx { kind: PathKind::Derive, ref qualified, .. }) => qualified,
2116
_ => return,
2217
};
2318

2419
let core = ctx.famous_defs().core();
2520

26-
match qualifier {
27-
Some(PathQualifierCtx { resolution, is_super_chain, .. }) => {
21+
match qualified {
22+
Qualified::With(PathQualifierCtx { resolution, is_super_chain, .. }) => {
2823
if *is_super_chain {
2924
acc.add_keyword(ctx, "super::");
3025
}
@@ -47,9 +42,9 @@ pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
4742
}
4843
}
4944
}
50-
None if is_absolute_path => acc.add_crate_roots(ctx),
45+
Qualified::Absolute => acc.add_crate_roots(ctx),
5146
// only show modules in a fresh UseTree
52-
None => {
47+
Qualified::No => {
5348
ctx.process_all_names(&mut |name, def| {
5449
let mac = match def {
5550
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac))
@@ -65,7 +60,7 @@ pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
6560

6661
match (core, mac.module(ctx.db).krate()) {
6762
// show derive dependencies for `core`/`std` derives
68-
(Some(core), mac_krate) if core == mac_krate && qualifier.is_none() => {}
63+
(Some(core), mac_krate) if core == mac_krate => {}
6964
_ => return acc.add_resolution(ctx, name, def),
7065
};
7166

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ide_db::FxHashSet;
55
use crate::{
66
context::{
77
CompletionContext, DotAccess, DotAccessKind, NameRefContext, NameRefKind,
8-
PathCompletionCtx, PathKind,
8+
PathCompletionCtx, PathKind, Qualified,
99
},
1010
CompletionItem, CompletionItemKind, Completions,
1111
};
@@ -50,8 +50,7 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
5050
match ctx.path_context() {
5151
Some(
5252
path_ctx @ PathCompletionCtx {
53-
is_absolute_path: false,
54-
qualifier: None,
53+
qualified: Qualified::No,
5554
kind: PathKind::Expr { .. },
5655
..
5756
},

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

+11-12
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ use hir::ScopeDef;
44
use ide_db::FxHashSet;
55

66
use crate::{
7-
context::{NameRefContext, NameRefKind, PathCompletionCtx, PathKind, PathQualifierCtx},
7+
context::{
8+
NameRefContext, NameRefKind, PathCompletionCtx, PathKind, PathQualifierCtx, Qualified,
9+
},
810
CompletionContext, Completions,
911
};
1012

1113
pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext) {
1214
let _p = profile::span("complete_expr_path");
1315

1416
let (
15-
is_absolute_path,
16-
qualifier,
17+
qualified,
1718
in_block_expr,
1819
in_loop_body,
1920
is_func_update,
@@ -33,14 +34,12 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
3334
ref ref_expr_parent,
3435
ref is_func_update,
3536
},
36-
is_absolute_path,
37-
ref qualifier,
37+
ref qualified,
3838
..
3939
})),
4040
..
4141
}) if ctx.qualifier_ctx.none() => (
42-
is_absolute_path,
43-
qualifier,
42+
qualified,
4443
in_block_expr,
4544
in_loop_body,
4645
is_func_update.is_some(),
@@ -61,8 +60,8 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
6160
}
6261
};
6362

64-
match qualifier {
65-
Some(PathQualifierCtx { is_infer_qualifier, resolution, .. }) => {
63+
match qualified {
64+
Qualified::With(PathQualifierCtx { is_infer_qualifier, resolution, .. }) => {
6665
if *is_infer_qualifier {
6766
ctx.traits_in_scope()
6867
.0
@@ -174,8 +173,8 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
174173
_ => (),
175174
}
176175
}
177-
None if is_absolute_path => acc.add_crate_roots(ctx),
178-
None => {
176+
Qualified::Absolute => acc.add_crate_roots(ctx),
177+
Qualified::No => {
179178
acc.add_nameref_keywords_with_colon(ctx);
180179
if let Some(adt) =
181180
ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
@@ -237,7 +236,7 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
237236
add_keyword("true", "true");
238237
add_keyword("false", "false");
239238

240-
if (in_condition && !is_absolute_path) || in_block_expr {
239+
if in_condition || in_block_expr {
241240
add_keyword("let", "let");
242241
}
243242

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{
44
context::{
55
IdentContext, NameContext, NameKind, NameRefContext, NameRefKind, PathCompletionCtx,
6-
PathKind, TypeLocation,
6+
PathKind, Qualified, TypeLocation,
77
},
88
CompletionContext, Completions,
99
};
@@ -15,8 +15,7 @@ pub(crate) fn complete_field_list(acc: &mut Completions, ctx: &CompletionContext
1515
kind:
1616
Some(NameRefKind::Path(PathCompletionCtx {
1717
has_macro_bang: false,
18-
is_absolute_path: false,
19-
qualifier: None,
18+
qualified: Qualified::No,
2019
parent: None,
2120
kind: PathKind::Type { location: TypeLocation::TupleField },
2221
has_type_args: false,

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

+12-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::{
44
completions::module_or_fn_macro,
5-
context::{ItemListKind, PathCompletionCtx, PathKind, PathQualifierCtx},
5+
context::{ItemListKind, PathCompletionCtx, PathKind, PathQualifierCtx, Qualified},
66
CompletionContext, Completions,
77
};
88

@@ -16,23 +16,17 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
1616
return;
1717
}
1818

19-
let (&is_absolute_path, path_qualifier, kind, is_trivial_path) = match ctx.path_context() {
20-
Some(
21-
ctx @ PathCompletionCtx {
22-
kind: PathKind::Item { kind },
23-
is_absolute_path,
24-
qualifier,
25-
..
26-
},
27-
) => (is_absolute_path, qualifier, Some(kind), ctx.is_trivial_path()),
19+
let (qualified, kind, is_trivial_path) = match ctx.path_context() {
20+
Some(ctx @ PathCompletionCtx { kind: PathKind::Item { kind }, qualified, .. }) => {
21+
(qualified, Some(kind), ctx.is_trivial_path())
22+
}
2823
Some(
2924
ctx @ PathCompletionCtx {
3025
kind: PathKind::Expr { in_block_expr: true, .. },
31-
is_absolute_path,
32-
qualifier,
26+
qualified,
3327
..
3428
},
35-
) => (is_absolute_path, qualifier, None, ctx.is_trivial_path()),
29+
) => (qualified, None, ctx.is_trivial_path()),
3630
_ => return,
3731
};
3832

@@ -49,8 +43,8 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
4943
return;
5044
}
5145

52-
match path_qualifier {
53-
Some(PathQualifierCtx { resolution, is_super_chain, .. }) => {
46+
match qualified {
47+
Qualified::With(PathQualifierCtx { resolution, is_super_chain, .. }) => {
5448
if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) = resolution {
5549
for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
5650
if let Some(def) = module_or_fn_macro(ctx.db, def) {
@@ -63,16 +57,16 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
6357
acc.add_keyword(ctx, "super::");
6458
}
6559
}
66-
None if is_absolute_path => acc.add_crate_roots(ctx),
67-
None if ctx.qualifier_ctx.none() => {
60+
Qualified::Absolute => acc.add_crate_roots(ctx),
61+
Qualified::No if ctx.qualifier_ctx.none() => {
6862
ctx.process_all_names(&mut |name, def| {
6963
if let Some(def) = module_or_fn_macro(ctx.db, def) {
7064
acc.add_resolution(ctx, name, def);
7165
}
7266
});
7367
acc.add_nameref_keywords_with_colon(ctx);
7468
}
75-
None => {}
69+
Qualified::No => {}
7670
}
7771
}
7872

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ide_db::FxHashSet;
55
use syntax::ast::Pat;
66

77
use crate::{
8-
context::{PathCompletionCtx, PathQualifierCtx, PatternRefutability},
8+
context::{PathCompletionCtx, PathQualifierCtx, PatternRefutability, Qualified},
99
CompletionContext, Completions,
1010
};
1111

@@ -111,10 +111,10 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
111111
fn pattern_path_completion(
112112
acc: &mut Completions,
113113
ctx: &CompletionContext,
114-
PathCompletionCtx { qualifier, is_absolute_path, .. }: &PathCompletionCtx,
114+
PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
115115
) {
116-
match qualifier {
117-
Some(PathQualifierCtx { resolution, is_super_chain, .. }) => {
116+
match qualified {
117+
Qualified::With(PathQualifierCtx { resolution, is_super_chain, .. }) => {
118118
if *is_super_chain {
119119
acc.add_keyword(ctx, "super::");
120120
}
@@ -197,8 +197,8 @@ fn pattern_path_completion(
197197
}
198198
}
199199
// qualifier can only be none here if we are in a TuplePat or RecordPat in which case special characters have to follow the path
200-
None if *is_absolute_path => acc.add_crate_roots(ctx),
201-
None => {
200+
Qualified::Absolute => acc.add_crate_roots(ctx),
201+
Qualified::No => {
202202
ctx.process_all_names(&mut |name, res| {
203203
// FIXME: properly filter here
204204
if let ScopeDef::ModuleDef(_) = res {

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use ide_db::SymbolKind;
33
use syntax::{ast::Expr, T};
44

55
use crate::{
6-
context::{NameRefContext, NameRefKind, PathCompletionCtx, PathKind, PatternContext},
6+
context::{
7+
NameRefContext, NameRefKind, PathCompletionCtx, PathKind, PatternContext, Qualified,
8+
},
79
CompletionContext, CompletionItem, CompletionItemKind, CompletionRelevance,
810
CompletionRelevancePostfixMatch, Completions,
911
};
@@ -19,7 +21,7 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
1921
NameRefKind::RecordExpr(record_expr)
2022
| NameRefKind::Path(PathCompletionCtx {
2123
kind: PathKind::Expr { is_func_update: Some(record_expr), .. },
22-
qualifier: None,
24+
qualified: Qualified::No,
2325
..
2426
}),
2527
),

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use hir::Documentation;
44
use ide_db::{imports::insert_use::ImportScope, SnippetCap};
55

66
use crate::{
7-
context::{ItemListKind, PathCompletionCtx, PathKind},
7+
context::{ItemListKind, PathCompletionCtx, PathKind, Qualified},
88
item::Builder,
99
CompletionContext, CompletionItem, CompletionItemKind, Completions, SnippetScope,
1010
};
@@ -18,8 +18,7 @@ fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str)
1818
pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
1919
let &can_be_stmt = match ctx.path_context() {
2020
Some(PathCompletionCtx {
21-
is_absolute_path: false,
22-
qualifier: None,
21+
qualified: Qualified::No,
2322
kind: PathKind::Expr { in_block_expr, .. },
2423
..
2524
}) => in_block_expr,
@@ -44,8 +43,7 @@ pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionConte
4443
pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
4544
let path_kind = match ctx.path_context() {
4645
Some(PathCompletionCtx {
47-
is_absolute_path: false,
48-
qualifier: None,
46+
qualified: Qualified::No,
4947
kind: kind @ (PathKind::Item { .. } | PathKind::Expr { in_block_expr: true, .. }),
5048
..
5149
}) => kind,

0 commit comments

Comments
 (0)