2020-11-01 04:48:42 -06:00
|
|
|
//! Renderer for function calls.
|
|
|
|
|
2020-11-27 04:22:10 -06:00
|
|
|
use hir::{HasSource, Type};
|
2020-11-01 03:35:04 -06:00
|
|
|
use syntax::{ast::Fn, display::function_declaration};
|
2020-12-01 05:23:00 -06:00
|
|
|
use test_utils::mark;
|
2020-11-01 03:35:04 -06:00
|
|
|
|
|
|
|
use crate::{
|
2020-12-03 03:13:28 -06:00
|
|
|
item::{CompletionItem, CompletionItemKind, CompletionKind, ImportEdit},
|
2020-11-01 03:35:04 -06:00
|
|
|
render::{builder_ext::Params, RenderContext},
|
|
|
|
};
|
|
|
|
|
2020-11-03 01:33:13 -06:00
|
|
|
pub(crate) fn render_fn<'a>(
|
|
|
|
ctx: RenderContext<'a>,
|
2020-12-03 03:13:28 -06:00
|
|
|
import_to_add: Option<ImportEdit>,
|
2020-11-03 01:33:13 -06:00
|
|
|
local_name: Option<String>,
|
|
|
|
fn_: hir::Function,
|
|
|
|
) -> CompletionItem {
|
2020-11-27 10:00:03 -06:00
|
|
|
let _p = profile::span("render_fn");
|
2020-11-27 04:22:10 -06:00
|
|
|
FunctionRender::new(ctx, local_name, fn_).render(import_to_add)
|
2020-11-03 01:33:13 -06:00
|
|
|
}
|
|
|
|
|
2020-11-01 03:35:04 -06:00
|
|
|
#[derive(Debug)]
|
2020-11-03 01:33:13 -06:00
|
|
|
struct FunctionRender<'a> {
|
2020-11-01 03:35:04 -06:00
|
|
|
ctx: RenderContext<'a>,
|
|
|
|
name: String,
|
2020-12-01 04:53:12 -06:00
|
|
|
func: hir::Function,
|
2020-11-01 03:35:04 -06:00
|
|
|
ast_node: Fn,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FunctionRender<'a> {
|
2020-11-03 01:33:13 -06:00
|
|
|
fn new(
|
2020-11-01 03:35:04 -06:00
|
|
|
ctx: RenderContext<'a>,
|
|
|
|
local_name: Option<String>,
|
|
|
|
fn_: hir::Function,
|
|
|
|
) -> FunctionRender<'a> {
|
|
|
|
let name = local_name.unwrap_or_else(|| fn_.name(ctx.db()).to_string());
|
2020-12-31 20:50:50 -06:00
|
|
|
#[allow(deprecated)]
|
2020-12-31 20:05:28 -06:00
|
|
|
let ast_node = fn_.source_old(ctx.db()).value;
|
2020-11-01 03:35:04 -06:00
|
|
|
|
2020-12-01 04:53:12 -06:00
|
|
|
FunctionRender { ctx, name, func: fn_, ast_node }
|
2020-11-01 03:35:04 -06:00
|
|
|
}
|
|
|
|
|
2020-12-03 03:13:28 -06:00
|
|
|
fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem {
|
2020-11-01 03:35:04 -06:00
|
|
|
let params = self.params();
|
|
|
|
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone())
|
|
|
|
.kind(self.kind())
|
2020-12-01 04:53:12 -06:00
|
|
|
.set_documentation(self.ctx.docs(self.func))
|
|
|
|
.set_deprecated(self.ctx.is_deprecated(self.func))
|
2020-11-01 03:35:04 -06:00
|
|
|
.detail(self.detail())
|
|
|
|
.add_call_parens(self.ctx.completion, self.name, params)
|
2020-12-07 16:46:56 -06:00
|
|
|
.add_import(import_to_add)
|
2020-11-01 03:35:04 -06:00
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn detail(&self) -> String {
|
|
|
|
function_declaration(&self.ast_node)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_arg(&self, arg: &str, ty: &Type) -> String {
|
|
|
|
if let Some(derefed_ty) = ty.remove_ref() {
|
|
|
|
for (name, local) in self.ctx.completion.locals.iter() {
|
|
|
|
if name == arg && local.ty(self.ctx.db()) == derefed_ty {
|
2020-11-01 03:59:43 -06:00
|
|
|
let mutability = if ty.is_mutable_reference() { "&mut " } else { "&" };
|
|
|
|
return format!("{}{}", mutability, arg);
|
2020-11-01 03:35:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arg.to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn params(&self) -> Params {
|
2020-12-01 05:23:00 -06:00
|
|
|
let ast_params = match self.ast_node.param_list() {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return Params::Named(Vec::new()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut params_pats = Vec::new();
|
2020-12-01 04:53:12 -06:00
|
|
|
let params_ty = if self.ctx.completion.dot_receiver.is_some() {
|
|
|
|
self.func.method_params(self.ctx.db()).unwrap_or_default()
|
|
|
|
} else {
|
2020-12-01 05:23:00 -06:00
|
|
|
if let Some(s) = ast_params.self_param() {
|
|
|
|
mark::hit!(parens_for_method_call_as_assoc_fn);
|
|
|
|
params_pats.push(Some(s.to_string()));
|
|
|
|
}
|
2020-12-01 04:53:12 -06:00
|
|
|
self.func.assoc_fn_params(self.ctx.db())
|
|
|
|
};
|
2020-12-01 05:23:00 -06:00
|
|
|
params_pats
|
|
|
|
.extend(ast_params.params().into_iter().map(|it| it.pat().map(|it| it.to_string())));
|
|
|
|
|
|
|
|
let params = params_pats
|
2020-11-01 03:35:04 -06:00
|
|
|
.into_iter()
|
|
|
|
.zip(params_ty)
|
2020-12-01 05:23:00 -06:00
|
|
|
.flat_map(|(pat, param_ty)| {
|
|
|
|
let pat = pat?;
|
2020-12-12 11:27:09 -06:00
|
|
|
let name = pat;
|
2020-12-01 05:23:00 -06:00
|
|
|
let arg = name.trim_start_matches("mut ").trim_start_matches('_');
|
|
|
|
Some(self.add_arg(arg, param_ty.ty()))
|
2020-11-01 03:35:04 -06:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
Params::Named(params)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn kind(&self) -> CompletionItemKind {
|
2020-12-01 04:53:12 -06:00
|
|
|
if self.func.self_param(self.ctx.db()).is_some() {
|
2020-11-01 03:35:04 -06:00
|
|
|
CompletionItemKind::Method
|
|
|
|
} else {
|
|
|
|
CompletionItemKind::Function
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-01 04:36:30 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use test_utils::mark;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
test_utils::{check_edit, check_edit_with_config},
|
|
|
|
CompletionConfig,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inserts_parens_for_function_calls() {
|
|
|
|
mark::check!(inserts_parens_for_function_calls);
|
|
|
|
check_edit(
|
|
|
|
"no_args",
|
|
|
|
r#"
|
|
|
|
fn no_args() {}
|
|
|
|
fn main() { no_<|> }
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn no_args() {}
|
|
|
|
fn main() { no_args()$0 }
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
check_edit(
|
|
|
|
"with_args",
|
|
|
|
r#"
|
|
|
|
fn with_args(x: i32, y: String) {}
|
|
|
|
fn main() { with_<|> }
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn with_args(x: i32, y: String) {}
|
|
|
|
fn main() { with_args(${1:x}, ${2:y})$0 }
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
check_edit(
|
|
|
|
"foo",
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
fn foo(&self) {}
|
|
|
|
}
|
|
|
|
fn bar(s: &S) { s.f<|> }
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
fn foo(&self) {}
|
|
|
|
}
|
|
|
|
fn bar(s: &S) { s.foo()$0 }
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
check_edit(
|
|
|
|
"foo",
|
|
|
|
r#"
|
|
|
|
struct S {}
|
|
|
|
impl S {
|
|
|
|
fn foo(&self, x: i32) {}
|
|
|
|
}
|
|
|
|
fn bar(s: &S) {
|
|
|
|
s.f<|>
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct S {}
|
|
|
|
impl S {
|
|
|
|
fn foo(&self, x: i32) {}
|
|
|
|
}
|
|
|
|
fn bar(s: &S) {
|
|
|
|
s.foo(${1:x})$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-01 05:23:00 -06:00
|
|
|
#[test]
|
|
|
|
fn parens_for_method_call_as_assoc_fn() {
|
|
|
|
mark::check!(parens_for_method_call_as_assoc_fn);
|
|
|
|
check_edit(
|
|
|
|
"foo",
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
fn foo(&self) {}
|
|
|
|
}
|
|
|
|
fn main() { S::f<|> }
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
fn foo(&self) {}
|
|
|
|
}
|
|
|
|
fn main() { S::foo(${1:&self})$0 }
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
#[test]
|
|
|
|
fn suppress_arg_snippets() {
|
|
|
|
mark::check!(suppress_arg_snippets);
|
|
|
|
check_edit_with_config(
|
|
|
|
CompletionConfig { add_call_argument_snippets: false, ..CompletionConfig::default() },
|
|
|
|
"with_args",
|
|
|
|
r#"
|
|
|
|
fn with_args(x: i32, y: String) {}
|
|
|
|
fn main() { with_<|> }
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn with_args(x: i32, y: String) {}
|
|
|
|
fn main() { with_args($0) }
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn strips_underscores_from_args() {
|
|
|
|
check_edit(
|
|
|
|
"foo",
|
|
|
|
r#"
|
|
|
|
fn foo(_foo: i32, ___bar: bool, ho_ge_: String) {}
|
|
|
|
fn main() { f<|> }
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo(_foo: i32, ___bar: bool, ho_ge_: String) {}
|
|
|
|
fn main() { foo(${1:foo}, ${2:bar}, ${3:ho_ge_})$0 }
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_ref_when_matching_local_in_scope() {
|
|
|
|
check_edit(
|
|
|
|
"ref_arg",
|
|
|
|
r#"
|
|
|
|
struct Foo {}
|
|
|
|
fn ref_arg(x: &Foo) {}
|
|
|
|
fn main() {
|
|
|
|
let x = Foo {};
|
|
|
|
ref_ar<|>
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo {}
|
|
|
|
fn ref_arg(x: &Foo) {}
|
|
|
|
fn main() {
|
|
|
|
let x = Foo {};
|
|
|
|
ref_arg(${1:&x})$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_mut_ref_when_matching_local_in_scope() {
|
|
|
|
check_edit(
|
|
|
|
"ref_arg",
|
|
|
|
r#"
|
|
|
|
struct Foo {}
|
|
|
|
fn ref_arg(x: &mut Foo) {}
|
|
|
|
fn main() {
|
|
|
|
let x = Foo {};
|
|
|
|
ref_ar<|>
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo {}
|
|
|
|
fn ref_arg(x: &mut Foo) {}
|
|
|
|
fn main() {
|
|
|
|
let x = Foo {};
|
|
|
|
ref_arg(${1:&mut x})$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_ref_when_matching_local_in_scope_for_method() {
|
|
|
|
check_edit(
|
|
|
|
"apply_foo",
|
|
|
|
r#"
|
|
|
|
struct Foo {}
|
|
|
|
struct Bar {}
|
|
|
|
impl Bar {
|
|
|
|
fn apply_foo(&self, x: &Foo) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = Foo {};
|
|
|
|
let y = Bar {};
|
|
|
|
y.<|>
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo {}
|
|
|
|
struct Bar {}
|
|
|
|
impl Bar {
|
|
|
|
fn apply_foo(&self, x: &Foo) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = Foo {};
|
|
|
|
let y = Bar {};
|
|
|
|
y.apply_foo(${1:&x})$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trim_mut_keyword_in_func_completion() {
|
|
|
|
check_edit(
|
|
|
|
"take_mutably",
|
|
|
|
r#"
|
|
|
|
fn take_mutably(mut x: &i32) {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
take_m<|>
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn take_mutably(mut x: &i32) {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
take_mutably(${1:x})$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|