aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_editor/src')
-rw-r--r--crates/ra_editor/src/assists.rs3
-rw-r--r--crates/ra_editor/src/assists/split_import.rs44
2 files changed, 47 insertions, 0 deletions
diff --git a/crates/ra_editor/src/assists.rs b/crates/ra_editor/src/assists.rs
index cc40ee4c8..57b78342a 100644
--- a/crates/ra_editor/src/assists.rs
+++ b/crates/ra_editor/src/assists.rs
@@ -8,6 +8,7 @@ mod add_derive;
8mod add_impl; 8mod add_impl;
9mod introduce_variable; 9mod introduce_variable;
10mod change_visibility; 10mod change_visibility;
11mod split_import;
11 12
12use ra_text_edit::{TextEdit, TextEditBuilder}; 13use ra_text_edit::{TextEdit, TextEditBuilder};
13use ra_syntax::{ 14use ra_syntax::{
@@ -23,6 +24,7 @@ pub use self::{
23 add_impl::add_impl, 24 add_impl::add_impl,
24 introduce_variable::introduce_variable, 25 introduce_variable::introduce_variable,
25 change_visibility::change_visibility, 26 change_visibility::change_visibility,
27 split_import::split_import,
26}; 28};
27 29
28/// Return all the assists applicable at the given position. 30/// Return all the assists applicable at the given position.
@@ -34,6 +36,7 @@ pub fn assists(file: &SourceFileNode, range: TextRange) -> Vec<LocalEdit> {
34 add_impl, 36 add_impl,
35 introduce_variable, 37 introduce_variable,
36 change_visibility, 38 change_visibility,
39 split_import,
37 ] 40 ]
38 .iter() 41 .iter()
39 .filter_map(|&assist| ctx.clone().apply(assist)) 42 .filter_map(|&assist| ctx.clone().apply(assist))
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 @@
1use ra_syntax::{
2 TextUnit, AstNode, SyntaxKind::COLONCOLON,
3 ast,
4 algo::generate,
5};
6
7use crate::assists::{AssistCtx, Assist};
8
9pub 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)]
32mod 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}