Skip to content

Commit 16d2e79

Browse files
committed
internal: Remove unqualified_path completions module
1 parent 25d133e commit 16d2e79

File tree

14 files changed

+464
-421
lines changed

14 files changed

+464
-421
lines changed

crates/ide-completion/src/completions.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
pub(crate) mod attribute;
44
pub(crate) mod dot;
5+
pub(crate) mod expr;
56
pub(crate) mod extern_abi;
67
pub(crate) mod flyimport;
78
pub(crate) mod fn_param;
@@ -16,7 +17,7 @@ pub(crate) mod qualified_path;
1617
pub(crate) mod record;
1718
pub(crate) mod snippet;
1819
pub(crate) mod trait_impl;
19-
pub(crate) mod unqualified_path;
20+
pub(crate) mod r#type;
2021
pub(crate) mod use_;
2122
pub(crate) mod vis;
2223

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//! Completion of names from the current scope in expression position.
2+
3+
use hir::ScopeDef;
4+
5+
use crate::{
6+
context::{PathCompletionCtx, PathKind, PathQualifierCtx},
7+
CompletionContext, Completions,
8+
};
9+
10+
pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext) {
11+
let _p = profile::span("complete_expr_path");
12+
if ctx.is_path_disallowed() {
13+
return;
14+
}
15+
16+
let (&is_absolute_path, qualifier) = match &ctx.path_context {
17+
Some(PathCompletionCtx {
18+
kind: Some(PathKind::Expr), is_absolute_path, qualifier, ..
19+
}) => (is_absolute_path, qualifier),
20+
_ => return,
21+
};
22+
23+
match qualifier {
24+
Some(PathQualifierCtx { .. }) => return,
25+
None if is_absolute_path => acc.add_crate_roots(ctx),
26+
None => {
27+
acc.add_nameref_keywords_with_colon(ctx);
28+
if let Some(hir::Adt::Enum(e)) =
29+
ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
30+
{
31+
super::enum_variants_with_paths(acc, ctx, e, |acc, ctx, variant, path| {
32+
acc.add_qualified_enum_variant(ctx, variant, path)
33+
});
34+
}
35+
ctx.process_all_names(&mut |name, def| {
36+
use hir::{GenericParam::*, ModuleDef::*};
37+
let add_resolution = match def {
38+
ScopeDef::GenericParam(LifetimeParam(_)) | ScopeDef::Label(_) => false,
39+
// Don't suggest attribute macros and derives.
40+
ScopeDef::ModuleDef(Macro(mac)) => mac.is_fn_like(ctx.db),
41+
_ => true,
42+
};
43+
if add_resolution {
44+
acc.add_resolution(ctx, name, def);
45+
}
46+
});
47+
}
48+
}
49+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
};
88

99
pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext) {
10-
let _p = profile::span("complete_unqualified_path");
10+
let _p = profile::span("complete_item_list");
1111
if ctx.is_path_disallowed() || ctx.has_unfinished_impl_or_trait_prev_sibling() {
1212
return;
1313
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//! Completion of names from the current scope in type position.
2+
3+
use hir::ScopeDef;
4+
use syntax::{ast, AstNode};
5+
6+
use crate::{
7+
context::{PathCompletionCtx, PathKind, PathQualifierCtx},
8+
patterns::ImmediateLocation,
9+
CompletionContext, Completions,
10+
};
11+
12+
pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext) {
13+
let _p = profile::span("complete_type_path");
14+
if ctx.is_path_disallowed() {
15+
return;
16+
}
17+
18+
let (&is_absolute_path, qualifier) = match &ctx.path_context {
19+
Some(PathCompletionCtx {
20+
kind: Some(PathKind::Type), is_absolute_path, qualifier, ..
21+
}) => (is_absolute_path, qualifier),
22+
_ => return,
23+
};
24+
25+
match qualifier {
26+
Some(PathQualifierCtx { .. }) => return,
27+
None if is_absolute_path => acc.add_crate_roots(ctx),
28+
None => {
29+
acc.add_nameref_keywords_with_colon(ctx);
30+
if let Some(ImmediateLocation::TypeBound) = &ctx.completion_location {
31+
ctx.process_all_names(&mut |name, res| {
32+
let add_resolution = match res {
33+
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => mac.is_fn_like(ctx.db),
34+
ScopeDef::ModuleDef(
35+
hir::ModuleDef::Trait(_) | hir::ModuleDef::Module(_),
36+
) => true,
37+
_ => false,
38+
};
39+
if add_resolution {
40+
acc.add_resolution(ctx, name, res);
41+
}
42+
});
43+
return;
44+
}
45+
if let Some(ImmediateLocation::GenericArgList(arg_list)) = &ctx.completion_location {
46+
if let Some(path_seg) = arg_list.syntax().parent().and_then(ast::PathSegment::cast)
47+
{
48+
if let Some(hir::PathResolution::Def(hir::ModuleDef::Trait(trait_))) =
49+
ctx.sema.resolve_path(&path_seg.parent_path())
50+
{
51+
trait_.items(ctx.sema.db).into_iter().for_each(|it| {
52+
if let hir::AssocItem::TypeAlias(alias) = it {
53+
acc.add_type_alias_with_eq(ctx, alias)
54+
}
55+
});
56+
}
57+
}
58+
}
59+
ctx.process_all_names(&mut |name, def| {
60+
use hir::{GenericParam::*, ModuleDef::*};
61+
let add_resolution = match def {
62+
ScopeDef::GenericParam(LifetimeParam(_)) | ScopeDef::Label(_) => false,
63+
// no values in type places
64+
ScopeDef::ModuleDef(Function(_) | Variant(_) | Static(_))
65+
| ScopeDef::Local(_) => false,
66+
// unless its a constant in a generic arg list position
67+
ScopeDef::ModuleDef(Const(_)) | ScopeDef::GenericParam(ConstParam(_)) => {
68+
ctx.expects_generic_arg()
69+
}
70+
ScopeDef::ImplSelfType(_) => {
71+
!ctx.previous_token_is(syntax::T![impl])
72+
&& !ctx.previous_token_is(syntax::T![for])
73+
}
74+
// Don't suggest attribute macros and derives.
75+
ScopeDef::ModuleDef(Macro(mac)) => mac.is_fn_like(ctx.db),
76+
// Type things are fine
77+
ScopeDef::ModuleDef(
78+
BuiltinType(_) | Adt(_) | Module(_) | Trait(_) | TypeAlias(_),
79+
)
80+
| ScopeDef::AdtSelfType(_)
81+
| ScopeDef::Unknown
82+
| ScopeDef::GenericParam(TypeParam(_)) => true,
83+
};
84+
if add_resolution {
85+
acc.add_resolution(ctx, name, def);
86+
}
87+
});
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)