diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-25 14:11:18 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-25 14:11:18 +0000 |
commit | d88a96bd05fdfdc6986e7807c93400af2cf6fa0f (patch) | |
tree | c7b41cc90eb3f02637168598a24ffebba338eb52 /crates/ra_assists | |
parent | c4ead49361e4b8c0586b810399c8e96a468b891c (diff) | |
parent | 61315e4a891b29c76bdae58b7f1e74c88732c398 (diff) |
Merge #1037
1037: inline immutable local varialbe r=matklad a=gfreezy
resolved #1033
Co-authored-by: gfreezy <[email protected]>
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/inline_local_variable.rs | 298 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 2 |
2 files changed, 300 insertions, 0 deletions
diff --git a/crates/ra_assists/src/inline_local_variable.rs b/crates/ra_assists/src/inline_local_variable.rs new file mode 100644 index 000000000..bd3cdb970 --- /dev/null +++ b/crates/ra_assists/src/inline_local_variable.rs | |||
@@ -0,0 +1,298 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use hir::source_binder::function_from_child_node; | ||
3 | use ra_syntax::{ast::{self, AstNode}, TextRange}; | ||
4 | use ra_syntax::ast::{PatKind, ExprKind}; | ||
5 | |||
6 | use crate::{Assist, AssistCtx, AssistId}; | ||
7 | use crate::assist_ctx::AssistBuilder; | ||
8 | |||
9 | pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | ||
10 | let let_stmt = ctx.node_at_offset::<ast::LetStmt>()?; | ||
11 | let bind_pat = match let_stmt.pat()?.kind() { | ||
12 | PatKind::BindPat(pat) => pat, | ||
13 | _ => return None, | ||
14 | }; | ||
15 | if bind_pat.is_mutable() { | ||
16 | return None; | ||
17 | } | ||
18 | let initializer = let_stmt.initializer()?; | ||
19 | let wrap_in_parens = match initializer.kind() { | ||
20 | ExprKind::LambdaExpr(_) | ||
21 | | ExprKind::IfExpr(_) | ||
22 | | ExprKind::LoopExpr(_) | ||
23 | | ExprKind::ForExpr(_) | ||
24 | | ExprKind::WhileExpr(_) | ||
25 | | ExprKind::ContinueExpr(_) | ||
26 | | ExprKind::BreakExpr(_) | ||
27 | | ExprKind::Label(_) | ||
28 | | ExprKind::ReturnExpr(_) | ||
29 | | ExprKind::MatchExpr(_) | ||
30 | | ExprKind::StructLit(_) | ||
31 | | ExprKind::CastExpr(_) | ||
32 | | ExprKind::PrefixExpr(_) | ||
33 | | ExprKind::RangeExpr(_) | ||
34 | | ExprKind::BinExpr(_) => true, | ||
35 | ExprKind::CallExpr(_) | ||
36 | | ExprKind::IndexExpr(_) | ||
37 | | ExprKind::MethodCallExpr(_) | ||
38 | | ExprKind::FieldExpr(_) | ||
39 | | ExprKind::TryExpr(_) | ||
40 | | ExprKind::RefExpr(_) | ||
41 | | ExprKind::Literal(_) | ||
42 | | ExprKind::TupleExpr(_) | ||
43 | | ExprKind::ArrayExpr(_) | ||
44 | | ExprKind::ParenExpr(_) | ||
45 | | ExprKind::PathExpr(_) | ||
46 | | ExprKind::BlockExpr(_) => false, | ||
47 | }; | ||
48 | |||
49 | let delete_range = if let Some(whitespace) = | ||
50 | let_stmt.syntax().next_sibling().and_then(ast::Whitespace::cast) | ||
51 | { | ||
52 | TextRange::from_to(let_stmt.syntax().range().start(), whitespace.syntax().range().end()) | ||
53 | } else { | ||
54 | let_stmt.syntax().range() | ||
55 | }; | ||
56 | |||
57 | let init_str = if wrap_in_parens { | ||
58 | format!("({})", initializer.syntax().text().to_string()) | ||
59 | } else { | ||
60 | initializer.syntax().text().to_string() | ||
61 | }; | ||
62 | let function = function_from_child_node(ctx.db, ctx.frange.file_id, bind_pat.syntax())?; | ||
63 | let scope = function.scopes(ctx.db); | ||
64 | let refs = scope.find_all_refs(bind_pat); | ||
65 | |||
66 | ctx.add_action( | ||
67 | AssistId("inline_local_variable"), | ||
68 | "inline local variable", | ||
69 | move |edit: &mut AssistBuilder| { | ||
70 | edit.delete(delete_range); | ||
71 | for desc in refs { | ||
72 | edit.replace(desc.range, init_str.clone()) | ||
73 | } | ||
74 | edit.set_cursor(delete_range.start()) | ||
75 | }, | ||
76 | ); | ||
77 | |||
78 | ctx.build() | ||
79 | } | ||
80 | |||
81 | #[cfg(test)] | ||
82 | mod tests { | ||
83 | use crate::helpers::{check_assist, check_assist_not_applicable}; | ||
84 | |||
85 | use super::*; | ||
86 | |||
87 | #[test] | ||
88 | fn test_inline_let_bind_literal_expr() { | ||
89 | check_assist( | ||
90 | inline_local_varialbe, | ||
91 | " | ||
92 | fn bar(a: usize) {} | ||
93 | fn foo() { | ||
94 | let a<|> = 1; | ||
95 | a + 1; | ||
96 | if a > 10 { | ||
97 | } | ||
98 | |||
99 | while a > 10 { | ||
100 | |||
101 | } | ||
102 | let b = a * 10; | ||
103 | bar(a); | ||
104 | }", | ||
105 | " | ||
106 | fn bar(a: usize) {} | ||
107 | fn foo() { | ||
108 | <|>1 + 1; | ||
109 | if 1 > 10 { | ||
110 | } | ||
111 | |||
112 | while 1 > 10 { | ||
113 | |||
114 | } | ||
115 | let b = 1 * 10; | ||
116 | bar(1); | ||
117 | }", | ||
118 | ); | ||
119 | } | ||
120 | |||
121 | #[test] | ||
122 | fn test_inline_let_bind_bin_expr() { | ||
123 | check_assist( | ||
124 | inline_local_varialbe, | ||
125 | " | ||
126 | fn bar(a: usize) {} | ||
127 | fn foo() { | ||
128 | let a<|> = 1 + 1; | ||
129 | a + 1; | ||
130 | if a > 10 { | ||
131 | } | ||
132 | |||
133 | while a > 10 { | ||
134 | |||
135 | } | ||
136 | let b = a * 10; | ||
137 | bar(a); | ||
138 | }", | ||
139 | " | ||
140 | fn bar(a: usize) {} | ||
141 | fn foo() { | ||
142 | <|>(1 + 1) + 1; | ||
143 | if (1 + 1) > 10 { | ||
144 | } | ||
145 | |||
146 | while (1 + 1) > 10 { | ||
147 | |||
148 | } | ||
149 | let b = (1 + 1) * 10; | ||
150 | bar((1 + 1)); | ||
151 | }", | ||
152 | ); | ||
153 | } | ||
154 | |||
155 | #[test] | ||
156 | fn test_inline_let_bind_function_call_expr() { | ||
157 | check_assist( | ||
158 | inline_local_varialbe, | ||
159 | " | ||
160 | fn bar(a: usize) {} | ||
161 | fn foo() { | ||
162 | let a<|> = bar(1); | ||
163 | a + 1; | ||
164 | if a > 10 { | ||
165 | } | ||
166 | |||
167 | while a > 10 { | ||
168 | |||
169 | } | ||
170 | let b = a * 10; | ||
171 | bar(a); | ||
172 | }", | ||
173 | " | ||
174 | fn bar(a: usize) {} | ||
175 | fn foo() { | ||
176 | <|>bar(1) + 1; | ||
177 | if bar(1) > 10 { | ||
178 | } | ||
179 | |||
180 | while bar(1) > 10 { | ||
181 | |||
182 | } | ||
183 | let b = bar(1) * 10; | ||
184 | bar(bar(1)); | ||
185 | }", | ||
186 | ); | ||
187 | } | ||
188 | |||
189 | #[test] | ||
190 | fn test_inline_let_bind_cast_expr() { | ||
191 | check_assist( | ||
192 | inline_local_varialbe, | ||
193 | " | ||
194 | fn bar(a: usize): usize { a } | ||
195 | fn foo() { | ||
196 | let a<|> = bar(1) as u64; | ||
197 | a + 1; | ||
198 | if a > 10 { | ||
199 | } | ||
200 | |||
201 | while a > 10 { | ||
202 | |||
203 | } | ||
204 | let b = a * 10; | ||
205 | bar(a); | ||
206 | }", | ||
207 | " | ||
208 | fn bar(a: usize): usize { a } | ||
209 | fn foo() { | ||
210 | <|>(bar(1) as u64) + 1; | ||
211 | if (bar(1) as u64) > 10 { | ||
212 | } | ||
213 | |||
214 | while (bar(1) as u64) > 10 { | ||
215 | |||
216 | } | ||
217 | let b = (bar(1) as u64) * 10; | ||
218 | bar((bar(1) as u64)); | ||
219 | }", | ||
220 | ); | ||
221 | } | ||
222 | |||
223 | #[test] | ||
224 | fn test_inline_let_bind_block_expr() { | ||
225 | check_assist( | ||
226 | inline_local_varialbe, | ||
227 | " | ||
228 | fn foo() { | ||
229 | let a<|> = { 10 + 1 }; | ||
230 | a + 1; | ||
231 | if a > 10 { | ||
232 | } | ||
233 | |||
234 | while a > 10 { | ||
235 | |||
236 | } | ||
237 | let b = a * 10; | ||
238 | bar(a); | ||
239 | }", | ||
240 | " | ||
241 | fn foo() { | ||
242 | <|>{ 10 + 1 } + 1; | ||
243 | if { 10 + 1 } > 10 { | ||
244 | } | ||
245 | |||
246 | while { 10 + 1 } > 10 { | ||
247 | |||
248 | } | ||
249 | let b = { 10 + 1 } * 10; | ||
250 | bar({ 10 + 1 }); | ||
251 | }", | ||
252 | ); | ||
253 | } | ||
254 | |||
255 | #[test] | ||
256 | fn test_inline_let_bind_paren_expr() { | ||
257 | check_assist( | ||
258 | inline_local_varialbe, | ||
259 | " | ||
260 | fn foo() { | ||
261 | let a<|> = ( 10 + 1 ); | ||
262 | a + 1; | ||
263 | if a > 10 { | ||
264 | } | ||
265 | |||
266 | while a > 10 { | ||
267 | |||
268 | } | ||
269 | let b = a * 10; | ||
270 | bar(a); | ||
271 | }", | ||
272 | " | ||
273 | fn foo() { | ||
274 | <|>( 10 + 1 ) + 1; | ||
275 | if ( 10 + 1 ) > 10 { | ||
276 | } | ||
277 | |||
278 | while ( 10 + 1 ) > 10 { | ||
279 | |||
280 | } | ||
281 | let b = ( 10 + 1 ) * 10; | ||
282 | bar(( 10 + 1 )); | ||
283 | }", | ||
284 | ); | ||
285 | } | ||
286 | |||
287 | #[test] | ||
288 | fn test_not_inline_mut_variable() { | ||
289 | check_assist_not_applicable( | ||
290 | inline_local_varialbe, | ||
291 | " | ||
292 | fn foo() { | ||
293 | let mut a<|> = 1 + 1; | ||
294 | a + 1; | ||
295 | }", | ||
296 | ); | ||
297 | } | ||
298 | } | ||
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 8900fbc4b..2e47b5215 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -93,6 +93,7 @@ mod change_visibility; | |||
93 | mod fill_match_arms; | 93 | mod fill_match_arms; |
94 | mod fill_struct_fields; | 94 | mod fill_struct_fields; |
95 | mod introduce_variable; | 95 | mod introduce_variable; |
96 | mod inline_local_variable; | ||
96 | mod replace_if_let_with_match; | 97 | mod replace_if_let_with_match; |
97 | mod split_import; | 98 | mod split_import; |
98 | mod remove_dbg; | 99 | mod remove_dbg; |
@@ -115,6 +116,7 @@ fn all_assists<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assis | |||
115 | auto_import::auto_import, | 116 | auto_import::auto_import, |
116 | add_missing_impl_members::add_missing_impl_members, | 117 | add_missing_impl_members::add_missing_impl_members, |
117 | add_missing_impl_members::add_missing_default_members, | 118 | add_missing_impl_members::add_missing_default_members, |
119 | inline_local_variable::inline_local_varialbe, | ||
118 | ] | 120 | ] |
119 | } | 121 | } |
120 | 122 | ||