aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/handlers/merge_imports.rs13
-rw-r--r--crates/ra_assists/src/handlers/merge_match_arms.rs10
-rw-r--r--crates/ra_syntax/src/algo.rs4
3 files changed, 14 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 @@
1use std::iter::successors; 1use std::iter::successors;
2 2
3use ast::{edit::AstNodeEdit, make}; 3use ra_syntax::{
4use 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
6use crate::{Assist, AssistCtx, AssistId}; 9use 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
52fn 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
56fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option<ast::UseTree> { 55fn 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 @@
1use std::iter::successors; 1use std::iter::successors;
2 2
3use ra_syntax::{ 3use 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
105fn 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)]
110mod tests { 107mod 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(
diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs
index b87c7084e..344cf0fbe 100644
--- a/crates/ra_syntax/src/algo.rs
+++ b/crates/ra_syntax/src/algo.rs
@@ -73,6 +73,10 @@ pub fn least_common_ancestor(u: &SyntaxNode, v: &SyntaxNode) -> Option<SyntaxNod
73 v.ancestors().find(|it| u_ancestors.contains(it)) 73 v.ancestors().find(|it| u_ancestors.contains(it))
74} 74}
75 75
76pub fn neighbor<T: AstNode>(me: &T, direction: Direction) -> Option<T> {
77 me.syntax().siblings(direction).skip(1).find_map(T::cast)
78}
79
76#[derive(Debug, PartialEq, Eq, Clone, Copy)] 80#[derive(Debug, PartialEq, Eq, Clone, Copy)]
77pub enum InsertPosition<T> { 81pub enum InsertPosition<T> {
78 First, 82 First,