aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src/completion')
-rw-r--r--crates/ra_ide_api/src/completion/complete_dot.rs65
1 files changed, 57 insertions, 8 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs
index 536ba36df..d43ff2eec 100644
--- a/crates/ra_ide_api/src/completion/complete_dot.rs
+++ b/crates/ra_ide_api/src/completion/complete_dot.rs
@@ -1,19 +1,36 @@
1use hir::{AdtDef, Ty, TypeCtor}; 1use hir::{AdtDef, Ty, TypeCtor};
2 2
3use crate::completion::{CompletionContext, Completions}; 3use crate::completion::completion_item::CompletionKind;
4use crate::{
5 completion::{completion_context::CompletionContext, completion_item::Completions},
6 CompletionItem,
7};
4use rustc_hash::FxHashSet; 8use rustc_hash::FxHashSet;
5 9
6/// Complete dot accesses, i.e. fields or methods (currently only fields). 10/// Complete dot accesses, i.e. fields or methods (and .await syntax).
7pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { 11pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
8 let receiver_ty = 12 let dot_receiver = match &ctx.dot_receiver {
9 match ctx.dot_receiver.as_ref().and_then(|it| ctx.analyzer.type_of(ctx.db, it)) { 13 Some(expr) => expr,
10 Some(it) => it, 14 _ => return,
11 None => return, 15 };
12 }; 16
17 let receiver_ty = match ctx.analyzer.type_of(ctx.db, &dot_receiver) {
18 Some(ty) => ty,
19 _ => return,
20 };
21
13 if !ctx.is_call { 22 if !ctx.is_call {
14 complete_fields(acc, ctx, receiver_ty.clone()); 23 complete_fields(acc, ctx, receiver_ty.clone());
15 } 24 }
16 complete_methods(acc, ctx, receiver_ty); 25 complete_methods(acc, ctx, receiver_ty.clone());
26
27 // Suggest .await syntax for types that implement Future trait
28 if ctx.analyzer.impls_future(ctx.db, receiver_ty) {
29 CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await")
30 .detail("expr.await")
31 .insert_text("await")
32 .add_to(acc);
33 }
17} 34}
18 35
19fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) { 36fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) {
@@ -406,4 +423,36 @@ mod tests {
406 "### 423 "###
407 ); 424 );
408 } 425 }
426
427 #[test]
428 fn test_completion_await_impls_future() {
429 assert_debug_snapshot_matches!(
430 do_completion(
431 r###"
432 //- /main.rs
433 use std::future::*;
434 struct A {}
435 impl Future for A {}
436 fn foo(a: A) {
437 a.<|>
438 }
439
440 //- /std/lib.rs
441 pub mod future {
442 pub trait Future {}
443 }
444 "###, CompletionKind::Keyword),
445 @r###"
446 ⋮[
447 ⋮ CompletionItem {
448 ⋮ label: "await",
449 ⋮ source_range: [74; 74),
450 ⋮ delete: [74; 74),
451 ⋮ insert: "await",
452 ⋮ detail: "expr.await",
453 ⋮ },
454 ⋮]
455 "###
456 )
457 }
409} 458}