diff options
author | gfreezy <[email protected]> | 2019-03-24 17:05:11 +0000 |
---|---|---|
committer | gfreezy <[email protected]> | 2019-03-25 02:18:20 +0000 |
commit | fd1585a071718ef9c9fb44f88336608dd7e624a5 (patch) | |
tree | 9f36cae02e334c57fe34b7f4ec3ef1084cd9cb87 /crates | |
parent | 94cf23bfc4738990e6c74424450bc40f0beb2b62 (diff) |
inline immutable local varialbe
Diffstat (limited to 'crates')
-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..ce0aafb27 --- /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(_) => true, | ||
21 | ExprKind::IfExpr(_) => true, | ||
22 | ExprKind::LoopExpr(_) => true, | ||
23 | ExprKind::ForExpr(_) => true, | ||
24 | ExprKind::WhileExpr(_) => true, | ||
25 | ExprKind::ContinueExpr(_) => true, | ||
26 | ExprKind::BreakExpr(_) => true, | ||
27 | ExprKind::Label(_) => true, | ||
28 | ExprKind::ReturnExpr(_) => true, | ||
29 | ExprKind::MatchExpr(_) => true, | ||
30 | ExprKind::StructLit(_) => true, | ||
31 | ExprKind::CastExpr(_) => true, | ||
32 | ExprKind::PrefixExpr(_) => true, | ||
33 | ExprKind::RangeExpr(_) => true, | ||
34 | ExprKind::BinExpr(_) => true, | ||
35 | ExprKind::CallExpr(_) => false, | ||
36 | ExprKind::IndexExpr(_) => false, | ||
37 | ExprKind::MethodCallExpr(_) => false, | ||
38 | ExprKind::FieldExpr(_) => false, | ||
39 | ExprKind::TryExpr(_) => false, | ||
40 | ExprKind::RefExpr(_) => false, | ||
41 | ExprKind::Literal(_) => false, | ||
42 | ExprKind::TupleExpr(_) => false, | ||
43 | ExprKind::ArrayExpr(_) => false, | ||
44 | ExprKind::ParenExpr(_) => false, | ||
45 | ExprKind::PathExpr(_) => false, | ||
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 fc36e8cc9..378470ac8 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -92,6 +92,7 @@ mod change_visibility; | |||
92 | mod fill_match_arms; | 92 | mod fill_match_arms; |
93 | mod fill_struct_fields; | 93 | mod fill_struct_fields; |
94 | mod introduce_variable; | 94 | mod introduce_variable; |
95 | mod inline_local_variable; | ||
95 | mod replace_if_let_with_match; | 96 | mod replace_if_let_with_match; |
96 | mod split_import; | 97 | mod split_import; |
97 | mod remove_dbg; | 98 | mod remove_dbg; |
@@ -113,6 +114,7 @@ fn all_assists<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assis | |||
113 | auto_import::auto_import, | 114 | auto_import::auto_import, |
114 | add_missing_impl_members::add_missing_impl_members, | 115 | add_missing_impl_members::add_missing_impl_members, |
115 | add_missing_impl_members::add_missing_default_members, | 116 | add_missing_impl_members::add_missing_default_members, |
117 | inline_local_variable::inline_local_varialbe, | ||
116 | ] | 118 | ] |
117 | } | 119 | } |
118 | 120 | ||