2019-01-26 08:20:30 +00:00
|
|
|
use ra_db::SourceDatabase;
|
2019-01-08 19:33:36 +00:00
|
|
|
use ra_syntax::{
|
|
|
|
algo::find_node_at_offset,
|
2019-07-04 20:05:17 +00:00
|
|
|
ast::{self, ArgListOwner},
|
|
|
|
AstNode, SyntaxNode, TextUnit,
|
2019-01-08 19:33:36 +00:00
|
|
|
};
|
2019-07-04 20:05:17 +00:00
|
|
|
use test_utils::tested_by;
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2019-07-04 20:05:17 +00:00
|
|
|
use crate::{db::RootDatabase, CallInfo, FilePosition, FunctionSignature};
|
2019-01-08 19:33:36 +00:00
|
|
|
|
|
|
|
/// Computes parameter information for the given call expression.
|
2019-01-15 18:02:42 +00:00
|
|
|
pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> {
|
2019-07-12 16:41:13 +00:00
|
|
|
let parse = db.parse(position.file_id);
|
|
|
|
let syntax = parse.tree().syntax();
|
2019-01-08 19:33:36 +00:00
|
|
|
|
|
|
|
// Find the calling expression and it's NameRef
|
2019-01-15 18:02:42 +00:00
|
|
|
let calling_node = FnCallNode::with_node(syntax, position.offset)?;
|
|
|
|
let name_ref = calling_node.name_ref()?;
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2019-04-12 21:44:47 +00:00
|
|
|
let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
|
2019-04-11 12:34:13 +00:00
|
|
|
let function = match calling_node {
|
|
|
|
FnCallNode::CallExpr(expr) => {
|
|
|
|
//FIXME: apply subst
|
2019-06-03 14:21:08 +00:00
|
|
|
let (callable_def, _subst) = analyzer.type_of(db, expr.expr()?)?.as_callable()?;
|
2019-04-11 12:34:13 +00:00
|
|
|
match callable_def {
|
|
|
|
hir::CallableDef::Function(it) => it,
|
|
|
|
//FIXME: handle other callables
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
}
|
2019-04-11 14:15:20 +00:00
|
|
|
FnCallNode::MethodCallExpr(expr) => analyzer.resolve_method_call(expr)?,
|
2019-04-11 12:34:13 +00:00
|
|
|
};
|
2019-01-30 02:39:09 +00:00
|
|
|
|
2019-04-08 07:46:26 +00:00
|
|
|
let mut call_info = CallInfo::new(db, function);
|
2019-03-27 14:00:51 +00:00
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
// If we have a calling expression let's find which argument we are on
|
2019-03-12 07:24:46 +00:00
|
|
|
let num_params = call_info.parameters().len();
|
2019-06-18 17:07:35 +00:00
|
|
|
let has_self = function.data(db).has_self_param();
|
2019-01-08 19:33:36 +00:00
|
|
|
|
|
|
|
if num_params == 1 {
|
|
|
|
if !has_self {
|
|
|
|
call_info.active_parameter = Some(0);
|
|
|
|
}
|
|
|
|
} else if num_params > 1 {
|
|
|
|
// Count how many parameters into the call we are.
|
2019-03-27 14:00:51 +00:00
|
|
|
if let Some(arg_list) = calling_node.arg_list() {
|
2019-03-27 15:02:06 +00:00
|
|
|
// Number of arguments specified at the call site
|
|
|
|
let num_args_at_callsite = arg_list.args().count();
|
2019-03-27 14:00:51 +00:00
|
|
|
|
2019-01-26 21:32:27 +00:00
|
|
|
let arg_list_range = arg_list.syntax().range();
|
|
|
|
if !arg_list_range.contains_inclusive(position.offset) {
|
|
|
|
tested_by!(call_info_bad_offset);
|
|
|
|
return None;
|
|
|
|
}
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2019-03-27 15:02:06 +00:00
|
|
|
let mut param = std::cmp::min(
|
|
|
|
num_args_at_callsite,
|
|
|
|
arg_list
|
|
|
|
.args()
|
|
|
|
.take_while(|arg| arg.syntax().range().end() < position.offset)
|
|
|
|
.count(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// If we are in a method account for `self`
|
|
|
|
if has_self {
|
2019-06-04 06:31:17 +00:00
|
|
|
param += 1;
|
2019-03-27 15:02:06 +00:00
|
|
|
}
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2019-02-14 14:15:58 +00:00
|
|
|
call_info.active_parameter = Some(param);
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 18:02:42 +00:00
|
|
|
Some(call_info)
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum FnCallNode<'a> {
|
|
|
|
CallExpr(&'a ast::CallExpr),
|
|
|
|
MethodCallExpr(&'a ast::MethodCallExpr),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FnCallNode<'a> {
|
2019-04-11 12:35:38 +00:00
|
|
|
fn with_node(syntax: &'a SyntaxNode, offset: TextUnit) -> Option<FnCallNode<'a>> {
|
2019-01-08 19:33:36 +00:00
|
|
|
if let Some(expr) = find_node_at_offset::<ast::CallExpr>(syntax, offset) {
|
|
|
|
return Some(FnCallNode::CallExpr(expr));
|
|
|
|
}
|
|
|
|
if let Some(expr) = find_node_at_offset::<ast::MethodCallExpr>(syntax, offset) {
|
|
|
|
return Some(FnCallNode::MethodCallExpr(expr));
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-04-11 12:35:38 +00:00
|
|
|
fn name_ref(&self) -> Option<&'a ast::NameRef> {
|
2019-01-08 19:33:36 +00:00
|
|
|
match *self {
|
|
|
|
FnCallNode::CallExpr(call_expr) => Some(match call_expr.expr()?.kind() {
|
|
|
|
ast::ExprKind::PathExpr(path_expr) => path_expr.path()?.segment()?.name_ref()?,
|
|
|
|
_ => return None,
|
|
|
|
}),
|
|
|
|
|
2019-02-08 11:49:43 +00:00
|
|
|
FnCallNode::MethodCallExpr(call_expr) => {
|
|
|
|
call_expr.syntax().children().filter_map(ast::NameRef::cast).nth(0)
|
|
|
|
}
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 12:35:38 +00:00
|
|
|
fn arg_list(&self) -> Option<&'a ast::ArgList> {
|
2019-01-08 19:33:36 +00:00
|
|
|
match *self {
|
|
|
|
FnCallNode::CallExpr(expr) => expr.arg_list(),
|
|
|
|
FnCallNode::MethodCallExpr(expr) => expr.arg_list(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CallInfo {
|
2019-04-08 07:46:26 +00:00
|
|
|
fn new(db: &RootDatabase, function: hir::Function) -> Self {
|
|
|
|
let signature = FunctionSignature::from_hir(db, function);
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2019-04-08 07:46:26 +00:00
|
|
|
CallInfo { signature, active_parameter: None }
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 07:24:46 +00:00
|
|
|
fn parameters(&self) -> &[String] {
|
|
|
|
&self.signature.parameters
|
|
|
|
}
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-01-26 21:32:27 +00:00
|
|
|
use test_utils::covers;
|
2019-01-08 19:33:36 +00:00
|
|
|
|
|
|
|
use crate::mock_analysis::single_file_with_position;
|
|
|
|
|
2019-01-26 21:32:27 +00:00
|
|
|
use super::*;
|
|
|
|
|
2019-04-04 15:06:22 +00:00
|
|
|
// These are only used when testing
|
|
|
|
impl CallInfo {
|
|
|
|
fn doc(&self) -> Option<hir::Documentation> {
|
|
|
|
self.signature.doc.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn label(&self) -> String {
|
|
|
|
self.signature.to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
fn call_info(text: &str) -> CallInfo {
|
|
|
|
let (analysis, position) = single_file_with_position(text);
|
|
|
|
analysis.call_info(position).unwrap().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-04-11 12:34:13 +00:00
|
|
|
fn test_fn_signature_two_args_firstx() {
|
2019-01-08 19:33:36 +00:00
|
|
|
let info = call_info(
|
|
|
|
r#"fn foo(x: u32, y: u32) -> u32 {x + y}
|
|
|
|
fn bar() { foo(<|>3, ); }"#,
|
|
|
|
);
|
|
|
|
|
2019-03-12 07:24:46 +00:00
|
|
|
assert_eq!(info.parameters(), ["x: u32", "y: u32"]);
|
2019-01-08 19:33:36 +00:00
|
|
|
assert_eq!(info.active_parameter, Some(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_two_args_second() {
|
|
|
|
let info = call_info(
|
|
|
|
r#"fn foo(x: u32, y: u32) -> u32 {x + y}
|
|
|
|
fn bar() { foo(3, <|>); }"#,
|
|
|
|
);
|
|
|
|
|
2019-03-12 07:24:46 +00:00
|
|
|
assert_eq!(info.parameters(), ["x: u32", "y: u32"]);
|
2019-01-08 19:33:36 +00:00
|
|
|
assert_eq!(info.active_parameter, Some(1));
|
|
|
|
}
|
|
|
|
|
2019-03-27 14:00:51 +00:00
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_two_args_empty() {
|
|
|
|
let info = call_info(
|
|
|
|
r#"fn foo(x: u32, y: u32) -> u32 {x + y}
|
|
|
|
fn bar() { foo(<|>); }"#,
|
|
|
|
);
|
|
|
|
|
2019-03-12 07:24:46 +00:00
|
|
|
assert_eq!(info.parameters(), ["x: u32", "y: u32"]);
|
|
|
|
assert_eq!(info.active_parameter, Some(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_two_args_first_generics() {
|
|
|
|
let info = call_info(
|
|
|
|
r#"fn foo<T, U: Copy + Display>(x: T, y: U) -> u32 where T: Copy + Display, U: Debug {x + y}
|
|
|
|
fn bar() { foo(<|>3, ); }"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(info.parameters(), ["x: T", "y: U"]);
|
|
|
|
assert_eq!(
|
|
|
|
info.label(),
|
|
|
|
r#"
|
|
|
|
fn foo<T, U: Copy + Display>(x: T, y: U) -> u32
|
|
|
|
where T: Copy + Display,
|
|
|
|
U: Debug
|
|
|
|
"#
|
|
|
|
.trim()
|
|
|
|
);
|
2019-03-27 14:00:51 +00:00
|
|
|
assert_eq!(info.active_parameter, Some(0));
|
|
|
|
}
|
|
|
|
|
2019-04-04 16:43:32 +00:00
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_no_params() {
|
|
|
|
let info = call_info(
|
|
|
|
r#"fn foo<T>() -> T where T: Copy + Display {}
|
|
|
|
fn bar() { foo(<|>); }"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(info.parameters().is_empty());
|
|
|
|
assert_eq!(
|
|
|
|
info.label(),
|
|
|
|
r#"
|
|
|
|
fn foo<T>() -> T
|
|
|
|
where T: Copy + Display
|
|
|
|
"#
|
|
|
|
.trim()
|
|
|
|
);
|
|
|
|
assert!(info.active_parameter.is_none());
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_for_impl() {
|
|
|
|
let info = call_info(
|
|
|
|
r#"struct F; impl F { pub fn new() { F{}} }
|
|
|
|
fn bar() {let _ : F = F::new(<|>);}"#,
|
|
|
|
);
|
|
|
|
|
2019-03-12 07:24:46 +00:00
|
|
|
assert!(info.parameters().is_empty());
|
2019-01-08 19:33:36 +00:00
|
|
|
assert_eq!(info.active_parameter, None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_for_method_self() {
|
|
|
|
let info = call_info(
|
|
|
|
r#"struct F;
|
|
|
|
impl F {
|
|
|
|
pub fn new() -> F{
|
|
|
|
F{}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn do_it(&self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar() {
|
|
|
|
let f : F = F::new();
|
|
|
|
f.do_it(<|>);
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
|
2019-03-12 07:24:46 +00:00
|
|
|
assert_eq!(info.parameters(), ["&self"]);
|
2019-01-08 19:33:36 +00:00
|
|
|
assert_eq!(info.active_parameter, None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_for_method_with_arg() {
|
|
|
|
let info = call_info(
|
|
|
|
r#"struct F;
|
|
|
|
impl F {
|
|
|
|
pub fn new() -> F{
|
|
|
|
F{}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn do_it(&self, x: i32) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar() {
|
|
|
|
let f : F = F::new();
|
|
|
|
f.do_it(<|>);
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
|
2019-03-12 07:24:46 +00:00
|
|
|
assert_eq!(info.parameters(), ["&self", "x: i32"]);
|
2019-01-08 19:33:36 +00:00
|
|
|
assert_eq!(info.active_parameter, Some(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_with_docs_simple() {
|
|
|
|
let info = call_info(
|
|
|
|
r#"
|
|
|
|
/// test
|
|
|
|
// non-doc-comment
|
|
|
|
fn foo(j: u32) -> u32 {
|
|
|
|
j
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar() {
|
|
|
|
let _ = foo(<|>);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
2019-03-12 07:24:46 +00:00
|
|
|
assert_eq!(info.parameters(), ["j: u32"]);
|
2019-01-08 19:33:36 +00:00
|
|
|
assert_eq!(info.active_parameter, Some(0));
|
2019-03-12 07:24:46 +00:00
|
|
|
assert_eq!(info.label(), "fn foo(j: u32) -> u32");
|
|
|
|
assert_eq!(info.doc().map(|it| it.into()), Some("test".to_string()));
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_with_docs() {
|
|
|
|
let info = call_info(
|
|
|
|
r#"
|
|
|
|
/// Adds one to the number given.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let five = 5;
|
|
|
|
///
|
|
|
|
/// assert_eq!(6, my_crate::add_one(5));
|
|
|
|
/// ```
|
|
|
|
pub fn add_one(x: i32) -> i32 {
|
|
|
|
x + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn do() {
|
|
|
|
add_one(<|>
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
|
2019-03-12 07:24:46 +00:00
|
|
|
assert_eq!(info.parameters(), ["x: i32"]);
|
2019-01-08 19:33:36 +00:00
|
|
|
assert_eq!(info.active_parameter, Some(0));
|
2019-03-12 07:24:46 +00:00
|
|
|
assert_eq!(info.label(), "pub fn add_one(x: i32) -> i32");
|
2019-01-08 19:33:36 +00:00
|
|
|
assert_eq!(
|
2019-03-12 07:24:46 +00:00
|
|
|
info.doc().map(|it| it.into()),
|
2019-01-08 19:33:36 +00:00
|
|
|
Some(
|
|
|
|
r#"Adds one to the number given.
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
|
2019-01-30 02:39:09 +00:00
|
|
|
```
|
2019-01-08 19:33:36 +00:00
|
|
|
let five = 5;
|
|
|
|
|
|
|
|
assert_eq!(6, my_crate::add_one(5));
|
|
|
|
```"#
|
2019-01-30 02:39:09 +00:00
|
|
|
.to_string()
|
2019-01-08 19:33:36 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_with_docs_impl() {
|
|
|
|
let info = call_info(
|
|
|
|
r#"
|
|
|
|
struct addr;
|
|
|
|
impl addr {
|
|
|
|
/// Adds one to the number given.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let five = 5;
|
|
|
|
///
|
|
|
|
/// assert_eq!(6, my_crate::add_one(5));
|
|
|
|
/// ```
|
|
|
|
pub fn add_one(x: i32) -> i32 {
|
|
|
|
x + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn do_it() {
|
|
|
|
addr {};
|
|
|
|
addr::add_one(<|>);
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
|
2019-03-12 07:24:46 +00:00
|
|
|
assert_eq!(info.parameters(), ["x: i32"]);
|
2019-01-08 19:33:36 +00:00
|
|
|
assert_eq!(info.active_parameter, Some(0));
|
2019-03-12 07:24:46 +00:00
|
|
|
assert_eq!(info.label(), "pub fn add_one(x: i32) -> i32");
|
2019-01-08 19:33:36 +00:00
|
|
|
assert_eq!(
|
2019-03-12 07:24:46 +00:00
|
|
|
info.doc().map(|it| it.into()),
|
2019-01-08 19:33:36 +00:00
|
|
|
Some(
|
|
|
|
r#"Adds one to the number given.
|
|
|
|
|
|
|
|
# Examples
|
|
|
|
|
2019-01-30 02:39:09 +00:00
|
|
|
```
|
2019-01-08 19:33:36 +00:00
|
|
|
let five = 5;
|
|
|
|
|
|
|
|
assert_eq!(6, my_crate::add_one(5));
|
|
|
|
```"#
|
2019-01-30 02:39:09 +00:00
|
|
|
.to_string()
|
2019-01-08 19:33:36 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_with_docs_from_actix() {
|
|
|
|
let info = call_info(
|
|
|
|
r#"
|
2019-04-11 12:34:13 +00:00
|
|
|
struct WriteHandler<E>;
|
|
|
|
|
|
|
|
impl<E> WriteHandler<E> {
|
2019-01-08 19:33:36 +00:00
|
|
|
/// Method is called when writer emits error.
|
|
|
|
///
|
|
|
|
/// If this method returns `ErrorAction::Continue` writer processing
|
|
|
|
/// continues otherwise stream processing stops.
|
|
|
|
fn error(&mut self, err: E, ctx: &mut Self::Context) -> Running {
|
|
|
|
Running::Stop
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Method is called when writer finishes.
|
|
|
|
///
|
|
|
|
/// By default this method stops actor's `Context`.
|
|
|
|
fn finished(&mut self, ctx: &mut Self::Context) {
|
|
|
|
ctx.stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 12:34:13 +00:00
|
|
|
pub fn foo(mut r: WriteHandler<()>) {
|
2019-01-08 19:33:36 +00:00
|
|
|
r.finished(<|>);
|
|
|
|
}
|
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
2019-03-12 07:24:46 +00:00
|
|
|
assert_eq!(info.parameters(), ["&mut self", "ctx: &mut Self::Context"]);
|
2019-01-08 19:33:36 +00:00
|
|
|
assert_eq!(info.active_parameter, Some(1));
|
|
|
|
assert_eq!(
|
2019-03-12 07:24:46 +00:00
|
|
|
info.doc().map(|it| it.into()),
|
2019-01-08 19:33:36 +00:00
|
|
|
Some(
|
|
|
|
r#"Method is called when writer finishes.
|
|
|
|
|
|
|
|
By default this method stops actor's `Context`."#
|
2019-01-30 02:39:09 +00:00
|
|
|
.to_string()
|
2019-01-08 19:33:36 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-26 21:32:27 +00:00
|
|
|
#[test]
|
|
|
|
fn call_info_bad_offset() {
|
|
|
|
covers!(call_info_bad_offset);
|
|
|
|
let (analysis, position) = single_file_with_position(
|
|
|
|
r#"fn foo(x: u32, y: u32) -> u32 {x + y}
|
|
|
|
fn bar() { foo <|> (3, ); }"#,
|
|
|
|
);
|
|
|
|
let call_info = analysis.call_info(position).unwrap();
|
|
|
|
assert!(call_info.is_none());
|
|
|
|
}
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|