2019-04-13 14:43:49 +00:00
|
|
|
use std::iter::successors;
|
|
|
|
|
2019-02-03 18:26:35 +00:00
|
|
|
use hir::db::HirDatabase;
|
2019-07-04 20:05:17 +00:00
|
|
|
use ra_syntax::{ast, AstNode, TextUnit, T};
|
2019-01-05 10:45:18 +00:00
|
|
|
|
2019-07-04 20:05:17 +00:00
|
|
|
use crate::{Assist, AssistCtx, AssistId};
|
2019-01-05 10:45:18 +00:00
|
|
|
|
2019-02-11 17:07:21 +00:00
|
|
|
pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
2019-05-15 12:35:47 +00:00
|
|
|
let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == T![::])?;
|
2019-03-30 10:25:53 +00:00
|
|
|
let path = ast::Path::cast(colon_colon.parent())?;
|
2019-04-13 14:43:49 +00:00
|
|
|
let top_path = successors(Some(path), |it| it.parent_path()).last()?;
|
2019-01-05 10:45:18 +00:00
|
|
|
|
|
|
|
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();
|
2019-01-06 09:48:33 +00:00
|
|
|
let r_curly = match top_path.syntax().parent().and_then(ast::UseTree::cast) {
|
|
|
|
Some(tree) => tree.syntax().range().end(),
|
|
|
|
None => top_path.syntax().range().end(),
|
|
|
|
};
|
2019-01-05 10:45:18 +00:00
|
|
|
|
2019-02-24 10:53:35 +00:00
|
|
|
ctx.add_action(AssistId("split_import"), "split import", |edit| {
|
2019-02-08 21:43:13 +00:00
|
|
|
edit.target(colon_colon.range());
|
2019-01-05 10:45:18 +00:00
|
|
|
edit.insert(l_curly, "{");
|
|
|
|
edit.insert(r_curly, "}");
|
|
|
|
edit.set_cursor(l_curly + TextUnit::of_str("{"));
|
2019-02-11 17:07:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
ctx.build()
|
2019-01-05 10:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-02-08 23:34:05 +00:00
|
|
|
use crate::helpers::{check_assist, check_assist_target};
|
2019-01-05 10:45:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_split_import() {
|
|
|
|
check_assist(
|
|
|
|
split_import,
|
|
|
|
"use crate::<|>db::RootDatabase;",
|
|
|
|
"use crate::{<|>db::RootDatabase};",
|
|
|
|
)
|
|
|
|
}
|
2019-01-06 09:48:33 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split_import_works_with_trees() {
|
|
|
|
check_assist(
|
|
|
|
split_import,
|
|
|
|
"use algo:<|>:visitor::{Visitor, visit}",
|
|
|
|
"use algo::{<|>visitor::{Visitor, visit}}",
|
|
|
|
)
|
|
|
|
}
|
2019-02-08 23:34:05 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split_import_target() {
|
|
|
|
check_assist_target(split_import, "use algo::<|>visitor::{Visitor, visit}", "::");
|
|
|
|
}
|
2019-01-05 10:45:18 +00:00
|
|
|
}
|