-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Show record field names in Enum completion #3169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Show record field names in Enum completion #3169
Conversation
Sorry for being nitpicky, but can you add some spaces next to the braces? It bugs me a little. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem - it's done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM up to some nitpicks.
crates/ra_hir_ty/src/lower.rs
Outdated
@@ -805,8 +806,9 @@ fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: StructId) -> PolyFn | |||
/// Build the type of a tuple struct constructor. | |||
fn type_for_struct_constructor(db: &impl HirDatabase, def: StructId) -> Binders<Ty> { | |||
let struct_data = db.struct_data(def.into()); | |||
if struct_data.variant_data.is_unit() { | |||
return type_for_adt(db, def.into()); // Unit struct | |||
match struct_data.variant_data.kind() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could be an if let
or if ... == StructKind::Unit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just changed it. Out of curiosity - what makes if let
preferable? Just that it's more concise? I would think it a lot of situations match
without a default is better because you'll get a compiler error if a new variant is added which forces you to explicitly decide how to handle the new variant. Of course, that doesn't really apply here because StructKind
isn't likely to change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in this case, as you said, StructKind
isn't likely to change, and we just want to handle the unit case as a special case here.
crates/ra_hir_ty/src/lower.rs
Outdated
@@ -830,8 +832,9 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariantId | |||
fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariantId) -> Binders<Ty> { | |||
let enum_data = db.enum_data(def.parent); | |||
let var_data = &enum_data.variants[def.local_id].variant_data; | |||
if var_data.is_unit() { | |||
return type_for_adt(db, def.parent.into()); // Unit variant | |||
match var_data.kind() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as could this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
bors r=flodiebold Thanks @adamrk ! |
3169: Show record field names in Enum completion r=flodiebold a=adamrk Adresses #2947. Previously the details shown when autocompleting an Enum variant would look like the variant was a tuple even if it was a record:  This change will show the names of the fields for a record and use curly braces instead of parentheses:  This required exposing the type `adt::StructKind` from `ra_hir` and adding a function ``` kind(self, db: &impl HirDatabase) -> StructKind ``` in the `impl` of `EnumVariant`. There was also a previously existing function `is_unit(self, db: &impl HirDatabase) -> bool` for `EnumVariant` which I removed because it seemed redundant after adding `kind`. Co-authored-by: adamrk <[email protected]>
Build succeeded
|
wow, thanks man! thanks for noticing my issue :) |
Adresses #2947.

Previously the details shown when autocompleting an Enum variant would look like the variant was a tuple even if it was a record:
This change will show the names of the fields for a record and use curly braces instead of parentheses:

This required exposing the type
adt::StructKind
fromra_hir
and adding a functionin the
impl
ofEnumVariant
.There was also a previously existing function
is_unit(self, db: &impl HirDatabase) -> bool
forEnumVariant
which I removed because it seemed redundant after addingkind
.