ide/inlay_hints/
bounds.rs

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
//! Implementation of trait bound hints.
//!
//! Currently this renders the implied `Sized` bound.
use ide_db::{FileRange, famous_defs::FamousDefs};

use span::EditionedFileId;
use syntax::ast::{self, AstNode, HasTypeBounds};

use crate::{
    InlayHint, InlayHintLabel, InlayHintLabelPart, InlayHintPosition, InlayHintsConfig, InlayKind,
    TryToNav,
};

pub(super) fn hints(
    acc: &mut Vec<InlayHint>,
    famous_defs @ FamousDefs(sema, _): &FamousDefs<'_, '_>,
    config: &InlayHintsConfig,
    _file_id: EditionedFileId,
    params: ast::GenericParamList,
) -> Option<()> {
    if !config.sized_bound {
        return None;
    }

    let sized_trait = famous_defs.core_marker_Sized();

    for param in params.type_or_const_params() {
        match param {
            ast::TypeOrConstParam::Type(type_param) => {
                let c = type_param.colon_token().map(|it| it.text_range());
                let has_bounds =
                    type_param.type_bound_list().is_some_and(|it| it.bounds().next().is_some());
                acc.push(InlayHint {
                    range: c.unwrap_or_else(|| type_param.syntax().text_range()),
                    kind: InlayKind::Type,
                    label: {
                        let mut hint = InlayHintLabel::default();
                        if c.is_none() {
                            hint.parts.push(InlayHintLabelPart {
                                text: ": ".to_owned(),
                                linked_location: None,
                                tooltip: None,
                            });
                        }
                        hint.parts.push(InlayHintLabelPart {
                            text: "Sized".to_owned(),
                            linked_location: sized_trait.and_then(|it| {
                                config.lazy_location_opt(|| {
                                    it.try_to_nav(sema.db).map(|it| {
                                        let n = it.call_site();
                                        FileRange {
                                            file_id: n.file_id,
                                            range: n.focus_or_full_range(),
                                        }
                                    })
                                })
                            }),
                            tooltip: None,
                        });
                        if has_bounds {
                            hint.parts.push(InlayHintLabelPart {
                                text: " +".to_owned(),
                                linked_location: None,
                                tooltip: None,
                            });
                        }
                        hint
                    },
                    text_edit: None,
                    position: InlayHintPosition::After,
                    pad_left: c.is_some(),
                    pad_right: has_bounds,
                    resolve_parent: Some(params.syntax().text_range()),
                });
            }
            ast::TypeOrConstParam::Const(_) => (),
        }
    }

    Some(())
}

#[cfg(test)]
mod tests {
    use expect_test::expect;

    use crate::inlay_hints::InlayHintsConfig;

    use crate::inlay_hints::tests::{DISABLED_CONFIG, check_expect, check_with_config};

    #[track_caller]
    fn check(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
        check_with_config(InlayHintsConfig { sized_bound: true, ..DISABLED_CONFIG }, ra_fixture);
    }

    #[test]
    fn smoke() {
        check(
            r#"
fn foo<T>() {}
    // ^ : Sized
"#,
        );
    }

    #[test]
    fn with_colon() {
        check(
            r#"
fn foo<T:>() {}
     // ^ Sized
"#,
        );
    }

    #[test]
    fn with_colon_and_bounds() {
        check(
            r#"
fn foo<T: 'static>() {}
     // ^ Sized +
"#,
        );
    }

    #[test]
    fn location_works() {
        check_expect(
            InlayHintsConfig { sized_bound: true, ..DISABLED_CONFIG },
            r#"
//- minicore: sized
fn foo<T>() {}
"#,
            expect![[r#"
                [
                    (
                        7..8,
                        [
                            ": ",
                            InlayHintLabelPart {
                                text: "Sized",
                                linked_location: Some(
                                    Computed(
                                        FileRangeWrapper {
                                            file_id: FileId(
                                                1,
                                            ),
                                            range: 135..140,
                                        },
                                    ),
                                ),
                                tooltip: "",
                            },
                        ],
                    ),
                ]
            "#]],
        );
    }
}