From d0a261468e67be9213a3fae20f1511e303cac064 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Feb 2019 18:51:38 +0300 Subject: introduce completion presentation This module should remove completion rendering boilerplate from the "brains" of completion engine. --- crates/ra_hir/src/lib.rs | 2 +- crates/ra_ide_api/src/completion.rs | 1 + crates/ra_ide_api/src/completion/complete_dot.rs | 24 +++------------- .../src/completion/complete_struct_literal.rs | 15 ++-------- crates/ra_ide_api/src/completion/presentation.rs | 33 ++++++++++++++++++++++ 5 files changed, 42 insertions(+), 33 deletions(-) create mode 100644 crates/ra_ide_api/src/completion/presentation.rs diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 2830d4a99..f2e0aae6c 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -55,7 +55,7 @@ pub use self::{ ids::{HirFileId, MacroCallId, MacroCallLoc, HirInterner}, macros::{MacroDef, MacroInput, MacroExpansion}, nameres::{ItemMap, PerNs, Namespace}, - ty::Ty, + ty::{Ty, Substs}, impl_block::{ImplBlock, ImplItem}, docs::{Docs, Documentation}, adt::AdtDef, diff --git a/crates/ra_ide_api/src/completion.rs b/crates/ra_ide_api/src/completion.rs index d92e01bfb..e13baa7a9 100644 --- a/crates/ra_ide_api/src/completion.rs +++ b/crates/ra_ide_api/src/completion.rs @@ -1,5 +1,6 @@ mod completion_item; mod completion_context; +mod presentation; mod complete_dot; mod complete_struct_literal; diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index 20fa323ce..c9099bcd7 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs @@ -1,7 +1,6 @@ -use hir::{Ty, AdtDef, Docs}; +use hir::{Ty, AdtDef}; -use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind}; -use crate::completion::completion_item::CompletionKind; +use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind, CompletionKind}; /// Complete dot accesses, i.e. fields or methods (currently only fields). pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { @@ -29,15 +28,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) match def_id { AdtDef::Struct(s) => { for field in s.fields(ctx.db) { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - field.name(ctx.db).to_string(), - ) - .kind(CompletionItemKind::Field) - .detail(field.ty(ctx.db).subst(substs).to_string()) - .set_documentation(field.docs(ctx.db)) - .add_to(acc); + acc.add_field(CompletionKind::Reference, ctx, field, substs); } } @@ -47,14 +38,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) } Ty::Tuple(fields) => { for (i, ty) in fields.iter().enumerate() { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - i.to_string(), - ) - .kind(CompletionItemKind::Field) - .detail(ty.to_string()) - .add_to(acc); + acc.add_pos_field(CompletionKind::Reference, ctx, i, ty); } } _ => {} diff --git a/crates/ra_ide_api/src/completion/complete_struct_literal.rs b/crates/ra_ide_api/src/completion/complete_struct_literal.rs index f8dd2baad..c617bff5f 100644 --- a/crates/ra_ide_api/src/completion/complete_struct_literal.rs +++ b/crates/ra_ide_api/src/completion/complete_struct_literal.rs @@ -1,7 +1,6 @@ -use hir::{Ty, AdtDef, Docs}; +use hir::{Ty, AdtDef}; -use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind}; -use crate::completion::completion_item::CompletionKind; +use crate::completion::{CompletionContext, Completions, CompletionKind}; /// Complete fields in fields literals. pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionContext) { @@ -23,15 +22,7 @@ pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionCon match adt { AdtDef::Struct(s) => { for field in s.fields(ctx.db) { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - field.name(ctx.db).to_string(), - ) - .kind(CompletionItemKind::Field) - .detail(field.ty(ctx.db).subst(substs).to_string()) - .set_documentation(field.docs(ctx.db)) - .add_to(acc); + acc.add_field(CompletionKind::Reference, ctx, field, substs); } } diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs new file mode 100644 index 000000000..6b2de56bc --- /dev/null +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -0,0 +1,33 @@ +//! This modules takes care of rendering various defenitions as completion items. +use hir::Docs; + +use crate::completion::{Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem}; + +impl Completions { + pub(crate) fn add_field( + &mut self, + kind: CompletionKind, + ctx: &CompletionContext, + field: hir::StructField, + substs: &hir::Substs, + ) { + CompletionItem::new(kind, ctx.source_range(), field.name(ctx.db).to_string()) + .kind(CompletionItemKind::Field) + .detail(field.ty(ctx.db).subst(substs).to_string()) + .set_documentation(field.docs(ctx.db)) + .add_to(self); + } + + pub(crate) fn add_pos_field( + &mut self, + kind: CompletionKind, + ctx: &CompletionContext, + field: usize, + ty: &hir::Ty, + ) { + CompletionItem::new(kind, ctx.source_range(), field.to_string()) + .kind(CompletionItemKind::Field) + .detail(ty.to_string()) + .add_to(self); + } +} -- cgit v1.2.3 From b04cadc02c72055ed97eb5b3c1318f1c3b5602dc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Feb 2019 19:34:27 +0300 Subject: move function rendering to presentation --- crates/ra_ide_api/src/completion/complete_dot.rs | 11 ++---- crates/ra_ide_api/src/completion/complete_path.rs | 9 +---- crates/ra_ide_api/src/completion/presentation.rs | 41 +++++++++++++++++++++- ...r_function_parens_if_already_call_assoc_fn.snap | 6 ++-- .../completion_item__struct_associated_method.snap | 6 ++-- 5 files changed, 49 insertions(+), 24 deletions(-) diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index c9099bcd7..a6b988062 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs @@ -1,6 +1,6 @@ use hir::{Ty, AdtDef}; -use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind, CompletionKind}; +use crate::completion::{CompletionContext, Completions, CompletionKind}; /// Complete dot accesses, i.e. fields or methods (currently only fields). pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { @@ -50,14 +50,7 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty receiver.iterate_methods(ctx.db, |_ty, func| { let sig = func.signature(ctx.db); if sig.has_self_param() { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - sig.name().to_string(), - ) - .from_function(ctx, func) - .kind(CompletionItemKind::Method) - .add_to(acc); + acc.add_function(CompletionKind::Reference, ctx, func); } None::<()> }); diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index d337fe970..f8595b5c4 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -64,14 +64,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { hir::ImplItem::Method(func) => { let sig = func.signature(ctx.db); if !sig.has_self_param() { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - sig.name().to_string(), - ) - .from_function(ctx, func) - .kind(CompletionItemKind::Method) - .add_to(acc); + acc.add_function(CompletionKind::Reference, ctx, func); } None::<()> } diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index 6b2de56bc..9c31a3801 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -1,7 +1,11 @@ //! This modules takes care of rendering various defenitions as completion items. +use test_utils::tested_by; use hir::Docs; -use crate::completion::{Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem}; +use crate::completion::{ + Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem, + function_label, +}; impl Completions { pub(crate) fn add_field( @@ -30,4 +34,39 @@ impl Completions { .detail(ty.to_string()) .add_to(self); } + + pub(crate) fn add_function( + &mut self, + kind: CompletionKind, + ctx: &CompletionContext, + func: hir::Function, + ) { + let sig = func.signature(ctx.db); + + let mut builder = CompletionItem::new(kind, ctx.source_range(), sig.name().to_string()) + .kind(if sig.has_self_param() { + CompletionItemKind::Method + } else { + CompletionItemKind::Function + }) + .set_documentation(func.docs(ctx.db)) + .set_detail(function_item_label(ctx, func)); + // If not an import, add parenthesis automatically. + if ctx.use_item_syntax.is_none() && !ctx.is_call { + tested_by!(inserts_parens_for_function_calls); + let snippet = + if sig.params().is_empty() || sig.has_self_param() && sig.params().len() == 1 { + format!("{}()$0", sig.name()) + } else { + format!("{}($0)", sig.name()) + }; + builder = builder.insert_snippet(snippet); + } + self.add(builder) + } +} + +fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Option { + let node = function.source(ctx.db).1; + function_label(&node) } diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_if_already_call_assoc_fn.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_if_already_call_assoc_fn.snap index 39fff2cb7..19375ea95 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_if_already_call_assoc_fn.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__dont_render_function_parens_if_already_call_assoc_fn.snap @@ -1,6 +1,6 @@ --- -created: "2019-02-18T09:22:24.268227065Z" -creator: insta@0.6.2 +created: "2019-02-24T16:33:48.008220694Z" +creator: insta@0.6.3 source: crates/ra_ide_api/src/completion/completion_item.rs expression: kind_completions --- @@ -10,7 +10,7 @@ expression: kind_completions source_range: [67; 69), delete: [67; 69), insert: "new", - kind: Method, + kind: Function, detail: "fn new() -> Foo" } ] diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_associated_method.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_associated_method.snap index 163b325c3..7c69eebeb 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_associated_method.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_associated_method.snap @@ -1,6 +1,6 @@ --- -created: "2019-02-18T09:22:24.093082839Z" -creator: insta@0.6.2 +created: "2019-02-24T16:33:47.990111169Z" +creator: insta@0.6.3 source: crates/ra_ide_api/src/completion/completion_item.rs expression: kind_completions --- @@ -10,7 +10,7 @@ expression: kind_completions source_range: [100; 100), delete: [100; 100), insert: "m()$0", - kind: Method, + kind: Function, detail: "fn m()", documentation: Documentation( "An associated method" -- cgit v1.2.3 From 3c7c5a7354760a12b38c7186193232d68056a76e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Feb 2019 19:37:22 +0300 Subject: move presentaion completion to presentation --- .../ra_ide_api/src/completion/completion_item.rs | 80 ---------------------- crates/ra_ide_api/src/completion/presentation.rs | 80 ++++++++++++++++++++++ 2 files changed, 80 insertions(+), 80 deletions(-) diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index 95bdd59b4..91e32b3c8 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs @@ -411,83 +411,3 @@ pub(crate) fn check_completion(test_name: &str, code: &str, kind: CompletionKind let kind_completions = do_completion(code, kind); assert_debug_snapshot_matches!(test_name, kind_completions); } - -#[cfg(test)] -mod tests { - use test_utils::covers; - - use super::*; - - fn check_reference_completion(code: &str, expected_completions: &str) { - check_completion(code, expected_completions, CompletionKind::Reference); - } - - #[test] - fn inserts_parens_for_function_calls() { - covers!(inserts_parens_for_function_calls); - check_reference_completion( - "inserts_parens_for_function_calls1", - r" - fn no_args() {} - fn main() { no_<|> } - ", - ); - check_reference_completion( - "inserts_parens_for_function_calls2", - r" - fn with_args(x: i32, y: String) {} - fn main() { with_<|> } - ", - ); - check_reference_completion( - "inserts_parens_for_function_calls3", - r" - struct S {} - impl S { - fn foo(&self) {} - } - fn bar(s: &S) { - s.f<|> - } - ", - ) - } - - #[test] - fn dont_render_function_parens_in_use_item() { - check_reference_completion( - "dont_render_function_parens_in_use_item", - " - //- /lib.rs - mod m { pub fn foo() {} } - use crate::m::f<|>; - ", - ) - } - - #[test] - fn dont_render_function_parens_if_already_call() { - check_reference_completion( - "dont_render_function_parens_if_already_call", - " - //- /lib.rs - fn frobnicate() {} - fn main() { - frob<|>(); - } - ", - ); - check_reference_completion( - "dont_render_function_parens_if_already_call_assoc_fn", - " - //- /lib.rs - struct Foo {} - impl Foo { fn new() -> Foo {} } - fn main() { - Foo::ne<|>(); - } - ", - ) - } - -} diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index 9c31a3801..057dbc21a 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -70,3 +70,83 @@ fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Opti let node = function.source(ctx.db).1; function_label(&node) } + +#[cfg(test)] +mod tests { + use test_utils::covers; + + use crate::completion::{CompletionKind, completion_item::check_completion}; + + fn check_reference_completion(code: &str, expected_completions: &str) { + check_completion(code, expected_completions, CompletionKind::Reference); + } + + #[test] + fn inserts_parens_for_function_calls() { + covers!(inserts_parens_for_function_calls); + check_reference_completion( + "inserts_parens_for_function_calls1", + r" + fn no_args() {} + fn main() { no_<|> } + ", + ); + check_reference_completion( + "inserts_parens_for_function_calls2", + r" + fn with_args(x: i32, y: String) {} + fn main() { with_<|> } + ", + ); + check_reference_completion( + "inserts_parens_for_function_calls3", + r" + struct S {} + impl S { + fn foo(&self) {} + } + fn bar(s: &S) { + s.f<|> + } + ", + ) + } + + #[test] + fn dont_render_function_parens_in_use_item() { + check_reference_completion( + "dont_render_function_parens_in_use_item", + " + //- /lib.rs + mod m { pub fn foo() {} } + use crate::m::f<|>; + ", + ) + } + + #[test] + fn dont_render_function_parens_if_already_call() { + check_reference_completion( + "dont_render_function_parens_if_already_call", + " + //- /lib.rs + fn frobnicate() {} + fn main() { + frob<|>(); + } + ", + ); + check_reference_completion( + "dont_render_function_parens_if_already_call_assoc_fn", + " + //- /lib.rs + struct Foo {} + impl Foo { fn new() -> Foo {} } + fn main() { + Foo::ne<|>(); + } + ", + ) + } + +} -- cgit v1.2.3 From 9af525dbd69bb22c968ba42d758657b2ad9bdd37 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Feb 2019 20:49:55 +0300 Subject: simplify --- crates/ra_ide_api/src/completion/complete_dot.rs | 8 ++-- crates/ra_ide_api/src/completion/complete_path.rs | 2 +- .../src/completion/complete_struct_literal.rs | 4 +- crates/ra_ide_api/src/completion/presentation.rs | 52 ++++++++++------------ 4 files changed, 31 insertions(+), 35 deletions(-) diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index a6b988062..be76b997e 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs @@ -1,6 +1,6 @@ use hir::{Ty, AdtDef}; -use crate::completion::{CompletionContext, Completions, CompletionKind}; +use crate::completion::{CompletionContext, Completions}; /// Complete dot accesses, i.e. fields or methods (currently only fields). pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { @@ -28,7 +28,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) match def_id { AdtDef::Struct(s) => { for field in s.fields(ctx.db) { - acc.add_field(CompletionKind::Reference, ctx, field, substs); + acc.add_field(ctx, field, substs); } } @@ -38,7 +38,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) } Ty::Tuple(fields) => { for (i, ty) in fields.iter().enumerate() { - acc.add_pos_field(CompletionKind::Reference, ctx, i, ty); + acc.add_pos_field(ctx, i, ty); } } _ => {} @@ -50,7 +50,7 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty receiver.iterate_methods(ctx.db, |_ty, func| { let sig = func.signature(ctx.db); if sig.has_self_param() { - acc.add_function(CompletionKind::Reference, ctx, func); + acc.add_function(ctx, func); } None::<()> }); diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index f8595b5c4..d2f65310f 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -64,7 +64,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { hir::ImplItem::Method(func) => { let sig = func.signature(ctx.db); if !sig.has_self_param() { - acc.add_function(CompletionKind::Reference, ctx, func); + acc.add_function(ctx, func); } None::<()> } diff --git a/crates/ra_ide_api/src/completion/complete_struct_literal.rs b/crates/ra_ide_api/src/completion/complete_struct_literal.rs index c617bff5f..1b8fb0768 100644 --- a/crates/ra_ide_api/src/completion/complete_struct_literal.rs +++ b/crates/ra_ide_api/src/completion/complete_struct_literal.rs @@ -1,6 +1,6 @@ use hir::{Ty, AdtDef}; -use crate::completion::{CompletionContext, Completions, CompletionKind}; +use crate::completion::{CompletionContext, Completions}; /// Complete fields in fields literals. pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionContext) { @@ -22,7 +22,7 @@ pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionCon match adt { AdtDef::Struct(s) => { for field in s.fields(ctx.db) { - acc.add_field(CompletionKind::Reference, ctx, field, substs); + acc.add_field(ctx, field, substs); } } diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index 057dbc21a..514f3b539 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -10,47 +10,43 @@ use crate::completion::{ impl Completions { pub(crate) fn add_field( &mut self, - kind: CompletionKind, ctx: &CompletionContext, field: hir::StructField, substs: &hir::Substs, ) { - CompletionItem::new(kind, ctx.source_range(), field.name(ctx.db).to_string()) - .kind(CompletionItemKind::Field) - .detail(field.ty(ctx.db).subst(substs).to_string()) - .set_documentation(field.docs(ctx.db)) - .add_to(self); + CompletionItem::new( + CompletionKind::Reference, + ctx.source_range(), + field.name(ctx.db).to_string(), + ) + .kind(CompletionItemKind::Field) + .detail(field.ty(ctx.db).subst(substs).to_string()) + .set_documentation(field.docs(ctx.db)) + .add_to(self); } - pub(crate) fn add_pos_field( - &mut self, - kind: CompletionKind, - ctx: &CompletionContext, - field: usize, - ty: &hir::Ty, - ) { - CompletionItem::new(kind, ctx.source_range(), field.to_string()) + pub(crate) fn add_pos_field(&mut self, ctx: &CompletionContext, field: usize, ty: &hir::Ty) { + CompletionItem::new(CompletionKind::Reference, ctx.source_range(), field.to_string()) .kind(CompletionItemKind::Field) .detail(ty.to_string()) .add_to(self); } - pub(crate) fn add_function( - &mut self, - kind: CompletionKind, - ctx: &CompletionContext, - func: hir::Function, - ) { + pub(crate) fn add_function(&mut self, ctx: &CompletionContext, func: hir::Function) { let sig = func.signature(ctx.db); - let mut builder = CompletionItem::new(kind, ctx.source_range(), sig.name().to_string()) - .kind(if sig.has_self_param() { - CompletionItemKind::Method - } else { - CompletionItemKind::Function - }) - .set_documentation(func.docs(ctx.db)) - .set_detail(function_item_label(ctx, func)); + let mut builder = CompletionItem::new( + CompletionKind::Reference, + ctx.source_range(), + sig.name().to_string(), + ) + .kind(if sig.has_self_param() { + CompletionItemKind::Method + } else { + CompletionItemKind::Function + }) + .set_documentation(func.docs(ctx.db)) + .set_detail(function_item_label(ctx, func)); // If not an import, add parenthesis automatically. if ctx.use_item_syntax.is_none() && !ctx.is_call { tested_by!(inserts_parens_for_function_calls); -- cgit v1.2.3 From 5887c0e574064934f2b090746a89894efc6c8f8f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Feb 2019 20:56:19 +0300 Subject: move enum-variants to presentation --- crates/ra_ide_api/src/completion/complete_path.rs | 26 +++++------------------ crates/ra_ide_api/src/completion/presentation.rs | 16 ++++++++++++++ 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index d2f65310f..74ea8d600 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -1,9 +1,8 @@ -use join_to_string::join; -use hir::{Docs, Resolution}; +use hir::{Resolution}; use ra_syntax::{AstNode, ast::NameOwner}; use test_utils::tested_by; -use crate::completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext}; +use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionContext}; pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { let path = match &ctx.path_prefix { @@ -39,24 +38,9 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { } } hir::ModuleDef::Enum(e) => { - e.variants(ctx.db).into_iter().for_each(|variant| { - if let Some(name) = variant.name(ctx.db) { - let detail_types = - variant.fields(ctx.db).into_iter().map(|field| field.ty(ctx.db)); - let detail = - join(detail_types).separator(", ").surround_with("(", ")").to_string(); - - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - name.to_string(), - ) - .kind(CompletionItemKind::EnumVariant) - .set_documentation(variant.docs(ctx.db)) - .set_detail(Some(detail)) - .add_to(acc) - } - }); + for variant in e.variants(ctx.db) { + acc.add_enum_variant(ctx, variant); + } } hir::ModuleDef::Struct(s) => { let ty = s.ty(ctx.db); diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index 514f3b539..bc2cb4a0f 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -1,4 +1,5 @@ //! This modules takes care of rendering various defenitions as completion items. +use join_to_string::join; use test_utils::tested_by; use hir::Docs; @@ -60,6 +61,21 @@ impl Completions { } self.add(builder) } + + pub(crate) fn add_enum_variant(&mut self, ctx: &CompletionContext, variant: hir::EnumVariant) { + let name = match variant.name(ctx.db) { + Some(it) => it, + None => return, + }; + let detail_types = variant.fields(ctx.db).into_iter().map(|field| field.ty(ctx.db)); + let detail = join(detail_types).separator(", ").surround_with("(", ")").to_string(); + + CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.to_string()) + .kind(CompletionItemKind::EnumVariant) + .set_documentation(variant.docs(ctx.db)) + .detail(detail) + .add_to(self); + } } fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Option { -- cgit v1.2.3 From 2369af8c829c187aa8c734f4f6b1d6fd452a41ff Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Feb 2019 20:59:12 +0300 Subject: simplify --- crates/ra_ide_api/src/completion/complete_path.rs | 62 +++++++++++------------ 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 74ea8d600..8713fb0f0 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -1,4 +1,4 @@ -use hir::{Resolution}; +use hir::Resolution; use ra_syntax::{AstNode, ast::NameOwner}; use test_utils::tested_by; @@ -44,40 +44,40 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { } hir::ModuleDef::Struct(s) => { let ty = s.ty(ctx.db); - ty.iterate_impl_items(ctx.db, |item| match item { - hir::ImplItem::Method(func) => { - let sig = func.signature(ctx.db); - if !sig.has_self_param() { - acc.add_function(ctx, func); + ty.iterate_impl_items(ctx.db, |item| { + match item { + hir::ImplItem::Method(func) => { + let sig = func.signature(ctx.db); + if !sig.has_self_param() { + acc.add_function(ctx, func); + } } - None::<()> - } - hir::ImplItem::Const(ct) => { - let source = ct.source(ctx.db); - if let Some(name) = source.1.name() { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - name.text().to_string(), - ) - .from_const(ctx, ct) - .add_to(acc); + hir::ImplItem::Const(ct) => { + let source = ct.source(ctx.db); + if let Some(name) = source.1.name() { + CompletionItem::new( + CompletionKind::Reference, + ctx.source_range(), + name.text().to_string(), + ) + .from_const(ctx, ct) + .add_to(acc); + } } - None::<()> - } - hir::ImplItem::Type(ty) => { - let source = ty.source(ctx.db); - if let Some(name) = source.1.name() { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - name.text().to_string(), - ) - .from_type(ctx, ty) - .add_to(acc); + hir::ImplItem::Type(ty) => { + let source = ty.source(ctx.db); + if let Some(name) = source.1.name() { + CompletionItem::new( + CompletionKind::Reference, + ctx.source_range(), + name.text().to_string(), + ) + .from_type(ctx, ty) + .add_to(acc); + } } - None::<()> } + None::<()> }); } _ => return, -- cgit v1.2.3 From a650a93bf5d0217e6f366ffc91ca8b25bfd37be4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Feb 2019 21:21:31 +0300 Subject: move res completion to presentation --- crates/ra_ide_api/src/completion/complete_path.rs | 39 +++------------------- crates/ra_ide_api/src/completion/complete_scope.rs | 8 ++--- crates/ra_ide_api/src/completion/presentation.rs | 36 +++++++++++++++++++- 3 files changed, 42 insertions(+), 41 deletions(-) diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 8713fb0f0..8e453b0e0 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -1,8 +1,8 @@ use hir::Resolution; -use ra_syntax::{AstNode, ast::NameOwner}; +use ra_syntax::AstNode; use test_utils::tested_by; -use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionContext}; +use crate::completion::{Completions, CompletionContext}; pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { let path = match &ctx.path_prefix { @@ -27,14 +27,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { } } } - - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - name.to_string(), - ) - .from_resolution(ctx, &res.def.map(hir::Resolution::Def)) - .add_to(acc); + acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def)); } } hir::ModuleDef::Enum(e) => { @@ -52,30 +45,8 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { acc.add_function(ctx, func); } } - hir::ImplItem::Const(ct) => { - let source = ct.source(ctx.db); - if let Some(name) = source.1.name() { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - name.text().to_string(), - ) - .from_const(ctx, ct) - .add_to(acc); - } - } - hir::ImplItem::Type(ty) => { - let source = ty.source(ctx.db); - if let Some(name) = source.1.name() { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - name.text().to_string(), - ) - .from_type(ctx, ty) - .add_to(acc); - } - } + hir::ImplItem::Const(ct) => acc.add_const(ctx, ct), + hir::ImplItem::Type(ty) => acc.add_type(ctx, ty), } None::<()> }); diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs index eeaf26d93..b44b943ae 100644 --- a/crates/ra_ide_api/src/completion/complete_scope.rs +++ b/crates/ra_ide_api/src/completion/complete_scope.rs @@ -1,4 +1,4 @@ -use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionContext}; +use crate::completion::{Completions, CompletionContext}; pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { if !ctx.is_trivial_path { @@ -6,11 +6,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { } let names = ctx.resolver.all_names(ctx.db); - names.into_iter().for_each(|(name, res)| { - CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.to_string()) - .from_resolution(ctx, &res) - .add_to(acc) - }); + names.into_iter().for_each(|(name, res)| acc.add_resolution(ctx, name.to_string(), &res)); } #[cfg(test)] diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index bc2cb4a0f..232ec80cd 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -1,7 +1,8 @@ //! This modules takes care of rendering various defenitions as completion items. use join_to_string::join; use test_utils::tested_by; -use hir::Docs; +use hir::{Docs, PerNs, Resolution}; +use ra_syntax::ast::NameOwner; use crate::completion::{ Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem, @@ -33,6 +34,17 @@ impl Completions { .add_to(self); } + pub(crate) fn add_resolution( + &mut self, + ctx: &CompletionContext, + local_name: String, + res: &PerNs, + ) { + CompletionItem::new(CompletionKind::Reference, ctx.source_range(), local_name) + .from_resolution(ctx, res) + .add_to(self); + } + pub(crate) fn add_function(&mut self, ctx: &CompletionContext, func: hir::Function) { let sig = func.signature(ctx.db); @@ -62,6 +74,28 @@ impl Completions { self.add(builder) } + pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { + let (_file_id, cosnt_def) = constant.source(ctx.db); + let name = match cosnt_def.name() { + Some(name) => name, + _ => return, + }; + CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) + .from_const(ctx, constant) + .add_to(self); + } + + pub(crate) fn add_type(&mut self, ctx: &CompletionContext, type_alias: hir::Type) { + let (_file_id, type_def) = type_alias.source(ctx.db); + let name = match type_def.name() { + Some(name) => name, + _ => return, + }; + CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) + .from_type(ctx, type_alias) + .add_to(self); + } + pub(crate) fn add_enum_variant(&mut self, ctx: &CompletionContext, variant: hir::EnumVariant) { let name = match variant.name(ctx.db) { Some(it) => it, -- cgit v1.2.3 From b7a78729100aa2068827e5812aec03abd3a5df9a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Feb 2019 21:34:38 +0300 Subject: move more code to presentation --- .../ra_ide_api/src/completion/completion_item.rs | 73 +--------------------- crates/ra_ide_api/src/completion/presentation.rs | 72 ++++++++++++++++----- 2 files changed, 57 insertions(+), 88 deletions(-) diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index 91e32b3c8..cb880d92c 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs @@ -1,13 +1,11 @@ use std::fmt; -use hir::{Docs, Documentation, PerNs, Resolution}; +use hir::{Docs, Documentation}; use ra_syntax::TextRange; use ra_text_edit::{ TextEditBuilder, TextEdit}; -use test_utils::tested_by; use crate::completion::{ completion_context::CompletionContext, - function_label, const_label, type_label }; @@ -255,70 +253,6 @@ impl Builder { self.documentation = docs.map(Into::into); self } - pub(super) fn from_resolution( - mut self, - ctx: &CompletionContext, - resolution: &PerNs, - ) -> Builder { - use hir::ModuleDef::*; - - let def = resolution.as_ref().take_types().or_else(|| resolution.as_ref().take_values()); - let def = match def { - None => return self, - Some(it) => it, - }; - let (kind, docs) = match def { - Resolution::Def(Module(it)) => (CompletionItemKind::Module, it.docs(ctx.db)), - Resolution::Def(Function(func)) => return self.from_function(ctx, *func), - Resolution::Def(Struct(it)) => (CompletionItemKind::Struct, it.docs(ctx.db)), - Resolution::Def(Enum(it)) => (CompletionItemKind::Enum, it.docs(ctx.db)), - Resolution::Def(EnumVariant(it)) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)), - Resolution::Def(Const(it)) => (CompletionItemKind::Const, it.docs(ctx.db)), - Resolution::Def(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)), - Resolution::Def(Trait(it)) => (CompletionItemKind::Trait, it.docs(ctx.db)), - Resolution::Def(Type(it)) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)), - Resolution::GenericParam(..) => (CompletionItemKind::TypeParam, None), - Resolution::LocalBinding(..) => (CompletionItemKind::Binding, None), - Resolution::SelfType(..) => ( - CompletionItemKind::TypeParam, // (does this need its own kind?) - None, - ), - }; - self.kind = Some(kind); - self.documentation = docs; - - self - } - - pub(super) fn from_function( - mut self, - ctx: &CompletionContext, - function: hir::Function, - ) -> Builder { - // If not an import, add parenthesis automatically. - if ctx.use_item_syntax.is_none() && !ctx.is_call { - tested_by!(inserts_parens_for_function_calls); - let sig = function.signature(ctx.db); - if sig.params().is_empty() || sig.has_self_param() && sig.params().len() == 1 { - self.insert_text = Some(format!("{}()$0", self.label)); - } else { - self.insert_text = Some(format!("{}($0)", self.label)); - } - self.insert_text_format = InsertTextFormat::Snippet; - } - - if let Some(docs) = function.docs(ctx.db) { - self.documentation = Some(docs); - } - - if let Some(label) = function_item_label(ctx, function) { - self.detail = Some(label); - } - - self.kind = Some(CompletionItemKind::Function); - self - } - pub(super) fn from_const(mut self, ctx: &CompletionContext, ct: hir::Const) -> Builder { if let Some(docs) = ct.docs(ctx.db) { self.documentation = Some(docs); @@ -373,11 +307,6 @@ impl Into> for Completions { } } -fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Option { - let node = function.source(ctx.db).1; - function_label(&node) -} - fn const_item_label(ctx: &CompletionContext, ct: hir::Const) -> String { let node = ct.source(ctx.db).1; const_label(&node) diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index 232ec80cd..d386288ed 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -38,28 +38,68 @@ impl Completions { &mut self, ctx: &CompletionContext, local_name: String, - res: &PerNs, + resolution: &PerNs, ) { + use hir::ModuleDef::*; + + let def = resolution.as_ref().take_types().or_else(|| resolution.as_ref().take_values()); + let def = match def { + None => { + self.add(CompletionItem::new( + CompletionKind::Reference, + ctx.source_range(), + local_name, + )); + return; + } + Some(it) => it, + }; + let (kind, docs) = match def { + Resolution::Def(Module(it)) => (CompletionItemKind::Module, it.docs(ctx.db)), + Resolution::Def(Function(func)) => { + return self.add_function_with_name(ctx, Some(local_name), *func); + } + Resolution::Def(Struct(it)) => (CompletionItemKind::Struct, it.docs(ctx.db)), + Resolution::Def(Enum(it)) => (CompletionItemKind::Enum, it.docs(ctx.db)), + Resolution::Def(EnumVariant(it)) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)), + Resolution::Def(Const(it)) => (CompletionItemKind::Const, it.docs(ctx.db)), + Resolution::Def(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)), + Resolution::Def(Trait(it)) => (CompletionItemKind::Trait, it.docs(ctx.db)), + Resolution::Def(Type(it)) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)), + Resolution::GenericParam(..) => (CompletionItemKind::TypeParam, None), + Resolution::LocalBinding(..) => (CompletionItemKind::Binding, None), + Resolution::SelfType(..) => ( + CompletionItemKind::TypeParam, // (does this need its own kind?) + None, + ), + }; CompletionItem::new(CompletionKind::Reference, ctx.source_range(), local_name) - .from_resolution(ctx, res) - .add_to(self); + .kind(kind) + .set_documentation(docs) + .add_to(self) } pub(crate) fn add_function(&mut self, ctx: &CompletionContext, func: hir::Function) { - let sig = func.signature(ctx.db); + self.add_function_with_name(ctx, None, func) + } - let mut builder = CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - sig.name().to_string(), - ) - .kind(if sig.has_self_param() { - CompletionItemKind::Method - } else { - CompletionItemKind::Function - }) - .set_documentation(func.docs(ctx.db)) - .set_detail(function_item_label(ctx, func)); + fn add_function_with_name( + &mut self, + ctx: &CompletionContext, + name: Option, + func: hir::Function, + ) { + let sig = func.signature(ctx.db); + let name = name.unwrap_or_else(|| sig.name().to_string()); + + let mut builder = CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name) + .kind(if sig.has_self_param() { + CompletionItemKind::Method + } else { + CompletionItemKind::Function + }) + .set_documentation(func.docs(ctx.db)) + .set_detail(function_item_label(ctx, func)); // If not an import, add parenthesis automatically. if ctx.use_item_syntax.is_none() && !ctx.is_call { tested_by!(inserts_parens_for_function_calls); -- cgit v1.2.3 From 98510ec5d3478f3e9178bbff076532487292d8f8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Feb 2019 21:46:04 +0300 Subject: move the rest of presentation to presentation --- .../ra_ide_api/src/completion/completion_item.rs | 41 ++-------------------- crates/ra_ide_api/src/completion/presentation.rs | 29 +++++++++------ 2 files changed, 20 insertions(+), 50 deletions(-) diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index cb880d92c..f515fcc14 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs @@ -1,14 +1,8 @@ use std::fmt; -use hir::{Docs, Documentation}; +use hir::Documentation; use ra_syntax::TextRange; -use ra_text_edit::{ TextEditBuilder, TextEdit}; - -use crate::completion::{ - completion_context::CompletionContext, - const_label, - type_label -}; +use ra_text_edit::{TextEditBuilder, TextEdit}; /// `CompletionItem` describes a single completion variant in the editor pop-up. /// It is basically a POD with various properties. To construct a @@ -253,27 +247,6 @@ impl Builder { self.documentation = docs.map(Into::into); self } - pub(super) fn from_const(mut self, ctx: &CompletionContext, ct: hir::Const) -> Builder { - if let Some(docs) = ct.docs(ctx.db) { - self.documentation = Some(docs); - } - - self.detail = Some(const_item_label(ctx, ct)); - self.kind = Some(CompletionItemKind::Const); - - self - } - - pub(super) fn from_type(mut self, ctx: &CompletionContext, ty: hir::Type) -> Builder { - if let Some(docs) = ty.docs(ctx.db) { - self.documentation = Some(docs); - } - - self.detail = Some(type_item_label(ctx, ty)); - self.kind = Some(CompletionItemKind::TypeAlias); - - self - } } impl<'a> Into for Builder { @@ -307,16 +280,6 @@ impl Into> for Completions { } } -fn const_item_label(ctx: &CompletionContext, ct: hir::Const) -> String { - let node = ct.source(ctx.db).1; - const_label(&node) -} - -fn type_item_label(ctx: &CompletionContext, ty: hir::Type) -> String { - let node = ty.source(ctx.db).1; - type_label(&node) -} - #[cfg(test)] pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec { use crate::mock_analysis::{single_file_with_position, analysis_and_position}; diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index d386288ed..0ead52032 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -6,7 +6,7 @@ use ra_syntax::ast::NameOwner; use crate::completion::{ Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem, - function_label, + function_label, const_label, type_label, }; impl Completions { @@ -91,6 +91,8 @@ impl Completions { ) { let sig = func.signature(ctx.db); let name = name.unwrap_or_else(|| sig.name().to_string()); + let (_, ast_node) = func.source(ctx.db); + let detail = function_label(&ast_node); let mut builder = CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name) .kind(if sig.has_self_param() { @@ -99,7 +101,7 @@ impl Completions { CompletionItemKind::Function }) .set_documentation(func.docs(ctx.db)) - .set_detail(function_item_label(ctx, func)); + .set_detail(detail); // If not an import, add parenthesis automatically. if ctx.use_item_syntax.is_none() && !ctx.is_call { tested_by!(inserts_parens_for_function_calls); @@ -115,13 +117,18 @@ impl Completions { } pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { - let (_file_id, cosnt_def) = constant.source(ctx.db); - let name = match cosnt_def.name() { + let (_file_id, ast_node) = constant.source(ctx.db); + let name = match ast_node.name() { Some(name) => name, _ => return, }; + let (_, ast_node) = constant.source(ctx.db); + let detail = const_label(&ast_node); + CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) - .from_const(ctx, constant) + .kind(CompletionItemKind::Const) + .set_documentation(constant.docs(ctx.db)) + .detail(detail) .add_to(self); } @@ -131,8 +138,13 @@ impl Completions { Some(name) => name, _ => return, }; + let (_, ast_node) = type_alias.source(ctx.db); + let detail = type_label(&ast_node); + CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) - .from_type(ctx, type_alias) + .kind(CompletionItemKind::TypeAlias) + .set_documentation(type_alias.docs(ctx.db)) + .detail(detail) .add_to(self); } @@ -152,11 +164,6 @@ impl Completions { } } -fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Option { - let node = function.source(ctx.db).1; - function_label(&node) -} - #[cfg(test)] mod tests { use test_utils::covers; -- cgit v1.2.3 From d5f6a5f5e29df3005533502b49f78daae314ac5b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Feb 2019 21:54:13 +0300 Subject: move testing functions --- crates/ra_ide_api/src/completion.rs | 3 +++ crates/ra_ide_api/src/completion/complete_dot.rs | 3 +-- crates/ra_ide_api/src/completion/complete_fn_param.rs | 3 +-- crates/ra_ide_api/src/completion/complete_keyword.rs | 3 +-- crates/ra_ide_api/src/completion/complete_path.rs | 7 ++----- crates/ra_ide_api/src/completion/complete_postfix.rs | 3 +-- crates/ra_ide_api/src/completion/complete_scope.rs | 3 +-- crates/ra_ide_api/src/completion/complete_snippet.rs | 3 +-- crates/ra_ide_api/src/completion/complete_struct_literal.rs | 4 ++-- crates/ra_ide_api/src/completion/presentation.rs | 2 +- 10 files changed, 14 insertions(+), 20 deletions(-) diff --git a/crates/ra_ide_api/src/completion.rs b/crates/ra_ide_api/src/completion.rs index e13baa7a9..fbfd7e3e7 100644 --- a/crates/ra_ide_api/src/completion.rs +++ b/crates/ra_ide_api/src/completion.rs @@ -21,7 +21,10 @@ use crate::{ completion_item::{Completions, CompletionKind}, completion_context::CompletionContext, }, + }; +#[cfg(test)] +use crate::completion::completion_item::{do_completion, check_completion}; pub use crate::completion::completion_item::{CompletionItem, CompletionItemKind, InsertTextFormat}; diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index be76b997e..d5ad2e79f 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs @@ -58,8 +58,7 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty #[cfg(test)] mod tests { - use crate::completion::*; - use crate::completion::completion_item::check_completion; + use crate::completion::{check_completion, CompletionKind}; fn check_ref_completion(name: &str, code: &str) { check_completion(name, code, CompletionKind::Reference); diff --git a/crates/ra_ide_api/src/completion/complete_fn_param.rs b/crates/ra_ide_api/src/completion/complete_fn_param.rs index 4d6416284..ffdc744b2 100644 --- a/crates/ra_ide_api/src/completion/complete_fn_param.rs +++ b/crates/ra_ide_api/src/completion/complete_fn_param.rs @@ -54,8 +54,7 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) #[cfg(test)] mod tests { - use crate::completion::*; - use crate::completion::completion_item::check_completion; + use crate::completion::{check_completion, CompletionKind}; fn check_magic_completion(name: &str, code: &str) { check_completion(name, code, CompletionKind::Magic); diff --git a/crates/ra_ide_api/src/completion/complete_keyword.rs b/crates/ra_ide_api/src/completion/complete_keyword.rs index b667381a7..841c0c554 100644 --- a/crates/ra_ide_api/src/completion/complete_keyword.rs +++ b/crates/ra_ide_api/src/completion/complete_keyword.rs @@ -109,8 +109,7 @@ fn complete_return( #[cfg(test)] mod tests { - use crate::completion::CompletionKind; - use crate::completion::completion_item::check_completion; + use crate::completion::{check_completion, CompletionKind}; fn check_keyword_completion(name: &str, code: &str) { check_completion(name, code, CompletionKind::Keyword); diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 8e453b0e0..629a7ee77 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -57,13 +57,10 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { #[cfg(test)] mod tests { - use crate::completion::{ - CompletionKind, - completion_item::{check_completion, do_completion}, -}; - use test_utils::covers; + use crate::completion::{CompletionKind, check_completion, do_completion}; + fn check_reference_completion(code: &str, expected_completions: &str) { check_completion(code, expected_completions, CompletionKind::Reference); } diff --git a/crates/ra_ide_api/src/completion/complete_postfix.rs b/crates/ra_ide_api/src/completion/complete_postfix.rs index ce3d6ed3c..4dfa5f176 100644 --- a/crates/ra_ide_api/src/completion/complete_postfix.rs +++ b/crates/ra_ide_api/src/completion/complete_postfix.rs @@ -56,8 +56,7 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { #[cfg(test)] mod tests { - use crate::completion::completion_item::CompletionKind; - use crate::completion::completion_item::check_completion; + use crate::completion::{CompletionKind, check_completion}; fn check_snippet_completion(test_name: &str, code: &str) { check_completion(test_name, code, CompletionKind::Postfix); diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs index b44b943ae..6146b7bb6 100644 --- a/crates/ra_ide_api/src/completion/complete_scope.rs +++ b/crates/ra_ide_api/src/completion/complete_scope.rs @@ -11,8 +11,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { #[cfg(test)] mod tests { - use crate::completion::CompletionKind; - use crate::completion::completion_item::check_completion; + use crate::completion::{CompletionKind, check_completion}; fn check_reference_completion(name: &str, code: &str) { check_completion(name, code, CompletionKind::Reference); diff --git a/crates/ra_ide_api/src/completion/complete_snippet.rs b/crates/ra_ide_api/src/completion/complete_snippet.rs index aa3a16fa2..e1df9e625 100644 --- a/crates/ra_ide_api/src/completion/complete_snippet.rs +++ b/crates/ra_ide_api/src/completion/complete_snippet.rs @@ -36,8 +36,7 @@ fn ${1:feature}() { #[cfg(test)] mod tests { - use crate::completion::CompletionKind; - use crate::completion::completion_item::check_completion; + use crate::completion::{CompletionKind, check_completion}; fn check_snippet_completion(name: &str, code: &str) { check_completion(name, code, CompletionKind::Snippet); diff --git a/crates/ra_ide_api/src/completion/complete_struct_literal.rs b/crates/ra_ide_api/src/completion/complete_struct_literal.rs index 1b8fb0768..afb092f59 100644 --- a/crates/ra_ide_api/src/completion/complete_struct_literal.rs +++ b/crates/ra_ide_api/src/completion/complete_struct_literal.rs @@ -34,10 +34,10 @@ pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionCon #[cfg(test)] mod tests { use insta::assert_debug_snapshot_matches; - use crate::completion::{CompletionItem, CompletionKind}; + use crate::completion::{CompletionItem, CompletionKind, do_completion}; fn complete(code: &str) -> Vec { - crate::completion::completion_item::do_completion(code, CompletionKind::Reference) + do_completion(code, CompletionKind::Reference) } #[test] diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index 0ead52032..6454436c9 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -168,7 +168,7 @@ impl Completions { mod tests { use test_utils::covers; - use crate::completion::{CompletionKind, completion_item::check_completion}; + use crate::completion::{CompletionKind, check_completion}; fn check_reference_completion(code: &str, expected_completions: &str) { check_completion(code, expected_completions, CompletionKind::Reference); -- cgit v1.2.3