diff options
author | Aleksey Kladov <[email protected]> | 2020-11-17 13:08:31 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-11-17 13:33:30 +0000 |
commit | 8c6f933773df872a394fdcae890b9c0c42bdbc6c (patch) | |
tree | 2ac6ed3ce23af719e18c521c894d63504f6eb4d2 | |
parent | 10fa9c595ab6cae7420eb879cabadc30db4d6d7e (diff) |
**Unwrap Block** supports stand-alone blocks
-rw-r--r-- | crates/assists/src/handlers/unwrap_block.rs | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/crates/assists/src/handlers/unwrap_block.rs b/crates/assists/src/handlers/unwrap_block.rs index a3ef33520..496714243 100644 --- a/crates/assists/src/handlers/unwrap_block.rs +++ b/crates/assists/src/handlers/unwrap_block.rs | |||
@@ -3,7 +3,7 @@ use syntax::{ | |||
3 | self, | 3 | self, |
4 | edit::{AstNodeEdit, IndentLevel}, | 4 | edit::{AstNodeEdit, IndentLevel}, |
5 | }, | 5 | }, |
6 | AstNode, TextRange, T, | 6 | AstNode, SyntaxKind, TextRange, T, |
7 | }; | 7 | }; |
8 | 8 | ||
9 | use crate::{utils::unwrap_trivial_block, AssistContext, AssistId, AssistKind, Assists}; | 9 | 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<()> | |||
37 | parent = parent.ancestors().find(|it| ast::MatchExpr::can_cast(it.kind()))? | 37 | parent = parent.ancestors().find(|it| ast::MatchExpr::can_cast(it.kind()))? |
38 | } | 38 | } |
39 | 39 | ||
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 | |||
40 | let parent = ast::Expr::cast(parent)?; | 49 | let parent = ast::Expr::cast(parent)?; |
41 | 50 | ||
42 | match parent.clone() { | 51 | match parent.clone() { |
@@ -110,6 +119,64 @@ mod tests { | |||
110 | use super::*; | 119 | use super::*; |
111 | 120 | ||
112 | #[test] | 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 | |||
179 | #[test] | ||
113 | fn simple_if() { | 180 | fn simple_if() { |
114 | check_assist( | 181 | check_assist( |
115 | unwrap_block, | 182 | unwrap_block, |