ide/inlay_hints/
range_exclusive.rs

1//! Implementation of "range exclusive" inlay hints:
2//! ```ignore
3//! for i in 0../* < */10 {}
4//! if let ../* < */100 = 50 {}
5//! ```
6use ide_db::famous_defs::FamousDefs;
7use syntax::{SyntaxToken, T, ast};
8
9use crate::{InlayHint, InlayHintsConfig};
10
11pub(super) fn hints(
12    acc: &mut Vec<InlayHint>,
13    FamousDefs(_sema, _): &FamousDefs<'_, '_>,
14    config: &InlayHintsConfig<'_>,
15    range: impl ast::RangeItem,
16) -> Option<()> {
17    (config.range_exclusive_hints && range.end().is_some())
18        .then(|| {
19            range.op_token().filter(|token| token.kind() == T![..]).map(|token| {
20                acc.push(inlay_hint(token));
21            })
22        })
23        .flatten()
24}
25
26fn inlay_hint(token: SyntaxToken) -> InlayHint {
27    InlayHint {
28        range: token.text_range(),
29        position: crate::InlayHintPosition::After,
30        pad_left: false,
31        pad_right: false,
32        kind: crate::InlayKind::RangeExclusive,
33        label: crate::InlayHintLabel::from("<"),
34        text_edit: None,
35        resolve_parent: None,
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use crate::{
42        InlayHintsConfig,
43        inlay_hints::tests::{DISABLED_CONFIG, check_with_config},
44    };
45
46    #[test]
47    fn range_exclusive_expression_bounded_above_hints() {
48        check_with_config(
49            InlayHintsConfig { range_exclusive_hints: true, ..DISABLED_CONFIG },
50            r#"
51fn main() {
52    let a = 0..10;
53           //^^<
54    let b = ..100;
55          //^^<
56    let c = (2 - 1)..(7 * 8)
57                 //^^<
58}"#,
59        );
60    }
61
62    #[test]
63    fn range_exclusive_expression_unbounded_above_no_hints() {
64        check_with_config(
65            InlayHintsConfig { range_exclusive_hints: true, ..DISABLED_CONFIG },
66            r#"
67fn main() {
68    let a = 0..;
69    let b = ..;
70}"#,
71        );
72    }
73
74    #[test]
75    fn range_inclusive_expression_no_hints() {
76        check_with_config(
77            InlayHintsConfig { range_exclusive_hints: true, ..DISABLED_CONFIG },
78            r#"
79fn main() {
80    let a = 0..=10;
81    let b = ..=100;
82}"#,
83        );
84    }
85
86    #[test]
87    fn range_exclusive_pattern_bounded_above_hints() {
88        check_with_config(
89            InlayHintsConfig { range_exclusive_hints: true, ..DISABLED_CONFIG },
90            r#"
91fn main() {
92    if let 0..10 = 0 {}
93          //^^<
94    if let ..100 = 0 {}
95         //^^<
96}"#,
97        );
98    }
99
100    #[test]
101    fn range_exclusive_pattern_unbounded_above_no_hints() {
102        check_with_config(
103            InlayHintsConfig { range_exclusive_hints: true, ..DISABLED_CONFIG },
104            r#"
105fn main() {
106    if let 0.. = 0 {}
107    if let .. = 0 {}
108}"#,
109        );
110    }
111
112    #[test]
113    fn range_inclusive_pattern_no_hints() {
114        check_with_config(
115            InlayHintsConfig { range_exclusive_hints: true, ..DISABLED_CONFIG },
116            r#"
117fn main() {
118    if let 0..=10 = 0 {}
119    if let ..=100 = 0 {}
120}"#,
121        );
122    }
123}