aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-04-24 00:17:33 +0100
committerAleksey Kladov <[email protected]>2020-04-24 00:17:33 +0100
commit953b5f23cc493d75288cec1347ab65dd3ed38fd7 (patch)
treef04eeb12188d2f19522043a26ecff68442018dde /crates/ra_ide/src
parent6654b9aff31dccdbc2284598deacc95c67b3672c (diff)
Restore CompletionItem immutability
Diffstat (limited to 'crates/ra_ide/src')
-rw-r--r--crates/ra_ide/src/completion/completion_item.rs7
-rw-r--r--crates/ra_ide/src/completion/presentation.rs47
2 files changed, 23 insertions, 31 deletions
diff --git a/crates/ra_ide/src/completion/completion_item.rs b/crates/ra_ide/src/completion/completion_item.rs
index 687b380bb..edbf4a5b7 100644
--- a/crates/ra_ide/src/completion/completion_item.rs
+++ b/crates/ra_ide/src/completion/completion_item.rs
@@ -190,7 +190,7 @@ impl CompletionItem {
190 } 190 }
191 /// What string is used for filtering. 191 /// What string is used for filtering.
192 pub fn lookup(&self) -> &str { 192 pub fn lookup(&self) -> &str {
193 self.lookup.as_deref().unwrap_or_else(|| self.label()) 193 self.lookup.as_deref().unwrap_or(&self.label)
194 } 194 }
195 195
196 pub fn kind(&self) -> Option<CompletionItemKind> { 196 pub fn kind(&self) -> Option<CompletionItemKind> {
@@ -205,10 +205,6 @@ impl CompletionItem {
205 self.score 205 self.score
206 } 206 }
207 207
208 pub fn set_score(&mut self, score: CompletionScore) {
209 self.score = Some(score);
210 }
211
212 pub fn trigger_call_info(&self) -> bool { 208 pub fn trigger_call_info(&self) -> bool {
213 self.trigger_call_info 209 self.trigger_call_info
214 } 210 }
@@ -310,7 +306,6 @@ impl Builder {
310 self.deprecated = Some(deprecated); 306 self.deprecated = Some(deprecated);
311 self 307 self
312 } 308 }
313 #[allow(unused)]
314 pub(crate) fn set_score(mut self, score: CompletionScore) -> Builder { 309 pub(crate) fn set_score(mut self, score: CompletionScore) -> Builder {
315 self.score = Some(score); 310 self.score = Some(score);
316 self 311 self
diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs
index 5aedfc47f..6d8a5dc08 100644
--- a/crates/ra_ide/src/completion/presentation.rs
+++ b/crates/ra_ide/src/completion/presentation.rs
@@ -23,22 +23,20 @@ impl Completions {
23 ty: &Type, 23 ty: &Type,
24 ) { 24 ) {
25 let is_deprecated = is_deprecated(field, ctx.db); 25 let is_deprecated = is_deprecated(field, ctx.db);
26 let mut completion_item = CompletionItem::new( 26 let ty = ty.display(ctx.db).to_string();
27 CompletionKind::Reference, 27 let name = field.name(ctx.db);
28 ctx.source_range(), 28 let mut completion_item =
29 field.name(ctx.db).to_string(), 29 CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.to_string())
30 ) 30 .kind(CompletionItemKind::Field)
31 .kind(CompletionItemKind::Field) 31 .detail(ty.clone())
32 .detail(ty.display(ctx.db).to_string()) 32 .set_documentation(field.docs(ctx.db))
33 .set_documentation(field.docs(ctx.db)) 33 .set_deprecated(is_deprecated);
34 .set_deprecated(is_deprecated) 34
35 .build(); 35 if let Some(score) = compute_score(ctx, &ty, &name.to_string()) {
36 36 completion_item = completion_item.set_score(score);
37 if let Some(score) = compute_score(&completion_item, ctx) {
38 completion_item.set_score(score);
39 } 37 }
40 38
41 self.add(completion_item); 39 completion_item.add_to(self);
42 } 40 }
43 41
44 pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &Type) { 42 pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &Type) {
@@ -308,8 +306,9 @@ impl Completions {
308} 306}
309 307
310pub(crate) fn compute_score( 308pub(crate) fn compute_score(
311 completion_item: &CompletionItem,
312 ctx: &CompletionContext, 309 ctx: &CompletionContext,
310 ty: &str,
311 name: &str,
313) -> Option<CompletionScore> { 312) -> Option<CompletionScore> {
314 let (active_name, active_type) = if let Some(record_field) = &ctx.record_field_syntax { 313 let (active_name, active_type) = if let Some(record_field) = &ctx.record_field_syntax {
315 if let Some((struct_field, _)) = ctx.sema.resolve_record_field(record_field) { 314 if let Some((struct_field, _)) = ctx.sema.resolve_record_field(record_field) {
@@ -334,16 +333,14 @@ pub(crate) fn compute_score(
334 333
335 // Compute score 334 // Compute score
336 // For the same type 335 // For the same type
337 if let Some(a_parameter_type) = completion_item.detail() { 336 if &active_type == ty {
338 if &active_type == a_parameter_type { 337 // If same type + same name then go top position
339 // If same type + same name then go top position 338 let res = if active_name == name {
340 let res = if active_name == completion_item.label() { 339 CompletionScore::TypeAndNameMatch
341 CompletionScore::TypeAndNameMatch 340 } else {
342 } else { 341 CompletionScore::TypeMatch
343 CompletionScore::TypeMatch 342 };
344 }; 343 return Some(res);
345 return Some(res);
346 }
347 } 344 }
348 None 345 None
349} 346}