From 2f85a640a2698fbd31155172218a91d2e986fd47 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Oct 2019 20:32:19 +0300 Subject: refactor --- crates/ra_ide_api/src/completion/presentation.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index eb480a775..9946db4ca 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -50,14 +50,8 @@ impl Completions { ScopeDef::ModuleDef(Function(func)) => { return self.add_function_with_name(ctx, Some(local_name), *func); } - ScopeDef::ModuleDef(Adt(hir::Adt::Struct(it))) => { - (CompletionItemKind::Struct, it.docs(ctx.db)) - } - ScopeDef::ModuleDef(Adt(hir::Adt::Union(it))) => { - (CompletionItemKind::Struct, it.docs(ctx.db)) - } - ScopeDef::ModuleDef(Adt(hir::Adt::Enum(it))) => { - (CompletionItemKind::Enum, it.docs(ctx.db)) + ScopeDef::ModuleDef(Adt(adt)) => { + return self.add_adt_with_name(ctx, local_name, *adt); } ScopeDef::ModuleDef(EnumVariant(it)) => { (CompletionItemKind::EnumVariant, it.docs(ctx.db)) @@ -173,6 +167,19 @@ impl Completions { self.add(builder) } + fn add_adt_with_name(&mut self, ctx: &CompletionContext, name: String, adt: hir::Adt) { + let builder = CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name); + + let (kind, docs) = match adt { + hir::Adt::Struct(it) => (CompletionItemKind::Struct, it.docs(ctx.db)), + // FIXME: add CompletionItemKind::Union + hir::Adt::Union(it) => (CompletionItemKind::Struct, it.docs(ctx.db)), + hir::Adt::Enum(it) => (CompletionItemKind::Enum, it.docs(ctx.db)), + }; + + builder.kind(kind).set_documentation(docs).add_to(self) + } + pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { let ast_node = constant.source(ctx.db).ast; let name = match ast_node.name() { -- cgit v1.2.3 From f4fa98b1bffe2200580506b83af5383555e1a958 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Oct 2019 20:34:30 +0300 Subject: simplify --- crates/ra_ide_api/src/completion/presentation.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index 9946db4ca..2d670372e 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -170,12 +170,13 @@ impl Completions { fn add_adt_with_name(&mut self, ctx: &CompletionContext, name: String, adt: hir::Adt) { let builder = CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name); - let (kind, docs) = match adt { - hir::Adt::Struct(it) => (CompletionItemKind::Struct, it.docs(ctx.db)), + let kind = match adt { + hir::Adt::Struct(_) => CompletionItemKind::Struct, // FIXME: add CompletionItemKind::Union - hir::Adt::Union(it) => (CompletionItemKind::Struct, it.docs(ctx.db)), - hir::Adt::Enum(it) => (CompletionItemKind::Enum, it.docs(ctx.db)), + hir::Adt::Union(_) => CompletionItemKind::Struct, + hir::Adt::Enum(_) => CompletionItemKind::Enum, }; + let docs = adt.docs(ctx.db); builder.kind(kind).set_documentation(docs).add_to(self) } -- cgit v1.2.3 From e0b1c17dcb367a3edfd9df4b1d7cfbcd059cd207 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Oct 2019 21:14:52 +0300 Subject: add `<>` when completing generic types --- crates/ra_ide_api/src/completion/complete_scope.rs | 154 +++++++++++---------- .../src/completion/completion_context.rs | 7 + crates/ra_ide_api/src/completion/presentation.rs | 114 ++++++++++++++- crates/ra_ide_api/src/marks.rs | 1 + 4 files changed, 198 insertions(+), 78 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs index 38a6c3d37..84826cdcc 100644 --- a/crates/ra_ide_api/src/completion/complete_scope.rs +++ b/crates/ra_ide_api/src/completion/complete_scope.rs @@ -290,22 +290,24 @@ mod tests { } " ), - @r###"[ - CompletionItem { - label: "T", - source_range: [54; 54), - delete: [54; 54), - insert: "T", - kind: TypeParam, - }, - CompletionItem { - label: "X", - source_range: [54; 54), - delete: [54; 54), - insert: "X", - kind: Struct, - }, -]"### + @r###" + [ + CompletionItem { + label: "T", + source_range: [54; 54), + delete: [54; 54), + insert: "T", + kind: TypeParam, + }, + CompletionItem { + label: "X", + source_range: [54; 54), + delete: [54; 54), + insert: "X<$0>", + kind: Struct, + }, + ] + "### ); } @@ -319,22 +321,24 @@ mod tests { } " ), - @r###"[ - CompletionItem { - label: "Self", - source_range: [48; 48), - delete: [48; 48), - insert: "Self", - kind: TypeParam, - }, - CompletionItem { - label: "X", - source_range: [48; 48), - delete: [48; 48), - insert: "X", - kind: Enum, - }, -]"### + @r###" + [ + CompletionItem { + label: "Self", + source_range: [48; 48), + delete: [48; 48), + insert: "Self", + kind: TypeParam, + }, + CompletionItem { + label: "X", + source_range: [48; 48), + delete: [48; 48), + insert: "X", + kind: Enum, + }, + ] + "### ); } @@ -442,23 +446,25 @@ mod tests { fn x() -> <|> " ), - @r###"[ - CompletionItem { - label: "Foo", - source_range: [55; 55), - delete: [55; 55), - insert: "Foo", - kind: Struct, - }, - CompletionItem { - label: "x", - source_range: [55; 55), - delete: [55; 55), - insert: "x()$0", - kind: Function, - detail: "fn x()", - }, -]"### + @r###" + [ + CompletionItem { + label: "Foo", + source_range: [55; 55), + delete: [55; 55), + insert: "Foo", + kind: Struct, + }, + CompletionItem { + label: "x", + source_range: [55; 55), + delete: [55; 55), + insert: "x()$0", + kind: Function, + detail: "fn x()", + }, + ] + "### ); } @@ -538,30 +544,32 @@ mod tests { } " ), - @r#"[ - CompletionItem { - label: "Option", - source_range: [18; 18), - delete: [18; 18), - insert: "Option", - kind: Struct, - }, - CompletionItem { - label: "foo", - source_range: [18; 18), - delete: [18; 18), - insert: "foo()$0", - kind: Function, - detail: "fn foo()", - }, - CompletionItem { - label: "std", - source_range: [18; 18), - delete: [18; 18), - insert: "std", - kind: Module, - }, -]"# + @r###" + [ + CompletionItem { + label: "Option", + source_range: [18; 18), + delete: [18; 18), + insert: "Option", + kind: Struct, + }, + CompletionItem { + label: "foo", + source_range: [18; 18), + delete: [18; 18), + insert: "foo()$0", + kind: Function, + detail: "fn foo()", + }, + CompletionItem { + label: "std", + source_range: [18; 18), + delete: [18; 18), + insert: "std", + kind: Module, + }, + ] + "### ); } diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs index e9ad06965..73f3f3960 100644 --- a/crates/ra_ide_api/src/completion/completion_context.rs +++ b/crates/ra_ide_api/src/completion/completion_context.rs @@ -40,6 +40,8 @@ pub(crate) struct CompletionContext<'a> { pub(super) dot_receiver: Option, /// If this is a call (method or function) in particular, i.e. the () are already there. pub(super) is_call: bool, + pub(super) is_path_type: bool, + pub(super) has_type_args: bool, } impl<'a> CompletionContext<'a> { @@ -76,6 +78,8 @@ impl<'a> CompletionContext<'a> { is_new_item: false, dot_receiver: None, is_call: false, + is_path_type: false, + has_type_args: false, }; ctx.fill(&original_parse, position.offset); Some(ctx) @@ -176,6 +180,9 @@ impl<'a> CompletionContext<'a> { .and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast)) .is_some(); + self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some(); + self.has_type_args = segment.type_arg_list().is_some(); + if let Some(mut path) = hir::Path::from_ast(path.clone()) { if !path.is_ident() { path.segments.pop().unwrap(); diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index 2d670372e..400a266a2 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -1,12 +1,12 @@ //! This modules takes care of rendering various definitions as completion items. -use hir::{Docs, HasSource, HirDisplay, ScopeDef, Ty, TypeWalk}; +use hir::{db::HirDatabase, Docs, HasSource, HirDisplay, ScopeDef, Ty, TypeWalk}; use join_to_string::join; use ra_syntax::ast::NameOwner; use test_utils::tested_by; use crate::completion::{ - CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions, + db, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions, }; use crate::display::{const_label, function_label, macro_label, type_label}; @@ -150,7 +150,8 @@ impl Completions { }) .set_documentation(func.docs(ctx.db)) .detail(detail); - // If not an import, add parenthesis automatically. + + // Add `<>` for generic types if ctx.use_item_syntax.is_none() && !ctx.is_call && ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis") @@ -164,11 +165,13 @@ impl Completions { }; builder = builder.insert_snippet(snippet); } + self.add(builder) } fn add_adt_with_name(&mut self, ctx: &CompletionContext, name: String, adt: hir::Adt) { - let builder = CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name); + let mut builder = + CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.clone()); let kind = match adt { hir::Adt::Struct(_) => CompletionItemKind::Struct, @@ -178,6 +181,17 @@ impl Completions { }; let docs = adt.docs(ctx.db); + // If not an import, add parenthesis automatically. + if ctx.is_path_type + && !ctx.has_type_args + && ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis") + { + if has_non_default_type_params(adt, ctx.db) { + tested_by!(inserts_angle_brackets_for_generics); + builder = builder.insert_snippet(format!("{}<$0>", name)); + } + } + builder.kind(kind).set_documentation(docs).add_to(self) } @@ -221,7 +235,6 @@ impl Completions { .separator(", ") .surround_with("(", ")") .to_string(); - CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.to_string()) .kind(CompletionItemKind::EnumVariant) .set_documentation(variant.docs(ctx.db)) @@ -230,6 +243,11 @@ impl Completions { } } +fn has_non_default_type_params(adt: hir::Adt, db: &db::RootDatabase) -> bool { + let subst = db.generic_defaults(adt.into()); + subst.iter().any(|ty| ty == &Ty::Unknown) +} + #[cfg(test)] mod tests { use crate::completion::{do_completion, CompletionItem, CompletionKind}; @@ -397,4 +415,90 @@ mod tests { ]"# ); } + + #[test] + fn inserts_angle_brackets_for_generics() { + covers!(inserts_angle_brackets_for_generics); + assert_debug_snapshot!( + do_reference_completion( + r" + struct Vec {} + fn foo(xs: Ve<|>) + " + ), + @r###" + [ + CompletionItem { + label: "Vec", + source_range: [61; 63), + delete: [61; 63), + insert: "Vec<$0>", + kind: Struct, + }, + CompletionItem { + label: "foo", + source_range: [61; 63), + delete: [61; 63), + insert: "foo($0)", + kind: Function, + detail: "fn foo(xs: Ve)", + }, + ] + "### + ); + assert_debug_snapshot!( + do_reference_completion( + r" + struct Vec {} + fn foo(xs: Ve<|>) + " + ), + @r###" + [ + CompletionItem { + label: "Vec", + source_range: [68; 70), + delete: [68; 70), + insert: "Vec", + kind: Struct, + }, + CompletionItem { + label: "foo", + source_range: [68; 70), + delete: [68; 70), + insert: "foo($0)", + kind: Function, + detail: "fn foo(xs: Ve)", + }, + ] + "### + ); + assert_debug_snapshot!( + do_reference_completion( + r" + struct Vec {} + fn foo(xs: Ve<|>) + " + ), + @r###" + [ + CompletionItem { + label: "Vec", + source_range: [61; 63), + delete: [61; 63), + insert: "Vec", + kind: Struct, + }, + CompletionItem { + label: "foo", + source_range: [61; 63), + delete: [61; 63), + insert: "foo($0)", + kind: Function, + detail: "fn foo(xs: Ve)", + }, + ] + "### + ); + } } diff --git a/crates/ra_ide_api/src/marks.rs b/crates/ra_ide_api/src/marks.rs index 3f4ba248b..848ae4dc7 100644 --- a/crates/ra_ide_api/src/marks.rs +++ b/crates/ra_ide_api/src/marks.rs @@ -1,6 +1,7 @@ //! See test_utils/src/marks.rs test_utils::marks!( + inserts_angle_brackets_for_generics inserts_parens_for_function_calls goto_definition_works_for_macros goto_definition_works_for_methods -- cgit v1.2.3