This commit is contained in:
Aleksey Kladov 2019-01-08 18:43:29 +03:00
parent db794abe66
commit 6f02f176c8

View file

@ -21,53 +21,47 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Cancelable
// Resolve the function's NameRef (NOTE: this isn't entirely accurate). // Resolve the function's NameRef (NOTE: this isn't entirely accurate).
let file_symbols = db.index_resolve(name_ref)?; let file_symbols = db.index_resolve(name_ref)?;
for symbol in file_symbols { let symbol = ctry!(file_symbols.into_iter().find(|it| it.ptr.kind() == FN_DEF));
if symbol.ptr.kind() == FN_DEF { let fn_file = db.source_file(symbol.file_id);
let fn_file = db.source_file(symbol.file_id); let fn_def = symbol.ptr.resolve(&fn_file);
let fn_def = symbol.ptr.resolve(&fn_file); let fn_def = ast::FnDef::cast(&fn_def).unwrap();
let fn_def = ast::FnDef::cast(&fn_def).unwrap(); let mut call_info = ctry!(CallInfo::new(fn_def));
if let Some(mut call_info) = CallInfo::new(fn_def) { // If we have a calling expression let's find which argument we are on
// If we have a calling expression let's find which argument we are on let num_params = call_info.parameters.len();
let num_params = call_info.parameters.len(); let has_self = fn_def.param_list().and_then(|l| l.self_param()).is_some();
let has_self = fn_def.param_list().and_then(|l| l.self_param()).is_some();
if num_params == 1 { if num_params == 1 {
if !has_self { if !has_self {
call_info.active_parameter = Some(0); call_info.active_parameter = Some(0);
} }
} else if num_params > 1 { } else if num_params > 1 {
// Count how many parameters into the call we are. // Count how many parameters into the call we are.
// TODO: This is best effort for now and should be fixed at some point. // TODO: This is best effort for now and should be fixed at some point.
// It may be better to see where we are in the arg_list and then check // It may be better to see where we are in the arg_list and then check
// where offset is in that list (or beyond). // where offset is in that list (or beyond).
// Revisit this after we get documentation comments in. // Revisit this after we get documentation comments in.
if let Some(ref arg_list) = calling_node.arg_list() { if let Some(ref arg_list) = calling_node.arg_list() {
let start = arg_list.syntax().range().start(); let start = arg_list.syntax().range().start();
let range_search = TextRange::from_to(start, position.offset); let range_search = TextRange::from_to(start, position.offset);
let mut commas: usize = arg_list let mut commas: usize = arg_list
.syntax() .syntax()
.text() .text()
.slice(range_search) .slice(range_search)
.to_string() .to_string()
.matches(',') .matches(',')
.count(); .count();
// If we have a method call eat the first param since it's just self. // If we have a method call eat the first param since it's just self.
if has_self { if has_self {
commas += 1; commas += 1;
}
call_info.active_parameter = Some(commas);
}
}
return Ok(Some(call_info));
} }
call_info.active_parameter = Some(commas);
} }
} }
Ok(None) Ok(Some(call_info))
} }
enum FnCallNode<'a> { enum FnCallNode<'a> {