diff options
author | Aleksey Kladov <[email protected]> | 2020-03-19 10:38:26 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-03-19 10:38:26 +0000 |
commit | ef3bf906c477048076faa8e18b50fc34175f6c14 (patch) | |
tree | 7e0134f8a0bc0a41e8cbcea91231b7a7be95262b /crates/ra_assists | |
parent | 5b0a7c7d162c6c30b8fd52309f4473659d647452 (diff) |
Generalize
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/handlers/merge_imports.rs | 13 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/merge_match_arms.rs | 10 |
2 files changed, 10 insertions, 13 deletions
diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs index 96b1ab86a..e9c49b7d0 100644 --- a/crates/ra_assists/src/handlers/merge_imports.rs +++ b/crates/ra_assists/src/handlers/merge_imports.rs | |||
@@ -1,7 +1,10 @@ | |||
1 | use std::iter::successors; | 1 | use std::iter::successors; |
2 | 2 | ||
3 | use ast::{edit::AstNodeEdit, make}; | 3 | use ra_syntax::{ |
4 | use ra_syntax::{ast, AstNode, AstToken, Direction, InsertPosition, SyntaxElement, T}; | 4 | algo::neighbor, |
5 | ast::{self, edit::AstNodeEdit, make}, | ||
6 | AstNode, AstToken, Direction, InsertPosition, SyntaxElement, T, | ||
7 | }; | ||
5 | 8 | ||
6 | use crate::{Assist, AssistCtx, AssistId}; | 9 | use crate::{Assist, AssistCtx, AssistId}; |
7 | 10 | ||
@@ -23,7 +26,7 @@ pub(crate) fn merge_imports(ctx: AssistCtx) -> Option<Assist> { | |||
23 | let (merged, to_delete) = [Direction::Prev, Direction::Next] | 26 | let (merged, to_delete) = [Direction::Prev, Direction::Next] |
24 | .iter() | 27 | .iter() |
25 | .copied() | 28 | .copied() |
26 | .filter_map(|dir| next_use_item(&use_item, dir)) | 29 | .filter_map(|dir| neighbor(&use_item, dir)) |
27 | .filter_map(|it| Some((it.clone(), it.use_tree()?))) | 30 | .filter_map(|it| Some((it.clone(), it.use_tree()?))) |
28 | .find_map(|(use_item, use_tree)| { | 31 | .find_map(|(use_item, use_tree)| { |
29 | Some((try_merge_trees(&tree, &use_tree)?, use_item.clone())) | 32 | Some((try_merge_trees(&tree, &use_tree)?, use_item.clone())) |
@@ -49,10 +52,6 @@ pub(crate) fn merge_imports(ctx: AssistCtx) -> Option<Assist> { | |||
49 | }) | 52 | }) |
50 | } | 53 | } |
51 | 54 | ||
52 | fn next_use_item(this_use_item: &ast::UseItem, direction: Direction) -> Option<ast::UseItem> { | ||
53 | this_use_item.syntax().siblings(direction).skip(1).find_map(ast::UseItem::cast) | ||
54 | } | ||
55 | |||
56 | fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option<ast::UseTree> { | 55 | fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option<ast::UseTree> { |
57 | let lhs_path = old.path()?; | 56 | let lhs_path = old.path()?; |
58 | let rhs_path = new.path()?; | 57 | let rhs_path = new.path()?; |
diff --git a/crates/ra_assists/src/handlers/merge_match_arms.rs b/crates/ra_assists/src/handlers/merge_match_arms.rs index b2a194cb5..eb967ab92 100644 --- a/crates/ra_assists/src/handlers/merge_match_arms.rs +++ b/crates/ra_assists/src/handlers/merge_match_arms.rs | |||
@@ -1,6 +1,7 @@ | |||
1 | use std::iter::successors; | 1 | use std::iter::successors; |
2 | 2 | ||
3 | use ra_syntax::{ | 3 | use ra_syntax::{ |
4 | algo::neighbor, | ||
4 | ast::{self, AstNode}, | 5 | ast::{self, AstNode}, |
5 | Direction, TextUnit, | 6 | Direction, TextUnit, |
6 | }; | 7 | }; |
@@ -53,7 +54,7 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option<Assist> { | |||
53 | 54 | ||
54 | // We check if the following match arms match this one. We could, but don't, | 55 | // We check if the following match arms match this one. We could, but don't, |
55 | // compare to the previous match arm as well. | 56 | // compare to the previous match arm as well. |
56 | let arms_to_merge = successors(Some(current_arm), next_arm) | 57 | let arms_to_merge = successors(Some(current_arm), |it| neighbor(it, Direction::Next)) |
57 | .take_while(|arm| { | 58 | .take_while(|arm| { |
58 | if arm.guard().is_some() { | 59 | if arm.guard().is_some() { |
59 | return false; | 60 | return false; |
@@ -102,15 +103,12 @@ fn contains_placeholder(a: &ast::MatchArm) -> bool { | |||
102 | } | 103 | } |
103 | } | 104 | } |
104 | 105 | ||
105 | fn next_arm(arm: &ast::MatchArm) -> Option<ast::MatchArm> { | ||
106 | arm.syntax().siblings(Direction::Next).skip(1).find_map(ast::MatchArm::cast) | ||
107 | } | ||
108 | |||
109 | #[cfg(test)] | 106 | #[cfg(test)] |
110 | mod tests { | 107 | mod tests { |
111 | use super::merge_match_arms; | ||
112 | use crate::helpers::{check_assist, check_assist_not_applicable}; | 108 | use crate::helpers::{check_assist, check_assist_not_applicable}; |
113 | 109 | ||
110 | use super::*; | ||
111 | |||
114 | #[test] | 112 | #[test] |
115 | fn merge_match_arms_single_patterns() { | 113 | fn merge_match_arms_single_patterns() { |
116 | check_assist( | 114 | check_assist( |