Add field for text edits to InlayHint

This commit is contained in:
Ryo Yoshida 2023-04-06 18:53:34 +09:00
parent ac03de773f
commit fcbc250723
No known key found for this signature in database
GPG Key ID: E25698A930586171
12 changed files with 54 additions and 7 deletions

View File

@ -16,6 +16,7 @@ use syntax::{
ast::{self, AstNode},
match_ast, NodeOrToken, SyntaxNode, TextRange,
};
use text_edit::TextEdit;
use crate::{navigation_target::TryToNav, FileId};
@ -113,14 +114,26 @@ pub struct InlayHint {
pub kind: InlayKind,
/// The actual label to show in the inlay hint.
pub label: InlayHintLabel,
/// Text edit to apply when "accepting" this inlay hint.
pub text_edit: Option<TextEdit>,
}
impl InlayHint {
fn closing_paren(range: TextRange) -> InlayHint {
InlayHint { range, kind: InlayKind::ClosingParenthesis, label: InlayHintLabel::from(")") }
InlayHint {
range,
kind: InlayKind::ClosingParenthesis,
label: InlayHintLabel::from(")"),
text_edit: None,
}
}
fn opening_paren(range: TextRange) -> InlayHint {
InlayHint { range, kind: InlayKind::OpeningParenthesis, label: InlayHintLabel::from("(") }
InlayHint {
range,
kind: InlayKind::OpeningParenthesis,
label: InlayHintLabel::from("("),
text_edit: None,
}
}
}

View File

@ -135,6 +135,7 @@ pub(super) fn hints(
))),
None,
),
text_edit: None,
});
}
if !postfix && needs_inner_parens {

View File

@ -12,9 +12,10 @@ use syntax::{
match_ast,
};
use crate::{inlay_hints::closure_has_block_body, InlayHint, InlayHintsConfig, InlayKind};
use super::label_of_ty;
use crate::{
inlay_hints::{closure_has_block_body, label_of_ty},
InlayHint, InlayHintsConfig, InlayKind,
};
pub(super) fn hints(
acc: &mut Vec<InlayHint>,
@ -50,6 +51,7 @@ pub(super) fn hints(
},
kind: InlayKind::Type,
label,
text_edit: None,
});
Some(())

View File

@ -49,7 +49,12 @@ pub(super) fn hints(
(true, false) => "&",
_ => return,
};
acc.push(InlayHint { range, kind: InlayKind::BindingMode, label: r.to_string().into() });
acc.push(InlayHint {
range,
kind: InlayKind::BindingMode,
label: r.to_string().into(),
text_edit: None,
});
});
match pat {
ast::Pat::IdentPat(pat) if pat.ref_token().is_none() && pat.mut_token().is_none() => {
@ -63,6 +68,7 @@ pub(super) fn hints(
range: pat.syntax().text_range(),
kind: InlayKind::BindingMode,
label: bm.to_string().into(),
text_edit: None,
});
}
ast::Pat::OrPat(pat) if !pattern_adjustments.is_empty() && outer_paren_pat.is_none() => {

View File

@ -61,6 +61,7 @@ pub(super) fn hints(
range: expr.syntax().text_range(),
kind: InlayKind::Chaining,
label: label_of_ty(famous_defs, config, ty)?,
text_edit: None,
});
}
}
@ -120,6 +121,7 @@ fn main() {
},
"",
],
text_edit: None,
},
InlayHint {
range: 147..154,
@ -140,6 +142,7 @@ fn main() {
},
"",
],
text_edit: None,
},
]
"#]],
@ -205,6 +208,7 @@ fn main() {
},
"",
],
text_edit: None,
},
InlayHint {
range: 143..179,
@ -225,6 +229,7 @@ fn main() {
},
"",
],
text_edit: None,
},
]
"#]],
@ -274,6 +279,7 @@ fn main() {
},
"",
],
text_edit: None,
},
InlayHint {
range: 143..179,
@ -294,6 +300,7 @@ fn main() {
},
"",
],
text_edit: None,
},
]
"#]],
@ -357,6 +364,7 @@ fn main() {
},
"<i32, bool>>",
],
text_edit: None,
},
InlayHint {
range: 246..265,
@ -390,6 +398,7 @@ fn main() {
},
"<i32, bool>>",
],
text_edit: None,
},
]
"#]],
@ -455,6 +464,7 @@ fn main() {
},
" = ()>",
],
text_edit: None,
},
InlayHint {
range: 174..224,
@ -488,6 +498,7 @@ fn main() {
},
" = ()>",
],
text_edit: None,
},
InlayHint {
range: 174..206,
@ -521,6 +532,7 @@ fn main() {
},
" = ()>",
],
text_edit: None,
},
InlayHint {
range: 174..189,
@ -541,6 +553,7 @@ fn main() {
},
"",
],
text_edit: None,
},
]
"#]],
@ -590,6 +603,7 @@ fn main() {
},
"",
],
text_edit: None,
},
InlayHint {
range: 145..185,
@ -610,6 +624,7 @@ fn main() {
},
"",
],
text_edit: None,
},
InlayHint {
range: 145..168,
@ -630,6 +645,7 @@ fn main() {
},
"",
],
text_edit: None,
},
InlayHint {
range: 222..228,
@ -648,6 +664,7 @@ fn main() {
tooltip: "",
},
],
text_edit: None,
},
]
"#]],

View File

@ -112,6 +112,7 @@ pub(super) fn hints(
range: closing_token.text_range(),
kind: InlayKind::ClosingBrace,
label: InlayHintLabel::simple(label, None, linked_location),
text_edit: None,
});
None

View File

@ -43,6 +43,7 @@ pub(super) fn hints(
range: param_list.syntax().text_range(),
kind: InlayKind::ClosureReturnType,
label: label_of_ty(famous_defs, config, ty)?,
text_edit: None,
});
Some(())
}

View File

@ -75,6 +75,7 @@ fn variant_hints(
})),
None,
),
text_edit: None,
});
Some(())

View File

@ -25,6 +25,7 @@ pub(super) fn hints(
range: t.text_range(),
kind: InlayKind::Lifetime,
label: label.into(),
text_edit: None,
};
let param_list = func.param_list()?;
@ -189,12 +190,14 @@ pub(super) fn hints(
if is_empty { "" } else { ", " }
)
.into(),
text_edit: None,
});
}
(None, allocated_lifetimes) => acc.push(InlayHint {
range: func.name()?.syntax().text_range(),
kind: InlayKind::GenericParamList,
label: format!("<{}>", allocated_lifetimes.iter().format(", "),).into(),
text_edit: None,
}),
}
Some(())

View File

@ -34,6 +34,7 @@ pub(super) fn hints(
range: t.text_range(),
kind: InlayKind::Lifetime,
label: "'static".to_owned().into(),
text_edit: None,
});
}
}

View File

@ -57,6 +57,7 @@ pub(super) fn hints(
range,
kind: InlayKind::Parameter,
label: InlayHintLabel::simple(param_name, None, linked_location),
text_edit: None,
}
});

View File

@ -510,7 +510,7 @@ pub(crate) fn inlay_hint(
| InlayKind::AdjustmentPostfix
| InlayKind::ClosingBrace => None,
},
text_edits: None,
text_edits: inlay_hint.text_edit.map(|it| text_edit_vec(line_index, it)),
data: None,
tooltip,
label,