2019-09-30 08:58:53 +00:00
|
|
|
//! FIXME: write short doc here
|
2020-07-16 16:24:26 +00:00
|
|
|
use either::Either;
|
2020-07-16 11:00:56 +00:00
|
|
|
use hir::{Docs, HirDisplay, Semantics, Type};
|
2020-02-06 11:52:32 +00:00
|
|
|
use ra_ide_db::RootDatabase;
|
2019-01-08 19:33:36 +00:00
|
|
|
use ra_syntax::{
|
2019-07-04 20:05:17 +00:00
|
|
|
ast::{self, ArgListOwner},
|
2020-07-16 11:00:56 +00:00
|
|
|
match_ast, AstNode, SyntaxNode, SyntaxToken, TextRange, TextSize,
|
2019-01-08 19:33:36 +00:00
|
|
|
};
|
2020-07-16 11:00:56 +00:00
|
|
|
use stdx::format_to;
|
2020-05-20 10:59:20 +00:00
|
|
|
use test_utils::mark;
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-07-16 11:00:56 +00:00
|
|
|
use crate::FilePosition;
|
2020-07-16 08:29:21 +00:00
|
|
|
|
|
|
|
/// Contains information about a call site. Specifically the
|
|
|
|
/// `FunctionSignature`and current parameter.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct CallInfo {
|
2020-07-16 11:00:56 +00:00
|
|
|
pub doc: Option<String>,
|
|
|
|
pub signature: String,
|
2020-07-16 08:29:21 +00:00
|
|
|
pub active_parameter: Option<usize>,
|
2020-07-16 11:00:56 +00:00
|
|
|
parameters: Vec<TextRange>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CallInfo {
|
|
|
|
pub fn parameter_labels(&self) -> impl Iterator<Item = &str> + '_ {
|
|
|
|
self.parameters.iter().map(move |&it| &self.signature[it])
|
|
|
|
}
|
|
|
|
pub fn parameter_ranges(&self) -> &[TextRange] {
|
|
|
|
&self.parameters
|
|
|
|
}
|
|
|
|
fn push_param(&mut self, param: &str) {
|
|
|
|
if !self.signature.ends_with('(') {
|
|
|
|
self.signature.push_str(", ");
|
|
|
|
}
|
|
|
|
let start = TextSize::of(&self.signature);
|
|
|
|
self.signature.push_str(param);
|
|
|
|
let end = TextSize::of(&self.signature);
|
|
|
|
self.parameters.push(TextRange::new(start, end))
|
|
|
|
}
|
2020-07-16 08:29:21 +00:00
|
|
|
}
|
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> {
|
2020-02-18 17:35:10 +00:00
|
|
|
let sema = Semantics::new(db);
|
|
|
|
let file = sema.parse(position.file_id);
|
|
|
|
let file = file.syntax();
|
2019-11-18 23:08:39 +00:00
|
|
|
let token = file.token_at_offset(position.offset).next()?;
|
2020-02-18 17:35:10 +00:00
|
|
|
let token = sema.descend_into_macros(token);
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-07-16 11:00:56 +00:00
|
|
|
let (callable, active_parameter) = call_info_impl(&sema, token)?;
|
2020-04-23 23:46:00 +00:00
|
|
|
|
2020-07-16 11:00:56 +00:00
|
|
|
let mut res =
|
|
|
|
CallInfo { doc: None, signature: String::new(), parameters: vec![], active_parameter };
|
|
|
|
|
|
|
|
match callable.kind() {
|
|
|
|
hir::CallableKind::Function(func) => {
|
|
|
|
res.doc = func.docs(db).map(|it| it.as_str().to_string());
|
|
|
|
format_to!(res.signature, "fn {}", func.name(db));
|
|
|
|
}
|
|
|
|
hir::CallableKind::TupleStruct(strukt) => {
|
|
|
|
res.doc = strukt.docs(db).map(|it| it.as_str().to_string());
|
|
|
|
format_to!(res.signature, "struct {}", strukt.name(db));
|
|
|
|
}
|
|
|
|
hir::CallableKind::TupleEnumVariant(variant) => {
|
|
|
|
res.doc = variant.docs(db).map(|it| it.as_str().to_string());
|
|
|
|
format_to!(
|
|
|
|
res.signature,
|
|
|
|
"enum {}::{}",
|
|
|
|
variant.parent_enum(db).name(db),
|
|
|
|
variant.name(db)
|
|
|
|
);
|
|
|
|
}
|
2020-07-17 08:57:49 +00:00
|
|
|
hir::CallableKind::Closure => (),
|
2020-04-23 23:46:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 11:00:56 +00:00
|
|
|
res.signature.push('(');
|
|
|
|
{
|
|
|
|
if let Some(self_param) = callable.receiver_param(db) {
|
|
|
|
format_to!(res.signature, "{}", self_param)
|
|
|
|
}
|
|
|
|
let mut buf = String::new();
|
|
|
|
for (pat, ty) in callable.params(db) {
|
|
|
|
buf.clear();
|
|
|
|
if let Some(pat) = pat {
|
2020-07-16 16:24:26 +00:00
|
|
|
match pat {
|
|
|
|
Either::Left(_self) => format_to!(buf, "self: "),
|
|
|
|
Either::Right(pat) => format_to!(buf, "{}: ", pat),
|
|
|
|
}
|
2020-07-16 11:00:56 +00:00
|
|
|
}
|
|
|
|
format_to!(buf, "{}", ty.display(db));
|
|
|
|
res.push_param(&buf);
|
|
|
|
}
|
2020-04-23 23:46:00 +00:00
|
|
|
}
|
2020-07-16 11:00:56 +00:00
|
|
|
res.signature.push(')');
|
|
|
|
|
|
|
|
match callable.kind() {
|
2020-07-17 08:57:49 +00:00
|
|
|
hir::CallableKind::Function(_) | hir::CallableKind::Closure => {
|
2020-07-16 11:00:56 +00:00
|
|
|
let ret_type = callable.return_type();
|
|
|
|
if !ret_type.is_unit() {
|
|
|
|
format_to!(res.signature, " -> {}", ret_type.display(db));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hir::CallableKind::TupleStruct(_) | hir::CallableKind::TupleEnumVariant(_) => {}
|
|
|
|
}
|
|
|
|
Some(res)
|
2020-04-23 23:46:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 11:00:56 +00:00
|
|
|
fn call_info_impl(
|
|
|
|
sema: &Semantics<RootDatabase>,
|
|
|
|
token: SyntaxToken,
|
|
|
|
) -> Option<(hir::Callable, Option<usize>)> {
|
2019-01-08 19:33:36 +00:00
|
|
|
// Find the calling expression and it's NameRef
|
2020-02-18 17:35:10 +00:00
|
|
|
let calling_node = FnCallNode::with_node(&token.parent())?;
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-07-16 11:00:56 +00:00
|
|
|
let callable = match &calling_node {
|
|
|
|
FnCallNode::CallExpr(call) => sema.type_of_expr(&call.expr()?)?.as_callable(sema.db)?,
|
|
|
|
FnCallNode::MethodCallExpr(call) => sema.resolve_method_call_as_callable(call)?,
|
|
|
|
};
|
|
|
|
let active_param = 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();
|
|
|
|
|
|
|
|
let arg_list_range = arg_list.syntax().text_range();
|
|
|
|
if !arg_list_range.contains_inclusive(token.text_range().start()) {
|
|
|
|
mark::hit!(call_info_bad_offset);
|
|
|
|
return None;
|
2019-10-29 16:16:55 +00:00
|
|
|
}
|
2020-07-16 11:00:56 +00:00
|
|
|
let param = std::cmp::min(
|
|
|
|
num_args_at_callsite,
|
|
|
|
arg_list
|
|
|
|
.args()
|
|
|
|
.take_while(|arg| arg.syntax().text_range().end() <= token.text_range().start())
|
|
|
|
.count(),
|
|
|
|
);
|
|
|
|
|
|
|
|
Some(param)
|
|
|
|
} else {
|
|
|
|
None
|
2019-04-11 12:34:13 +00:00
|
|
|
};
|
2020-07-16 11:00:56 +00:00
|
|
|
Some((callable, active_param))
|
|
|
|
}
|
2019-01-30 02:39:09 +00:00
|
|
|
|
2020-07-16 11:00:56 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct ActiveParameter {
|
|
|
|
pub(crate) ty: Type,
|
|
|
|
pub(crate) name: String,
|
|
|
|
}
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-07-16 11:00:56 +00:00
|
|
|
impl ActiveParameter {
|
|
|
|
pub(crate) fn at(db: &RootDatabase, position: FilePosition) -> Option<Self> {
|
|
|
|
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);
|
|
|
|
Self::at_token(&sema, token)
|
|
|
|
}
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-07-16 11:00:56 +00:00
|
|
|
pub(crate) fn at_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<Self> {
|
|
|
|
let (signature, active_parameter) = call_info_impl(&sema, token)?;
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-07-16 11:00:56 +00:00
|
|
|
let idx = active_parameter?;
|
|
|
|
let mut params = signature.params(sema.db);
|
2020-07-17 13:44:37 +00:00
|
|
|
if !(idx < params.len()) {
|
|
|
|
mark::hit!(too_many_arguments);
|
|
|
|
return None;
|
|
|
|
}
|
2020-07-16 11:00:56 +00:00
|
|
|
let (pat, ty) = params.swap_remove(idx);
|
|
|
|
let name = pat?.to_string();
|
|
|
|
Some(ActiveParameter { ty, name })
|
|
|
|
}
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 16:16:55 +00:00
|
|
|
#[derive(Debug)]
|
2019-12-30 14:12:06 +00:00
|
|
|
pub(crate) enum FnCallNode {
|
2019-07-19 09:56:47 +00:00
|
|
|
CallExpr(ast::CallExpr),
|
|
|
|
MethodCallExpr(ast::MethodCallExpr),
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 09:56:47 +00:00
|
|
|
impl FnCallNode {
|
2019-11-18 23:08:39 +00:00
|
|
|
fn with_node(syntax: &SyntaxNode) -> Option<FnCallNode> {
|
|
|
|
syntax.ancestors().find_map(|node| {
|
2019-10-30 18:39:05 +00:00
|
|
|
match_ast! {
|
|
|
|
match node {
|
2020-04-06 14:21:33 +00:00
|
|
|
ast::CallExpr(it) => Some(FnCallNode::CallExpr(it)),
|
2020-02-27 15:05:35 +00:00
|
|
|
ast::MethodCallExpr(it) => {
|
|
|
|
let arg_list = it.arg_list()?;
|
2020-04-24 21:40:41 +00:00
|
|
|
if !arg_list.syntax().text_range().contains_range(syntax.text_range()) {
|
2020-02-27 15:05:35 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(FnCallNode::MethodCallExpr(it))
|
|
|
|
},
|
2020-04-06 14:21:33 +00:00
|
|
|
_ => None,
|
2019-10-30 18:39:05 +00:00
|
|
|
}
|
2019-10-27 16:26:44 +00:00
|
|
|
}
|
|
|
|
})
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 14:12:06 +00:00
|
|
|
pub(crate) fn with_node_exact(node: &SyntaxNode) -> Option<FnCallNode> {
|
|
|
|
match_ast! {
|
|
|
|
match node {
|
2020-04-06 14:21:33 +00:00
|
|
|
ast::CallExpr(it) => Some(FnCallNode::CallExpr(it)),
|
|
|
|
ast::MethodCallExpr(it) => Some(FnCallNode::MethodCallExpr(it)),
|
|
|
|
_ => None,
|
2019-12-30 14:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn name_ref(&self) -> Option<ast::NameRef> {
|
2019-07-19 09:56:47 +00:00
|
|
|
match self {
|
2019-08-19 11:13:58 +00:00
|
|
|
FnCallNode::CallExpr(call_expr) => Some(match call_expr.expr()? {
|
|
|
|
ast::Expr::PathExpr(path_expr) => path_expr.path()?.segment()?.name_ref()?,
|
2019-01-08 19:33:36 +00:00
|
|
|
_ => return None,
|
|
|
|
}),
|
|
|
|
|
2019-02-08 11:49:43 +00:00
|
|
|
FnCallNode::MethodCallExpr(call_expr) => {
|
2020-02-18 13:32:19 +00:00
|
|
|
call_expr.syntax().children().filter_map(ast::NameRef::cast).next()
|
2019-02-08 11:49:43 +00:00
|
|
|
}
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 09:56:47 +00:00
|
|
|
fn arg_list(&self) -> Option<ast::ArgList> {
|
|
|
|
match self {
|
2019-01-08 19:33:36 +00:00
|
|
|
FnCallNode::CallExpr(expr) => expr.arg_list(),
|
|
|
|
FnCallNode::MethodCallExpr(expr) => expr.arg_list(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-07-15 08:09:10 +00:00
|
|
|
use expect::{expect, Expect};
|
2020-05-20 10:59:20 +00:00
|
|
|
use test_utils::mark;
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-06-24 09:31:30 +00:00
|
|
|
use crate::mock_analysis::analysis_and_position;
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-07-15 08:09:10 +00:00
|
|
|
fn check(ra_fixture: &str, expect: Expect) {
|
|
|
|
let (analysis, position) = analysis_and_position(ra_fixture);
|
|
|
|
let call_info = analysis.call_info(position).unwrap();
|
|
|
|
let actual = match call_info {
|
|
|
|
Some(call_info) => {
|
2020-07-16 11:00:56 +00:00
|
|
|
let docs = match &call_info.doc {
|
2020-07-15 08:09:10 +00:00
|
|
|
None => "".to_string(),
|
|
|
|
Some(docs) => format!("{}\n------\n", docs.as_str()),
|
|
|
|
};
|
|
|
|
let params = call_info
|
2020-07-16 11:00:56 +00:00
|
|
|
.parameter_labels()
|
2020-07-15 08:09:10 +00:00
|
|
|
.enumerate()
|
|
|
|
.map(|(i, param)| {
|
|
|
|
if Some(i) == call_info.active_parameter {
|
|
|
|
format!("<{}>", param)
|
|
|
|
} else {
|
2020-07-16 11:00:56 +00:00
|
|
|
param.to_string()
|
2020-07-15 08:09:10 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ");
|
|
|
|
format!("{}{}\n({})\n", docs, call_info.signature, params)
|
|
|
|
}
|
|
|
|
None => String::new(),
|
|
|
|
};
|
|
|
|
expect.assert_eq(&actual);
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-15 08:09:10 +00:00
|
|
|
fn test_fn_signature_two_args() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo(x: u32, y: u32) -> u32 {x + y}
|
|
|
|
fn bar() { foo(<|>3, ); }
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
fn foo(x: u32, y: u32) -> u32
|
|
|
|
(<x: u32>, y: u32)
|
|
|
|
"#]],
|
2019-01-08 19:33:36 +00:00
|
|
|
);
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo(x: u32, y: u32) -> u32 {x + y}
|
|
|
|
fn bar() { foo(3<|>, ); }
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
fn foo(x: u32, y: u32) -> u32
|
|
|
|
(<x: u32>, y: u32)
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo(x: u32, y: u32) -> u32 {x + y}
|
|
|
|
fn bar() { foo(3,<|> ); }
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
fn foo(x: u32, y: u32) -> u32
|
2020-07-15 08:14:23 +00:00
|
|
|
(x: u32, <y: u32>)
|
2020-07-15 08:09:10 +00:00
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo(x: u32, y: u32) -> u32 {x + y}
|
|
|
|
fn bar() { foo(3, <|>); }
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
fn foo(x: u32, y: u32) -> u32
|
|
|
|
(x: u32, <y: u32>)
|
|
|
|
"#]],
|
2019-01-08 19:33:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-27 14:00:51 +00:00
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_two_args_empty() {
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo(x: u32, y: u32) -> u32 {x + y}
|
|
|
|
fn bar() { foo(<|>); }
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
fn foo(x: u32, y: u32) -> u32
|
|
|
|
(<x: u32>, y: u32)
|
|
|
|
"#]],
|
2019-03-27 14:00:51 +00:00
|
|
|
);
|
2019-03-12 07:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_two_args_first_generics() {
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
2019-03-12 07:24:46 +00:00
|
|
|
r#"
|
|
|
|
fn foo<T, U: Copy + Display>(x: T, y: U) -> u32
|
2020-07-15 08:09:10 +00:00
|
|
|
where T: Copy + Display, U: Debug
|
|
|
|
{ x + y }
|
|
|
|
|
|
|
|
fn bar() { foo(<|>3, ); }
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2020-07-16 11:00:56 +00:00
|
|
|
fn foo(x: i32, y: {unknown}) -> u32
|
|
|
|
(<x: i32>, y: {unknown})
|
2020-07-15 08:09:10 +00:00
|
|
|
"#]],
|
2019-03-12 07:24:46 +00:00
|
|
|
);
|
2019-03-27 14:00:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 16:43:32 +00:00
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_no_params() {
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
2019-04-04 16:43:32 +00:00
|
|
|
r#"
|
2020-07-15 08:09:10 +00:00
|
|
|
fn foo<T>() -> T where T: Copy + Display {}
|
|
|
|
fn bar() { foo(<|>); }
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2020-07-16 11:00:56 +00:00
|
|
|
fn foo() -> {unknown}
|
2020-07-15 08:09:10 +00:00
|
|
|
()
|
|
|
|
"#]],
|
2019-04-04 16:43:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:33:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_for_impl() {
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
|
|
|
r#"
|
2020-07-16 11:00:56 +00:00
|
|
|
struct F;
|
|
|
|
impl F { pub fn new() { } }
|
|
|
|
fn bar() {
|
|
|
|
let _ : F = F::new(<|>);
|
|
|
|
}
|
2020-07-15 08:09:10 +00:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2020-07-16 11:00:56 +00:00
|
|
|
fn new()
|
2020-07-15 08:09:10 +00:00
|
|
|
()
|
|
|
|
"#]],
|
2019-01-08 19:33:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_for_method_self() {
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S { pub fn do_it(&self) {} }
|
2019-01-08 19:33:36 +00:00
|
|
|
|
|
|
|
fn bar() {
|
2020-07-15 08:09:10 +00:00
|
|
|
let s: S = S;
|
|
|
|
s.do_it(<|>);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2020-07-16 11:00:56 +00:00
|
|
|
fn do_it(&self)
|
|
|
|
()
|
2020-07-15 08:09:10 +00:00
|
|
|
"#]],
|
2019-01-08 19:33:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_for_method_with_arg() {
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct S;
|
2020-07-16 16:24:26 +00:00
|
|
|
impl S {
|
|
|
|
fn foo(&self, x: i32) {}
|
2020-07-15 08:09:10 +00:00
|
|
|
}
|
2020-07-16 16:24:26 +00:00
|
|
|
|
|
|
|
fn main() { S.foo(<|>); }
|
2020-07-15 08:09:10 +00:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2020-07-16 16:24:26 +00:00
|
|
|
fn foo(&self, x: i32)
|
2020-07-16 11:00:56 +00:00
|
|
|
(<x: i32>)
|
2020-07-15 08:09:10 +00:00
|
|
|
"#]],
|
2020-07-16 16:24:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_for_method_with_arg_as_assoc_fn() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
fn foo(&self, x: i32) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() { S::foo(<|>); }
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
fn foo(self: &S, x: i32)
|
|
|
|
(<self: &S>, x: i32)
|
|
|
|
"#]],
|
2019-01-08 19:33:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_with_docs_simple() {
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
2019-01-08 19:33:36 +00:00
|
|
|
r#"
|
|
|
|
/// test
|
|
|
|
// non-doc-comment
|
|
|
|
fn foo(j: u32) -> u32 {
|
|
|
|
j
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar() {
|
|
|
|
let _ = foo(<|>);
|
|
|
|
}
|
|
|
|
"#,
|
2020-07-15 08:09:10 +00:00
|
|
|
expect![[r#"
|
|
|
|
test
|
|
|
|
------
|
|
|
|
fn foo(j: u32) -> u32
|
|
|
|
(<j: u32>)
|
|
|
|
"#]],
|
2019-01-08 19:33:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_with_docs() {
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
2019-01-08 19:33:36 +00:00
|
|
|
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(<|>
|
|
|
|
}"#,
|
2020-07-15 08:09:10 +00:00
|
|
|
expect![[r##"
|
|
|
|
Adds one to the number given.
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-07-15 08:09:10 +00:00
|
|
|
# Examples
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-07-15 08:09:10 +00:00
|
|
|
```
|
|
|
|
let five = 5;
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-07-15 08:09:10 +00:00
|
|
|
assert_eq!(6, my_crate::add_one(5));
|
|
|
|
```
|
|
|
|
------
|
2020-07-16 11:00:56 +00:00
|
|
|
fn add_one(x: i32) -> i32
|
2020-07-15 08:09:10 +00:00
|
|
|
(<x: i32>)
|
|
|
|
"##]],
|
2019-01-08 19:33:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_with_docs_impl() {
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
2019-01-08 19:33:36 +00:00
|
|
|
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(<|>);
|
2020-07-15 08:09:10 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r##"
|
|
|
|
Adds one to the number given.
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-07-15 08:09:10 +00:00
|
|
|
# Examples
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-07-15 08:09:10 +00:00
|
|
|
```
|
|
|
|
let five = 5;
|
2019-01-08 19:33:36 +00:00
|
|
|
|
2020-07-15 08:09:10 +00:00
|
|
|
assert_eq!(6, my_crate::add_one(5));
|
|
|
|
```
|
|
|
|
------
|
2020-07-16 11:00:56 +00:00
|
|
|
fn add_one(x: i32) -> i32
|
2020-07-15 08:09:10 +00:00
|
|
|
(<x: i32>)
|
|
|
|
"##]],
|
2019-01-08 19:33:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_signature_with_docs_from_actix() {
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
2019-01-08 19:33:36 +00:00
|
|
|
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(<|>);
|
|
|
|
}
|
|
|
|
"#,
|
2020-07-15 08:09:10 +00:00
|
|
|
expect![[r#"
|
|
|
|
Method is called when writer finishes.
|
|
|
|
|
|
|
|
By default this method stops actor's `Context`.
|
|
|
|
------
|
2020-07-16 11:00:56 +00:00
|
|
|
fn finished(&mut self, ctx: &mut {unknown})
|
|
|
|
(<ctx: &mut {unknown}>)
|
2020-07-15 08:09:10 +00:00
|
|
|
"#]],
|
2019-01-08 19:33:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-26 21:32:27 +00:00
|
|
|
#[test]
|
|
|
|
fn call_info_bad_offset() {
|
2020-05-20 10:59:20 +00:00
|
|
|
mark::check!(call_info_bad_offset);
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo(x: u32, y: u32) -> u32 {x + y}
|
|
|
|
fn bar() { foo <|> (3, ); }
|
|
|
|
"#,
|
|
|
|
expect![[""]],
|
2019-01-26 21:32:27 +00:00
|
|
|
);
|
|
|
|
}
|
2019-10-27 16:26:44 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-07-15 08:09:10 +00:00
|
|
|
fn test_nested_method_in_lambda() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo { fn bar(&self, _: u32) { } }
|
2019-10-27 16:26:44 +00:00
|
|
|
|
|
|
|
fn bar(_: u32) { }
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = Foo;
|
|
|
|
std::thread::spawn(move || foo.bar(<|>));
|
2020-07-15 08:09:10 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
fn bar(&self, _: u32)
|
2020-07-16 11:00:56 +00:00
|
|
|
(<_: u32>)
|
2020-07-15 08:09:10 +00:00
|
|
|
"#]],
|
2019-10-27 16:26:44 +00:00
|
|
|
);
|
|
|
|
}
|
2019-10-28 12:42:17 +00:00
|
|
|
|
2019-10-28 14:48:40 +00:00
|
|
|
#[test]
|
2019-10-27 23:12:21 +00:00
|
|
|
fn works_for_tuple_structs() {
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
2019-10-27 23:12:21 +00:00
|
|
|
r#"
|
|
|
|
/// A cool tuple struct
|
2020-07-16 11:00:56 +00:00
|
|
|
struct S(u32, i32);
|
2019-10-27 23:12:21 +00:00
|
|
|
fn main() {
|
2020-07-16 11:00:56 +00:00
|
|
|
let s = S(0, <|>);
|
2020-07-15 08:09:10 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
A cool tuple struct
|
|
|
|
------
|
2020-07-16 11:00:56 +00:00
|
|
|
struct S(u32, i32)
|
2020-07-15 08:09:10 +00:00
|
|
|
(u32, <i32>)
|
|
|
|
"#]],
|
2019-10-27 23:12:21 +00:00
|
|
|
);
|
|
|
|
}
|
2019-10-28 01:26:12 +00:00
|
|
|
|
Fix completion of HashMap::new
The `ty` function in code_model returned the type with placeholders for type
parameters. That's nice for printing, but not good for completion, because
placeholders won't unify with anything else: So the type we got for `HashMap`
was `HashMap<K, V, T>`, which doesn't unify with `HashMap<?, ?, RandomState>`,
so the `new` method wasn't shown.
Now we instead return `HashMap<{unknown}, {unknown}, {unknown}>`, which does
unify with the impl type. Maybe we should just expose this properly as variables
though, i.e. we'd return something like `exists<type, type, type> HashMap<?0,
?1, ?2>` (in Chalk notation). It'll make the API more complicated, but harder to
misuse. (And it would handle cases like `type TypeAlias<T> = HashMap<T, T>` more
correctly.)
2020-03-13 10:45:58 +00:00
|
|
|
#[test]
|
|
|
|
fn generic_struct() {
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
Fix completion of HashMap::new
The `ty` function in code_model returned the type with placeholders for type
parameters. That's nice for printing, but not good for completion, because
placeholders won't unify with anything else: So the type we got for `HashMap`
was `HashMap<K, V, T>`, which doesn't unify with `HashMap<?, ?, RandomState>`,
so the `new` method wasn't shown.
Now we instead return `HashMap<{unknown}, {unknown}, {unknown}>`, which does
unify with the impl type. Maybe we should just expose this properly as variables
though, i.e. we'd return something like `exists<type, type, type> HashMap<?0,
?1, ?2>` (in Chalk notation). It'll make the API more complicated, but harder to
misuse. (And it would handle cases like `type TypeAlias<T> = HashMap<T, T>` more
correctly.)
2020-03-13 10:45:58 +00:00
|
|
|
r#"
|
2020-07-16 11:00:56 +00:00
|
|
|
struct S<T>(T);
|
Fix completion of HashMap::new
The `ty` function in code_model returned the type with placeholders for type
parameters. That's nice for printing, but not good for completion, because
placeholders won't unify with anything else: So the type we got for `HashMap`
was `HashMap<K, V, T>`, which doesn't unify with `HashMap<?, ?, RandomState>`,
so the `new` method wasn't shown.
Now we instead return `HashMap<{unknown}, {unknown}, {unknown}>`, which does
unify with the impl type. Maybe we should just expose this properly as variables
though, i.e. we'd return something like `exists<type, type, type> HashMap<?0,
?1, ?2>` (in Chalk notation). It'll make the API more complicated, but harder to
misuse. (And it would handle cases like `type TypeAlias<T> = HashMap<T, T>` more
correctly.)
2020-03-13 10:45:58 +00:00
|
|
|
fn main() {
|
2020-07-16 11:00:56 +00:00
|
|
|
let s = S(<|>);
|
2020-07-15 08:09:10 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2020-07-16 11:00:56 +00:00
|
|
|
struct S({unknown})
|
|
|
|
(<{unknown}>)
|
2020-07-15 08:09:10 +00:00
|
|
|
"#]],
|
Fix completion of HashMap::new
The `ty` function in code_model returned the type with placeholders for type
parameters. That's nice for printing, but not good for completion, because
placeholders won't unify with anything else: So the type we got for `HashMap`
was `HashMap<K, V, T>`, which doesn't unify with `HashMap<?, ?, RandomState>`,
so the `new` method wasn't shown.
Now we instead return `HashMap<{unknown}, {unknown}, {unknown}>`, which does
unify with the impl type. Maybe we should just expose this properly as variables
though, i.e. we'd return something like `exists<type, type, type> HashMap<?0,
?1, ?2>` (in Chalk notation). It'll make the API more complicated, but harder to
misuse. (And it would handle cases like `type TypeAlias<T> = HashMap<T, T>` more
correctly.)
2020-03-13 10:45:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-28 01:26:12 +00:00
|
|
|
#[test]
|
|
|
|
fn works_for_enum_variants() {
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
2019-10-28 01:26:12 +00:00
|
|
|
r#"
|
|
|
|
enum E {
|
|
|
|
/// A Variant
|
|
|
|
A(i32),
|
|
|
|
/// Another
|
|
|
|
B,
|
|
|
|
/// And C
|
2019-10-28 02:03:58 +00:00
|
|
|
C { a: i32, b: i32 }
|
2019-10-28 01:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let a = E::A(<|>);
|
|
|
|
}
|
2020-07-15 08:09:10 +00:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
A Variant
|
|
|
|
------
|
2020-07-16 11:00:56 +00:00
|
|
|
enum E::A(i32)
|
|
|
|
(<i32>)
|
2020-07-15 08:09:10 +00:00
|
|
|
"#]],
|
2019-10-28 01:26:12 +00:00
|
|
|
);
|
|
|
|
}
|
2019-10-28 14:48:40 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-07-16 11:00:56 +00:00
|
|
|
fn cant_call_struct_record() {
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
2019-10-28 14:48:40 +00:00
|
|
|
r#"
|
2020-07-16 11:00:56 +00:00
|
|
|
struct S { x: u32, y: i32 }
|
2019-10-28 14:48:40 +00:00
|
|
|
fn main() {
|
2020-07-16 11:00:56 +00:00
|
|
|
let s = S(<|>);
|
2019-10-28 14:48:40 +00:00
|
|
|
}
|
2020-07-15 08:09:10 +00:00
|
|
|
"#,
|
|
|
|
expect![[""]],
|
2019-10-28 14:48:40 +00:00
|
|
|
);
|
|
|
|
}
|
2019-10-29 16:16:55 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-07-16 11:00:56 +00:00
|
|
|
fn cant_call_enum_record() {
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
2019-10-29 16:16:55 +00:00
|
|
|
r#"
|
2020-07-16 11:00:56 +00:00
|
|
|
enum E {
|
|
|
|
/// A Variant
|
|
|
|
A(i32),
|
|
|
|
/// Another
|
|
|
|
B,
|
|
|
|
/// And C
|
|
|
|
C { a: i32, b: i32 }
|
2019-10-29 16:16:55 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 11:00:56 +00:00
|
|
|
fn main() {
|
|
|
|
let a = E::C(<|>);
|
2019-10-29 16:16:55 +00:00
|
|
|
}
|
2020-07-15 08:09:10 +00:00
|
|
|
"#,
|
2020-07-16 11:00:56 +00:00
|
|
|
expect![[""]],
|
2019-10-29 16:16:55 +00:00
|
|
|
);
|
|
|
|
}
|
2019-11-18 23:08:39 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fn_signature_for_call_in_macro() {
|
2020-07-15 08:09:10 +00:00
|
|
|
check(
|
2019-11-18 23:08:39 +00:00
|
|
|
r#"
|
2020-07-15 08:09:10 +00:00
|
|
|
macro_rules! id { ($($tt:tt)*) => { $($tt)* } }
|
|
|
|
fn foo() { }
|
|
|
|
id! {
|
|
|
|
fn bar() { foo(<|>); }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
fn foo()
|
|
|
|
()
|
|
|
|
"#]],
|
2019-11-18 23:08:39 +00:00
|
|
|
);
|
|
|
|
}
|
2020-07-17 08:57:49 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn call_info_for_lambdas() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
fn foo(s: S) -> i32 { 92 }
|
|
|
|
fn main() {
|
|
|
|
(|s| foo(s))(<|>)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
(S) -> i32
|
|
|
|
(<S>)
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
2020-07-17 10:03:59 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn call_info_for_fn_ptr() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main(f: fn(i32, f64) -> char) {
|
|
|
|
f(0, <|>)
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
(i32, f64) -> char
|
|
|
|
(i32, <f64>)
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
2019-01-08 19:33:36 +00:00
|
|
|
}
|