aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/handlers/split_import.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-08-13 16:34:51 +0100
committerGitHub <[email protected]>2020-08-13 16:34:51 +0100
commit00fb411f3edea72a1a9739f7df6f21cca045730b (patch)
tree500d7c2ec2179309be12a063634cb6a77c9af845 /crates/assists/src/handlers/split_import.rs
parentd2212a49f6d447a14cdc87a9de2a4844e78b6905 (diff)
parentfc34403018079ea053f26d0a31b7517053c7dd8c (diff)
Merge #5749
5749: Rename ra_assists -> assists r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/assists/src/handlers/split_import.rs')
-rw-r--r--crates/assists/src/handlers/split_import.rs79
1 files changed, 79 insertions, 0 deletions
diff --git a/crates/assists/src/handlers/split_import.rs b/crates/assists/src/handlers/split_import.rs
new file mode 100644
index 000000000..15e67eaa1
--- /dev/null
+++ b/crates/assists/src/handlers/split_import.rs
@@ -0,0 +1,79 @@
1use std::iter::successors;
2
3use syntax::{ast, AstNode, T};
4
5use crate::{AssistContext, AssistId, AssistKind, Assists};
6
7// Assist: split_import
8//
9// Wraps the tail of import into braces.
10//
11// ```
12// use std::<|>collections::HashMap;
13// ```
14// ->
15// ```
16// use std::{collections::HashMap};
17// ```
18pub(crate) fn split_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
19 let colon_colon = ctx.find_token_at_offset(T![::])?;
20 let path = ast::Path::cast(colon_colon.parent())?.qualifier()?;
21 let top_path = successors(Some(path.clone()), |it| it.parent_path()).last()?;
22
23 let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast)?;
24
25 let new_tree = use_tree.split_prefix(&path);
26 if new_tree == use_tree {
27 return None;
28 }
29
30 let target = colon_colon.text_range();
31 acc.add(AssistId("split_import", AssistKind::RefactorRewrite), "Split import", target, |edit| {
32 edit.replace_ast(use_tree, new_tree);
33 })
34}
35
36#[cfg(test)]
37mod tests {
38 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
39
40 use super::*;
41
42 #[test]
43 fn test_split_import() {
44 check_assist(
45 split_import,
46 "use crate::<|>db::RootDatabase;",
47 "use crate::{db::RootDatabase};",
48 )
49 }
50
51 #[test]
52 fn split_import_works_with_trees() {
53 check_assist(
54 split_import,
55 "use crate:<|>:db::{RootDatabase, FileSymbol}",
56 "use crate::{db::{RootDatabase, FileSymbol}}",
57 )
58 }
59
60 #[test]
61 fn split_import_target() {
62 check_assist_target(split_import, "use crate::<|>db::{RootDatabase, FileSymbol}", "::");
63 }
64
65 #[test]
66 fn issue4044() {
67 check_assist_not_applicable(split_import, "use crate::<|>:::self;")
68 }
69
70 #[test]
71 fn test_empty_use() {
72 check_assist_not_applicable(
73 split_import,
74 r"
75use std::<|>
76fn main() {}",
77 );
78 }
79}