Skip to content

Commit 7e768cb

Browse files
rmehri01Veykril
authored andcommitted
fix: prefer keeping Self if it is in the same impl def
1 parent f4349ff commit 7e768cb

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

crates/hir-def/src/resolver.rs

+8
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,14 @@ impl Resolver {
588588
_ => None,
589589
})
590590
}
591+
592+
pub fn impl_def(&self) -> Option<ImplId> {
593+
self.scopes().find_map(|scope| match scope {
594+
Scope::ImplDefScope(def) => Some(*def),
595+
_ => None,
596+
})
597+
}
598+
591599
/// `expr_id` is required to be an expression id that comes after the top level expression scope in the given resolver
592600
#[must_use]
593601
pub fn update_to_inner_scope(

crates/hir/src/semantics.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,10 @@ impl SemanticsScope<'_> {
15561556
pub fn extern_crate_decls(&self) -> impl Iterator<Item = Name> + '_ {
15571557
self.resolver.extern_crate_decls_in_scope(self.db.upcast())
15581558
}
1559+
1560+
pub fn has_same_self_type(&self, other: &SemanticsScope<'_>) -> bool {
1561+
self.resolver.impl_def() == other.resolver.impl_def()
1562+
}
15591563
}
15601564

15611565
#[derive(Debug)]

crates/ide-assists/src/handlers/generate_delegate_methods.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashSet;
22

3-
use hir::{self, HasCrate, HasSource, HasVisibility};
3+
use hir::{self, HasCrate, HasVisibility};
44
use ide_db::path_transform::PathTransform;
55
use syntax::{
66
ast::{
@@ -106,11 +106,8 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
106106
target,
107107
|edit| {
108108
// Create the function
109-
let method_source = match method.source(ctx.db()) {
110-
Some(source) => {
111-
ctx.sema.parse_or_expand(source.file_id);
112-
source.value
113-
}
109+
let method_source = match ctx.sema.source(method) {
110+
Some(source) => source.value,
114111
None => return,
115112
};
116113
let vis = method_source.visibility();
@@ -187,11 +184,11 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
187184
let assoc_items = impl_def.get_or_create_assoc_item_list();
188185
assoc_items.add_item(f.clone().into());
189186

190-
PathTransform::generic_transformation(
191-
&ctx.sema.scope(strukt.syntax()).unwrap(),
192-
&ctx.sema.scope(method_source.syntax()).unwrap(),
193-
)
194-
.apply(f.syntax());
187+
if let Some((target, source)) =
188+
ctx.sema.scope(strukt.syntax()).zip(ctx.sema.scope(method_source.syntax()))
189+
{
190+
PathTransform::generic_transformation(&target, &source).apply(f.syntax());
191+
}
195192

196193
if let Some(cap) = ctx.config.snippet_cap {
197194
edit.add_tabstop_before(cap, f)

crates/ide-db/src/path_transform.rs

+8
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ impl<'a> PathTransform<'a> {
183183
lifetime_substs,
184184
target_module,
185185
source_scope: self.source_scope,
186+
same_self_type: self.target_scope.has_same_self_type(self.source_scope),
186187
};
187188
ctx.transform_default_values(defaulted_params);
188189
ctx
@@ -195,6 +196,7 @@ struct Ctx<'a> {
195196
lifetime_substs: FxHashMap<LifetimeName, ast::Lifetime>,
196197
target_module: hir::Module,
197198
source_scope: &'a SemanticsScope<'a>,
199+
same_self_type: bool,
198200
}
199201

200202
fn postorder(item: &SyntaxNode) -> impl Iterator<Item = SyntaxNode> {
@@ -333,6 +335,11 @@ impl Ctx<'_> {
333335
}
334336
}
335337
hir::PathResolution::SelfType(imp) => {
338+
// keep Self type if it does not need to be replaced
339+
if self.same_self_type {
340+
return None;
341+
}
342+
336343
let ty = imp.self_ty(self.source_scope.db);
337344
let ty_str = &ty
338345
.display_source_code(
@@ -349,6 +356,7 @@ impl Ctx<'_> {
349356
self.source_scope.db.upcast(),
350357
ModuleDef::from(adt),
351358
false,
359+
true,
352360
)?;
353361

354362
if let Some(qual) = mod_path_to_ast(&found_path).qualifier() {

0 commit comments

Comments
 (0)