Skip to content

Commit 18c62c8

Browse files
bors[bot]Veykril
andauthored
Merge #6019
6019: Remove make::path_from_text r=matklad a=Veykril This removes the `make::path_from_text` function, which according to a note should've been private. I removed it since it didn't really serve a purpose as it was simply wrapping `make::ast_from_text`. Co-authored-by: Lukas Wirth <[email protected]>
2 parents 7b674f9 + f2ae412 commit 18c62c8

File tree

6 files changed

+61
-47
lines changed

6 files changed

+61
-47
lines changed

crates/assists/src/handlers/auto_import.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::collections::BTreeSet;
22

3-
use ast::make;
43
use either::Either;
54
use hir::{
65
AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait,
@@ -54,11 +53,8 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
5453
format!("Import `{}`", &import),
5554
range,
5655
|builder| {
57-
let new_syntax = insert_use(
58-
&scope,
59-
make::path_from_text(&import.to_string()),
60-
ctx.config.insert_use.merge,
61-
);
56+
let new_syntax =
57+
insert_use(&scope, import.to_ast_path(), ctx.config.insert_use.merge);
6258
builder.replace(syntax.text_range(), new_syntax.to_string())
6359
},
6460
);

crates/assists/src/handlers/expand_glob_import.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,8 @@ fn replace_ast(
264264
match use_trees.as_slice() {
265265
[name] => {
266266
if let Some(end_path) = name.path() {
267-
let replacement = make::use_tree(
268-
make::path_from_text(&format!("{}::{}", path, end_path)),
269-
None,
270-
None,
271-
false,
272-
);
267+
let replacement =
268+
make::use_tree(make::path_concat(path, end_path), None, None, false);
273269

274270
algo::diff(
275271
&parent.either(|n| n.syntax().clone(), |n| n.syntax().clone()),

crates/assists/src/handlers/extract_struct_from_enum_variant.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use syntax::{
1212
use crate::{
1313
assist_context::AssistBuilder, utils::insert_use, AssistContext, AssistId, AssistKind, Assists,
1414
};
15-
use ast::make;
1615
use insert_use::ImportScope;
1716

1817
// Assist: extract_struct_from_enum_variant
@@ -112,11 +111,7 @@ fn insert_import(
112111
let scope = ImportScope::find_insert_use_container(path.syntax(), ctx)?;
113112
let syntax = scope.as_syntax_node();
114113

115-
let new_syntax = insert_use(
116-
&scope,
117-
make::path_from_text(&mod_path.to_string()),
118-
ctx.config.insert_use.merge,
119-
);
114+
let new_syntax = insert_use(&scope, mod_path.to_ast_path(), ctx.config.insert_use.merge);
120115
// FIXME: this will currently panic as multiple imports will have overlapping text ranges
121116
builder.replace(syntax.text_range(), new_syntax.to_string())
122117
}

crates/assists/src/handlers/replace_qualified_name_with_use.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use syntax::{algo::SyntaxRewriter, ast, match_ast, AstNode, SyntaxNode, TextRange};
1+
use syntax::{algo::SyntaxRewriter, ast, match_ast, AstNode, SyntaxNode};
22
use test_utils::mark;
33

44
use crate::{
55
utils::{insert_use, ImportScope},
66
AssistContext, AssistId, AssistKind, Assists,
77
};
8-
use ast::make;
98

109
// Assist: replace_qualified_name_with_use
1110
//
@@ -33,15 +32,6 @@ pub(crate) fn replace_qualified_name_with_use(
3332
mark::hit!(dont_import_trivial_paths);
3433
return None;
3534
}
36-
let path_to_import = path.to_string();
37-
let path_to_import = match path.segment()?.generic_arg_list() {
38-
Some(generic_args) => {
39-
let generic_args_start =
40-
generic_args.syntax().text_range().start() - path.syntax().text_range().start();
41-
&path_to_import[TextRange::up_to(generic_args_start)]
42-
}
43-
None => path_to_import.as_str(),
44-
};
4535

4636
let target = path.syntax().text_range();
4737
let scope = ImportScope::find_insert_use_container(path.syntax(), ctx)?;
@@ -54,22 +44,18 @@ pub(crate) fn replace_qualified_name_with_use(
5444
// Now that we've brought the name into scope, re-qualify all paths that could be
5545
// affected (that is, all paths inside the node we added the `use` to).
5646
let mut rewriter = SyntaxRewriter::default();
57-
shorten_paths(&mut rewriter, syntax.clone(), path);
47+
shorten_paths(&mut rewriter, syntax.clone(), &path);
5848
let rewritten_syntax = rewriter.rewrite(&syntax);
5949
if let Some(ref import_scope) = ImportScope::from(rewritten_syntax) {
60-
let new_syntax = insert_use(
61-
import_scope,
62-
make::path_from_text(path_to_import),
63-
ctx.config.insert_use.merge,
64-
);
50+
let new_syntax = insert_use(import_scope, path, ctx.config.insert_use.merge);
6551
builder.replace(syntax.text_range(), new_syntax.to_string())
6652
}
6753
},
6854
)
6955
}
7056

7157
/// Adds replacements to `re` that shorten `path` in all descendants of `node`.
72-
fn shorten_paths(rewriter: &mut SyntaxRewriter<'static>, node: SyntaxNode, path: ast::Path) {
58+
fn shorten_paths(rewriter: &mut SyntaxRewriter<'static>, node: SyntaxNode, path: &ast::Path) {
7359
for child in node.children() {
7460
match_ast! {
7561
match child {
@@ -82,10 +68,10 @@ fn shorten_paths(rewriter: &mut SyntaxRewriter<'static>, node: SyntaxNode, path:
8268
ast::Path(p) => {
8369
match maybe_replace_path(rewriter, p.clone(), path.clone()) {
8470
Some(()) => {},
85-
None => shorten_paths(rewriter, p.syntax().clone(), path.clone()),
71+
None => shorten_paths(rewriter, p.syntax().clone(), path),
8672
}
8773
},
88-
_ => shorten_paths(rewriter, child, path.clone()),
74+
_ => shorten_paths(rewriter, child, path),
8975
}
9076
}
9177
}

crates/hir_def/src/path.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use hir_expand::{
1313
hygiene::Hygiene,
1414
name::{AsName, Name},
1515
};
16-
use syntax::ast;
16+
use syntax::ast::{self, make};
1717

1818
use crate::{
1919
type_ref::{TypeBound, TypeRef},
@@ -100,6 +100,26 @@ impl ModPath {
100100
}
101101
self.segments.first()
102102
}
103+
104+
pub fn to_ast_path(&self) -> ast::Path {
105+
let mut segments = Vec::new();
106+
let mut is_abs = false;
107+
match self.kind {
108+
PathKind::Plain => {}
109+
PathKind::Super(0) => segments.push(make::path_segment_self()),
110+
PathKind::Super(n) => segments.extend((0..n).map(|_| make::path_segment_super())),
111+
PathKind::Crate => segments.push(make::path_segment_crate()),
112+
PathKind::Abs => is_abs = true,
113+
PathKind::DollarCrate(_) => (),
114+
}
115+
116+
segments.extend(
117+
self.segments
118+
.iter()
119+
.map(|segment| make::path_segment(make::name_ref(&segment.to_string()))),
120+
);
121+
make::path_from_segments(segments, is_abs)
122+
}
103123
}
104124

105125
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -286,10 +306,8 @@ impl Display for ModPath {
286306
};
287307
match self.kind {
288308
PathKind::Plain => {}
309+
PathKind::Super(0) => add_segment("self")?,
289310
PathKind::Super(n) => {
290-
if n == 0 {
291-
add_segment("self")?;
292-
}
293311
for _ in 0..n {
294312
add_segment("super")?;
295313
}

crates/syntax/src/ast/make.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,41 @@ pub fn assoc_item_list() -> ast::AssocItemList {
2828
pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment {
2929
ast_from_text(&format!("use {};", name_ref))
3030
}
31+
3132
pub fn path_segment_self() -> ast::PathSegment {
3233
ast_from_text("use self;")
3334
}
35+
36+
pub fn path_segment_super() -> ast::PathSegment {
37+
ast_from_text("use super;")
38+
}
39+
40+
pub fn path_segment_crate() -> ast::PathSegment {
41+
ast_from_text("use crate;")
42+
}
43+
3444
pub fn path_unqualified(segment: ast::PathSegment) -> ast::Path {
35-
path_from_text(&format!("use {}", segment))
45+
ast_from_text(&format!("use {}", segment))
3646
}
47+
3748
pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path {
38-
path_from_text(&format!("{}::{}", qual, segment))
49+
ast_from_text(&format!("{}::{}", qual, segment))
3950
}
40-
// FIXME: make this private
41-
pub fn path_from_text(text: &str) -> ast::Path {
42-
ast_from_text(text)
51+
52+
pub fn path_concat(first: ast::Path, second: ast::Path) -> ast::Path {
53+
ast_from_text(&format!("{}::{}", first, second))
54+
}
55+
56+
pub fn path_from_segments(
57+
segments: impl IntoIterator<Item = ast::PathSegment>,
58+
is_abs: bool,
59+
) -> ast::Path {
60+
let segments = segments.into_iter().map(|it| it.syntax().clone()).join("::");
61+
ast_from_text(&if is_abs {
62+
format!("use ::{};", segments)
63+
} else {
64+
format!("use {};", segments)
65+
})
4366
}
4467

4568
pub fn glob_use_tree() -> ast::UseTree {

0 commit comments

Comments
 (0)