aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-10-09 09:53:32 +0100
committerAleksey Kladov <[email protected]>2019-10-09 09:53:32 +0100
commitd9f83d9b16bf871882d7e54419b310e4283c531b (patch)
tree5dc3666436ee7e6a37d804dcd7ef2b25dc4cf7c9 /crates/ra_ide_api/src/completion
parent048be5d9a31e2fb005e93c1a1f648d58167622e8 (diff)
add <> for type aliases as well
Diffstat (limited to 'crates/ra_ide_api/src/completion')
-rw-r--r--crates/ra_ide_api/src/completion/presentation.rs132
1 files changed, 80 insertions, 52 deletions
diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs
index aa4890375..b7807ef8e 100644
--- a/crates/ra_ide_api/src/completion/presentation.rs
+++ b/crates/ra_ide_api/src/completion/presentation.rs
@@ -48,44 +48,52 @@ impl Completions {
48 ScopeDef::ModuleDef(BuiltinType(..)) => CompletionKind::BuiltinType, 48 ScopeDef::ModuleDef(BuiltinType(..)) => CompletionKind::BuiltinType,
49 _ => CompletionKind::Reference, 49 _ => CompletionKind::Reference,
50 }; 50 };
51 let (kind, docs) = match resolution { 51
52 ScopeDef::ModuleDef(Module(it)) => (CompletionItemKind::Module, it.docs(ctx.db)), 52 let kind = match resolution {
53 ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::Module,
53 ScopeDef::ModuleDef(Function(func)) => { 54 ScopeDef::ModuleDef(Function(func)) => {
54 return self.add_function_with_name(ctx, Some(local_name), *func); 55 return self.add_function_with_name(ctx, Some(local_name), *func);
55 } 56 }
56 ScopeDef::ModuleDef(Adt(adt)) => { 57 ScopeDef::ModuleDef(Adt(hir::Adt::Struct(_))) => CompletionItemKind::Struct,
57 return self.add_adt_with_name(ctx, local_name, *adt); 58 // FIXME: add CompletionItemKind::Union
58 } 59 ScopeDef::ModuleDef(Adt(hir::Adt::Union(_))) => CompletionItemKind::Struct,
59 ScopeDef::ModuleDef(EnumVariant(it)) => { 60 ScopeDef::ModuleDef(Adt(hir::Adt::Enum(_))) => CompletionItemKind::Enum,
60 (CompletionItemKind::EnumVariant, it.docs(ctx.db)) 61
61 } 62 ScopeDef::ModuleDef(EnumVariant(..)) => CompletionItemKind::EnumVariant,
62 ScopeDef::ModuleDef(Const(it)) => (CompletionItemKind::Const, it.docs(ctx.db)), 63 ScopeDef::ModuleDef(Const(..)) => CompletionItemKind::Const,
63 ScopeDef::ModuleDef(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)), 64 ScopeDef::ModuleDef(Static(..)) => CompletionItemKind::Static,
64 ScopeDef::ModuleDef(Trait(it)) => (CompletionItemKind::Trait, it.docs(ctx.db)), 65 ScopeDef::ModuleDef(Trait(..)) => CompletionItemKind::Trait,
65 ScopeDef::ModuleDef(TypeAlias(it)) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)), 66 ScopeDef::ModuleDef(TypeAlias(..)) => CompletionItemKind::TypeAlias,
66 ScopeDef::ModuleDef(BuiltinType(..)) => (CompletionItemKind::BuiltinType, None), 67 ScopeDef::ModuleDef(BuiltinType(..)) => CompletionItemKind::BuiltinType,
67 ScopeDef::GenericParam(..) => (CompletionItemKind::TypeParam, None), 68 ScopeDef::GenericParam(..) => CompletionItemKind::TypeParam,
68 ScopeDef::LocalBinding(..) => (CompletionItemKind::Binding, None), 69 ScopeDef::LocalBinding(..) => CompletionItemKind::Binding,
69 ScopeDef::AdtSelfType(..) | ScopeDef::ImplSelfType(..) => ( 70 // (does this need its own kind?)
70 CompletionItemKind::TypeParam, // (does this need its own kind?) 71 ScopeDef::AdtSelfType(..) | ScopeDef::ImplSelfType(..) => CompletionItemKind::TypeParam,
71 None,
72 ),
73 ScopeDef::MacroDef(mac) => { 72 ScopeDef::MacroDef(mac) => {
74 self.add_macro(ctx, Some(local_name), *mac); 73 return self.add_macro(ctx, Some(local_name), *mac);
75 return;
76 } 74 }
77 ScopeDef::Unknown => { 75 ScopeDef::Unknown => {
78 self.add(CompletionItem::new( 76 return self.add(CompletionItem::new(
79 CompletionKind::Reference, 77 CompletionKind::Reference,
80 ctx.source_range(), 78 ctx.source_range(),
81 local_name, 79 local_name,
82 )); 80 ));
83 return;
84 } 81 }
85 }; 82 };
86 83
84 let docs = match resolution {
85 ScopeDef::ModuleDef(Module(it)) => it.docs(ctx.db),
86 ScopeDef::ModuleDef(Adt(it)) => it.docs(ctx.db),
87 ScopeDef::ModuleDef(EnumVariant(it)) => it.docs(ctx.db),
88 ScopeDef::ModuleDef(Const(it)) => it.docs(ctx.db),
89 ScopeDef::ModuleDef(Static(it)) => it.docs(ctx.db),
90 ScopeDef::ModuleDef(Trait(it)) => it.docs(ctx.db),
91 ScopeDef::ModuleDef(TypeAlias(it)) => it.docs(ctx.db),
92 _ => None,
93 };
94
87 let mut completion_item = 95 let mut completion_item =
88 CompletionItem::new(completion_kind, ctx.source_range(), local_name); 96 CompletionItem::new(completion_kind, ctx.source_range(), local_name.clone());
89 if let ScopeDef::LocalBinding(pat_id) = resolution { 97 if let ScopeDef::LocalBinding(pat_id) = resolution {
90 let ty = ctx 98 let ty = ctx
91 .analyzer 99 .analyzer
@@ -94,6 +102,25 @@ impl Completions {
94 .map(|t| t.display(ctx.db).to_string()); 102 .map(|t| t.display(ctx.db).to_string());
95 completion_item = completion_item.set_detail(ty); 103 completion_item = completion_item.set_detail(ty);
96 }; 104 };
105
106 // If not an import, add parenthesis automatically.
107 if ctx.is_path_type
108 && !ctx.has_type_args
109 && ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis")
110 {
111 let generic_def: Option<hir::GenericDef> = match resolution {
112 ScopeDef::ModuleDef(Adt(it)) => Some((*it).into()),
113 ScopeDef::ModuleDef(TypeAlias(it)) => Some((*it).into()),
114 _ => None,
115 };
116 if let Some(def) = generic_def {
117 if has_non_default_type_params(def, ctx.db) {
118 tested_by!(inserts_angle_brackets_for_generics);
119 completion_item = completion_item.insert_snippet(format!("{}<$0>", local_name));
120 }
121 }
122 }
123
97 completion_item.kind(kind).set_documentation(docs).add_to(self) 124 completion_item.kind(kind).set_documentation(docs).add_to(self)
98 } 125 }
99 126
@@ -169,32 +196,6 @@ impl Completions {
169 self.add(builder) 196 self.add(builder)
170 } 197 }
171 198
172 fn add_adt_with_name(&mut self, ctx: &CompletionContext, name: String, adt: hir::Adt) {
173 let mut builder =
174 CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.clone());
175
176 let kind = match adt {
177 hir::Adt::Struct(_) => CompletionItemKind::Struct,
178 // FIXME: add CompletionItemKind::Union
179 hir::Adt::Union(_) => CompletionItemKind::Struct,
180 hir::Adt::Enum(_) => CompletionItemKind::Enum,
181 };
182 let docs = adt.docs(ctx.db);
183
184 // If not an import, add parenthesis automatically.
185 if ctx.is_path_type
186 && !ctx.has_type_args
187 && ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis")
188 {
189 if has_non_default_type_params(adt, ctx.db) {
190 tested_by!(inserts_angle_brackets_for_generics);
191 builder = builder.insert_snippet(format!("{}<$0>", name));
192 }
193 }
194
195 builder.kind(kind).set_documentation(docs).add_to(self)
196 }
197
198 pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { 199 pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
199 let ast_node = constant.source(ctx.db).ast; 200 let ast_node = constant.source(ctx.db).ast;
200 let name = match ast_node.name() { 201 let name = match ast_node.name() {
@@ -243,8 +244,8 @@ impl Completions {
243 } 244 }
244} 245}
245 246
246fn has_non_default_type_params(adt: hir::Adt, db: &db::RootDatabase) -> bool { 247fn has_non_default_type_params(def: hir::GenericDef, db: &db::RootDatabase) -> bool {
247 let subst = db.generic_defaults(adt.into()); 248 let subst = db.generic_defaults(def);
248 subst.iter().any(|ty| ty == &Ty::Unknown) 249 subst.iter().any(|ty| ty == &Ty::Unknown)
249} 250}
250 251
@@ -449,6 +450,33 @@ mod tests {
449 assert_debug_snapshot!( 450 assert_debug_snapshot!(
450 do_reference_completion( 451 do_reference_completion(
451 r" 452 r"
453 type Vec<T> = (T,);
454 fn foo(xs: Ve<|>)
455 "
456 ),
457 @r###"
458 [
459 CompletionItem {
460 label: "Vec",
461 source_range: [64; 66),
462 delete: [64; 66),
463 insert: "Vec<$0>",
464 kind: TypeAlias,
465 },
466 CompletionItem {
467 label: "foo",
468 source_range: [64; 66),
469 delete: [64; 66),
470 insert: "foo($0)",
471 kind: Function,
472 detail: "fn foo(xs: Ve)",
473 },
474 ]
475 "###
476 );
477 assert_debug_snapshot!(
478 do_reference_completion(
479 r"
452 struct Vec<T = i128> {} 480 struct Vec<T = i128> {}
453 fn foo(xs: Ve<|>) 481 fn foo(xs: Ve<|>)
454 " 482 "