diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-02-11 17:38:05 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-02-11 17:38:05 +0000 |
commit | 00e672a51b0caec9e2bb7004281ce2f57bdbefe5 (patch) | |
tree | 5a3715910dbe92240f07bd1fc470f633ea4d267b /crates/ra_ide | |
parent | 305d9219823f5c0a6ffe526351b225b410702dfd (diff) | |
parent | f3dd0a05bb9135d4c3b70243d4dde15e02355910 (diff) |
Merge #3112
3112: Return early, return often r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide')
-rw-r--r-- | crates/ra_ide/src/join_lines.rs | 85 |
1 files changed, 57 insertions, 28 deletions
diff --git a/crates/ra_ide/src/join_lines.rs b/crates/ra_ide/src/join_lines.rs index 7deeb3494..01fb32b3d 100644 --- a/crates/ra_ide/src/join_lines.rs +++ b/crates/ra_ide/src/join_lines.rs | |||
@@ -60,36 +60,15 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU | |||
60 | return; | 60 | return; |
61 | } | 61 | } |
62 | 62 | ||
63 | // Special case that turns something like: | ||
64 | // | ||
65 | // ``` | ||
66 | // my_function({<|> | ||
67 | // <some-expr> | ||
68 | // }) | ||
69 | // ``` | ||
70 | // | ||
71 | // into `my_function(<some-expr>)` | ||
72 | if join_single_expr_block(edit, token).is_some() { | ||
73 | return; | ||
74 | } | ||
75 | // ditto for | ||
76 | // | ||
77 | // ``` | ||
78 | // use foo::{<|> | ||
79 | // bar | ||
80 | // }; | ||
81 | // ``` | ||
82 | if join_single_use_tree(edit, token).is_some() { | ||
83 | return; | ||
84 | } | ||
85 | |||
86 | // The node is between two other nodes | 63 | // The node is between two other nodes |
87 | let prev = token.prev_sibling_or_token().unwrap(); | 64 | let prev = token.prev_sibling_or_token().unwrap(); |
88 | let next = token.next_sibling_or_token().unwrap(); | 65 | let next = token.next_sibling_or_token().unwrap(); |
89 | if is_trailing_comma(prev.kind(), next.kind()) { | 66 | if is_trailing_comma(prev.kind(), next.kind()) { |
90 | // Removes: trailing comma, newline (incl. surrounding whitespace) | 67 | // Removes: trailing comma, newline (incl. surrounding whitespace) |
91 | edit.delete(TextRange::from_to(prev.text_range().start(), token.text_range().end())); | 68 | edit.delete(TextRange::from_to(prev.text_range().start(), token.text_range().end())); |
92 | } else if prev.kind() == T![,] && next.kind() == T!['}'] { | 69 | return; |
70 | } | ||
71 | if prev.kind() == T![,] && next.kind() == T!['}'] { | ||
93 | // Removes: comma, newline (incl. surrounding whitespace) | 72 | // Removes: comma, newline (incl. surrounding whitespace) |
94 | let space = if let Some(left) = prev.prev_sibling_or_token() { | 73 | let space = if let Some(left) = prev.prev_sibling_or_token() { |
95 | compute_ws(left.kind(), next.kind()) | 74 | compute_ws(left.kind(), next.kind()) |
@@ -100,7 +79,10 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU | |||
100 | TextRange::from_to(prev.text_range().start(), token.text_range().end()), | 79 | TextRange::from_to(prev.text_range().start(), token.text_range().end()), |
101 | space.to_string(), | 80 | space.to_string(), |
102 | ); | 81 | ); |
103 | } else if let (Some(_), Some(next)) = ( | 82 | return; |
83 | } | ||
84 | |||
85 | if let (Some(_), Some(next)) = ( | ||
104 | prev.as_token().cloned().and_then(ast::Comment::cast), | 86 | prev.as_token().cloned().and_then(ast::Comment::cast), |
105 | next.as_token().cloned().and_then(ast::Comment::cast), | 87 | next.as_token().cloned().and_then(ast::Comment::cast), |
106 | ) { | 88 | ) { |
@@ -109,10 +91,34 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU | |||
109 | token.text_range().start(), | 91 | token.text_range().start(), |
110 | next.syntax().text_range().start() + TextUnit::of_str(next.prefix()), | 92 | next.syntax().text_range().start() + TextUnit::of_str(next.prefix()), |
111 | )); | 93 | )); |
112 | } else { | 94 | return; |
113 | // Remove newline but add a computed amount of whitespace characters | 95 | } |
114 | edit.replace(token.text_range(), compute_ws(prev.kind(), next.kind()).to_string()); | 96 | |
97 | // Special case that turns something like: | ||
98 | // | ||
99 | // ``` | ||
100 | // my_function({<|> | ||
101 | // <some-expr> | ||
102 | // }) | ||
103 | // ``` | ||
104 | // | ||
105 | // into `my_function(<some-expr>)` | ||
106 | if join_single_expr_block(edit, token).is_some() { | ||
107 | return; | ||
115 | } | 108 | } |
109 | // ditto for | ||
110 | // | ||
111 | // ``` | ||
112 | // use foo::{<|> | ||
113 | // bar | ||
114 | // }; | ||
115 | // ``` | ||
116 | if join_single_use_tree(edit, token).is_some() { | ||
117 | return; | ||
118 | } | ||
119 | |||
120 | // Remove newline but add a computed amount of whitespace characters | ||
121 | edit.replace(token.text_range(), compute_ws(prev.kind(), next.kind()).to_string()); | ||
116 | } | 122 | } |
117 | 123 | ||
118 | fn has_comma_after(node: &SyntaxNode) -> bool { | 124 | fn has_comma_after(node: &SyntaxNode) -> bool { |
@@ -608,4 +614,27 @@ pub fn handle_find_matching_brace() { | |||
608 | }", | 614 | }", |
609 | ); | 615 | ); |
610 | } | 616 | } |
617 | |||
618 | #[test] | ||
619 | fn test_join_lines_commented_block() { | ||
620 | check_join_lines( | ||
621 | r" | ||
622 | fn main() { | ||
623 | let _ = { | ||
624 | // <|>foo | ||
625 | // bar | ||
626 | 92 | ||
627 | }; | ||
628 | } | ||
629 | ", | ||
630 | r" | ||
631 | fn main() { | ||
632 | let _ = { | ||
633 | // <|>foo bar | ||
634 | 92 | ||
635 | }; | ||
636 | } | ||
637 | ", | ||
638 | ) | ||
639 | } | ||
611 | } | 640 | } |