Skip to content

Commit 753af41

Browse files
bors[bot]matklad
andauthored
Merge #5920
5920: Reduce path_from_text usage r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents ac4b134 + b1f59ff commit 753af41

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
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 & 14 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
}
@@ -144,10 +145,6 @@ fn expr_from_text(text: &str) -> ast::Expr {
144145
ast_from_text(&format!("const C: () = {};", text))
145146
}
146147

147-
pub fn try_expr_from_text(text: &str) -> Option<ast::Expr> {
148-
try_ast_from_text(&format!("const C: () = {};", text))
149-
}
150-
151148
pub fn condition(expr: ast::Expr, pattern: Option<ast::Pat>) -> ast::Condition {
152149
match pattern {
153150
None => ast_from_text(&format!("const _: () = while {} {{}};", expr)),
@@ -332,16 +329,6 @@ fn ast_from_text<N: AstNode>(text: &str) -> N {
332329
node
333330
}
334331

335-
fn try_ast_from_text<N: AstNode>(text: &str) -> Option<N> {
336-
let parse = SourceFile::parse(text);
337-
let node = parse.tree().syntax().descendants().find_map(N::cast)?;
338-
let node = node.syntax().clone();
339-
let node = unroot(node);
340-
let node = N::cast(node).unwrap();
341-
assert_eq!(node.syntax().text_range().start(), 0.into());
342-
Some(node)
343-
}
344-
345332
fn unroot(n: SyntaxNode) -> SyntaxNode {
346333
SyntaxNode::new_root(n.green().clone())
347334
}

0 commit comments

Comments
 (0)