ide_assists/handlers/
toggle_ignore.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
use syntax::{
    ast::{self, HasAttrs},
    AstNode, AstToken,
};

use crate::{utils::test_related_attribute_syn, AssistContext, AssistId, AssistKind, Assists};

// Assist: toggle_ignore
//
// Adds `#[ignore]` attribute to the test.
//
// ```
// $0#[test]
// fn arithmetics {
//     assert_eq!(2 + 2, 5);
// }
// ```
// ->
// ```
// #[test]
// #[ignore]
// fn arithmetics {
//     assert_eq!(2 + 2, 5);
// }
// ```
pub(crate) fn toggle_ignore(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
    let attr: ast::Attr = ctx.find_node_at_offset()?;
    let func = attr.syntax().parent().and_then(ast::Fn::cast)?;
    let attr = test_related_attribute_syn(&func)?;

    match has_ignore_attribute(&func) {
        None => acc.add(
            AssistId("toggle_ignore", AssistKind::None),
            "Ignore this test",
            attr.syntax().text_range(),
            |builder| builder.insert(attr.syntax().text_range().end(), "\n#[ignore]"),
        ),
        Some(ignore_attr) => acc.add(
            AssistId("toggle_ignore", AssistKind::None),
            "Re-enable this test",
            ignore_attr.syntax().text_range(),
            |builder| {
                builder.delete(ignore_attr.syntax().text_range());
                let whitespace = ignore_attr
                    .syntax()
                    .next_sibling_or_token()
                    .and_then(|x| x.into_token())
                    .and_then(ast::Whitespace::cast);
                if let Some(whitespace) = whitespace {
                    builder.delete(whitespace.syntax().text_range());
                }
            },
        ),
    }
}

fn has_ignore_attribute(fn_def: &ast::Fn) -> Option<ast::Attr> {
    fn_def.attrs().find(|attr| attr.path().is_some_and(|it| it.syntax().text() == "ignore"))
}

#[cfg(test)]
mod tests {
    use crate::tests::check_assist;

    use super::*;

    #[test]
    fn test_base_case() {
        check_assist(
            toggle_ignore,
            r#"
            #[test$0]
            fn test() {}
            "#,
            r#"
            #[test]
            #[ignore]
            fn test() {}
            "#,
        )
    }

    #[test]
    fn test_unignore() {
        check_assist(
            toggle_ignore,
            r#"
            #[test$0]
            #[ignore]
            fn test() {}
            "#,
            r#"
            #[test]
            fn test() {}
            "#,
        )
    }
}