aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/completions.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_completion/src/completions.rs')
-rw-r--r--crates/ide_completion/src/completions.rs62
1 files changed, 34 insertions, 28 deletions
diff --git a/crates/ide_completion/src/completions.rs b/crates/ide_completion/src/completions.rs
index ffdcdc930..bd90cefb2 100644
--- a/crates/ide_completion/src/completions.rs
+++ b/crates/ide_completion/src/completions.rs
@@ -6,7 +6,6 @@ pub(crate) mod flyimport;
6pub(crate) mod fn_param; 6pub(crate) mod fn_param;
7pub(crate) mod keyword; 7pub(crate) mod keyword;
8pub(crate) mod lifetime; 8pub(crate) mod lifetime;
9pub(crate) mod macro_in_item_position;
10pub(crate) mod mod_; 9pub(crate) mod mod_;
11pub(crate) mod pattern; 10pub(crate) mod pattern;
12pub(crate) mod postfix; 11pub(crate) mod postfix;
@@ -30,7 +29,7 @@ use crate::{
30 macro_::render_macro, 29 macro_::render_macro,
31 pattern::{render_struct_pat, render_variant_pat}, 30 pattern::{render_struct_pat, render_variant_pat},
32 render_field, render_resolution, render_tuple_field, 31 render_field, render_resolution, render_tuple_field,
33 type_alias::render_type_alias, 32 type_alias::{render_type_alias, render_type_alias_with_eq},
34 RenderContext, 33 RenderContext,
35 }, 34 },
36 CompletionContext, CompletionItem, CompletionItemKind, 35 CompletionContext, CompletionItem, CompletionItemKind,
@@ -57,10 +56,16 @@ impl Builder {
57} 56}
58 57
59impl Completions { 58impl Completions {
60 pub(crate) fn add(&mut self, item: CompletionItem) { 59 fn add(&mut self, item: CompletionItem) {
61 self.buf.push(item) 60 self.buf.push(item)
62 } 61 }
63 62
63 fn add_opt(&mut self, item: Option<CompletionItem>) {
64 if let Some(item) = item {
65 self.buf.push(item)
66 }
67 }
68
64 pub(crate) fn add_all<I>(&mut self, items: I) 69 pub(crate) fn add_all<I>(&mut self, items: I)
65 where 70 where
66 I: IntoIterator, 71 I: IntoIterator,
@@ -104,9 +109,10 @@ impl Completions {
104 local_name: hir::Name, 109 local_name: hir::Name,
105 resolution: &hir::ScopeDef, 110 resolution: &hir::ScopeDef,
106 ) { 111 ) {
107 if let Some(item) = render_resolution(RenderContext::new(ctx), local_name, resolution) { 112 if ctx.expects_type() && resolution.is_value_def() {
108 self.add(item); 113 return;
109 } 114 }
115 self.add_opt(render_resolution(RenderContext::new(ctx), local_name, resolution));
110 } 116 }
111 117
112 pub(crate) fn add_macro( 118 pub(crate) fn add_macro(
@@ -119,9 +125,7 @@ impl Completions {
119 Some(it) => it, 125 Some(it) => it,
120 None => return, 126 None => return,
121 }; 127 };
122 if let Some(item) = render_macro(RenderContext::new(ctx), None, name, macro_) { 128 self.add_opt(render_macro(RenderContext::new(ctx), None, name, macro_));
123 self.add(item);
124 }
125 } 129 }
126 130
127 pub(crate) fn add_function( 131 pub(crate) fn add_function(
@@ -130,9 +134,10 @@ impl Completions {
130 func: hir::Function, 134 func: hir::Function,
131 local_name: Option<hir::Name>, 135 local_name: Option<hir::Name>,
132 ) { 136 ) {
133 if let Some(item) = render_fn(RenderContext::new(ctx), None, local_name, func) { 137 if ctx.expects_type() {
134 self.add(item) 138 return;
135 } 139 }
140 self.add_opt(render_fn(RenderContext::new(ctx), None, local_name, func));
136 } 141 }
137 142
138 pub(crate) fn add_method( 143 pub(crate) fn add_method(
@@ -142,10 +147,7 @@ impl Completions {
142 receiver: Option<hir::Name>, 147 receiver: Option<hir::Name>,
143 local_name: Option<hir::Name>, 148 local_name: Option<hir::Name>,
144 ) { 149 ) {
145 if let Some(item) = render_method(RenderContext::new(ctx), None, receiver, local_name, func) 150 self.add_opt(render_method(RenderContext::new(ctx), None, receiver, local_name, func));
146 {
147 self.add(item)
148 }
149 } 151 }
150 152
151 pub(crate) fn add_variant_pat( 153 pub(crate) fn add_variant_pat(
@@ -154,9 +156,7 @@ impl Completions {
154 variant: hir::Variant, 156 variant: hir::Variant,
155 local_name: Option<hir::Name>, 157 local_name: Option<hir::Name>,
156 ) { 158 ) {
157 if let Some(item) = render_variant_pat(RenderContext::new(ctx), variant, local_name, None) { 159 self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, local_name, None));
158 self.add(item);
159 }
160 } 160 }
161 161
162 pub(crate) fn add_qualified_variant_pat( 162 pub(crate) fn add_qualified_variant_pat(
@@ -165,9 +165,7 @@ impl Completions {
165 variant: hir::Variant, 165 variant: hir::Variant,
166 path: hir::ModPath, 166 path: hir::ModPath,
167 ) { 167 ) {
168 if let Some(item) = render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)) { 168 self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)));
169 self.add(item);
170 }
171 } 169 }
172 170
173 pub(crate) fn add_struct_pat( 171 pub(crate) fn add_struct_pat(
@@ -176,21 +174,26 @@ impl Completions {
176 strukt: hir::Struct, 174 strukt: hir::Struct,
177 local_name: Option<hir::Name>, 175 local_name: Option<hir::Name>,
178 ) { 176 ) {
179 if let Some(item) = render_struct_pat(RenderContext::new(ctx), strukt, local_name) { 177 self.add_opt(render_struct_pat(RenderContext::new(ctx), strukt, local_name));
180 self.add(item);
181 }
182 } 178 }
183 179
184 pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { 180 pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
185 if let Some(item) = render_const(RenderContext::new(ctx), constant) { 181 if ctx.expects_type() {
186 self.add(item); 182 return;
187 } 183 }
184 self.add_opt(render_const(RenderContext::new(ctx), constant));
188 } 185 }
189 186
190 pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) { 187 pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
191 if let Some(item) = render_type_alias(RenderContext::new(ctx), type_alias) { 188 self.add_opt(render_type_alias(RenderContext::new(ctx), type_alias));
192 self.add(item) 189 }
193 } 190
191 pub(crate) fn add_type_alias_with_eq(
192 &mut self,
193 ctx: &CompletionContext,
194 type_alias: hir::TypeAlias,
195 ) {
196 self.add_opt(render_type_alias_with_eq(RenderContext::new(ctx), type_alias));
194 } 197 }
195 198
196 pub(crate) fn add_qualified_enum_variant( 199 pub(crate) fn add_qualified_enum_variant(
@@ -209,6 +212,9 @@ impl Completions {
209 variant: hir::Variant, 212 variant: hir::Variant,
210 local_name: Option<hir::Name>, 213 local_name: Option<hir::Name>,
211 ) { 214 ) {
215 if ctx.expects_type() {
216 return;
217 }
212 let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None); 218 let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None);
213 self.add(item); 219 self.add(item);
214 } 220 }