2021-04-24 06:31:16 -05:00
|
|
|
use ide_db::helpers::merge_imports::{try_merge_imports, try_merge_trees, MergeBehavior};
|
2021-03-18 04:57:55 -05:00
|
|
|
use syntax::{algo::neighbor, ast, ted, AstNode};
|
2020-03-18 10:41:24 -05:00
|
|
|
|
2020-05-06 11:45:35 -05:00
|
|
|
use crate::{
|
|
|
|
assist_context::{AssistContext, Assists},
|
2020-11-24 15:25:13 -06:00
|
|
|
utils::next_prev,
|
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.
|
|
|
|
//
|
|
|
|
// ```
|
2021-01-06 14:15:48 -06:00
|
|
|
// use std::$0fmt::Formatter;
|
2020-03-18 10:41:24 -05:00
|
|
|
// 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
|
|
|
|
2021-03-18 04:57:55 -05:00
|
|
|
let mut imports = None;
|
|
|
|
let mut uses = None;
|
2020-07-30 07:12:04 -05:00
|
|
|
if let Some(use_item) = tree.syntax().parent().and_then(ast::Use::cast) {
|
2021-03-18 04:57:55 -05:00
|
|
|
let (merged, to_remove) =
|
2020-09-05 08:51:26 -05:00
|
|
|
next_prev().filter_map(|dir| neighbor(&use_item, dir)).find_map(|use_item2| {
|
2021-05-10 14:03:50 -05:00
|
|
|
try_merge_imports(&use_item, &use_item2, MergeBehavior::Crate).zip(Some(use_item2))
|
2020-03-19 06:18:59 -05:00
|
|
|
})?;
|
2020-03-18 10:41:24 -05:00
|
|
|
|
2021-03-18 04:57:55 -05:00
|
|
|
imports = Some((use_item, merged, to_remove));
|
2020-03-19 06:18:59 -05:00
|
|
|
} else {
|
2021-03-18 04:57:55 -05:00
|
|
|
let (merged, to_remove) =
|
2020-09-05 08:51:26 -05:00
|
|
|
next_prev().filter_map(|dir| neighbor(&tree, dir)).find_map(|use_tree| {
|
2021-05-10 14:03:50 -05:00
|
|
|
try_merge_trees(&tree, &use_tree, MergeBehavior::Crate).zip(Some(use_tree))
|
2020-09-05 08:51:26 -05:00
|
|
|
})?;
|
2020-03-19 06:18:59 -05:00
|
|
|
|
2021-03-18 04:57:55 -05:00
|
|
|
uses = Some((tree.clone(), merged, to_remove))
|
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| {
|
2021-03-18 04:57:55 -05:00
|
|
|
if let Some((to_replace, replacement, to_remove)) = imports {
|
2021-05-16 06:18:49 -05:00
|
|
|
let to_replace = builder.make_mut(to_replace);
|
|
|
|
let to_remove = builder.make_mut(to_remove);
|
2021-03-23 09:31:19 -05:00
|
|
|
|
|
|
|
ted::replace(to_replace.syntax(), replacement.syntax());
|
2021-03-18 04:57:55 -05:00
|
|
|
to_remove.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some((to_replace, replacement, to_remove)) = uses {
|
2021-05-16 06:18:49 -05:00
|
|
|
let to_replace = builder.make_mut(to_replace);
|
|
|
|
let to_remove = builder.make_mut(to_remove);
|
2021-03-23 09:31:19 -05:00
|
|
|
|
|
|
|
ted::replace(to_replace.syntax(), replacement.syntax());
|
2021-03-18 04:57:55 -05:00
|
|
|
to_remove.remove()
|
|
|
|
}
|
2020-06-28 17:36:05 -05:00
|
|
|
},
|
|
|
|
)
|
2020-03-18 10:41:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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::*;
|
|
|
|
|
2020-10-13 18:39:58 -05:00
|
|
|
#[test]
|
|
|
|
fn test_merge_equal() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
2021-01-06 14:15:48 -06:00
|
|
|
use std::fmt$0::{Display, Debug};
|
2020-10-13 18:39:58 -05:00
|
|
|
use std::fmt::{Display, Debug};
|
|
|
|
",
|
|
|
|
r"
|
2021-11-19 03:06:36 -06:00
|
|
|
use std::fmt::{Display, Debug};
|
2020-10-13 18:39:58 -05:00
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-18 10:41:24 -05:00
|
|
|
#[test]
|
|
|
|
fn test_merge_first() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
2021-01-06 14:15:48 -06:00
|
|
|
use std::fmt$0::Debug;
|
2020-03-18 10:41:24 -05:00
|
|
|
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;
|
2021-01-06 14:15:48 -06:00
|
|
|
use std::fmt$0::Display;
|
2020-03-18 10:41:24 -05:00
|
|
|
",
|
|
|
|
r"
|
2021-11-19 03:06:36 -06: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"
|
2021-01-06 14:15:48 -06:00
|
|
|
use std::fmt$0;
|
2020-08-12 11:49:43 -05:00
|
|
|
use std::fmt::Display;
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use std::fmt::{self, Display};
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_self2() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
2021-01-06 14:15:48 -06:00
|
|
|
use std::{fmt, $0fmt::Display};
|
2020-08-12 11:49:43 -05:00
|
|
|
",
|
|
|
|
r"
|
2021-11-19 03:06:36 -06:00
|
|
|
use std::{fmt::{Display, self}};
|
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"
|
2021-01-06 14:15:48 -06:00
|
|
|
pub use std::fmt$0::Debug;
|
2020-09-05 08:51:26 -05:00
|
|
|
use std::fmt::Display;
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn skip_pub_last() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
2021-01-06 14:15:48 -06:00
|
|
|
use std::fmt$0::Debug;
|
2020-09-05 08:51:26 -05:00
|
|
|
pub use std::fmt::Display;
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-05 09:15:16 -05:00
|
|
|
#[test]
|
|
|
|
fn skip_pub_crate_pub() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
2021-01-06 14:15:48 -06:00
|
|
|
pub(crate) use std::fmt$0::Debug;
|
2020-09-05 09:15:16 -05:00
|
|
|
pub use std::fmt::Display;
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn skip_pub_pub_crate() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
2021-01-06 14:15:48 -06:00
|
|
|
pub use std::fmt$0::Debug;
|
2020-09-05 09:15:16 -05:00
|
|
|
pub(crate) use std::fmt::Display;
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_pub() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
2021-01-06 14:15:48 -06:00
|
|
|
pub use std::fmt$0::Debug;
|
2020-09-05 09:15:16 -05:00
|
|
|
pub use std::fmt::Display;
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
pub use std::fmt::{Debug, Display};
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_pub_crate() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
2021-01-06 14:15:48 -06:00
|
|
|
pub(crate) use std::fmt$0::Debug;
|
2020-09-05 09:15:16 -05:00
|
|
|
pub(crate) use std::fmt::Display;
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
pub(crate) use std::fmt::{Debug, Display};
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-20 10:27:51 -05:00
|
|
|
#[test]
|
|
|
|
fn merge_pub_in_path_crate() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
pub(in this::path) use std::fmt$0::Debug;
|
|
|
|
pub(in this::path) use std::fmt::Display;
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
pub(in this::path) use std::fmt::{Debug, Display};
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-18 10:41:24 -05:00
|
|
|
#[test]
|
|
|
|
fn test_merge_nested() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
2021-01-06 14:15:48 -06:00
|
|
|
use std::{fmt$0::Debug, fmt::Display};
|
2020-03-18 10:41:24 -05:00
|
|
|
",
|
|
|
|
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
|
|
|
);
|
2020-09-12 12:18:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_merge_nested2() {
|
2020-03-19 06:18:59 -05:00
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
2021-01-06 14:15:48 -06:00
|
|
|
use std::{fmt::Debug, fmt$0::Display};
|
2020-03-19 06:18:59 -05:00
|
|
|
",
|
|
|
|
r"
|
2021-11-19 03:06:36 -06:00
|
|
|
use std::{fmt::{Display, Debug}};
|
2020-03-23 14:57:42 -05:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-14 17:46:16 -06:00
|
|
|
#[test]
|
|
|
|
fn test_merge_with_nested_self_item() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use std$0::{fmt::{Write, Display}};
|
|
|
|
use std::{fmt::{self, Debug}};
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use std::{fmt::{Write, Display, self, Debug}};
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_merge_with_nested_self_item2() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use std$0::{fmt::{self, Debug}};
|
|
|
|
use std::{fmt::{Write, Display}};
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use std::{fmt::{self, Debug, Write, Display}};
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_merge_self_with_nested_self_item() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use std::{fmt$0::{self, Debug}, fmt::{Write, Display}};
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use std::{fmt::{self, Debug, Write, Display}};
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_merge_nested_self_and_empty() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use foo::$0{bar::{self}};
|
|
|
|
use foo::{bar};
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use foo::{bar::{self}};
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_merge_nested_empty_and_self() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use foo::$0{bar};
|
|
|
|
use foo::{bar::{self}};
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use foo::{bar::{self}};
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-14 18:41:49 -06:00
|
|
|
#[test]
|
|
|
|
fn test_merge_nested_list_self_and_glob() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
|
|
|
use std$0::{fmt::*};
|
|
|
|
use std::{fmt::{self, Display}};
|
|
|
|
",
|
|
|
|
r"use std::{fmt::{self, *, Display}};",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:28:25 -05:00
|
|
|
#[test]
|
|
|
|
fn test_merge_single_wildcard_diff_prefixes() {
|
|
|
|
check_assist(
|
|
|
|
merge_imports,
|
|
|
|
r"
|
2021-01-06 14:15:48 -06:00
|
|
|
use std$0::cell::*;
|
2020-03-27 11:28:25 -05:00
|
|
|
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"
|
2021-01-06 14:15:48 -06:00
|
|
|
use std$0::cell::*;
|
2020-03-27 11:28:25 -05:00
|
|
|
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"
|
2021-01-06 14:15:48 -06:00
|
|
|
use foo$0::bar;
|
2020-03-23 14:57:42 -05:00
|
|
|
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 {
|
2021-01-06 14:15:48 -06:00
|
|
|
foo$0::bar,
|
2020-03-24 11:03:05 -05:00
|
|
|
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,
|
2021-01-06 14:15:48 -06:00
|
|
|
foo$0::bar,
|
2020-03-24 11:03:05 -05:00
|
|
|
};
|
|
|
|
",
|
|
|
|
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;
|
2021-01-06 14:15:48 -06:00
|
|
|
use foo::$0{
|
2020-04-12 20:34:01 -05:00
|
|
|
FooBar,
|
2020-04-12 20:29:14 -05:00
|
|
|
};
|
|
|
|
",
|
|
|
|
r"
|
2021-11-19 03:06:36 -06:00
|
|
|
use foo::{
|
|
|
|
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"
|
2021-01-06 14:15:48 -06:00
|
|
|
use std::$0
|
2020-06-23 01:41:43 -05:00
|
|
|
fn main() {}",
|
|
|
|
);
|
|
|
|
}
|
2020-03-18 10:41:24 -05:00
|
|
|
}
|