mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
feat: Generate static method using Self::assoc() syntax
This change improves the `generate_function` assist to support generating static methods/associated functions using the `Self::assoc()` syntax. Previously, one could generate a static method, but only when specifying the type name directly (like `Foo::assoc()`). After this change, `Self` is supported as well as the type name. Fixes #13012
This commit is contained in:
parent
baa2cccb24
commit
39d17efde7
1 changed files with 53 additions and 11 deletions
|
@ -61,7 +61,7 @@ fn gen_fn(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let fn_name = &*name_ref.text();
|
let fn_name = &*name_ref.text();
|
||||||
let target_module;
|
let mut target_module = None;
|
||||||
let mut adt_name = None;
|
let mut adt_name = None;
|
||||||
|
|
||||||
let (target, file, insert_offset) = match path.qualifier() {
|
let (target, file, insert_offset) = match path.qualifier() {
|
||||||
|
@ -78,16 +78,11 @@ fn gen_fn(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let current_module = ctx.sema.scope(call.syntax())?.module();
|
static_method_target(ctx, &call, adt, &mut target_module, fn_name, &mut adt_name)?
|
||||||
let module = adt.module(ctx.sema.db);
|
|
||||||
target_module = if current_module == module { None } else { Some(module) };
|
|
||||||
if current_module.krate() != module.krate() {
|
|
||||||
return None;
|
|
||||||
}
|
}
|
||||||
let (impl_, file) = get_adt_source(ctx, &adt, fn_name)?;
|
Some(hir::PathResolution::SelfType(impl_)) => {
|
||||||
let (target, insert_offset) = get_method_target(ctx, &module, &impl_)?;
|
let adt = impl_.self_ty(ctx.db()).as_adt()?;
|
||||||
adt_name = if impl_.is_none() { Some(adt.name(ctx.sema.db)) } else { None };
|
static_method_target(ctx, &call, adt, &mut target_module, fn_name, &mut adt_name)?
|
||||||
(target, file, insert_offset)
|
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return None;
|
return None;
|
||||||
|
@ -399,6 +394,26 @@ fn get_method_target(
|
||||||
Some((target.clone(), get_insert_offset(&target)))
|
Some((target.clone(), get_insert_offset(&target)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn static_method_target(
|
||||||
|
ctx: &AssistContext<'_>,
|
||||||
|
call: &CallExpr,
|
||||||
|
adt: hir::Adt,
|
||||||
|
target_module: &mut Option<Module>,
|
||||||
|
fn_name: &str,
|
||||||
|
adt_name: &mut Option<hir::Name>,
|
||||||
|
) -> Option<(GeneratedFunctionTarget, FileId, TextSize)> {
|
||||||
|
let current_module = ctx.sema.scope(call.syntax())?.module();
|
||||||
|
let module = adt.module(ctx.sema.db);
|
||||||
|
*target_module = if current_module == module { None } else { Some(module) };
|
||||||
|
if current_module.krate() != module.krate() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let (impl_, file) = get_adt_source(ctx, &adt, fn_name)?;
|
||||||
|
let (target, insert_offset) = get_method_target(ctx, &module, &impl_)?;
|
||||||
|
*adt_name = if impl_.is_none() { Some(adt.name(ctx.sema.db)) } else { None };
|
||||||
|
Some((target, file, insert_offset))
|
||||||
|
}
|
||||||
|
|
||||||
fn get_insert_offset(target: &GeneratedFunctionTarget) -> TextSize {
|
fn get_insert_offset(target: &GeneratedFunctionTarget) -> TextSize {
|
||||||
match &target {
|
match &target {
|
||||||
GeneratedFunctionTarget::BehindItem(it) => it.text_range().end(),
|
GeneratedFunctionTarget::BehindItem(it) => it.text_range().end(),
|
||||||
|
@ -1625,6 +1640,33 @@ fn foo() {S::bar();}
|
||||||
impl S {
|
impl S {
|
||||||
|
|
||||||
|
|
||||||
|
fn bar() ${0:-> _} {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn create_static_method_within_an_impl_with_self_syntax() {
|
||||||
|
check_assist(
|
||||||
|
generate_function,
|
||||||
|
r"
|
||||||
|
struct S;
|
||||||
|
impl S {
|
||||||
|
fn foo(&self) {
|
||||||
|
Self::bar$0();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
",
|
||||||
|
r"
|
||||||
|
struct S;
|
||||||
|
impl S {
|
||||||
|
fn foo(&self) {
|
||||||
|
Self::bar();
|
||||||
|
}
|
||||||
|
|
||||||
fn bar() ${0:-> _} {
|
fn bar() ${0:-> _} {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue