fix(generate_module): generate new impl near its ADT

This commit is contained in:
feniljain 2022-09-15 19:33:19 +05:30
parent d0f2db3bf4
commit 37ff07e1ff

View File

@ -1,4 +1,4 @@
use hir::{HasSource, HirDisplay, Module, Semantics, TypeInfo}; use hir::{Adt, HasSource, HirDisplay, Module, Semantics, TypeInfo};
use ide_db::{ use ide_db::{
base_db::FileId, base_db::FileId,
defs::{Definition, NameRefClass}, defs::{Definition, NameRefClass},
@ -145,7 +145,8 @@ fn gen_method(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
return None; return None;
} }
let (impl_, file) = get_adt_source(ctx, &adt, fn_name.text().as_str())?; let (impl_, file) = get_adt_source(ctx, &adt, fn_name.text().as_str())?;
let (target, insert_offset) = get_method_target(ctx, &target_module, &impl_)?; let (target, insert_offset) = get_method_target(ctx, &impl_, &adt)?;
let function_builder = let function_builder =
FunctionBuilder::from_method_call(ctx, &call, &fn_name, target_module, target)?; FunctionBuilder::from_method_call(ctx, &call, &fn_name, target_module, target)?;
let text_range = call.syntax().text_range(); let text_range = call.syntax().text_range();
@ -418,14 +419,13 @@ fn get_fn_target(
fn get_method_target( fn get_method_target(
ctx: &AssistContext<'_>, ctx: &AssistContext<'_>,
target_module: &Module,
impl_: &Option<ast::Impl>, impl_: &Option<ast::Impl>,
adt: &Adt,
) -> Option<(GeneratedFunctionTarget, TextSize)> { ) -> Option<(GeneratedFunctionTarget, TextSize)> {
let target = match impl_ { let target = match impl_ {
Some(impl_) => next_space_for_fn_in_impl(impl_)?, Some(impl_) => next_space_for_fn_in_impl(impl_)?,
None => { None => {
next_space_for_fn_in_module(ctx.sema.db, &target_module.definition_source(ctx.sema.db))? GeneratedFunctionTarget::BehindItem(adt.source(ctx.sema.db)?.syntax().value.clone())
.1
} }
}; };
Some((target.clone(), get_insert_offset(&target))) Some((target.clone(), get_insert_offset(&target)))
@ -444,7 +444,7 @@ fn assoc_fn_target_info(
return None; return None;
} }
let (impl_, file) = get_adt_source(ctx, &adt, fn_name)?; let (impl_, file) = get_adt_source(ctx, &adt, fn_name)?;
let (target, insert_offset) = get_method_target(ctx, &module, &impl_)?; let (target, insert_offset) = get_method_target(ctx, &impl_, &adt)?;
let adt_name = if impl_.is_none() { Some(adt.name(ctx.sema.db)) } else { None }; let adt_name = if impl_.is_none() { Some(adt.name(ctx.sema.db)) } else { None };
Some(TargetInfo::new(target_module, adt_name, target, file, insert_offset)) Some(TargetInfo::new(target_module, adt_name, target, file, insert_offset))
} }
@ -1475,12 +1475,12 @@ fn foo() {S.bar$0();}
", ",
r" r"
struct S; struct S;
fn foo() {S.bar();}
impl S { impl S {
fn bar(&self) ${0:-> _} { fn bar(&self) ${0:-> _} {
todo!() todo!()
} }
} }
fn foo() {S.bar();}
", ",
) )
} }
@ -1547,16 +1547,16 @@ mod s {
", ",
r" r"
struct S; struct S;
mod s {
fn foo() {
super::S.bar();
}
}
impl S { impl S {
fn bar(&self) ${0:-> _} { fn bar(&self) ${0:-> _} {
todo!() todo!()
} }
} }
mod s {
fn foo() {
super::S.bar();
}
}
", ",
) )
@ -1572,12 +1572,12 @@ fn foo() {$0S.bar();}
", ",
r" r"
struct S; struct S;
fn foo() {S.bar();}
impl S { impl S {
fn bar(&self) ${0:-> _} { fn bar(&self) ${0:-> _} {
todo!() todo!()
} }
} }
fn foo() {S.bar();}
", ",
) )
} }
@ -1592,12 +1592,12 @@ fn foo() {S::bar$0();}
", ",
r" r"
struct S; struct S;
fn foo() {S::bar();}
impl S { impl S {
fn bar() ${0:-> _} { fn bar() ${0:-> _} {
todo!() todo!()
} }
} }
fn foo() {S::bar();}
", ",
) )
} }
@ -1659,12 +1659,12 @@ fn foo() {$0S::bar();}
", ",
r" r"
struct S; struct S;
fn foo() {S::bar();}
impl S { impl S {
fn bar() ${0:-> _} { fn bar() ${0:-> _} {
todo!() todo!()
} }
} }
fn foo() {S::bar();}
", ",
) )
} }
@ -1834,14 +1834,14 @@ fn main() {
", ",
r" r"
enum Foo {} enum Foo {}
fn main() {
Foo::new();
}
impl Foo { impl Foo {
fn new() ${0:-> _} { fn new() ${0:-> _} {
todo!() todo!()
} }
} }
fn main() {
Foo::new();
}
", ",
) )
} }