5920: Reduce path_from_text usage
 r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-08-31 13:48:07 +00:00 committed by GitHub
commit 753af41005
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 27 deletions

View file

@ -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<ast::UseTree> = 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,
)
Either::Left(_) => {
make::use_tree(path, Some(make::use_tree_list(names.to_owned())), None, false)
.syntax()
.clone(),
Either::Right(_) => ast::make::use_tree_list(names.to_owned()).syntax().clone(),
.clone()
}
Either::Right(_) => make::use_tree_list(names.to_owned()).syntax().clone(),
};
algo::diff(

View file

@ -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)
}
@ -144,10 +145,6 @@ fn expr_from_text(text: &str) -> ast::Expr {
ast_from_text(&format!("const C: () = {};", text))
}
pub fn try_expr_from_text(text: &str) -> Option<ast::Expr> {
try_ast_from_text(&format!("const C: () = {};", text))
}
pub fn condition(expr: ast::Expr, pattern: Option<ast::Pat>) -> ast::Condition {
match pattern {
None => ast_from_text(&format!("const _: () = while {} {{}};", expr)),
@ -332,16 +329,6 @@ fn ast_from_text<N: AstNode>(text: &str) -> N {
node
}
fn try_ast_from_text<N: AstNode>(text: &str) -> Option<N> {
let parse = SourceFile::parse(text);
let node = parse.tree().syntax().descendants().find_map(N::cast)?;
let node = node.syntax().clone();
let node = unroot(node);
let node = N::cast(node).unwrap();
assert_eq!(node.syntax().text_range().start(), 0.into());
Some(node)
}
fn unroot(n: SyntaxNode) -> SyntaxNode {
SyntaxNode::new_root(n.green().clone())
}