Use proper inlay kinds

This commit is contained in:
Kirill Bulatov 2019-08-05 00:28:36 +03:00
parent 2c02aebeb5
commit 15411d4474
3 changed files with 15 additions and 43 deletions

View file

@ -9,14 +9,9 @@ use ra_syntax::{
SmolStr, SyntaxKind, SyntaxNode, TextRange, SmolStr, SyntaxKind, SyntaxNode, TextRange,
}; };
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq)]
pub enum InlayKind { pub enum InlayKind {
LetBindingType, TypeHint,
ClosureParameterType,
ForExpressionBindingType,
IfExpressionType,
WhileLetExpressionType,
MatchArmType,
} }
#[derive(Debug)] #[derive(Debug)]
@ -46,7 +41,7 @@ fn get_inlay_hints(
} }
let pat = let_statement.pat()?; let pat = let_statement.pat()?;
let analyzer = SourceAnalyzer::new(db, file_id, let_statement.syntax(), None); 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| { .visit(|closure_parameter: LambdaExpr| {
let analyzer = SourceAnalyzer::new(db, file_id, closure_parameter.syntax(), None); let analyzer = SourceAnalyzer::new(db, file_id, closure_parameter.syntax(), None);
@ -55,15 +50,7 @@ fn get_inlay_hints(
.params() .params()
.filter(|closure_param| closure_param.ascribed_type().is_none()) .filter(|closure_param| closure_param.ascribed_type().is_none())
.filter_map(|closure_param| closure_param.pat()) .filter_map(|closure_param| closure_param.pat())
.map(|root_pat| { .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, false))
get_pat_hints(
db,
&analyzer,
root_pat,
InlayKind::ClosureParameterType,
false,
)
})
.flatten() .flatten()
.collect() .collect()
}) })
@ -71,17 +58,17 @@ fn get_inlay_hints(
.visit(|for_expression: ForExpr| { .visit(|for_expression: ForExpr| {
let pat = for_expression.pat()?; let pat = for_expression.pat()?;
let analyzer = SourceAnalyzer::new(db, file_id, for_expression.syntax(), None); 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| { .visit(|if_expr: IfExpr| {
let pat = if_expr.condition()?.pat()?; let pat = if_expr.condition()?.pat()?;
let analyzer = SourceAnalyzer::new(db, file_id, if_expr.syntax(), None); 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| { .visit(|while_expr: WhileExpr| {
let pat = while_expr.condition()?.pat()?; let pat = while_expr.condition()?.pat()?;
let analyzer = SourceAnalyzer::new(db, file_id, while_expr.syntax(), None); 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| { .visit(|match_arm_list: MatchArmList| {
let analyzer = SourceAnalyzer::new(db, file_id, match_arm_list.syntax(), None); let analyzer = SourceAnalyzer::new(db, file_id, match_arm_list.syntax(), None);
@ -90,9 +77,7 @@ fn get_inlay_hints(
.arms() .arms()
.map(|match_arm| match_arm.pats()) .map(|match_arm| match_arm.pats())
.flatten() .flatten()
.map(|root_pat| { .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, true))
get_pat_hints(db, &analyzer, root_pat, InlayKind::MatchArmType, true)
})
.flatten() .flatten()
.collect(), .collect(),
) )
@ -100,11 +85,10 @@ fn get_inlay_hints(
.accept(&node)? .accept(&node)?
} }
fn get_pat_hints( fn get_pat_type_hints(
db: &RootDatabase, db: &RootDatabase,
analyzer: &SourceAnalyzer, analyzer: &SourceAnalyzer,
root_pat: Pat, root_pat: Pat,
kind: InlayKind,
skip_root_pat_hint: bool, skip_root_pat_hint: bool,
) -> Vec<InlayHint> { ) -> Vec<InlayHint> {
let original_pat = &root_pat.clone(); let original_pat = &root_pat.clone();
@ -118,7 +102,7 @@ fn get_pat_hints(
}) })
.map(|(range, pat_type)| InlayHint { .map(|(range, pat_type)| InlayHint {
range, range,
kind: kind.clone(), kind: InlayKind::TypeHint,
label: pat_type.display(db).to_string().into(), label: pat_type.display(db).to_string().into(),
}) })
.collect() .collect()
@ -364,7 +348,7 @@ fn main() {
if let CustomOption::Some(Test { a: CustomOption::Some(x), b: y }) = &test {}; 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 { a: CustomOption::None, b: y }) = &test {};
if let CustomOption::Some(Test { b: y, .. }) = &test {}; if let CustomOption::Some(Test { b: y, .. }) = &test {};
if test == CustomOption::None {} if test == CustomOption::None {}
}"#, }"#,
); );
@ -425,7 +409,7 @@ fn main() {
while let CustomOption::Some(Test { a: CustomOption::Some(x), b: y }) = &test {}; 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 { a: CustomOption::None, b: y }) = &test {};
while let CustomOption::Some(Test { b: y, .. }) = &test {}; while let CustomOption::Some(Test { b: y, .. }) = &test {};
while test == CustomOption::None {} while test == CustomOption::None {}
}"#, }"#,
); );
@ -445,7 +429,7 @@ fn main() {
let (analysis, file_id) = single_file( let (analysis, file_id) = single_file(
r#" r#"
#[derive(PartialEq)] #[derive(PartialEq)]
enum CustomOption<T> { enum CustomOption<T> {
None, None,
Some(T), Some(T),
} }

View file

@ -894,14 +894,7 @@ pub fn handle_inlay_hints(
label: api_type.label.to_string(), label: api_type.label.to_string(),
range: api_type.range.conv_with(&line_index), range: api_type.range.conv_with(&line_index),
kind: match api_type.kind { kind: match api_type.kind {
ra_ide_api::InlayKind::LetBindingType => InlayKind::LetBindingType, ra_ide_api::InlayKind::TypeHint => InlayKind::TypeHint,
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,
}, },
}) })
.collect()) .collect())

View file

@ -213,12 +213,7 @@ pub struct InlayHintsParams {
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)] #[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum InlayKind { pub enum InlayKind {
LetBindingType, TypeHint,
ClosureParameterType,
ForExpressionBindingType,
IfExpressionType,
WhileLetExpressionType,
MatchArmType,
} }
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]