rust/crates/assists/src/handlers/merge_imports.rs

346 lines
6.2 KiB
Rust
Raw Normal View History

2020-08-12 11:26:51 -05:00
use syntax::{
2020-09-05 08:51:26 -05:00
algo::{neighbor, SyntaxRewriter},
ast, AstNode,
2020-03-19 05:38:26 -05:00
};
use crate::{
assist_context::{AssistContext, Assists},
2020-09-05 08:51:26 -05:00
utils::{
insert_use::{try_merge_imports, try_merge_trees},
next_prev, MergeBehaviour,
},
2020-06-28 17:36:05 -05:00
AssistId, AssistKind,
};
// Assist: merge_imports
//
// Merges two imports with a common prefix.
//
// ```
// use std::<|>fmt::Formatter;
// use std::io;
// ```
// ->
// ```
// use std::{fmt::Formatter, io};
// ```
pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let tree: ast::UseTree = ctx.find_node_at_offset()?;
let mut rewriter = SyntaxRewriter::default();
2020-05-07 10:32:01 -05:00
let mut offset = ctx.offset();
2020-07-30 07:12:04 -05:00
if let Some(use_item) = tree.syntax().parent().and_then(ast::Use::cast) {
2020-09-05 08:51:26 -05:00
let (merged, to_delete) =
next_prev().filter_map(|dir| neighbor(&use_item, dir)).find_map(|use_item2| {
try_merge_imports(&use_item, &use_item2, MergeBehaviour::Full).zip(Some(use_item2))
2020-03-19 06:18:59 -05:00
})?;
2020-09-05 08:51:26 -05:00
rewriter.replace_ast(&use_item, &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
} else {
2020-09-05 08:51:26 -05:00
let (merged, to_delete) =
next_prev().filter_map(|dir| neighbor(&tree, dir)).find_map(|use_tree| {
try_merge_trees(&tree, &use_tree, MergeBehaviour::Full).zip(Some(use_tree))
})?;
2020-03-19 06:18:59 -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
}
};
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);
},
)
}
#[cfg(test)]
mod tests {
use crate::tests::{check_assist, check_assist_not_applicable};
use super::*;
#[test]
fn test_merge_equal() {
check_assist(
merge_imports,
r"
use std::fmt<|>::{Display, Debug};
use std::fmt::{Display, Debug};
",
r"
use std::fmt::{Debug, Display};
",
)
}
#[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};
",
)
}
#[test]
fn test_merge_second() {
check_assist(
merge_imports,
r"
use std::fmt::Debug;
use std::fmt<|>::Display;
",
r"
use std::fmt::{Debug, Display};
",
2020-03-19 06:18:59 -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::{self, Display}};
2020-08-12 11:49:43 -05:00
",
);
}
2020-09-05 08:51:26 -05:00
#[test]
fn skip_pub1() {
check_assist_not_applicable(
merge_imports,
r"
pub use std::fmt<|>::Debug;
use std::fmt::Display;
",
);
}
#[test]
fn skip_pub_last() {
check_assist_not_applicable(
merge_imports,
r"
use std::fmt<|>::Debug;
pub use std::fmt::Display;
",
);
}
#[test]
fn skip_pub_crate_pub() {
check_assist_not_applicable(
merge_imports,
r"
pub(crate) use std::fmt<|>::Debug;
pub use std::fmt::Display;
",
);
}
#[test]
fn skip_pub_pub_crate() {
check_assist_not_applicable(
merge_imports,
r"
pub use std::fmt<|>::Debug;
pub(crate) use std::fmt::Display;
",
);
}
#[test]
fn merge_pub() {
check_assist(
merge_imports,
r"
pub use std::fmt<|>::Debug;
pub use std::fmt::Display;
",
r"
pub use std::fmt::{Debug, Display};
",
)
}
#[test]
fn merge_pub_crate() {
check_assist(
merge_imports,
r"
pub(crate) use std::fmt<|>::Debug;
pub(crate) use std::fmt::Display;
",
r"
pub(crate) use std::fmt::{Debug, Display};
",
)
}
#[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-19 06:18:59 -05:00
);
}
#[test]
fn test_merge_nested2() {
2020-03-19 06:18:59 -05:00
check_assist(
merge_imports,
r"
use std::{fmt::Debug, fmt<|>::Display};
",
r"
use std::{fmt::{Debug, Display}};
2020-03-23 14:57:42 -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};
",
)
}
#[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-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
",
);
}
#[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},
};
",
);
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-19 06:18:59 -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,
};
",
r"
use foo::{FooBar, bar::baz};
",
)
}
#[test]
fn test_empty_use() {
check_assist_not_applicable(
merge_imports,
r"
use std::<|>
fn main() {}",
);
}
}