From fc8a1cd8006b021541ff673ec7f37a0f4b7bef57 Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Sun, 1 Nov 2020 12:35:04 +0300 Subject: Introduce render module --- crates/completion/src/render/function.rs | 87 ++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 crates/completion/src/render/function.rs (limited to 'crates/completion/src/render/function.rs') diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs new file mode 100644 index 000000000..16f15e22c --- /dev/null +++ b/crates/completion/src/render/function.rs @@ -0,0 +1,87 @@ +use hir::{Documentation, HasAttrs, HasSource, Type}; +use syntax::{ast::Fn, display::function_declaration}; + +use crate::{ + item::{CompletionItem, CompletionItemKind, CompletionKind}, + render::{builder_ext::Params, RenderContext}, +}; + +#[derive(Debug)] +pub(crate) struct FunctionRender<'a> { + ctx: RenderContext<'a>, + name: String, + fn_: hir::Function, + ast_node: Fn, +} + +impl<'a> FunctionRender<'a> { + pub(crate) fn new( + ctx: RenderContext<'a>, + local_name: Option, + fn_: hir::Function, + ) -> FunctionRender<'a> { + let name = local_name.unwrap_or_else(|| fn_.name(ctx.db()).to_string()); + let ast_node = fn_.source(ctx.db()).value; + + FunctionRender { ctx, name, fn_, ast_node } + } + + pub(crate) fn render(self) -> CompletionItem { + let params = self.params(); + CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone()) + .kind(self.kind()) + .set_documentation(self.docs()) + .set_deprecated(self.ctx.is_deprecated(self.fn_)) + .detail(self.detail()) + .add_call_parens(self.ctx.completion, self.name, params) + .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 { + return (if ty.is_mutable_reference() { "&mut " } else { "&" }).to_string() + + &arg.to_string(); + } + } + } + arg.to_string() + } + + fn params(&self) -> Params { + let params_ty = self.fn_.params(self.ctx.db()); + let params = self + .ast_node + .param_list() + .into_iter() + .flat_map(|it| it.params()) + .zip(params_ty) + .flat_map(|(it, param_ty)| { + if let Some(pat) = it.pat() { + let name = pat.to_string(); + let arg = name.trim_start_matches("mut ").trim_start_matches('_'); + return Some(self.add_arg(arg, param_ty.ty())); + } + None + }) + .collect(); + Params::Named(params) + } + + fn kind(&self) -> CompletionItemKind { + if self.fn_.self_param(self.ctx.db()).is_some() { + CompletionItemKind::Method + } else { + CompletionItemKind::Function + } + } + + fn docs(&self) -> Option { + self.fn_.docs(self.ctx.db()) + } +} -- cgit v1.2.3 From 944ccf60758305a1b15224defe622cfca6939aaa Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Sun, 1 Nov 2020 12:59:43 +0300 Subject: Add ConstRender --- crates/completion/src/render/function.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'crates/completion/src/render/function.rs') diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs index 16f15e22c..d22081236 100644 --- a/crates/completion/src/render/function.rs +++ b/crates/completion/src/render/function.rs @@ -1,4 +1,4 @@ -use hir::{Documentation, HasAttrs, HasSource, Type}; +use hir::{HasSource, Type}; use syntax::{ast::Fn, display::function_declaration}; use crate::{ @@ -30,7 +30,7 @@ impl<'a> FunctionRender<'a> { let params = self.params(); CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone()) .kind(self.kind()) - .set_documentation(self.docs()) + .set_documentation(self.ctx.docs(self.fn_)) .set_deprecated(self.ctx.is_deprecated(self.fn_)) .detail(self.detail()) .add_call_parens(self.ctx.completion, self.name, params) @@ -45,8 +45,8 @@ impl<'a> FunctionRender<'a> { 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 { - return (if ty.is_mutable_reference() { "&mut " } else { "&" }).to_string() - + &arg.to_string(); + let mutability = if ty.is_mutable_reference() { "&mut " } else { "&" }; + return format!("{}{}", mutability, arg); } } } @@ -80,8 +80,4 @@ impl<'a> FunctionRender<'a> { CompletionItemKind::Function } } - - fn docs(&self) -> Option { - self.fn_.docs(self.ctx.db()) - } } -- cgit v1.2.3 From 97a504805d4b0cf8b48bc5052453b2b2f3298449 Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Sun, 1 Nov 2020 13:36:30 +0300 Subject: Move rendering tests to the render module --- crates/completion/src/render/function.rs | 210 +++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) (limited to 'crates/completion/src/render/function.rs') diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs index d22081236..cf3852bf6 100644 --- a/crates/completion/src/render/function.rs +++ b/crates/completion/src/render/function.rs @@ -81,3 +81,213 @@ impl<'a> FunctionRender<'a> { } } } + +#[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 +} +"#, + ); + } + + #[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 +} +"#, + ); + } +} -- cgit v1.2.3 From 2a214e15d38c7d97243e23e5e26fee5f4e26bb50 Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Sun, 1 Nov 2020 13:48:42 +0300 Subject: Add doc-comments to the new files --- crates/completion/src/render/function.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'crates/completion/src/render/function.rs') diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs index cf3852bf6..9d9bbe309 100644 --- a/crates/completion/src/render/function.rs +++ b/crates/completion/src/render/function.rs @@ -1,3 +1,5 @@ +//! Renderer for function calls. + use hir::{HasSource, Type}; use syntax::{ast::Fn, display::function_declaration}; -- cgit v1.2.3 From 4d333ebb6372c135e5a723da899528cc22d07faa Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Tue, 3 Nov 2020 10:33:13 +0300 Subject: Get rid of do-er antipattern --- crates/completion/src/render/function.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'crates/completion/src/render/function.rs') diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs index 9d9bbe309..4fa6eafd7 100644 --- a/crates/completion/src/render/function.rs +++ b/crates/completion/src/render/function.rs @@ -8,8 +8,16 @@ use crate::{ render::{builder_ext::Params, RenderContext}, }; +pub(crate) fn render_fn<'a>( + ctx: RenderContext<'a>, + local_name: Option, + fn_: hir::Function, +) -> CompletionItem { + FunctionRender::new(ctx, local_name, fn_).render() +} + #[derive(Debug)] -pub(crate) struct FunctionRender<'a> { +struct FunctionRender<'a> { ctx: RenderContext<'a>, name: String, fn_: hir::Function, @@ -17,7 +25,7 @@ pub(crate) struct FunctionRender<'a> { } impl<'a> FunctionRender<'a> { - pub(crate) fn new( + fn new( ctx: RenderContext<'a>, local_name: Option, fn_: hir::Function, @@ -28,7 +36,7 @@ impl<'a> FunctionRender<'a> { FunctionRender { ctx, name, fn_, ast_node } } - pub(crate) fn render(self) -> CompletionItem { + fn render(self) -> CompletionItem { let params = self.params(); CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone()) .kind(self.kind()) -- cgit v1.2.3