]> git.lizzy.rs Git - rust.git/blob - crates/ide_assists/src/handlers/split_import.rs
Use mutable syntax trees in `merge_imports`, `split_imports`
[rust.git] / crates / ide_assists / src / handlers / split_import.rs
1 use syntax::{ast, AstNode, T};
2
3 use crate::{AssistContext, AssistId, AssistKind, Assists};
4
5 // Assist: split_import
6 //
7 // Wraps the tail of import into braces.
8 //
9 // ```
10 // use std::$0collections::HashMap;
11 // ```
12 // ->
13 // ```
14 // use std::{collections::HashMap};
15 // ```
16 pub(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("split_import", AssistKind::RefactorRewrite), "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)]
40 mod 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"
78 use std::$0
79 fn main() {}",
80         );
81     }
82 }