aboutsummaryrefslogtreecommitdiff
path: root/crates/assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/assists')
-rw-r--r--crates/assists/src/handlers/invert_if.rs9
-rw-r--r--crates/assists/src/handlers/reorder_fields.rs129
-rw-r--r--crates/assists/src/utils.rs4
3 files changed, 80 insertions, 62 deletions
diff --git a/crates/assists/src/handlers/invert_if.rs b/crates/assists/src/handlers/invert_if.rs
index ea722b91b..91e2f5c8c 100644
--- a/crates/assists/src/handlers/invert_if.rs
+++ b/crates/assists/src/handlers/invert_if.rs
@@ -69,6 +69,15 @@ mod tests {
69 use crate::tests::{check_assist, check_assist_not_applicable}; 69 use crate::tests::{check_assist, check_assist_not_applicable};
70 70
71 #[test] 71 #[test]
72 fn invert_if_composite_condition() {
73 check_assist(
74 invert_if,
75 "fn f() { i<|>f x == 3 || x == 4 || x == 5 { 1 } else { 3 * 2 } }",
76 "fn f() { if !(x == 3 || x == 4 || x == 5) { 3 * 2 } else { 1 } }",
77 )
78 }
79
80 #[test]
72 fn invert_if_remove_inequality() { 81 fn invert_if_remove_inequality() {
73 check_assist( 82 check_assist(
74 invert_if, 83 invert_if,
diff --git a/crates/assists/src/handlers/reorder_fields.rs b/crates/assists/src/handlers/reorder_fields.rs
index 7c0f0f44e..fe5574242 100644
--- a/crates/assists/src/handlers/reorder_fields.rs
+++ b/crates/assists/src/handlers/reorder_fields.rs
@@ -4,6 +4,7 @@ use rustc_hash::FxHashMap;
4use hir::{Adt, ModuleDef, PathResolution, Semantics, Struct}; 4use hir::{Adt, ModuleDef, PathResolution, Semantics, Struct};
5use ide_db::RootDatabase; 5use ide_db::RootDatabase;
6use syntax::{algo, ast, match_ast, AstNode, SyntaxKind, SyntaxKind::*, SyntaxNode}; 6use syntax::{algo, ast, match_ast, AstNode, SyntaxKind, SyntaxKind::*, SyntaxNode};
7use test_utils::mark;
7 8
8use crate::{AssistContext, AssistId, AssistKind, Assists}; 9use crate::{AssistContext, AssistId, AssistKind, Assists};
9 10
@@ -38,6 +39,7 @@ fn reorder<R: AstNode>(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
38 }); 39 });
39 40
40 if sorted_fields == fields { 41 if sorted_fields == fields {
42 mark::hit!(reorder_sorted_fields);
41 return None; 43 return None;
42 } 44 }
43 45
@@ -107,22 +109,25 @@ fn compute_fields_ranks(path: &ast::Path, ctx: &AssistContext) -> Option<FxHashM
107 109
108#[cfg(test)] 110#[cfg(test)]
109mod tests { 111mod tests {
112 use test_utils::mark;
113
110 use crate::tests::{check_assist, check_assist_not_applicable}; 114 use crate::tests::{check_assist, check_assist_not_applicable};
111 115
112 use super::*; 116 use super::*;
113 117
114 #[test] 118 #[test]
115 fn not_applicable_if_sorted() { 119 fn reorder_sorted_fields() {
120 mark::check!(reorder_sorted_fields);
116 check_assist_not_applicable( 121 check_assist_not_applicable(
117 reorder_fields, 122 reorder_fields,
118 r#" 123 r#"
119 struct Foo { 124struct Foo {
120 foo: i32, 125 foo: i32,
121 bar: i32, 126 bar: i32,
122 } 127}
123 128
124 const test: Foo = <|>Foo { foo: 0, bar: 0 }; 129const test: Foo = <|>Foo { foo: 0, bar: 0 };
125 "#, 130"#,
126 ) 131 )
127 } 132 }
128 133
@@ -131,9 +136,9 @@ mod tests {
131 check_assist_not_applicable( 136 check_assist_not_applicable(
132 reorder_fields, 137 reorder_fields,
133 r#" 138 r#"
134 struct Foo {}; 139struct Foo {};
135 const test: Foo = <|>Foo {} 140const test: Foo = <|>Foo {}
136 "#, 141"#,
137 ) 142 )
138 } 143 }
139 144
@@ -142,13 +147,13 @@ mod tests {
142 check_assist( 147 check_assist(
143 reorder_fields, 148 reorder_fields,
144 r#" 149 r#"
145 struct Foo {foo: i32, bar: i32}; 150struct Foo {foo: i32, bar: i32};
146 const test: Foo = <|>Foo {bar: 0, foo: 1} 151const test: Foo = <|>Foo {bar: 0, foo: 1}
147 "#, 152"#,
148 r#" 153 r#"
149 struct Foo {foo: i32, bar: i32}; 154struct Foo {foo: i32, bar: i32};
150 const test: Foo = Foo {foo: 1, bar: 0} 155const test: Foo = Foo {foo: 1, bar: 0}
151 "#, 156"#,
152 ) 157 )
153 } 158 }
154 159
@@ -157,25 +162,25 @@ mod tests {
157 check_assist( 162 check_assist(
158 reorder_fields, 163 reorder_fields,
159 r#" 164 r#"
160 struct Foo { foo: i64, bar: i64, baz: i64 } 165struct Foo { foo: i64, bar: i64, baz: i64 }
161 166
162 fn f(f: Foo) -> { 167fn f(f: Foo) -> {
163 match f { 168 match f {
164 <|>Foo { baz: 0, ref mut bar, .. } => (), 169 <|>Foo { baz: 0, ref mut bar, .. } => (),
165 _ => () 170 _ => ()
166 } 171 }
167 } 172}
168 "#, 173"#,
169 r#" 174 r#"
170 struct Foo { foo: i64, bar: i64, baz: i64 } 175struct Foo { foo: i64, bar: i64, baz: i64 }
171 176
172 fn f(f: Foo) -> { 177fn f(f: Foo) -> {
173 match f { 178 match f {
174 Foo { ref mut bar, baz: 0, .. } => (), 179 Foo { ref mut bar, baz: 0, .. } => (),
175 _ => () 180 _ => ()
176 } 181 }
177 } 182}
178 "#, 183"#,
179 ) 184 )
180 } 185 }
181 186
@@ -184,39 +189,39 @@ mod tests {
184 check_assist( 189 check_assist(
185 reorder_fields, 190 reorder_fields,
186 r#" 191 r#"
187 struct Foo { 192struct Foo {
188 foo: String, 193 foo: String,
189 bar: String, 194 bar: String,
190 } 195}
191 196
192 impl Foo { 197impl Foo {
193 fn new() -> Foo { 198 fn new() -> Foo {
194 let foo = String::new(); 199 let foo = String::new();
195 <|>Foo { 200 <|>Foo {
196 bar: foo.clone(), 201 bar: foo.clone(),
197 extra: "Extra field", 202 extra: "Extra field",
198 foo, 203 foo,
199 } 204 }
200 } 205 }
201 } 206}
202 "#, 207"#,
203 r#" 208 r#"
204 struct Foo { 209struct Foo {
205 foo: String, 210 foo: String,
206 bar: String, 211 bar: String,
207 } 212}
208 213
209 impl Foo { 214impl Foo {
210 fn new() -> Foo { 215 fn new() -> Foo {
211 let foo = String::new(); 216 let foo = String::new();
212 Foo { 217 Foo {
213 foo, 218 foo,
214 bar: foo.clone(), 219 bar: foo.clone(),
215 extra: "Extra field", 220 extra: "Extra field",
216 } 221 }
217 } 222 }
218 } 223}
219 "#, 224"#,
220 ) 225 )
221 } 226 }
222} 227}
diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs
index 01f5c291f..f2cacf7c8 100644
--- a/crates/assists/src/utils.rs
+++ b/crates/assists/src/utils.rs
@@ -212,6 +212,10 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
212 ast::Expr::BinExpr(bin) => match bin.op_kind()? { 212 ast::Expr::BinExpr(bin) => match bin.op_kind()? {
213 ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()), 213 ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()),
214 ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()), 214 ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()),
215 // Parenthesize composite boolean expressions before prefixing `!`
216 ast::BinOp::BooleanAnd | ast::BinOp::BooleanOr => {
217 Some(make::expr_prefix(T![!], make::expr_paren(expr.clone())))
218 }
215 _ => None, 219 _ => None,
216 }, 220 },
217 ast::Expr::MethodCallExpr(mce) => { 221 ast::Expr::MethodCallExpr(mce) => {