Skip to content

Commit b1f59ff

Browse files
committed
Reduce path_from_text usage
1 parent 60706fc commit b1f59ff

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

crates/assists/src/handlers/expand_glob_import.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ use ide_db::{
44
defs::{classify_name_ref, Definition, NameRefClass},
55
search::SearchScope,
66
};
7-
use syntax::{algo, ast, AstNode, Direction, SyntaxNode, SyntaxToken, T};
7+
use syntax::{
8+
algo,
9+
ast::{self, make},
10+
AstNode, Direction, SyntaxNode, SyntaxToken, T,
11+
};
812

913
use crate::{
1014
assist_context::{AssistBuilder, AssistContext, Assists},
@@ -249,16 +253,19 @@ fn replace_ast(
249253

250254
let new_use_trees: Vec<ast::UseTree> = names_to_import
251255
.iter()
252-
.map(|n| ast::make::use_tree(ast::make::path_from_text(&n.to_string()), None, None, false))
256+
.map(|n| {
257+
let path = make::path_unqualified(make::path_segment(make::name_ref(&n.to_string())));
258+
make::use_tree(path, None, None, false)
259+
})
253260
.collect();
254261

255262
let use_trees = [&existing_use_trees[..], &new_use_trees[..]].concat();
256263

257264
match use_trees.as_slice() {
258265
[name] => {
259266
if let Some(end_path) = name.path() {
260-
let replacement = ast::make::use_tree(
261-
ast::make::path_from_text(&format!("{}::{}", path, end_path)),
267+
let replacement = make::use_tree(
268+
make::path_from_text(&format!("{}::{}", path, end_path)),
262269
None,
263270
None,
264271
false,
@@ -273,15 +280,12 @@ fn replace_ast(
273280
}
274281
names => {
275282
let replacement = match parent {
276-
Either::Left(_) => ast::make::use_tree(
277-
path,
278-
Some(ast::make::use_tree_list(names.to_owned())),
279-
None,
280-
false,
281-
)
282-
.syntax()
283-
.clone(),
284-
Either::Right(_) => ast::make::use_tree_list(names.to_owned()).syntax().clone(),
283+
Either::Left(_) => {
284+
make::use_tree(path, Some(make::use_tree_list(names.to_owned())), None, false)
285+
.syntax()
286+
.clone()
287+
}
288+
Either::Right(_) => make::use_tree_list(names.to_owned()).syntax().clone(),
285289
};
286290

287291
algo::diff(

crates/syntax/src/ast/make.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub fn path_unqualified(segment: ast::PathSegment) -> ast::Path {
3333
pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path {
3434
path_from_text(&format!("{}::{}", qual, segment))
3535
}
36+
// FIXME: make this private
3637
pub fn path_from_text(text: &str) -> ast::Path {
3738
ast_from_text(text)
3839
}

0 commit comments

Comments
 (0)