aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api_light/src/join_lines.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api_light/src/join_lines.rs')
-rw-r--r--crates/ra_ide_api_light/src/join_lines.rs54
1 files changed, 6 insertions, 48 deletions
diff --git a/crates/ra_ide_api_light/src/join_lines.rs b/crates/ra_ide_api_light/src/join_lines.rs
index bda6be878..ab7c5b4b5 100644
--- a/crates/ra_ide_api_light/src/join_lines.rs
+++ b/crates/ra_ide_api_light/src/join_lines.rs
@@ -1,14 +1,15 @@
1use std::mem;
2
3use itertools::Itertools; 1use itertools::Itertools;
4use ra_syntax::{ 2use ra_syntax::{
5 SourceFile, TextRange, TextUnit, AstNode, SyntaxNode, 3 SourceFile, TextRange, TextUnit, AstNode, SyntaxNode,
6 SyntaxKind::{self, WHITESPACE, COMMA, L_CURLY, R_CURLY, L_PAREN, R_PAREN, L_BRACK, R_BRACK, USE_TREE, DOT}, 4 SyntaxKind::{self, WHITESPACE, COMMA, R_CURLY, R_PAREN, R_BRACK},
7 algo::find_covering_node, 5 algo::find_covering_node,
8 ast, 6 ast,
9}; 7};
10 8
11use crate::{LocalEdit, TextEditBuilder}; 9use crate::{
10 LocalEdit, TextEditBuilder,
11 formatting::{compute_ws, extract_trivial_expression},
12};
12 13
13pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit { 14pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit {
14 let range = if range.is_empty() { 15 let range = if range.is_empty() {
@@ -132,7 +133,7 @@ fn remove_newline(
132fn join_single_expr_block(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> { 133fn join_single_expr_block(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> {
133 let block = ast::Block::cast(node.parent()?)?; 134 let block = ast::Block::cast(node.parent()?)?;
134 let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?; 135 let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?;
135 let expr = single_expr(block)?; 136 let expr = extract_trivial_expression(block)?;
136 edit.replace( 137 edit.replace(
137 block_expr.syntax().range(), 138 block_expr.syntax().range(),
138 expr.syntax().text().to_string(), 139 expr.syntax().text().to_string(),
@@ -140,26 +141,6 @@ fn join_single_expr_block(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Opti
140 Some(()) 141 Some(())
141} 142}
142 143
143fn single_expr(block: &ast::Block) -> Option<&ast::Expr> {
144 let mut res = None;
145 for child in block.syntax().children() {
146 if let Some(expr) = ast::Expr::cast(child) {
147 if expr.syntax().text().contains('\n') {
148 return None;
149 }
150 if mem::replace(&mut res, Some(expr)).is_some() {
151 return None;
152 }
153 } else {
154 match child.kind() {
155 WHITESPACE | L_CURLY | R_CURLY => (),
156 _ => return None,
157 }
158 }
159 }
160 res
161}
162
163fn join_single_use_tree(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> { 144fn join_single_use_tree(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> {
164 let use_tree_list = ast::UseTreeList::cast(node.parent()?)?; 145 let use_tree_list = ast::UseTreeList::cast(node.parent()?)?;
165 let (tree,) = use_tree_list.use_trees().collect_tuple()?; 146 let (tree,) = use_tree_list.use_trees().collect_tuple()?;
@@ -177,29 +158,6 @@ fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
177 } 158 }
178} 159}
179 160
180fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str {
181 match left.kind() {
182 L_PAREN | L_BRACK => return "",
183 L_CURLY => {
184 if let USE_TREE = right.kind() {
185 return "";
186 }
187 }
188 _ => (),
189 }
190 match right.kind() {
191 R_PAREN | R_BRACK => return "",
192 R_CURLY => {
193 if let USE_TREE = left.kind() {
194 return "";
195 }
196 }
197 DOT => return "",
198 _ => (),
199 }
200 " "
201}
202
203#[cfg(test)] 161#[cfg(test)]
204mod tests { 162mod tests {
205 use crate::test_utils::{assert_eq_text, check_action, extract_range}; 163 use crate::test_utils::{assert_eq_text, check_action, extract_range};