Skip to content

Commit f3dd0a0

Browse files
committed
Return early, return often
1 parent adfed5c commit f3dd0a0

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

crates/ra_ide/src/join_lines.rs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU
6666
if is_trailing_comma(prev.kind(), next.kind()) {
6767
// Removes: trailing comma, newline (incl. surrounding whitespace)
6868
edit.delete(TextRange::from_to(prev.text_range().start(), token.text_range().end()));
69-
} else if prev.kind() == T![,] && next.kind() == T!['}'] {
69+
return;
70+
}
71+
if prev.kind() == T![,] && next.kind() == T!['}'] {
7072
// Removes: comma, newline (incl. surrounding whitespace)
7173
let space = if let Some(left) = prev.prev_sibling_or_token() {
7274
compute_ws(left.kind(), next.kind())
@@ -77,7 +79,10 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU
7779
TextRange::from_to(prev.text_range().start(), token.text_range().end()),
7880
space.to_string(),
7981
);
80-
} else if let (Some(_), Some(next)) = (
82+
return;
83+
}
84+
85+
if let (Some(_), Some(next)) = (
8186
prev.as_token().cloned().and_then(ast::Comment::cast),
8287
next.as_token().cloned().and_then(ast::Comment::cast),
8388
) {
@@ -86,33 +91,34 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU
8691
token.text_range().start(),
8792
next.syntax().text_range().start() + TextUnit::of_str(next.prefix()),
8893
));
89-
} else {
90-
// Special case that turns something like:
91-
//
92-
// ```
93-
// my_function({<|>
94-
// <some-expr>
95-
// })
96-
// ```
97-
//
98-
// into `my_function(<some-expr>)`
99-
if join_single_expr_block(edit, token).is_some() {
100-
return;
101-
}
102-
// ditto for
103-
//
104-
// ```
105-
// use foo::{<|>
106-
// bar
107-
// };
108-
// ```
109-
if join_single_use_tree(edit, token).is_some() {
110-
return;
111-
}
94+
return;
95+
}
11296

113-
// Remove newline but add a computed amount of whitespace characters
114-
edit.replace(token.text_range(), compute_ws(prev.kind(), next.kind()).to_string());
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;
115108
}
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());
116122
}
117123

118124
fn has_comma_after(node: &SyntaxNode) -> bool {

0 commit comments

Comments
 (0)