2020-03-18 10:41:24 -05:00
|
|
|
use std::iter::successors;
|
|
|
|
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2020-04-13 12:59:30 -05:00
|
|
|
algo::{neighbor, skip_trivia_token, SyntaxRewriter},
|
2020-03-19 05:38:26 -05:00
|
|
|
ast::{self, edit::AstNodeEdit, make},
|
2020-04-13 12:59:30 -05:00
|
|
|
AstNode, Direction, InsertPosition, SyntaxElement, T,
|
2020-03-19 05:38:26 -05:00
|
|
|
};
|
2020-03-18 10:41:24 -05:00
|
|
|
|
2020-05-06 11:45:35 -05:00
|
|
|
use crate::{
|
|
|
|
assist_context::{AssistContext, Assists},
|
2020-06-28 17:36:05 -05:00
|
|
|
AssistId, AssistKind,
|
2020-05-06 11:45:35 -05:00
|
|
|
};
|
2020-03-18 10:41:24 -05:00
|
|
|
|
|
|
|
// Assist: merge_imports
|
|
|
|
//
|
|
|
|
// Merges two imports with a common prefix.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// use std::<|>fmt::Formatter;
|
|
|
|
// use std::io;
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// use std::{fmt::Formatter, io};
|
|
|
|
// ```
|
2020-05-06 11:45:35 -05:00
|
|
|
pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2020-03-18 10:41:24 -05:00
|
|
|
let tree: ast::UseTree = ctx.find_node_at_offset()?;
|
2020-03-24 11:03:05 -05:00
|
|
|
let mut rewriter = SyntaxRewriter::default();
|
2020-05-07 10:32:01 -05:00
|
|
|
let mut offset = ctx.offset();
|
2020-03-24 11:03:05 -05:00
|
|
|
|
2020-07-30 07:12:04 -05:00
|
|
|
if let Some(use_item) = tree.syntax().parent().and_then(ast::Use::cast) {
|
2020-03-19 06:18:59 -05:00
|
|
|
let (merged, to_delete) = next_prev()
|
|
|
|
.filter_map(|dir| neighbor(&use_item, dir))
|
|
|
|
.filter_map(|it| Some((it.clone(), it.use_tree()?)))
|
|
|
|
.find_map(|(use_item, use_tree)| {
|
2020-04-19 14:15:49 -05:00
|
|
|
Some((try_merge_trees(&tree, &use_tree)?, use_item))
|
2020-03-19 06:18:59 -05:00
|
|
|
})?;
|
2020-03-18 10:41:24 -05:00
|
|
|
|
2020-03-24 11:03:05 -05:00
|
|
|
rewriter.replace_ast(&tree, &merged);
|
|
|
|
rewriter += to_delete.remove();
|
|
|
|
|
|
|
|
if to_delete.syntax().text_range().end() < offset {
|
|
|
|
offset -= to_delete.syntax().text_range().len();
|
2020-03-18 10:41:24 -05:00
|
|
|
}
|
2020-03-19 06:18:59 -05:00
|
|
|
} else {
|
|
|
|
let (merged, to_delete) = next_prev()
|
|
|
|
.filter_map(|dir| neighbor(&tree, dir))
|
|
|
|
.find_map(|use_tree| Some((try_merge_trees(&tree, &use_tree)?, use_tree.clone())))?;
|
|
|
|
|
2020-03-24 11:03:05 -05:00
|
|
|
rewriter.replace_ast(&tree, &merged);
|
|
|
|
rewriter += to_delete.remove();
|
|
|
|
|
|
|
|
if to_delete.syntax().text_range().end() < offset {
|
|
|
|
offset -= to_delete.syntax().text_range().len();
|
2020-03-19 06:18:59 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-06 05:51:28 -05:00
|
|
|
let target = tree.syntax().text_range();
|
2020-06-28 17:36:05 -05:00
|
|
|
acc.add(
|
2020-07-02 16:48:35 -05:00
|
|
|
AssistId("merge_imports", AssistKind::RefactorRewrite),
|
2020-06-28 17:36:05 -05:00
|
|
|
"Merge imports",
|
|
|
|
target,
|
|
|
|
|builder| {
|
|
|
|
builder.rewrite(rewriter);
|
|
|
|
},
|
|
|
|
)
|
2020-03-18 10:41:24 -05:00
|
|
|
}
|
|
|
|
|
2020-03-19 06:18:59 -05:00
|
|
|
fn next_prev() -> impl Iterator<Item = Direction> {
|
|
|
|
[Direction::Next, Direction::Prev].iter().copied()
|
|
|
|
}
|
|
|
|
|
2020-03-18 10:41:24 -05:00
|
|
|
fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option<ast::UseTree> {
|
|
|
|
let lhs_path = old.path()?;
|
|
|
|
let rhs_path = new.path()?;
|
|
|
|
|
|
|
|
let (lhs_prefix, rhs_prefix) = common_prefix(&lhs_path, &rhs_path)?;
|
|
|
|
|
|
|
|
let lhs = old.split_prefix(&lhs_prefix);
|
|
|
|
let rhs = new.split_prefix(&rhs_prefix);
|
|
|
|
|
2020-04-13 12:59:30 -05:00
|
|
|
let should_insert_comma = lhs
|
|
|
|
.use_tree_list()?
|
|
|
|
.r_curly_token()
|
|
|
|
.and_then(|it| skip_trivia_token(it.prev_token()?, Direction::Prev))
|
|
|
|
.map(|it| it.kind() != T![,])
|
|
|
|
.unwrap_or(true);
|
2020-04-12 20:29:14 -05:00
|
|
|
|
2020-03-18 10:41:24 -05:00
|
|
|
let mut to_insert: Vec<SyntaxElement> = Vec::new();
|
2020-04-12 20:29:14 -05:00
|
|
|
if should_insert_comma {
|
|
|
|
to_insert.push(make::token(T![,]).into());
|
|
|
|
to_insert.push(make::tokens::single_space().into());
|
|
|
|
}
|
2020-03-18 10:41:24 -05:00
|
|
|
to_insert.extend(
|
|
|
|
rhs.use_tree_list()?
|
|
|
|
.syntax()
|
|
|
|
.children_with_tokens()
|
|
|
|
.filter(|it| it.kind() != T!['{'] && it.kind() != T!['}']),
|
|
|
|
);
|
|
|
|
let use_tree_list = lhs.use_tree_list()?;
|
2020-04-10 03:27:23 -05:00
|
|
|
let pos = InsertPosition::Before(use_tree_list.r_curly_token()?.into());
|
2020-03-18 10:41:24 -05:00
|
|
|
let use_tree_list = use_tree_list.insert_children(pos, to_insert);
|
|
|
|
Some(lhs.with_use_tree_list(use_tree_list))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn common_prefix(lhs: &ast::Path, rhs: &ast::Path) -> Option<(ast::Path, ast::Path)> {
|
|
|
|
let mut res = None;
|
|
|
|
let mut lhs_curr = first_path(&lhs);
|
|
|
|
let mut rhs_curr = first_path(&rhs);
|
|
|
|
loop {
|
|
|
|
match (lhs_curr.segment(), rhs_curr.segment()) {
|
|
|
|
(Some(lhs), Some(rhs)) if lhs.syntax().text() == rhs.syntax().text() => (),
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
res = Some((lhs_curr.clone(), rhs_curr.clone()));
|
|
|
|
|
|
|
|
match (lhs_curr.parent_path(), rhs_curr.parent_path()) {
|
|
|
|
(Some(lhs), Some(rhs)) => {
|
|
|
|
lhs_curr = lhs;
|
|
|
|
rhs_curr = rhs;
|
|
|
|
}
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn first_path(path: &ast::Path) -> ast::Path {
|
|
|
|
successors(Some(path.clone()), |it| it.qualifier()).last().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-06-23 01:41:43 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
2020-03-18 10:41:24 -05:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_merge_first() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use std::fmt<|>::Debug;
|
|
|
|
use std::fmt::Display;
|
|
|
|
",
|
|
|
|
r"
|
2020-05-20 16:14:31 -05:00
|
|
|
use std::fmt::{Debug, Display};
|
2020-03-18 10:41:24 -05:00
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_merge_second() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::fmt<|>::Display;
|
|
|
|
",
|
|
|
|
r"
|
2020-05-20 16:14:31 -05:00
|
|
|
use std::fmt::{Display, Debug};
|
2020-03-18 10:41:24 -05:00
|
|
|
",
|
2020-03-19 06:18:59 -05:00
|
|
|
);
|
2020-03-18 10:41:24 -05:00
|
|
|
}
|
|
|
|
|
2020-08-12 11:49:43 -05:00
|
|
|
#[test]
|
|
|
|
fn merge_self1() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use std::fmt<|>;
|
|
|
|
use std::fmt::Display;
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use std::fmt::{self, Display};
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_self2() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use std::{fmt, <|>fmt::Display};
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use std::{fmt::{Display, self}};
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-18 10:41:24 -05:00
|
|
|
#[test]
|
|
|
|
fn test_merge_nested() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use std::{fmt<|>::Debug, fmt::Display};
|
|
|
|
",
|
|
|
|
r"
|
2020-05-20 16:14:31 -05:00
|
|
|
use std::{fmt::{Debug, Display}};
|
2020-03-18 10:41:24 -05:00
|
|
|
",
|
2020-03-19 06:18:59 -05:00
|
|
|
);
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use std::{fmt::Debug, fmt<|>::Display};
|
|
|
|
",
|
|
|
|
r"
|
2020-05-20 16:14:31 -05:00
|
|
|
use std::{fmt::{Display, Debug}};
|
2020-03-23 14:57:42 -05:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:28:25 -05:00
|
|
|
#[test]
|
|
|
|
fn test_merge_single_wildcard_diff_prefixes() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use std<|>::cell::*;
|
|
|
|
use std::str;
|
|
|
|
",
|
|
|
|
r"
|
2020-05-20 16:14:31 -05:00
|
|
|
use std::{cell::*, str};
|
2020-03-27 11:28:25 -05:00
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_merge_both_wildcard_diff_prefixes() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use std<|>::cell::*;
|
|
|
|
use std::str::*;
|
|
|
|
",
|
|
|
|
r"
|
2020-05-20 16:14:31 -05:00
|
|
|
use std::{cell::*, str::*};
|
2020-03-27 11:28:25 -05:00
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-23 14:57:42 -05:00
|
|
|
#[test]
|
|
|
|
fn removes_just_enough_whitespace() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use foo<|>::bar;
|
|
|
|
use foo::baz;
|
|
|
|
|
|
|
|
/// Doc comment
|
|
|
|
",
|
|
|
|
r"
|
2020-05-20 16:14:31 -05:00
|
|
|
use foo::{bar, baz};
|
2020-03-23 14:57:42 -05:00
|
|
|
|
|
|
|
/// Doc comment
|
2020-03-24 11:03:05 -05:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn works_with_trailing_comma() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use {
|
|
|
|
foo<|>::bar,
|
|
|
|
foo::baz,
|
|
|
|
};
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use {
|
2020-05-20 16:14:31 -05:00
|
|
|
foo::{bar, baz},
|
2020-03-24 11:03:05 -05:00
|
|
|
};
|
|
|
|
",
|
|
|
|
);
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use {
|
|
|
|
foo::baz,
|
|
|
|
foo<|>::bar,
|
|
|
|
};
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use {
|
2020-05-20 16:14:31 -05:00
|
|
|
foo::{bar, baz},
|
2020-03-24 11:03:05 -05:00
|
|
|
};
|
2020-03-19 06:18:59 -05:00
|
|
|
",
|
|
|
|
);
|
2020-03-18 10:41:24 -05:00
|
|
|
}
|
2020-04-12 20:29:14 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_double_comma() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
2020-04-12 20:34:01 -05:00
|
|
|
use foo::bar::baz;
|
|
|
|
use foo::<|>{
|
|
|
|
FooBar,
|
2020-04-12 20:29:14 -05:00
|
|
|
};
|
|
|
|
",
|
|
|
|
r"
|
2020-05-20 16:14:31 -05:00
|
|
|
use foo::{
|
2020-04-12 20:34:01 -05:00
|
|
|
FooBar,
|
|
|
|
bar::baz};
|
2020-04-12 20:29:14 -05:00
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
2020-06-23 01:41:43 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty_use() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use std::<|>
|
|
|
|
fn main() {}",
|
|
|
|
);
|
|
|
|
}
|
2020-03-18 10:41:24 -05:00
|
|
|
}
|