Skip to content

Commit 00e672a

Browse files
bors[bot]matklad
andauthored
Merge #3112
3112: Return early, return often r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 305d921 + f3dd0a0 commit 00e672a

File tree

1 file changed

+57
-28
lines changed

1 file changed

+57
-28
lines changed

crates/ra_ide/src/join_lines.rs

+57-28
Original file line numberDiff line numberDiff line change
@@ -60,36 +60,15 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU
6060
return;
6161
}
6262

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-
8663
// The node is between two other nodes
8764
let prev = token.prev_sibling_or_token().unwrap();
8865
let next = token.next_sibling_or_token().unwrap();
8966
if is_trailing_comma(prev.kind(), next.kind()) {
9067
// Removes: trailing comma, newline (incl. surrounding whitespace)
9168
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!['}'] {
9372
// Removes: comma, newline (incl. surrounding whitespace)
9473
let space = if let Some(left) = prev.prev_sibling_or_token() {
9574
compute_ws(left.kind(), next.kind())
@@ -100,7 +79,10 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU
10079
TextRange::from_to(prev.text_range().start(), token.text_range().end()),
10180
space.to_string(),
10281
);
103-
} else if let (Some(_), Some(next)) = (
82+
return;
83+
}
84+
85+
if let (Some(_), Some(next)) = (
10486
prev.as_token().cloned().and_then(ast::Comment::cast),
10587
next.as_token().cloned().and_then(ast::Comment::cast),
10688
) {
@@ -109,10 +91,34 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU
10991
token.text_range().start(),
11092
next.syntax().text_range().start() + TextUnit::of_str(next.prefix()),
11193
));
112-
} else {
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());
94+
return;
95+
}
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;
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 {
@@ -608,4 +614,27 @@ pub fn handle_find_matching_brace() {
608614
}",
609615
);
610616
}
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+
}
611640
}

0 commit comments

Comments
 (0)