2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{ast, AstNode, T};
|
2019-01-05 04:45:18 -06:00
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
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.
|
|
|
|
//
|
|
|
|
// ```
|
2021-01-06 14:15:48 -06:00
|
|
|
// use std::$0collections::HashMap;
|
2019-10-27 04:22:53 -05:00
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// use std::{collections::HashMap};
|
|
|
|
// ```
|
2020-05-06 11:45:35 -05:00
|
|
|
pub(crate) fn split_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2020-11-06 15:21:56 -06:00
|
|
|
let colon_colon = ctx.find_token_syntax_at_offset(T![::])?;
|
2021-01-30 09:19:21 -06:00
|
|
|
let path = ast::Path::cast(colon_colon.parent()?)?.qualifier()?;
|
2019-01-05 04:45:18 -06:00
|
|
|
|
2021-06-30 14:29:40 -05:00
|
|
|
let use_tree = path.top_path().syntax().ancestors().find_map(ast::UseTree::cast)?;
|
2019-01-05 04:45:18 -06:00
|
|
|
|
2020-03-18 10:41:24 -05:00
|
|
|
let new_tree = use_tree.split_prefix(&path);
|
|
|
|
if new_tree == use_tree {
|
|
|
|
return None;
|
|
|
|
}
|
2019-01-05 04:45:18 -06:00
|
|
|
|
2020-05-06 05:51:28 -05:00
|
|
|
let target = colon_colon.text_range();
|
2020-07-02 16:48:35 -05:00
|
|
|
acc.add(AssistId("split_import", AssistKind::RefactorRewrite), "Split import", target, |edit| {
|
2020-03-05 12:03:14 -06:00
|
|
|
edit.replace_ast(use_tree, new_tree);
|
2019-10-27 09:35:37 -05:00
|
|
|
})
|
2019-01-05 04:45:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-05-06 03:16:55 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable, 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,
|
2021-01-06 14:15:48 -06:00
|
|
|
"use crate::$0db::RootDatabase;",
|
2020-05-20 16:50:29 -05: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,
|
2021-01-06 14:15:48 -06:00
|
|
|
"use crate:$0:db::{RootDatabase, FileSymbol}",
|
2020-05-20 16:50:29 -05: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() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist_target(split_import, "use crate::$0db::{RootDatabase, FileSymbol}", "::");
|
2019-02-08 17:34:05 -06:00
|
|
|
}
|
2020-04-20 09:34:01 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue4044() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist_not_applicable(split_import, "use crate::$0:::self;")
|
2020-04-20 09:34:01 -05:00
|
|
|
}
|
2020-06-23 01:41:43 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty_use() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
split_import,
|
|
|
|
r"
|
2021-01-06 14:15:48 -06:00
|
|
|
use std::$0
|
2020-06-23 01:41:43 -05:00
|
|
|
fn main() {}",
|
|
|
|
);
|
|
|
|
}
|
2019-01-05 04:45:18 -06:00
|
|
|
}
|