aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/inlay_hints.rs
blob: 739a44b195e10238abcefdfa69319d323ada3787 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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,
}

pub(crate) fn inlay_hints(file: &SourceFile) -> Vec<InlayHint> {
    file.syntax().descendants().map(|node| get_inlay_hints(&node)).flatten().collect()
}

fn get_inlay_hints(node: &SyntaxNode) -> Vec<InlayHint> {
    visitor()
        .visit(|let_statement: ast::LetStmt| {
            let let_syntax = let_statement.syntax();

            if let_statement.ascribed_type().is_some() {
                return Vec::new();
            }

            let pat_range = match let_statement.pat().map(|pat| pat.kind()) {
                Some(PatKind::BindPat(bind_pat)) => bind_pat.syntax().text_range(),
                Some(PatKind::TuplePat(tuple_pat)) => tuple_pat.syntax().text_range(),
                _ => return Vec::new(),
            };

            vec![InlayHint {
                range: pat_range,
                text: let_syntax.text().to_smol_string(),
                inlay_kind: InlayKind::LetBinding,
            }]
        })
        .visit(|closure_parameter: ast::LambdaExpr| {
            if let Some(param_list) = closure_parameter.param_list() {
                param_list
                    .params()
                    .filter(|closure_param| closure_param.ascribed_type().is_none())
                    .map(|closure_param| {
                        let closure_param_syntax = closure_param.syntax();
                        InlayHint {
                            range: closure_param_syntax.text_range(),
                            text: closure_param_syntax.text().to_smol_string(),
                            inlay_kind: InlayKind::ClosureParameter,
                        }
                    })
                    .collect()
            } else {
                Vec::new()
            }
        })
        .accept(&node)
        .unwrap_or_default()
}

#[cfg(test)]
mod tests {
    use super::*;
    use insta::assert_debug_snapshot_matches;

    #[test]
    fn test_inlay_hints() {
        let file = SourceFile::parse(
            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
    });

    let test: i32 = 33;

    let (x, c) = (42, 'a');
    let test = (42, 'a');
}

"#,
        )
        .ok()
        .unwrap();
        assert_debug_snapshot_matches!(inlay_hints(&file), @r#"[
    InlayHint {
        range: [71; 75),
        text: "let test = 54;",
        inlay_kind: LetBinding,
    },
    InlayHint {
        range: [90; 94),
        text: "let test = InnerStruct {};",
        inlay_kind: LetBinding,
    },
    InlayHint {
        range: [121; 125),
        text: "let test = OuterStruct {};",
        inlay_kind: LetBinding,
    },
    InlayHint {
        range: [152; 156),
        text: "let test = vec![222];",
        inlay_kind: LetBinding,
    },
    InlayHint {
        range: [178; 186),
        text: "let mut test = Vec::new();",
        inlay_kind: LetBinding,
    },
    InlayHint {
        range: [229; 233),
        text: "let test = test.into_iter().map(|i| i * i).collect::<Vec<_>>();",
        inlay_kind: LetBinding,
    },
    InlayHint {
        range: [258; 259),
        text: "i",
        inlay_kind: ClosureParameter,
    },
    InlayHint {
        range: [297; 305),
        text: "let mut test = 33;",
        inlay_kind: LetBinding,
    },
    InlayHint {
        range: [417; 426),
        text: "let i_squared = i * i;",
        inlay_kind: LetBinding,
    },
    InlayHint {
        range: [496; 502),
        text: "let (x, c) = (42, \'a\');",
        inlay_kind: LetBinding,
    },
    InlayHint {
        range: [524; 528),
        text: "let test = (42, \'a\');",
        inlay_kind: LetBinding,
    },
]"#
        );
    }
}