2020-03-05 12:03:14 -06:00
|
|
|
use std::iter::{once, successors};
|
2019-04-13 09:43:49 -05:00
|
|
|
|
2020-03-05 12:03:14 -06:00
|
|
|
use ra_syntax::{
|
|
|
|
ast::{self, make},
|
|
|
|
AstNode, T,
|
|
|
|
};
|
2019-01-05 04:45:18 -06:00
|
|
|
|
2019-07-04 15:05:17 -05:00
|
|
|
use crate::{Assist, AssistCtx, AssistId};
|
2019-01-05 04:45:18 -06:00
|
|
|
|
2019-10-27 04:22:53 -05:00
|
|
|
// Assist: split_import
|
|
|
|
//
|
|
|
|
// Wraps the tail of import into braces.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// use std::<|>collections::HashMap;
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// use std::{collections::HashMap};
|
|
|
|
// ```
|
2020-02-06 09:58:57 -06:00
|
|
|
pub(crate) fn split_import(ctx: AssistCtx) -> Option<Assist> {
|
2019-10-27 03:45:59 -05:00
|
|
|
let colon_colon = ctx.find_token_at_offset(T![::])?;
|
2020-03-05 12:03:14 -06:00
|
|
|
let path = ast::Path::cast(colon_colon.parent())?.qualifier()?;
|
|
|
|
let top_path = successors(Some(path.clone()), |it| it.parent_path()).last()?;
|
2019-01-05 04:45:18 -06:00
|
|
|
|
2020-03-05 12:03:14 -06:00
|
|
|
let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast)?;
|
2019-01-05 04:45:18 -06:00
|
|
|
|
2020-03-05 12:03:14 -06:00
|
|
|
let new_tree = split_use_tree_prefix(&use_tree, &path)?;
|
|
|
|
let cursor = ctx.frange.range.start();
|
2019-01-05 04:45:18 -06:00
|
|
|
|
2020-01-14 11:32:26 -06:00
|
|
|
ctx.add_assist(AssistId("split_import"), "Split import", |edit| {
|
2019-07-20 04:58:27 -05:00
|
|
|
edit.target(colon_colon.text_range());
|
2020-03-05 12:03:14 -06:00
|
|
|
edit.replace_ast(use_tree, new_tree);
|
|
|
|
edit.set_cursor(cursor);
|
2019-10-27 09:35:37 -05:00
|
|
|
})
|
2019-01-05 04:45:18 -06:00
|
|
|
}
|
|
|
|
|
2020-03-05 12:03:14 -06:00
|
|
|
fn split_use_tree_prefix(use_tree: &ast::UseTree, prefix: &ast::Path) -> Option<ast::UseTree> {
|
|
|
|
let suffix = split_path_prefix(&prefix)?;
|
|
|
|
let use_tree = make::use_tree(suffix.clone(), use_tree.use_tree_list(), use_tree.alias());
|
|
|
|
let nested = make::use_tree_list(once(use_tree));
|
|
|
|
let res = make::use_tree(prefix.clone(), Some(nested), None);
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn split_path_prefix(prefix: &ast::Path) -> Option<ast::Path> {
|
|
|
|
let parent = prefix.parent_path()?;
|
|
|
|
let mut res = make::path_unqualified(parent.segment()?);
|
|
|
|
for p in successors(parent.parent_path(), |it| it.parent_path()) {
|
|
|
|
res = make::path_qualified(res, p.segment()?);
|
|
|
|
}
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
|
2019-01-05 04:45:18 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-02-08 17:34:05 -06:00
|
|
|
use crate::helpers::{check_assist, check_assist_target};
|
2019-01-05 04:45:18 -06:00
|
|
|
|
2020-03-05 12:03:14 -06:00
|
|
|
use super::*;
|
|
|
|
|
2019-01-05 04:45:18 -06:00
|
|
|
#[test]
|
|
|
|
fn test_split_import() {
|
|
|
|
check_assist(
|
|
|
|
split_import,
|
|
|
|
"use crate::<|>db::RootDatabase;",
|
2020-03-05 12:03:14 -06:00
|
|
|
"use crate::<|>{db::RootDatabase};",
|
2019-01-05 04:45:18 -06:00
|
|
|
)
|
|
|
|
}
|
2019-01-06 03:48:33 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split_import_works_with_trees() {
|
|
|
|
check_assist(
|
|
|
|
split_import,
|
2019-10-05 09:48:31 -05:00
|
|
|
"use crate:<|>:db::{RootDatabase, FileSymbol}",
|
2020-03-05 12:03:14 -06:00
|
|
|
"use crate:<|>:{db::{RootDatabase, FileSymbol}}",
|
2019-01-06 03:48:33 -06:00
|
|
|
)
|
|
|
|
}
|
2019-02-08 17:34:05 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split_import_target() {
|
2019-10-05 09:48:31 -05:00
|
|
|
check_assist_target(split_import, "use crate::<|>db::{RootDatabase, FileSymbol}", "::");
|
2019-02-08 17:34:05 -06:00
|
|
|
}
|
2019-01-05 04:45:18 -06:00
|
|
|
}
|