Skip to content

Commit ebe6c38

Browse files
bors[bot]Veykril
andauthored
Merge #10438
10438: minor: Simplify r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 0618a6f + 0943c4b commit ebe6c38

File tree

7 files changed

+91
-143
lines changed

7 files changed

+91
-143
lines changed

crates/ide/src/call_hierarchy.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use indexmap::IndexMap;
44

55
use hir::Semantics;
66
use ide_db::{
7-
call_info::FnCallNode,
87
defs::{Definition, NameClass, NameRefClass},
98
helpers::pick_best_token,
109
search::FileReference,
@@ -101,23 +100,27 @@ pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Optio
101100
_ => None,
102101
})
103102
.flatten()
104-
.filter_map(|node| FnCallNode::with_node_exact(&node))
103+
.filter_map(ast::CallableExpr::cast)
105104
.filter_map(|call_node| {
106-
let name_ref = call_node.name_ref()?;
107-
let func_target = match call_node {
108-
FnCallNode::CallExpr(expr) => {
109-
let callable = sema.type_of_expr(&expr.expr()?)?.original.as_callable(db)?;
105+
let (nav_target, range) = match call_node {
106+
ast::CallableExpr::Call(call) => {
107+
let expr = call.expr()?;
108+
let callable = sema.type_of_expr(&expr)?.original.as_callable(db)?;
110109
match callable.kind() {
111-
hir::CallableKind::Function(it) => it.try_to_nav(db),
110+
hir::CallableKind::Function(it) => {
111+
let range = expr.syntax().text_range();
112+
it.try_to_nav(db).zip(Some(range))
113+
}
112114
_ => None,
113115
}
114116
}
115-
FnCallNode::MethodCallExpr(expr) => {
117+
ast::CallableExpr::MethodCall(expr) => {
118+
let range = expr.name_ref()?.syntax().text_range();
116119
let function = sema.resolve_method_call(&expr)?;
117-
function.try_to_nav(db)
120+
function.try_to_nav(db).zip(Some(range))
118121
}
119122
}?;
120-
Some((func_target, name_ref.syntax().text_range()))
123+
Some((nav_target, range))
121124
})
122125
.for_each(|(nav, range)| calls.add(nav, range));
123126

crates/ide_assists/src/handlers/generate_function.rs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,6 @@ pub(crate) fn generate_function(acc: &mut Assists, ctx: &AssistContext) -> Optio
5151
gen_fn(acc, ctx).or_else(|| gen_method(acc, ctx))
5252
}
5353

54-
enum FuncExpr {
55-
Func(ast::CallExpr),
56-
Method(ast::MethodCallExpr),
57-
}
58-
59-
impl FuncExpr {
60-
fn arg_list(&self) -> Option<ast::ArgList> {
61-
match self {
62-
FuncExpr::Func(fn_call) => fn_call.arg_list(),
63-
FuncExpr::Method(m_call) => m_call.arg_list(),
64-
}
65-
}
66-
67-
fn syntax(&self) -> &SyntaxNode {
68-
match self {
69-
FuncExpr::Func(fn_call) => fn_call.syntax(),
70-
FuncExpr::Method(m_call) => m_call.syntax(),
71-
}
72-
}
73-
}
74-
7554
fn gen_fn(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
7655
let path_expr: ast::PathExpr = ctx.find_node_at_offset()?;
7756
let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;
@@ -254,7 +233,8 @@ impl FunctionBuilder {
254233
let needs_pub = target_module.is_some();
255234
let target_module = target_module.or_else(|| current_module(target.syntax(), ctx))?;
256235
let fn_name = make::name(fn_name);
257-
let (type_params, params) = fn_args(ctx, target_module, FuncExpr::Func(call.clone()))?;
236+
let (type_params, params) =
237+
fn_args(ctx, target_module, ast::CallableExpr::Call(call.clone()))?;
258238

259239
let await_expr = call.syntax().parent().and_then(ast::AwaitExpr::cast);
260240
let is_async = await_expr.is_some();
@@ -284,7 +264,8 @@ impl FunctionBuilder {
284264
let needs_pub =
285265
!module_is_descendant(&current_module(call.syntax(), ctx)?, &target_module, ctx);
286266
let fn_name = make::name(&name.text());
287-
let (type_params, params) = fn_args(ctx, target_module, FuncExpr::Method(call.clone()))?;
267+
let (type_params, params) =
268+
fn_args(ctx, target_module, ast::CallableExpr::MethodCall(call.clone()))?;
288269

289270
let await_expr = call.syntax().parent().and_then(ast::AwaitExpr::cast);
290271
let is_async = await_expr.is_some();
@@ -392,7 +373,7 @@ fn get_fn_target(
392373
file = in_file;
393374
target
394375
}
395-
None => next_space_for_fn_after_call_site(FuncExpr::Func(call))?,
376+
None => next_space_for_fn_after_call_site(ast::CallableExpr::Call(call))?,
396377
};
397378
Some((target.clone(), file, get_insert_offset(&target)))
398379
}
@@ -438,7 +419,7 @@ impl GeneratedFunctionTarget {
438419
fn fn_args(
439420
ctx: &AssistContext,
440421
target_module: hir::Module,
441-
call: FuncExpr,
422+
call: ast::CallableExpr,
442423
) -> Option<(Option<ast::GenericParamList>, ast::ParamList)> {
443424
let mut arg_names = Vec::new();
444425
let mut arg_types = Vec::new();
@@ -468,8 +449,8 @@ fn fn_args(
468449
None,
469450
make::param_list(
470451
match call {
471-
FuncExpr::Func(_) => None,
472-
FuncExpr::Method(_) => Some(make::self_param()),
452+
ast::CallableExpr::Call(_) => None,
453+
ast::CallableExpr::MethodCall(_) => Some(make::self_param()),
473454
},
474455
params,
475456
),
@@ -553,7 +534,7 @@ fn fn_arg_type(
553534
/// directly after the current block
554535
/// We want to write the generated function directly after
555536
/// fns, impls or macro calls, but inside mods
556-
fn next_space_for_fn_after_call_site(expr: FuncExpr) -> Option<GeneratedFunctionTarget> {
537+
fn next_space_for_fn_after_call_site(expr: ast::CallableExpr) -> Option<GeneratedFunctionTarget> {
557538
let mut ancestors = expr.syntax().ancestors().peekable();
558539
let mut last_ancestor: Option<SyntaxNode> = None;
559540
while let Some(next_ancestor) = ancestors.next() {

crates/ide_assists/src/handlers/inline_call.rs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ide_db::{
1212
use itertools::{izip, Itertools};
1313
use syntax::{
1414
ast::{self, edit_in_place::Indent, HasArgList, PathExpr},
15-
ted, AstNode, SyntaxNode,
15+
ted, AstNode,
1616
};
1717

1818
use crate::{
@@ -178,7 +178,7 @@ pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
178178
let name_ref: ast::NameRef = ctx.find_node_at_offset()?;
179179
let call_info = CallInfo::from_name_ref(name_ref.clone())?;
180180
let (function, label) = match &call_info.node {
181-
CallExprNode::Call(call) => {
181+
ast::CallableExpr::Call(call) => {
182182
let path = match call.expr()? {
183183
ast::Expr::PathExpr(path) => path.path(),
184184
_ => None,
@@ -190,7 +190,7 @@ pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
190190
};
191191
(function, format!("Inline `{}`", path))
192192
}
193-
CallExprNode::MethodCallExpr(call) => {
193+
ast::CallableExpr::MethodCall(call) => {
194194
(ctx.sema.resolve_method_call(call)?, format!("Inline `{}`", name_ref))
195195
}
196196
};
@@ -223,31 +223,17 @@ pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
223223

224224
builder.replace_ast(
225225
match call_info.node {
226-
CallExprNode::Call(it) => ast::Expr::CallExpr(it),
227-
CallExprNode::MethodCallExpr(it) => ast::Expr::MethodCallExpr(it),
226+
ast::CallableExpr::Call(it) => ast::Expr::CallExpr(it),
227+
ast::CallableExpr::MethodCall(it) => ast::Expr::MethodCallExpr(it),
228228
},
229229
replacement,
230230
);
231231
},
232232
)
233233
}
234234

235-
enum CallExprNode {
236-
Call(ast::CallExpr),
237-
MethodCallExpr(ast::MethodCallExpr),
238-
}
239-
240-
impl CallExprNode {
241-
fn syntax(&self) -> &SyntaxNode {
242-
match self {
243-
CallExprNode::Call(it) => it.syntax(),
244-
CallExprNode::MethodCallExpr(it) => it.syntax(),
245-
}
246-
}
247-
}
248-
249235
struct CallInfo {
250-
node: CallExprNode,
236+
node: ast::CallableExpr,
251237
arguments: Vec<ast::Expr>,
252238
generic_arg_list: Option<ast::GenericArgList>,
253239
}
@@ -261,7 +247,7 @@ impl CallInfo {
261247
arguments.extend(call.arg_list()?.args());
262248
Some(CallInfo {
263249
generic_arg_list: call.generic_arg_list(),
264-
node: CallExprNode::MethodCallExpr(call),
250+
node: ast::CallableExpr::MethodCall(call),
265251
arguments,
266252
})
267253
} else if let Some(segment) = ast::PathSegment::cast(parent) {
@@ -271,7 +257,7 @@ impl CallInfo {
271257

272258
Some(CallInfo {
273259
arguments: call.arg_list()?.args().collect(),
274-
node: CallExprNode::Call(call),
260+
node: ast::CallableExpr::Call(call),
275261
generic_arg_list: segment.generic_arg_list(),
276262
})
277263
} else {
@@ -367,8 +353,9 @@ fn inline(
367353
ted::replace(usage.syntax(), &replacement.syntax().clone_for_update());
368354
}
369355
};
370-
// izip confuses RA due to our lack of hygiene info currently losing us typeinfo
356+
// izip confuses RA due to our lack of hygiene info currently losing us type info causing incorrect errors
371357
let usages: &[ast::PathExpr] = &*usages;
358+
let expr: &ast::Expr = expr;
372359
match usages {
373360
// inline single use closure arguments
374361
[usage]
@@ -414,8 +401,8 @@ fn inline(
414401
}
415402

416403
let original_indentation = match node {
417-
CallExprNode::Call(it) => it.indent_level(),
418-
CallExprNode::MethodCallExpr(it) => it.indent_level(),
404+
ast::CallableExpr::Call(it) => it.indent_level(),
405+
ast::CallableExpr::MethodCall(it) => it.indent_level(),
419406
};
420407
body.reindent_to(original_indentation);
421408

crates/ide_db/src/call_info.rs

Lines changed: 17 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use stdx::format_to;
66
use syntax::{
77
algo,
88
ast::{self, HasArgList, HasName},
9-
match_ast, AstNode, Direction, SyntaxNode, SyntaxToken, TextRange, TextSize,
9+
AstNode, Direction, SyntaxToken, TextRange, TextSize,
1010
};
1111

1212
use crate::RootDatabase;
@@ -25,9 +25,11 @@ impl CallInfo {
2525
pub fn parameter_labels(&self) -> impl Iterator<Item = &str> + '_ {
2626
self.parameters.iter().map(move |&it| &self.signature[it])
2727
}
28+
2829
pub fn parameter_ranges(&self) -> &[TextRange] {
2930
&self.parameters
3031
}
32+
3133
fn push_param(&mut self, param: &str) {
3234
if !self.signature.ends_with('(') {
3335
self.signature.push_str(", ");
@@ -115,31 +117,24 @@ fn call_info_impl(
115117
token: SyntaxToken,
116118
) -> Option<(hir::Callable, Option<usize>)> {
117119
// Find the calling expression and it's NameRef
118-
let calling_node = FnCallNode::with_node(&token.parent()?)?;
120+
let parent = token.parent()?;
121+
let calling_node = parent.ancestors().filter_map(ast::CallableExpr::cast).find(|it| {
122+
it.arg_list()
123+
.map_or(false, |it| it.syntax().text_range().contains(token.text_range().start()))
124+
})?;
119125

120126
let callable = match &calling_node {
121-
FnCallNode::CallExpr(call) => {
122-
sema.type_of_expr(&call.expr()?)?.adjusted().as_callable(sema.db)?
127+
ast::CallableExpr::Call(call) => {
128+
let expr = call.expr()?;
129+
sema.type_of_expr(&expr)?.adjusted().as_callable(sema.db)
123130
}
124-
FnCallNode::MethodCallExpr(call) => sema.resolve_method_call_as_callable(call)?,
125-
};
131+
ast::CallableExpr::MethodCall(call) => sema.resolve_method_call_as_callable(call),
132+
}?;
126133
let active_param = if let Some(arg_list) = calling_node.arg_list() {
127-
// Number of arguments specified at the call site
128-
let num_args_at_callsite = arg_list.args().count();
129-
130-
let arg_list_range = arg_list.syntax().text_range();
131-
if !arg_list_range.contains_inclusive(token.text_range().start()) {
132-
cov_mark::hit!(call_info_bad_offset);
133-
return None;
134-
}
135-
let param = std::cmp::min(
136-
num_args_at_callsite,
137-
arg_list
138-
.args()
139-
.take_while(|arg| arg.syntax().text_range().end() <= token.text_range().start())
140-
.count(),
141-
);
142-
134+
let param = arg_list
135+
.args()
136+
.take_while(|arg| arg.syntax().text_range().end() <= token.text_range().start())
137+
.count();
143138
Some(param)
144139
} else {
145140
None
@@ -175,60 +170,5 @@ impl ActiveParameter {
175170
}
176171
}
177172

178-
#[derive(Debug)]
179-
pub enum FnCallNode {
180-
CallExpr(ast::CallExpr),
181-
MethodCallExpr(ast::MethodCallExpr),
182-
}
183-
184-
impl FnCallNode {
185-
fn with_node(syntax: &SyntaxNode) -> Option<FnCallNode> {
186-
syntax.ancestors().find_map(|node| {
187-
match_ast! {
188-
match node {
189-
ast::CallExpr(it) => Some(FnCallNode::CallExpr(it)),
190-
ast::MethodCallExpr(it) => {
191-
let arg_list = it.arg_list()?;
192-
if !arg_list.syntax().text_range().contains_range(syntax.text_range()) {
193-
return None;
194-
}
195-
Some(FnCallNode::MethodCallExpr(it))
196-
},
197-
_ => None,
198-
}
199-
}
200-
})
201-
}
202-
203-
pub fn with_node_exact(node: &SyntaxNode) -> Option<FnCallNode> {
204-
match_ast! {
205-
match node {
206-
ast::CallExpr(it) => Some(FnCallNode::CallExpr(it)),
207-
ast::MethodCallExpr(it) => Some(FnCallNode::MethodCallExpr(it)),
208-
_ => None,
209-
}
210-
}
211-
}
212-
213-
pub fn name_ref(&self) -> Option<ast::NameRef> {
214-
match self {
215-
FnCallNode::CallExpr(call_expr) => Some(match call_expr.expr()? {
216-
ast::Expr::PathExpr(path_expr) => path_expr.path()?.segment()?.name_ref()?,
217-
_ => return None,
218-
}),
219-
FnCallNode::MethodCallExpr(call_expr) => {
220-
call_expr.syntax().children().find_map(ast::NameRef::cast)
221-
}
222-
}
223-
}
224-
225-
fn arg_list(&self) -> Option<ast::ArgList> {
226-
match self {
227-
FnCallNode::CallExpr(expr) => expr.arg_list(),
228-
FnCallNode::MethodCallExpr(expr) => expr.arg_list(),
229-
}
230-
}
231-
}
232-
233173
#[cfg(test)]
234174
mod tests;

crates/ide_db/src/call_info/tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,6 @@ pub fn foo(mut r: WriteHandler<()>) {
362362

363363
#[test]
364364
fn call_info_bad_offset() {
365-
cov_mark::check!(call_info_bad_offset);
366365
check(
367366
r#"
368367
fn foo(x: u32, y: u32) -> u32 {x + y}

crates/syntax/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
};
1919

2020
pub use self::{
21-
expr_ext::{ArrayExprKind, BlockModifier, ElseBranch, LiteralKind},
21+
expr_ext::{ArrayExprKind, BlockModifier, CallableExpr, ElseBranch, LiteralKind},
2222
generated::{nodes::*, tokens::*},
2323
node_ext::{
2424
AttrKind, FieldKind, Macro, NameLike, NameOrNameRef, PathSegmentKind, SelfParamKind,

0 commit comments

Comments
 (0)