From 3f6dc20d3cf3fa101552a9067b98a1314260a679 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 18 Mar 2020 16:41:24 +0100 Subject: Merge imports assist Work towards #2220 --- crates/ra_assists/src/handlers/split_import.rs | 29 ++++++-------------------- 1 file changed, 6 insertions(+), 23 deletions(-) (limited to 'crates/ra_assists/src/handlers/split_import.rs') diff --git a/crates/ra_assists/src/handlers/split_import.rs b/crates/ra_assists/src/handlers/split_import.rs index 292c39f59..d9244f22d 100644 --- a/crates/ra_assists/src/handlers/split_import.rs +++ b/crates/ra_assists/src/handlers/split_import.rs @@ -1,9 +1,6 @@ -use std::iter::{once, successors}; +use std::iter::successors; -use ra_syntax::{ - ast::{self, make}, - AstNode, T, -}; +use ra_syntax::{ast, AstNode, T}; use crate::{Assist, AssistCtx, AssistId}; @@ -25,7 +22,10 @@ pub(crate) fn split_import(ctx: AssistCtx) -> Option { let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast)?; - let new_tree = split_use_tree_prefix(&use_tree, &path)?; + let new_tree = use_tree.split_prefix(&path); + if new_tree == use_tree { + return None; + } let cursor = ctx.frange.range.start(); ctx.add_assist(AssistId("split_import"), "Split import", |edit| { @@ -35,23 +35,6 @@ pub(crate) fn split_import(ctx: AssistCtx) -> Option { }) } -fn split_use_tree_prefix(use_tree: &ast::UseTree, prefix: &ast::Path) -> Option { - let suffix = split_path_prefix(&prefix)?; - let use_tree = make::use_tree(suffix.clone(), use_tree.use_tree_list(), use_tree.alias()); - let nested = make::use_tree_list(once(use_tree)); - let res = make::use_tree(prefix.clone(), Some(nested), None); - Some(res) -} - -fn split_path_prefix(prefix: &ast::Path) -> Option { - let parent = prefix.parent_path()?; - let mut res = make::path_unqualified(parent.segment()?); - for p in successors(parent.parent_path(), |it| it.parent_path()) { - res = make::path_qualified(res, p.segment()?); - } - Some(res) -} - #[cfg(test)] mod tests { use crate::helpers::{check_assist, check_assist_target}; -- cgit v1.2.3