Add text edit to discriminant hints

This commit is contained in:
Lukas Wirth 2024-10-23 09:57:00 +02:00
parent fd17fa10a2
commit bfaad1431b

View file

@ -8,6 +8,7 @@ use hir::Semantics;
use ide_db::{famous_defs::FamousDefs, RootDatabase};
use span::EditionedFileId;
use syntax::ast::{self, AstNode, HasName};
use text_edit::TextEdit;
use crate::{
DiscriminantHints, InlayHint, InlayHintLabel, InlayHintPosition, InlayHintsConfig, InlayKind,
@ -65,11 +66,11 @@ fn variant_hints(
let eq_ = if eq_token.is_none() { " =" } else { "" };
let label = InlayHintLabel::simple(
match d {
Ok(x) => {
if x >= 10 {
format!("{eq_} {x} ({x:#X})")
Ok(val) => {
if val >= 10 {
format!("{eq_} {val} ({val:#X})")
} else {
format!("{eq_} {x}")
format!("{eq_} {val}")
}
}
Err(_) => format!("{eq_} ?"),
@ -87,7 +88,7 @@ fn variant_hints(
},
kind: InlayKind::Discriminant,
label,
text_edit: None,
text_edit: d.ok().map(|val| TextEdit::insert(range.start(), format!("{eq_} {val}"))),
position: InlayHintPosition::After,
pad_left: false,
pad_right: false,