From 8e49bb664a03c29e25055a5a2ec03aa055462423 Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Tue, 23 Jul 2019 19:24:54 +0300 Subject: show local variable types in completion --- crates/ra_hir/src/source_binder.rs | 8 ++++++++ crates/ra_ide_api/src/completion/presentation.rs | 18 +++++++++++++----- .../completion_item__bindings_from_if_let.snap | 3 ++- .../snapshots/completion_item__bindings_from_let.snap | 4 +++- ...item__dont_show_both_completions_for_shadowing.snap | 3 ++- .../snapshots/completion_item__self_in_methods.snap | 3 ++- 6 files changed, 30 insertions(+), 9 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index fc9bc33d2..c2c6921cb 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -256,6 +256,14 @@ impl SourceAnalyzer { Some(self.infer.as_ref()?[pat_id].clone()) } + pub fn type_of_pat_by_id( + &self, + _db: &impl HirDatabase, + pat_id: expr::PatId, + ) -> Option { + Some(self.infer.as_ref()?[pat_id].clone()) + } + pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option { let expr_id = self.body_source_map.as_ref()?.node_expr(&call.clone().into())?; self.infer.as_ref()?.method_resolution(expr_id) diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index 98060947a..5cf55a496 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -1,5 +1,5 @@ //! This modules takes care of rendering various defenitions as completion items. -use hir::{Docs, HasSource, HirDisplay, PerNs, Resolution}; +use hir::{Docs, HasSource, HirDisplay, PerNs, Resolution, Ty}; use join_to_string::join; use ra_syntax::ast::NameOwner; use test_utils::tested_by; @@ -80,10 +80,18 @@ impl Completions { None, ), }; - CompletionItem::new(completion_kind, ctx.source_range(), local_name) - .kind(kind) - .set_documentation(docs) - .add_to(self) + + let mut completion_item = + CompletionItem::new(completion_kind, ctx.source_range(), local_name); + if let Resolution::LocalBinding(pat_id) = def { + let ty = ctx + .analyzer + .type_of_pat_by_id(ctx.db, pat_id.clone()) + .filter(|t| t != &Ty::Unknown) + .map(|t| t.display(ctx.db).to_string()); + completion_item = completion_item.set_detail(ty); + }; + completion_item.kind(kind).set_documentation(docs).add_to(self) } pub(crate) fn add_function(&mut self, ctx: &CompletionContext, func: hir::Function) { diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_if_let.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_if_let.snap index 2a22201ad..f94477b43 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_if_let.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_if_let.snap @@ -1,5 +1,5 @@ --- -created: "2019-05-23T22:23:35.122168608Z" +created: "2019-07-23T16:11:48.828805910Z" creator: insta@0.8.1 source: crates/ra_ide_api/src/completion/completion_item.rs expression: kind_completions @@ -18,6 +18,7 @@ expression: kind_completions delete: [214; 214), insert: "b", kind: Binding, + detail: "i32", }, CompletionItem { label: "quux", diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_let.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_let.snap index b9a5dc9c8..590e2a820 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_let.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__bindings_from_let.snap @@ -1,5 +1,5 @@ --- -created: "2019-05-23T22:23:35.122797188Z" +created: "2019-07-23T16:11:48.828811567Z" creator: insta@0.8.1 source: crates/ra_ide_api/src/completion/completion_item.rs expression: kind_completions @@ -19,6 +19,7 @@ expression: kind_completions delete: [79; 79), insert: "x", kind: Binding, + detail: "i32", }, CompletionItem { label: "y", @@ -26,5 +27,6 @@ expression: kind_completions delete: [79; 79), insert: "y", kind: Binding, + detail: "i32", }, ] diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_show_both_completions_for_shadowing.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_show_both_completions_for_shadowing.snap index 57434210d..158a2e5b9 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_show_both_completions_for_shadowing.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_show_both_completions_for_shadowing.snap @@ -1,5 +1,5 @@ --- -created: "2019-05-23T22:23:35.142044205Z" +created: "2019-07-23T16:11:48.860949870Z" creator: insta@0.8.1 source: crates/ra_ide_api/src/completion/completion_item.rs expression: kind_completions @@ -11,6 +11,7 @@ expression: kind_completions delete: [126; 126), insert: "bar", kind: Binding, + detail: "i32", }, CompletionItem { label: "foo", diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__self_in_methods.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__self_in_methods.snap index e1af94870..b7bcbe864 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__self_in_methods.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__self_in_methods.snap @@ -1,5 +1,5 @@ --- -created: "2019-05-23T22:23:35.141900902Z" +created: "2019-07-23T16:11:48.859812318Z" creator: insta@0.8.1 source: crates/ra_ide_api/src/completion/completion_item.rs expression: kind_completions @@ -18,5 +18,6 @@ expression: kind_completions delete: [25; 25), insert: "self", kind: Binding, + detail: "&{unknown}", }, ] -- cgit v1.2.3