mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Generalize
This commit is contained in:
parent
5b0a7c7d16
commit
ef3bf906c4
3 changed files with 14 additions and 13 deletions
|
@ -1,7 +1,10 @@
|
||||||
use std::iter::successors;
|
use std::iter::successors;
|
||||||
|
|
||||||
use ast::{edit::AstNodeEdit, make};
|
use ra_syntax::{
|
||||||
use ra_syntax::{ast, AstNode, AstToken, Direction, InsertPosition, SyntaxElement, T};
|
algo::neighbor,
|
||||||
|
ast::{self, edit::AstNodeEdit, make},
|
||||||
|
AstNode, AstToken, Direction, InsertPosition, SyntaxElement, T,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{Assist, AssistCtx, AssistId};
|
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]
|
let (merged, to_delete) = [Direction::Prev, Direction::Next]
|
||||||
.iter()
|
.iter()
|
||||||
.copied()
|
.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()?)))
|
.filter_map(|it| Some((it.clone(), it.use_tree()?)))
|
||||||
.find_map(|(use_item, use_tree)| {
|
.find_map(|(use_item, use_tree)| {
|
||||||
Some((try_merge_trees(&tree, &use_tree)?, use_item.clone()))
|
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> {
|
fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option<ast::UseTree> {
|
||||||
let lhs_path = old.path()?;
|
let lhs_path = old.path()?;
|
||||||
let rhs_path = new.path()?;
|
let rhs_path = new.path()?;
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use std::iter::successors;
|
use std::iter::successors;
|
||||||
|
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
|
algo::neighbor,
|
||||||
ast::{self, AstNode},
|
ast::{self, AstNode},
|
||||||
Direction, TextUnit,
|
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,
|
// We check if the following match arms match this one. We could, but don't,
|
||||||
// compare to the previous match arm as well.
|
// 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| {
|
.take_while(|arm| {
|
||||||
if arm.guard().is_some() {
|
if arm.guard().is_some() {
|
||||||
return false;
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::merge_match_arms;
|
|
||||||
use crate::helpers::{check_assist, check_assist_not_applicable};
|
use crate::helpers::{check_assist, check_assist_not_applicable};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn merge_match_arms_single_patterns() {
|
fn merge_match_arms_single_patterns() {
|
||||||
check_assist(
|
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))
|
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)]
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
pub enum InsertPosition<T> {
|
pub enum InsertPosition<T> {
|
||||||
First,
|
First,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue