Skip to content

Commit 3956a5b

Browse files
committed
Simplify
1 parent 06b0cbf commit 3956a5b

File tree

9 files changed

+91
-51
lines changed

9 files changed

+91
-51
lines changed

crates/ide_completion/src/completions/keyword.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
//! Completes keywords.
1+
//! Completes keywords, except:
2+
//! - `self`, `super` and `crate`, as these are considered part of path completions.
3+
//! - `await`, as this is a postfix completion we handle this in the postfix completions.
24
35
use syntax::{SyntaxKind, T};
46

@@ -25,18 +27,6 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
2527
return;
2628
}
2729

28-
// Suggest .await syntax for types that implement Future trait
29-
if let Some(receiver) = ctx.dot_receiver() {
30-
if let Some(ty) = ctx.sema.type_of_expr(receiver) {
31-
if ty.impls_future(ctx.db) {
32-
let mut item =
33-
CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await");
34-
item.kind(CompletionItemKind::Keyword).detail("expr.await");
35-
item.add_to(acc);
36-
}
37-
};
38-
}
39-
4030
let mut add_keyword = |kw, snippet| add_keyword(ctx, acc, kw, snippet);
4131

4232
let expects_assoc_item = ctx.expects_assoc_item();

crates/ide_completion/src/completions/postfix.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
4242
None => return,
4343
};
4444

