Return early, return often

This commit is contained in:
Aleksey Kladov 2020-02-11 18:36:12 +01:00
parent adfed5c689
commit f3dd0a05bb

View file

@ -66,7 +66,9 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU
if is_trailing_comma(prev.kind(), next.kind()) { if is_trailing_comma(prev.kind(), next.kind()) {
// Removes: trailing comma, newline (incl. surrounding whitespace) // Removes: trailing comma, newline (incl. surrounding whitespace)
edit.delete(TextRange::from_to(prev.text_range().start(), token.text_range().end())); edit.delete(TextRange::from_to(prev.text_range().start(), token.text_range().end()));
} else if prev.kind() == T![,] && next.kind() == T!['}'] { return;
}
if prev.kind() == T![,] && next.kind() == T!['}'] {
// Removes: comma, newline (incl. surrounding whitespace) // Removes: comma, newline (incl. surrounding whitespace)
let space = if let Some(left) = prev.prev_sibling_or_token() { let space = if let Some(left) = prev.prev_sibling_or_token() {
compute_ws(left.kind(), next.kind()) compute_ws(left.kind(), next.kind())
@ -77,7 +79,10 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU
TextRange::from_to(prev.text_range().start(), token.text_range().end()), TextRange::from_to(prev.text_range().start(), token.text_range().end()),
space.to_string(), space.to_string(),
); );
} else if let (Some(_), Some(next)) = ( return;
}
if let (Some(_), Some(next)) = (
prev.as_token().cloned().and_then(ast::Comment::cast), prev.as_token().cloned().and_then(ast::Comment::cast),
next.as_token().cloned().and_then(ast::Comment::cast), next.as_token().cloned().and_then(ast::Comment::cast),
) { ) {
@ -86,7 +91,9 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU
token.text_range().start(), token.text_range().start(),
next.syntax().text_range().start() + TextUnit::of_str(next.prefix()), next.syntax().text_range().start() + TextUnit::of_str(next.prefix()),
)); ));
} else { return;
}
// Special case that turns something like: // Special case that turns something like:
// //
// ``` // ```
@ -112,7 +119,6 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU
// Remove newline but add a computed amount of whitespace characters // Remove newline but add a computed amount of whitespace characters
edit.replace(token.text_range(), compute_ws(prev.kind(), next.kind()).to_string()); edit.replace(token.text_range(), compute_ws(prev.kind(), next.kind()).to_string());
}
} }
fn has_comma_after(node: &SyntaxNode) -> bool { fn has_comma_after(node: &SyntaxNode) -> bool {