diff --git a/Cargo.lock b/Cargo.lock index bfa0bf2edb..a8113a87db 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 61942bbbb3..dfdddea501 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 e3f1d42f81..172aedf95d 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__reference_completion.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_variant.snap similarity index 78% rename from crates/ra_ide_api/src/completion/snapshots/completion_item__reference_completion.snap rename to crates/ra_ide_api/src/completion/snapshots/completion_item__enum_variant.snap index e46f7807ba..8c84439b29 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__reference_completion.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_variant.snap @@ -1,8 +1,8 @@ --- -created: "2019-01-23T23:49:43.278245900+00:00" -creator: insta@0.5.1 +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" +source: crates/ra_ide_api/src/completion/completion_item.rs --- [ CompletionItem { @@ -11,7 +11,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( EnumVariant ), - detail: None, + detail: Some( + "()" + ), documentation: Some( Documentation( "Foo Variant" @@ -29,7 +31,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" kind: Some( EnumVariant ), - detail: None, + detail: Some( + "(i32)" + ), documentation: Some( Documentation( "Bar Variant with i32" 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 0000000000..3846345171 --- /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 + } +]