diff options
Diffstat (limited to 'crates/ide_assists')
-rw-r--r-- | crates/ide_assists/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ide_assists/src/handlers/apply_demorgan.rs | 87 | ||||
-rw-r--r-- | crates/ide_assists/src/handlers/generate_is_empty_from_len.rs | 272 | ||||
-rw-r--r-- | crates/ide_assists/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ide_assists/src/tests/generated.rs | 29 |
5 files changed, 380 insertions, 12 deletions
diff --git a/crates/ide_assists/Cargo.toml b/crates/ide_assists/Cargo.toml index dd9aa27c6..a83acb191 100644 --- a/crates/ide_assists/Cargo.toml +++ b/crates/ide_assists/Cargo.toml | |||
@@ -10,7 +10,7 @@ edition = "2018" | |||
10 | doctest = false | 10 | doctest = false |
11 | 11 | ||
12 | [dependencies] | 12 | [dependencies] |
13 | cov-mark = "1.1" | 13 | cov-mark = { version = "1.1", features = ["thread-local"] } |
14 | rustc-hash = "1.1.0" | 14 | rustc-hash = "1.1.0" |
15 | itertools = "0.10.0" | 15 | itertools = "0.10.0" |
16 | either = "1.6.1" | 16 | either = "1.6.1" |
diff --git a/crates/ide_assists/src/handlers/apply_demorgan.rs b/crates/ide_assists/src/handlers/apply_demorgan.rs index a1c339603..5c936a510 100644 --- a/crates/ide_assists/src/handlers/apply_demorgan.rs +++ b/crates/ide_assists/src/handlers/apply_demorgan.rs | |||
@@ -1,3 +1,5 @@ | |||
1 | use std::collections::VecDeque; | ||
2 | |||
1 | use syntax::ast::{self, AstNode}; | 3 | use syntax::ast::{self, AstNode}; |
2 | 4 | ||
3 | use crate::{utils::invert_boolean_expression, AssistContext, AssistId, AssistKind, Assists}; | 5 | use crate::{utils::invert_boolean_expression, AssistContext, AssistId, AssistKind, Assists}; |
@@ -30,19 +32,52 @@ pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<( | |||
30 | return None; | 32 | return None; |
31 | } | 33 | } |
32 | 34 | ||
33 | let lhs = expr.lhs()?; | 35 | let mut expr = expr; |
34 | let lhs_range = lhs.syntax().text_range(); | 36 | |
35 | let not_lhs = invert_boolean_expression(&ctx.sema, lhs); | 37 | // Walk up the tree while we have the same binary operator |
38 | while let Some(parent_expr) = expr.syntax().parent().and_then(ast::BinExpr::cast) { | ||
39 | if let Some(parent_op) = expr.op_kind() { | ||
40 | if parent_op == op { | ||
41 | expr = parent_expr | ||
42 | } | ||
43 | } | ||
44 | } | ||
45 | |||
46 | let mut expr_stack = vec![expr.clone()]; | ||
47 | let mut terms = Vec::new(); | ||
48 | let mut op_ranges = Vec::new(); | ||
49 | |||
50 | // Find all the children with the same binary operator | ||
51 | while let Some(expr) = expr_stack.pop() { | ||
52 | let mut traverse_bin_expr_arm = |expr| { | ||
53 | if let ast::Expr::BinExpr(bin_expr) = expr { | ||
54 | if let Some(expr_op) = bin_expr.op_kind() { | ||
55 | if expr_op == op { | ||
56 | expr_stack.push(bin_expr); | ||
57 | } else { | ||
58 | terms.push(ast::Expr::BinExpr(bin_expr)); | ||
59 | } | ||
60 | } else { | ||
61 | terms.push(ast::Expr::BinExpr(bin_expr)); | ||
62 | } | ||
63 | } else { | ||
64 | terms.push(expr); | ||
65 | } | ||
66 | }; | ||
36 | 67 | ||
37 | let rhs = expr.rhs()?; | 68 | op_ranges.extend(expr.op_token().map(|t| t.text_range())); |
38 | let rhs_range = rhs.syntax().text_range(); | 69 | traverse_bin_expr_arm(expr.lhs()?); |
39 | let not_rhs = invert_boolean_expression(&ctx.sema, rhs); | 70 | traverse_bin_expr_arm(expr.rhs()?); |
71 | } | ||
40 | 72 | ||
41 | acc.add( | 73 | acc.add( |
42 | AssistId("apply_demorgan", AssistKind::RefactorRewrite), | 74 | AssistId("apply_demorgan", AssistKind::RefactorRewrite), |
43 | "Apply De Morgan's law", | 75 | "Apply De Morgan's law", |
44 | op_range, | 76 | op_range, |
45 | |edit| { | 77 | |edit| { |
78 | terms.sort_by_key(|t| t.syntax().text_range().start()); | ||
79 | let mut terms = VecDeque::from(terms); | ||
80 | |||
46 | let paren_expr = expr.syntax().parent().and_then(|parent| ast::ParenExpr::cast(parent)); | 81 | let paren_expr = expr.syntax().parent().and_then(|parent| ast::ParenExpr::cast(parent)); |
47 | 82 | ||
48 | let neg_expr = paren_expr | 83 | let neg_expr = paren_expr |
@@ -57,11 +92,18 @@ pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<( | |||
57 | } | 92 | } |
58 | }); | 93 | }); |
59 | 94 | ||
60 | edit.replace(op_range, opposite_op); | 95 | for op_range in op_ranges { |
96 | edit.replace(op_range, opposite_op); | ||
97 | } | ||
61 | 98 | ||
62 | if let Some(paren_expr) = paren_expr { | 99 | if let Some(paren_expr) = paren_expr { |
63 | edit.replace(lhs_range, not_lhs.syntax().text()); | 100 | for term in terms { |
64 | edit.replace(rhs_range, not_rhs.syntax().text()); | 101 | let range = term.syntax().text_range(); |
102 | let not_term = invert_boolean_expression(&ctx.sema, term); | ||
103 | |||
104 | edit.replace(range, not_term.syntax().text()); | ||
105 | } | ||
106 | |||
65 | if let Some(neg_expr) = neg_expr { | 107 | if let Some(neg_expr) = neg_expr { |
66 | cov_mark::hit!(demorgan_double_negation); | 108 | cov_mark::hit!(demorgan_double_negation); |
67 | edit.replace(neg_expr.op_token().unwrap().text_range(), ""); | 109 | edit.replace(neg_expr.op_token().unwrap().text_range(), ""); |
@@ -70,8 +112,25 @@ pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<( | |||
70 | edit.replace(paren_expr.l_paren_token().unwrap().text_range(), "!("); | 112 | edit.replace(paren_expr.l_paren_token().unwrap().text_range(), "!("); |
71 | } | 113 | } |
72 | } else { | 114 | } else { |
73 | edit.replace(lhs_range, format!("!({}", not_lhs.syntax().text())); | 115 | if let Some(lhs) = terms.pop_front() { |
74 | edit.replace(rhs_range, format!("{})", not_rhs.syntax().text())); | 116 | let lhs_range = lhs.syntax().text_range(); |
117 | let not_lhs = invert_boolean_expression(&ctx.sema, lhs); | ||
118 | |||
119 | edit.replace(lhs_range, format!("!({}", not_lhs.syntax().text())); | ||
120 | } | ||
121 | |||
122 | if let Some(rhs) = terms.pop_back() { | ||
123 | let rhs_range = rhs.syntax().text_range(); | ||
124 | let not_rhs = invert_boolean_expression(&ctx.sema, rhs); | ||
125 | |||
126 | edit.replace(rhs_range, format!("{})", not_rhs.syntax().text())); | ||
127 | } | ||
128 | |||
129 | for term in terms { | ||
130 | let term_range = term.syntax().text_range(); | ||
131 | let not_term = invert_boolean_expression(&ctx.sema, term); | ||
132 | edit.replace(term_range, not_term.syntax().text()); | ||
133 | } | ||
75 | } | 134 | } |
76 | }, | 135 | }, |
77 | ) | 136 | ) |
@@ -180,6 +239,12 @@ fn f() { | |||
180 | } | 239 | } |
181 | 240 | ||
182 | #[test] | 241 | #[test] |
242 | fn demorgan_multiple_terms() { | ||
243 | check_assist(apply_demorgan, "fn f() { x ||$0 y || z }", "fn f() { !(!x && !y && !z) }"); | ||
244 | check_assist(apply_demorgan, "fn f() { x || y ||$0 z }", "fn f() { !(!x && !y && !z) }"); | ||
245 | } | ||
246 | |||
247 | #[test] | ||
183 | fn demorgan_doesnt_apply_with_cursor_not_on_op() { | 248 | fn demorgan_doesnt_apply_with_cursor_not_on_op() { |
184 | check_assist_not_applicable(apply_demorgan, "fn f() { $0 !x || !x }") | 249 | check_assist_not_applicable(apply_demorgan, "fn f() { $0 !x || !x }") |
185 | } | 250 | } |
diff --git a/crates/ide_assists/src/handlers/generate_is_empty_from_len.rs b/crates/ide_assists/src/handlers/generate_is_empty_from_len.rs new file mode 100644 index 000000000..b8834d283 --- /dev/null +++ b/crates/ide_assists/src/handlers/generate_is_empty_from_len.rs | |||
@@ -0,0 +1,272 @@ | |||
1 | use hir::{known, HasSource, Name}; | ||
2 | use syntax::{ | ||
3 | ast::{self, NameOwner}, | ||
4 | AstNode, | ||
5 | }; | ||
6 | |||
7 | use crate::{ | ||
8 | assist_context::{AssistContext, Assists}, | ||
9 | AssistId, AssistKind, | ||
10 | }; | ||
11 | |||
12 | // Assist: generate_is_empty_from_len | ||
13 | // | ||
14 | // Generates is_empty implementation from the len method. | ||
15 | // | ||
16 | // ``` | ||
17 | // struct MyStruct { data: Vec<String> } | ||
18 | // | ||
19 | // impl MyStruct { | ||
20 | // p$0ub fn len(&self) -> usize { | ||
21 | // self.data.len() | ||
22 | // } | ||
23 | // } | ||
24 | // ``` | ||
25 | // -> | ||
26 | // ``` | ||
27 | // struct MyStruct { data: Vec<String> } | ||
28 | // | ||
29 | // impl MyStruct { | ||
30 | // pub fn len(&self) -> usize { | ||
31 | // self.data.len() | ||
32 | // } | ||
33 | // | ||
34 | // pub fn is_empty(&self) -> bool { | ||
35 | // self.len() == 0 | ||
36 | // } | ||
37 | // } | ||
38 | // ``` | ||
39 | pub(crate) fn generate_is_empty_from_len(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
40 | let fn_node = ctx.find_node_at_offset::<ast::Fn>()?; | ||
41 | let fn_name = fn_node.name()?; | ||
42 | |||
43 | if fn_name.text() != "len" { | ||
44 | cov_mark::hit!(len_function_not_present); | ||
45 | return None; | ||
46 | } | ||
47 | |||
48 | if fn_node.param_list()?.params().next().is_some() { | ||
49 | cov_mark::hit!(len_function_with_parameters); | ||
50 | return None; | ||
51 | } | ||
52 | |||
53 | let impl_ = fn_node.syntax().ancestors().find_map(ast::Impl::cast)?; | ||
54 | let len_fn = get_impl_method(ctx, &impl_, &known::len)?; | ||
55 | if !len_fn.ret_type(ctx.sema.db).is_usize() { | ||
56 | cov_mark::hit!(len_fn_different_return_type); | ||
57 | return None; | ||
58 | } | ||
59 | |||
60 | if get_impl_method(ctx, &impl_, &known::is_empty).is_some() { | ||
61 | cov_mark::hit!(is_empty_already_implemented); | ||
62 | return None; | ||
63 | } | ||
64 | |||
65 | let node = len_fn.source(ctx.sema.db)?; | ||
66 | let range = node.syntax().value.text_range(); | ||
67 | |||
68 | acc.add( | ||
69 | AssistId("generate_is_empty_from_len", AssistKind::Generate), | ||
70 | "Generate a is_empty impl from a len function", | ||
71 | range, | ||
72 | |builder| { | ||
73 | let code = r#" | ||
74 | |||
75 | pub fn is_empty(&self) -> bool { | ||
76 | self.len() == 0 | ||
77 | }"# | ||
78 | .to_string(); | ||
79 | builder.insert(range.end(), code) | ||
80 | }, | ||
81 | ) | ||
82 | } | ||
83 | |||
84 | fn get_impl_method( | ||
85 | ctx: &AssistContext, | ||
86 | impl_: &ast::Impl, | ||
87 | fn_name: &Name, | ||
88 | ) -> Option<hir::Function> { | ||
89 | let db = ctx.sema.db; | ||
90 | let impl_def: hir::Impl = ctx.sema.to_def(impl_)?; | ||
91 | |||
92 | let scope = ctx.sema.scope(impl_.syntax()); | ||
93 | let krate = impl_def.module(db).krate(); | ||
94 | let ty = impl_def.target_ty(db); | ||
95 | let traits_in_scope = scope.traits_in_scope(); | ||
96 | ty.iterate_method_candidates(db, krate, &traits_in_scope, Some(fn_name), |_, func| Some(func)) | ||
97 | } | ||
98 | |||
99 | #[cfg(test)] | ||
100 | mod tests { | ||
101 | use crate::tests::{check_assist, check_assist_not_applicable}; | ||
102 | |||
103 | use super::*; | ||
104 | |||
105 | #[test] | ||
106 | fn len_function_not_present() { | ||
107 | cov_mark::check!(len_function_not_present); | ||
108 | check_assist_not_applicable( | ||
109 | generate_is_empty_from_len, | ||
110 | r#" | ||
111 | struct MyStruct { data: Vec<String> } | ||
112 | |||
113 | impl MyStruct { | ||
114 | p$0ub fn test(&self) -> usize { | ||
115 | self.data.len() | ||
116 | } | ||
117 | } | ||
118 | "#, | ||
119 | ); | ||
120 | } | ||
121 | |||
122 | #[test] | ||
123 | fn len_function_with_parameters() { | ||
124 | cov_mark::check!(len_function_with_parameters); | ||
125 | check_assist_not_applicable( | ||
126 | generate_is_empty_from_len, | ||
127 | r#" | ||
128 | struct MyStruct { data: Vec<String> } | ||
129 | |||
130 | impl MyStruct { | ||
131 | p$0ub fn len(&self, _i: bool) -> usize { | ||
132 | self.data.len() | ||
133 | } | ||
134 | } | ||
135 | "#, | ||
136 | ); | ||
137 | } | ||
138 | |||
139 | #[test] | ||
140 | fn is_empty_already_implemented() { | ||
141 | cov_mark::check!(is_empty_already_implemented); | ||
142 | check_assist_not_applicable( | ||
143 | generate_is_empty_from_len, | ||
144 | r#" | ||
145 | struct MyStruct { data: Vec<String> } | ||
146 | |||
147 | impl MyStruct { | ||
148 | p$0ub fn len(&self) -> usize { | ||
149 | self.data.len() | ||
150 | } | ||
151 | |||
152 | pub fn is_empty(&self) -> bool { | ||
153 | self.len() == 0 | ||
154 | } | ||
155 | } | ||
156 | "#, | ||
157 | ); | ||
158 | } | ||
159 | |||
160 | #[test] | ||
161 | fn len_fn_different_return_type() { | ||
162 | cov_mark::check!(len_fn_different_return_type); | ||
163 | check_assist_not_applicable( | ||
164 | generate_is_empty_from_len, | ||
165 | r#" | ||
166 | struct MyStruct { data: Vec<String> } | ||
167 | |||
168 | impl MyStruct { | ||
169 | p$0ub fn len(&self) -> u32 { | ||
170 | self.data.len() | ||
171 | } | ||
172 | } | ||
173 | "#, | ||
174 | ); | ||
175 | } | ||
176 | |||
177 | #[test] | ||
178 | fn generate_is_empty() { | ||
179 | check_assist( | ||
180 | generate_is_empty_from_len, | ||
181 | r#" | ||
182 | struct MyStruct { data: Vec<String> } | ||
183 | |||
184 | impl MyStruct { | ||
185 | p$0ub fn len(&self) -> usize { | ||
186 | self.data.len() | ||
187 | } | ||
188 | } | ||
189 | "#, | ||
190 | r#" | ||
191 | struct MyStruct { data: Vec<String> } | ||
192 | |||
193 | impl MyStruct { | ||
194 | pub fn len(&self) -> usize { | ||
195 | self.data.len() | ||
196 | } | ||
197 | |||
198 | pub fn is_empty(&self) -> bool { | ||
199 | self.len() == 0 | ||
200 | } | ||
201 | } | ||
202 | "#, | ||
203 | ); | ||
204 | } | ||
205 | |||
206 | #[test] | ||
207 | fn multiple_functions_in_impl() { | ||
208 | check_assist( | ||
209 | generate_is_empty_from_len, | ||
210 | r#" | ||
211 | struct MyStruct { data: Vec<String> } | ||
212 | |||
213 | impl MyStruct { | ||
214 | pub fn new() -> Self { | ||
215 | Self { data: 0 } | ||
216 | } | ||
217 | |||
218 | p$0ub fn len(&self) -> usize { | ||
219 | self.data.len() | ||
220 | } | ||
221 | |||
222 | pub fn work(&self) -> Option<usize> { | ||
223 | |||
224 | } | ||
225 | } | ||
226 | "#, | ||
227 | r#" | ||
228 | struct MyStruct { data: Vec<String> } | ||
229 | |||
230 | impl MyStruct { | ||
231 | pub fn new() -> Self { | ||
232 | Self { data: 0 } | ||
233 | } | ||
234 | |||
235 | pub fn len(&self) -> usize { | ||
236 | self.data.len() | ||
237 | } | ||
238 | |||
239 | pub fn is_empty(&self) -> bool { | ||
240 | self.len() == 0 | ||
241 | } | ||
242 | |||
243 | pub fn work(&self) -> Option<usize> { | ||
244 | |||
245 | } | ||
246 | } | ||
247 | "#, | ||
248 | ); | ||
249 | } | ||
250 | |||
251 | #[test] | ||
252 | fn multiple_impls() { | ||
253 | check_assist_not_applicable( | ||
254 | generate_is_empty_from_len, | ||
255 | r#" | ||
256 | struct MyStruct { data: Vec<String> } | ||
257 | |||
258 | impl MyStruct { | ||
259 | p$0ub fn len(&self) -> usize { | ||
260 | self.data.len() | ||
261 | } | ||
262 | } | ||
263 | |||
264 | impl MyStruct { | ||
265 | pub fn is_empty(&self) -> bool { | ||
266 | self.len() == 0 | ||
267 | } | ||
268 | } | ||
269 | "#, | ||
270 | ); | ||
271 | } | ||
272 | } | ||
diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs index f1aab74d4..8c068a6c0 100644 --- a/crates/ide_assists/src/lib.rs +++ b/crates/ide_assists/src/lib.rs | |||
@@ -129,6 +129,7 @@ mod handlers { | |||
129 | mod flip_trait_bound; | 129 | mod flip_trait_bound; |
130 | mod generate_default_from_enum_variant; | 130 | mod generate_default_from_enum_variant; |
131 | mod generate_default_from_new; | 131 | mod generate_default_from_new; |
132 | mod generate_is_empty_from_len; | ||
132 | mod generate_derive; | 133 | mod generate_derive; |
133 | mod generate_enum_is_method; | 134 | mod generate_enum_is_method; |
134 | mod generate_enum_projection_method; | 135 | mod generate_enum_projection_method; |
@@ -193,6 +194,7 @@ mod handlers { | |||
193 | flip_trait_bound::flip_trait_bound, | 194 | flip_trait_bound::flip_trait_bound, |
194 | generate_default_from_enum_variant::generate_default_from_enum_variant, | 195 | generate_default_from_enum_variant::generate_default_from_enum_variant, |
195 | generate_default_from_new::generate_default_from_new, | 196 | generate_default_from_new::generate_default_from_new, |
197 | generate_is_empty_from_len::generate_is_empty_from_len, | ||
196 | generate_derive::generate_derive, | 198 | generate_derive::generate_derive, |
197 | generate_enum_is_method::generate_enum_is_method, | 199 | generate_enum_is_method::generate_enum_is_method, |
198 | generate_enum_projection_method::generate_enum_as_method, | 200 | generate_enum_projection_method::generate_enum_as_method, |
diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs index 3f77edd8d..736027ff0 100644 --- a/crates/ide_assists/src/tests/generated.rs +++ b/crates/ide_assists/src/tests/generated.rs | |||
@@ -722,6 +722,35 @@ impl<T: Clone> Ctx<T> { | |||
722 | } | 722 | } |
723 | 723 | ||
724 | #[test] | 724 | #[test] |
725 | fn doctest_generate_is_empty_from_len() { | ||
726 | check_doc_test( | ||
727 | "generate_is_empty_from_len", | ||
728 | r#####" | ||
729 | struct MyStruct { data: Vec<String> } | ||
730 | |||
731 | impl MyStruct { | ||
732 | p$0ub fn len(&self) -> usize { | ||
733 | self.data.len() | ||
734 | } | ||
735 | } | ||
736 | "#####, | ||
737 | r#####" | ||
738 | struct MyStruct { data: Vec<String> } | ||
739 | |||
740 | impl MyStruct { | ||
741 | pub fn len(&self) -> usize { | ||
742 | self.data.len() | ||
743 | } | ||
744 | |||
745 | pub fn is_empty(&self) -> bool { | ||
746 | self.len() == 0 | ||
747 | } | ||
748 | } | ||
749 | "#####, | ||
750 | ) | ||
751 | } | ||
752 | |||
753 | #[test] | ||
725 | fn doctest_generate_new() { | 754 | fn doctest_generate_new() { |
726 | check_doc_test( | 755 | check_doc_test( |
727 | "generate_new", | 756 | "generate_new", |