Skip to content

Commit 79c70d0

Browse files
committed
Simplify
1 parent 1a50f90 commit 79c70d0

File tree

1 file changed

+46
-60
lines changed

1 file changed

+46
-60
lines changed

crates/ide_assists/src/handlers/inline_call.rs

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -100,31 +100,7 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext) -> Opt
100100
ast::NameLike::NameRef(name_ref) => Some(name_ref),
101101
_ => None,
102102
});
103-
let call_infos = name_refs.filter_map(|name_ref| {
104-
let parent = name_ref.syntax().parent()?;
105-
if let Some(call) = ast::MethodCallExpr::cast(parent.clone()) {
106-
let receiver = call.receiver()?;
107-
let mut arguments = vec![receiver];
108-
arguments.extend(call.arg_list()?.args());
109-
Some(CallInfo {
110-
generic_arg_list: call.generic_arg_list(),
111-
node: CallExprNode::MethodCallExpr(call),
112-
arguments,
113-
})
114-
} else if let Some(segment) = ast::PathSegment::cast(parent) {
115-
let path = segment.syntax().parent().and_then(ast::Path::cast)?;
116-
let path = path.syntax().parent().and_then(ast::PathExpr::cast)?;
117-
let call = path.syntax().parent().and_then(ast::CallExpr::cast)?;
118-
119-
Some(CallInfo {
120-
arguments: call.arg_list()?.args().collect(),
121-
node: CallExprNode::Call(call),
122-
generic_arg_list: segment.generic_arg_list(),
123-
})
124-
} else {
125-
None
126-
}
127-
});
103+
let call_infos = name_refs.filter_map(CallInfo::from_name_ref);
128104
let replaced = call_infos
129105
.map(|call_info| {
130106
let replacement =
@@ -178,43 +154,25 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext) -> Opt
178154
// }
179155
// ```
180156
pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
181-
let (label, function, call_info) =
182-
if let Some(path_expr) = ctx.find_node_at_offset::<ast::PathExpr>() {
183-
// FIXME make applicable only on nameref
184-
let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;
185-
let path = path_expr.path()?;
186-
157+
let name_ref: ast::NameRef = ctx.find_node_at_offset()?;
158+
let call_info = CallInfo::from_name_ref(name_ref.clone())?;
159+
let (function, label) = match &call_info.node {
160+
CallExprNode::Call(call) => {
161+
let path = match call.expr()? {
162+
ast::Expr::PathExpr(path) => path.path(),
163+
_ => None,
164+
}?;
187165
let function = match ctx.sema.resolve_path(&path)? {
188-
PathResolution::Def(hir::ModuleDef::Function(f))
189-
| PathResolution::AssocItem(hir::AssocItem::Function(f)) => f,
166+
PathResolution::Def(hir::ModuleDef::Function(f)) => f,
167+
PathResolution::AssocItem(hir::AssocItem::Function(f)) => f,
190168
_ => return None,
191169
};
192-
(
193-
format!("Inline `{}`", path),
194-
function,
195-
CallInfo {
196-
arguments: call.arg_list()?.args().collect(),
197-
node: CallExprNode::Call(call),
198-
generic_arg_list: path.segment().and_then(|it| it.generic_arg_list()),
199-
},
200-
)
201-
} else {
202-
let name_ref: ast::NameRef = ctx.find_node_at_offset()?;
203-
let call = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
204-
let receiver = call.receiver()?;
205-
let function = ctx.sema.resolve_method_call(&call)?;
206-
let mut arguments = vec![receiver];
207-
arguments.extend(call.arg_list()?.args());
208-
(
209-
format!("Inline `{}`", name_ref),
210-
function,
211-
CallInfo {
212-
generic_arg_list: call.generic_arg_list(),
213-
node: CallExprNode::MethodCallExpr(call),
214-
arguments,
215-
},
216-
)
217-
};
170+
(function, format!("Inline `{}`", path))
171+
}
172+
CallExprNode::MethodCallExpr(call) => {
173+
(ctx.sema.resolve_method_call(call)?, format!("Inline `{}`", name_ref))
174+
}
175+
};
218176

219177
let fn_source = function.source(ctx.db())?;
220178
let fn_body = fn_source.value.body()?;
@@ -273,6 +231,34 @@ struct CallInfo {
273231
generic_arg_list: Option<ast::GenericArgList>,
274232
}
275233

234+
impl CallInfo {
235+
fn from_name_ref(name_ref: ast::NameRef) -> Option<CallInfo> {
236+
let parent = name_ref.syntax().parent()?;
237+
if let Some(call) = ast::MethodCallExpr::cast(parent.clone()) {
238+
let receiver = call.receiver()?;
239+
let mut arguments = vec![receiver];
240+
arguments.extend(call.arg_list()?.args());
241+
Some(CallInfo {
242+
generic_arg_list: call.generic_arg_list(),
243+
node: CallExprNode::MethodCallExpr(call),
244+
arguments,
245+
})
246+
} else if let Some(segment) = ast::PathSegment::cast(parent) {
247+
let path = segment.syntax().parent().and_then(ast::Path::cast)?;
248+
let path = path.syntax().parent().and_then(ast::PathExpr::cast)?;
249+
let call = path.syntax().parent().and_then(ast::CallExpr::cast)?;
250+
251+
Some(CallInfo {
252+
arguments: call.arg_list()?.args().collect(),
253+
node: CallExprNode::Call(call),
254+
generic_arg_list: segment.generic_arg_list(),
255+
})
256+
} else {
257+
None
258+
}
259+
}
260+
}
261+
276262
fn get_fn_params(
277263
db: &dyn HirDatabase,
278264
function: hir::Function,
@@ -860,7 +846,7 @@ fn foo<T, const N: usize>() {
860846
fn bar<U, const M: usize>() {}
861847
862848
fn main() {
863-
foo::<usize, {0}>$0();
849+
foo$0::<usize, {0}>();
864850
}
865851
"#,
866852
r#"

0 commit comments

Comments
 (0)