ide_assists/handlers/
split_import.rs1use syntax::{AstNode, T, ast};
2
3use crate::{AssistContext, AssistId, Assists};
4
5pub(crate) fn split_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
17 let colon_colon = ctx.find_token_syntax_at_offset(T![::])?;
18 let path = ast::Path::cast(colon_colon.parent()?)?.qualifier()?;
19
20 let use_tree = path.top_path().syntax().ancestors().find_map(ast::UseTree::cast)?;
21
22 let has_errors = use_tree
23 .syntax()
24 .descendants_with_tokens()
25 .any(|it| it.kind() == syntax::SyntaxKind::ERROR);
26 let last_segment = use_tree.path().and_then(|it| it.segment());
27 if has_errors || last_segment.is_none() {
28 return None;
29 }
30
31 let target = colon_colon.text_range();
32 acc.add(AssistId::refactor_rewrite("split_import"), "Split import", target, |edit| {
33 let use_tree = edit.make_mut(use_tree.clone());
34 let path = edit.make_mut(path);
35 use_tree.split_prefix(&path);
36 })
37}
38
39#[cfg(test)]
40mod tests {
41 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
42
43 use super::*;
44
45 #[test]
46 fn test_split_import() {
47 check_assist(
48 split_import,
49 "use crate::$0db::RootDatabase;",
50 "use crate::{db::RootDatabase};",
51 )
52 }
53
54 #[test]
55 fn split_import_works_with_trees() {
56 check_assist(
57 split_import,
58 "use crate:$0:db::{RootDatabase, FileSymbol}",
59 "use crate::{db::{RootDatabase, FileSymbol}}",
60 )
61 }
62
63 #[test]
64 fn split_import_target() {
65 check_assist_target(split_import, "use crate::$0db::{RootDatabase, FileSymbol}", "::");
66 }
67
68 #[test]
69 fn issue4044() {
70 check_assist_not_applicable(split_import, "use crate::$0:::self;")
71 }
72
73 #[test]
74 fn test_empty_use() {
75 check_assist_not_applicable(
76 split_import,
77 r"
78use std::$0
79fn main() {}",
80 );
81 }
82}