Skip to content

Commit 37ff07e

Browse files
committed
fix(generate_module): generate new impl near its ADT
1 parent d0f2db3 commit 37ff07e

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use hir::{HasSource, HirDisplay, Module, Semantics, TypeInfo};
1+
use hir::{Adt, HasSource, HirDisplay, Module, Semantics, TypeInfo};
22
use ide_db::{
33
base_db::FileId,
44
defs::{Definition, NameRefClass},
@@ -145,7 +145,8 @@ fn gen_method(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
145145
return None;
146146
}
147147
let (impl_, file) = get_adt_source(ctx, &adt, fn_name.text().as_str())?;
148-
let (target, insert_offset) = get_method_target(ctx, &target_module, &impl_)?;
148+
let (target, insert_offset) = get_method_target(ctx, &impl_, &adt)?;
149+
149150
let function_builder =
150151
FunctionBuilder::from_method_call(ctx, &call, &fn_name, target_module, target)?;
151152
let text_range = call.syntax().text_range();
@@ -418,14 +419,13 @@ fn get_fn_target(
418419

419420
fn get_method_target(
420421
ctx: &AssistContext<'_>,
421-
target_module: &Module,
422422
impl_: &Option<ast::Impl>,
423+
adt: &Adt,
423424
) -> Option<(GeneratedFunctionTarget, TextSize)> {
424425
let target = match impl_ {
425426
Some(impl_) => next_space_for_fn_in_impl(impl_)?,
426427
None => {
427-
next_space_for_fn_in_module(ctx.sema.db, &target_module.definition_source(ctx.sema.db))?
428-
.1
428+
GeneratedFunctionTarget::BehindItem(adt.source(ctx.sema.db)?.syntax().value.clone())
429429
}
430430
};
431431
Some((target.clone(), get_insert_offset(&target)))
@@ -444,7 +444,7 @@ fn assoc_fn_target_info(
444444
return None;
445445
}
446446
let (impl_, file) = get_adt_source(ctx, &adt, fn_name)?;
447-
let (target, insert_offset) = get_method_target(ctx, &module, &impl_)?;
447+
let (target, insert_offset) = get_method_target(ctx, &impl_, &adt)?;
448448
let adt_name = if impl_.is_none() { Some(adt.name(ctx.sema.db)) } else { None };
449449
Some(TargetInfo::new(target_module, adt_name, target, file, insert_offset))
450450
}
@@ -1475,12 +1475,12 @@ fn foo() {S.bar$0();}
14751475
",
14761476
r"
14771477
struct S;
1478-
fn foo() {S.bar();}
14791478
impl S {
14801479
fn bar(&self) ${0:-> _} {
14811480
todo!()
14821481
}
14831482
}
1483+
fn foo() {S.bar();}
14841484
",
14851485
)
14861486
}
@@ -1547,16 +1547,16 @@ mod s {
15471547
",
15481548
r"
15491549
struct S;
1550-
mod s {
1551-
fn foo() {
1552-
super::S.bar();
1553-
}
1554-
}
15551550
impl S {
15561551
fn bar(&self) ${0:-> _} {
15571552
todo!()
15581553
}
15591554
}
1555+
mod s {
1556+
fn foo() {
1557+
super::S.bar();
1558+
}
1559+
}
15601560
15611561
",
15621562
)
@@ -1572,12 +1572,12 @@ fn foo() {$0S.bar();}
15721572
",
15731573
r"
15741574
struct S;
1575-
fn foo() {S.bar();}
15761575
impl S {
15771576
fn bar(&self) ${0:-> _} {
15781577
todo!()
15791578
}
15801579
}
1580+
fn foo() {S.bar();}
15811581
",
15821582
)
15831583
}
@@ -1592,12 +1592,12 @@ fn foo() {S::bar$0();}
15921592
",
15931593
r"
15941594
struct S;
1595-
fn foo() {S::bar();}
15961595
impl S {
15971596
fn bar() ${0:-> _} {
15981597
todo!()
15991598
}
16001599
}
1600+
fn foo() {S::bar();}
16011601
",
16021602
)
16031603
}
@@ -1659,12 +1659,12 @@ fn foo() {$0S::bar();}
16591659
",
16601660
r"
16611661
struct S;
1662-
fn foo() {S::bar();}
16631662
impl S {
16641663
fn bar() ${0:-> _} {
16651664
todo!()
16661665
}
16671666
}
1667+
fn foo() {S::bar();}
16681668
",
16691669
)
16701670
}
@@ -1834,14 +1834,14 @@ fn main() {
18341834
",
18351835
r"
18361836
enum Foo {}
1837-
fn main() {
1838-
Foo::new();
1839-
}
18401837
impl Foo {
18411838
fn new() ${0:-> _} {
18421839
todo!()
18431840
}
18441841
}
1842+
fn main() {
1843+
Foo::new();
1844+
}
18451845
",
18461846
)
18471847
}

0 commit comments

Comments
 (0)