aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-02-11 17:36:12 +0000
committerAleksey Kladov <[email protected]>2020-02-11 17:36:12 +0000
commitf3dd0a05bb9135d4c3b70243d4dde15e02355910 (patch)
tree5a3715910dbe92240f07bd1fc470f633ea4d267b /crates
parentadfed5c6894602159ca76dd14d9ed26d76c1cc1b (diff)
Return early, return often
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide/src/join_lines.rs60
1 files changed, 33 insertions, 27 deletions
diff --git a/crates/ra_ide/src/join_lines.rs b/crates/ra_ide/src/join_lines.rs
index 552d7f66e..01fb32b3d 100644
--- a/crates/ra_ide/src/join_lines.rs
+++ b/crates/ra_ide/src/join_lines.rs
@@ -66,7 +66,9 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU
66 if is_trailing_comma(prev.kind(), next.kind()) { 66 if is_trailing_comma(prev.kind(), next.kind()) {
67 // Removes: trailing comma, newline (incl. surrounding whitespace) 67 // Removes: trailing comma, newline (incl. surrounding whitespace)
68 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()));
69 } else if prev.kind() == T![,] && next.kind() == T!['}'] { 69 return;
70 }
71 if prev.kind() == T![,] && next.kind() == T!['}'] {
70 // Removes: comma, newline (incl. surrounding whitespace) 72 // Removes: comma, newline (incl. surrounding whitespace)
71 let space = if let Some(left) = prev.prev_sibling_or_token() { 73 let space = if let Some(left) = prev.prev_sibling_or_token() {
72 compute_ws(left.kind(), next.kind()) 74 compute_ws(left.kind(), next.kind())
@@ -77,7 +79,10 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU
77 TextRange::from_to(prev.text_range().start(), token.text_range().end()), 79 TextRange::from_to(prev.text_range().start(), token.text_range().end()),
78 space.to_string(), 80 space.to_string(),
79 ); 81 );
80 } else if let (Some(_), Some(next)) = ( 82 return;
83 }
84
85 if let (Some(_), Some(next)) = (
81 prev.as_token().cloned().and_then(ast::Comment::cast), 86 prev.as_token().cloned().and_then(ast::Comment::cast),
82 next.as_token().cloned().and_then(ast::Comment::cast), 87 next.as_token().cloned().and_then(ast::Comment::cast),
83 ) { 88 ) {
@@ -86,33 +91,34 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU
86 token.text_range().start(), 91 token.text_range().start(),
87 next.syntax().text_range().start() + TextUnit::of_str(next.prefix()), 92 next.syntax().text_range().start() + TextUnit::of_str(next.prefix()),
88 )); 93 ));
89 } else { 94 return;
90 // Special case that turns something like: 95 }
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 }
112 96
113 // Remove newline but add a computed amount of whitespace characters 97 // Special case that turns something like:
114 edit.replace(token.text_range(), compute_ws(prev.kind(), next.kind()).to_string()); 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
118fn has_comma_after(node: &SyntaxNode) -> bool { 124fn has_comma_after(node: &SyntaxNode) -> bool {