aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/item.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_completion/src/item.rs')
-rw-r--r--crates/ide_completion/src/item.rs59
1 files changed, 39 insertions, 20 deletions
diff --git a/crates/ide_completion/src/item.rs b/crates/ide_completion/src/item.rs
index 9a4b5217a..cc4ac9ea2 100644
--- a/crates/ide_completion/src/item.rs
+++ b/crates/ide_completion/src/item.rs
@@ -122,7 +122,7 @@ impl fmt::Debug for CompletionItem {
122 } 122 }
123} 123}
124 124
125#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Default)] 125#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
126pub struct CompletionRelevance { 126pub struct CompletionRelevance {
127 /// This is set in cases like these: 127 /// This is set in cases like these:
128 /// 128 ///
@@ -134,31 +134,41 @@ pub struct CompletionRelevance {
134 /// } 134 /// }
135 /// ``` 135 /// ```
136 pub exact_name_match: bool, 136 pub exact_name_match: bool,
137 /// See CompletionRelevanceTypeMatch doc comments for cases where this is set.
138 pub type_match: Option<CompletionRelevanceTypeMatch>,
137 /// This is set in cases like these: 139 /// This is set in cases like these:
138 /// 140 ///
139 /// ``` 141 /// ```
140 /// fn f(spam: String) {} 142 /// fn foo(a: u32) {
141 /// fn main { 143 /// let b = 0;
142 /// let foo = String::new(); 144 /// $0 // `a` and `b` are local
143 /// f($0) // type of local matches the type of param
144 /// } 145 /// }
145 /// ``` 146 /// ```
146 pub exact_type_match: bool, 147 pub is_local: bool,
148}
149
150#[derive(Debug, Clone, Copy, Eq, PartialEq)]
151pub enum CompletionRelevanceTypeMatch {
147 /// This is set in cases like these: 152 /// This is set in cases like these:
148 /// 153 ///
149 /// ``` 154 /// ```
150 /// fn foo(bar: u32) { 155 /// enum Option<T> { Some(T), None }
151 /// $0 // `bar` is local 156 /// fn f(a: Option<u32>) {}
157 /// fn main {
158 /// f(Option::N$0) // type `Option<T>` could unify with `Option<u32>`
152 /// } 159 /// }
153 /// ``` 160 /// ```
161 CouldUnify,
162 /// This is set in cases like these:
154 /// 163 ///
155 /// ``` 164 /// ```
156 /// fn foo() { 165 /// fn f(spam: String) {}
157 /// let bar = 0; 166 /// fn main {
158 /// $0 // `bar` is local 167 /// let foo = String::new();
168 /// f($0) // type of local matches the type of param
159 /// } 169 /// }
160 /// ``` 170 /// ```
161 pub is_local: bool, 171 Exact,
162} 172}
163 173
164impl CompletionRelevance { 174impl CompletionRelevance {
@@ -177,9 +187,11 @@ impl CompletionRelevance {
177 if self.exact_name_match { 187 if self.exact_name_match {
178 score += 1; 188 score += 1;
179 } 189 }
180 if self.exact_type_match { 190 score += match self.type_match {
181 score += 3; 191 Some(CompletionRelevanceTypeMatch::Exact) => 4,
182 } 192 Some(CompletionRelevanceTypeMatch::CouldUnify) => 3,
193 None => 0,
194 };
183 if self.is_local { 195 if self.is_local {
184 score += 1; 196 score += 1;
185 } 197 }
@@ -342,7 +354,7 @@ impl CompletionItem {
342 // match, but with exact type match set because self.ref_match 354 // match, but with exact type match set because self.ref_match
343 // is only set if there is an exact type match. 355 // is only set if there is an exact type match.
344 let mut relevance = self.relevance; 356 let mut relevance = self.relevance;
345 relevance.exact_type_match = true; 357 relevance.type_match = Some(CompletionRelevanceTypeMatch::Exact);
346 358
347 self.ref_match.map(|mutability| (mutability, relevance)) 359 self.ref_match.map(|mutability| (mutability, relevance))
348 } 360 }
@@ -523,7 +535,7 @@ mod tests {
523 use itertools::Itertools; 535 use itertools::Itertools;
524 use test_utils::assert_eq_text; 536 use test_utils::assert_eq_text;
525 537
526 use super::CompletionRelevance; 538 use super::{CompletionRelevance, CompletionRelevanceTypeMatch};
527 539
528 /// Check that these are CompletionRelevance are sorted in ascending order 540 /// Check that these are CompletionRelevance are sorted in ascending order
529 /// by their relevance score. 541 /// by their relevance score.
@@ -576,15 +588,22 @@ mod tests {
576 is_local: true, 588 is_local: true,
577 ..CompletionRelevance::default() 589 ..CompletionRelevance::default()
578 }], 590 }],
579 vec![CompletionRelevance { exact_type_match: true, ..CompletionRelevance::default() }], 591 vec![CompletionRelevance {
592 type_match: Some(CompletionRelevanceTypeMatch::CouldUnify),
593 ..CompletionRelevance::default()
594 }],
595 vec![CompletionRelevance {
596 type_match: Some(CompletionRelevanceTypeMatch::Exact),
597 ..CompletionRelevance::default()
598 }],
580 vec![CompletionRelevance { 599 vec![CompletionRelevance {
581 exact_name_match: true, 600 exact_name_match: true,
582 exact_type_match: true, 601 type_match: Some(CompletionRelevanceTypeMatch::Exact),
583 ..CompletionRelevance::default() 602 ..CompletionRelevance::default()
584 }], 603 }],
585 vec![CompletionRelevance { 604 vec![CompletionRelevance {
586 exact_name_match: true, 605 exact_name_match: true,
587 exact_type_match: true, 606 type_match: Some(CompletionRelevanceTypeMatch::Exact),
588 is_local: true, 607 is_local: true,
589 }], 608 }],
590 ]; 609 ];