mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 06:03:58 +00:00
EnumVariant details for completion
This commit is contained in:
parent
6df1f71b7d
commit
9a97c10fda
5 changed files with 109 additions and 6 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -757,6 +757,7 @@ dependencies = [
|
||||||
"fst 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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)",
|
"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)",
|
"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)",
|
"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)",
|
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"ra_db 0.1.0",
|
"ra_db 0.1.0",
|
||||||
|
|
|
@ -6,6 +6,7 @@ authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
itertools = "0.8.0"
|
itertools = "0.8.0"
|
||||||
|
join_to_string = "0.1.3"
|
||||||
log = "0.4.5"
|
log = "0.4.5"
|
||||||
relative-path = "0.4.0"
|
relative-path = "0.4.0"
|
||||||
rayon = "1.0.2"
|
rayon = "1.0.2"
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use join_to_string::join;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext},
|
completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext},
|
||||||
};
|
};
|
||||||
|
@ -29,6 +31,15 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
hir::ModuleDef::Enum(e) => {
|
hir::ModuleDef::Enum(e) => {
|
||||||
e.variants(ctx.db).into_iter().for_each(|variant| {
|
e.variants(ctx.db).into_iter().for_each(|variant| {
|
||||||
if let Some(name) = variant.name(ctx.db) {
|
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(
|
CompletionItem::new(
|
||||||
CompletionKind::Reference,
|
CompletionKind::Reference,
|
||||||
ctx.source_range(),
|
ctx.source_range(),
|
||||||
|
@ -36,6 +47,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
)
|
)
|
||||||
.kind(CompletionItemKind::EnumVariant)
|
.kind(CompletionItemKind::EnumVariant)
|
||||||
.set_documentation(variant.docs(ctx.db))
|
.set_documentation(variant.docs(ctx.db))
|
||||||
|
.set_detail(Some(detail))
|
||||||
.add_to(acc)
|
.add_to(acc)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -116,7 +128,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn completes_enum_variant() {
|
fn completes_enum_variant() {
|
||||||
check_reference_completion(
|
check_reference_completion(
|
||||||
"reference_completion",
|
"enum_variant",
|
||||||
"
|
"
|
||||||
//- /lib.rs
|
//- /lib.rs
|
||||||
/// An enum
|
/// 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::<|> }
|
||||||
|
",
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
---
|
---
|
||||||
created: "2019-01-23T23:49:43.278245900+00:00"
|
created: "2019-01-25T16:44:04.640545300+00:00"
|
||||||
creator: insta@0.5.1
|
creator: insta@0.5.2
|
||||||
expression: kind_completions
|
expression: kind_completions
|
||||||
source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs"
|
source: crates/ra_ide_api/src/completion/completion_item.rs
|
||||||
---
|
---
|
||||||
[
|
[
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
|
@ -11,7 +11,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs"
|
||||||
kind: Some(
|
kind: Some(
|
||||||
EnumVariant
|
EnumVariant
|
||||||
),
|
),
|
||||||
detail: None,
|
detail: Some(
|
||||||
|
"()"
|
||||||
|
),
|
||||||
documentation: Some(
|
documentation: Some(
|
||||||
Documentation(
|
Documentation(
|
||||||
"Foo Variant"
|
"Foo Variant"
|
||||||
|
@ -29,7 +31,9 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs"
|
||||||
kind: Some(
|
kind: Some(
|
||||||
EnumVariant
|
EnumVariant
|
||||||
),
|
),
|
||||||
detail: None,
|
detail: Some(
|
||||||
|
"(i32)"
|
||||||
|
),
|
||||||
documentation: Some(
|
documentation: Some(
|
||||||
Documentation(
|
Documentation(
|
||||||
"Bar Variant with i32"
|
"Bar Variant with i32"
|
|
@ -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
|
||||||
|
}
|
||||||
|
]
|
Loading…
Reference in a new issue