Skip to content

Improve type hints behavior #1652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 41 additions & 57 deletions crates/ra_ide_api/src/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@ use ra_syntax::{
SmolStr, SyntaxKind, SyntaxNode, TextRange,
};

#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq)]
pub enum InlayKind {
LetBindingType,
ClosureParameterType,
ForExpressionBindingType,
IfExpressionType,
WhileLetExpressionType,
MatchArmType,
TypeHint,
}

#[derive(Debug)]
Expand Down Expand Up @@ -46,7 +41,7 @@ fn get_inlay_hints(
}
let pat = let_statement.pat()?;
let analyzer = SourceAnalyzer::new(db, file_id, let_statement.syntax(), None);
Some(get_pat_hints(db, &analyzer, pat, InlayKind::LetBindingType, false))
Some(get_pat_type_hints(db, &analyzer, pat, false))
})
.visit(|closure_parameter: LambdaExpr| {
let analyzer = SourceAnalyzer::new(db, file_id, closure_parameter.syntax(), None);
Expand All @@ -55,33 +50,25 @@ fn get_inlay_hints(
.params()
.filter(|closure_param| closure_param.ascribed_type().is_none())
.filter_map(|closure_param| closure_param.pat())
.map(|root_pat| {
get_pat_hints(
db,
&analyzer,
root_pat,
InlayKind::ClosureParameterType,
false,
)
})
.map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, false))
.flatten()
.collect()
})
})
.visit(|for_expression: ForExpr| {
let pat = for_expression.pat()?;
let analyzer = SourceAnalyzer::new(db, file_id, for_expression.syntax(), None);
Some(get_pat_hints(db, &analyzer, pat, InlayKind::ForExpressionBindingType, false))
Some(get_pat_type_hints(db, &analyzer, pat, false))
})
.visit(|if_expr: IfExpr| {
let pat = if_expr.condition()?.pat()?;
let analyzer = SourceAnalyzer::new(db, file_id, if_expr.syntax(), None);
Some(get_pat_hints(db, &analyzer, pat, InlayKind::IfExpressionType, true))
Some(get_pat_type_hints(db, &analyzer, pat, true))
})
.visit(|while_expr: WhileExpr| {
let pat = while_expr.condition()?.pat()?;
let analyzer = SourceAnalyzer::new(db, file_id, while_expr.syntax(), None);
Some(get_pat_hints(db, &analyzer, pat, InlayKind::WhileLetExpressionType, true))
Some(get_pat_type_hints(db, &analyzer, pat, true))
})
.visit(|match_arm_list: MatchArmList| {
let analyzer = SourceAnalyzer::new(db, file_id, match_arm_list.syntax(), None);
Expand All @@ -90,21 +77,18 @@ fn get_inlay_hints(
.arms()
.map(|match_arm| match_arm.pats())
.flatten()
.map(|root_pat| {
get_pat_hints(db, &analyzer, root_pat, InlayKind::MatchArmType, true)
})
.map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, true))
.flatten()
.collect(),
)
})
.accept(&node)?
}

