2020-11-23 13:22:13 -06:00
|
|
|
use syntax::{
|
2021-09-27 05:54:24 -05:00
|
|
|
ast::{self, HasAttrs},
|
2020-11-23 13:22:13 -06:00
|
|
|
AstNode, AstToken,
|
|
|
|
};
|
2020-11-17 07:22:04 -06:00
|
|
|
|
|
|
|
use crate::{utils::test_related_attribute, AssistContext, AssistId, AssistKind, Assists};
|
|
|
|
|
2020-11-30 04:45:32 -06:00
|
|
|
// Assist: toggle_ignore
|
2020-11-17 07:22:04 -06:00
|
|
|
//
|
|
|
|
// Adds `#[ignore]` attribute to the test.
|
|
|
|
//
|
|
|
|
// ```
|
2021-01-06 14:15:48 -06:00
|
|
|
// $0#[test]
|
2020-11-17 07:22:04 -06:00
|
|
|
// fn arithmetics {
|
|
|
|
// assert_eq!(2 + 2, 5);
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// #[test]
|
|
|
|
// #[ignore]
|
|
|
|
// fn arithmetics {
|
|
|
|
// assert_eq!(2 + 2, 5);
|
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn toggle_ignore(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2020-11-17 07:22:04 -06:00
|
|
|
let attr: ast::Attr = ctx.find_node_at_offset()?;
|
|
|
|
let func = attr.syntax().parent().and_then(ast::Fn::cast)?;
|
|
|
|
let attr = test_related_attribute(&func)?;
|
|
|
|
|
2020-11-23 13:22:13 -06:00
|
|
|
match has_ignore_attribute(&func) {
|
|
|
|
None => acc.add(
|
2020-11-30 04:45:32 -06:00
|
|
|
AssistId("toggle_ignore", AssistKind::None),
|
2020-11-23 13:22:13 -06:00
|
|
|
"Ignore this test",
|
|
|
|
attr.syntax().text_range(),
|
2021-09-13 11:50:19 -05:00
|
|
|
|builder| builder.insert(attr.syntax().text_range().end(), "\n#[ignore]"),
|
2020-11-23 13:22:13 -06:00
|
|
|
),
|
|
|
|
Some(ignore_attr) => acc.add(
|
2020-11-30 04:45:32 -06:00
|
|
|
AssistId("toggle_ignore", AssistKind::None),
|
2020-11-23 13:22:13 -06:00
|
|
|
"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> {
|
2020-11-30 04:45:32 -06:00
|
|
|
fn_def.attrs().find(|attr| attr.path().map(|it| it.syntax().text() == "ignore") == Some(true))
|
2020-11-23 13:22:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::tests::check_assist;
|
|
|
|
|
2020-11-30 04:45:32 -06:00
|
|
|
use super::*;
|
|
|
|
|
2020-11-23 13:22:13 -06:00
|
|
|
#[test]
|
|
|
|
fn test_base_case() {
|
|
|
|
check_assist(
|
2020-11-30 04:45:32 -06:00
|
|
|
toggle_ignore,
|
2020-11-23 13:22:13 -06:00
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
#[test$0]
|
2020-11-23 13:22:13 -06:00
|
|
|
fn test() {}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn test() {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unignore() {
|
|
|
|
check_assist(
|
2020-11-30 04:45:32 -06:00
|
|
|
toggle_ignore,
|
2020-11-23 13:22:13 -06:00
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
#[test$0]
|
2020-11-23 13:22:13 -06:00
|
|
|
#[ignore]
|
|
|
|
fn test() {}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
#[test]
|
|
|
|
fn test() {}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2020-11-17 07:22:04 -06:00
|
|
|
}
|