From ef3bf906c477048076faa8e18b50fc34175f6c14 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 19 Mar 2020 11:38:26 +0100 Subject: Generalize --- crates/ra_assists/src/handlers/merge_imports.rs | 13 ++++++------- crates/ra_assists/src/handlers/merge_match_arms.rs | 10 ++++------ 2 files changed, 10 insertions(+), 13 deletions(-) (limited to 'crates/ra_assists/src/handlers') 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 @@ use std::iter::successors; -use ast::{edit::AstNodeEdit, make}; -use ra_syntax::{ast, AstNode, AstToken, Direction, InsertPosition, SyntaxElement, T}; +use ra_syntax::{ + algo::neighbor, + ast::{self, edit::AstNodeEdit, make}, + AstNode, AstToken, Direction, InsertPosition, SyntaxElement, T, +}; use crate::{Assist, AssistCtx, AssistId}; @@ -23,7 +26,7 @@ pub(crate) fn merge_imports(ctx: AssistCtx) -> Option { let (merged, to_delete) = [Direction::Prev, Direction::Next] .iter() .copied() - .filter_map(|dir| next_use_item(&use_item, dir)) + .filter_map(|dir| neighbor(&use_item, dir)) .filter_map(|it| Some((it.clone(), it.use_tree()?))) .find_map(|(use_item, use_tree)| { Some((try_merge_trees(&tree, &use_tree)?, use_item.clone())) @@ -49,10 +52,6 @@ pub(crate) fn merge_imports(ctx: AssistCtx) -> Option { }) } -fn next_use_item(this_use_item: &ast::UseItem, direction: Direction) -> Option { - this_use_item.syntax().siblings(direction).skip(1).find_map(ast::UseItem::cast) -} - fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option { let lhs_path = old.path()?; 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 @@ use std::iter::successors; use ra_syntax::{ + algo::neighbor, ast::{self, AstNode}, Direction, TextUnit, }; @@ -53,7 +54,7 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option { // We check if the following match arms match this one. We could, but don't, // compare to the previous match arm as well. - let arms_to_merge = successors(Some(current_arm), next_arm) + let arms_to_merge = successors(Some(current_arm), |it| neighbor(it, Direction::Next)) .take_while(|arm| { if arm.guard().is_some() { return false; @@ -102,15 +103,12 @@ fn contains_placeholder(a: &ast::MatchArm) -> bool { } } -fn next_arm(arm: &ast::MatchArm) -> Option { - arm.syntax().siblings(Direction::Next).skip(1).find_map(ast::MatchArm::cast) -} - #[cfg(test)] mod tests { - use super::merge_match_arms; use crate::helpers::{check_assist, check_assist_not_applicable}; + use super::*; + #[test] fn merge_match_arms_single_patterns() { check_assist( -- cgit v1.2.3