Skip to content

Commit 11f2c69

Browse files
bors[bot]matklad
andauthored
Merge #3650
3650: Generalize r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 13af63a + ef3bf90 commit 11f2c69

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

crates/ra_assists/src/handlers/merge_imports.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use std::iter::successors;
22

3-
use ast::{edit::AstNodeEdit, make};
4-
use ra_syntax::{ast, AstNode, AstToken, Direction, InsertPosition, SyntaxElement, T};
3+
use ra_syntax::{
4+
algo::neighbor,
5+
ast::{self, edit::AstNodeEdit, make},
6+
AstNode, AstToken, Direction, InsertPosition, SyntaxElement, T,
7+
};
58

69
use crate::{Assist, AssistCtx, AssistId};
710

@@ -23,7 +26,7 @@ pub(crate) fn merge_imports(ctx: AssistCtx) -> Option<Assist> {
2326
let (merged, to_delete) = [Direction::Prev, Direction::Next]
2427
.iter()
2528
.copied()
26-
.filter_map(|dir| next_use_item(&use_item, dir))
29+
.filter_map(|dir| neighbor(&use_item, dir))
2730
.filter_map(|it| Some((it.clone(), it.use_tree()?)))
2831
.find_map(|(use_item, use_tree)| {
2932
Some((try_merge_trees(&tree, &use_tree)?, use_item.clone()))
@@ -49,10 +52,6 @@ pub(crate) fn merge_imports(ctx: AssistCtx) -> Option<Assist> {
4952
})
5053
}
5154

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-
5655
fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option<ast::UseTree> {
5756
let lhs_path = old.path()?;
5857
let rhs_path = new.path()?;

crates/ra_assists/src/handlers/merge_match_arms.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::iter::successors;
22

33
use ra_syntax::{
4+
algo::neighbor,
45
ast::{self, AstNode},
56
Direction, TextUnit,
67
};
@@ -53,7 +54,7 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option<Assist> {
5354

5455
// We check if the following match arms match this one. We could, but don't,
5556
// 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))
5758
.take_while(|arm| {
5859
if arm.guard().is_some() {
5960
return false;
@@ -102,15 +103,12 @@ fn contains_placeholder(a: &ast::MatchArm) -> bool {
102103
}
103104
}
104105

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-
109106
#[cfg(test)]
110107
mod tests {
111-
use super::merge_match_arms;
112108
use crate::helpers::{check_assist, check_assist_not_applicable};
113109

110+
use super::*;
111+
114112
#[test]
115113
fn merge_match_arms_single_patterns() {
116114
check_assist(

crates/ra_syntax/src/algo.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ pub fn least_common_ancestor(u: &SyntaxNode, v: &SyntaxNode) -> Option<SyntaxNod
7373
v.ancestors().find(|it| u_ancestors.contains(it))
7474
}
7575

76+
pub fn neighbor<T: AstNode>(me: &T, direction: Direction) -> Option<T> {
77+
me.syntax().siblings(direction).skip(1).find_map(T::cast)
78+
}
79+
7680
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
7781
pub enum InsertPosition<T> {
7882
First,

0 commit comments

Comments
 (0)