hide type inlay hints for enum variant constructors and tuple struct constructors

This commit is contained in:
Heinenen 2021-12-21 17:26:37 +01:00 committed by me
parent 4ea1f58bf6
commit d7dfe93fc3

View file

@ -257,26 +257,20 @@ fn is_named_constructor(
}?; }?;
let expr = match expr { let expr = match expr {
ast::Expr::CallExpr(call) => match call.expr()? { ast::Expr::CallExpr(call) => match call.expr()? {
ast::Expr::PathExpr(p) => p, ast::Expr::PathExpr(path) => path,
_ => return None, _ => return None,
}, },
ast::Expr::PathExpr(path) => path,
_ => return None, _ => return None,
}; };
let path = expr.path()?; let path = expr.path()?;
// Check for tuple-struct or tuple-variant in which case we can check the last segment // If it exists, use qualifying segment as the constructor name.
let callable = sema.type_of_expr(&ast::Expr::PathExpr(expr))?.original.as_callable(sema.db); // If not, use the last segment.
let callable_kind = callable.map(|it| it.kind()); let qual_seg = match path.qualifier() {
if let Some(hir::CallableKind::TupleStruct(_) | hir::CallableKind::TupleEnumVariant(_)) = Some(qual) => qual.segment(),
callable_kind None => path.segment(),
{ }?;
if let Some(ctor) = path.segment() {
return (ctor.to_string() == ty_name).then(|| ());
}
}
// otherwise use the qualifying segment as the constructor name
let qual_seg = path.qualifier()?.segment()?;
let ctor_name = match qual_seg.kind()? { let ctor_name = match qual_seg.kind()? {
ast::PathSegmentKind::Name(name_ref) => { ast::PathSegmentKind::Name(name_ref) => {
match qual_seg.generic_arg_list().map(|it| it.generic_args()) { match qual_seg.generic_arg_list().map(|it| it.generic_args()) {
@ -1341,7 +1335,7 @@ fn main() {
} }
#[test] #[test]
fn skip_constructor_type_hints() { fn skip_constructor_and_enum_type_hints() {
check_with_config( check_with_config(
InlayHintsConfig { InlayHintsConfig {
type_hints: true, type_hints: true,
@ -1351,7 +1345,7 @@ fn main() {
max_length: None, max_length: None,
}, },
r#" r#"
//- minicore: try //- minicore: try, option
use core::ops::ControlFlow; use core::ops::ControlFlow;
struct Struct; struct Struct;
@ -1373,13 +1367,37 @@ impl Generic<i32> {
} }
} }
enum Enum {
Variant(u32)
}
fn times2(value: i32) -> i32 {
2 * value
}
fn main() { fn main() {
let enumb = Enum::Variant(0);
let strukt = Struct;
let strukt = Struct::new(); let strukt = Struct::new();
let tuple_struct = TupleStruct(); let tuple_struct = TupleStruct();
let generic0 = Generic::new(); let generic0 = Generic::new();
// ^^^^^^^^ Generic<i32> // ^^^^^^^^ Generic<i32>
let generic1 = Generic::<i32>::new(); let generic1 = Generic(0);
let generic2 = <Generic<i32>>::new(); // ^^^^^^^^ Generic<i32>
let generic2 = Generic::<i32>::new();
let generic3 = <Generic<i32>>::new();
let generic4 = Generic::<i32>(0);
let option = Some(0);
// ^^^^^^ Option<i32>
let func = times2;
// ^^^^ fn times2(i32) -> i32
let closure = |x: i32| x * 2;
// ^^^^^^^ |i32| -> i32
} }
fn fallible() -> ControlFlow<()> { fn fallible() -> ControlFlow<()> {