45+
// Suggest .await syntax for types that implement Future trait
46+
if receiver_ty.impls_future(ctx.db) {
47+
let mut item = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await");
48+
item.kind(CompletionItemKind::Keyword).detail("expr.await");
49+
item.add_to(acc);
50+
}
51+
4552
let cap = match ctx.config.snippet_cap {
4653
Some(it) => it,
4754
None => return,

crates/ide_completion/src/completions/qualified_path.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ use hir::HasVisibility;
66
use rustc_hash::FxHashSet;
77
use syntax::{ast, AstNode};
88

9-
use crate::{context::PathCompletionContext, CompletionContext, Completions};
9+
use crate::{
10+
context::PathCompletionContext, patterns::ImmediateLocation, CompletionContext, Completions,
11+
};
1012

1113
pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) {
12-
if ctx.is_path_disallowed() {
14+
if ctx.is_path_disallowed() || ctx.has_impl_or_trait_prev_sibling() {
1315
return;
1416
}
1517
let (path, use_tree_parent) = match &ctx.path_context {
@@ -26,10 +28,11 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
2628

2729
let context_module = ctx.scope.module();
2830

29-
if ctx.expects_item() || ctx.expects_assoc_item() {
31+
if let Some(ImmediateLocation::ItemList | ImmediateLocation::Trait | ImmediateLocation::Impl) =
32+
ctx.completion_location
33+
{
3034
if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
31-
let module_scope = module.scope(ctx.db, context_module);
32-
for (name, def) in module_scope {
35+
for (name, def) in module.scope(ctx.db, context_module) {
3336
if let hir::ScopeDef::MacroDef(macro_def) = def {
3437
if macro_def.is_fn_like() {
3538
acc.add_macro(ctx, Some(name.clone()), macro_def);

crates/ide_completion/src/completions/unqualified_path.rs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
1212

1313
if ctx.in_use_tree() {
1414
// only show modules in a fresh UseTree
15-
cov_mark::hit!(only_completes_modules_in_import);
15+
cov_mark::hit!(unqualified_path_only_modules_in_import);
1616
ctx.scope.process_all_names(&mut |name, res| {
1717
if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
1818
acc.add_resolution(ctx, name, &res);
@@ -24,37 +24,39 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
2424
return;
2525
}
2626
std::array::IntoIter::new(["self", "super", "crate"]).for_each(|kw| acc.add_keyword(ctx, kw));
27-
if let Some(ImmediateLocation::Visibility(_)) = ctx.completion_location {
28-
return;
29-
}
3027

31-
if ctx.expects_item() || ctx.expects_assoc_item() {
32-
// only show macros in {Assoc}ItemList
33-
ctx.scope.process_all_names(&mut |name, res| {
34-
if let hir::ScopeDef::MacroDef(mac) = res {
35-
if mac.is_fn_like() {
36-
acc.add_macro(ctx, Some(name.clone()), mac);
28+
match &ctx.completion_location {
29+
Some(ImmediateLocation::Visibility(_)) => return,
30+
Some(ImmediateLocation::ItemList | ImmediateLocation::Trait | ImmediateLocation::Impl) => {
31+
// only show macros in {Assoc}ItemList
32+
ctx.scope.process_all_names(&mut |name, res| {
33+
if let hir::ScopeDef::MacroDef(mac) = res {
34+
if mac.is_fn_like() {
35+
acc.add_macro(ctx, Some(name.clone()), mac);
36+
}
3737
}
38-
}
39-
if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
40-
acc.add_resolution(ctx, name, &res);
41-
}
42-
});
43-
return;
44-
}
45-
46-
if matches!(&ctx.completion_location, Some(ImmediateLocation::TypeBound)) {
47-
ctx.scope.process_all_names(&mut |name, res| {
48-
let add_resolution = match res {
49-
ScopeDef::MacroDef(mac) => mac.is_fn_like(),
50-
ScopeDef::ModuleDef(hir::ModuleDef::Trait(_) | hir::ModuleDef::Module(_)) => true,
51-
_ => false,
52-
};
53-
if add_resolution {
54-
acc.add_resolution(ctx, name, &res);
55-
}
56-
});
57-
return;
38+
if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
39+
acc.add_resolution(ctx, name, &res);
40+
}
41+
});
42+
return;
43+
}
44+
Some(ImmediateLocation::TypeBound) => {
45+
ctx.scope.process_all_names(&mut |name, res| {
46+
let add_resolution = match res {
47+
ScopeDef::MacroDef(mac) => mac.is_fn_like(),
48+
ScopeDef::ModuleDef(hir::ModuleDef::Trait(_) | hir::ModuleDef::Module(_)) => {
49+
true
50+
}
51+
_ => false,
52+
};
53+
if add_resolution {
54+
acc.add_resolution(ctx, name, &res);
55+
}
56+
});
57+
return;
58+
}
59+
_ => (),
5860
}
5961

6062
if !ctx.expects_type() {

crates/ide_completion/src/patterns.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
//! Patterns telling us certain facts about current syntax element, they are used in completion context
2+
//!
3+
//! Most logic in this module first expands the token below the cursor to a maximum node that acts similar to the token itself.
4+
//! This means we for example expand a NameRef token to its outermost Path node, as semantically these act in the same location
5+
//! and the completions usually query for path specific things on the Path context instead. This simplifies some location handling.
26
37
use hir::Semantics;
48
use ide_db::RootDatabase;

crates/ide_completion/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ pub(crate) fn check_pattern_is_not_applicable(code: &str, check: fn(SyntaxElemen
201201

202202
pub(crate) fn get_all_items(config: CompletionConfig, code: &str) -> Vec<CompletionItem> {
203203
let (db, position) = position(code);
204-
crate::completions(&db, &config, position).unwrap().into()
204+
crate::completions(&db, &config, position).map_or_else(Vec::default, Into::into)
205205
}
206206

207207
fn check_no_completion(ra_fixture: &str) {

crates/ide_completion/src/tests/item.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ fn after_trait_name_in_trait_def() {
6868
}
6969

7070
#[test]
71-
fn after_trait_or_target_name_in_impl() {
71+
fn after_target_name_in_impl() {
7272
check(
7373
r"impl Trait $0",
7474
expect![[r#"
7575
kw where
7676
kw for
7777
"#]],
7878
);
79+
// FIXME: This should emit `kw where`
80+
check(r"impl Trait for Type $0", expect![[r#""#]]);
7981
}
8082

8183
#[test]

crates/ide_completion/src/tests/use_tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn check(ra_fixture: &str, expect: Expect) {
1010

1111
#[test]
1212
fn use_tree_start() {
13-
cov_mark::check!(only_completes_modules_in_import);
13+
cov_mark::check!(unqualified_path_only_modules_in_import);
1414
check(
1515
r#"
1616
//- /lib.rs crate:main deps:other_crate

crates/ide_completion/src/tests/visibility.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,35 @@ pub($0)
2323
"#]],
2424
);
2525
}
26+
27+
#[test]
28+
fn after_in_kw() {
29+
check(
30+
r#"
31+
pub(in $0)
32+
"#,
33+
expect![[r#"
34+
kw self
35+
kw super
36+
kw crate
37+
"#]],
38+
);
39+
}
40+
41+
#[test]
42+
fn qualified() {
43+
// FIXME: only show parent modules
44+
check(
45+
r#"
46+
mod foo {
47+
pub(in crate::$0)
48+
}
49+
50+
mod bar {}
51+
"#,
52+
expect![[r#"
53+
md bar
54+
md foo
55+
"#]],
56+
);
57+
}

0 commit comments

Comments
 (0)