Skip to content

Commit e2c233a

Browse files
committed
simplify
1 parent 13bbed7 commit e2c233a

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

crates/ide_assists/src/handlers/generate_function.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use hir::{HasSource, HirDisplay, Module, TypeInfo};
1+
use hir::{HasSource, HirDisplay, InFile, Module, TypeInfo};
22
use ide_db::{base_db::FileId, helpers::SnippetCap};
33
use rustc_hash::{FxHashMap, FxHashSet};
44
use stdx::to_lower_snake_case;
@@ -109,7 +109,7 @@ fn gen_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
109109
let fn_name = call.name_ref()?;
110110
let adt = ctx.sema.type_of_expr(&call.receiver()?)?.original().strip_references().as_adt()?;
111111

112-
let current_module = ctx.sema.scope(call.syntax()).module()?;
112+
let current_module = current_module(call.syntax(), ctx)?;
113113
let target_module = adt.module(ctx.sema.db);
114114

115115
if current_module.krate() != target_module.krate() {
@@ -152,6 +152,22 @@ fn gen_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
152152
)
153153
}
154154

155+
fn get_impl(
156+
adt: InFile<&SyntaxNode>,
157+
fn_name: &ast::NameRef,
158+
ctx: &AssistContext,
159+
) -> Option<(Option<ast::Impl>, FileId)> {
160+
let file = adt.file_id.original_file(ctx.sema.db);
161+
let adt = adt.value;
162+
let adt = ast::Adt::cast(adt.clone())?;
163+
let r = find_struct_impl(ctx, &adt, fn_name.text().as_str())?;
164+
Some((r, file))
165+
}
166+
167+
fn current_module(current_node: &SyntaxNode, ctx: &AssistContext) -> Option<Module> {
168+
ctx.sema.scope(current_node).module()
169+
}
170+
155171
struct FunctionTemplate {
156172
insert_offset: TextSize,
157173
leading_ws: String,
@@ -218,7 +234,7 @@ impl FunctionBuilder {
218234
None => next_space_for_fn_after_call_site(FuncExpr::Func(call.clone()))?,
219235
};
220236
let needs_pub = target_module.is_some();
221-
let target_module = target_module.or_else(|| ctx.sema.scope(target.syntax()).module())?;
237+
let target_module = target_module.or_else(|| current_module(target.syntax(), ctx))?;
222238
let fn_name = fn_name(path)?;
223239
let (type_params, params) = fn_args(ctx, target_module, FuncExpr::Func(call.clone()))?;
224240

@@ -250,8 +266,6 @@ impl FunctionBuilder {
250266
target_module: Module,
251267
current_module: Module,
252268
) -> Option<Self> {
253-
// let mut file = ctx.frange.file_id;
254-
// let target_module = ctx.sema.scope(call.syntax()).module()?;
255269
let target = match impl_ {
256270
Some(impl_) => next_space_for_fn_in_impl(&impl_)?,
257271
None => {

0 commit comments

Comments
 (0)