Don't show chaining hints for record literals and unit structs

This commit is contained in:
Laurențiu Nicola 2020-03-31 21:25:03 +03:00
parent f77fc158fc
commit 70960df437

View file

@ -75,8 +75,7 @@ fn get_chaining_hints(
return None; return None;
} }
let ty = sema.type_of_expr(&expr)?; if matches!(expr, ast::Expr::RecordLit(_)) {
if ty.is_unknown() {
return None; return None;
} }
@ -95,6 +94,17 @@ fn get_chaining_hints(
let next = tokens.next()?.kind(); let next = tokens.next()?.kind();
let next_next = tokens.next()?.kind(); let next_next = tokens.next()?.kind();
if next == SyntaxKind::WHITESPACE && next_next == SyntaxKind::DOT { if next == SyntaxKind::WHITESPACE && next_next == SyntaxKind::DOT {
let ty = sema.type_of_expr(&expr)?;
if ty.is_unknown() {
return None;
}
if matches!(expr, ast::Expr::PathExpr(_)) {
if let Some(Adt::Struct(st)) = ty.as_adt() {
if st.fields(sema.db).is_empty() {
return None;
}
}
}
let label = ty.display_truncated(sema.db, config.max_length).to_string(); let label = ty.display_truncated(sema.db, config.max_length).to_string();
acc.push(InlayHint { acc.push(InlayHint {
range: expr.syntax().text_range(), range: expr.syntax().text_range(),
@ -1154,32 +1164,35 @@ fn main() {
struct A { pub b: B } struct A { pub b: B }
struct B { pub c: C } struct B { pub c: C }
struct C(pub bool); struct C(pub bool);
struct D;
impl D {
fn foo(&self) -> i32 { 42 }
}
fn main() { fn main() {
let x = A { b: B { c: C(true) } } let x = A { b: B { c: C(true) } }
.b .b
.c .c
.0; .0;
let x = D
.foo();
}"#, }"#,
); );
assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintsConfig{ parameter_hints: false, type_hints: false, chaining_hints: true, max_length: None}).unwrap(), @r###" assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintsConfig{ parameter_hints: false, type_hints: false, chaining_hints: true, max_length: None}).unwrap(), @r###"
[ [
InlayHint { InlayHint {
range: [150; 221), range: [252; 323),
kind: ChainingHint, kind: ChainingHint,
label: "C", label: "C",
}, },
InlayHint { InlayHint {
range: [150; 198), range: [252; 300),
kind: ChainingHint, kind: ChainingHint,
label: "B", label: "B",
}, },
InlayHint { ]
range: [150; 175), "###);
kind: ChainingHint,
label: "A",
},
]"###);
} }
#[test] #[test]