diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-08-04 10:42:17 +0100 |
---|---|---|
committer | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-08-04 10:42:17 +0100 |
commit | 4912cc35afe96b1506e5cda1be34d3e2418bc702 (patch) | |
tree | fa40d55d943e18ce52ea072cc26c74fcf519f911 /crates/ra_ide_api/src/completion/complete_dot.rs | |
parent | 658382c1f5d27813daf93495f4cbd5aada1f0567 (diff) | |
parent | 9f9c5aa65914763f5a9ff80230544fea48cbcdf1 (diff) |
Merge #1634
1634: Implement .await completion for futures r=flodiebold a=eupn
Closes #1263 with completion for `.await` syntax for types that are implementing `std::future::Future` trait.
r? @flodiebold
Co-authored-by: Evgenii P <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/completion/complete_dot.rs')
-rw-r--r-- | crates/ra_ide_api/src/completion/complete_dot.rs | 65 |
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 @@ | |||
1 | use hir::{AdtDef, Ty, TypeCtor}; | 1 | use hir::{AdtDef, Ty, TypeCtor}; |
2 | 2 | ||
3 | use crate::completion::{CompletionContext, Completions}; | 3 | use crate::completion::completion_item::CompletionKind; |
4 | use crate::{ | ||
5 | completion::{completion_context::CompletionContext, completion_item::Completions}, | ||
6 | CompletionItem, | ||
7 | }; | ||
4 | use rustc_hash::FxHashSet; | 8 | use 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). |
7 | pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { | 11 | pub(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 | ||
19 | fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) { | 36 | fn 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 | } |