From ea3504057e73f541af64451a1b5d2c691d5c01bc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 5 Jan 2019 13:45:18 +0300 Subject: split import assist --- crates/ra_editor/src/assists.rs | 3 ++ crates/ra_editor/src/assists/split_import.rs | 44 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 crates/ra_editor/src/assists/split_import.rs (limited to 'crates/ra_editor/src') 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; mod add_impl; mod introduce_variable; mod change_visibility; +mod split_import; use ra_text_edit::{TextEdit, TextEditBuilder}; use ra_syntax::{ @@ -23,6 +24,7 @@ pub use self::{ add_impl::add_impl, introduce_variable::introduce_variable, change_visibility::change_visibility, + split_import::split_import, }; /// Return all the assists applicable at the given position. @@ -34,6 +36,7 @@ pub fn assists(file: &SourceFileNode, range: TextRange) -> Vec { add_impl, introduce_variable, change_visibility, + split_import, ] .iter() .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 @@ +use ra_syntax::{ + TextUnit, AstNode, SyntaxKind::COLONCOLON, + ast, + algo::generate, +}; + +use crate::assists::{AssistCtx, Assist}; + +pub fn split_import(ctx: AssistCtx) -> Option { + let colon_colon = ctx + .leaf_at_offset() + .find(|leaf| leaf.kind() == COLONCOLON)?; + let path = colon_colon.parent().and_then(ast::Path::cast)?; + let top_path = generate(Some(path), |it| it.parent_path()).last()?; + + let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast); + if use_tree.is_none() { + return None; + } + + let l_curly = colon_colon.range().end(); + let r_curly = top_path.syntax().range().end(); + + ctx.build("split import", |edit| { + edit.insert(l_curly, "{"); + edit.insert(r_curly, "}"); + edit.set_cursor(l_curly + TextUnit::of_str("{")); + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::assists::check_assist; + + #[test] + fn test_split_import() { + check_assist( + split_import, + "use crate::<|>db::RootDatabase;", + "use crate::{<|>db::RootDatabase};", + ) + } +} -- cgit v1.2.3