diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-11-24 00:54:53 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-11-24 00:54:53 +0000 |
commit | 6294286fee6e53c29e8ad563f476721ad85ef266 (patch) | |
tree | a10e4e43027d124ef1ff9d9c7aadc36bd1c2ab9d /crates/assists/src/handlers | |
parent | 2ff78cde31cb4f5a4e05ef51dc9347838f790898 (diff) | |
parent | a172c2317c621ba15b7fc4c11b10040dac346ec8 (diff) |
Merge #6610
6610: add 'Re-enable this test' assist r=SomeoneToIgnore a=jakobhellermann
The `Ignore this test` assist previously allowed ignoring multiple times, each time adding a `#[ignore]` attribute.
This PR instead shows an assist to undo the ignoring.
Co-authored-by: Jakob Hellermann <[email protected]>
Diffstat (limited to 'crates/assists/src/handlers')
-rw-r--r-- | crates/assists/src/handlers/ignore_test.rs | 83 |
1 files changed, 76 insertions, 7 deletions
diff --git a/crates/assists/src/handlers/ignore_test.rs b/crates/assists/src/handlers/ignore_test.rs index d2339184f..2b9dff081 100644 --- a/crates/assists/src/handlers/ignore_test.rs +++ b/crates/assists/src/handlers/ignore_test.rs | |||
@@ -1,4 +1,7 @@ | |||
1 | use syntax::{ast, AstNode}; | 1 | use syntax::{ |
2 | ast::{self, AttrsOwner}, | ||
3 | AstNode, AstToken, | ||
4 | }; | ||
2 | 5 | ||
3 | use crate::{utils::test_related_attribute, AssistContext, AssistId, AssistKind, Assists}; | 6 | use crate::{utils::test_related_attribute, AssistContext, AssistId, AssistKind, Assists}; |
4 | 7 | ||
@@ -25,10 +28,76 @@ pub(crate) fn ignore_test(acc: &mut Assists, ctx: &AssistContext) -> Option<()> | |||
25 | let func = attr.syntax().parent().and_then(ast::Fn::cast)?; | 28 | let func = attr.syntax().parent().and_then(ast::Fn::cast)?; |
26 | let attr = test_related_attribute(&func)?; | 29 | let attr = test_related_attribute(&func)?; |
27 | 30 | ||
28 | acc.add( | 31 | match has_ignore_attribute(&func) { |
29 | AssistId("ignore_test", AssistKind::None), | 32 | None => acc.add( |
30 | "Ignore this test", | 33 | AssistId("ignore_test", AssistKind::None), |
31 | attr.syntax().text_range(), | 34 | "Ignore this test", |
32 | |builder| builder.insert(attr.syntax().text_range().end(), &format!("\n#[ignore]")), | 35 | attr.syntax().text_range(), |
33 | ) | 36 | |builder| builder.insert(attr.syntax().text_range().end(), &format!("\n#[ignore]")), |
37 | ), | ||
38 | Some(ignore_attr) => acc.add( | ||
39 | AssistId("unignore_test", AssistKind::None), | ||
40 | "Re-enable this test", | ||
41 | ignore_attr.syntax().text_range(), | ||
42 | |builder| { | ||
43 | builder.delete(ignore_attr.syntax().text_range()); | ||
44 | let whitespace = ignore_attr | ||
45 | .syntax() | ||
46 | .next_sibling_or_token() | ||
47 | .and_then(|x| x.into_token()) | ||
48 | .and_then(ast::Whitespace::cast); | ||
49 | if let Some(whitespace) = whitespace { | ||
50 | builder.delete(whitespace.syntax().text_range()); | ||
51 | } | ||
52 | }, | ||
53 | ), | ||
54 | } | ||
55 | } | ||
56 | |||
57 | fn has_ignore_attribute(fn_def: &ast::Fn) -> Option<ast::Attr> { | ||
58 | fn_def.attrs().find_map(|attr| { | ||
59 | if attr.path()?.syntax().text().to_string() == "ignore" { | ||
60 | Some(attr) | ||
61 | } else { | ||
62 | None | ||
63 | } | ||
64 | }) | ||
65 | } | ||
66 | |||
67 | #[cfg(test)] | ||
68 | mod tests { | ||
69 | use super::ignore_test; | ||
70 | use crate::tests::check_assist; | ||
71 | |||
72 | #[test] | ||
73 | fn test_base_case() { | ||
74 | check_assist( | ||
75 | ignore_test, | ||
76 | r#" | ||
77 | #[test<|>] | ||
78 | fn test() {} | ||
79 | "#, | ||
80 | r#" | ||
81 | #[test] | ||
82 | #[ignore] | ||
83 | fn test() {} | ||
84 | "#, | ||
85 | ) | ||
86 | } | ||
87 | |||
88 | #[test] | ||
89 | fn test_unignore() { | ||
90 | check_assist( | ||
91 | ignore_test, | ||
92 | r#" | ||
93 | #[test<|>] | ||
94 | #[ignore] | ||
95 | fn test() {} | ||
96 | "#, | ||
97 | r#" | ||
98 | #[test] | ||
99 | fn test() {} | ||
100 | "#, | ||
101 | ) | ||
102 | } | ||
34 | } | 103 | } |