|
1 |
| -use hir::{HasSource, HirDisplay, Module, TypeInfo}; |
| 1 | +use hir::{HasSource, HirDisplay, InFile, Module, TypeInfo}; |
2 | 2 | use ide_db::{base_db::FileId, helpers::SnippetCap};
|
3 | 3 | use rustc_hash::{FxHashMap, FxHashSet};
|
4 | 4 | use stdx::to_lower_snake_case;
|
@@ -109,7 +109,7 @@ fn gen_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
109 | 109 | let fn_name = call.name_ref()?;
|
110 | 110 | let adt = ctx.sema.type_of_expr(&call.receiver()?)?.original().strip_references().as_adt()?;
|
111 | 111 |
|
112 |
| - let current_module = ctx.sema.scope(call.syntax()).module()?; |
| 112 | + let current_module = current_module(call.syntax(), ctx)?; |
113 | 113 | let target_module = adt.module(ctx.sema.db);
|
114 | 114 |
|
115 | 115 | if current_module.krate() != target_module.krate() {
|
@@ -152,6 +152,22 @@ fn gen_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
152 | 152 | )
|
153 | 153 | }
|
154 | 154 |
|
| 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 | + |
155 | 171 | struct FunctionTemplate {
|
156 | 172 | insert_offset: TextSize,
|
157 | 173 | leading_ws: String,
|
@@ -218,7 +234,7 @@ impl FunctionBuilder {
|
218 | 234 | None => next_space_for_fn_after_call_site(FuncExpr::Func(call.clone()))?,
|
219 | 235 | };
|
220 | 236 | 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))?; |
222 | 238 | let fn_name = fn_name(path)?;
|
223 | 239 | let (type_params, params) = fn_args(ctx, target_module, FuncExpr::Func(call.clone()))?;
|
224 | 240 |
|
@@ -250,8 +266,6 @@ impl FunctionBuilder {
|
250 | 266 | target_module: Module,
|
251 | 267 | current_module: Module,
|
252 | 268 | ) -> Option<Self> {
|
253 |
| - // let mut file = ctx.frange.file_id; |
254 |
| - // let target_module = ctx.sema.scope(call.syntax()).module()?; |
255 | 269 | let target = match impl_ {
|
256 | 270 | Some(impl_) => next_space_for_fn_in_impl(&impl_)?,
|
257 | 271 | None => {
|
|
0 commit comments