diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-05 10:57:39 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-05 10:57:39 +0000 |
commit | 9b5b13dfcfe32bccbf3593eee26bf88a5fd60413 (patch) | |
tree | 6e9822bea3fbda059fa02661f6e6137770885520 /crates/ra_editor/src/assists | |
parent | 481713a0e17e1557288e88a6b1a173b111792998 (diff) | |
parent | ea3504057e73f541af64451a1b5d2c691d5c01bc (diff) |
Merge #430
430: split import assist r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_editor/src/assists')
-rw-r--r-- | crates/ra_editor/src/assists/split_import.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/crates/ra_editor/src/assists/split_import.rs b/crates/ra_editor/src/assists/split_import.rs new file mode 100644 index 000000000..75f9e8dfb --- /dev/null +++ b/crates/ra_editor/src/assists/split_import.rs | |||
@@ -0,0 +1,44 @@ | |||
1 | use ra_syntax::{ | ||
2 | TextUnit, AstNode, SyntaxKind::COLONCOLON, | ||
3 | ast, | ||
4 | algo::generate, | ||
5 | }; | ||
6 | |||
7 | use crate::assists::{AssistCtx, Assist}; | ||
8 | |||
9 | pub fn split_import(ctx: AssistCtx) -> Option<Assist> { | ||
10 | let colon_colon = ctx | ||
11 | .leaf_at_offset() | ||
12 | .find(|leaf| leaf.kind() == COLONCOLON)?; | ||
13 | let path = colon_colon.parent().and_then(ast::Path::cast)?; | ||
14 | let top_path = generate(Some(path), |it| it.parent_path()).last()?; | ||
15 | |||
16 | let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast); | ||
17 | if use_tree.is_none() { | ||
18 | return None; | ||
19 | } | ||
20 | |||
21 | let l_curly = colon_colon.range().end(); | ||
22 | let r_curly = top_path.syntax().range().end(); | ||
23 | |||
24 | ctx.build("split import", |edit| { | ||
25 | edit.insert(l_curly, "{"); | ||
26 | edit.insert(r_curly, "}"); | ||
27 | edit.set_cursor(l_curly + TextUnit::of_str("{")); | ||
28 | }) | ||
29 | } | ||
30 | |||
31 | #[cfg(test)] | ||
32 | mod tests { | ||
33 | use super::*; | ||
34 | use crate::assists::check_assist; | ||
35 | |||
36 | #[test] | ||
37 | fn test_split_import() { | ||
38 | check_assist( | ||
39 | split_import, | ||
40 | "use crate::<|>db::RootDatabase;", | ||
41 | "use crate::{<|>db::RootDatabase};", | ||
42 | ) | ||
43 | } | ||
44 | } | ||