-
Notifications
You must be signed in to change notification settings - Fork 1.7k
completion relevance consider if types can be unified #8056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bors
merged 2 commits into
rust-lang:master
from
JoshMcguigan:completion-relevance-could-unify
Mar 26, 2021
+124
−43
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,7 +122,7 @@ impl fmt::Debug for CompletionItem { | |
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Default)] | ||
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)] | ||
pub struct CompletionRelevance { | ||
/// This is set in cases like these: | ||
/// | ||
|
@@ -134,31 +134,41 @@ pub struct CompletionRelevance { | |
/// } | ||
/// ``` | ||
pub exact_name_match: bool, | ||
/// See CompletionRelevanceTypeMatch doc comments for cases where this is set. | ||
pub type_match: Option<CompletionRelevanceTypeMatch>, | ||
/// This is set in cases like these: | ||
/// | ||
/// ``` | ||
/// fn f(spam: String) {} | ||
/// fn main { | ||
/// let foo = String::new(); | ||
/// f($0) // type of local matches the type of param | ||
/// fn foo(a: u32) { | ||
/// let b = 0; | ||
/// $0 // `a` and `b` are local | ||
/// } | ||
/// ``` | ||
pub exact_type_match: bool, | ||
pub is_local: bool, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, Eq, PartialEq)] | ||
pub enum CompletionRelevanceTypeMatch { | ||
/// This is set in cases like these: | ||
/// | ||
/// ``` | ||
/// fn foo(bar: u32) { | ||
/// $0 // `bar` is local | ||
/// enum Option<T> { Some(T), None } | ||
/// fn f(a: Option<u32>) {} | ||
/// fn main { | ||
/// f(Option::N$0) // type `Option<T>` could unify with `Option<u32>` | ||
/// } | ||
/// ``` | ||
CouldUnify, | ||
/// This is set in cases like these: | ||
/// | ||
/// ``` | ||
/// fn foo() { | ||
/// let bar = 0; | ||
/// $0 // `bar` is local | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is unrelated to this PR, but combining these two examples has been discussed as something we'd like to do. |
||
/// fn f(spam: String) {} | ||
/// fn main { | ||
/// let foo = String::new(); | ||
/// f($0) // type of local matches the type of param | ||
/// } | ||
/// ``` | ||
pub is_local: bool, | ||
Exact, | ||
} | ||
|
||
impl CompletionRelevance { | ||
|
@@ -177,9 +187,11 @@ impl CompletionRelevance { | |
if self.exact_name_match { | ||
score += 1; | ||
} | ||
if self.exact_type_match { | ||
score += 3; | ||
} | ||
score += match self.type_match { | ||
Some(CompletionRelevanceTypeMatch::Exact) => 4, | ||
Some(CompletionRelevanceTypeMatch::CouldUnify) => 3, | ||
None => 0, | ||
}; | ||
if self.is_local { | ||
score += 1; | ||
} | ||
|
@@ -342,7 +354,7 @@ impl CompletionItem { | |
// match, but with exact type match set because self.ref_match | ||
// is only set if there is an exact type match. | ||
let mut relevance = self.relevance; | ||
relevance.exact_type_match = true; | ||
relevance.type_match = Some(CompletionRelevanceTypeMatch::Exact); | ||
|
||
self.ref_match.map(|mutability| (mutability, relevance)) | ||
} | ||
|
@@ -523,7 +535,7 @@ mod tests { | |
use itertools::Itertools; | ||
use test_utils::assert_eq_text; | ||
|
||
use super::CompletionRelevance; | ||
use super::{CompletionRelevance, CompletionRelevanceTypeMatch}; | ||
|
||
/// Check that these are CompletionRelevance are sorted in ascending order | ||
/// by their relevance score. | ||
|
@@ -576,15 +588,22 @@ mod tests { | |
is_local: true, | ||
..CompletionRelevance::default() | ||
}], | ||
vec![CompletionRelevance { exact_type_match: true, ..CompletionRelevance::default() }], | ||
vec![CompletionRelevance { | ||
type_match: Some(CompletionRelevanceTypeMatch::CouldUnify), | ||
..CompletionRelevance::default() | ||
}], | ||
vec![CompletionRelevance { | ||
type_match: Some(CompletionRelevanceTypeMatch::Exact), | ||
..CompletionRelevance::default() | ||
}], | ||
vec![CompletionRelevance { | ||
exact_name_match: true, | ||
exact_type_match: true, | ||
type_match: Some(CompletionRelevanceTypeMatch::Exact), | ||
..CompletionRelevance::default() | ||
}], | ||
vec![CompletionRelevance { | ||
exact_name_match: true, | ||
exact_type_match: true, | ||
type_match: Some(CompletionRelevanceTypeMatch::Exact), | ||
is_local: true, | ||
}], | ||
]; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1138,7 +1138,7 @@ mod tests { | |
( | ||
"&arg", | ||
Some( | ||
"fffffffa", | ||
"fffffff9", | ||
), | ||
), | ||
( | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These weren't used, because
CompletionRelevance
should only be compared by score. I could re-implement them in terms of score, but that would require updating theEq
impl which we use in a different way, so it would broaden the scope of this PR a bit.The reason I removed these at all in this PR is because it didn't make sense to me to derive them on the new enum
CompletionRelevanceTypeMatch
.