fn get_pat_hints(
fn get_pat_type_hints(
db: &RootDatabase,
analyzer: &SourceAnalyzer,
root_pat: Pat,
kind: InlayKind,
skip_root_pat_hint: bool,
) -> Vec<InlayHint> {
let original_pat = &root_pat.clone();
Expand All @@ -118,7 +102,7 @@ fn get_pat_hints(
})
.map(|(range, pat_type)| InlayHint {
range,
kind: kind.clone(),
kind: InlayKind::TypeHint,
label: pat_type.display(db).to_string().into(),
})
.collect()
Expand Down Expand Up @@ -232,52 +216,52 @@ fn main() {
assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[
InlayHint {
range: [193; 197),
kind: LetBindingType,
kind: TypeHint,
label: "i32",
},
InlayHint {
range: [236; 244),
kind: LetBindingType,
kind: TypeHint,
label: "i32",
},
InlayHint {
range: [275; 279),
kind: LetBindingType,
kind: TypeHint,
label: "&str",
},
InlayHint {
range: [539; 543),
kind: LetBindingType,
kind: TypeHint,
label: "(i32, char)",
},
InlayHint {
range: [566; 567),
kind: LetBindingType,
kind: TypeHint,
label: "i32",
},
InlayHint {
range: [570; 571),
kind: LetBindingType,
kind: TypeHint,
label: "i32",
},
InlayHint {
range: [573; 574),
kind: LetBindingType,
kind: TypeHint,
label: "i32",
},
InlayHint {
range: [584; 585),
kind: LetBindingType,
kind: TypeHint,
label: "i32",
},
InlayHint {
range: [577; 578),
kind: LetBindingType,
kind: TypeHint,
label: "f64",
},
InlayHint {
range: [580; 581),
kind: LetBindingType,
kind: TypeHint,
label: "f64",
},
]"#
Expand All @@ -299,12 +283,12 @@ fn main() {
assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[
InlayHint {
range: [21; 30),
kind: LetBindingType,
kind: TypeHint,
label: "i32",
},
InlayHint {
range: [57; 66),
kind: ClosureParameterType,
kind: TypeHint,
label: "i32",
},
]"#
Expand All @@ -326,12 +310,12 @@ fn main() {
assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[
InlayHint {
range: [21; 30),
kind: LetBindingType,
kind: TypeHint,
label: "i32",
},
InlayHint {
range: [44; 53),
kind: ForExpressionBindingType,
kind: TypeHint,
label: "i32",
},
]"#
Expand Down Expand Up @@ -364,35 +348,35 @@ fn main() {
if let CustomOption::Some(Test { a: CustomOption::Some(x), b: y }) = &test {};
if let CustomOption::Some(Test { a: CustomOption::None, b: y }) = &test {};
if let CustomOption::Some(Test { b: y, .. }) = &test {};

if test == CustomOption::None {}
}"#,
);

assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[
InlayHint {
range: [166; 170),
kind: LetBindingType,
kind: TypeHint,
label: "CustomOption<Test>",
},
InlayHint {
range: [334; 338),
kind: IfExpressionType,
kind: TypeHint,
label: "&Test",
},
InlayHint {
range: [389; 390),
kind: IfExpressionType,
kind: TypeHint,
label: "&CustomOption<u32>",
},
InlayHint {
range: [392; 393),
kind: IfExpressionType,
kind: TypeHint,
label: "&u8",
},
InlayHint {
range: [531; 532),
kind: IfExpressionType,
kind: TypeHint,
label: "&u32",
},
]"#
Expand Down Expand Up @@ -425,15 +409,15 @@ fn main() {
while let CustomOption::Some(Test { a: CustomOption::Some(x), b: y }) = &test {};
while let CustomOption::Some(Test { a: CustomOption::None, b: y }) = &test {};
while let CustomOption::Some(Test { b: y, .. }) = &test {};

while test == CustomOption::None {}
}"#,
);

assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[
InlayHint {
range: [166; 170),
kind: LetBindingType,
kind: TypeHint,
label: "CustomOption<Test>",
},
]"#
Expand All @@ -445,7 +429,7 @@ fn main() {
let (analysis, file_id) = single_file(
r#"
#[derive(PartialEq)]
enum CustomOption<T> {
enum CustomOption<T> {
None,
Some(T),
}
Expand Down Expand Up @@ -473,23 +457,23 @@ fn main() {

assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[
InlayHint {
range: [312; 316),
kind: MatchArmType,
range: [311; 315),
kind: TypeHint,
label: "Test",
},
InlayHint {
range: [359; 360),
kind: MatchArmType,
range: [358; 359),
kind: TypeHint,
label: "CustomOption<u32>",
},
InlayHint {
range: [362; 363),
kind: MatchArmType,
range: [361; 362),
kind: TypeHint,
label: "u8",
},
InlayHint {
range: [485; 486),
kind: MatchArmType,
range: [484; 485),
kind: TypeHint,
label: "u32",
},
]"#
Expand Down
9 changes: 1 addition & 8 deletions crates/ra_lsp_server/src/main_loop/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,14 +894,7 @@ pub fn handle_inlay_hints(
label: api_type.label.to_string(),
range: api_type.range.conv_with(&line_index),
kind: match api_type.kind {
ra_ide_api::InlayKind::LetBindingType => InlayKind::LetBindingType,
ra_ide_api::InlayKind::ClosureParameterType => InlayKind::ClosureParameterType,
ra_ide_api::InlayKind::ForExpressionBindingType => {
InlayKind::ForExpressionBindingType
}
ra_ide_api::InlayKind::IfExpressionType => InlayKind::IfExpressionType,
ra_ide_api::InlayKind::WhileLetExpressionType => InlayKind::WhileLetExpressionType,
ra_ide_api::InlayKind::MatchArmType => InlayKind::MatchArmType,
ra_ide_api::InlayKind::TypeHint => InlayKind::TypeHint,
},
})
.collect())
Expand Down
7 changes: 1 addition & 6 deletions crates/ra_lsp_server/src/req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,7 @@ pub struct InlayHintsParams {

#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum InlayKind {
LetBindingType,
ClosureParameterType,
ForExpressionBindingType,
IfExpressionType,
WhileLetExpressionType,
MatchArmType,
TypeHint,
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down
Loading