From 9a97c10fdafd0148b0f198e28b9694876cd0d672 Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Fri, 25 Jan 2019 11:50:59 -0500 Subject: EnumVariant details for completion --- Cargo.lock | 1 + crates/ra_ide_api/Cargo.toml | 1 + crates/ra_ide_api/src/completion/complete_path.rs | 35 +++++++++++- .../snapshots/completion_item__enum_variant.snap | 48 ++++++++++++++++ ...completion_item__enum_variant_with_details.snap | 64 ++++++++++++++++++++++ .../completion_item__reference_completion.snap | 44 --------------- 6 files changed, 148 insertions(+), 45 deletions(-) create mode 100644 crates/ra_ide_api/src/completion/snapshots/completion_item__enum_variant.snap create mode 100644 crates/ra_ide_api/src/completion/snapshots/completion_item__enum_variant_with_details.snap delete mode 100644 crates/ra_ide_api/src/completion/snapshots/completion_item__reference_completion.snap diff --git a/Cargo.lock b/Cargo.lock index bfa0bf2ed..a8113a87d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -757,6 +757,7 @@ dependencies = [ "fst 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "insta 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "join_to_string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "ra_db 0.1.0", diff --git a/crates/ra_ide_api/Cargo.toml b/crates/ra_ide_api/Cargo.toml index 61942bbbb..dfdddea50 100644 --- a/crates/ra_ide_api/Cargo.toml +++ b/crates/ra_ide_api/Cargo.toml @@ -6,6 +6,7 @@ authors = ["Aleksey Kladov "] [dependencies] itertools = "0.8.0" +join_to_string = "0.1.3" log = "0.4.5" relative-path = "0.4.0" rayon = "1.0.2" diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index e3f1d42f8..172aedf95 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -1,3 +1,5 @@ +use join_to_string::join; + use crate::{ completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext}, }; @@ -29,6 +31,15 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { hir::ModuleDef::Enum(e) => { e.variants(ctx.db).into_iter().for_each(|variant| { if let Some(name) = variant.name(ctx.db) { + let detail_types = variant + .fields(ctx.db) + .into_iter() + .map(|field| field.ty(ctx.db)); + let detail = join(detail_types) + .separator(", ") + .surround_with("(", ")") + .to_string(); + CompletionItem::new( CompletionKind::Reference, ctx.source_range(), @@ -36,6 +47,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { ) .kind(CompletionItemKind::EnumVariant) .set_documentation(variant.docs(ctx.db)) + .set_detail(Some(detail)) .add_to(acc) } }); @@ -116,7 +128,7 @@ mod tests { #[test] fn completes_enum_variant() { check_reference_completion( - "reference_completion", + "enum_variant", " //- /lib.rs /// An enum @@ -130,4 +142,25 @@ mod tests { ", ); } + + #[test] + fn completes_enum_variant_with_details() { + check_reference_completion( + "enum_variant_with_details", + " + //- /lib.rs + struct S { field: u32 } + /// An enum + enum E { + /// Foo Variant (empty) + Foo, + /// Bar Variant with i32 and u32 + Bar(i32, u32), + /// + S(S), + } + fn foo() { let _ = E::<|> } + ", + ); + } } diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_variant.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_variant.snap new file mode 100644 index 000000000..8c84439b2 --- /dev/null +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_variant.snap @@ -0,0 +1,48 @@ +--- +created: "2019-01-25T16:44:04.640545300+00:00" +creator: insta@0.5.2 +expression: kind_completions +source: crates/ra_ide_api/src/completion/completion_item.rs +--- +[ + CompletionItem { + completion_kind: Reference, + label: "Foo", + kind: Some( + EnumVariant + ), + detail: Some( + "()" + ), + documentation: Some( + Documentation( + "Foo Variant" + ) + ), + lookup: None, + insert_text: None, + insert_text_format: PlainText, + source_range: [116; 116), + text_edit: None + }, + CompletionItem { + completion_kind: Reference, + label: "Bar", + kind: Some( + EnumVariant + ), + detail: Some( + "(i32)" + ), + documentation: Some( + Documentation( + "Bar Variant with i32" + ) + ), + lookup: None, + insert_text: None, + insert_text_format: PlainText, + source_range: [116; 116), + text_edit: None + } +] diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_variant_with_details.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_variant_with_details.snap new file mode 100644 index 000000000..384634517 --- /dev/null +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_variant_with_details.snap @@ -0,0 +1,64 @@ +--- +created: "2019-01-25T16:44:04.641542400+00:00" +creator: insta@0.5.2 +expression: kind_completions +source: crates/ra_ide_api/src/completion/completion_item.rs +--- +[ + CompletionItem { + completion_kind: Reference, + label: "Foo", + kind: Some( + EnumVariant + ), + detail: Some( + "()" + ), + documentation: Some( + Documentation( + "Foo Variant (empty)" + ) + ), + lookup: None, + insert_text: None, + insert_text_format: PlainText, + source_range: [180; 180), + text_edit: None + }, + CompletionItem { + completion_kind: Reference, + label: "Bar", + kind: Some( + EnumVariant + ), + detail: Some( + "(i32, u32)" + ), + documentation: Some( + Documentation( + "Bar Variant with i32 and u32" + ) + ), + lookup: None, + insert_text: None, + insert_text_format: PlainText, + source_range: [180; 180), + text_edit: None + }, + CompletionItem { + completion_kind: Reference, + label: "S", + kind: Some( + EnumVariant + ), + detail: Some( + "(S)" + ), + documentation: None, + lookup: None, + insert_text: None, + insert_text_format: PlainText, + source_range: [180; 180), + text_edit: None + } +] diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__reference_completion.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__reference_completion.snap deleted file mode 100644 index e46f7807b..000000000 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__reference_completion.snap +++ /dev/null @@ -1,44 +0,0 @@ ---- -created: "2019-01-23T23:49:43.278245900+00:00" -creator: insta@0.5.1 -expression: kind_completions -source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" ---- -[ - CompletionItem { - completion_kind: Reference, - label: "Foo", - kind: Some( - EnumVariant - ), - detail: None, - documentation: Some( - Documentation( - "Foo Variant" - ) - ), - lookup: None, - insert_text: None, - insert_text_format: PlainText, - source_range: [116; 116), - text_edit: None - }, - CompletionItem { - completion_kind: Reference, - label: "Bar", - kind: Some( - EnumVariant - ), - detail: None, - documentation: Some( - Documentation( - "Bar Variant with i32" - ) - ), - lookup: None, - insert_text: None, - insert_text_format: PlainText, - source_range: [116; 116), - text_edit: None - } -] -- cgit v1.2.3