From b1f59ff6c1dcb88f75e999324d6bb95dfdb78da3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 31 Aug 2020 15:47:42 +0200 Subject: [PATCH] Reduce path_from_text usage --- .../src/handlers/expand_glob_import.rs | 30 +++++++++++-------- crates/syntax/src/ast/make.rs | 1 + 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/crates/assists/src/handlers/expand_glob_import.rs b/crates/assists/src/handlers/expand_glob_import.rs index b39d040f6f..e14ac7f65e 100644 --- a/crates/assists/src/handlers/expand_glob_import.rs +++ b/crates/assists/src/handlers/expand_glob_import.rs @@ -4,7 +4,11 @@ use ide_db::{ defs::{classify_name_ref, Definition, NameRefClass}, search::SearchScope, }; -use syntax::{algo, ast, AstNode, Direction, SyntaxNode, SyntaxToken, T}; +use syntax::{ + algo, + ast::{self, make}, + AstNode, Direction, SyntaxNode, SyntaxToken, T, +}; use crate::{ assist_context::{AssistBuilder, AssistContext, Assists}, @@ -249,7 +253,10 @@ fn replace_ast( let new_use_trees: Vec = names_to_import .iter() - .map(|n| ast::make::use_tree(ast::make::path_from_text(&n.to_string()), None, None, false)) + .map(|n| { + let path = make::path_unqualified(make::path_segment(make::name_ref(&n.to_string()))); + make::use_tree(path, None, None, false) + }) .collect(); let use_trees = [&existing_use_trees[..], &new_use_trees[..]].concat(); @@ -257,8 +264,8 @@ fn replace_ast( match use_trees.as_slice() { [name] => { if let Some(end_path) = name.path() { - let replacement = ast::make::use_tree( - ast::make::path_from_text(&format!("{}::{}", path, end_path)), + let replacement = make::use_tree( + make::path_from_text(&format!("{}::{}", path, end_path)), None, None, false, @@ -273,15 +280,12 @@ fn replace_ast( } names => { let replacement = match parent { - Either::Left(_) => ast::make::use_tree( - path, - Some(ast::make::use_tree_list(names.to_owned())), - None, - false, - ) - .syntax() - .clone(), - Either::Right(_) => ast::make::use_tree_list(names.to_owned()).syntax().clone(), + Either::Left(_) => { + make::use_tree(path, Some(make::use_tree_list(names.to_owned())), None, false) + .syntax() + .clone() + } + Either::Right(_) => make::use_tree_list(names.to_owned()).syntax().clone(), }; algo::diff( diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index a83f768578..c2c938ad11 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -33,6 +33,7 @@ pub fn path_unqualified(segment: ast::PathSegment) -> ast::Path { pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path { path_from_text(&format!("{}::{}", qual, segment)) } +// FIXME: make this private pub fn path_from_text(text: &str) -> ast::Path { ast_from_text(text) }