rust/crates/ra_ide/src/call_info.rs

688 lines
17 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
use hir::Semantics;
2020-02-06 05:52:32 -06:00
use ra_ide_db::RootDatabase;
2019-01-08 13:33:36 -06:00
use ra_syntax::{
ast::{self, ArgListOwner},
2020-02-27 09:05:35 -06:00
match_ast, AstNode, SyntaxNode, SyntaxToken,
2019-01-08 13:33:36 -06:00
};
2020-05-20 05:59:20 -05:00
use test_utils::mark;
2019-01-08 13:33:36 -06:00
use crate::{CallInfo, FilePosition, FunctionSignature};
2019-01-08 13:33:36 -06:00
/// Computes parameter information for the given call expression.
2019-01-15 12:02:42 -06:00
pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> {
let sema = Semantics::new(db);
let file = sema.parse(position.file_id);
let file = file.syntax();
let token = file.token_at_offset(position.offset).next()?;
let token = sema.descend_into_macros(token);
2020-02-27 09:05:35 -06:00
call_info_for_token(&sema, token)
}
2019-01-08 13:33:36 -06:00
2020-04-23 18:46:00 -05:00
#[derive(Debug)]
pub(crate) struct ActiveParameter {
/// FIXME: should be `Type` and `Name
pub(crate) ty: String,
pub(crate) name: String,
}
impl ActiveParameter {
pub(crate) fn at(db: &RootDatabase, position: FilePosition) -> Option<Self> {
call_info(db, position)?.into_active_parameter()
}
pub(crate) fn at_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<Self> {
call_info_for_token(sema, token)?.into_active_parameter()
}
}
fn call_info_for_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<CallInfo> {
2019-01-08 13:33:36 -06:00
// Find the calling expression and it's NameRef
let calling_node = FnCallNode::with_node(&token.parent())?;
2019-01-08 13:33:36 -06:00
let (mut call_info, has_self) = match &calling_node {
FnCallNode::CallExpr(call) => {
//FIXME: Type::as_callable is broken
let callable_def = sema.type_of_expr(&call.expr()?)?.as_callable()?;
match callable_def {
2019-11-25 07:26:52 -06:00
hir::CallableDef::FunctionId(it) => {
let fn_def = it.into();
2020-02-27 09:05:35 -06:00
(CallInfo::with_fn(sema.db, fn_def), fn_def.has_self_param(sema.db))
}
hir::CallableDef::StructId(it) => {
(CallInfo::with_struct(sema.db, it.into())?, false)
2019-11-25 07:26:52 -06:00
}
hir::CallableDef::EnumVariantId(it) => {
2020-02-27 09:05:35 -06:00
(CallInfo::with_enum_variant(sema.db, it.into())?, false)
}
}
}
FnCallNode::MethodCallExpr(method_call) => {
let function = sema.resolve_method_call(&method_call)?;
2020-02-27 09:05:35 -06:00
(CallInfo::with_fn(sema.db, function), function.has_self_param(sema.db))
}
FnCallNode::MacroCallExpr(macro_call) => {
let macro_def = sema.resolve_macro_call(&macro_call)?;
2020-02-27 09:05:35 -06:00
(CallInfo::with_macro(sema.db, macro_def)?, false)
2019-10-29 11:16:55 -05:00
}
};
2019-01-08 13:33:36 -06:00
// If we have a calling expression let's find which argument we are on
let num_params = call_info.parameters().len();
2019-01-08 13:33:36 -06:00
2020-01-13 10:38:53 -06:00
match num_params {
0 => (),
1 => {
2020-01-13 10:27:06 -06:00
if !has_self {
call_info.active_parameter = Some(0);
}
2019-01-08 13:33:36 -06:00
}
2020-01-13 10:38:53 -06:00
_ => {
2020-01-13 10:27:06 -06:00
if let Some(arg_list) = calling_node.arg_list() {
// Number of arguments specified at the call site
let num_args_at_callsite = arg_list.args().count();
2020-01-13 10:27:06 -06:00
let arg_list_range = arg_list.syntax().text_range();
2020-02-27 09:05:35 -06:00
if !arg_list_range.contains_inclusive(token.text_range().start()) {
2020-05-20 05:59:20 -05:00
mark::hit!(call_info_bad_offset);
2020-01-13 10:27:06 -06:00
return None;
}
2019-01-08 13:33:36 -06:00
2020-01-13 10:27:06 -06:00
let mut param = std::cmp::min(
num_args_at_callsite,
arg_list
.args()
2020-02-27 09:05:35 -06:00
.take_while(|arg| {
arg.syntax().text_range().end() < token.text_range().start()
})
2020-01-13 10:27:06 -06:00
.count(),
);
2019-03-27 10:02:06 -05:00
2020-01-13 10:27:06 -06:00
// If we are in a method account for `self`
if has_self {
param += 1;
}
2019-01-08 13:33:36 -06:00
2020-01-13 10:27:06 -06:00
call_info.active_parameter = Some(param);
}
2019-01-08 13:33:36 -06:00
}
}
2019-01-15 12:02:42 -06:00
Some(call_info)
2019-01-08 13:33:36 -06:00
}
2019-10-29 11:16:55 -05:00
#[derive(Debug)]
pub(crate) enum FnCallNode {
2019-07-19 04:56:47 -05:00
CallExpr(ast::CallExpr),
MethodCallExpr(ast::MethodCallExpr),
2019-10-29 11:16:55 -05:00
MacroCallExpr(ast::MacroCall),
2019-01-08 13:33:36 -06:00
}
2019-07-19 04:56:47 -05:00
impl FnCallNode {
fn with_node(syntax: &SyntaxNode) -> Option<FnCallNode> {
syntax.ancestors().find_map(|node| {
match_ast! {
match node {
2020-04-06 09:21:33 -05:00
ast::CallExpr(it) => Some(FnCallNode::CallExpr(it)),
2020-02-27 09:05:35 -06:00
ast::MethodCallExpr(it) => {
let arg_list = it.arg_list()?;
2020-04-24 16:40:41 -05:00
if !arg_list.syntax().text_range().contains_range(syntax.text_range()) {
2020-02-27 09:05:35 -06:00
return None;
}
Some(FnCallNode::MethodCallExpr(it))
},
2020-04-06 09:21:33 -05:00
ast::MacroCall(it) => Some(FnCallNode::MacroCallExpr(it)),
_ => None,
}
}
})
2019-01-08 13:33:36 -06:00
}
pub(crate) fn with_node_exact(node: &SyntaxNode) -> Option<FnCallNode> {
match_ast! {
match node {
2020-04-06 09:21:33 -05:00
ast::CallExpr(it) => Some(FnCallNode::CallExpr(it)),
ast::MethodCallExpr(it) => Some(FnCallNode::MethodCallExpr(it)),
ast::MacroCall(it) => Some(FnCallNode::MacroCallExpr(it)),
_ => None,
}
}
}
pub(crate) fn name_ref(&self) -> Option<ast::NameRef> {
2019-07-19 04:56:47 -05:00
match self {
2019-08-19 06:13:58 -05:00
FnCallNode::CallExpr(call_expr) => Some(match call_expr.expr()? {
ast::Expr::PathExpr(path_expr) => path_expr.path()?.segment()?.name_ref()?,
2019-01-08 13:33:36 -06:00
_ => return None,
}),
2019-02-08 05:49:43 -06:00
FnCallNode::MethodCallExpr(call_expr) => {
2020-02-18 07:32:19 -06:00
call_expr.syntax().children().filter_map(ast::NameRef::cast).next()
2019-02-08 05:49:43 -06:00
}
2019-10-29 11:16:55 -05:00
FnCallNode::MacroCallExpr(call_expr) => call_expr.path()?.segment()?.name_ref(),
2019-01-08 13:33:36 -06:00
}
}
2019-07-19 04:56:47 -05:00
fn arg_list(&self) -> Option<ast::ArgList> {
match self {
2019-01-08 13:33:36 -06:00
FnCallNode::CallExpr(expr) => expr.arg_list(),
FnCallNode::MethodCallExpr(expr) => expr.arg_list(),
2019-10-29 11:16:55 -05:00
FnCallNode::MacroCallExpr(_) => None,
2019-01-08 13:33:36 -06:00
}
}
}
impl CallInfo {
2020-04-23 18:46:00 -05:00
fn into_active_parameter(self) -> Option<ActiveParameter> {
let idx = self.active_parameter?;
let ty = self.signature.parameter_types.get(idx)?.clone();
let name = self.signature.parameter_names.get(idx)?.clone();
let res = ActiveParameter { ty, name };
Some(res)
}
fn with_fn(db: &RootDatabase, function: hir::Function) -> Self {
2019-04-08 02:46:26 -05:00
let signature = FunctionSignature::from_hir(db, function);
2019-01-08 13:33:36 -06:00
2019-04-08 02:46:26 -05:00
CallInfo { signature, active_parameter: None }
2019-01-08 13:33:36 -06:00
}
fn with_struct(db: &RootDatabase, st: hir::Struct) -> Option<Self> {
let signature = FunctionSignature::from_struct(db, st)?;
Some(CallInfo { signature, active_parameter: None })
}
fn with_enum_variant(db: &RootDatabase, variant: hir::EnumVariant) -> Option<Self> {
let signature = FunctionSignature::from_enum_variant(db, variant)?;
2019-10-27 20:26:12 -05:00
Some(CallInfo { signature, active_parameter: None })
2019-10-27 20:26:12 -05:00
}
2019-10-29 11:16:55 -05:00
fn with_macro(db: &RootDatabase, macro_def: hir::MacroDef) -> Option<Self> {
let signature = FunctionSignature::from_macro(db, macro_def)?;
Some(CallInfo { signature, active_parameter: None })
}
fn parameters(&self) -> &[String] {
&self.signature.parameters
}
2019-01-08 13:33:36 -06:00
}
#[cfg(test)]
mod tests {
2020-05-20 05:59:20 -05:00
use test_utils::mark;
2019-01-08 13:33:36 -06:00
use crate::mock_analysis::single_file_with_position;
use super::*;
2019-04-04 10:06:22 -05: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()
}
}
fn call_info_helper(text: &str) -> Option<CallInfo> {
2019-01-08 13:33:36 -06:00
let (analysis, position) = single_file_with_position(text);
analysis.call_info(position).unwrap()
}
fn call_info(text: &str) -> CallInfo {
let info = call_info_helper(text);
assert!(info.is_some());
info.unwrap()
}
fn no_call_info(text: &str) {
let info = call_info_helper(text);
assert!(info.is_none());
2019-01-08 13:33:36 -06:00
}
#[test]
fn test_fn_signature_two_args_firstx() {
2019-01-08 13:33:36 -06:00
let info = call_info(
r#"fn foo(x: u32, y: u32) -> u32 {x + y}
fn bar() { foo(<|>3, ); }"#,
);
assert_eq!(info.parameters(), ["x: u32", "y: u32"]);
2019-01-08 13:33:36 -06: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, <|>); }"#,
);
assert_eq!(info.parameters(), ["x: u32", "y: u32"]);
2019-01-08 13:33:36 -06:00
assert_eq!(info.active_parameter, Some(1));
}
#[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(<|>); }"#,
);
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()
);
assert_eq!(info.active_parameter, Some(0));
}
2019-04-04 11:43:32 -05: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 13:33:36 -06: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(<|>);}"#,
);
assert!(info.parameters().is_empty());
2019-01-08 13:33:36 -06: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(<|>);
}"#,
);
assert_eq!(info.parameters(), ["&self"]);
2019-01-08 13:33:36 -06: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(<|>);
}"#,
);
assert_eq!(info.parameters(), ["&self", "x: i32"]);
2019-01-08 13:33:36 -06: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(<|>);
}
"#,
);
assert_eq!(info.parameters(), ["j: u32"]);
2019-01-08 13:33:36 -06:00
assert_eq!(info.active_parameter, Some(0));
assert_eq!(info.label(), "fn foo(j: u32) -> u32");
assert_eq!(info.doc().map(|it| it.into()), Some("test".to_string()));
2019-01-08 13:33:36 -06: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(<|>
}"#,
);
assert_eq!(info.parameters(), ["x: i32"]);
2019-01-08 13:33:36 -06:00
assert_eq!(info.active_parameter, Some(0));
assert_eq!(info.label(), "pub fn add_one(x: i32) -> i32");
2019-01-08 13:33:36 -06:00
assert_eq!(
info.doc().map(|it| it.into()),
2019-01-08 13:33:36 -06:00
Some(
r#"Adds one to the number given.
# Examples
```
2019-01-08 13:33:36 -06:00
let five = 5;
assert_eq!(6, my_crate::add_one(5));
```"#
.to_string()
2019-01-08 13:33:36 -06: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(<|>);
}"#,
);
assert_eq!(info.parameters(), ["x: i32"]);
2019-01-08 13:33:36 -06:00
assert_eq!(info.active_parameter, Some(0));
assert_eq!(info.label(), "pub fn add_one(x: i32) -> i32");
2019-01-08 13:33:36 -06:00
assert_eq!(
info.doc().map(|it| it.into()),
2019-01-08 13:33:36 -06:00
Some(
r#"Adds one to the number given.
# Examples
```
2019-01-08 13:33:36 -06:00
let five = 5;
assert_eq!(6, my_crate::add_one(5));
```"#
.to_string()
2019-01-08 13:33:36 -06:00
)
);
}
#[test]
fn test_fn_signature_with_docs_from_actix() {
let info = call_info(
r#"
struct WriteHandler<E>;
impl<E> WriteHandler<E> {
2019-01-08 13:33:36 -06: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()
}
}
pub fn foo(mut r: WriteHandler<()>) {
2019-01-08 13:33:36 -06:00
r.finished(<|>);
}
"#,
);
2019-10-29 14:25:31 -05:00
assert_eq!(info.label(), "fn finished(&mut self, ctx: &mut Self::Context)".to_string());
assert_eq!(info.parameters(), ["&mut self", "ctx: &mut Self::Context"]);
2019-01-08 13:33:36 -06:00
assert_eq!(info.active_parameter, Some(1));
assert_eq!(
info.doc().map(|it| it.into()),
2019-01-08 13:33:36 -06:00
Some(
r#"Method is called when writer finishes.
By default this method stops actor's `Context`."#
.to_string()
2019-01-08 13:33:36 -06:00
)
);
}
#[test]
fn call_info_bad_offset() {
2020-05-20 05:59:20 -05:00
mark::check!(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());
}
#[test]
fn test_nested_method_in_lamba() {
let info = call_info(
r#"struct Foo;
impl Foo {
fn bar(&self, _: u32) { }
}
fn bar(_: u32) { }
fn main() {
let foo = Foo;
std::thread::spawn(move || foo.bar(<|>));
}"#,
);
assert_eq!(info.parameters(), ["&self", "_: u32"]);
assert_eq!(info.active_parameter, Some(1));
assert_eq!(info.label(), "fn bar(&self, _: u32)");
}
2019-10-28 07:42:17 -05:00
#[test]
fn works_for_tuple_structs() {
let info = call_info(
r#"
/// A cool tuple struct
2019-10-27 19:11:02 -05:00
struct TS(u32, i32);
fn main() {
2019-10-27 19:11:02 -05:00
let s = TS(0, <|>);
}"#,
);
assert_eq!(info.label(), "struct TS(u32, i32) -> TS");
assert_eq!(info.doc().map(|it| it.into()), Some("A cool tuple struct".to_string()));
assert_eq!(info.active_parameter, Some(1));
}
2019-10-27 20:26:12 -05:00
#[test]
fn generic_struct() {
let info = call_info(
r#"
struct TS<T>(T);
fn main() {
let s = TS(<|>);
}"#,
);
assert_eq!(info.label(), "struct TS<T>(T) -> TS");
assert_eq!(info.active_parameter, Some(0));
}
#[test]
fn cant_call_named_structs() {
no_call_info(
r#"
struct TS { x: u32, y: i32 }
fn main() {
let s = TS(<|>);
}"#,
);
}
2019-10-27 20:26:12 -05:00
#[test]
fn works_for_enum_variants() {
let info = call_info(
r#"
enum E {
/// A Variant
A(i32),
/// Another
B,
/// And C
2019-10-27 21:03:58 -05:00
C { a: i32, b: i32 }
2019-10-27 20:26:12 -05:00
}
fn main() {
let a = E::A(<|>);
}
"#,
);
assert_eq!(info.label(), "E::A(0: i32)");
assert_eq!(info.doc().map(|it| it.into()), Some("A Variant".to_string()));
assert_eq!(info.active_parameter, Some(0));
}
#[test]
fn cant_call_enum_records() {
no_call_info(
r#"
enum E {
/// A Variant
A(i32),
/// Another
B,
/// And C
C { a: i32, b: i32 }
}
fn main() {
let a = E::C(<|>);
}
"#,
);
}
2019-10-29 11:16:55 -05:00
#[test]
fn fn_signature_for_macro() {
let info = call_info(
r#"
/// empty macro
macro_rules! foo {
() => {}
}
fn f() {
foo!(<|>);
}
"#,
);
assert_eq!(info.label(), "foo!()");
assert_eq!(info.doc().map(|it| it.into()), Some("empty macro".to_string()));
}
#[test]
fn fn_signature_for_call_in_macro() {
let info = call_info(
r#"
macro_rules! id {
($($tt:tt)*) => { $($tt)* }
}
fn foo() {
}
id! {
fn bar() {
foo(<|>);
}
}
"#,
);
assert_eq!(info.label(), "fn foo()");
}
2019-01-08 13:33:36 -06:00
}