Skip to content

Commit 2090b53

Browse files
committed
Move ModPath->ast::Path function to IDE layer
closes #6092
1 parent 9507a01 commit 2090b53

File tree

8 files changed

+65
-46
lines changed

8 files changed

+65
-46
lines changed

crates/assists/src/ast_transform.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
//! `AstTransformer`s are functions that replace nodes in an AST and can be easily combined.
2-
use rustc_hash::FxHashMap;
3-
42
use hir::{HirDisplay, PathResolution, SemanticsScope};
3+
use rustc_hash::FxHashMap;
54
use syntax::{
65
algo::SyntaxRewriter,
76
ast::{self, AstNode},
87
SyntaxNode,
98
};
109

10+
use crate::utils::mod_path_to_ast;
11+
1112
pub fn apply<'a, N: AstNode>(transformer: &dyn AstTransform<'a>, node: N) -> N {
1213
SyntaxRewriter::from_fn(|element| match element {
1314
syntax::SyntaxElement::Node(n) => {
@@ -189,7 +190,7 @@ impl<'a> AstTransform<'a> for QualifyPaths<'a> {
189190
match resolution {
190191
PathResolution::Def(def) => {
191192
let found_path = from.find_use_path(self.source_scope.db.upcast(), def)?;
192-
let mut path = path_to_ast(found_path);
193+
let mut path = mod_path_to_ast(&found_path);
193194

194195
let type_args = p
195196
.segment()
@@ -210,13 +211,3 @@ impl<'a> AstTransform<'a> for QualifyPaths<'a> {
210211
}
211212
}
212213
}
213-
214-
pub(crate) fn path_to_ast(path: hir::ModPath) -> ast::Path {
215-
let parse = ast::SourceFile::parse(&path.to_string());
216-
parse
217-
.tree()
218-
.syntax()
219-
.descendants()
220-
.find_map(ast::Path::cast)
221-
.unwrap_or_else(|| panic!("failed to parse path {:?}, `{}`", path, path))
222-
}

crates/assists/src/handlers/add_missing_impl_members.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,4 +820,29 @@ impl Tr for () {
820820
}"#,
821821
)
822822
}
823+
824+
#[test]
825+
fn weird_path() {
826+
check_assist(
827+
add_missing_impl_members,
828+
r#"
829+
trait Test {
830+
fn foo(&self, x: crate)
831+
}
832+
impl Test for () {
833+
<|>
834+
}
835+
"#,
836+
r#"
837+
trait Test {
838+
fn foo(&self, x: crate)
839+
}
840+
impl Test for () {
841+
fn foo(&self, x: crate) {
842+
${0:todo!()}
843+
}
844+
}
845+
"#,
846+
)
847+
}
823848
}

crates/assists/src/handlers/auto_import.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use syntax::{
1313
SyntaxNode,
1414
};
1515

16-
use crate::{utils::insert_use, AssistContext, AssistId, AssistKind, Assists, GroupLabel};
16+
use crate::{
17+
utils::insert_use, utils::mod_path_to_ast, AssistContext, AssistId, AssistKind, Assists,
18+
GroupLabel,
19+
};
1720

1821
// Assist: auto_import
1922
//
@@ -54,7 +57,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
5457
range,
5558
|builder| {
5659
let new_syntax =
57-
insert_use(&scope, import.to_ast_path(), ctx.config.insert_use.merge);
60+
insert_use(&scope, mod_path_to_ast(&import), ctx.config.insert_use.merge);
5861
builder.replace(syntax.text_range(), new_syntax.to_string())
5962
},
6063
);

crates/assists/src/handlers/extract_struct_from_enum_variant.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ use syntax::{
1010
};
1111

1212
use crate::{
13-
assist_context::AssistBuilder, utils::insert_use, AssistContext, AssistId, AssistKind, Assists,
13+
assist_context::AssistBuilder,
14+
utils::{insert_use, mod_path_to_ast, ImportScope},
15+
AssistContext, AssistId, AssistKind, Assists,
1416
};
15-
use insert_use::ImportScope;
1617

