Skip to content

Commit a0c978c

Browse files
committed
fix code duplication
1 parent 3ca76c2 commit a0c978c

File tree

4 files changed

+55
-68
lines changed

4 files changed

+55
-68
lines changed

crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use ra_syntax::{
2-
AstNode, SyntaxKind::{L_CURLY, R_CURLY, WHITESPACE},
3-
ast,
4-
};
1+
use ra_syntax::{AstNode, ast};
52

6-
use crate::assists::{AssistCtx, Assist};
3+
use crate::{
4+
assists::{AssistCtx, Assist},
5+
formatting::extract_trivial_expression,
6+
};
77

88
pub fn replace_if_let_with_match(ctx: AssistCtx) -> Option<Assist> {
99
let if_expr: &ast::IfExpr = ctx.node_at_offset()?;
@@ -39,26 +39,12 @@ fn build_match_expr(
3939
}
4040

4141
fn format_arm(block: &ast::Block) -> String {
42-
match extract_expression(block) {
42+
match extract_trivial_expression(block) {
4343
None => block.syntax().text().to_string(),
4444
Some(e) => format!("{},", e.syntax().text()),
4545
}
4646
}
4747

48-
fn 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-
6248
#[cfg(test)]
6349
mod tests {
6450
use super::*;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use ra_syntax::{
2+
ast, AstNode,
3+
SyntaxNode, SyntaxKind::*,
4+
};
5+
6+
pub(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+
21+
pub(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+
}

crates/ra_ide_api_light/src/join_lines.rs

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
use std::mem;
2-
31
use itertools::Itertools;
42
use ra_syntax::{
53
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},
75
algo::find_covering_node,
86
ast,
97
};
108

11-
use crate::{LocalEdit, TextEditBuilder};
9+
use crate::{
10+
LocalEdit, TextEditBuilder,
11+
formatting::{compute_ws, extract_trivial_expression},
12+
};
1213

1314
pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit {
1415
let range = if range.is_empty() {
@@ -132,34 +133,14 @@ fn remove_newline(
132133
fn join_single_expr_block(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> {
133134
let block = ast::Block::cast(node.parent()?)?;
134135
let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?;
135-
let expr = single_expr(block)?;
136+
let expr = extract_trivial_expression(block)?;
136137
edit.replace(
137138
block_expr.syntax().range(),
138139
expr.syntax().text().to_string(),
139140
);
140141
Some(())
141142
}
142143

143-
fn 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-
163144
fn join_single_use_tree(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> {
164145
let use_tree_list = ast::UseTreeList::cast(node.parent()?)?;
165146
let (tree,) = use_tree_list.use_trees().collect_tuple()?;
@@ -177,29 +158,6 @@ fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
177158
}
178159
}
179160

180-
fn 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-
203161
#[cfg(test)]
204162
mod tests {
205163
use crate::test_utils::{assert_eq_text, check_action, extract_range};

crates/ra_ide_api_light/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod test_utils;
1414
mod join_lines;
1515
mod typing;
1616
mod diagnostics;
17+
pub(crate) mod formatting;
1718

1819
pub use self::{
1920
assists::LocalEdit,

0 commit comments

Comments
 (0)