Skip to content

Commit 34f2848

Browse files
committed
Fix indent for toggle_ignore
Example --- ```rust mod indent { #[test$0] fn test() {} } ``` **Before this PR** ```rust mod indent { #[test] #[ignore] fn test() {} } ``` And re-enable ```rust mod indent { #[test] fn test() {} } ``` **After this PR** ```rust mod indent { #[test] #[ignore] fn test() {} } ```
1 parent 450860a commit 34f2848

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/toggle_ignore.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use syntax::{
22
AstNode, AstToken,
3-
ast::{self, HasAttrs},
3+
ast::{self, HasAttrs, edit::AstNodeEdit},
44
};
55

66
use crate::{AssistContext, AssistId, Assists, utils::test_related_attribute_syn};
@@ -27,13 +27,16 @@ pub(crate) fn toggle_ignore(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
2727
let attr: ast::Attr = ctx.find_node_at_offset()?;
2828
let func = attr.syntax().parent().and_then(ast::Fn::cast)?;
2929
let attr = test_related_attribute_syn(&func)?;
30+
let indent = attr.indent_level();
3031

3132
match has_ignore_attribute(&func) {
3233
None => acc.add(
3334
AssistId::refactor("toggle_ignore"),
3435
"Ignore this test",
3536
attr.syntax().text_range(),
36-
|builder| builder.insert(attr.syntax().text_range().end(), "\n#[ignore]"),
37+
|builder| {
38+
builder.insert(attr.syntax().text_range().end(), format!("\n{indent}#[ignore]"))
39+
},
3740
),
3841
Some(ignore_attr) => acc.add(
3942
AssistId::refactor("toggle_ignore"),
@@ -69,13 +72,17 @@ mod tests {
6972
check_assist(
7073
toggle_ignore,
7174
r#"
72-
#[test$0]
73-
fn test() {}
75+
mod indent {
76+
#[test$0]
77+
fn test() {}
78+
}
7479
"#,
7580
r#"
76-
#[test]
77-
#[ignore]
78-
fn test() {}
81+
mod indent {
82+
#[test]
83+
#[ignore]
84+
fn test() {}
85+
}
7986
"#,
8087
)
8188
}
@@ -85,13 +92,17 @@ mod tests {
8592
check_assist(
8693
toggle_ignore,
8794
r#"
88-
#[test$0]
89-
#[ignore]
90-
fn test() {}
95+
mod indent {
96+
#[test$0]
97+
#[ignore]
98+
fn test() {}
99+
}
91100
"#,
92101
r#"
93-
#[test]
94-
fn test() {}
102+
mod indent {
103+
#[test]
104+
fn test() {}
105+
}
95106
"#,
96107
)
97108
}

0 commit comments

Comments
 (0)