1718
// Assist: extract_struct_from_enum_variant
1819
//
@@ -111,7 +112,8 @@ fn insert_import(
111112
let scope = ImportScope::find_insert_use_container(path.syntax(), ctx)?;
112113
let syntax = scope.as_syntax_node();
113114

114-
let new_syntax = insert_use(&scope, mod_path.to_ast_path(), ctx.config.insert_use.merge);
115+
let new_syntax =
116+
insert_use(&scope, mod_path_to_ast(&mod_path), ctx.config.insert_use.merge);
115117
// FIXME: this will currently panic as multiple imports will have overlapping text ranges
116118
builder.replace(syntax.text_range(), new_syntax.to_string())
117119
}

crates/assists/src/handlers/fill_match_arms.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use syntax::ast::{self, make, AstNode, MatchArm, NameOwner, Pat};
77
use test_utils::mark;
88

99
use crate::{
10-
utils::{render_snippet, Cursor, FamousDefs},
10+
utils::{mod_path_to_ast, render_snippet, Cursor, FamousDefs},
1111
AssistContext, AssistId, AssistKind, Assists,
1212
};
1313

@@ -192,7 +192,7 @@ fn resolve_tuple_of_enum_def(
192192
}
193193

194194
fn build_pat(db: &RootDatabase, module: hir::Module, var: hir::EnumVariant) -> Option<ast::Pat> {
195-
let path = crate::ast_transform::path_to_ast(module.find_use_path(db, ModuleDef::from(var))?);
195+
let path = mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var))?);
196196

197197
// FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though
198198
let pat: ast::Pat = match var.source(db).value.kind() {

crates/assists/src/utils.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@ use crate::assist_config::SnippetCap;
1919
pub use insert_use::MergeBehaviour;
2020
pub(crate) use insert_use::{insert_use, ImportScope};
2121

22+
pub fn mod_path_to_ast(path: &hir::ModPath) -> ast::Path {
23+
let mut segments = Vec::new();
24+
let mut is_abs = false;
25+
match path.kind {
26+
hir::PathKind::Plain => {}
27+
hir::PathKind::Super(0) => segments.push(make::path_segment_self()),
28+
hir::PathKind::Super(n) => segments.extend((0..n).map(|_| make::path_segment_super())),
29+
hir::PathKind::DollarCrate(_) | hir::PathKind::Crate => {
30+
segments.push(make::path_segment_crate())
31+
}
32+
hir::PathKind::Abs => is_abs = true,
33+
}
34+
35+
segments.extend(
36+
path.segments
37+
.iter()
38+
.map(|segment| make::path_segment(make::name_ref(&segment.to_string()))),
39+
);
40+
make::path_from_segments(segments, is_abs)
41+
}
42+
2243
pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr {
2344
extract_trivial_expression(&block)
2445
.filter(|expr| !expr.syntax().text().contains_char('\n'))

crates/hir/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub use hir_def::{
5151
find_path::PrefixKind,
5252
item_scope::ItemInNs,
5353
nameres::ModuleSource,
54-
path::ModPath,
54+
path::{ModPath, PathKind},
5555
type_ref::{Mutability, TypeRef},
5656
};
5757
pub use hir_expand::{
@@ -63,7 +63,4 @@ pub use hir_ty::display::HirDisplay;
6363
// These are negative re-exports: pub using these names is forbidden, they
6464
// should remain private to hir internals.
6565
#[allow(unused)]
66-
use {
67-
hir_def::path::{Path, PathKind},
68-
hir_expand::hygiene::Hygiene,
69-
};
66+
use {hir_def::path::Path, hir_expand::hygiene::Hygiene};

crates/hir_def/src/path.rs

Lines changed: 1 addition & 21 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::{self, make};
16+
use syntax::ast::{self};
1717

1818
use crate::{
1919
type_ref::{TypeBound, TypeRef},
@@ -100,26 +100,6 @@ 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-
}
123103
}
124104

125105
#[derive(Debug, Clone, PartialEq, Eq, Hash)]

0 commit comments

Comments
 (0)