diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-05 07:16:48 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-05 07:16:48 +0000 |
commit | 481713a0e17e1557288e88a6b1a173b111792998 (patch) | |
tree | fc5aa5ccd6a4df6393dc7a14e78ac061148096c7 /crates/ra_editor/src | |
parent | 4608a41ad41b00130a719e8ada65d8064fbc9d54 (diff) | |
parent | 19c641390d2d152cddf9616aef51fa291260d289 (diff) |
Merge #427
427: Remove extra space when joining lines in use items r=matklad a=alanhdu
Fixes #423.
Co-authored-by: Alan Du <[email protected]>
Diffstat (limited to 'crates/ra_editor/src')
-rw-r--r-- | crates/ra_editor/src/typing.rs | 60 |
1 files changed, 58 insertions, 2 deletions
diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index dd3d0f260..1b568e96c 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs | |||
@@ -188,10 +188,14 @@ fn remove_newline( | |||
188 | edit.delete(TextRange::from_to(prev.range().start(), node.range().end())); | 188 | edit.delete(TextRange::from_to(prev.range().start(), node.range().end())); |
189 | } else if prev.kind() == COMMA && next.kind() == R_CURLY { | 189 | } else if prev.kind() == COMMA && next.kind() == R_CURLY { |
190 | // Removes: comma, newline (incl. surrounding whitespace) | 190 | // Removes: comma, newline (incl. surrounding whitespace) |
191 | // Adds: a single whitespace | 191 | let space = if let Some(left) = prev.prev_sibling() { |
192 | compute_ws(left, next) | ||
193 | } else { | ||
194 | " " | ||
195 | }; | ||
192 | edit.replace( | 196 | edit.replace( |
193 | TextRange::from_to(prev.range().start(), node.range().end()), | 197 | TextRange::from_to(prev.range().start(), node.range().end()), |
194 | " ".to_string(), | 198 | space.to_string(), |
195 | ); | 199 | ); |
196 | } else if let (Some(_), Some(next)) = (ast::Comment::cast(prev), ast::Comment::cast(next)) { | 200 | } else if let (Some(_), Some(next)) = (ast::Comment::cast(prev), ast::Comment::cast(next)) { |
197 | // Removes: newline (incl. surrounding whitespace), start of the next comment | 201 | // Removes: newline (incl. surrounding whitespace), start of the next comment |
@@ -256,10 +260,20 @@ fn join_single_use_tree(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Opti | |||
256 | fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str { | 260 | fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str { |
257 | match left.kind() { | 261 | match left.kind() { |
258 | L_PAREN | L_BRACK => return "", | 262 | L_PAREN | L_BRACK => return "", |
263 | L_CURLY => { | ||
264 | if let USE_TREE = right.kind() { | ||
265 | return ""; | ||
266 | } | ||
267 | } | ||
259 | _ => (), | 268 | _ => (), |
260 | } | 269 | } |
261 | match right.kind() { | 270 | match right.kind() { |
262 | R_PAREN | R_BRACK => return "", | 271 | R_PAREN | R_BRACK => return "", |
272 | R_CURLY => { | ||
273 | if let USE_TREE = left.kind() { | ||
274 | return ""; | ||
275 | } | ||
276 | } | ||
263 | DOT => return "", | 277 | DOT => return "", |
264 | _ => (), | 278 | _ => (), |
265 | } | 279 | } |
@@ -331,6 +345,48 @@ fn foo() { | |||
331 | } | 345 | } |
332 | 346 | ||
333 | #[test] | 347 | #[test] |
348 | fn test_join_lines_use_items_left() { | ||
349 | // No space after the '{' | ||
350 | check_join_lines( | ||
351 | r" | ||
352 | <|>use ra_syntax::{ | ||
353 | TextUnit, TextRange, | ||
354 | };", | ||
355 | r" | ||
356 | <|>use ra_syntax::{TextUnit, TextRange, | ||
357 | };", | ||
358 | ); | ||
359 | } | ||
360 | |||
361 | #[test] | ||
362 | fn test_join_lines_use_items_right() { | ||
363 | // No space after the '}' | ||
364 | check_join_lines( | ||
365 | r" | ||
366 | use ra_syntax::{ | ||
367 | <|> TextUnit, TextRange | ||
368 | };", | ||
369 | r" | ||
370 | use ra_syntax::{ | ||
371 | <|> TextUnit, TextRange};", | ||
372 | ); | ||
373 | } | ||
374 | |||
375 | #[test] | ||
376 | fn test_join_lines_use_items_right_comma() { | ||
377 | // No space after the '}' | ||
378 | check_join_lines( | ||
379 | r" | ||
380 | use ra_syntax::{ | ||
381 | <|> TextUnit, TextRange, | ||
382 | };", | ||
383 | r" | ||
384 | use ra_syntax::{ | ||
385 | <|> TextUnit, TextRange};", | ||
386 | ); | ||
387 | } | ||
388 | |||
389 | #[test] | ||
334 | fn test_join_lines_use_tree() { | 390 | fn test_join_lines_use_tree() { |
335 | check_join_lines( | 391 | check_join_lines( |
336 | r" | 392 | r" |