aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api_light/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-10 15:32:02 +0000
committerAleksey Kladov <[email protected]>2019-01-10 15:32:02 +0000
commita0c978cd0ce95446f6d3e6a13047474670d9ee55 (patch)
tree9a1d8f53d0befa10e5cd0adba180ac2ebd534800 /crates/ra_ide_api_light/src
parent3ca76c203921215cc9af68042ed8fbf4f50b5969 (diff)
fix code duplication
Diffstat (limited to 'crates/ra_ide_api_light/src')
-rw-r--r--crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs26
-rw-r--r--crates/ra_ide_api_light/src/formatting.rs42
-rw-r--r--crates/ra_ide_api_light/src/join_lines.rs54
-rw-r--r--crates/ra_ide_api_light/src/lib.rs1
4 files changed, 55 insertions, 68 deletions
diff --git a/crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs b/crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs
index 30c371480..d64c34d54 100644
--- a/crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs
+++ b/crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs
@@ -1,9 +1,9 @@
1use ra_syntax::{ 1use ra_syntax::{AstNode, ast};
2 AstNode, SyntaxKind::{L_CURLY, R_CURLY, WHITESPACE},
3 ast,
4};
5 2
6use crate::assists::{AssistCtx, Assist}; 3use crate::{
4 assists::{AssistCtx, Assist},
5 formatting::extract_trivial_expression,
6};
7 7
8pub fn replace_if_let_with_match(ctx: AssistCtx) -> Option<Assist> { 8pub fn replace_if_let_with_match(ctx: AssistCtx) -> Option<Assist> {
9 let if_expr: &ast::IfExpr = ctx.node_at_offset()?; 9 let if_expr: &ast::IfExpr = ctx.node_at_offset()?;
@@ -39,26 +39,12 @@ fn build_match_expr(
39} 39}
40 40
41fn format_arm(block: &ast::Block) -> String { 41fn format_arm(block: &ast::Block) -> String {
42 match extract_expression(block) { 42 match extract_trivial_expression(block) {
43 None => block.syntax().text().to_string(), 43 None => block.syntax().text().to_string(),
44 Some(e) => format!("{},", e.syntax().text()), 44 Some(e) => format!("{},", e.syntax().text()),
45 } 45 }
46} 46}
47 47
48fn extract_expression(block: &ast::Block) -> Option<&ast::Expr> {
49 let expr = block.expr()?;
50 let non_trivial_children = block.syntax().children().filter(|it| {
51 !(it == &expr.syntax()
52 || it.kind() == L_CURLY
53 || it.kind() == R_CURLY
54 || it.kind() == WHITESPACE)
55 });
56 if non_trivial_children.count() > 0 {
57 return None;
58 }
59 Some(expr)
60}
61
62#[cfg(test)] 48#[cfg(test)]
63mod tests { 49mod tests {
64 use super::*; 50 use super::*;
diff --git a/crates/ra_ide_api_light/src/formatting.rs b/crates/ra_ide_api_light/src/formatting.rs
new file mode 100644
index 000000000..1f3769209
--- /dev/null
+++ b/crates/ra_ide_api_light/src/formatting.rs
@@ -0,0 +1,42 @@
1use ra_syntax::{
2 ast, AstNode,
3 SyntaxNode, SyntaxKind::*,
4};
5
6pub(crate) fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
7 let expr = block.expr()?;
8 if expr.syntax().text().contains('\n') {
9 return None;
10 }
11 let non_trivial_children = block.syntax().children().filter(|it| match it.kind() {
12 WHITESPACE | L_CURLY | R_CURLY => false,
13 _ => it != &expr.syntax(),
14 });
15 if non_trivial_children.count() > 0 {
16 return None;
17 }
18 Some(expr)
19}
20
21pub(crate) fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str {
22 match left.kind() {
23 L_PAREN | L_BRACK => return "",
24 L_CURLY => {
25 if let USE_TREE = right.kind() {
26 return "";
27 }
28 }
29 _ => (),
30 }
31 match right.kind() {
32 R_PAREN | R_BRACK => return "",
33 R_CURLY => {
34 if let USE_TREE = left.kind() {
35 return "";
36 }
37 }
38 DOT => return "",
39 _ => (),
40 }
41 " "
42}
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};
diff --git a/crates/ra_ide_api_light/src/lib.rs b/crates/ra_ide_api_light/src/lib.rs
index e632108ce..bc9bee752 100644
--- a/crates/ra_ide_api_light/src/lib.rs
+++ b/crates/ra_ide_api_light/src/lib.rs
@@ -14,6 +14,7 @@ mod test_utils;
14mod join_lines; 14mod join_lines;
15mod typing; 15mod typing;
16mod diagnostics; 16mod diagnostics;
17pub(crate) mod formatting;
17 18
18pub use self::{ 19pub use self::{
19 assists::LocalEdit, 20 assists::LocalEdit,