aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-08-31 14:48:07 +0100
committerGitHub <[email protected]>2020-08-31 14:48:07 +0100
commit753af410056937d7be3a1b3457c834a887288957 (patch)
tree1e1420034351cee24f789d9398ec029b7a506f33 /crates
parentac4b134c6be27642dbe915f32a41f9a21bd0c1c9 (diff)
parentb1f59ff6c1dcb88f75e999324d6bb95dfdb78da3 (diff)
Merge #5920
5920: Reduce path_from_text usage r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/assists/src/handlers/expand_glob_import.rs30
-rw-r--r--crates/syntax/src/ast/make.rs15
2 files changed, 18 insertions, 27 deletions
diff --git a/crates/assists/src/handlers/expand_glob_import.rs b/crates/assists/src/handlers/expand_glob_import.rs
index b39d040f6..e14ac7f65 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::{
4 defs::{classify_name_ref, Definition, NameRefClass}, 4 defs::{classify_name_ref, Definition, NameRefClass},
5 search::SearchScope, 5 search::SearchScope,
6}; 6};
7use syntax::{algo, ast, AstNode, Direction, SyntaxNode, SyntaxToken, T}; 7use syntax::{
8 algo,
9 ast::{self, make},
10 AstNode, Direction, SyntaxNode, SyntaxToken, T,
11};
8 12
9use crate::{ 13use crate::{
10 assist_context::{AssistBuilder, AssistContext, Assists}, 14 assist_context::{AssistBuilder, AssistContext, Assists},
@@ -249,7 +253,10 @@ fn replace_ast(
249 253
250 let new_use_trees: Vec<ast::UseTree> = names_to_import 254 let new_use_trees: Vec<ast::UseTree> = names_to_import
251 .iter() 255 .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 })
253 .collect(); 260 .collect();
254 261
255 let use_trees = [&existing_use_trees[..], &new_use_trees[..]].concat(); 262 let use_trees = [&existing_use_trees[..], &new_use_trees[..]].concat();
@@ -257,8 +264,8 @@ fn replace_ast(
257 match use_trees.as_slice() { 264 match use_trees.as_slice() {
258 [name] => { 265 [name] => {
259 if let Some(end_path) = name.path() { 266 if let Some(end_path) = name.path() {
260 let replacement = ast::make::use_tree( 267 let replacement = make::use_tree(
261 ast::make::path_from_text(&format!("{}::{}", path, end_path)), 268 make::path_from_text(&format!("{}::{}", path, end_path)),
262 None, 269 None,
263 None, 270 None,
264 false, 271 false,
@@ -273,15 +280,12 @@ fn replace_ast(
273 } 280 }
274 names => { 281 names => {
275 let replacement = match parent { 282 let replacement = match parent {
276 Either::Left(_) => ast::make::use_tree( 283 Either::Left(_) => {
277 path, 284 make::use_tree(path, Some(make::use_tree_list(names.to_owned())), None, false)
278 Some(ast::make::use_tree_list(names.to_owned())), 285 .syntax()
279 None, 286 .clone()
280 false, 287 }
281 ) 288 Either::Right(_) => make::use_tree_list(names.to_owned()).syntax().clone(),
282 .syntax()
283 .clone(),
284 Either::Right(_) => ast::make::use_tree_list(names.to_owned()).syntax().clone(),
285 }; 289 };
286 290
287 algo::diff( 291 algo::diff(
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs
index 7ba625990..c2c938ad1 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 {
33pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path { 33pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path {
34 path_from_text(&format!("{}::{}", qual, segment)) 34 path_from_text(&format!("{}::{}", qual, segment))
35} 35}
36// FIXME: make this private
36pub fn path_from_text(text: &str) -> ast::Path { 37pub fn path_from_text(text: &str) -> ast::Path {
37 ast_from_text(text) 38 ast_from_text(text)
38} 39}
@@ -144,10 +145,6 @@ fn expr_from_text(text: &str) -> ast::Expr {
144 ast_from_text(&format!("const C: () = {};", text)) 145 ast_from_text(&format!("const C: () = {};", text))
145} 146}
146 147
147pub fn try_expr_from_text(text: &str) -> Option<ast::Expr> {
148 try_ast_from_text(&format!("const C: () = {};", text))
149}
150
151pub fn condition(expr: ast::Expr, pattern: Option<ast::Pat>) -> ast::Condition { 148pub fn condition(expr: ast::Expr, pattern: Option<ast::Pat>) -> ast::Condition {
152 match pattern { 149 match pattern {
153 None => ast_from_text(&format!("const _: () = while {} {{}};", expr)), 150 None => ast_from_text(&format!("const _: () = while {} {{}};", expr)),
@@ -332,16 +329,6 @@ fn ast_from_text<N: AstNode>(text: &str) -> N {
332 node 329 node
333} 330}
334 331
335fn 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
345fn unroot(n: SyntaxNode) -> SyntaxNode { 332fn unroot(n: SyntaxNode) -> SyntaxNode {
346 SyntaxNode::new_root(n.green().clone()) 333 SyntaxNode::new_root(n.green().clone())
347} 334}