Skip to content

Commit 11c7207

Browse files
authored
Merge pull request #20103 from ChayimFriedman2/path-transform-prettify
fix: Prettify AST in `PathTransform` if it's coming from a macro
2 parents d2691ac + 4db8341 commit 11c7207

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

crates/hir/src/semantics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,6 +2199,10 @@ pub struct SemanticsScope<'db> {
21992199
}
22002200

22012201
impl<'db> SemanticsScope<'db> {
2202+
pub fn file_id(&self) -> HirFileId {
2203+
self.file_id
2204+
}
2205+
22022206
pub fn module(&self) -> Module {
22032207
Module { id: self.resolver.module() }
22042208
}

crates/ide-completion/src/tests/item_list.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,30 @@ fn inside_extern_blocks() {
550550
"#]],
551551
)
552552
}
553+
554+
#[test]
555+
fn tokens_from_macro() {
556+
check_edit(
557+
"fn as_ref",
558+
r#"
559+
//- proc_macros: identity
560+
//- minicore: as_ref
561+
struct Foo;
562+
563+
#[proc_macros::identity]
564+
impl<'a> AsRef<&'a i32> for Foo {
565+
$0
566+
}
567+
"#,
568+
r#"
569+
struct Foo;
570+
571+
#[proc_macros::identity]
572+
impl<'a> AsRef<&'a i32> for Foo {
573+
fn as_ref(&self) -> &&'a i32 {
574+
$0
575+
}
576+
}
577+
"#,
578+
);
579+
}

crates/ide-db/src/path_transform.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
33
use crate::helpers::mod_path_to_ast;
44
use either::Either;
5-
use hir::{AsAssocItem, HirDisplay, ImportPathConfig, ModuleDef, SemanticsScope};
5+
use hir::{
6+
AsAssocItem, HirDisplay, HirFileId, ImportPathConfig, ModuleDef, SemanticsScope,
7+
prettify_macro_expansion,
8+
};
69
use itertools::Itertools;
710
use rustc_hash::FxHashMap;
811
use span::Edition;
@@ -136,6 +139,25 @@ impl<'a> PathTransform<'a> {
136139
}
137140
}
138141

142+
fn prettify_target_node(&self, node: SyntaxNode) -> SyntaxNode {
143+
match self.target_scope.file_id() {
144+
HirFileId::FileId(_) => node,
145+
HirFileId::MacroFile(file_id) => {
146+
let db = self.target_scope.db;
147+
prettify_macro_expansion(
148+
db,
149+
node,
150+
&db.expansion_span_map(file_id),
151+
self.target_scope.module().krate().into(),
152+
)
153+
}
154+
}
155+
}
156+
157+
fn prettify_target_ast<N: AstNode>(&self, node: N) -> N {
158+
N::cast(self.prettify_target_node(node.syntax().clone())).unwrap()
159+
}
160+
139161
fn build_ctx(&self) -> Ctx<'a> {
140162
let db = self.source_scope.db;
141163
let target_module = self.target_scope.module();
@@ -163,7 +185,7 @@ impl<'a> PathTransform<'a> {
163185
.for_each(|(k, v)| match (k.split(db), v) {
164186
(Either::Right(k), Some(TypeOrConst::Either(v))) => {
165187
if let Some(ty) = v.ty() {
166-
type_substs.insert(k, ty);
188+
type_substs.insert(k, self.prettify_target_ast(ty));
167189
}
168190
}
169191
(Either::Right(k), None) => {
@@ -178,7 +200,7 @@ impl<'a> PathTransform<'a> {
178200
}
179201
(Either::Left(k), Some(TypeOrConst::Either(v))) => {
180202
if let Some(ty) = v.ty() {
181-
const_substs.insert(k, ty.syntax().clone());
203+
const_substs.insert(k, self.prettify_target_node(ty.syntax().clone()));
182204
}
183205
}
184206
(Either::Left(k), Some(TypeOrConst::Const(v))) => {
@@ -189,7 +211,7 @@ impl<'a> PathTransform<'a> {
189211
// and sometimes require slight modifications; see
190212
// https://doc.rust-lang.org/reference/statements.html#expression-statements
191213
// (default values in curly brackets can cause the same problem)
192-
const_substs.insert(k, expr.syntax().clone());
214+
const_substs.insert(k, self.prettify_target_node(expr.syntax().clone()));
193215
}
194216
}
195217
(Either::Left(k), None) => {
@@ -204,6 +226,7 @@ impl<'a> PathTransform<'a> {
204226
}
205227
_ => (), // ignore mismatching params
206228
});
229+
// No need to prettify lifetimes, there's nothing to prettify.
207230
let lifetime_substs: FxHashMap<_, _> = self
208231
.generic_def
209232
.into_iter()

0 commit comments

Comments
 (0)