Skip to content

Commit 99570f3

Browse files
committed
refactor: use single next space
1 parent b777e49 commit 99570f3

File tree

1 file changed

+10
-25
lines changed

1 file changed

+10
-25
lines changed

crates/ide_assists/src/handlers/generate_function.rs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ impl<'a> FuncExpr<'a> {
2929
FuncExpr::Method(m_call) => m_call.arg_list(),
3030
}
3131
}
32+
33+
fn syntax(&self) -> &SyntaxNode {
34+
match *self {
35+
FuncExpr::Func(fn_call) => fn_call.syntax(),
36+
FuncExpr::Method(m_call) => m_call.syntax(),
37+
}
38+
}
3239
}
3340

3441
// Assist: generate_function
@@ -173,7 +180,7 @@ impl FunctionBuilder {
173180
file = in_file;
174181
target
175182
}
176-
None => next_space_for_fn_after_call_site(call)?,
183+
None => next_space_for_fn_after_call_site(FuncExpr::Func(call))?,
177184
};
178185
let needs_pub = target_module.is_some();
179186
let target_module = target_module.or_else(|| ctx.sema.scope(target.syntax()).module())?;
@@ -238,7 +245,7 @@ impl FunctionBuilder {
238245
file = in_file;
239246
target
240247
}
241-
None => next_space_for_fn_after_method_call_site(call)?,
248+
None => next_space_for_fn_after_call_site(FuncExpr::Method(call))?,
242249
};
243250
let needs_pub = false;
244251
let target_module = target_module.or_else(|| ctx.sema.scope(target.syntax()).module())?;
@@ -465,29 +472,7 @@ fn fn_arg_type(
465472
/// directly after the current block
466473
/// We want to write the generated function directly after
467474
/// fns, impls or macro calls, but inside mods
468-
fn next_space_for_fn_after_call_site(expr: &ast::CallExpr) -> Option<GeneratedFunctionTarget> {
469-
let mut ancestors = expr.syntax().ancestors().peekable();
470-
let mut last_ancestor: Option<SyntaxNode> = None;
471-
while let Some(next_ancestor) = ancestors.next() {
472-
match next_ancestor.kind() {
473-
SyntaxKind::SOURCE_FILE => {
474-
break;
475-
}
476-
SyntaxKind::ITEM_LIST => {
477-
if ancestors.peek().map(|a| a.kind()) == Some(SyntaxKind::MODULE) {
478-
break;
479-
}
480-
}
481-
_ => {}
482-
}
483-
last_ancestor = Some(next_ancestor);
484-
}
485-
last_ancestor.map(GeneratedFunctionTarget::BehindItem)
486-
}
487-
488-
fn next_space_for_fn_after_method_call_site(
489-
expr: &ast::MethodCallExpr,
490-
) -> Option<GeneratedFunctionTarget> {
475+
fn next_space_for_fn_after_call_site(expr: FuncExpr) -> Option<GeneratedFunctionTarget> {
491476
let mut ancestors = expr.syntax().ancestors().peekable();
492477
let mut last_ancestor: Option<SyntaxNode> = None;
493478
while let Some(next_ancestor) = ancestors.next() {

0 commit comments

Comments
 (0)