diff options
Diffstat (limited to 'crates/ra_ide_api_light')
-rw-r--r-- | crates/ra_ide_api_light/src/join_lines.rs | 46 |
1 files changed, 18 insertions, 28 deletions
diff --git a/crates/ra_ide_api_light/src/join_lines.rs b/crates/ra_ide_api_light/src/join_lines.rs index 9f59fbd43..9a400199f 100644 --- a/crates/ra_ide_api_light/src/join_lines.rs +++ b/crates/ra_ide_api_light/src/join_lines.rs | |||
@@ -120,28 +120,15 @@ fn remove_newline( | |||
120 | } | 120 | } |
121 | } | 121 | } |
122 | 122 | ||
123 | /// fixes a comma after the given expression and optionally inserts a new trailing comma | 123 | fn has_comma_after(node: &SyntaxNode) -> bool { |
124 | /// if no comma was found and `comma_offset` is provided | ||
125 | fn fix_comma_after(edit: &mut TextEditBuilder, node: &SyntaxNode, comma_offset: Option<TextUnit>) { | ||
126 | let next = node.next_sibling(); | 124 | let next = node.next_sibling(); |
127 | let nnext = node.next_sibling().and_then(|n| n.next_sibling()); | 125 | let nnext = node.next_sibling().and_then(|n| n.next_sibling()); |
128 | 126 | ||
129 | match (next, nnext) { | 127 | match (next, nnext) { |
130 | // Whitespace followed by a comma | 128 | // Whitespace followed by a comma is fine |
131 | // remove the whitespace | 129 | (Some(ws), Some(comma)) if ws.kind() == WHITESPACE && comma.kind() == COMMA => true, |
132 | (Some(ws), Some(comma)) if ws.kind() == WHITESPACE && comma.kind() == COMMA => { | 130 | (Some(n), _) => n.kind() == COMMA, |
133 | edit.delete(ws.range()); | 131 | _ => false, |
134 | } | ||
135 | |||
136 | // if we are not a comma and if comma_offset was provided, | ||
137 | // insert trailing comma after the block | ||
138 | (Some(n), _) if n.kind() != COMMA => { | ||
139 | if let Some(comma_offset) = comma_offset { | ||
140 | edit.insert(comma_offset, ",".to_owned()); | ||
141 | } | ||
142 | } | ||
143 | |||
144 | _ => {} | ||
145 | } | 132 | } |
146 | } | 133 | } |
147 | 134 | ||
@@ -151,16 +138,17 @@ fn join_single_expr_block(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Opti | |||
151 | let expr = extract_trivial_expression(block)?; | 138 | let expr = extract_trivial_expression(block)?; |
152 | 139 | ||
153 | let block_range = block_expr.syntax().range(); | 140 | let block_range = block_expr.syntax().range(); |
154 | edit.replace(block_range, expr.syntax().text().to_string()); | 141 | let mut buf = expr.syntax().text().to_string(); |
155 | 142 | ||
156 | // Match block needs to have a comma after the block | 143 | // Match block needs to have a comma after the block |
157 | // otherwise we'll maintain a comma after the block if such existed | ||
158 | // but we remove excess whitespace between the expression and the comma. | ||
159 | if let Some(match_arm) = block_expr.syntax().parent().and_then(ast::MatchArm::cast) { | 144 | if let Some(match_arm) = block_expr.syntax().parent().and_then(ast::MatchArm::cast) { |
160 | fix_comma_after(edit, match_arm.syntax(), Some(block_range.end())); | 145 | if !has_comma_after(match_arm.syntax()) { |
161 | } else { | 146 | buf.push(','); |
162 | fix_comma_after(edit, block_expr.syntax(), None); | 147 | } |
163 | } | 148 | } |
149 | |||
150 | edit.replace(block_range, buf); | ||
151 | |||
164 | Some(()) | 152 | Some(()) |
165 | } | 153 | } |
166 | 154 | ||
@@ -301,7 +289,7 @@ fn foo(e: Result<U, V>) { | |||
301 | r" | 289 | r" |
302 | fn foo(e: Result<U, V>) { | 290 | fn foo(e: Result<U, V>) { |
303 | match e { | 291 | match e { |
304 | Ok(u) => <|>u.foo(), | 292 | Ok(u) => <|>u.foo() , |
305 | Err(v) => v, | 293 | Err(v) => v, |
306 | } | 294 | } |
307 | }", | 295 | }", |
@@ -322,7 +310,8 @@ fn foo(e: Result<U, V>) { | |||
322 | r" | 310 | r" |
323 | fn foo(e: Result<U, V>) { | 311 | fn foo(e: Result<U, V>) { |
324 | match e { | 312 | match e { |
325 | Ok(u) => <|>u.foo(), | 313 | Ok(u) => <|>u.foo() |
314 | , | ||
326 | Err(v) => v, | 315 | Err(v) => v, |
327 | } | 316 | } |
328 | }", | 317 | }", |
@@ -355,7 +344,7 @@ fn foo() { | |||
355 | }", | 344 | }", |
356 | r" | 345 | r" |
357 | fn foo() { | 346 | fn foo() { |
358 | let x = (<|>4,); | 347 | let x = (<|>4 ,); |
359 | }", | 348 | }", |
360 | ); | 349 | ); |
361 | 350 | ||
@@ -370,7 +359,8 @@ fn foo() { | |||
370 | }", | 359 | }", |
371 | r" | 360 | r" |
372 | fn foo() { | 361 | fn foo() { |
373 | let x = (<|>4,); | 362 | let x = (<|>4 |
363 | ,); | ||
374 | }", | 364 | }", |
375 | ); | 365 | ); |
376 | } | 366 | } |