Merge #3650
3650: Generalize r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
11f2c6908b
@ -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<Assist> {
|
||||
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<Assist> {
|
||||
})
|
||||
}
|
||||
|
||||
fn next_use_item(this_use_item: &ast::UseItem, direction: Direction) -> Option<ast::UseItem> {
|
||||
this_use_item.syntax().siblings(direction).skip(1).find_map(ast::UseItem::cast)
|
||||
}
|
||||
|
||||
fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option<ast::UseTree> {
|
||||
let lhs_path = old.path()?;
|
||||
let rhs_path = new.path()?;
|
||||
|
@ -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<Assist> {
|
||||
|
||||
// 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<ast::MatchArm> {
|
||||
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(
|
||||
|
@ -73,6 +73,10 @@ pub fn least_common_ancestor(u: &SyntaxNode, v: &SyntaxNode) -> Option<SyntaxNod
|
||||
v.ancestors().find(|it| u_ancestors.contains(it))
|
||||
}
|
||||
|
||||
pub fn neighbor<T: AstNode>(me: &T, direction: Direction) -> Option<T> {
|
||||
me.syntax().siblings(direction).skip(1).find_map(T::cast)
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
pub enum InsertPosition<T> {
|
||||
First,
|
||||
|
Loading…
x
Reference in New Issue
Block a user