Skip to content

Commit a2e7e4e

Browse files
committed
Auto merge of #12588 - Veykril:completions, r=Veykril
internal: More completion reorganizing
2 parents 427061d + 9048332 commit a2e7e4e

20 files changed

+411
-389
lines changed

crates/ide-completion/src/completions.rs

Lines changed: 135 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ pub(crate) mod vis;
2222

2323
use std::iter;
2424

25-
use hir::{db::HirDatabase, known, ScopeDef};
25+
use hir::{known, ScopeDef};
2626
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,
@@ -43,22 +46,6 @@ use crate::{
4346
CompletionContext, CompletionItem, CompletionItemKind,
4447
};
4548

46-
fn module_or_attr(db: &dyn HirDatabase, def: ScopeDef) -> Option<ScopeDef> {
47-
match def {
48-
ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(db) => Some(def),
49-
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => Some(def),
50-
_ => None,
51-
}
52-
}
53-
54-
fn module_or_fn_macro(db: &dyn HirDatabase, def: ScopeDef) -> Option<ScopeDef> {
55-
match def {
56-
ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_fn_like(db) => Some(def),
57-
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => Some(def),
58-
_ => None,
59-
}
60-
}
61-
6249
/// Represents an in-progress set of completions being built.
6350
#[derive(Debug, Default)]
6451
pub struct Completions {
@@ -181,6 +168,15 @@ impl Completions {
181168
self.add(render_resolution_simple(RenderContext::new(ctx), local_name, resolution).build());
182169
}
183170

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

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ use syntax::{
1717
};
1818

1919
use crate::{
20-
completions::module_or_attr,
21-
context::{CompletionContext, PathCompletionCtx, PathKind, Qualified},
20+
context::{AttrCtx, CompletionContext, PathCompletionCtx, Qualified},
2221
item::CompletionItem,
2322
Completions,
2423
};
@@ -28,7 +27,7 @@ mod derive;
2827
mod lint;
2928
mod repr;
3029

31-
pub(crate) use self::derive::complete_derive;
30+
pub(crate) use self::derive::complete_derive_path;
3231

3332
/// Complete inputs to known builtin attributes as well as derive attributes
3433
pub(crate) fn complete_known_attribute_input(
@@ -69,19 +68,13 @@ pub(crate) fn complete_known_attribute_input(
6968
Some(())
7069
}
7170

72-
pub(crate) fn complete_attribute(
71+
pub(crate) fn complete_attribute_path(
7372
acc: &mut Completions,
7473
ctx: &CompletionContext,
75-
path_ctx: &PathCompletionCtx,
74+
PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
75+
&AttrCtx { kind, annotated_item_kind }: &AttrCtx,
7676
) {
77-
let (qualified, is_inner, annotated_item_kind) = match path_ctx {
78-
&PathCompletionCtx {
79-
kind: PathKind::Attr { kind, annotated_item_kind },
80-
ref qualified,
81-
..
82-
} => (qualified, kind == AttrKind::Inner, annotated_item_kind),
83-
_ => return,
84-
};
77+
let is_inner = kind == AttrKind::Inner;
8578

8679
match qualified {
8780
Qualified::With {
@@ -94,8 +87,14 @@ pub(crate) fn complete_attribute(
9487
}
9588

9689
for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
97-
if let Some(def) = module_or_attr(ctx.db, def) {
98-
acc.add_resolution(ctx, name, def);
90+
match def {
91+
hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
92+
acc.add_macro(ctx, m, name)
93+
}
94+
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
95+
acc.add_module(ctx, m, name)
96+
}
97+
_ => (),
9998
}
10099
}
101100
return;
@@ -104,10 +103,12 @@ pub(crate) fn complete_attribute(
104103
Qualified::Absolute => acc.add_crate_roots(ctx),
105104
// only show modules in a fresh UseTree
106105
Qualified::No => {
107-
ctx.process_all_names(&mut |name, def| {
108-
if let Some(def) = module_or_attr(ctx.db, def) {
109-
acc.add_resolution(ctx, name, def);
106+
ctx.process_all_names(&mut |name, def| match def {
107+
hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
108+
acc.add_macro(ctx, m, name)
110109
}
110+
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => acc.add_module(ctx, m, name),
111+
_ => (),
111112
});
112113
acc.add_nameref_keywords_with_colon(ctx);
113114
}

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

Lines changed: 15 additions & 22 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

13-
pub(crate) fn complete_derive(
13+
pub(crate) fn complete_derive_path(
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 {
@@ -35,15 +29,14 @@ pub(crate) fn complete_derive(
3529
}
3630

3731
for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
38-
let add_def = match def {
39-
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => {
40-
!existing_derives.contains(&mac) && mac.is_derive(ctx.db)
32+
match def {
33+
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac))
34+
if !existing_derives.contains(&mac) && mac.is_derive(ctx.db) =>
35+
{
36+
acc.add_macro(ctx, mac, name)
4137
}
42-
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => true,
43-
_ => false,
44-
};
45-
if add_def {
46-
acc.add_resolution(ctx, name, def);
38+
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => acc.add_module(ctx, m, name),
39+
_ => (),
4740
}
4841
}
4942
}
@@ -57,16 +50,16 @@ pub(crate) fn complete_derive(
5750
{
5851
mac
5952
}
60-
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => {
61-
return acc.add_resolution(ctx, name, def);
53+
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
54+
return acc.add_module(ctx, m, name);
6255
}
6356
_ => return,
6457
};
6558

6659
match (core, mac.module(ctx.db).krate()) {
6760
// show derive dependencies for `core`/`std` derives
6861
(Some(core), mac_krate) if core == mac_krate => {}
69-
_ => return acc.add_resolution(ctx, name, def),
62+
_ => return acc.add_macro(ctx, mac, name),
7063
};
7164

7265
let name_ = name.to_smol_str();
@@ -99,7 +92,7 @@ pub(crate) fn complete_derive(
9992
item.lookup_by(lookup);
10093
item.add_to(acc);
10194
}
102-
None => acc.add_resolution(ctx, name, def),
95+
None => acc.add_macro(ctx, mac, name),
10396
}
10497
});
10598
acc.add_nameref_keywords_with_colon(ctx);

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

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

55
use crate::{
6-
context::{
7-
CompletionContext, DotAccess, DotAccessKind, PathCompletionCtx, PathKind, Qualified,
8-
},
6+
context::{CompletionContext, DotAccess, DotAccessKind, ExprCtx, PathCompletionCtx, Qualified},
97
CompletionItem, CompletionItemKind, Completions,
108
};
119

@@ -42,16 +40,22 @@ pub(crate) fn complete_undotted_self(
4240
acc: &mut Completions,
4341
ctx: &CompletionContext,
4442
path_ctx: &PathCompletionCtx,
43+
expr_ctx: &ExprCtx,
4544
) {
4645
if !ctx.config.enable_self_on_the_fly {
4746
return;
4847
}
49-
let self_param = match path_ctx {
50-
PathCompletionCtx {
51-
qualified: Qualified::No,
52-
kind: PathKind::Expr { self_param: Some(self_param), .. },
53-
..
54-
} 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,
5559
_ => return,
5660
};
5761

0 commit comments

Comments
 (0)