From f2ae412ccfa96c4bde42f0b004594c4d6fa54634 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 16 Sep 2020 21:29:48 +0200 Subject: Remove make::path_from_text --- crates/assists/src/handlers/auto_import.rs | 8 ++----- crates/assists/src/handlers/expand_glob_import.rs | 8 ++----- .../handlers/extract_struct_from_enum_variant.rs | 7 +----- .../handlers/replace_qualified_name_with_use.rs | 26 +++++----------------- 4 files changed, 11 insertions(+), 38 deletions(-) (limited to 'crates/assists/src') diff --git a/crates/assists/src/handlers/auto_import.rs b/crates/assists/src/handlers/auto_import.rs index b5eb2c722..5a1b5a4ac 100644 --- a/crates/assists/src/handlers/auto_import.rs +++ b/crates/assists/src/handlers/auto_import.rs @@ -1,6 +1,5 @@ use std::collections::BTreeSet; -use ast::make; use either::Either; use hir::{ AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait, @@ -54,11 +53,8 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> format!("Import `{}`", &import), range, |builder| { - let new_syntax = insert_use( - &scope, - make::path_from_text(&import.to_string()), - ctx.config.insert_use.merge, - ); + let new_syntax = + insert_use(&scope, import.to_ast_path(), ctx.config.insert_use.merge); builder.replace(syntax.text_range(), new_syntax.to_string()) }, ); diff --git a/crates/assists/src/handlers/expand_glob_import.rs b/crates/assists/src/handlers/expand_glob_import.rs index e14ac7f65..d1adff972 100644 --- a/crates/assists/src/handlers/expand_glob_import.rs +++ b/crates/assists/src/handlers/expand_glob_import.rs @@ -264,12 +264,8 @@ fn replace_ast( match use_trees.as_slice() { [name] => { if let Some(end_path) = name.path() { - let replacement = make::use_tree( - make::path_from_text(&format!("{}::{}", path, end_path)), - None, - None, - false, - ); + let replacement = + make::use_tree(make::path_concat(path, end_path), None, None, false); algo::diff( &parent.either(|n| n.syntax().clone(), |n| n.syntax().clone()), diff --git a/crates/assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/assists/src/handlers/extract_struct_from_enum_variant.rs index 3ea50f375..d1eadaa99 100644 --- a/crates/assists/src/handlers/extract_struct_from_enum_variant.rs +++ b/crates/assists/src/handlers/extract_struct_from_enum_variant.rs @@ -12,7 +12,6 @@ use syntax::{ use crate::{ assist_context::AssistBuilder, utils::insert_use, AssistContext, AssistId, AssistKind, Assists, }; -use ast::make; use insert_use::ImportScope; // Assist: extract_struct_from_enum_variant @@ -112,11 +111,7 @@ fn insert_import( let scope = ImportScope::find_insert_use_container(path.syntax(), ctx)?; let syntax = scope.as_syntax_node(); - let new_syntax = insert_use( - &scope, - make::path_from_text(&mod_path.to_string()), - ctx.config.insert_use.merge, - ); + let new_syntax = insert_use(&scope, mod_path.to_ast_path(), ctx.config.insert_use.merge); // FIXME: this will currently panic as multiple imports will have overlapping text ranges builder.replace(syntax.text_range(), new_syntax.to_string()) } diff --git a/crates/assists/src/handlers/replace_qualified_name_with_use.rs b/crates/assists/src/handlers/replace_qualified_name_with_use.rs index e48407fcc..27b49455c 100644 --- a/crates/assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/assists/src/handlers/replace_qualified_name_with_use.rs @@ -1,11 +1,10 @@ -use syntax::{algo::SyntaxRewriter, ast, match_ast, AstNode, SyntaxNode, TextRange}; +use syntax::{algo::SyntaxRewriter, ast, match_ast, AstNode, SyntaxNode}; use test_utils::mark; use crate::{ utils::{insert_use, ImportScope}, AssistContext, AssistId, AssistKind, Assists, }; -use ast::make; // Assist: replace_qualified_name_with_use // @@ -33,15 +32,6 @@ pub(crate) fn replace_qualified_name_with_use( mark::hit!(dont_import_trivial_paths); return None; } - let path_to_import = path.to_string(); - let path_to_import = match path.segment()?.generic_arg_list() { - Some(generic_args) => { - let generic_args_start = - generic_args.syntax().text_range().start() - path.syntax().text_range().start(); - &path_to_import[TextRange::up_to(generic_args_start)] - } - None => path_to_import.as_str(), - }; let target = path.syntax().text_range(); let scope = ImportScope::find_insert_use_container(path.syntax(), ctx)?; @@ -54,14 +44,10 @@ pub(crate) fn replace_qualified_name_with_use( // 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(); - shorten_paths(&mut rewriter, syntax.clone(), path); + shorten_paths(&mut rewriter, syntax.clone(), &path); let rewritten_syntax = rewriter.rewrite(&syntax); if let Some(ref import_scope) = ImportScope::from(rewritten_syntax) { - let new_syntax = insert_use( - import_scope, - make::path_from_text(path_to_import), - ctx.config.insert_use.merge, - ); + let new_syntax = insert_use(import_scope, path, ctx.config.insert_use.merge); builder.replace(syntax.text_range(), new_syntax.to_string()) } }, @@ -69,7 +55,7 @@ pub(crate) fn replace_qualified_name_with_use( } /// Adds replacements to `re` that shorten `path` in all descendants of `node`. -fn shorten_paths(rewriter: &mut SyntaxRewriter<'static>, node: SyntaxNode, path: ast::Path) { +fn shorten_paths(rewriter: &mut SyntaxRewriter<'static>, node: SyntaxNode, path: &ast::Path) { for child in node.children() { match_ast! { match child { @@ -82,10 +68,10 @@ fn shorten_paths(rewriter: &mut SyntaxRewriter<'static>, node: SyntaxNode, path: ast::Path(p) => { match maybe_replace_path(rewriter, p.clone(), path.clone()) { Some(()) => {}, - None => shorten_paths(rewriter, p.syntax().clone(), path.clone()), + None => shorten_paths(rewriter, p.syntax().clone(), path), } }, - _ => shorten_paths(rewriter, child, path.clone()), + _ => shorten_paths(rewriter, child, path), } } } -- cgit v1.2.3