Skip to content

Commit 8c6f933

Browse files
committed
**Unwrap Block** supports stand-alone blocks
1 parent 10fa9c5 commit 8c6f933

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

crates/assists/src/handlers/unwrap_block.rs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use syntax::{
33
self,
44
edit::{AstNodeEdit, IndentLevel},
55
},
6-
AstNode, TextRange, T,
6+
AstNode, SyntaxKind, TextRange, T,
77
};
88

99
use crate::{utils::unwrap_trivial_block, AssistContext, AssistId, AssistKind, Assists};
@@ -37,6 +37,15 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
3737
parent = parent.ancestors().find(|it| ast::MatchExpr::can_cast(it.kind()))?
3838
}
3939

40+
if matches!(parent.kind(), SyntaxKind::BLOCK_EXPR | SyntaxKind::EXPR_STMT) {
41+
return acc.add(assist_id, assist_label, target, |builder| {
42+
builder.replace(
43+
block.syntax().text_range(),
44+
update_expr_string(block.to_string(), &[' ', '{', '\n']),
45+
);
46+
});
47+
}
48+
4049
let parent = ast::Expr::cast(parent)?;
4150

4251
match parent.clone() {
@@ -109,6 +118,64 @@ mod tests {
109118

110119
use super::*;
111120

121+
#[test]
122+
fn unwrap_tail_expr_block() {
123+
check_assist(
124+
unwrap_block,
125+
r#"
126+
fn main() {
127+
<|>{
128+
92
129+
}
130+
}
131+
"#,
132+
r#"
133+
fn main() {
134+
92
135+
}
136+
"#,
137+
)
138+
}
139+
140+
#[test]
141+
fn unwrap_stmt_expr_block() {
142+
check_assist(
143+
unwrap_block,
144+
r#"
145+
fn main() {
146+
<|>{
147+
92;
148+
}
149+
()
150+
}
151+
"#,
152+
r#"
153+
fn main() {
154+
92;
155+
()
156+
}
157+
"#,
158+
);
159+
// Pedantically, we should add an `;` here...
160+
check_assist(
161+
unwrap_block,
162+
r#"
163+
fn main() {
164+
<|>{
165+
92
166+
}
167+
()
168+
}
169+
"#,
170+
r#"
171+
fn main() {
172+
92
173+
()
174+
}
175+
"#,
176+
);
177+
}
178+
112179
#[test]
113180
fn simple_if() {
114181
check_assist(

0 commit comments

Comments
 (0)