2019-07-21 20:28:05 +00:00
|
|
|
use crate::{db::RootDatabase, FileId};
|
|
|
|
use hir::{HirDisplay, Ty};
|
|
|
|
use ra_syntax::ast::Pat;
|
2019-07-19 21:20:09 +00:00
|
|
|
use ra_syntax::{
|
|
|
|
algo::visit::{visitor, Visitor},
|
|
|
|
ast::{self, PatKind, TypeAscriptionOwner},
|
|
|
|
AstNode, SmolStr, SourceFile, SyntaxNode, TextRange,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub enum InlayKind {
|
|
|
|
LetBinding,
|
|
|
|
ClosureParameter,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct InlayHint {
|
|
|
|
pub range: TextRange,
|
|
|
|
pub text: SmolStr,
|
|
|
|
pub inlay_kind: InlayKind,
|
2019-07-21 21:47:44 +00:00
|
|
|
pub inlay_type_string: SmolStr,
|
2019-07-19 21:20:09 +00:00
|
|
|
}
|
|
|
|
|
2019-07-21 20:28:05 +00:00
|
|
|
pub(crate) fn inlay_hints(db: &RootDatabase, file_id: FileId, file: &SourceFile) -> Vec<InlayHint> {
|
|
|
|
file.syntax()
|
|
|
|
.descendants()
|
|
|
|
.map(|node| get_inlay_hints(db, file_id, &node).unwrap_or_default())
|
|
|
|
.flatten()
|
|
|
|
.collect()
|
2019-07-19 21:20:09 +00:00
|
|
|
}
|
|
|
|
|
2019-07-21 20:28:05 +00:00
|
|
|
fn get_inlay_hints(
|
|
|
|
db: &RootDatabase,
|
|
|
|
file_id: FileId,
|
|
|
|
node: &SyntaxNode,
|
|
|
|
) -> Option<Vec<InlayHint>> {
|
2019-07-19 21:20:09 +00:00
|
|
|
visitor()
|
|
|
|
.visit(|let_statement: ast::LetStmt| {
|
|
|
|
let let_syntax = let_statement.syntax();
|
|
|
|
|
|
|
|
if let_statement.ascribed_type().is_some() {
|
2019-07-21 20:28:05 +00:00
|
|
|
return None;
|
2019-07-19 21:20:09 +00:00
|
|
|
}
|
|
|
|
|
2019-07-21 20:28:05 +00:00
|
|
|
let let_pat = let_statement.pat()?;
|
|
|
|
let inlay_type_string = get_node_displayable_type(db, file_id, let_syntax, &let_pat)?
|
|
|
|
.display(db)
|
2019-07-21 21:47:44 +00:00
|
|
|
.to_string()
|
|
|
|
.into();
|
2019-07-21 20:28:05 +00:00
|
|
|
|
|
|
|
let pat_range = match let_pat.kind() {
|
|
|
|
PatKind::BindPat(bind_pat) => bind_pat.syntax().text_range(),
|
|
|
|
PatKind::TuplePat(tuple_pat) => tuple_pat.syntax().text_range(),
|
|
|
|
_ => return None,
|
2019-07-19 21:20:09 +00:00
|
|
|
};
|
|
|
|
|
2019-07-21 20:28:05 +00:00
|
|
|
Some(vec![InlayHint {
|
2019-07-19 21:20:09 +00:00
|
|
|
range: pat_range,
|
2019-07-21 21:16:07 +00:00
|
|
|
text: let_syntax.text().to_string().into(),
|
2019-07-19 21:20:09 +00:00
|
|
|
inlay_kind: InlayKind::LetBinding,
|
2019-07-21 20:28:05 +00:00
|
|
|
inlay_type_string,
|
|
|
|
}])
|
2019-07-19 21:20:09 +00:00
|
|
|
})
|
2019-07-21 20:28:05 +00:00
|
|
|
.visit(|closure_parameter: ast::LambdaExpr| match closure_parameter.param_list() {
|
|
|
|
Some(param_list) => Some(
|
2019-07-19 21:20:09 +00:00
|
|
|
param_list
|
|
|
|
.params()
|
|
|
|
.filter(|closure_param| closure_param.ascribed_type().is_none())
|
2019-07-21 20:28:05 +00:00
|
|
|
.filter_map(|closure_param| {
|
2019-07-19 21:20:09 +00:00
|
|
|
let closure_param_syntax = closure_param.syntax();
|
2019-07-21 20:28:05 +00:00
|
|
|
let inlay_type_string = get_node_displayable_type(
|
|
|
|
db,
|
|
|
|
file_id,
|
|
|
|
closure_param_syntax,
|
|
|
|
&closure_param.pat()?,
|
|
|
|
)?
|
|
|
|
.display(db)
|
2019-07-21 21:47:44 +00:00
|
|
|
.to_string()
|
|
|
|
.into();
|
|
|
|
|
2019-07-21 20:28:05 +00:00
|
|
|
Some(InlayHint {
|
2019-07-19 21:20:09 +00:00
|
|
|
range: closure_param_syntax.text_range(),
|
2019-07-21 21:16:07 +00:00
|
|
|
text: closure_param_syntax.text().to_string().into(),
|
2019-07-19 21:20:09 +00:00
|
|
|
inlay_kind: InlayKind::ClosureParameter,
|
2019-07-21 20:28:05 +00:00
|
|
|
inlay_type_string,
|
|
|
|
})
|
2019-07-19 21:20:09 +00:00
|
|
|
})
|
2019-07-21 20:28:05 +00:00
|
|
|
.collect(),
|
|
|
|
),
|
|
|
|
None => None,
|
2019-07-19 21:20:09 +00:00
|
|
|
})
|
2019-07-21 20:28:05 +00:00
|
|
|
.accept(&node)?
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_node_displayable_type(
|
|
|
|
db: &RootDatabase,
|
|
|
|
file_id: FileId,
|
|
|
|
node_syntax: &SyntaxNode,
|
|
|
|
node_pat: &Pat,
|
|
|
|
) -> Option<Ty> {
|
|
|
|
let analyzer = hir::SourceAnalyzer::new(db, file_id, node_syntax, None);
|
|
|
|
analyzer.type_of_pat(db, node_pat).and_then(|resolved_type| {
|
|
|
|
if let Ty::Apply(_) = resolved_type {
|
|
|
|
Some(resolved_type)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2019-07-19 21:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-07-21 20:28:05 +00:00
|
|
|
use crate::mock_analysis::single_file;
|
2019-07-19 21:20:09 +00:00
|
|
|
use insta::assert_debug_snapshot_matches;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_inlay_hints() {
|
2019-07-21 20:28:05 +00:00
|
|
|
let (analysis, file_id) = single_file(
|
2019-07-19 21:20:09 +00:00
|
|
|
r#"
|
|
|
|
struct OuterStruct {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
struct InnerStruct {}
|
|
|
|
|
|
|
|
let test = 54;
|
|
|
|
let test = InnerStruct {};
|
|
|
|
let test = OuterStruct {};
|
|
|
|
let test = vec![222];
|
|
|
|
let mut test = Vec::new();
|
|
|
|
test.push(333);
|
|
|
|
let test = test.into_iter().map(|i| i * i).collect::<Vec<_>>();
|
|
|
|
let mut test = 33;
|
|
|
|
let _ = 22;
|
|
|
|
let test: Vec<_> = (0..3).collect();
|
|
|
|
|
|
|
|
let _ = (0..23).map(|i: u32| {
|
|
|
|
let i_squared = i * i;
|
|
|
|
i_squared
|
|
|
|
});
|
2019-07-21 17:51:27 +00:00
|
|
|
|
2019-07-19 21:20:09 +00:00
|
|
|
let test: i32 = 33;
|
|
|
|
|
|
|
|
let (x, c) = (42, 'a');
|
|
|
|
let test = (42, 'a');
|
|
|
|
}
|
|
|
|
"#,
|
2019-07-21 20:28:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[
|
2019-07-21 17:51:27 +00:00
|
|
|
InlayHint {
|
|
|
|
range: [71; 75),
|
|
|
|
text: "let test = 54;",
|
|
|
|
inlay_kind: LetBinding,
|
2019-07-21 20:28:05 +00:00
|
|
|
inlay_type_string: "i32",
|
2019-07-21 17:51:27 +00:00
|
|
|
},
|
|
|
|
InlayHint {
|
|
|
|
range: [121; 125),
|
|
|
|
text: "let test = OuterStruct {};",
|
|
|
|
inlay_kind: LetBinding,
|
2019-07-21 20:28:05 +00:00
|
|
|
inlay_type_string: "OuterStruct",
|
2019-07-21 17:51:27 +00:00
|
|
|
},
|
|
|
|
InlayHint {
|
|
|
|
range: [297; 305),
|
|
|
|
text: "let mut test = 33;",
|
|
|
|
inlay_kind: LetBinding,
|
2019-07-21 20:28:05 +00:00
|
|
|
inlay_type_string: "i32",
|
2019-07-21 17:51:27 +00:00
|
|
|
},
|
|
|
|
InlayHint {
|
|
|
|
range: [417; 426),
|
|
|
|
text: "let i_squared = i * i;",
|
|
|
|
inlay_kind: LetBinding,
|
2019-07-21 20:28:05 +00:00
|
|
|
inlay_type_string: "u32",
|
2019-07-21 17:51:27 +00:00
|
|
|
},
|
|
|
|
InlayHint {
|
|
|
|
range: [496; 502),
|
|
|
|
text: "let (x, c) = (42, \'a\');",
|
|
|
|
inlay_kind: LetBinding,
|
2019-07-21 20:28:05 +00:00
|
|
|
inlay_type_string: "(i32, char)",
|
2019-07-21 17:51:27 +00:00
|
|
|
},
|
|
|
|
InlayHint {
|
|
|
|
range: [524; 528),
|
|
|
|
text: "let test = (42, \'a\');",
|
|
|
|
inlay_kind: LetBinding,
|
2019-07-21 20:28:05 +00:00
|
|
|
inlay_type_string: "(i32, char)",
|
2019-07-21 17:51:27 +00:00
|
|
|
},
|
|
|
|
]"#
|
|
|
|
);
|
2019-07-19 21:20:09 +00:00
|
|
|
}
|
|
|
|
}
|