2020-09-16 14:29:48 -05:00
|
|
|
use syntax::{algo::SyntaxRewriter, ast, match_ast, AstNode, SyntaxNode};
|
2020-08-15 11:50:41 -05:00
|
|
|
use test_utils::mark;
|
2019-10-27 08:46:49 -05:00
|
|
|
|
2020-06-13 12:05:31 -05:00
|
|
|
use crate::{
|
2020-09-12 04:55:01 -05:00
|
|
|
utils::{insert_use, ImportScope},
|
2020-06-28 17:36:05 -05:00
|
|
|
AssistContext, AssistId, AssistKind, Assists,
|
2020-06-13 12:05:31 -05:00
|
|
|
};
|
2019-10-27 08:46:49 -05:00
|
|
|
|
2020-02-07 15:35:34 -06:00
|
|
|
// Assist: replace_qualified_name_with_use
|
2019-10-27 09:49:39 -05:00
|
|
|
//
|
2020-02-07 15:35:34 -06:00
|
|
|
// Adds a use statement for a given fully-qualified name.
|
2019-10-27 09:49:39 -05:00
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn process(map: std::collections::<|>HashMap<String, String>) {}
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// use std::collections::HashMap;
|
|
|
|
//
|
|
|
|
// fn process(map: HashMap<String, String>) {}
|
|
|
|
// ```
|
2020-05-06 11:45:35 -05:00
|
|
|
pub(crate) fn replace_qualified_name_with_use(
|
|
|
|
acc: &mut Assists,
|
|
|
|
ctx: &AssistContext,
|
|
|
|
) -> Option<()> {
|
2019-10-27 08:46:49 -05:00
|
|
|
let path: ast::Path = ctx.find_node_at_offset()?;
|
|
|
|
// We don't want to mess with use statements
|
2020-07-30 07:12:04 -05:00
|
|
|
if path.syntax().ancestors().find_map(ast::Use::cast).is_some() {
|
2019-10-27 08:46:49 -05:00
|
|
|
return None;
|
|
|
|
}
|
2020-08-15 11:50:41 -05:00
|
|
|
if path.qualifier().is_none() {
|
|
|
|
mark::hit!(dont_import_trivial_paths);
|
2019-10-27 08:46:49 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-05-06 05:51:28 -05:00
|
|
|
let target = path.syntax().text_range();
|
2020-09-03 09:23:33 -05:00
|
|
|
let scope = ImportScope::find_insert_use_container(path.syntax(), ctx)?;
|
|
|
|
let syntax = scope.as_syntax_node();
|
2020-05-06 11:45:35 -05:00
|
|
|
acc.add(
|
2020-07-02 16:48:35 -05:00
|
|
|
AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite),
|
2020-02-07 15:35:34 -06:00
|
|
|
"Replace qualified path with use",
|
2020-05-06 05:51:28 -05:00
|
|
|
target,
|
2020-05-06 11:45:35 -05:00
|
|
|
|builder| {
|
2020-06-13 12:05:31 -05:00
|
|
|
// Now that we've brought the name into scope, re-qualify all paths that could be
|
|
|
|
// affected (that is, all paths inside the node we added the `use` to).
|
|
|
|
let mut rewriter = SyntaxRewriter::default();
|
2020-09-16 14:29:48 -05:00
|
|
|
shorten_paths(&mut rewriter, syntax.clone(), &path);
|
2020-09-03 07:22:22 -05:00
|
|
|
let rewritten_syntax = rewriter.rewrite(&syntax);
|
2020-09-03 09:23:33 -05:00
|
|
|
if let Some(ref import_scope) = ImportScope::from(rewritten_syntax) {
|
2020-09-16 14:29:48 -05:00
|
|
|
let new_syntax = insert_use(import_scope, path, ctx.config.insert_use.merge);
|
2020-09-03 09:23:33 -05:00
|
|
|
builder.replace(syntax.text_range(), new_syntax.to_string())
|
|
|
|
}
|
2020-02-28 14:53:20 -06:00
|
|
|
},
|
|
|
|
)
|
2019-02-11 14:57:38 -06:00
|
|
|
}
|
|
|
|
|
2020-06-13 12:05:31 -05:00
|
|
|
/// Adds replacements to `re` that shorten `path` in all descendants of `node`.
|
2020-09-16 14:29:48 -05:00
|
|
|
fn shorten_paths(rewriter: &mut SyntaxRewriter<'static>, node: SyntaxNode, path: &ast::Path) {
|
2020-06-13 12:05:31 -05:00
|
|
|
for child in node.children() {
|
|
|
|
match_ast! {
|
|
|
|
match child {
|
|
|
|
// Don't modify `use` items, as this can break the `use` item when injecting a new
|
|
|
|
// import into the use tree.
|
2020-07-30 07:12:04 -05:00
|
|
|
ast::Use(_it) => continue,
|
2020-06-13 12:05:31 -05:00
|
|
|
// Don't descend into submodules, they don't have the same `use` items in scope.
|
|
|
|
ast::Module(_it) => continue,
|
|
|
|
|
|
|
|
ast::Path(p) => {
|
2020-06-15 17:49:59 -05:00
|
|
|
match maybe_replace_path(rewriter, p.clone(), path.clone()) {
|
2020-06-13 12:05:31 -05:00
|
|
|
Some(()) => {},
|
2020-09-16 14:29:48 -05:00
|
|
|
None => shorten_paths(rewriter, p.syntax().clone(), path),
|
2020-06-13 12:05:31 -05:00
|
|
|
}
|
|
|
|
},
|
2020-09-16 14:29:48 -05:00
|
|
|
_ => shorten_paths(rewriter, child, path),
|
2020-06-13 12:05:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn maybe_replace_path(
|
2020-06-15 17:49:59 -05:00
|
|
|
rewriter: &mut SyntaxRewriter<'static>,
|
|
|
|
path: ast::Path,
|
|
|
|
target: ast::Path,
|
2020-06-13 12:05:31 -05:00
|
|
|
) -> Option<()> {
|
2020-07-19 13:26:24 -05:00
|
|
|
if !path_eq(path.clone(), target) {
|
2020-06-13 12:05:31 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:49:59 -05:00
|
|
|
// Shorten `path`, leaving only its last segment.
|
|
|
|
if let Some(parent) = path.qualifier() {
|
|
|
|
rewriter.delete(parent.syntax());
|
|
|
|
}
|
|
|
|
if let Some(double_colon) = path.coloncolon_token() {
|
|
|
|
rewriter.delete(&double_colon);
|
2020-06-13 12:05:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:49:59 -05:00
|
|
|
fn path_eq(lhs: ast::Path, rhs: ast::Path) -> bool {
|
|
|
|
let mut lhs_curr = lhs;
|
|
|
|
let mut rhs_curr = rhs;
|
|
|
|
loop {
|
|
|
|
match (lhs_curr.segment(), rhs_curr.segment()) {
|
|
|
|
(Some(lhs), Some(rhs)) if lhs.syntax().text() == rhs.syntax().text() => (),
|
|
|
|
_ => return false,
|
|
|
|
}
|
|
|
|
|
|
|
|
match (lhs_curr.qualifier(), rhs_curr.qualifier()) {
|
|
|
|
(Some(lhs), Some(rhs)) => {
|
|
|
|
lhs_curr = lhs;
|
|
|
|
rhs_curr = rhs;
|
|
|
|
}
|
|
|
|
(None, None) => return true,
|
|
|
|
_ => return false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 08:34:37 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-05-06 03:16:55 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
2019-02-06 08:34:37 -06:00
|
|
|
|
2019-10-27 09:26:51 -05:00
|
|
|
use super::*;
|
|
|
|
|
2019-02-06 08:34:37 -06:00
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_add_use_no_anchor() {
|
2019-02-06 08:34:37 -06:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
std::fmt::Debug<|>
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
Debug
|
2019-04-19 15:30:05 -05:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_add_use_no_anchor_with_item_below() {
|
2019-04-19 15:30:05 -05:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-04-19 15:30:05 -05:00
|
|
|
std::fmt::Debug<|>
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-04-19 15:30:05 -05:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
Debug
|
2019-04-19 15:30:05 -05:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_add_use_no_anchor_with_item_above() {
|
2019-04-19 15:30:05 -05:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-04-19 15:30:05 -05:00
|
|
|
fn main() {
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fmt::Debug<|>
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-04-19 15:30:05 -05:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}
|
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
Debug
|
2019-02-06 08:34:37 -06:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-11 14:57:38 -06:00
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_add_use_no_anchor_2seg() {
|
2019-02-11 14:57:38 -06:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-11 14:57:38 -06:00
|
|
|
std::fmt<|>::Debug
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-11 14:57:38 -06:00
|
|
|
use std::fmt;
|
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
fmt::Debug
|
2019-02-11 14:57:38 -06:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-06 08:34:37 -06:00
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_add_use() {
|
2019-02-06 08:34:37 -06:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
use stdx;
|
|
|
|
|
|
|
|
impl std::fmt::Debug<|> for Foo {
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
2020-09-03 07:22:22 -05:00
|
|
|
use stdx;
|
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
impl Debug for Foo {
|
2019-02-06 08:34:37 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_file_use_other_anchor() {
|
2019-02-06 08:34:37 -06:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
impl std::fmt::Debug<|> for Foo {
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
impl Debug for Foo {
|
2019-02-06 08:34:37 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_add_use_other_anchor_indent() {
|
2019-02-06 08:34:37 -06:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
impl std::fmt::Debug<|> for Foo {
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
impl Debug for Foo {
|
2019-02-06 08:34:37 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_split_different() {
|
2019-02-06 08:34:37 -06:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
impl std::io<|> for Foo {
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-09-03 07:22:22 -05:00
|
|
|
use std::{fmt, io};
|
2019-02-06 08:34:37 -06:00
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
impl io for Foo {
|
2019-02-06 08:34:37 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_split_self_for_use() {
|
2019-02-06 08:34:37 -06:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
impl std::fmt::Debug<|> for Foo {
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-09-03 07:22:22 -05:00
|
|
|
use std::fmt::{self, Debug};
|
2019-02-06 08:34:37 -06:00
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
impl Debug for Foo {
|
2019-02-06 08:34:37 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_split_self_for_target() {
|
2019-02-06 08:34:37 -06:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
|
|
|
impl std::fmt<|> for Foo {
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-09-12 12:18:14 -05:00
|
|
|
use std::fmt::{self, Debug};
|
2019-02-06 08:34:37 -06:00
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
impl fmt for Foo {
|
2019-02-06 08:34:37 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_add_to_nested_self_nested() {
|
2019-02-06 08:34:37 -06:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
use std::fmt::{Debug, nested::{Display}};
|
|
|
|
|
|
|
|
impl std::fmt::nested<|> for Foo {
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-09-12 12:18:14 -05:00
|
|
|
use std::fmt::{Debug, nested::{self, Display}};
|
2019-02-06 08:34:37 -06:00
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
impl nested for Foo {
|
2019-02-06 08:34:37 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_add_to_nested_self_already_included() {
|
2019-02-06 08:34:37 -06:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
use std::fmt::{Debug, nested::{self, Display}};
|
|
|
|
|
|
|
|
impl std::fmt::nested<|> for Foo {
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-09-12 12:18:14 -05:00
|
|
|
use std::fmt::{Debug, nested::{self, Display}};
|
2019-02-06 08:34:37 -06:00
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
impl nested for Foo {
|
2019-02-06 08:34:37 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_add_to_nested_nested() {
|
2019-02-06 08:34:37 -06:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
use std::fmt::{Debug, nested::{Display}};
|
|
|
|
|
|
|
|
impl std::fmt::nested::Debug<|> for Foo {
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-09-12 12:18:14 -05:00
|
|
|
use std::fmt::{Debug, nested::{Debug, Display}};
|
2019-02-06 08:34:37 -06:00
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
impl Debug for Foo {
|
2019-02-06 08:34:37 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_split_common_target_longer() {
|
2019-02-14 14:33:38 -06:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-14 14:33:38 -06:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
|
|
|
impl std::fmt::nested::Display<|> for Foo {
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-09-03 07:22:22 -05:00
|
|
|
use std::fmt::{Debug, nested::Display};
|
2019-02-14 14:33:38 -06:00
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
impl Display for Foo {
|
2019-02-14 14:33:38 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_split_common_use_longer() {
|
2019-02-14 14:33:38 -06:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-14 14:33:38 -06:00
|
|
|
use std::fmt::nested::Debug;
|
|
|
|
|
|
|
|
impl std::fmt::Display<|> for Foo {
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-10-12 16:34:32 -05:00
|
|
|
use std::fmt::{nested::Debug, Display};
|
2019-02-14 14:33:38 -06:00
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
impl Display for Foo {
|
2019-02-14 14:33:38 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-28 13:08:56 -05:00
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_use_nested_import() {
|
2019-10-28 13:08:56 -05:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-10-28 13:08:56 -05:00
|
|
|
use crate::{
|
|
|
|
ty::{Substs, Ty},
|
|
|
|
AssocItem,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn foo() { crate::ty::lower<|>::trait_env() }
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-09-12 12:18:14 -05:00
|
|
|
use crate::{AssocItem, ty::{Substs, Ty, lower}};
|
2019-10-28 13:08:56 -05:00
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
fn foo() { lower::trait_env() }
|
2019-10-28 13:08:56 -05:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-14 14:33:38 -06:00
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_alias() {
|
2019-02-06 08:34:37 -06:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
use std::fmt as foo;
|
|
|
|
|
|
|
|
impl foo::Debug<|> for Foo {
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
use std::fmt as foo;
|
|
|
|
|
2020-09-03 07:22:22 -05:00
|
|
|
use foo::Debug;
|
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
impl Debug for Foo {
|
2019-02-06 08:34:37 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-08-15 11:50:41 -05:00
|
|
|
fn dont_import_trivial_paths() {
|
|
|
|
mark::check!(dont_import_trivial_paths);
|
2019-02-06 08:34:37 -06:00
|
|
|
check_assist_not_applicable(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-06 08:34:37 -06:00
|
|
|
impl foo<|> for Foo {
|
|
|
|
}
|
2019-02-11 14:57:38 -06:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_not_applicable_in_use() {
|
2019-02-11 14:57:38 -06:00
|
|
|
check_assist_not_applicable(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-11 14:57:38 -06:00
|
|
|
use std::fmt<|>;
|
2019-02-06 08:34:37 -06:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2019-02-12 14:14:51 -06:00
|
|
|
|
|
|
|
#[test]
|
2020-02-07 15:35:34 -06:00
|
|
|
fn test_replace_add_use_no_anchor_in_mod_mod() {
|
2019-02-12 14:14:51 -06:00
|
|
|
check_assist(
|
2020-02-07 15:35:34 -06:00
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-12 14:14:51 -06:00
|
|
|
mod foo {
|
|
|
|
mod bar {
|
|
|
|
std::fmt::Debug<|>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2019-02-12 14:14:51 -06:00
|
|
|
mod foo {
|
|
|
|
mod bar {
|
|
|
|
use std::fmt::Debug;
|
|
|
|
|
2020-05-20 15:55:37 -05:00
|
|
|
Debug
|
2019-02-12 14:14:51 -06:00
|
|
|
}
|
2020-02-12 08:21:55 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inserts_imports_after_inner_attributes() {
|
|
|
|
check_assist(
|
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-02-12 08:21:55 -06:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
std::fmt::Debug<|>
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-02-12 08:21:55 -06:00
|
|
|
#![allow(dead_code)]
|
2020-09-03 07:38:34 -05:00
|
|
|
|
2020-02-12 08:21:55 -06:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
|
|
|
fn main() {
|
2020-05-20 15:55:37 -05:00
|
|
|
Debug
|
2020-06-13 12:05:31 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replaces_all_affected_paths() {
|
|
|
|
check_assist(
|
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-06-13 12:05:31 -05:00
|
|
|
fn main() {
|
|
|
|
std::fmt::Debug<|>;
|
|
|
|
let x: std::fmt::Debug = std::fmt::Debug;
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-06-13 12:05:31 -05:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
Debug;
|
|
|
|
let x: Debug = Debug;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replaces_all_affected_paths_mod() {
|
|
|
|
check_assist(
|
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-06-13 12:05:31 -05:00
|
|
|
mod m {
|
|
|
|
fn f() {
|
|
|
|
std::fmt::Debug<|>;
|
|
|
|
let x: std::fmt::Debug = std::fmt::Debug;
|
|
|
|
}
|
|
|
|
fn g() {
|
|
|
|
std::fmt::Debug;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
std::fmt::Debug;
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-06-13 12:05:31 -05:00
|
|
|
mod m {
|
|
|
|
use std::fmt::Debug;
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
Debug;
|
|
|
|
let x: Debug = Debug;
|
|
|
|
}
|
|
|
|
fn g() {
|
|
|
|
Debug;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
std::fmt::Debug;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn does_not_replace_in_submodules() {
|
|
|
|
check_assist(
|
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-06-13 12:05:31 -05:00
|
|
|
fn main() {
|
|
|
|
std::fmt::Debug<|>;
|
|
|
|
}
|
|
|
|
|
|
|
|
mod sub {
|
|
|
|
fn f() {
|
|
|
|
std::fmt::Debug;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-06-13 12:05:31 -05:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
Debug;
|
|
|
|
}
|
|
|
|
|
|
|
|
mod sub {
|
|
|
|
fn f() {
|
|
|
|
std::fmt::Debug;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn does_not_replace_in_use() {
|
|
|
|
check_assist(
|
|
|
|
replace_qualified_name_with_use,
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-06-13 12:05:31 -05:00
|
|
|
use std::fmt::Display;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
std::fmt<|>;
|
|
|
|
}
|
|
|
|
",
|
2020-06-15 15:39:26 -05:00
|
|
|
r"
|
2020-09-12 12:18:14 -05:00
|
|
|
use std::fmt::{self, Display};
|
2020-06-13 12:05:31 -05:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
fmt;
|
2020-08-03 14:17:05 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn does_not_replace_pub_use() {
|
|
|
|
check_assist(
|
|
|
|
replace_qualified_name_with_use,
|
|
|
|
r"
|
|
|
|
pub use std::fmt;
|
|
|
|
|
|
|
|
impl std::io<|> for Foo {
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
pub use std::fmt;
|
2020-09-03 07:22:22 -05:00
|
|
|
use std::io;
|
2020-08-03 14:17:05 -05:00
|
|
|
|
|
|
|
impl io for Foo {
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn does_not_replace_pub_crate_use() {
|
|
|
|
check_assist(
|
|
|
|
replace_qualified_name_with_use,
|
|
|
|
r"
|
|
|
|
pub(crate) use std::fmt;
|
|
|
|
|
|
|
|
impl std::io<|> for Foo {
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
pub(crate) use std::fmt;
|
2020-09-03 07:22:22 -05:00
|
|
|
use std::io;
|
2020-08-03 14:17:05 -05:00
|
|
|
|
|
|
|
impl io for Foo {
|
2019-02-12 14:14:51 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2019-02-06 08:34:37 -06:00
|
|
|
}
|