From 14f19c73e9b548a654e95443095e1d267bc64efe Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 1 Feb 2023 11:38:39 +0100 Subject: [PATCH] fix: Don't render fieldless discriminant inlay hints for datacarrying enums --- crates/ide/src/inlay_hints.rs | 4 +- crates/ide/src/inlay_hints/discriminant.rs | 71 ++++++++++++++-------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 3d4a3466e2..a9581db9ad 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -422,11 +422,9 @@ fn hints( // static type elisions ast::Item::Static(it) => implicit_static::hints(hints, config, Either::Left(it)), ast::Item::Const(it) => implicit_static::hints(hints, config, Either::Right(it)), + ast::Item::Enum(it) => discriminant::enum_hints(hints, famous_defs, config, file_id, it), _ => None, }, - ast::Variant(v) => { - discriminant::hints(hints, famous_defs, config, file_id, &v) - }, // FIXME: fn-ptr type, dyn fn type, and trait object type elisions ast::Type(_) => None, _ => None, diff --git a/crates/ide/src/inlay_hints/discriminant.rs b/crates/ide/src/inlay_hints/discriminant.rs index 310295cc37..c5c947150b 100644 --- a/crates/ide/src/inlay_hints/discriminant.rs +++ b/crates/ide/src/inlay_hints/discriminant.rs @@ -4,29 +4,40 @@ //! Bar/* = 0*/, //! } //! ``` -use ide_db::{base_db::FileId, famous_defs::FamousDefs}; +use hir::Semantics; +use ide_db::{base_db::FileId, famous_defs::FamousDefs, RootDatabase}; use syntax::ast::{self, AstNode, HasName}; use crate::{ DiscriminantHints, InlayHint, InlayHintLabel, InlayHintsConfig, InlayKind, InlayTooltip, }; -pub(super) fn hints( +pub(super) fn enum_hints( acc: &mut Vec, FamousDefs(sema, _): &FamousDefs<'_, '_>, config: &InlayHintsConfig, _: FileId, + enum_: ast::Enum, +) -> Option<()> { + let disabled = match config.discriminant_hints { + DiscriminantHints::Always => false, + DiscriminantHints::Fieldless => sema.to_def(&enum_)?.is_data_carrying(sema.db), + DiscriminantHints::Never => true, + }; + if disabled { + return None; + } + for variant in enum_.variant_list()?.variants() { + variant_hints(acc, sema, &variant); + } + None +} + +fn variant_hints( + acc: &mut Vec, + sema: &Semantics<'_, RootDatabase>, variant: &ast::Variant, ) -> Option<()> { - let field_list = match config.discriminant_hints { - DiscriminantHints::Always => variant.field_list(), - DiscriminantHints::Fieldless => match variant.field_list() { - Some(_) => return None, - None => None, - }, - DiscriminantHints::Never => return None, - }; - if variant.eq_token().is_some() { return None; } @@ -39,7 +50,7 @@ pub(super) fn hints( let d = v.eval(sema.db); acc.push(InlayHint { - range: match field_list { + range: match variant.field_list() { Some(field_list) => name.syntax().text_range().cover(field_list.syntax().text_range()), None => name.syntax().text_range(), }, @@ -91,15 +102,30 @@ mod tests { check_discriminants( r#" enum Enum { - Variant, - //^^^^^^^0 - Variant1, - //^^^^^^^^1 - Variant2, - //^^^^^^^^2 - Variant5 = 5, - Variant6, - //^^^^^^^^6 + Variant, +//^^^^^^^0 + Variant1, +//^^^^^^^^1 + Variant2, +//^^^^^^^^2 + Variant5 = 5, + Variant6, +//^^^^^^^^6 +} +"#, + ); + check_discriminants_fieldless( + r#" +enum Enum { + Variant, +//^^^^^^^0 + Variant1, +//^^^^^^^^1 + Variant2, +//^^^^^^^^2 + Variant5 = 5, + Variant6, +//^^^^^^^^6 } "#, ); @@ -133,13 +159,10 @@ enum Enum { enum Enum { Variant(), Variant1, - //^^^^^^^^1 Variant2 {}, Variant3, - //^^^^^^^^3 Variant5 = 5, Variant6, - //^^^^^^^^6 } "#, );